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