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