• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /* Test gallium occlusion queries.
2  */
3 
4 #include <stdio.h>
5 #include <inttypes.h>
6 
7 #include "graw_util.h"
8 
9 
10 static int width = 300;
11 static int height = 300;
12 
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;
16 
17 
18 static struct graw_info info;
19 
20 struct vertex {
21    float position[4];
22    float color[4];
23 };
24 
25 #define z0 0.2
26 #define z1 0.6
27 
28 static struct vertex obj1_vertices[4] =
29 {
30    {
31       {-0.9, -0.9, z0, 1.0 },
32       { 1, 0, 0, 1 }
33    },
34 
35    {
36       { 0.9, -0.9, z0, 1.0 },
37       { 1, 0, 0, 1 }
38    },
39 
40    {
41       { 0.9,  0.9, z0, 1.0 },
42       { 1, 0, 0, 1 }
43    },
44 
45    {
46       {-0.9,  0.9, z0, 1.0 },
47       { 1, 0, 0, 1 }
48    }
49 };
50 
51 static struct vertex obj2_vertices[4] =
52 {
53    {
54       { -0.2,  -0.2, z1, 1.0 },
55       { 0, 0, 1, 1 }
56    },
57 
58    {
59       { 0.95, -0.2, z1, 1.0 },
60       { 0, 0, 1, 1 }
61    },
62 
63    {
64       { 0.95,  0.2, z1, 1.0 },
65       { 0, 0, 1, 1 }
66    },
67 
68    {
69       { -0.2, 0.2, z1, 1.0 },
70       { 0, 0, 1, 1 }
71    },
72 };
73 
74 #define NUM_VERTS 4
75 
76 
77 
78 static void
set_vertices(struct vertex * vertices,unsigned bytes)79 set_vertices(struct vertex *vertices, unsigned bytes)
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 
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,
100                                               PIPE_USAGE_DEFAULT,
101                                               bytes,
102                                               vertices);
103 
104    info.ctx->set_vertex_buffers(info.ctx, 0, 1, 0, false, &vbuf);
105 }
106 
107 
108 static void
set_vertex_shader(struct graw_info * info)109 set_vertex_shader(struct graw_info *info)
110 {
111    void *handle;
112    const char *text =
113       "VERT\n"
114       "DCL IN[0]\n"
115       "DCL IN[1]\n"
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"
120       "  2: END\n";
121 
122    handle = graw_parse_vertex_shader(info->ctx, text);
123    if (!handle) {
124       debug_printf("Failed to parse vertex shader\n");
125       return;
126    }
127    info->ctx->bind_vs_state(info->ctx, handle);
128 }
129 
130 
131 static void
set_fragment_shader(struct graw_info * info)132 set_fragment_shader(struct graw_info *info)
133 {
134    void *handle;
135    const char *text =
136       "FRAG\n"
137       "DCL IN[0], GENERIC, LINEAR\n"
138       "DCL OUT[0], COLOR\n"
139       " 0: MOV OUT[0], IN[0]\n"
140       " 1: END\n";
141 
142    handle = graw_parse_fragment_shader(info->ctx, text);
143    if (!handle) {
144       debug_printf("Failed to parse fragment shader\n");
145       return;
146    }
147    info->ctx->bind_fs_state(info->ctx, handle);
148 }
149 
150 
151 static void
draw(void)152 draw(void)
153 {
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);
158 
159    union pipe_color_union clear_color;
160 
161    struct pipe_query *q1, *q2;
162    union pipe_query_result res1, res2;
163 
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;
168 
169    info.ctx->clear(info.ctx,
170                    PIPE_CLEAR_COLOR | PIPE_CLEAR_DEPTHSTENCIL,
171                    NULL,
172                    &clear_color, 1.0, 0);
173 
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);
176 
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);
182 
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);
188 
189    info.ctx->get_query_result(info.ctx, q1, TRUE, &res1);
190    info.ctx->get_query_result(info.ctx, q2, TRUE, &res2);
191 
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);
197 
198    info.ctx->flush(info.ctx, NULL, 0);
199 
200    graw_util_flush_front(&info);
201 
202    info.ctx->destroy_query(info.ctx, q1);
203    info.ctx->destroy_query(info.ctx, q2);
204 }
205 
206 
207 #if 0
208 static void
209 resize(int w, int h)
210 {
211    width = w;
212    height = h;
213 
214    graw_util_viewport(&info, 0, 0, width, height, 30, 1000);
215 }
216 #endif
217 
218 
219 static void
init(void)220 init(void)
221 {
222    if (!graw_util_create_window(&info, width, height, 1, TRUE))
223       exit(1);
224 
225    graw_util_default_state(&info, TRUE);
226 
227    graw_util_viewport(&info, 0, 0, width, height, -1.0, 1.0);
228 
229    set_vertex_shader(&info);
230    set_fragment_shader(&info);
231 }
232 
233 
234 int
main(int argc,char * argv[])235 main(int argc, char *argv[])
236 {
237    init();
238 
239    printf("The red quad should mostly occlude the blue quad.\n");
240 
241    graw_set_display_func(draw);
242    /*graw_set_reshape_func(resize);*/
243    graw_main_loop();
244    return 0;
245 }
246