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