1bf215546Sopenharmony_ci/* Test the TGSI_SEMANTIC_POSITION fragment shader input.
2bf215546Sopenharmony_ci * Plus properties for upper-left vs. lower-left origin and
3bf215546Sopenharmony_ci * center integer vs. half-integer;
4bf215546Sopenharmony_ci */
5bf215546Sopenharmony_ci
6bf215546Sopenharmony_ci#include <stdio.h>
7bf215546Sopenharmony_ci
8bf215546Sopenharmony_ci#include "graw_util.h"
9bf215546Sopenharmony_ci
10bf215546Sopenharmony_ci#include "util/macros.h"
11bf215546Sopenharmony_ci
12bf215546Sopenharmony_ci
13bf215546Sopenharmony_cistatic int width = 300;
14bf215546Sopenharmony_cistatic int height = 300;
15bf215546Sopenharmony_ci
16bf215546Sopenharmony_cistatic struct graw_info info;
17bf215546Sopenharmony_ci
18bf215546Sopenharmony_cistruct vertex {
19bf215546Sopenharmony_ci   float position[4];
20bf215546Sopenharmony_ci   float color[4];
21bf215546Sopenharmony_ci};
22bf215546Sopenharmony_ci
23bf215546Sopenharmony_ci/* Note: the upper-left vertex is pushed to the left a bit to
24bf215546Sopenharmony_ci * make sure we can spot upside-down rendering.
25bf215546Sopenharmony_ci */
26bf215546Sopenharmony_cistatic struct vertex vertices[] =
27bf215546Sopenharmony_ci{
28bf215546Sopenharmony_ci   {
29bf215546Sopenharmony_ci      {-0.95, -0.95, 0.5, 1.0 },
30bf215546Sopenharmony_ci      { 0, 0, 0, 1 }
31bf215546Sopenharmony_ci   },
32bf215546Sopenharmony_ci
33bf215546Sopenharmony_ci   {
34bf215546Sopenharmony_ci      { 0.85, -0.95, 0.5, 1.0 },
35bf215546Sopenharmony_ci      { 0, 0, 0, 1 }
36bf215546Sopenharmony_ci   },
37bf215546Sopenharmony_ci
38bf215546Sopenharmony_ci   {
39bf215546Sopenharmony_ci      { 0.95,  0.95, 0.5, 1.0 },
40bf215546Sopenharmony_ci      { 0, 0, 0, 1 }
41bf215546Sopenharmony_ci   },
42bf215546Sopenharmony_ci
43bf215546Sopenharmony_ci   {
44bf215546Sopenharmony_ci      {-0.95,  0.95, 0.5, 1.0 },
45bf215546Sopenharmony_ci      { 0, 0, 0, 1 }
46bf215546Sopenharmony_ci   }
47bf215546Sopenharmony_ci};
48bf215546Sopenharmony_ci
49bf215546Sopenharmony_ci#define NUM_VERTS ARRAY_SIZE(vertices)
50bf215546Sopenharmony_ci
51bf215546Sopenharmony_ci
52bf215546Sopenharmony_cistatic void
53bf215546Sopenharmony_ciset_vertices(void)
54bf215546Sopenharmony_ci{
55bf215546Sopenharmony_ci   struct pipe_vertex_element ve[2];
56bf215546Sopenharmony_ci   struct pipe_vertex_buffer vbuf;
57bf215546Sopenharmony_ci   void *handle;
58bf215546Sopenharmony_ci
59bf215546Sopenharmony_ci   memset(ve, 0, sizeof ve);
60bf215546Sopenharmony_ci
61bf215546Sopenharmony_ci   ve[0].src_offset = Offset(struct vertex, position);
62bf215546Sopenharmony_ci   ve[0].src_format = PIPE_FORMAT_R32G32B32A32_FLOAT;
63bf215546Sopenharmony_ci   ve[1].src_offset = Offset(struct vertex, color);
64bf215546Sopenharmony_ci   ve[1].src_format = PIPE_FORMAT_R32G32B32A32_FLOAT;
65bf215546Sopenharmony_ci
66bf215546Sopenharmony_ci   handle = info.ctx->create_vertex_elements_state(info.ctx, 2, ve);
67bf215546Sopenharmony_ci   info.ctx->bind_vertex_elements_state(info.ctx, handle);
68bf215546Sopenharmony_ci
69bf215546Sopenharmony_ci   memset(&vbuf, 0, sizeof vbuf);
70bf215546Sopenharmony_ci
71bf215546Sopenharmony_ci   vbuf.stride = sizeof(struct vertex);
72bf215546Sopenharmony_ci   vbuf.buffer_offset = 0;
73bf215546Sopenharmony_ci   vbuf.buffer.resource = pipe_buffer_create_with_data(info.ctx,
74bf215546Sopenharmony_ci                                              PIPE_BIND_VERTEX_BUFFER,
75bf215546Sopenharmony_ci                                              PIPE_USAGE_DEFAULT,
76bf215546Sopenharmony_ci                                              sizeof(vertices),
77bf215546Sopenharmony_ci                                              vertices);
78bf215546Sopenharmony_ci
79bf215546Sopenharmony_ci   info.ctx->set_vertex_buffers(info.ctx, 0, 1, 0, false, &vbuf);
80bf215546Sopenharmony_ci}
81bf215546Sopenharmony_ci
82bf215546Sopenharmony_ci
83bf215546Sopenharmony_cistatic void
84bf215546Sopenharmony_ciset_vertex_shader(void)
85bf215546Sopenharmony_ci{
86bf215546Sopenharmony_ci   void *handle;
87bf215546Sopenharmony_ci   const char *text =
88bf215546Sopenharmony_ci      "VERT\n"
89bf215546Sopenharmony_ci      "DCL IN[0]\n"
90bf215546Sopenharmony_ci      "DCL IN[1]\n"
91bf215546Sopenharmony_ci      "DCL OUT[0], POSITION\n"
92bf215546Sopenharmony_ci      "DCL OUT[1], GENERIC[0]\n"
93bf215546Sopenharmony_ci      "  0: MOV OUT[0], IN[0]\n"
94bf215546Sopenharmony_ci      "  1: MOV OUT[1], IN[1]\n"
95bf215546Sopenharmony_ci      "  2: END\n";
96bf215546Sopenharmony_ci
97bf215546Sopenharmony_ci   handle = graw_parse_vertex_shader(info.ctx, text);
98bf215546Sopenharmony_ci   info.ctx->bind_vs_state(info.ctx, handle);
99bf215546Sopenharmony_ci}
100bf215546Sopenharmony_ci
101bf215546Sopenharmony_ci
102bf215546Sopenharmony_cistatic void
103bf215546Sopenharmony_ciset_fragment_shader(int mode)
104bf215546Sopenharmony_ci{
105bf215546Sopenharmony_ci   void *handle;
106bf215546Sopenharmony_ci
107bf215546Sopenharmony_ci   const char *origin_upper_left_text =
108bf215546Sopenharmony_ci      "FRAG\n"
109bf215546Sopenharmony_ci      "PROPERTY FS_COORD_ORIGIN UPPER_LEFT\n"  /* upper-left = black corner */
110bf215546Sopenharmony_ci      "DCL IN[0], POSITION, LINEAR\n"
111bf215546Sopenharmony_ci      "DCL OUT[0], COLOR\n"
112bf215546Sopenharmony_ci      "DCL TEMP[0]\n"
113bf215546Sopenharmony_ci      "IMM FLT32 { 0.003333, 0.003333, 1.0, 1.0 }\n"
114bf215546Sopenharmony_ci      "IMM FLT32 { 0.0, 300.0, 0.0, 0.0 }\n"
115bf215546Sopenharmony_ci      " 0: MOV TEMP[0], IN[0] \n"
116bf215546Sopenharmony_ci      " 1: MOV TEMP[0].zw, IMM[1].xxxx \n"
117bf215546Sopenharmony_ci      " 2: MUL OUT[0], TEMP[0], IMM[0] \n"
118bf215546Sopenharmony_ci      " 3: END\n";
119bf215546Sopenharmony_ci
120bf215546Sopenharmony_ci   const char *origin_lower_left_text =
121bf215546Sopenharmony_ci      "FRAG\n"
122bf215546Sopenharmony_ci      "PROPERTY FS_COORD_ORIGIN LOWER_LEFT\n"  /* lower-left = black corner */
123bf215546Sopenharmony_ci      "DCL IN[0], POSITION, LINEAR\n"
124bf215546Sopenharmony_ci      "DCL OUT[0], COLOR\n"
125bf215546Sopenharmony_ci      "DCL TEMP[0]\n"
126bf215546Sopenharmony_ci      "IMM FLT32 { 0.003333, 0.003333, 1.0, 1.0 }\n"
127bf215546Sopenharmony_ci      "IMM FLT32 { 0.0, 300.0, 0.0, 0.0 }\n"
128bf215546Sopenharmony_ci      " 0: MOV TEMP[0], IN[0] \n"
129bf215546Sopenharmony_ci      " 1: MOV TEMP[0].zw, IMM[1].xxxx \n"
130bf215546Sopenharmony_ci      " 2: MUL OUT[0], TEMP[0], IMM[0] \n"
131bf215546Sopenharmony_ci      " 3: END\n";
132bf215546Sopenharmony_ci
133bf215546Sopenharmony_ci   /* Test fragcoord center integer vs. half integer */
134bf215546Sopenharmony_ci   const char *center_integer_text =
135bf215546Sopenharmony_ci      "FRAG\n"
136bf215546Sopenharmony_ci      "PROPERTY FS_COORD_PIXEL_CENTER INTEGER \n"       /* pixels are black */
137bf215546Sopenharmony_ci      "DCL IN[0], POSITION, LINEAR \n"
138bf215546Sopenharmony_ci      "DCL OUT[0], COLOR \n"
139bf215546Sopenharmony_ci      "DCL TEMP[0] \n"
140bf215546Sopenharmony_ci      "IMM FLT32 { 0.003333, 0.003333, 1.0, 1.0 } \n"
141bf215546Sopenharmony_ci      "IMM FLT32 { 0.0, 300.0, 0.0, 0.0 } \n"
142bf215546Sopenharmony_ci      "0: FRC TEMP[0], IN[0]  \n"
143bf215546Sopenharmony_ci      "1: MOV TEMP[0].zw, IMM[1].xxxx \n"
144bf215546Sopenharmony_ci      "2: MOV OUT[0], TEMP[0] \n"
145bf215546Sopenharmony_ci      "3: END \n";
146bf215546Sopenharmony_ci
147bf215546Sopenharmony_ci   const char *center_half_integer_text =
148bf215546Sopenharmony_ci      "FRAG\n"
149bf215546Sopenharmony_ci      "PROPERTY FS_COORD_PIXEL_CENTER HALF_INTEGER \n"  /* pixels are olive colored */
150bf215546Sopenharmony_ci      "DCL IN[0], POSITION, LINEAR \n"
151bf215546Sopenharmony_ci      "DCL OUT[0], COLOR \n"
152bf215546Sopenharmony_ci      "DCL TEMP[0] \n"
153bf215546Sopenharmony_ci      "IMM FLT32 { 0.003333, 0.003333, 1.0, 1.0 } \n"
154bf215546Sopenharmony_ci      "IMM FLT32 { 0.0, 300.0, 0.0, 0.0 } \n"
155bf215546Sopenharmony_ci      "0: FRC TEMP[0], IN[0]  \n"
156bf215546Sopenharmony_ci      "1: MOV TEMP[0].zw, IMM[1].xxxx \n"
157bf215546Sopenharmony_ci      "2: MOV OUT[0], TEMP[0] \n"
158bf215546Sopenharmony_ci      "3: END \n";
159bf215546Sopenharmony_ci
160bf215546Sopenharmony_ci   const char *text;
161bf215546Sopenharmony_ci
162bf215546Sopenharmony_ci   if (mode == 0)
163bf215546Sopenharmony_ci      text = origin_upper_left_text;
164bf215546Sopenharmony_ci   else if (mode == 1)
165bf215546Sopenharmony_ci      text = origin_lower_left_text;
166bf215546Sopenharmony_ci   else if (mode == 2)
167bf215546Sopenharmony_ci      text = center_integer_text;
168bf215546Sopenharmony_ci   else
169bf215546Sopenharmony_ci      text = center_half_integer_text;
170bf215546Sopenharmony_ci
171bf215546Sopenharmony_ci   handle = graw_parse_fragment_shader(info.ctx, text);
172bf215546Sopenharmony_ci   info.ctx->bind_fs_state(info.ctx, handle);
173bf215546Sopenharmony_ci}
174bf215546Sopenharmony_ci
175bf215546Sopenharmony_ci
176bf215546Sopenharmony_cistatic void
177bf215546Sopenharmony_cidraw(void)
178bf215546Sopenharmony_ci{
179bf215546Sopenharmony_ci   union pipe_color_union clear_color;
180bf215546Sopenharmony_ci
181bf215546Sopenharmony_ci   clear_color.f[0] = 0.25;
182bf215546Sopenharmony_ci   clear_color.f[1] = 0.25;
183bf215546Sopenharmony_ci   clear_color.f[2] = 0.25;
184bf215546Sopenharmony_ci   clear_color.f[3] = 1.0;
185bf215546Sopenharmony_ci
186bf215546Sopenharmony_ci   info.ctx->clear(info.ctx,
187bf215546Sopenharmony_ci              PIPE_CLEAR_COLOR | PIPE_CLEAR_DEPTHSTENCIL,
188bf215546Sopenharmony_ci              NULL,
189bf215546Sopenharmony_ci              &clear_color, 1.0, 0);
190bf215546Sopenharmony_ci   util_draw_arrays(info.ctx, PIPE_PRIM_QUADS, 0, NUM_VERTS);
191bf215546Sopenharmony_ci   info.ctx->flush(info.ctx, NULL, 0);
192bf215546Sopenharmony_ci
193bf215546Sopenharmony_ci#if 0
194bf215546Sopenharmony_ci   /* At the moment, libgraw leaks out/makes available some of the
195bf215546Sopenharmony_ci    * symbols from gallium/auxiliary, including these debug helpers.
196bf215546Sopenharmony_ci    * Will eventually want to bless some of these paths, and lock the
197bf215546Sopenharmony_ci    * others down so they aren't accessible from test programs.
198bf215546Sopenharmony_ci    *
199bf215546Sopenharmony_ci    * This currently just happens to work on debug builds - a release
200bf215546Sopenharmony_ci    * build will probably fail to link here:
201bf215546Sopenharmony_ci    */
202bf215546Sopenharmony_ci   debug_dump_surface_bmp(info.ctx, "result.bmp", surf);
203bf215546Sopenharmony_ci#endif
204bf215546Sopenharmony_ci
205bf215546Sopenharmony_ci   graw_util_flush_front(&info);
206bf215546Sopenharmony_ci}
207bf215546Sopenharmony_ci
208bf215546Sopenharmony_ci
209bf215546Sopenharmony_ci#if 0
210bf215546Sopenharmony_cistatic void
211bf215546Sopenharmony_ciresize(int w, int h)
212bf215546Sopenharmony_ci{
213bf215546Sopenharmony_ci   width = w;
214bf215546Sopenharmony_ci   height = h;
215bf215546Sopenharmony_ci
216bf215546Sopenharmony_ci   set_viewport(0, 0, width, height, 30, 1000);
217bf215546Sopenharmony_ci}
218bf215546Sopenharmony_ci#endif
219bf215546Sopenharmony_ci
220bf215546Sopenharmony_ci
221bf215546Sopenharmony_cistatic void
222bf215546Sopenharmony_ciinit(int mode)
223bf215546Sopenharmony_ci{
224bf215546Sopenharmony_ci   if (!graw_util_create_window(&info, width, height, 1, TRUE))
225bf215546Sopenharmony_ci      exit(1);
226bf215546Sopenharmony_ci
227bf215546Sopenharmony_ci   graw_util_default_state(&info, TRUE);
228bf215546Sopenharmony_ci
229bf215546Sopenharmony_ci   graw_util_viewport(&info, 0, 0, width, height, -1.0, 1.0);
230bf215546Sopenharmony_ci
231bf215546Sopenharmony_ci   set_vertices();
232bf215546Sopenharmony_ci   set_vertex_shader();
233bf215546Sopenharmony_ci   set_fragment_shader(mode);
234bf215546Sopenharmony_ci}
235bf215546Sopenharmony_ci
236bf215546Sopenharmony_ci
237bf215546Sopenharmony_ciint
238bf215546Sopenharmony_cimain(int argc, char *argv[])
239bf215546Sopenharmony_ci{
240bf215546Sopenharmony_ci   int mode = argc > 1 ? atoi(argv[1]) : 0;
241bf215546Sopenharmony_ci
242bf215546Sopenharmony_ci   switch (mode) {
243bf215546Sopenharmony_ci   default:
244bf215546Sopenharmony_ci   case 0:
245bf215546Sopenharmony_ci      printf("frag coord origin upper-left (lower-left = black)\n");
246bf215546Sopenharmony_ci      break;
247bf215546Sopenharmony_ci   case 1:
248bf215546Sopenharmony_ci      printf("frag coord origin lower-left (upper-left = black)\n");
249bf215546Sopenharmony_ci      break;
250bf215546Sopenharmony_ci   case 2:
251bf215546Sopenharmony_ci      printf("frag coord center integer (all pixels black)\n");
252bf215546Sopenharmony_ci      break;
253bf215546Sopenharmony_ci   case 3:
254bf215546Sopenharmony_ci      printf("frag coord center half-integer (all pixels olive color)\n");
255bf215546Sopenharmony_ci      break;
256bf215546Sopenharmony_ci   }
257bf215546Sopenharmony_ci
258bf215546Sopenharmony_ci   init(mode);
259bf215546Sopenharmony_ci
260bf215546Sopenharmony_ci   graw_set_display_func(draw);
261bf215546Sopenharmony_ci   /*graw_set_reshape_func(resize);*/
262bf215546Sopenharmony_ci   graw_main_loop();
263bf215546Sopenharmony_ci   return 0;
264bf215546Sopenharmony_ci}
265