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