+static inline const int
+pipeline_target_to_index(int target)
+{
+ if (target == GL_GEOMETRY_SHADER_INVOCATIONS)
+ return MAX_PIPELINE_STATISTICS - 1;
+ else
+ return target - GL_VERTICES_SUBMITTED_ARB;
+}
+
+static void
+emit_pipeline_stat(struct brw_context *brw, drm_intel_bo *bo,
+ int stream, int target, int idx)
+{
+ /* One source of confusion is the tessellation shader statistics. The
+ * hardware has no statistics specific to the TE unit. Ideally we could have
+ * the HS primitives for TESS_CONTROL_SHADER_PATCHES_ARB, and the DS
+ * invocations as the register for TESS_CONTROL_SHADER_PATCHES_ARB.
+ * Unfortunately we don't have HS primitives, we only have HS invocations.
+ */
+
+ /* Everything except GEOMETRY_SHADER_INVOCATIONS can be kept in a simple
+ * lookup table
+ */
+ static const uint32_t target_to_register[] = {
+ IA_VERTICES_COUNT, /* VERTICES_SUBMITTED */
+ IA_PRIMITIVES_COUNT, /* PRIMITIVES_SUBMITTED */
+ VS_INVOCATION_COUNT, /* VERTEX_SHADER_INVOCATIONS */
+ HS_INVOCATION_COUNT, /* TESS_CONTROL_SHADER_PATCHES */
+ DS_INVOCATION_COUNT, /* TESS_EVALUATION_SHADER_INVOCATIONS */
+ GS_PRIMITIVES_COUNT, /* GEOMETRY_SHADER_PRIMITIVES_EMITTED */
+ PS_INVOCATION_COUNT, /* FRAGMENT_SHADER_INVOCATIONS */
+ CS_INVOCATION_COUNT, /* COMPUTE_SHADER_INVOCATIONS */
+ CL_INVOCATION_COUNT, /* CLIPPING_INPUT_PRIMITIVES */
+ CL_PRIMITIVES_COUNT, /* CLIPPING_OUTPUT_PRIMITIVES */
+ GS_INVOCATION_COUNT /* This one is special... */
+ };
+ STATIC_ASSERT(ARRAY_SIZE(target_to_register) == MAX_PIPELINE_STATISTICS);
+ uint32_t reg = target_to_register[pipeline_target_to_index(target)];
+ /* Gen6 GS code counts full primitives, that is, it won't count individual
+ * triangles in a triangle strip. Use CL_INVOCATION_COUNT for that.
+ */
+ if (brw->gen == 6 && target == GL_GEOMETRY_SHADER_PRIMITIVES_EMITTED_ARB)
+ reg = CL_INVOCATION_COUNT;
+ assert(reg != 0);
+
+ /* Emit a flush to make sure various parts of the pipeline are complete and
+ * we get an accurate value
+ */
+ brw_emit_mi_flush(brw);
+
+ brw_store_register_mem64(brw, bo, reg, idx);
+}
+
+