tests/graw: Add a bunch of tests.
[mesa.git] / src / gallium / tests / graw / fs-write-z.c
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
11 static int width = 300;
12 static int height = 300;
13
14 static struct graw_info info;
15
16
17 struct 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
27 static 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
76 static void
77 set_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 = info.screen->user_buffer_create(info.screen,
97 vertices,
98 sizeof(vertices),
99 PIPE_BIND_VERTEX_BUFFER);
100
101 info.ctx->set_vertex_buffers(info.ctx, 1, &vbuf);
102 }
103
104
105 static void
106 set_vertex_shader(void)
107 {
108 void *handle;
109 const char *text =
110 "VERT\n"
111 "DCL IN[0]\n"
112 "DCL IN[1]\n"
113 "DCL OUT[0], POSITION\n"
114 "DCL OUT[1], GENERIC[0]\n"
115 " 0: MOV OUT[0], IN[0]\n"
116 " 1: MOV OUT[1], IN[1]\n"
117 " 2: END\n";
118
119 handle = graw_parse_vertex_shader(info.ctx, text);
120 info.ctx->bind_vs_state(info.ctx, handle);
121 }
122
123
124 static void
125 set_fragment_shader(void)
126 {
127 void *handle;
128 const char *text =
129 "FRAG\n"
130 "DCL IN[0], GENERIC, CONSTANT\n"
131 "DCL OUT[0], COLOR\n"
132 "DCL OUT[1], POSITION\n"
133 "DCL TEMP[0]\n"
134 "IMM FLT32 { 1.0, 0.0, 0.0, 0.0 }\n"
135 "IMM FLT32 { 0.0, 1.0, 0.0, 0.0 }\n"
136 "IMM FLT32 { 0.5, 0.4, 0.0, 0.0 }\n"
137 " 0: MOV OUT[0], IN[0]\n" /* front-facing: red */
138 " 1: IF IN[0].xxxx :3\n"
139 " 2: MOV OUT[1].z, IMM[2].yyyy\n" /* red: Z = 0.4 */
140 " 3: ELSE :5\n"
141 " 4: MOV OUT[1].z, IMM[2].xxxx\n" /* blue: Z = 0.5 */
142 " 5: ENDIF\n"
143 " 6: END\n";
144
145 handle = graw_parse_fragment_shader(info.ctx, text);
146 info.ctx->bind_fs_state(info.ctx, handle);
147 }
148
149
150
151 static void
152 draw(void)
153 {
154 union pipe_color_union clear_color;
155
156 clear_color.f[0] = 0.25;
157 clear_color.f[1] = 0.25;
158 clear_color.f[2] = 0.25;
159 clear_color.f[3] = 1.00;
160
161 info.ctx->clear(info.ctx,
162 PIPE_CLEAR_COLOR | PIPE_CLEAR_DEPTHSTENCIL,
163 &clear_color, 1.0, 0);
164 util_draw_arrays(info.ctx, PIPE_PRIM_QUADS, 0, NUM_VERTS);
165 info.ctx->flush(info.ctx, NULL);
166
167 #if 0
168 /* At the moment, libgraw leaks out/makes available some of the
169 * symbols from gallium/auxiliary, including these debug helpers.
170 * Will eventually want to bless some of these paths, and lock the
171 * others down so they aren't accessible from test programs.
172 *
173 * This currently just happens to work on debug builds - a release
174 * build will probably fail to link here:
175 */
176 debug_dump_surface_bmp(info.ctx, "result.bmp", surf);
177 #endif
178
179 graw_util_flush_front(&info);
180 }
181
182
183 #if 0
184 static void
185 resize(int w, int h)
186 {
187 width = w;
188 height = h;
189
190 graw_util_viewport(&info, 0, 0, width, height, -1.0, 1.0);
191 }
192 #endif
193
194
195 static void
196 init(void)
197 {
198 if (!graw_util_create_window(&info, width, height, 1, TRUE))
199 exit(1);
200
201 graw_util_default_state(&info, TRUE);
202
203 graw_util_viewport(&info, 0, 0, width, height, -1.0, 1.0);
204
205 set_vertices();
206 set_vertex_shader();
207 set_fragment_shader();
208 }
209
210
211 int
212 main(int argc, char *argv[])
213 {
214 init();
215
216 printf("The red quad should be entirely in front of the blue quad.\n");
217
218 graw_set_display_func(draw);
219 /*graw_set_reshape_func(resize);*/
220 graw_main_loop();
221 return 0;
222 }