1848b8605Smrg/* Test the writing Z in fragment shader.
2848b8605Smrg * The red quad should be entirely in front of the blue quad even
3848b8605Smrg * though the overlap and intersect in Z.
4848b8605Smrg */
5848b8605Smrg
6848b8605Smrg#include <stdio.h>
7848b8605Smrg
8848b8605Smrg#include "graw_util.h"
9848b8605Smrg
10b8e80941Smrg#include "util/macros.h"
11b8e80941Smrg
12848b8605Smrg
13848b8605Smrgstatic int width = 300;
14848b8605Smrgstatic int height = 300;
15848b8605Smrg
16848b8605Smrgstatic struct graw_info info;
17848b8605Smrg
18848b8605Smrg
19848b8605Smrgstruct vertex {
20848b8605Smrg   float position[4];
21848b8605Smrg   float color[4];
22848b8605Smrg};
23848b8605Smrg
24848b8605Smrg#define z0 0.2
25848b8605Smrg#define z01 0.5
26848b8605Smrg#define z1 0.4
27848b8605Smrg
28848b8605Smrg
29848b8605Smrgstatic struct vertex vertices[] =
30848b8605Smrg{
31848b8605Smrg   /* left quad: clock-wise, front-facing, red */
32848b8605Smrg   {
33848b8605Smrg      {-0.8, -0.9, z0, 1.0 },
34848b8605Smrg      { 1, 0, 0, 1 }
35848b8605Smrg   },
36848b8605Smrg
37848b8605Smrg   {
38848b8605Smrg      { -0.2, -0.9, z0, 1.0 },
39848b8605Smrg      { 1, 0, 0, 1 }
40848b8605Smrg   },
41848b8605Smrg
42848b8605Smrg   {
43848b8605Smrg      { 0.2,  0.9, z01, 1.0 },
44848b8605Smrg      { 1, 0, 0, 1 }
45848b8605Smrg   },
46848b8605Smrg
47848b8605Smrg   {
48848b8605Smrg      {-0.9,  0.9, z01, 1.0 },
49848b8605Smrg      { 1, 0, 0, 1 }
50848b8605Smrg   },
51848b8605Smrg
52848b8605Smrg   /* right quad : counter-clock-wise, back-facing, green */
53848b8605Smrg   {
54848b8605Smrg      { 0.2,  -0.9, z1, 1.0 },
55848b8605Smrg      { 0, 0, 1, -1 }
56848b8605Smrg   },
57848b8605Smrg
58848b8605Smrg   {
59848b8605Smrg      { -0.2,  0.8, z1, 1.0 },
60848b8605Smrg      { 0, 0, 1, -1 }
61848b8605Smrg   },
62848b8605Smrg
63848b8605Smrg   {
64848b8605Smrg      { 0.9,  0.8, z1, 1.0 },
65848b8605Smrg      { 0, 0, 1, -1 }
66848b8605Smrg   },
67848b8605Smrg
68848b8605Smrg   {
69848b8605Smrg      { 0.8, -0.9, z1, 1.0 },
70848b8605Smrg      { 0, 0, 1, -1 }
71848b8605Smrg   },
72848b8605Smrg};
73848b8605Smrg
74b8e80941Smrg#define NUM_VERTS ARRAY_SIZE(vertices)
75848b8605Smrg
76848b8605Smrg
77848b8605Smrg
78848b8605Smrgstatic void
79848b8605Smrgset_vertices(void)
80848b8605Smrg{
81848b8605Smrg   struct pipe_vertex_element ve[2];
82848b8605Smrg   struct pipe_vertex_buffer vbuf;
83848b8605Smrg   void *handle;
84848b8605Smrg
85848b8605Smrg   memset(ve, 0, sizeof ve);
86848b8605Smrg
87848b8605Smrg   ve[0].src_offset = Offset(struct vertex, position);
88848b8605Smrg   ve[0].src_format = PIPE_FORMAT_R32G32B32A32_FLOAT;
89848b8605Smrg   ve[1].src_offset = Offset(struct vertex, color);
90848b8605Smrg   ve[1].src_format = PIPE_FORMAT_R32G32B32A32_FLOAT;
91848b8605Smrg
92848b8605Smrg   handle = info.ctx->create_vertex_elements_state(info.ctx, 2, ve);
93848b8605Smrg   info.ctx->bind_vertex_elements_state(info.ctx, handle);
94848b8605Smrg
95848b8605Smrg   memset(&vbuf, 0, sizeof vbuf);
96848b8605Smrg
97848b8605Smrg   vbuf.stride = sizeof(struct vertex);
98848b8605Smrg   vbuf.buffer_offset = 0;
99b8e80941Smrg   vbuf.buffer.resource = pipe_buffer_create_with_data(info.ctx,
100848b8605Smrg                                              PIPE_BIND_VERTEX_BUFFER,
101848b8605Smrg                                              PIPE_USAGE_DEFAULT,
102848b8605Smrg                                              sizeof(vertices),
103848b8605Smrg                                              vertices);
104848b8605Smrg
105848b8605Smrg   info.ctx->set_vertex_buffers(info.ctx, 0, 1, &vbuf);
106848b8605Smrg}
107848b8605Smrg
108848b8605Smrg
109848b8605Smrgstatic void
110848b8605Smrgset_vertex_shader(void)
111848b8605Smrg{
112848b8605Smrg   void *handle;
113848b8605Smrg   const char *text =
114848b8605Smrg      "VERT\n"
115848b8605Smrg      "DCL IN[0]\n"
116848b8605Smrg      "DCL IN[1]\n"
117848b8605Smrg      "DCL OUT[0], POSITION\n"
118848b8605Smrg      "DCL OUT[1], GENERIC[0]\n"
119848b8605Smrg      "  0: MOV OUT[0], IN[0]\n"
120848b8605Smrg      "  1: MOV OUT[1], IN[1]\n"
121848b8605Smrg      "  2: END\n";
122848b8605Smrg
123848b8605Smrg   handle = graw_parse_vertex_shader(info.ctx, text);
124848b8605Smrg   info.ctx->bind_vs_state(info.ctx, handle);
125848b8605Smrg}
126848b8605Smrg
127848b8605Smrg
128848b8605Smrgstatic void
129848b8605Smrgset_fragment_shader(void)
130848b8605Smrg{
131848b8605Smrg   void *handle;
132848b8605Smrg   const char *text =
133848b8605Smrg      "FRAG\n"
134848b8605Smrg      "DCL IN[0], GENERIC, CONSTANT\n"
135848b8605Smrg      "DCL OUT[0], COLOR\n"
136848b8605Smrg      "DCL OUT[1], POSITION\n"
137848b8605Smrg      "DCL TEMP[0]\n"
138848b8605Smrg      "IMM FLT32 {    1.0,     0.0,     0.0,     0.0 }\n"
139848b8605Smrg      "IMM FLT32 {    0.0,     1.0,     0.0,     0.0 }\n"
140848b8605Smrg      "IMM FLT32 {    0.5,     0.4,     0.0,     0.0 }\n"
141848b8605Smrg      " 0: MOV OUT[0], IN[0]\n"    /* front-facing: red */
142848b8605Smrg      " 1: IF IN[0].xxxx :3\n"
143848b8605Smrg      " 2:   MOV OUT[1].z, IMM[2].yyyy\n"   /* red: Z = 0.4 */
144848b8605Smrg      " 3: ELSE :5\n"
145848b8605Smrg      " 4:   MOV OUT[1].z, IMM[2].xxxx\n"   /* blue: Z = 0.5 */
146848b8605Smrg      " 5: ENDIF\n"
147848b8605Smrg      " 6: END\n";
148848b8605Smrg
149848b8605Smrg   handle = graw_parse_fragment_shader(info.ctx, text);
150848b8605Smrg   info.ctx->bind_fs_state(info.ctx, handle);
151848b8605Smrg}
152848b8605Smrg
153848b8605Smrg
154848b8605Smrg
155848b8605Smrgstatic void
156848b8605Smrgdraw(void)
157848b8605Smrg{
158848b8605Smrg   union pipe_color_union clear_color;
159848b8605Smrg
160848b8605Smrg   clear_color.f[0] = 0.25;
161848b8605Smrg   clear_color.f[1] = 0.25;
162848b8605Smrg   clear_color.f[2] = 0.25;
163848b8605Smrg   clear_color.f[3] = 1.00;
164848b8605Smrg
165848b8605Smrg   info.ctx->clear(info.ctx,
166848b8605Smrg              PIPE_CLEAR_COLOR | PIPE_CLEAR_DEPTHSTENCIL,
167848b8605Smrg              &clear_color, 1.0, 0);
168848b8605Smrg   util_draw_arrays(info.ctx, PIPE_PRIM_QUADS, 0, NUM_VERTS);
169848b8605Smrg   info.ctx->flush(info.ctx, NULL, 0);
170848b8605Smrg
171848b8605Smrg#if 0
172848b8605Smrg   /* At the moment, libgraw leaks out/makes available some of the
173848b8605Smrg    * symbols from gallium/auxiliary, including these debug helpers.
174848b8605Smrg    * Will eventually want to bless some of these paths, and lock the
175848b8605Smrg    * others down so they aren't accessible from test programs.
176848b8605Smrg    *
177848b8605Smrg    * This currently just happens to work on debug builds - a release
178848b8605Smrg    * build will probably fail to link here:
179848b8605Smrg    */
180848b8605Smrg   debug_dump_surface_bmp(info.ctx, "result.bmp", surf);
181848b8605Smrg#endif
182848b8605Smrg
183848b8605Smrg   graw_util_flush_front(&info);
184848b8605Smrg}
185848b8605Smrg
186848b8605Smrg
187848b8605Smrg#if 0
188848b8605Smrgstatic void
189848b8605Smrgresize(int w, int h)
190848b8605Smrg{
191848b8605Smrg   width = w;
192848b8605Smrg   height = h;
193848b8605Smrg
194848b8605Smrg   graw_util_viewport(&info, 0, 0, width, height, -1.0, 1.0);
195848b8605Smrg}
196848b8605Smrg#endif
197848b8605Smrg
198848b8605Smrg
199848b8605Smrgstatic void
200848b8605Smrginit(void)
201848b8605Smrg{
202848b8605Smrg   if (!graw_util_create_window(&info, width, height, 1, TRUE))
203848b8605Smrg      exit(1);
204848b8605Smrg
205848b8605Smrg   graw_util_default_state(&info, TRUE);
206848b8605Smrg
207848b8605Smrg   graw_util_viewport(&info, 0, 0, width, height, -1.0, 1.0);
208848b8605Smrg
209848b8605Smrg   set_vertices();
210848b8605Smrg   set_vertex_shader();
211848b8605Smrg   set_fragment_shader();
212848b8605Smrg}
213848b8605Smrg
214848b8605Smrg
215848b8605Smrgint
216848b8605Smrgmain(int argc, char *argv[])
217848b8605Smrg{
218848b8605Smrg   init();
219848b8605Smrg
220848b8605Smrg   printf("The red quad should be entirely in front of the blue quad.\n");
221848b8605Smrg
222848b8605Smrg   graw_set_display_func(draw);
223848b8605Smrg   /*graw_set_reshape_func(resize);*/
224848b8605Smrg   graw_main_loop();
225848b8605Smrg   return 0;
226848b8605Smrg}
227