1af69d88dSmrg/* Test the writing Z in fragment shader.
2af69d88dSmrg * The red quad should be entirely in front of the blue quad even
3af69d88dSmrg * though the overlap and intersect in Z.
4af69d88dSmrg */
5af69d88dSmrg
6af69d88dSmrg#include <stdio.h>
7af69d88dSmrg
8af69d88dSmrg#include "graw_util.h"
9af69d88dSmrg
1001e04c3fSmrg#include "util/macros.h"
1101e04c3fSmrg
12af69d88dSmrg
13af69d88dSmrgstatic int width = 300;
14af69d88dSmrgstatic int height = 300;
15af69d88dSmrg
16af69d88dSmrgstatic struct graw_info info;
17af69d88dSmrg
18af69d88dSmrg
19af69d88dSmrgstruct vertex {
20af69d88dSmrg   float position[4];
21af69d88dSmrg   float color[4];
22af69d88dSmrg};
23af69d88dSmrg
24af69d88dSmrg#define z0 0.2
25af69d88dSmrg#define z01 0.5
26af69d88dSmrg#define z1 0.4
27af69d88dSmrg
28af69d88dSmrg
29af69d88dSmrgstatic struct vertex vertices[] =
30af69d88dSmrg{
31af69d88dSmrg   /* left quad: clock-wise, front-facing, red */
32af69d88dSmrg   {
33af69d88dSmrg      {-0.8, -0.9, z0, 1.0 },
34af69d88dSmrg      { 1, 0, 0, 1 }
35af69d88dSmrg   },
36af69d88dSmrg
37af69d88dSmrg   {
38af69d88dSmrg      { -0.2, -0.9, z0, 1.0 },
39af69d88dSmrg      { 1, 0, 0, 1 }
40af69d88dSmrg   },
41af69d88dSmrg
42af69d88dSmrg   {
43af69d88dSmrg      { 0.2,  0.9, z01, 1.0 },
44af69d88dSmrg      { 1, 0, 0, 1 }
45af69d88dSmrg   },
46af69d88dSmrg
47af69d88dSmrg   {
48af69d88dSmrg      {-0.9,  0.9, z01, 1.0 },
49af69d88dSmrg      { 1, 0, 0, 1 }
50af69d88dSmrg   },
51af69d88dSmrg
52af69d88dSmrg   /* right quad : counter-clock-wise, back-facing, green */
53af69d88dSmrg   {
54af69d88dSmrg      { 0.2,  -0.9, z1, 1.0 },
55af69d88dSmrg      { 0, 0, 1, -1 }
56af69d88dSmrg   },
57af69d88dSmrg
58af69d88dSmrg   {
59af69d88dSmrg      { -0.2,  0.8, z1, 1.0 },
60af69d88dSmrg      { 0, 0, 1, -1 }
61af69d88dSmrg   },
62af69d88dSmrg
63af69d88dSmrg   {
64af69d88dSmrg      { 0.9,  0.8, z1, 1.0 },
65af69d88dSmrg      { 0, 0, 1, -1 }
66af69d88dSmrg   },
67af69d88dSmrg
68af69d88dSmrg   {
69af69d88dSmrg      { 0.8, -0.9, z1, 1.0 },
70af69d88dSmrg      { 0, 0, 1, -1 }
71af69d88dSmrg   },
72af69d88dSmrg};
73af69d88dSmrg
7401e04c3fSmrg#define NUM_VERTS ARRAY_SIZE(vertices)
75af69d88dSmrg
76af69d88dSmrg
77af69d88dSmrg
78af69d88dSmrgstatic void
79af69d88dSmrgset_vertices(void)
80af69d88dSmrg{
81af69d88dSmrg   struct pipe_vertex_element ve[2];
82af69d88dSmrg   struct pipe_vertex_buffer vbuf;
83af69d88dSmrg   void *handle;
84af69d88dSmrg
85af69d88dSmrg   memset(ve, 0, sizeof ve);
86af69d88dSmrg
87af69d88dSmrg   ve[0].src_offset = Offset(struct vertex, position);
88af69d88dSmrg   ve[0].src_format = PIPE_FORMAT_R32G32B32A32_FLOAT;
89af69d88dSmrg   ve[1].src_offset = Offset(struct vertex, color);
90af69d88dSmrg   ve[1].src_format = PIPE_FORMAT_R32G32B32A32_FLOAT;
91af69d88dSmrg
92af69d88dSmrg   handle = info.ctx->create_vertex_elements_state(info.ctx, 2, ve);
93af69d88dSmrg   info.ctx->bind_vertex_elements_state(info.ctx, handle);
94af69d88dSmrg
95af69d88dSmrg   memset(&vbuf, 0, sizeof vbuf);
96af69d88dSmrg
97af69d88dSmrg   vbuf.stride = sizeof(struct vertex);
98af69d88dSmrg   vbuf.buffer_offset = 0;
9901e04c3fSmrg   vbuf.buffer.resource = pipe_buffer_create_with_data(info.ctx,
100af69d88dSmrg                                              PIPE_BIND_VERTEX_BUFFER,
101af69d88dSmrg                                              PIPE_USAGE_DEFAULT,
102af69d88dSmrg                                              sizeof(vertices),
103af69d88dSmrg                                              vertices);
104af69d88dSmrg
1057ec681f3Smrg   info.ctx->set_vertex_buffers(info.ctx, 0, 1, 0, false, &vbuf);
106af69d88dSmrg}
107af69d88dSmrg
108af69d88dSmrg
109af69d88dSmrgstatic void
110af69d88dSmrgset_vertex_shader(void)
111af69d88dSmrg{
112af69d88dSmrg   void *handle;
113af69d88dSmrg   const char *text =
114af69d88dSmrg      "VERT\n"
115af69d88dSmrg      "DCL IN[0]\n"
116af69d88dSmrg      "DCL IN[1]\n"
117af69d88dSmrg      "DCL OUT[0], POSITION\n"
118af69d88dSmrg      "DCL OUT[1], GENERIC[0]\n"
119af69d88dSmrg      "  0: MOV OUT[0], IN[0]\n"
120af69d88dSmrg      "  1: MOV OUT[1], IN[1]\n"
121af69d88dSmrg      "  2: END\n";
122af69d88dSmrg
123af69d88dSmrg   handle = graw_parse_vertex_shader(info.ctx, text);
124af69d88dSmrg   info.ctx->bind_vs_state(info.ctx, handle);
125af69d88dSmrg}
126af69d88dSmrg
127af69d88dSmrg
128af69d88dSmrgstatic void
129af69d88dSmrgset_fragment_shader(void)
130af69d88dSmrg{
131af69d88dSmrg   void *handle;
132af69d88dSmrg   const char *text =
133af69d88dSmrg      "FRAG\n"
134af69d88dSmrg      "DCL IN[0], GENERIC, CONSTANT\n"
135af69d88dSmrg      "DCL OUT[0], COLOR\n"
136af69d88dSmrg      "DCL OUT[1], POSITION\n"
137af69d88dSmrg      "DCL TEMP[0]\n"
138af69d88dSmrg      "IMM FLT32 {    1.0,     0.0,     0.0,     0.0 }\n"
139af69d88dSmrg      "IMM FLT32 {    0.0,     1.0,     0.0,     0.0 }\n"
140af69d88dSmrg      "IMM FLT32 {    0.5,     0.4,     0.0,     0.0 }\n"
141af69d88dSmrg      " 0: MOV OUT[0], IN[0]\n"    /* front-facing: red */
142af69d88dSmrg      " 1: IF IN[0].xxxx :3\n"
143af69d88dSmrg      " 2:   MOV OUT[1].z, IMM[2].yyyy\n"   /* red: Z = 0.4 */
144af69d88dSmrg      " 3: ELSE :5\n"
145af69d88dSmrg      " 4:   MOV OUT[1].z, IMM[2].xxxx\n"   /* blue: Z = 0.5 */
146af69d88dSmrg      " 5: ENDIF\n"
147af69d88dSmrg      " 6: END\n";
148af69d88dSmrg
149af69d88dSmrg   handle = graw_parse_fragment_shader(info.ctx, text);
150af69d88dSmrg   info.ctx->bind_fs_state(info.ctx, handle);
151af69d88dSmrg}
152af69d88dSmrg
153af69d88dSmrg
154af69d88dSmrg
155af69d88dSmrgstatic void
156af69d88dSmrgdraw(void)
157af69d88dSmrg{
158af69d88dSmrg   union pipe_color_union clear_color;
159af69d88dSmrg
160af69d88dSmrg   clear_color.f[0] = 0.25;
161af69d88dSmrg   clear_color.f[1] = 0.25;
162af69d88dSmrg   clear_color.f[2] = 0.25;
163af69d88dSmrg   clear_color.f[3] = 1.00;
164af69d88dSmrg
165af69d88dSmrg   info.ctx->clear(info.ctx,
166af69d88dSmrg              PIPE_CLEAR_COLOR | PIPE_CLEAR_DEPTHSTENCIL,
1677ec681f3Smrg              NULL,
168af69d88dSmrg              &clear_color, 1.0, 0);
169af69d88dSmrg   util_draw_arrays(info.ctx, PIPE_PRIM_QUADS, 0, NUM_VERTS);
170af69d88dSmrg   info.ctx->flush(info.ctx, NULL, 0);
171af69d88dSmrg
172af69d88dSmrg#if 0
173af69d88dSmrg   /* At the moment, libgraw leaks out/makes available some of the
174af69d88dSmrg    * symbols from gallium/auxiliary, including these debug helpers.
175af69d88dSmrg    * Will eventually want to bless some of these paths, and lock the
176af69d88dSmrg    * others down so they aren't accessible from test programs.
177af69d88dSmrg    *
178af69d88dSmrg    * This currently just happens to work on debug builds - a release
179af69d88dSmrg    * build will probably fail to link here:
180af69d88dSmrg    */
181af69d88dSmrg   debug_dump_surface_bmp(info.ctx, "result.bmp", surf);
182af69d88dSmrg#endif
183af69d88dSmrg
184af69d88dSmrg   graw_util_flush_front(&info);
185af69d88dSmrg}
186af69d88dSmrg
187af69d88dSmrg
188af69d88dSmrg#if 0
189af69d88dSmrgstatic void
190af69d88dSmrgresize(int w, int h)
191af69d88dSmrg{
192af69d88dSmrg   width = w;
193af69d88dSmrg   height = h;
194af69d88dSmrg
195af69d88dSmrg   graw_util_viewport(&info, 0, 0, width, height, -1.0, 1.0);
196af69d88dSmrg}
197af69d88dSmrg#endif
198af69d88dSmrg
199af69d88dSmrg
200af69d88dSmrgstatic void
201af69d88dSmrginit(void)
202af69d88dSmrg{
203af69d88dSmrg   if (!graw_util_create_window(&info, width, height, 1, TRUE))
204af69d88dSmrg      exit(1);
205af69d88dSmrg
206af69d88dSmrg   graw_util_default_state(&info, TRUE);
207af69d88dSmrg
208af69d88dSmrg   graw_util_viewport(&info, 0, 0, width, height, -1.0, 1.0);
209af69d88dSmrg
210af69d88dSmrg   set_vertices();
211af69d88dSmrg   set_vertex_shader();
212af69d88dSmrg   set_fragment_shader();
213af69d88dSmrg}
214af69d88dSmrg
215af69d88dSmrg
216af69d88dSmrgint
217af69d88dSmrgmain(int argc, char *argv[])
218af69d88dSmrg{
219af69d88dSmrg   init();
220af69d88dSmrg
221af69d88dSmrg   printf("The red quad should be entirely in front of the blue quad.\n");
222af69d88dSmrg
223af69d88dSmrg   graw_set_display_func(draw);
224af69d88dSmrg   /*graw_set_reshape_func(resize);*/
225af69d88dSmrg   graw_main_loop();
226af69d88dSmrg   return 0;
227af69d88dSmrg}
228