Path: blob/21.2-virgl/src/gallium/tests/graw/occlusion-query.c
4565 views
/* Test gallium occlusion queries.1*/23#include <stdio.h>4#include <inttypes.h>56#include "graw_util.h"789static int width = 300;10static int height = 300;1112/* expected results of occlusion test (depndsd on window size) */13static int expected1 = (int) ((300 * 0.9) * (300 * 0.9));14static int expected2 = 420;151617static struct graw_info info;1819struct vertex {20float position[4];21float color[4];22};2324#define z0 0.225#define z1 0.62627static struct vertex obj1_vertices[4] =28{29{30{-0.9, -0.9, z0, 1.0 },31{ 1, 0, 0, 1 }32},3334{35{ 0.9, -0.9, z0, 1.0 },36{ 1, 0, 0, 1 }37},3839{40{ 0.9, 0.9, z0, 1.0 },41{ 1, 0, 0, 1 }42},4344{45{-0.9, 0.9, z0, 1.0 },46{ 1, 0, 0, 1 }47}48};4950static struct vertex obj2_vertices[4] =51{52{53{ -0.2, -0.2, z1, 1.0 },54{ 0, 0, 1, 1 }55},5657{58{ 0.95, -0.2, z1, 1.0 },59{ 0, 0, 1, 1 }60},6162{63{ 0.95, 0.2, z1, 1.0 },64{ 0, 0, 1, 1 }65},6667{68{ -0.2, 0.2, z1, 1.0 },69{ 0, 0, 1, 1 }70},71};7273#define NUM_VERTS 474757677static void78set_vertices(struct vertex *vertices, unsigned bytes)79{80struct pipe_vertex_element ve[2];81struct pipe_vertex_buffer vbuf;82void *handle;8384memset(ve, 0, sizeof ve);8586ve[0].src_offset = Offset(struct vertex, position);87ve[0].src_format = PIPE_FORMAT_R32G32B32A32_FLOAT;88ve[1].src_offset = Offset(struct vertex, color);89ve[1].src_format = PIPE_FORMAT_R32G32B32A32_FLOAT;9091handle = info.ctx->create_vertex_elements_state(info.ctx, 2, ve);92info.ctx->bind_vertex_elements_state(info.ctx, handle);939495vbuf.stride = sizeof(struct vertex);96vbuf.buffer_offset = 0;97vbuf.buffer.resource = pipe_buffer_create_with_data(info.ctx,98PIPE_BIND_VERTEX_BUFFER,99PIPE_USAGE_DEFAULT,100bytes,101vertices);102103info.ctx->set_vertex_buffers(info.ctx, 0, 1, 0, false, &vbuf);104}105106107static void108set_vertex_shader(struct graw_info *info)109{110void *handle;111const char *text =112"VERT\n"113"DCL IN[0]\n"114"DCL IN[1]\n"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"119" 2: END\n";120121handle = graw_parse_vertex_shader(info->ctx, text);122if (!handle) {123debug_printf("Failed to parse vertex shader\n");124return;125}126info->ctx->bind_vs_state(info->ctx, handle);127}128129130static void131set_fragment_shader(struct graw_info *info)132{133void *handle;134const char *text =135"FRAG\n"136"DCL IN[0], GENERIC, LINEAR\n"137"DCL OUT[0], COLOR\n"138" 0: MOV OUT[0], IN[0]\n"139" 1: END\n";140141handle = graw_parse_fragment_shader(info->ctx, text);142if (!handle) {143debug_printf("Failed to parse fragment shader\n");144return;145}146info->ctx->bind_fs_state(info->ctx, handle);147}148149150static void151draw(void)152{153int expected1_min = (int) (expected1 * 0.95);154int expected1_max = (int) (expected1 * 1.05);155int expected2_min = (int) (expected2 * 0.95);156int expected2_max = (int) (expected2 * 1.05);157158union pipe_color_union clear_color;159160struct pipe_query *q1, *q2;161union pipe_query_result res1, res2;162163clear_color.f[0] = 0.25;164clear_color.f[1] = 0.25;165clear_color.f[2] = 0.25;166clear_color.f[3] = 1.00;167168info.ctx->clear(info.ctx,169PIPE_CLEAR_COLOR | PIPE_CLEAR_DEPTHSTENCIL,170NULL,171&clear_color, 1.0, 0);172173q1 = info.ctx->create_query(info.ctx, PIPE_QUERY_OCCLUSION_COUNTER, 0);174q2 = info.ctx->create_query(info.ctx, PIPE_QUERY_OCCLUSION_COUNTER, 0);175176/* draw first, large object */177set_vertices(obj1_vertices, sizeof(obj1_vertices));178info.ctx->begin_query(info.ctx, q1);179util_draw_arrays(info.ctx, PIPE_PRIM_QUADS, 0, NUM_VERTS);180info.ctx->end_query(info.ctx, q1);181182/* draw second, small object behind first object */183set_vertices(obj2_vertices, sizeof(obj2_vertices));184info.ctx->begin_query(info.ctx, q2);185util_draw_arrays(info.ctx, PIPE_PRIM_QUADS, 0, NUM_VERTS);186info.ctx->end_query(info.ctx, q2);187188info.ctx->get_query_result(info.ctx, q1, TRUE, &res1);189info.ctx->get_query_result(info.ctx, q2, TRUE, &res2);190191printf("result1 = %" PRIu64 " result2 = %" PRIu64 "\n", res1.u64, res2.u64);192if (res1.u64 < expected1_min || res1.u64 > expected1_max)193printf(" Failure: result1 should be near %d\n", expected1);194if (res2.u64 < expected2_min || res2.u64 > expected2_max)195printf(" Failure: result2 should be near %d\n", expected2);196197info.ctx->flush(info.ctx, NULL, 0);198199graw_util_flush_front(&info);200201info.ctx->destroy_query(info.ctx, q1);202info.ctx->destroy_query(info.ctx, q2);203}204205206#if 0207static void208resize(int w, int h)209{210width = w;211height = h;212213graw_util_viewport(&info, 0, 0, width, height, 30, 1000);214}215#endif216217218static void219init(void)220{221if (!graw_util_create_window(&info, width, height, 1, TRUE))222exit(1);223224graw_util_default_state(&info, TRUE);225226graw_util_viewport(&info, 0, 0, width, height, -1.0, 1.0);227228set_vertex_shader(&info);229set_fragment_shader(&info);230}231232233int234main(int argc, char *argv[])235{236init();237238printf("The red quad should mostly occlude the blue quad.\n");239240graw_set_display_func(draw);241/*graw_set_reshape_func(resize);*/242graw_main_loop();243return 0;244}245246247