1/* Test the TGSI_SEMANTIC_FACE fragment shader input.
2 */
3
4#include <stdio.h>
5
6#include "graw_util.h"
7
8#include "util/macros.h"
9
10
11static int width = 300;
12static int height = 300;
13
14static struct graw_info info;
15
16struct vertex {
17   float position[4];
18   float color[4];
19};
20
21#define z0 0.2
22#define z01 0.5
23#define z1 0.4
24
25static struct vertex vertices[] =
26{
27   /* left quad: clock-wise, front-facing, red */
28   {
29      {-0.8, -0.9, z0, 1.0 },
30      { 0, 0, 0, 1 }
31   },
32
33   {
34      { -0.2, -0.9, z0, 1.0 },
35      { 0, 0, 0, 1 }
36   },
37
38   {
39      { 0.2,  0.9, z01, 1.0 },
40      { 0, 0, 0, 1 }
41   },
42
43   {
44      {-0.9,  0.9, z01, 1.0 },
45      { 0, 0, 0, 1 }
46   },
47
48   /* right quad : counter-clock-wise, back-facing, green */
49   {
50      { 0.2,  -0.9, z1, 1.0 },
51      { 1, 1, 1, -1 }
52   },
53
54   {
55      { -0.2,  0.8, z1, 1.0 },
56      { 1, 1, 1, -1 }
57   },
58
59   {
60      { 0.9,  0.8, z1, 1.0 },
61      { 1, 1, 1, -1 }
62   },
63
64   {
65      { 0.8, -0.9, z1, 1.0 },
66      { 1, 1, 1, -1 }
67   },
68};
69
70#define NUM_VERTS ARRAY_SIZE(vertices)
71
72
73
74static void
75set_vertices(void)
76{
77   struct pipe_vertex_element ve[2];
78   struct pipe_vertex_buffer vbuf;
79   void *handle;
80
81   memset(ve, 0, sizeof ve);
82
83   ve[0].src_offset = Offset(struct vertex, position);
84   ve[0].src_format = PIPE_FORMAT_R32G32B32A32_FLOAT;
85   ve[1].src_offset = Offset(struct vertex, color);
86   ve[1].src_format = PIPE_FORMAT_R32G32B32A32_FLOAT;
87
88   handle = info.ctx->create_vertex_elements_state(info.ctx, 2, ve);
89   info.ctx->bind_vertex_elements_state(info.ctx, handle);
90
91   memset(&vbuf, 0, sizeof vbuf);
92
93   vbuf.stride = sizeof(struct vertex);
94   vbuf.buffer_offset = 0;
95   vbuf.buffer.resource = pipe_buffer_create_with_data(info.ctx,
96                                              PIPE_BIND_VERTEX_BUFFER,
97                                              PIPE_USAGE_DEFAULT,
98                                              sizeof(vertices),
99                                              vertices);
100
101   info.ctx->set_vertex_buffers(info.ctx, 0, 1, &vbuf);
102}
103
104
105static void
106set_vertex_shader(void)
107{
108   void *handle;
109   const char *text =
110      "VERT\n"
111      "DCL IN[0]\n"
112      "DCL IN[1]\n"
113      "DCL OUT[0], POSITION\n"
114      "DCL OUT[1], GENERIC[0]\n"
115      "  0: MOV OUT[0], IN[0]\n"
116      "  1: MOV OUT[1], IN[1]\n"
117      "  2: END\n";
118
119   handle = graw_parse_vertex_shader(info.ctx, text);
120   info.ctx->bind_vs_state(info.ctx, handle);
121}
122
123
124static void
125set_fragment_shader(void)
126{
127   void *handle;
128   const char *text =
129      "FRAG\n"
130      "DCL IN[0], FACE, CONSTANT\n"
131      "DCL IN[1], GENERIC, CONSTANT\n"
132      "DCL OUT[0], COLOR\n"
133      "DCL TEMP[0]\n"
134      "IMM FLT32 {    1.0,     0.0,     0.0,     0.0 }\n"
135      "IMM FLT32 {    0.0,     1.0,     0.0,     0.0 }\n"
136      "IMM FLT32 {    0.5,     0.6,     0.0,     0.0 }\n"
137      " 0: SGT TEMP[0].x, IN[0].xxxx, IMM[1].xxxx\n"  /* TMP[0].x = IN[0].x > 0.0 */
138      " 1: IF TEMP[0].xxxx :4\n"
139      " 2:   MOV OUT[0], IMM[0]\n"    /* front-facing: red */
140      " 3: ELSE :5\n"
141      " 4:   MOV OUT[0], IMM[1]\n"    /* back-facing: green */
142      " 5: ENDIF\n"
143      " 6: END\n";
144
145   handle = graw_parse_fragment_shader(info.ctx, text);
146   info.ctx->bind_fs_state(info.ctx, handle);
147}
148
149
150static void
151draw(void)
152{
153   union pipe_color_union clear_color;
154
155   clear_color.f[0] = 0.25;
156   clear_color.f[1] = 0.25;
157   clear_color.f[2] = 0.25;
158   clear_color.f[3] = 1.00;
159
160   info.ctx->clear(info.ctx,
161              PIPE_CLEAR_COLOR | PIPE_CLEAR_DEPTHSTENCIL,
162              &clear_color, 1.0, 0);
163   util_draw_arrays(info.ctx, PIPE_PRIM_QUADS, 0, NUM_VERTS);
164   info.ctx->flush(info.ctx, NULL, 0);
165
166   graw_util_flush_front(&info);
167}
168
169
170#if 0
171static void
172resize(int w, int h)
173{
174   width = w;
175   height = h;
176
177   set_viewport(0, 0, width, height, 30, 1000);
178}
179#endif
180
181
182static void
183init(void)
184{
185   if (!graw_util_create_window(&info, width, height, 1, TRUE))
186      exit(1);
187
188   graw_util_default_state(&info, TRUE);
189
190   graw_util_viewport(&info, 0, 0, width, height, -1.0, 1.0);
191
192   set_vertices();
193   set_vertex_shader();
194   set_fragment_shader();
195}
196
197
198int
199main(int argc, char *argv[])
200{
201   init();
202
203   printf("Left quad: clock-wise, front-facing, red\n");
204   printf("Right quad: counter clock-wise, back-facing, green\n");
205
206   graw_set_display_func(draw);
207   /*graw_set_reshape_func(resize);*/
208   graw_main_loop();
209   return 0;
210}
211