Path: blob/21.2-virgl/src/gallium/drivers/v3d/v3d_query.c
4570 views
/*1* Copyright © 2014 Broadcom2*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* the rights to use, copy, modify, merge, publish, distribute, sublicense,7* and/or sell copies of the Software, and to permit persons to whom the8* 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 NONINFRINGEMENT. IN NO EVENT SHALL17* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER18* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING19* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS20* IN THE SOFTWARE.21*/2223/**24* Gallium query object support.25*26* The HW has native support for occlusion queries, with the query result27* being loaded and stored by the TLB unit. From a SW perspective, we have to28* be careful to make sure that the jobs that need to be tracking queries are29* bracketed by the start and end of counting, even across FBO transitions.30*31* For the transform feedback PRIMITIVES_GENERATED/WRITTEN queries, we have to32* do the calculations in software at draw time.33*/3435#include "v3d_context.h"36#include "broadcom/cle/v3d_packet_v33_pack.h"3738struct v3d_query39{40enum pipe_query_type type;41struct v3d_bo *bo;4243uint32_t start, end;44};4546static struct pipe_query *47v3d_create_query(struct pipe_context *pctx, unsigned query_type, unsigned index)48{49struct v3d_query *q = calloc(1, sizeof(*q));5051q->type = query_type;5253/* Note that struct pipe_query isn't actually defined anywhere. */54return (struct pipe_query *)q;55}5657static void58v3d_destroy_query(struct pipe_context *pctx, struct pipe_query *query)59{60struct v3d_query *q = (struct v3d_query *)query;6162v3d_bo_unreference(&q->bo);63free(q);64}6566static bool67v3d_begin_query(struct pipe_context *pctx, struct pipe_query *query)68{69struct v3d_context *v3d = v3d_context(pctx);70struct v3d_query *q = (struct v3d_query *)query;7172switch (q->type) {73case PIPE_QUERY_PRIMITIVES_GENERATED:74/* If we are using PRIMITIVE_COUNTS_FEEDBACK to retrieve75* primitive counts from the GPU (which we need when a GS76* is present), then we need to update our counters now77* to discard any primitives generated before this.78*/79if (v3d->prog.gs)80v3d_update_primitive_counters(v3d);81q->start = v3d->prims_generated;82break;83case PIPE_QUERY_PRIMITIVES_EMITTED:84/* If we are inside transform feedback we need to update the85* primitive counts to skip primitives recorded before this.86*/87if (v3d->streamout.num_targets > 0)88v3d_update_primitive_counters(v3d);89q->start = v3d->tf_prims_generated;90break;91case PIPE_QUERY_OCCLUSION_COUNTER:92case PIPE_QUERY_OCCLUSION_PREDICATE:93case PIPE_QUERY_OCCLUSION_PREDICATE_CONSERVATIVE:94q->bo = v3d_bo_alloc(v3d->screen, 4096, "query");95uint32_t *map = v3d_bo_map(q->bo);96*map = 0;9798v3d->current_oq = q->bo;99v3d->dirty |= V3D_DIRTY_OQ;100break;101default:102unreachable("unsupported query type");103}104105return true;106}107108static bool109v3d_end_query(struct pipe_context *pctx, struct pipe_query *query)110{111struct v3d_context *v3d = v3d_context(pctx);112struct v3d_query *q = (struct v3d_query *)query;113114switch (q->type) {115case PIPE_QUERY_PRIMITIVES_GENERATED:116/* If we are using PRIMITIVE_COUNTS_FEEDBACK to retrieve117* primitive counts from the GPU (which we need when a GS118* is present), then we need to update our counters now.119*/120if (v3d->prog.gs)121v3d_update_primitive_counters(v3d);122q->end = v3d->prims_generated;123break;124case PIPE_QUERY_PRIMITIVES_EMITTED:125/* If transform feedback has ended, then we have already126* updated the primitive counts at glEndTransformFeedback()127* time. Otherwise, we have to do it now.128*/129if (v3d->streamout.num_targets > 0)130v3d_update_primitive_counters(v3d);131q->end = v3d->tf_prims_generated;132break;133case PIPE_QUERY_OCCLUSION_COUNTER:134case PIPE_QUERY_OCCLUSION_PREDICATE:135case PIPE_QUERY_OCCLUSION_PREDICATE_CONSERVATIVE:136v3d->current_oq = NULL;137v3d->dirty |= V3D_DIRTY_OQ;138break;139default:140unreachable("unsupported query type");141}142143return true;144}145146static bool147v3d_get_query_result(struct pipe_context *pctx, struct pipe_query *query,148bool wait, union pipe_query_result *vresult)149{150struct v3d_context *v3d = v3d_context(pctx);151struct v3d_query *q = (struct v3d_query *)query;152uint32_t result = 0;153154if (q->bo) {155v3d_flush_jobs_using_bo(v3d, q->bo);156157if (wait) {158if (!v3d_bo_wait(q->bo, ~0ull, "query"))159return false;160} else {161if (!v3d_bo_wait(q->bo, 0, "query"))162return false;163}164165/* XXX: Sum up per-core values. */166uint32_t *map = v3d_bo_map(q->bo);167result = *map;168169v3d_bo_unreference(&q->bo);170}171172switch (q->type) {173case PIPE_QUERY_OCCLUSION_COUNTER:174vresult->u64 = result;175break;176case PIPE_QUERY_OCCLUSION_PREDICATE:177case PIPE_QUERY_OCCLUSION_PREDICATE_CONSERVATIVE:178vresult->b = result != 0;179break;180case PIPE_QUERY_PRIMITIVES_GENERATED:181case PIPE_QUERY_PRIMITIVES_EMITTED:182vresult->u64 = q->end - q->start;183break;184default:185unreachable("unsupported query type");186}187188return true;189}190191static void192v3d_set_active_query_state(struct pipe_context *pctx, bool enable)193{194struct v3d_context *v3d = v3d_context(pctx);195196v3d->active_queries = enable;197v3d->dirty |= V3D_DIRTY_OQ;198v3d->dirty |= V3D_DIRTY_STREAMOUT;199}200201void202v3d_query_init(struct pipe_context *pctx)203{204pctx->create_query = v3d_create_query;205pctx->destroy_query = v3d_destroy_query;206pctx->begin_query = v3d_begin_query;207pctx->end_query = v3d_end_query;208pctx->get_query_result = v3d_get_query_result;209pctx->set_active_query_state = v3d_set_active_query_state;210}211212213214