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 
11 static int width = 300;
12 static int height = 300;
13 
14 static struct graw_info info;
15 
16 struct 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 
25 static 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 
74 static void
set_vertices(void)75 set_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, 0, false, &vbuf);
102 }
103 
104 
105 static void
set_vertex_shader(void)106 set_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 
124 static void
set_fragment_shader(void)125 set_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 
150 static void
draw(void)151 draw(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               NULL,
163               &clear_color, 1.0, 0);
164    util_draw_arrays(info.ctx, PIPE_PRIM_QUADS, 0, NUM_VERTS);
165    info.ctx->flush(info.ctx, NULL, 0);
166 
167    graw_util_flush_front(&info);
168 }
169 
170 
171 #if 0
172 static void
173 resize(int w, int h)
174 {
175    width = w;
176    height = h;
177 
178    set_viewport(0, 0, width, height, 30, 1000);
179 }
180 #endif
181 
182 
183 static void
init(void)184 init(void)
185 {
186    if (!graw_util_create_window(&info, width, height, 1, TRUE))
187       exit(1);
188 
189    graw_util_default_state(&info, TRUE);
190 
191    graw_util_viewport(&info, 0, 0, width, height, -1.0, 1.0);
192 
193    set_vertices();
194    set_vertex_shader();
195    set_fragment_shader();
196 }
197 
198 
199 int
main(int argc,char * argv[])200 main(int argc, char *argv[])
201 {
202    init();
203 
204    printf("Left quad: clock-wise, front-facing, red\n");
205    printf("Right quad: counter clock-wise, back-facing, green\n");
206 
207    graw_set_display_func(draw);
208    /*graw_set_reshape_func(resize);*/
209    graw_main_loop();
210    return 0;
211 }
212