Path: blob/21.2-virgl/src/gallium/drivers/virgl/virgl_query.c
4570 views
/*1* Copyright 2014, 2015 Red Hat.2*3* Permission is hereby granted, free of charge, to any person obtaining a4* copy of this software and associated documentation files (the "Software"),5* to deal in the Software without restriction, including without limitation6* on the rights to use, copy, modify, merge, publish, distribute, sub7* license, and/or sell copies of the Software, and to permit persons to whom8* the Software is furnished to do so, subject to the following conditions:9*10* The above copyright notice and this permission notice (including the next11* paragraph) shall be included in all copies or substantial portions of the12* Software.13*14* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR15* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,16* FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL17* THE AUTHOR(S) AND/OR THEIR SUPPLIERS BE LIABLE FOR ANY CLAIM,18* DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR19* OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE20* USE OR OTHER DEALINGS IN THE SOFTWARE.21*/2223#include "util/u_memory.h"24#include "util/u_inlines.h"25#include "virgl_context.h"26#include "virgl_encode.h"27#include "virtio-gpu/virgl_protocol.h"28#include "virgl_resource.h"29#include "virgl_screen.h"3031struct virgl_query {32struct virgl_resource *buf;33uint32_t handle;34uint32_t result_size;3536bool ready;37uint64_t result;38};3940#define VIRGL_QUERY_OCCLUSION_COUNTER 041#define VIRGL_QUERY_OCCLUSION_PREDICATE 142#define VIRGL_QUERY_TIMESTAMP 243#define VIRGL_QUERY_TIMESTAMP_DISJOINT 344#define VIRGL_QUERY_TIME_ELAPSED 445#define VIRGL_QUERY_PRIMITIVES_GENERATED 546#define VIRGL_QUERY_PRIMITIVES_EMITTED 647#define VIRGL_QUERY_SO_STATISTICS 748#define VIRGL_QUERY_SO_OVERFLOW_PREDICATE 849#define VIRGL_QUERY_GPU_FINISHED 950#define VIRGL_QUERY_PIPELINE_STATISTICS 1051#define VIRGL_QUERY_OCCLUSION_PREDICATE_CONSERVATIVE 1152#define VIRGL_QUERY_SO_OVERFLOW_ANY_PREDICATE 125354static const int pquery_map[] =55{56VIRGL_QUERY_OCCLUSION_COUNTER,57VIRGL_QUERY_OCCLUSION_PREDICATE,58VIRGL_QUERY_OCCLUSION_PREDICATE_CONSERVATIVE,59VIRGL_QUERY_TIMESTAMP,60VIRGL_QUERY_TIMESTAMP_DISJOINT,61VIRGL_QUERY_TIME_ELAPSED,62VIRGL_QUERY_PRIMITIVES_GENERATED,63VIRGL_QUERY_PRIMITIVES_EMITTED,64VIRGL_QUERY_SO_STATISTICS,65VIRGL_QUERY_SO_OVERFLOW_PREDICATE,66VIRGL_QUERY_SO_OVERFLOW_ANY_PREDICATE,67VIRGL_QUERY_GPU_FINISHED,68VIRGL_QUERY_PIPELINE_STATISTICS,69};7071static int pipe_to_virgl_query(enum pipe_query_type ptype)72{73return pquery_map[ptype];74}7576static inline struct virgl_query *virgl_query(struct pipe_query *q)77{78return (struct virgl_query *)q;79}8081static void virgl_render_condition(struct pipe_context *ctx,82struct pipe_query *q,83bool condition,84enum pipe_render_cond_flag mode)85{86struct virgl_context *vctx = virgl_context(ctx);87struct virgl_query *query = virgl_query(q);88uint32_t handle = 0;89if (q)90handle = query->handle;91virgl_encoder_render_condition(vctx, handle, condition, mode);92}9394static struct pipe_query *virgl_create_query(struct pipe_context *ctx,95unsigned query_type, unsigned index)96{97struct virgl_context *vctx = virgl_context(ctx);98struct virgl_query *query;99100query = CALLOC_STRUCT(virgl_query);101if (!query)102return NULL;103104query->buf = (struct virgl_resource *)105pipe_buffer_create(ctx->screen, PIPE_BIND_CUSTOM, PIPE_USAGE_STAGING,106sizeof(struct virgl_host_query_state));107if (!query->buf) {108FREE(query);109return NULL;110}111112query->handle = virgl_object_assign_handle();113query->result_size = (query_type == PIPE_QUERY_TIMESTAMP ||114query_type == PIPE_QUERY_TIME_ELAPSED) ? 8 : 4;115116util_range_add(&query->buf->b, &query->buf->valid_buffer_range, 0,117sizeof(struct virgl_host_query_state));118virgl_resource_dirty(query->buf, 0);119120virgl_encoder_create_query(vctx, query->handle,121pipe_to_virgl_query(query_type), index, query->buf, 0);122123return (struct pipe_query *)query;124}125126static void virgl_destroy_query(struct pipe_context *ctx,127struct pipe_query *q)128{129struct virgl_context *vctx = virgl_context(ctx);130struct virgl_query *query = virgl_query(q);131132virgl_encode_delete_object(vctx, query->handle, VIRGL_OBJECT_QUERY);133134pipe_resource_reference((struct pipe_resource **)&query->buf, NULL);135FREE(query);136}137138static bool virgl_begin_query(struct pipe_context *ctx,139struct pipe_query *q)140{141struct virgl_context *vctx = virgl_context(ctx);142struct virgl_query *query = virgl_query(q);143144virgl_encoder_begin_query(vctx, query->handle);145146return true;147}148149static bool virgl_end_query(struct pipe_context *ctx,150struct pipe_query *q)151{152struct virgl_screen *vs = virgl_screen(ctx->screen);153struct virgl_context *vctx = virgl_context(ctx);154struct virgl_query *query = virgl_query(q);155struct virgl_host_query_state *host_state;156157host_state = vs->vws->resource_map(vs->vws, query->buf->hw_res);158if (!host_state)159return false;160161host_state->query_state = VIRGL_QUERY_STATE_WAIT_HOST;162query->ready = false;163164virgl_encoder_end_query(vctx, query->handle);165166/* start polling now */167virgl_encoder_get_query_result(vctx, query->handle, 0);168vs->vws->emit_res(vs->vws, vctx->cbuf, query->buf->hw_res, false);169170return true;171}172173static bool virgl_get_query_result(struct pipe_context *ctx,174struct pipe_query *q,175bool wait,176union pipe_query_result *result)177{178struct virgl_query *query = virgl_query(q);179180if (!query->ready) {181struct virgl_screen *vs = virgl_screen(ctx->screen);182struct virgl_context *vctx = virgl_context(ctx);183volatile struct virgl_host_query_state *host_state;184struct pipe_transfer *transfer = NULL;185186if (vs->vws->res_is_referenced(vs->vws, vctx->cbuf, query->buf->hw_res))187ctx->flush(ctx, NULL, 0);188189if (wait)190vs->vws->resource_wait(vs->vws, query->buf->hw_res);191else if (vs->vws->resource_is_busy(vs->vws, query->buf->hw_res))192return false;193194host_state = vs->vws->resource_map(vs->vws, query->buf->hw_res);195196/* The resource is idle and the result should be available at this point,197* unless we are dealing with an older host. In that case,198* VIRGL_CCMD_GET_QUERY_RESULT is not fenced, the buffer is not199* coherent, and transfers are unsynchronized. We have to repeatedly200* transfer until we get the result back.201*/202while (host_state->query_state != VIRGL_QUERY_STATE_DONE) {203debug_printf("VIRGL: get_query_result is forced blocking\n");204205if (transfer) {206pipe_buffer_unmap(ctx, transfer);207if (!wait)208return false;209}210211host_state = pipe_buffer_map(ctx, &query->buf->b,212PIPE_MAP_READ, &transfer);213}214215if (query->result_size == 8)216query->result = host_state->result;217else218query->result = (uint32_t) host_state->result;219220if (transfer)221pipe_buffer_unmap(ctx, transfer);222223query->ready = true;224}225226result->u64 = query->result;227228return true;229}230231static void232virgl_set_active_query_state(struct pipe_context *pipe, bool enable)233{234}235236static void237virgl_get_query_result_resource(struct pipe_context *ctx,238struct pipe_query *q,239bool wait,240enum pipe_query_value_type result_type,241int index,242struct pipe_resource *resource,243unsigned offset)244{245struct virgl_context *vctx = virgl_context(ctx);246struct virgl_query *query = virgl_query(q);247struct virgl_resource *qbo = (struct virgl_resource *)resource;248249virgl_encode_get_query_result_qbo(vctx, query->handle, qbo, wait, result_type, offset, index);250}251252void virgl_init_query_functions(struct virgl_context *vctx)253{254vctx->base.render_condition = virgl_render_condition;255vctx->base.create_query = virgl_create_query;256vctx->base.destroy_query = virgl_destroy_query;257vctx->base.begin_query = virgl_begin_query;258vctx->base.end_query = virgl_end_query;259vctx->base.get_query_result = virgl_get_query_result;260vctx->base.set_active_query_state = virgl_set_active_query_state;261vctx->base.get_query_result_resource = virgl_get_query_result_resource;262}263264265