Path: blob/21.2-virgl/src/gallium/drivers/softpipe/sp_query.c
4570 views
/**************************************************************************1*2* Copyright 2007 VMware, Inc.3* All Rights Reserved.4*5* Permission is hereby granted, free of charge, to any person obtaining a6* copy of this software and associated documentation files (the7* "Software"), to deal in the Software without restriction, including8* without limitation the rights to use, copy, modify, merge, publish,9* distribute, sub license, and/or sell copies of the Software, and to10* permit persons to whom the Software is furnished to do so, subject to11* the following conditions:12*13* The above copyright notice and this permission notice (including the14* next paragraph) shall be included in all copies or substantial portions15* of the Software.16*17* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS18* OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF19* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.20* IN NO EVENT SHALL VMWARE AND/OR ITS SUPPLIERS BE LIABLE FOR21* ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,22* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE23* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.24*25**************************************************************************/2627/* Author:28* Keith Whitwell <[email protected]>29*/3031#include "draw/draw_context.h"32#include "util/os_time.h"33#include "pipe/p_defines.h"34#include "util/u_memory.h"35#include "sp_context.h"36#include "sp_query.h"37#include "sp_state.h"3839struct softpipe_query {40unsigned type;41unsigned index;42uint64_t start;43uint64_t end;44struct pipe_query_data_so_statistics so[PIPE_MAX_VERTEX_STREAMS];45struct pipe_query_data_pipeline_statistics stats;46};474849static struct softpipe_query *softpipe_query( struct pipe_query *p )50{51return (struct softpipe_query *)p;52}5354static struct pipe_query *55softpipe_create_query(struct pipe_context *pipe,56unsigned type,57unsigned index)58{59struct softpipe_query* sq;6061assert(type == PIPE_QUERY_OCCLUSION_COUNTER ||62type == PIPE_QUERY_OCCLUSION_PREDICATE ||63type == PIPE_QUERY_OCCLUSION_PREDICATE_CONSERVATIVE ||64type == PIPE_QUERY_TIME_ELAPSED ||65type == PIPE_QUERY_SO_STATISTICS ||66type == PIPE_QUERY_SO_OVERFLOW_PREDICATE ||67type == PIPE_QUERY_SO_OVERFLOW_ANY_PREDICATE ||68type == PIPE_QUERY_PRIMITIVES_EMITTED ||69type == PIPE_QUERY_PRIMITIVES_GENERATED ||70type == PIPE_QUERY_PIPELINE_STATISTICS ||71type == PIPE_QUERY_GPU_FINISHED ||72type == PIPE_QUERY_TIMESTAMP ||73type == PIPE_QUERY_TIMESTAMP_DISJOINT);74sq = CALLOC_STRUCT( softpipe_query );75sq->type = type;76sq->index = index;77return (struct pipe_query *)sq;78}798081static void82softpipe_destroy_query(struct pipe_context *pipe, struct pipe_query *q)83{84FREE(q);85}868788static bool89softpipe_begin_query(struct pipe_context *pipe, struct pipe_query *q)90{91struct softpipe_context *softpipe = softpipe_context( pipe );92struct softpipe_query *sq = softpipe_query(q);9394switch (sq->type) {95case PIPE_QUERY_OCCLUSION_COUNTER:96case PIPE_QUERY_OCCLUSION_PREDICATE:97case PIPE_QUERY_OCCLUSION_PREDICATE_CONSERVATIVE:98sq->start = softpipe->occlusion_count;99break;100case PIPE_QUERY_TIME_ELAPSED:101sq->start = os_time_get_nano();102break;103case PIPE_QUERY_SO_STATISTICS:104sq->so[sq->index].num_primitives_written = softpipe->so_stats[sq->index].num_primitives_written;105sq->so[sq->index].primitives_storage_needed = softpipe->so_stats[sq->index].primitives_storage_needed;106break;107case PIPE_QUERY_SO_OVERFLOW_PREDICATE:108sq->so[sq->index].num_primitives_written = softpipe->so_stats[sq->index].num_primitives_written;109sq->so[sq->index].primitives_storage_needed = softpipe->so_stats[sq->index].primitives_storage_needed;110break;111case PIPE_QUERY_SO_OVERFLOW_ANY_PREDICATE:112for (unsigned i = 0; i < PIPE_MAX_VERTEX_STREAMS; i++) {113sq->so[i].num_primitives_written = softpipe->so_stats[i].num_primitives_written;114sq->so[i].primitives_storage_needed = softpipe->so_stats[i].primitives_storage_needed;115}116break;117case PIPE_QUERY_PRIMITIVES_EMITTED:118sq->so[sq->index].num_primitives_written = softpipe->so_stats[sq->index].num_primitives_written;119break;120case PIPE_QUERY_PRIMITIVES_GENERATED:121sq->so[sq->index].primitives_storage_needed = softpipe->so_stats[sq->index].primitives_storage_needed;122break;123case PIPE_QUERY_TIMESTAMP:124case PIPE_QUERY_GPU_FINISHED:125case PIPE_QUERY_TIMESTAMP_DISJOINT:126break;127case PIPE_QUERY_PIPELINE_STATISTICS:128/* reset our cache */129if (softpipe->active_statistics_queries == 0) {130memset(&softpipe->pipeline_statistics, 0,131sizeof(softpipe->pipeline_statistics));132}133memcpy(&sq->stats, &softpipe->pipeline_statistics,134sizeof(sq->stats));135softpipe->active_statistics_queries++;136break;137default:138assert(0);139break;140}141softpipe->active_query_count++;142softpipe->dirty |= SP_NEW_QUERY;143return true;144}145146147static bool148softpipe_end_query(struct pipe_context *pipe, struct pipe_query *q)149{150struct softpipe_context *softpipe = softpipe_context( pipe );151struct softpipe_query *sq = softpipe_query(q);152153softpipe->active_query_count--;154switch (sq->type) {155case PIPE_QUERY_OCCLUSION_COUNTER:156case PIPE_QUERY_OCCLUSION_PREDICATE:157case PIPE_QUERY_OCCLUSION_PREDICATE_CONSERVATIVE:158sq->end = softpipe->occlusion_count;159break;160case PIPE_QUERY_TIMESTAMP:161sq->start = 0;162FALLTHROUGH;163case PIPE_QUERY_TIME_ELAPSED:164sq->end = os_time_get_nano();165break;166case PIPE_QUERY_SO_OVERFLOW_PREDICATE:167sq->so[sq->index].num_primitives_written =168softpipe->so_stats[sq->index].num_primitives_written - sq->so[sq->index].num_primitives_written;169sq->so[sq->index].primitives_storage_needed =170softpipe->so_stats[sq->index].primitives_storage_needed - sq->so[sq->index].primitives_storage_needed;171sq->end = sq->so[sq->index].primitives_storage_needed > sq->so[sq->index].num_primitives_written;172break;173case PIPE_QUERY_SO_OVERFLOW_ANY_PREDICATE:174sq->end = 0;175for (unsigned i = 0; i < PIPE_MAX_VERTEX_STREAMS; i++) {176sq->so[i].num_primitives_written =177softpipe->so_stats[i].num_primitives_written - sq->so[i].num_primitives_written;178sq->so[i].primitives_storage_needed =179softpipe->so_stats[i].primitives_storage_needed - sq->so[i].primitives_storage_needed;180sq->end |= sq->so[i].primitives_storage_needed > sq->so[i].num_primitives_written;181}182break;183case PIPE_QUERY_SO_STATISTICS:184sq->so[sq->index].num_primitives_written =185softpipe->so_stats[sq->index].num_primitives_written - sq->so[sq->index].num_primitives_written;186sq->so[sq->index].primitives_storage_needed =187softpipe->so_stats[sq->index].primitives_storage_needed - sq->so[sq->index].primitives_storage_needed;188break;189case PIPE_QUERY_PRIMITIVES_EMITTED:190sq->so[sq->index].num_primitives_written =191softpipe->so_stats[sq->index].num_primitives_written - sq->so[sq->index].num_primitives_written;192break;193case PIPE_QUERY_PRIMITIVES_GENERATED:194sq->so[sq->index].primitives_storage_needed =195softpipe->so_stats[sq->index].primitives_storage_needed - sq->so[sq->index].primitives_storage_needed;196break;197case PIPE_QUERY_GPU_FINISHED:198case PIPE_QUERY_TIMESTAMP_DISJOINT:199break;200case PIPE_QUERY_PIPELINE_STATISTICS:201sq->stats.ia_vertices =202softpipe->pipeline_statistics.ia_vertices - sq->stats.ia_vertices;203sq->stats.ia_primitives =204softpipe->pipeline_statistics.ia_primitives - sq->stats.ia_primitives;205sq->stats.vs_invocations =206softpipe->pipeline_statistics.vs_invocations - sq->stats.vs_invocations;207sq->stats.gs_invocations =208softpipe->pipeline_statistics.gs_invocations - sq->stats.gs_invocations;209sq->stats.gs_primitives =210softpipe->pipeline_statistics.gs_primitives - sq->stats.gs_primitives;211sq->stats.c_invocations =212softpipe->pipeline_statistics.c_invocations - sq->stats.c_invocations;213sq->stats.c_primitives =214softpipe->pipeline_statistics.c_primitives - sq->stats.c_primitives;215sq->stats.ps_invocations =216softpipe->pipeline_statistics.ps_invocations - sq->stats.ps_invocations;217sq->stats.cs_invocations =218softpipe->pipeline_statistics.cs_invocations - sq->stats.cs_invocations;219220softpipe->active_statistics_queries--;221break;222default:223assert(0);224break;225}226softpipe->dirty |= SP_NEW_QUERY;227return true;228}229230231static bool232softpipe_get_query_result(struct pipe_context *pipe,233struct pipe_query *q,234bool wait,235union pipe_query_result *vresult)236{237struct softpipe_query *sq = softpipe_query(q);238uint64_t *result = (uint64_t*)vresult;239240switch (sq->type) {241case PIPE_QUERY_SO_STATISTICS: {242struct pipe_query_data_so_statistics *stats =243(struct pipe_query_data_so_statistics *)vresult;244stats->num_primitives_written = sq->so[sq->index].num_primitives_written;245stats->primitives_storage_needed = sq->so[sq->index].primitives_storage_needed;246}247break;248case PIPE_QUERY_PIPELINE_STATISTICS:249memcpy(vresult, &sq->stats,250sizeof(struct pipe_query_data_pipeline_statistics));251break;252case PIPE_QUERY_GPU_FINISHED:253vresult->b = true;254break;255case PIPE_QUERY_SO_OVERFLOW_PREDICATE:256case PIPE_QUERY_SO_OVERFLOW_ANY_PREDICATE:257vresult->b = sq->end != 0;258break;259case PIPE_QUERY_TIMESTAMP_DISJOINT: {260struct pipe_query_data_timestamp_disjoint *td =261(struct pipe_query_data_timestamp_disjoint *)vresult;262/* os_get_time_nano return nanoseconds */263td->frequency = UINT64_C(1000000000);264td->disjoint = false;265}266break;267case PIPE_QUERY_PRIMITIVES_EMITTED:268*result = sq->so[sq->index].num_primitives_written;269break;270case PIPE_QUERY_PRIMITIVES_GENERATED:271*result = sq->so[sq->index].primitives_storage_needed;272break;273case PIPE_QUERY_OCCLUSION_PREDICATE:274case PIPE_QUERY_OCCLUSION_PREDICATE_CONSERVATIVE:275vresult->b = sq->end - sq->start != 0;276break;277default:278*result = sq->end - sq->start;279break;280}281return true;282}283284static bool285is_result_nonzero(struct pipe_query *q,286union pipe_query_result *vresult)287{288struct softpipe_query *sq = softpipe_query(q);289290switch (sq->type) {291case PIPE_QUERY_TIMESTAMP_DISJOINT:292case PIPE_QUERY_SO_STATISTICS:293case PIPE_QUERY_PIPELINE_STATISTICS:294unreachable("unpossible");295break;296case PIPE_QUERY_GPU_FINISHED:297case PIPE_QUERY_OCCLUSION_PREDICATE:298case PIPE_QUERY_OCCLUSION_PREDICATE_CONSERVATIVE:299case PIPE_QUERY_SO_OVERFLOW_PREDICATE:300case PIPE_QUERY_SO_OVERFLOW_ANY_PREDICATE:301return vresult->b;302default:303return !!vresult->u64;304}305return false;306}307308/**309* Called by rendering function to check rendering is conditional.310* \return TRUE if we should render, FALSE if we should skip rendering311*/312boolean313softpipe_check_render_cond(struct softpipe_context *sp)314{315struct pipe_context *pipe = &sp->pipe;316boolean b, wait;317union pipe_query_result result;318memset(&result, 0, sizeof(union pipe_query_result));319320if (!sp->render_cond_query) {321return TRUE; /* no query predicate, draw normally */322}323324wait = (sp->render_cond_mode == PIPE_RENDER_COND_WAIT ||325sp->render_cond_mode == PIPE_RENDER_COND_BY_REGION_WAIT);326327b = pipe->get_query_result(pipe, sp->render_cond_query, wait,328&result);329if (b)330return !is_result_nonzero(sp->render_cond_query, &result) == sp->render_cond_cond;331else332return TRUE;333}334335336static void337softpipe_set_active_query_state(struct pipe_context *pipe, bool enable)338{339}340341342void softpipe_init_query_funcs(struct softpipe_context *softpipe )343{344softpipe->pipe.create_query = softpipe_create_query;345softpipe->pipe.destroy_query = softpipe_destroy_query;346softpipe->pipe.begin_query = softpipe_begin_query;347softpipe->pipe.end_query = softpipe_end_query;348softpipe->pipe.get_query_result = softpipe_get_query_result;349softpipe->pipe.set_active_query_state = softpipe_set_active_query_state;350}351352353354355