gallium: Add the ability to query a single pipeline statistics counter
authorKenneth Graunke <kenneth@whitecape.org>
Fri, 28 Sep 2018 09:21:47 +0000 (11:21 +0200)
committerKenneth Graunke <kenneth@whitecape.org>
Tue, 15 Jan 2019 19:43:04 +0000 (11:43 -0800)
Gallium historically has treated pipeline statistics queries as a single
query, PIPE_QUERY_PIPELINE_STATISTICS, which returns a block of 11
values.  This was originally patterned after the D3D1x API.  Much later,
Brian introduced an OpenGL extension that exposed these counters - but
it exposes 11 separate queries, each of which returns a single value.

Today, st/mesa simply queries all 11 values, and returns a single value.
While pipeline statistics counters aren't typically performance
critical, this is still not a great fit.  A D3D1x->GL translator might
request all 11 counters by creating 11 separate GL queries...which
Gallium would map to reads of all 11 values each time, resulting in a
total 121 counter reads.  That's not ideal.

This patch adds a new cap, PIPE_CAP_QUERY_PIPELINE_STATISTICS_SINGLE,
and corresponding query type PIPE_QUERY_PIPELINE_STATISTICS_SINGLE.
When calling create_query(), q->index should be set to one of the
PIPE_STAT_QUERY_* enums to select a counter.  Unlike the block query,
this returns the value in pipe_query_result::u64 (as it's a single
value) instead of the pipe_query_data_pipeline_statistics group.

We update st/mesa to expose ARB_pipeline_statistics_query if either
capability is set, preferring the new SINGLE variant when available.

Thanks to Roland, Ilia, and Marek for helping me sort this out.

Reviewed-by: Marek Olšák <marek.olsak@amd.com>
Reviewed-by: Tapani Pälli <tapani.palli@intel.com>
src/gallium/auxiliary/util/u_screen.c
src/gallium/docs/source/context.rst
src/gallium/include/pipe/p_defines.h
src/mesa/state_tracker/st_cb_queryobj.c
src/mesa/state_tracker/st_context.c
src/mesa/state_tracker/st_context.h
src/mesa/state_tracker/st_extensions.c

index f1e8eda701f82d825330e173f9606639a7d5912d..66dfa852540f7c785c81661afd36cd2cc77336c3 100644 (file)
@@ -145,6 +145,7 @@ u_pipe_screen_get_param_defaults(struct pipe_screen *pscreen,
       return 1;
 
    case PIPE_CAP_QUERY_PIPELINE_STATISTICS:
+   case PIPE_CAP_QUERY_PIPELINE_STATISTICS_SINGLE:
    case PIPE_CAP_TEXTURE_BORDER_COLOR_QUIRK:
       return 0;
 
index 20d0df793127e5a9a9c586ce62434fa0b74a910a..f89d9e1005eb6d3ba16381e3b1bf846997beff1f 100644 (file)
@@ -491,6 +491,11 @@ Number of tessellation evaluation shader threads launched.
 If a shader type is not supported by the device/driver,
 the corresponding values should be set to 0.
 
+``PIPE_QUERY_PIPELINE_STATISTICS_SINGLE`` returns a single counter from
+the ``PIPE_QUERY_PIPELINE_STATISTICS`` group.  The specific counter must
+be selected when calling ``create_query`` by passing one of the
+``PIPE_STAT_QUERY`` enums as the query's ``index``.
+
 Gallium does not guarantee the availability of any query types; one must
 always check the capabilities of the :ref:`Screen` first.
 
index 4480b54eb2fa186be3d4d01e84708b3560b23d24..d76fadadfdf2354975806da784e9ad2bc2ce8248 100644 (file)
@@ -563,6 +563,7 @@ enum pipe_query_type {
    PIPE_QUERY_SO_OVERFLOW_ANY_PREDICATE,
    PIPE_QUERY_GPU_FINISHED,
    PIPE_QUERY_PIPELINE_STATISTICS,
+   PIPE_QUERY_PIPELINE_STATISTICS_SINGLE,
    PIPE_QUERY_TYPES,
    /* start of driver queries, see pipe_screen::get_driver_query_info */
    PIPE_QUERY_DRIVER_SPECIFIC = 256,
@@ -851,6 +852,7 @@ enum pipe_cap
    PIPE_CAP_MAX_VERTEX_ELEMENT_SRC_OFFSET,
    PIPE_CAP_SURFACE_SAMPLE_COUNT,
    PIPE_CAP_TGSI_ATOMFADD,
+   PIPE_CAP_QUERY_PIPELINE_STATISTICS_SINGLE,
 };
 
 /**
index 9c551da005f8baf1d7d43e2e3b24fa6cc42ba075..abb126547c9b719194937f43b3ea9c97396bbf94 100644 (file)
@@ -88,6 +88,44 @@ st_DeleteQuery(struct gl_context *ctx, struct gl_query_object *q)
    free(stq);
 }
 
+static int
+target_to_index(const struct st_context *st, const struct gl_query_object *q)
+{
+   if (q->Target == GL_TRANSFORM_FEEDBACK_PRIMITIVES_WRITTEN ||
+       q->Target == GL_TRANSFORM_FEEDBACK_STREAM_OVERFLOW_ARB)
+      return q->Stream;
+
+   if (st->has_single_pipe_stat) {
+      switch (q->Target) {
+      case GL_VERTICES_SUBMITTED_ARB:
+         return PIPE_STAT_QUERY_IA_VERTICES;
+      case GL_PRIMITIVES_SUBMITTED_ARB:
+         return PIPE_STAT_QUERY_IA_PRIMITIVES;
+      case GL_VERTEX_SHADER_INVOCATIONS_ARB:
+         return PIPE_STAT_QUERY_VS_INVOCATIONS;
+      case GL_GEOMETRY_SHADER_INVOCATIONS:
+         return PIPE_STAT_QUERY_GS_INVOCATIONS;
+      case GL_GEOMETRY_SHADER_PRIMITIVES_EMITTED_ARB:
+         return PIPE_STAT_QUERY_GS_PRIMITIVES;
+      case GL_CLIPPING_INPUT_PRIMITIVES_ARB:
+         return PIPE_STAT_QUERY_C_INVOCATIONS;
+      case GL_CLIPPING_OUTPUT_PRIMITIVES_ARB:
+         return PIPE_STAT_QUERY_C_PRIMITIVES;
+      case GL_FRAGMENT_SHADER_INVOCATIONS_ARB:
+         return PIPE_STAT_QUERY_PS_INVOCATIONS;
+      case GL_TESS_CONTROL_SHADER_PATCHES_ARB:
+         return PIPE_STAT_QUERY_HS_INVOCATIONS;
+      case GL_TESS_EVALUATION_SHADER_INVOCATIONS_ARB:
+         return PIPE_STAT_QUERY_DS_INVOCATIONS;
+      case GL_COMPUTE_SHADER_INVOCATIONS_ARB:
+         return PIPE_STAT_QUERY_CS_INVOCATIONS;
+      default:
+         break;
+      }
+   }
+
+   return 0;
+}
 
 static void
 st_BeginQuery(struct gl_context *ctx, struct gl_query_object *q)
@@ -140,7 +178,8 @@ st_BeginQuery(struct gl_context *ctx, struct gl_query_object *q)
    case GL_COMPUTE_SHADER_INVOCATIONS_ARB:
    case GL_CLIPPING_INPUT_PRIMITIVES_ARB:
    case GL_CLIPPING_OUTPUT_PRIMITIVES_ARB:
-      type = PIPE_QUERY_PIPELINE_STATISTICS;
+      type = st->has_single_pipe_stat ? PIPE_QUERY_PIPELINE_STATISTICS_SINGLE
+                                      : PIPE_QUERY_PIPELINE_STATISTICS;
       break;
    default:
       assert(0 && "unexpected query target in st_BeginQuery()");
@@ -164,7 +203,7 @@ st_BeginQuery(struct gl_context *ctx, struct gl_query_object *q)
          ret = pipe->end_query(pipe, stq->pq_begin);
    } else {
       if (!stq->pq) {
-         stq->pq = pipe->create_query(pipe, type, q->Stream);
+         stq->pq = pipe->create_query(pipe, type, target_to_index(st, q));
          stq->type = type;
       }
       if (stq->pq)
index 354876746f44da066339f798652df9ab3f178dc3..30380446041aa07795b2fc75ca3306a32bc61fe4 100644 (file)
@@ -462,6 +462,8 @@ st_create_context_priv(struct gl_context *ctx, struct pipe_context *pipe,
       screen->get_param(screen, PIPE_CAP_TGSI_PACK_HALF_FLOAT);
    st->has_multi_draw_indirect =
       screen->get_param(screen, PIPE_CAP_MULTI_DRAW_INDIRECT);
+   st->has_single_pipe_stat =
+      screen->get_param(screen, PIPE_CAP_QUERY_PIPELINE_STATISTICS_SINGLE);
 
    st->has_hw_atomics =
       screen->get_shader_param(screen, PIPE_SHADER_FRAGMENT,
index b31e719708e369e6e97c3a5e139dac2d1a963242..8b736ebff75ff76f7cf10af1d336b333a9a178cb 100644 (file)
@@ -127,6 +127,7 @@ struct st_context
    boolean has_shareable_shaders;
    boolean has_half_float_packing;
    boolean has_multi_draw_indirect;
+   boolean has_single_pipe_stat;
    boolean can_bind_const_buffer_as_vertex;
 
    /**
index 931a2643bfa099edb3bc8e7ebfadfb490acd672c..46280792603d5daefc65b7630efadc259157ec7e 100644 (file)
@@ -704,6 +704,7 @@ void st_init_extensions(struct pipe_screen *screen,
       { o(ARB_occlusion_query),              PIPE_CAP_OCCLUSION_QUERY                  },
       { o(ARB_occlusion_query2),             PIPE_CAP_OCCLUSION_QUERY                  },
       { o(ARB_pipeline_statistics_query),    PIPE_CAP_QUERY_PIPELINE_STATISTICS        },
+      { o(ARB_pipeline_statistics_query),    PIPE_CAP_QUERY_PIPELINE_STATISTICS_SINGLE },
       { o(ARB_point_sprite),                 PIPE_CAP_POINT_SPRITE                     },
       { o(ARB_polygon_offset_clamp),         PIPE_CAP_POLYGON_OFFSET_CLAMP             },
       { o(ARB_post_depth_coverage),          PIPE_CAP_POST_DEPTH_COVERAGE              },