fs-frontface.c revision af69d88d
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   memset(&vbuf, 0, sizeof vbuf);
90
91   vbuf.stride = sizeof(struct vertex);
92   vbuf.buffer_offset = 0;
93   vbuf.buffer = pipe_buffer_create_with_data(info.ctx,
94                                              PIPE_BIND_VERTEX_BUFFER,
95                                              PIPE_USAGE_DEFAULT,
96                                              sizeof(vertices),
97                                              vertices);
98
99   info.ctx->set_vertex_buffers(info.ctx, 0, 1, &vbuf);
100}
101
102
103static void
104set_vertex_shader(void)
105{
106   void *handle;
107   const char *text =
108      "VERT\n"
109      "DCL IN[0]\n"
110      "DCL IN[1]\n"
111      "DCL OUT[0], POSITION\n"
112      "DCL OUT[1], GENERIC[0]\n"
113      "  0: MOV OUT[0], IN[0]\n"
114      "  1: MOV OUT[1], IN[1]\n"
115      "  2: END\n";
116
117   handle = graw_parse_vertex_shader(info.ctx, text);
118   info.ctx->bind_vs_state(info.ctx, handle);
119}
120
121
122static void
123set_fragment_shader(void)
124{
125   void *handle;
126   const char *text =
127      "FRAG\n"
128      "DCL IN[0], FACE, CONSTANT\n"
129      "DCL IN[1], GENERIC, CONSTANT\n"
130      "DCL OUT[0], COLOR\n"
131      "DCL TEMP[0]\n"
132      "IMM FLT32 {    1.0,     0.0,     0.0,     0.0 }\n"
133      "IMM FLT32 {    0.0,     1.0,     0.0,     0.0 }\n"
134      "IMM FLT32 {    0.5,     0.6,     0.0,     0.0 }\n"
135      " 0: SGT TEMP[0].x, IN[0].xxxx, IMM[1].xxxx\n"  /* TMP[0].x = IN[0].x > 0.0 */
136      " 1: IF TEMP[0].xxxx :4\n"
137      " 2:   MOV OUT[0], IMM[0]\n"    /* front-facing: red */
138      " 3: ELSE :5\n"
139      " 4:   MOV OUT[0], IMM[1]\n"    /* back-facing: green */
140      " 5: ENDIF\n"
141      " 6: END\n";
142
143   handle = graw_parse_fragment_shader(info.ctx, text);
144   info.ctx->bind_fs_state(info.ctx, handle);
145}
146
147
148static void
149draw(void)
150{
151   union pipe_color_union clear_color;
152
153   clear_color.f[0] = 0.25;
154   clear_color.f[1] = 0.25;
155   clear_color.f[2] = 0.25;
156   clear_color.f[3] = 1.00;
157
158   info.ctx->clear(info.ctx,
159              PIPE_CLEAR_COLOR | PIPE_CLEAR_DEPTHSTENCIL,
160              &clear_color, 1.0, 0);
161   util_draw_arrays(info.ctx, PIPE_PRIM_QUADS, 0, NUM_VERTS);
162   info.ctx->flush(info.ctx, NULL, 0);
163
164   graw_util_flush_front(&info);
165}
166
167
168#if 0
169static void
170resize(int w, int h)
171{
172   width = w;
173   height = h;
174
175   set_viewport(0, 0, width, height, 30, 1000);
176}
177#endif
178
179
180static void
181init(void)
182{
183   if (!graw_util_create_window(&info, width, height, 1, TRUE))
184      exit(1);
185
186   graw_util_default_state(&info, TRUE);
187
188   graw_util_viewport(&info, 0, 0, width, height, -1.0, 1.0);
189
190   set_vertices();
191   set_vertex_shader();
192   set_fragment_shader();
193}
194
195
196int
197main(int argc, char *argv[])
198{
199   init();
200
201   printf("Left quad: clock-wise, front-facing, red\n");
202   printf("Right quad: counter clock-wise, back-facing, green\n");
203
204   graw_set_display_func(draw);
205   /*graw_set_reshape_func(resize);*/
206   graw_main_loop();
207   return 0;
208}
209