1848b8605Smrg/* Test the TGSI_SEMANTIC_FACE fragment shader input.
2848b8605Smrg */
3848b8605Smrg
4848b8605Smrg#include <stdio.h>
5848b8605Smrg
6848b8605Smrg#include "graw_util.h"
7848b8605Smrg
8b8e80941Smrg#include "util/macros.h"
9b8e80941Smrg
10848b8605Smrg
11848b8605Smrgstatic int width = 300;
12848b8605Smrgstatic int height = 300;
13848b8605Smrg
14848b8605Smrgstatic struct graw_info info;
15848b8605Smrg
16848b8605Smrgstruct vertex {
17848b8605Smrg   float position[4];
18848b8605Smrg   float color[4];
19848b8605Smrg};
20848b8605Smrg
21848b8605Smrg#define z0 0.2
22848b8605Smrg#define z01 0.5
23848b8605Smrg#define z1 0.4
24848b8605Smrg
25848b8605Smrgstatic struct vertex vertices[] =
26848b8605Smrg{
27848b8605Smrg   /* left quad: clock-wise, front-facing, red */
28848b8605Smrg   {
29848b8605Smrg      {-0.8, -0.9, z0, 1.0 },
30848b8605Smrg      { 0, 0, 0, 1 }
31848b8605Smrg   },
32848b8605Smrg
33848b8605Smrg   {
34848b8605Smrg      { -0.2, -0.9, z0, 1.0 },
35848b8605Smrg      { 0, 0, 0, 1 }
36848b8605Smrg   },
37848b8605Smrg
38848b8605Smrg   {
39848b8605Smrg      { 0.2,  0.9, z01, 1.0 },
40848b8605Smrg      { 0, 0, 0, 1 }
41848b8605Smrg   },
42848b8605Smrg
43848b8605Smrg   {
44848b8605Smrg      {-0.9,  0.9, z01, 1.0 },
45848b8605Smrg      { 0, 0, 0, 1 }
46848b8605Smrg   },
47848b8605Smrg
48848b8605Smrg   /* right quad : counter-clock-wise, back-facing, green */
49848b8605Smrg   {
50848b8605Smrg      { 0.2,  -0.9, z1, 1.0 },
51848b8605Smrg      { 1, 1, 1, -1 }
52848b8605Smrg   },
53848b8605Smrg
54848b8605Smrg   {
55848b8605Smrg      { -0.2,  0.8, z1, 1.0 },
56848b8605Smrg      { 1, 1, 1, -1 }
57848b8605Smrg   },
58848b8605Smrg
59848b8605Smrg   {
60848b8605Smrg      { 0.9,  0.8, z1, 1.0 },
61848b8605Smrg      { 1, 1, 1, -1 }
62848b8605Smrg   },
63848b8605Smrg
64848b8605Smrg   {
65848b8605Smrg      { 0.8, -0.9, z1, 1.0 },
66848b8605Smrg      { 1, 1, 1, -1 }
67848b8605Smrg   },
68848b8605Smrg};
69848b8605Smrg
70b8e80941Smrg#define NUM_VERTS ARRAY_SIZE(vertices)
71848b8605Smrg
72848b8605Smrg
73848b8605Smrg
74848b8605Smrgstatic void
75848b8605Smrgset_vertices(void)
76848b8605Smrg{
77848b8605Smrg   struct pipe_vertex_element ve[2];
78848b8605Smrg   struct pipe_vertex_buffer vbuf;
79848b8605Smrg   void *handle;
80848b8605Smrg
81848b8605Smrg   memset(ve, 0, sizeof ve);
82848b8605Smrg
83848b8605Smrg   ve[0].src_offset = Offset(struct vertex, position);
84848b8605Smrg   ve[0].src_format = PIPE_FORMAT_R32G32B32A32_FLOAT;
85848b8605Smrg   ve[1].src_offset = Offset(struct vertex, color);
86848b8605Smrg   ve[1].src_format = PIPE_FORMAT_R32G32B32A32_FLOAT;
87848b8605Smrg
88848b8605Smrg   handle = info.ctx->create_vertex_elements_state(info.ctx, 2, ve);
89848b8605Smrg   info.ctx->bind_vertex_elements_state(info.ctx, handle);
90848b8605Smrg
91848b8605Smrg   memset(&vbuf, 0, sizeof vbuf);
92848b8605Smrg
93848b8605Smrg   vbuf.stride = sizeof(struct vertex);
94848b8605Smrg   vbuf.buffer_offset = 0;
95b8e80941Smrg   vbuf.buffer.resource = pipe_buffer_create_with_data(info.ctx,
96848b8605Smrg                                              PIPE_BIND_VERTEX_BUFFER,
97848b8605Smrg                                              PIPE_USAGE_DEFAULT,
98848b8605Smrg                                              sizeof(vertices),
99848b8605Smrg                                              vertices);
100848b8605Smrg
101848b8605Smrg   info.ctx->set_vertex_buffers(info.ctx, 0, 1, &vbuf);
102848b8605Smrg}
103848b8605Smrg
104848b8605Smrg
105848b8605Smrgstatic void
106848b8605Smrgset_vertex_shader(void)
107848b8605Smrg{
108848b8605Smrg   void *handle;
109848b8605Smrg   const char *text =
110848b8605Smrg      "VERT\n"
111848b8605Smrg      "DCL IN[0]\n"
112848b8605Smrg      "DCL IN[1]\n"
113848b8605Smrg      "DCL OUT[0], POSITION\n"
114848b8605Smrg      "DCL OUT[1], GENERIC[0]\n"
115848b8605Smrg      "  0: MOV OUT[0], IN[0]\n"
116848b8605Smrg      "  1: MOV OUT[1], IN[1]\n"
117848b8605Smrg      "  2: END\n";
118848b8605Smrg
119848b8605Smrg   handle = graw_parse_vertex_shader(info.ctx, text);
120848b8605Smrg   info.ctx->bind_vs_state(info.ctx, handle);
121848b8605Smrg}
122848b8605Smrg
123848b8605Smrg
124848b8605Smrgstatic void
125848b8605Smrgset_fragment_shader(void)
126848b8605Smrg{
127848b8605Smrg   void *handle;
128848b8605Smrg   const char *text =
129848b8605Smrg      "FRAG\n"
130848b8605Smrg      "DCL IN[0], FACE, CONSTANT\n"
131848b8605Smrg      "DCL IN[1], GENERIC, CONSTANT\n"
132848b8605Smrg      "DCL OUT[0], COLOR\n"
133848b8605Smrg      "DCL TEMP[0]\n"
134848b8605Smrg      "IMM FLT32 {    1.0,     0.0,     0.0,     0.0 }\n"
135848b8605Smrg      "IMM FLT32 {    0.0,     1.0,     0.0,     0.0 }\n"
136848b8605Smrg      "IMM FLT32 {    0.5,     0.6,     0.0,     0.0 }\n"
137848b8605Smrg      " 0: SGT TEMP[0].x, IN[0].xxxx, IMM[1].xxxx\n"  /* TMP[0].x = IN[0].x > 0.0 */
138848b8605Smrg      " 1: IF TEMP[0].xxxx :4\n"
139848b8605Smrg      " 2:   MOV OUT[0], IMM[0]\n"    /* front-facing: red */
140848b8605Smrg      " 3: ELSE :5\n"
141848b8605Smrg      " 4:   MOV OUT[0], IMM[1]\n"    /* back-facing: green */
142848b8605Smrg      " 5: ENDIF\n"
143848b8605Smrg      " 6: END\n";
144848b8605Smrg
145848b8605Smrg   handle = graw_parse_fragment_shader(info.ctx, text);
146848b8605Smrg   info.ctx->bind_fs_state(info.ctx, handle);
147848b8605Smrg}
148848b8605Smrg
149848b8605Smrg
150848b8605Smrgstatic void
151848b8605Smrgdraw(void)
152848b8605Smrg{
153848b8605Smrg   union pipe_color_union clear_color;
154848b8605Smrg
155848b8605Smrg   clear_color.f[0] = 0.25;
156848b8605Smrg   clear_color.f[1] = 0.25;
157848b8605Smrg   clear_color.f[2] = 0.25;
158848b8605Smrg   clear_color.f[3] = 1.00;
159848b8605Smrg
160848b8605Smrg   info.ctx->clear(info.ctx,
161848b8605Smrg              PIPE_CLEAR_COLOR | PIPE_CLEAR_DEPTHSTENCIL,
162848b8605Smrg              &clear_color, 1.0, 0);
163848b8605Smrg   util_draw_arrays(info.ctx, PIPE_PRIM_QUADS, 0, NUM_VERTS);
164848b8605Smrg   info.ctx->flush(info.ctx, NULL, 0);
165848b8605Smrg
166848b8605Smrg   graw_util_flush_front(&info);
167848b8605Smrg}
168848b8605Smrg
169848b8605Smrg
170848b8605Smrg#if 0
171848b8605Smrgstatic void
172848b8605Smrgresize(int w, int h)
173848b8605Smrg{
174848b8605Smrg   width = w;
175848b8605Smrg   height = h;
176848b8605Smrg
177848b8605Smrg   set_viewport(0, 0, width, height, 30, 1000);
178848b8605Smrg}
179848b8605Smrg#endif
180848b8605Smrg
181848b8605Smrg
182848b8605Smrgstatic void
183848b8605Smrginit(void)
184848b8605Smrg{
185848b8605Smrg   if (!graw_util_create_window(&info, width, height, 1, TRUE))
186848b8605Smrg      exit(1);
187848b8605Smrg
188848b8605Smrg   graw_util_default_state(&info, TRUE);
189848b8605Smrg
190848b8605Smrg   graw_util_viewport(&info, 0, 0, width, height, -1.0, 1.0);
191848b8605Smrg
192848b8605Smrg   set_vertices();
193848b8605Smrg   set_vertex_shader();
194848b8605Smrg   set_fragment_shader();
195848b8605Smrg}
196848b8605Smrg
197848b8605Smrg
198848b8605Smrgint
199848b8605Smrgmain(int argc, char *argv[])
200848b8605Smrg{
201848b8605Smrg   init();
202848b8605Smrg
203848b8605Smrg   printf("Left quad: clock-wise, front-facing, red\n");
204848b8605Smrg   printf("Right quad: counter clock-wise, back-facing, green\n");
205848b8605Smrg
206848b8605Smrg   graw_set_display_func(draw);
207848b8605Smrg   /*graw_set_reshape_func(resize);*/
208848b8605Smrg   graw_main_loop();
209848b8605Smrg   return 0;
210848b8605Smrg}
211