xref: /aosp_15_r20/external/mesa3d/src/gallium/drivers/freedreno/a3xx/fd3_query.c (revision 6104692788411f58d303aa86923a9ff6ecaded22)
1 /*
2  * Copyright © 2014 Rob Clark <[email protected]>
3  * SPDX-License-Identifier: MIT
4  *
5  * Authors:
6  *    Rob Clark <[email protected]>
7  */
8 
9 #include "freedreno_batch.h"
10 #include "freedreno_context.h"
11 #include "freedreno_query_hw.h"
12 #include "freedreno_util.h"
13 
14 #include "fd3_format.h"
15 #include "fd3_query.h"
16 
17 struct fd_rb_samp_ctrs {
18    uint64_t ctr[16];
19 };
20 
21 /*
22  * Occlusion Query:
23  *
24  * OCCLUSION_COUNTER and OCCLUSION_PREDICATE differ only in how they
25  * interpret results
26  */
27 
28 static struct fd_hw_sample *
occlusion_get_sample(struct fd_batch * batch,struct fd_ringbuffer * ring)29 occlusion_get_sample(struct fd_batch *batch, struct fd_ringbuffer *ring)
30 {
31    struct fd_hw_sample *samp =
32       fd_hw_sample_init(batch, sizeof(struct fd_rb_samp_ctrs));
33 
34    /* Set RB_SAMPLE_COUNT_ADDR to samp->offset plus value of
35     * HW_QUERY_BASE_REG register:
36     */
37    OUT_PKT3(ring, CP_SET_CONSTANT, 3);
38    OUT_RING(ring, CP_REG(REG_A3XX_RB_SAMPLE_COUNT_ADDR) | 0x80000000);
39    OUT_RING(ring, HW_QUERY_BASE_REG);
40    OUT_RING(ring, samp->offset);
41 
42    OUT_PKT0(ring, REG_A3XX_RB_SAMPLE_COUNT_CONTROL, 1);
43    OUT_RING(ring, A3XX_RB_SAMPLE_COUNT_CONTROL_COPY);
44 
45    OUT_PKT3(ring, CP_DRAW_INDX, 3);
46    OUT_RING(ring, 0x00000000);
47    OUT_RING(ring, DRAW(DI_PT_POINTLIST_PSIZE, DI_SRC_SEL_AUTO_INDEX,
48                        INDEX_SIZE_IGN, USE_VISIBILITY, 0));
49    OUT_RING(ring, 0); /* NumIndices */
50 
51    fd_event_write(batch, ring, ZPASS_DONE);
52 
53    OUT_PKT0(ring, REG_A3XX_RBBM_PERFCTR_CTL, 1);
54    OUT_RING(ring, A3XX_RBBM_PERFCTR_CTL_ENABLE);
55 
56    OUT_PKT0(ring, REG_A3XX_VBIF_PERF_CNT_EN, 1);
57    OUT_RING(ring, A3XX_VBIF_PERF_CNT_EN_CNT0 | A3XX_VBIF_PERF_CNT_EN_CNT1 |
58                      A3XX_VBIF_PERF_CNT_EN_PWRCNT0 |
59                      A3XX_VBIF_PERF_CNT_EN_PWRCNT1 |
60                      A3XX_VBIF_PERF_CNT_EN_PWRCNT2);
61 
62    return samp;
63 }
64 
65 static uint64_t
count_samples(const struct fd_rb_samp_ctrs * start,const struct fd_rb_samp_ctrs * end)66 count_samples(const struct fd_rb_samp_ctrs *start,
67               const struct fd_rb_samp_ctrs *end)
68 {
69    uint64_t n = 0;
70    unsigned i;
71 
72    /* not quite sure what all of these are, possibly different
73     * counters for each MRT render target:
74     */
75    for (i = 0; i < 16; i += 4)
76       n += end->ctr[i] - start->ctr[i];
77 
78    return n;
79 }
80 
81 static void
occlusion_counter_accumulate_result(struct fd_context * ctx,const void * start,const void * end,union pipe_query_result * result)82 occlusion_counter_accumulate_result(struct fd_context *ctx, const void *start,
83                                     const void *end,
84                                     union pipe_query_result *result)
85 {
86    uint64_t n = count_samples(start, end);
87    result->u64 += n;
88 }
89 
90 static void
occlusion_predicate_accumulate_result(struct fd_context * ctx,const void * start,const void * end,union pipe_query_result * result)91 occlusion_predicate_accumulate_result(struct fd_context *ctx, const void *start,
92                                       const void *end,
93                                       union pipe_query_result *result)
94 {
95    uint64_t n = count_samples(start, end);
96    result->b |= (n > 0);
97 }
98 
99 static const struct fd_hw_sample_provider occlusion_counter = {
100    .query_type = PIPE_QUERY_OCCLUSION_COUNTER,
101    .get_sample = occlusion_get_sample,
102    .accumulate_result = occlusion_counter_accumulate_result,
103 };
104 
105 static const struct fd_hw_sample_provider occlusion_predicate = {
106    .query_type = PIPE_QUERY_OCCLUSION_PREDICATE,
107    .get_sample = occlusion_get_sample,
108    .accumulate_result = occlusion_predicate_accumulate_result,
109 };
110 
111 static const struct fd_hw_sample_provider occlusion_predicate_conservative = {
112    .query_type = PIPE_QUERY_OCCLUSION_PREDICATE_CONSERVATIVE,
113    .get_sample = occlusion_get_sample,
114    .accumulate_result = occlusion_predicate_accumulate_result,
115 };
116 
117 void
fd3_query_context_init(struct pipe_context * pctx)118 fd3_query_context_init(struct pipe_context *pctx) disable_thread_safety_analysis
119 {
120    struct fd_context *ctx = fd_context(pctx);
121 
122    ctx->create_query = fd_hw_create_query;
123    ctx->query_prepare = fd_hw_query_prepare;
124    ctx->query_prepare_tile = fd_hw_query_prepare_tile;
125    ctx->query_update_batch = fd_hw_query_update_batch;
126 
127    fd_hw_query_register_provider(pctx, &occlusion_counter);
128    fd_hw_query_register_provider(pctx, &occlusion_predicate);
129    fd_hw_query_register_provider(pctx, &occlusion_predicate_conservative);
130 }
131