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