1 /* Test gallium occlusion queries.
9 static int width
= 300;
10 static int height
= 300;
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;
17 static struct graw_info info
;
27 static struct vertex obj1_vertices
[4] =
30 {-0.9, -0.9, z0
, 1.0 },
35 { 0.9, -0.9, z0
, 1.0 },
40 { 0.9, 0.9, z0
, 1.0 },
45 {-0.9, 0.9, z0
, 1.0 },
50 static struct vertex obj2_vertices
[4] =
53 { -0.2, -0.2, z1
, 1.0 },
58 { 0.95, -0.2, z1
, 1.0 },
63 { 0.95, 0.2, z1
, 1.0 },
68 { -0.2, 0.2, z1
, 1.0 },
78 set_vertices(struct vertex
*vertices
, unsigned bytes
)
80 struct pipe_vertex_element ve
[2];
81 struct pipe_vertex_buffer vbuf
;
84 memset(ve
, 0, sizeof ve
);
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
;
91 handle
= info
.ctx
->create_vertex_elements_state(info
.ctx
, 2, ve
);
92 info
.ctx
->bind_vertex_elements_state(info
.ctx
, handle
);
95 vbuf
.stride
= sizeof(struct vertex
);
96 vbuf
.buffer_offset
= 0;
97 vbuf
.buffer
= pipe_buffer_create_with_data(info
.ctx
,
98 PIPE_BIND_VERTEX_BUFFER
,
103 info
.ctx
->set_vertex_buffers(info
.ctx
, 0, 1, &vbuf
);
108 set_vertex_shader(struct graw_info
*info
)
115 "DCL OUT[0], POSITION\n"
116 "DCL OUT[1], GENERIC[0]\n"
117 " 0: MOV OUT[0], IN[0]\n"
118 " 1: MOV OUT[1], IN[1]\n"
121 handle
= graw_parse_vertex_shader(info
->ctx
, text
);
123 debug_printf("Failed to parse vertex shader\n");
126 info
->ctx
->bind_vs_state(info
->ctx
, handle
);
131 set_fragment_shader(struct graw_info
*info
)
136 "DCL IN[0], GENERIC, LINEAR\n"
137 "DCL OUT[0], COLOR\n"
138 " 0: MOV OUT[0], IN[0]\n"
141 handle
= graw_parse_fragment_shader(info
->ctx
, text
);
143 debug_printf("Failed to parse fragment shader\n");
146 info
->ctx
->bind_fs_state(info
->ctx
, handle
);
153 int expected1_min
= (int) (expected1
* 0.95);
154 int expected1_max
= (int) (expected1
* 1.05);
155 int expected2_min
= (int) (expected2
* 0.95);
156 int expected2_max
= (int) (expected2
* 1.05);
158 union pipe_color_union clear_color
;
160 struct pipe_query
*q1
, *q2
;
161 union pipe_query_result res1
, res2
;
163 clear_color
.f
[0] = 0.25;
164 clear_color
.f
[1] = 0.25;
165 clear_color
.f
[2] = 0.25;
166 clear_color
.f
[3] = 1.00;
168 info
.ctx
->clear(info
.ctx
,
169 PIPE_CLEAR_COLOR
| PIPE_CLEAR_DEPTHSTENCIL
,
170 &clear_color
, 1.0, 0);
172 q1
= info
.ctx
->create_query(info
.ctx
, PIPE_QUERY_OCCLUSION_COUNTER
, 0);
173 q2
= info
.ctx
->create_query(info
.ctx
, PIPE_QUERY_OCCLUSION_COUNTER
, 0);
175 /* draw first, large object */
176 set_vertices(obj1_vertices
, sizeof(obj1_vertices
));
177 info
.ctx
->begin_query(info
.ctx
, q1
);
178 util_draw_arrays(info
.ctx
, PIPE_PRIM_QUADS
, 0, NUM_VERTS
);
179 info
.ctx
->end_query(info
.ctx
, q1
);
181 /* draw second, small object behind first object */
182 set_vertices(obj2_vertices
, sizeof(obj2_vertices
));
183 info
.ctx
->begin_query(info
.ctx
, q2
);
184 util_draw_arrays(info
.ctx
, PIPE_PRIM_QUADS
, 0, NUM_VERTS
);
185 info
.ctx
->end_query(info
.ctx
, q2
);
187 info
.ctx
->get_query_result(info
.ctx
, q1
, TRUE
, &res1
);
188 info
.ctx
->get_query_result(info
.ctx
, q2
, TRUE
, &res2
);
190 printf("result1 = %lu result2 = %lu\n", res1
.u64
, res2
.u64
);
191 if (res1
.u64
< expected1_min
|| res1
.u64
> expected1_max
)
192 printf(" Failure: result1 should be near %d\n", expected1
);
193 if (res2
.u64
< expected2_min
|| res2
.u64
> expected2_max
)
194 printf(" Failure: result2 should be near %d\n", expected2
);
196 info
.ctx
->flush(info
.ctx
, NULL
, 0);
198 graw_util_flush_front(&info
);
200 info
.ctx
->destroy_query(info
.ctx
, q1
);
201 info
.ctx
->destroy_query(info
.ctx
, q2
);
212 graw_util_viewport(&info
, 0, 0, width
, height
, 30, 1000);
220 if (!graw_util_create_window(&info
, width
, height
, 1, TRUE
))
223 graw_util_default_state(&info
, TRUE
);
225 graw_util_viewport(&info
, 0, 0, width
, height
, -1.0, 1.0);
227 set_vertex_shader(&info
);
228 set_fragment_shader(&info
);
233 main(int argc
, char *argv
[])
237 printf("The red quad should mostly occlude the blue quad.\n");
239 graw_set_display_func(draw
);
240 /*graw_set_reshape_func(resize);*/