1 /* Test gallium occlusion queries.
10 static int width
= 300;
11 static int height
= 300;
13 /* expected results of occlusion test (depndsd on window size) */
14 static int expected1
= (int) ((300 * 0.9) * (300 * 0.9));
15 static int expected2
= 420;
18 static struct graw_info info
;
28 static struct vertex obj1_vertices
[4] =
31 {-0.9, -0.9, z0
, 1.0 },
36 { 0.9, -0.9, z0
, 1.0 },
41 { 0.9, 0.9, z0
, 1.0 },
46 {-0.9, 0.9, z0
, 1.0 },
51 static struct vertex obj2_vertices
[4] =
54 { -0.2, -0.2, z1
, 1.0 },
59 { 0.95, -0.2, z1
, 1.0 },
64 { 0.95, 0.2, z1
, 1.0 },
69 { -0.2, 0.2, z1
, 1.0 },
79 set_vertices(struct vertex
*vertices
, unsigned bytes
)
81 struct pipe_vertex_element ve
[2];
82 struct pipe_vertex_buffer vbuf
;
85 memset(ve
, 0, sizeof ve
);
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
;
92 handle
= info
.ctx
->create_vertex_elements_state(info
.ctx
, 2, ve
);
93 info
.ctx
->bind_vertex_elements_state(info
.ctx
, handle
);
96 vbuf
.stride
= sizeof(struct vertex
);
97 vbuf
.buffer_offset
= 0;
98 vbuf
.buffer
.resource
= pipe_buffer_create_with_data(info
.ctx
,
99 PIPE_BIND_VERTEX_BUFFER
,
104 info
.ctx
->set_vertex_buffers(info
.ctx
, 0, 1, &vbuf
);
109 set_vertex_shader(struct graw_info
*info
)
116 "DCL OUT[0], POSITION\n"
117 "DCL OUT[1], GENERIC[0]\n"
118 " 0: MOV OUT[0], IN[0]\n"
119 " 1: MOV OUT[1], IN[1]\n"
122 handle
= graw_parse_vertex_shader(info
->ctx
, text
);
124 debug_printf("Failed to parse vertex shader\n");
127 info
->ctx
->bind_vs_state(info
->ctx
, handle
);
132 set_fragment_shader(struct graw_info
*info
)
137 "DCL IN[0], GENERIC, LINEAR\n"
138 "DCL OUT[0], COLOR\n"
139 " 0: MOV OUT[0], IN[0]\n"
142 handle
= graw_parse_fragment_shader(info
->ctx
, text
);
144 debug_printf("Failed to parse fragment shader\n");
147 info
->ctx
->bind_fs_state(info
->ctx
, handle
);
154 int expected1_min
= (int) (expected1
* 0.95);
155 int expected1_max
= (int) (expected1
* 1.05);
156 int expected2_min
= (int) (expected2
* 0.95);
157 int expected2_max
= (int) (expected2
* 1.05);
159 union pipe_color_union clear_color
;
161 struct pipe_query
*q1
, *q2
;
162 union pipe_query_result res1
, res2
;
164 clear_color
.f
[0] = 0.25;
165 clear_color
.f
[1] = 0.25;
166 clear_color
.f
[2] = 0.25;
167 clear_color
.f
[3] = 1.00;
169 info
.ctx
->clear(info
.ctx
,
170 PIPE_CLEAR_COLOR
| PIPE_CLEAR_DEPTHSTENCIL
,
172 &clear_color
, 1.0, 0);
174 q1
= info
.ctx
->create_query(info
.ctx
, PIPE_QUERY_OCCLUSION_COUNTER
, 0);
175 q2
= info
.ctx
->create_query(info
.ctx
, PIPE_QUERY_OCCLUSION_COUNTER
, 0);
177 /* draw first, large object */
178 set_vertices(obj1_vertices
, sizeof(obj1_vertices
));
179 info
.ctx
->begin_query(info
.ctx
, q1
);
180 util_draw_arrays(info
.ctx
, PIPE_PRIM_QUADS
, 0, NUM_VERTS
);
181 info
.ctx
->end_query(info
.ctx
, q1
);
183 /* draw second, small object behind first object */
184 set_vertices(obj2_vertices
, sizeof(obj2_vertices
));
185 info
.ctx
->begin_query(info
.ctx
, q2
);
186 util_draw_arrays(info
.ctx
, PIPE_PRIM_QUADS
, 0, NUM_VERTS
);
187 info
.ctx
->end_query(info
.ctx
, q2
);
189 info
.ctx
->get_query_result(info
.ctx
, q1
, TRUE
, &res1
);
190 info
.ctx
->get_query_result(info
.ctx
, q2
, TRUE
, &res2
);
192 printf("result1 = %" PRIu64
" result2 = %" PRIu64
"\n", res1
.u64
, res2
.u64
);
193 if (res1
.u64
< expected1_min
|| res1
.u64
> expected1_max
)
194 printf(" Failure: result1 should be near %d\n", expected1
);
195 if (res2
.u64
< expected2_min
|| res2
.u64
> expected2_max
)
196 printf(" Failure: result2 should be near %d\n", expected2
);
198 info
.ctx
->flush(info
.ctx
, NULL
, 0);
200 graw_util_flush_front(&info
);
202 info
.ctx
->destroy_query(info
.ctx
, q1
);
203 info
.ctx
->destroy_query(info
.ctx
, q2
);
214 graw_util_viewport(&info
, 0, 0, width
, height
, 30, 1000);
222 if (!graw_util_create_window(&info
, width
, height
, 1, TRUE
))
225 graw_util_default_state(&info
, TRUE
);
227 graw_util_viewport(&info
, 0, 0, width
, height
, -1.0, 1.0);
229 set_vertex_shader(&info
);
230 set_fragment_shader(&info
);
235 main(int argc
, char *argv
[])
239 printf("The red quad should mostly occlude the blue quad.\n");
241 graw_set_display_func(draw
);
242 /*graw_set_reshape_func(resize);*/