gallium: change set_constant_buffer to be UBO-friendly
[mesa.git] / src / gallium / tests / graw / occlusion-query.c
1 /* Test gallium occlusion queries.
2 */
3
4 #include <stdio.h>
5
6 #include "graw_util.h"
7
8
9 static int width = 300;
10 static int height = 300;
11
12 /* expected results of occlusion test (depndsd on window size) */
13 static int expected1 = (int) ((300 * 0.9) * (300 * 0.9));
14 static int expected2 = 420;
15
16
17 static struct graw_info info;
18
19 struct vertex {
20 float position[4];
21 float color[4];
22 };
23
24 #define z0 0.2
25 #define z1 0.6
26
27 static struct vertex obj1_vertices[4] =
28 {
29 {
30 {-0.9, -0.9, z0, 1.0 },
31 { 1, 0, 0, 1 }
32 },
33
34 {
35 { 0.9, -0.9, z0, 1.0 },
36 { 1, 0, 0, 1 }
37 },
38
39 {
40 { 0.9, 0.9, z0, 1.0 },
41 { 1, 0, 0, 1 }
42 },
43
44 {
45 {-0.9, 0.9, z0, 1.0 },
46 { 1, 0, 0, 1 }
47 }
48 };
49
50 static struct vertex obj2_vertices[4] =
51 {
52 {
53 { -0.2, -0.2, z1, 1.0 },
54 { 0, 0, 1, 1 }
55 },
56
57 {
58 { 0.95, -0.2, z1, 1.0 },
59 { 0, 0, 1, 1 }
60 },
61
62 {
63 { 0.95, 0.2, z1, 1.0 },
64 { 0, 0, 1, 1 }
65 },
66
67 {
68 { -0.2, 0.2, z1, 1.0 },
69 { 0, 0, 1, 1 }
70 },
71 };
72
73 #define NUM_VERTS 4
74
75
76
77 static void
78 set_vertices(struct vertex *vertices, unsigned bytes)
79 {
80 struct pipe_vertex_element ve[2];
81 struct pipe_vertex_buffer vbuf;
82 void *handle;
83
84 memset(ve, 0, sizeof ve);
85
86 ve[0].src_offset = Offset(struct vertex, position);
87 ve[0].src_format = PIPE_FORMAT_R32G32B32A32_FLOAT;
88 ve[1].src_offset = Offset(struct vertex, color);
89 ve[1].src_format = PIPE_FORMAT_R32G32B32A32_FLOAT;
90
91 handle = info.ctx->create_vertex_elements_state(info.ctx, 2, ve);
92 info.ctx->bind_vertex_elements_state(info.ctx, handle);
93
94
95 vbuf.stride = sizeof(struct vertex);
96 vbuf.buffer_offset = 0;
97 vbuf.buffer = info.screen->user_buffer_create(info.screen,
98 vertices,
99 bytes,
100 PIPE_BIND_VERTEX_BUFFER);
101
102 info.ctx->set_vertex_buffers(info.ctx, 1, &vbuf);
103 }
104
105
106 static void
107 set_vertex_shader(struct graw_info *info)
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 if (!handle) {
122 debug_printf("Failed to parse vertex shader\n");
123 return;
124 }
125 info->ctx->bind_vs_state(info->ctx, handle);
126 }
127
128
129 static void
130 set_fragment_shader(struct graw_info *info)
131 {
132 void *handle;
133 const char *text =
134 "FRAG\n"
135 "DCL IN[0], GENERIC, LINEAR\n"
136 "DCL OUT[0], COLOR\n"
137 " 0: MOV OUT[0], IN[0]\n"
138 " 1: END\n";
139
140 handle = graw_parse_fragment_shader(info->ctx, text);
141 if (!handle) {
142 debug_printf("Failed to parse fragment shader\n");
143 return;
144 }
145 info->ctx->bind_fs_state(info->ctx, handle);
146 }
147
148
149 static void
150 draw(void)
151 {
152 int expected1_min = (int) (expected1 * 0.95);
153 int expected1_max = (int) (expected1 * 1.05);
154 int expected2_min = (int) (expected2 * 0.95);
155 int expected2_max = (int) (expected2 * 1.05);
156
157 union pipe_color_union clear_color;
158
159 struct pipe_query *q1, *q2;
160 uint64_t res1, res2;
161
162 clear_color.f[0] = 0.25;
163 clear_color.f[1] = 0.25;
164 clear_color.f[2] = 0.25;
165 clear_color.f[3] = 1.00;
166
167 info.ctx->clear(info.ctx,
168 PIPE_CLEAR_COLOR | PIPE_CLEAR_DEPTHSTENCIL,
169 &clear_color, 1.0, 0);
170
171 q1 = info.ctx->create_query(info.ctx, PIPE_QUERY_OCCLUSION_COUNTER);
172 q2 = info.ctx->create_query(info.ctx, PIPE_QUERY_OCCLUSION_COUNTER);
173
174 /* draw first, large object */
175 set_vertices(obj1_vertices, sizeof(obj1_vertices));
176 info.ctx->begin_query(info.ctx, q1);
177 util_draw_arrays(info.ctx, PIPE_PRIM_QUADS, 0, NUM_VERTS);
178 info.ctx->end_query(info.ctx, q1);
179
180 /* draw second, small object behind first object */
181 set_vertices(obj2_vertices, sizeof(obj2_vertices));
182 info.ctx->begin_query(info.ctx, q2);
183 util_draw_arrays(info.ctx, PIPE_PRIM_QUADS, 0, NUM_VERTS);
184 info.ctx->end_query(info.ctx, q2);
185
186 info.ctx->get_query_result(info.ctx, q1, TRUE, &res1);
187 info.ctx->get_query_result(info.ctx, q2, TRUE, &res2);
188
189 printf("result1 = %lu result2 = %lu\n", res1, res2);
190 if (res1 < expected1_min || res1 > expected1_max)
191 printf(" Failure: result1 should be near %d\n", expected1);
192 if (res2 < expected2_min || res2 > expected2_max)
193 printf(" Failure: result2 should be near %d\n", expected2);
194
195 info.ctx->flush(info.ctx, NULL);
196
197 graw_util_flush_front(&info);
198
199 info.ctx->destroy_query(info.ctx, q1);
200 info.ctx->destroy_query(info.ctx, q2);
201 }
202
203
204 #if 0
205 static void
206 resize(int w, int h)
207 {
208 width = w;
209 height = h;
210
211 graw_util_viewport(&info, 0, 0, width, height, 30, 1000);
212 }
213 #endif
214
215
216 static void
217 init(void)
218 {
219 if (!graw_util_create_window(&info, width, height, 1, TRUE))
220 exit(1);
221
222 graw_util_default_state(&info, TRUE);
223
224 graw_util_viewport(&info, 0, 0, width, height, -1.0, 1.0);
225
226 set_vertex_shader(&info);
227 set_fragment_shader(&info);
228 }
229
230
231 int
232 main(int argc, char *argv[])
233 {
234 init();
235
236 printf("The red quad should mostly occlude the blue quad.\n");
237
238 graw_set_display_func(draw);
239 /*graw_set_reshape_func(resize);*/
240 graw_main_loop();
241 return 0;
242 }