1bf215546Sopenharmony_ci/*
2bf215546Sopenharmony_ci * Copyright (C) 2014 Rob Clark <robclark@freedesktop.org>
3bf215546Sopenharmony_ci *
4bf215546Sopenharmony_ci * Permission is hereby granted, free of charge, to any person obtaining a
5bf215546Sopenharmony_ci * copy of this software and associated documentation files (the "Software"),
6bf215546Sopenharmony_ci * to deal in the Software without restriction, including without limitation
7bf215546Sopenharmony_ci * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8bf215546Sopenharmony_ci * and/or sell copies of the Software, and to permit persons to whom the
9bf215546Sopenharmony_ci * Software is furnished to do so, subject to the following conditions:
10bf215546Sopenharmony_ci *
11bf215546Sopenharmony_ci * The above copyright notice and this permission notice (including the next
12bf215546Sopenharmony_ci * paragraph) shall be included in all copies or substantial portions of the
13bf215546Sopenharmony_ci * Software.
14bf215546Sopenharmony_ci *
15bf215546Sopenharmony_ci * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16bf215546Sopenharmony_ci * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17bf215546Sopenharmony_ci * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
18bf215546Sopenharmony_ci * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19bf215546Sopenharmony_ci * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20bf215546Sopenharmony_ci * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21bf215546Sopenharmony_ci * SOFTWARE.
22bf215546Sopenharmony_ci *
23bf215546Sopenharmony_ci * Authors:
24bf215546Sopenharmony_ci *    Rob Clark <robclark@freedesktop.org>
25bf215546Sopenharmony_ci */
26bf215546Sopenharmony_ci
27bf215546Sopenharmony_ci#include "freedreno_batch.h"
28bf215546Sopenharmony_ci#include "freedreno_context.h"
29bf215546Sopenharmony_ci#include "freedreno_query_hw.h"
30bf215546Sopenharmony_ci#include "freedreno_util.h"
31bf215546Sopenharmony_ci
32bf215546Sopenharmony_ci#include "fd3_format.h"
33bf215546Sopenharmony_ci#include "fd3_query.h"
34bf215546Sopenharmony_ci
35bf215546Sopenharmony_cistruct fd_rb_samp_ctrs {
36bf215546Sopenharmony_ci   uint64_t ctr[16];
37bf215546Sopenharmony_ci};
38bf215546Sopenharmony_ci
39bf215546Sopenharmony_ci/*
40bf215546Sopenharmony_ci * Occlusion Query:
41bf215546Sopenharmony_ci *
42bf215546Sopenharmony_ci * OCCLUSION_COUNTER and OCCLUSION_PREDICATE differ only in how they
43bf215546Sopenharmony_ci * interpret results
44bf215546Sopenharmony_ci */
45bf215546Sopenharmony_ci
46bf215546Sopenharmony_cistatic struct fd_hw_sample *
47bf215546Sopenharmony_ciocclusion_get_sample(struct fd_batch *batch, struct fd_ringbuffer *ring)
48bf215546Sopenharmony_ci{
49bf215546Sopenharmony_ci   struct fd_hw_sample *samp =
50bf215546Sopenharmony_ci      fd_hw_sample_init(batch, sizeof(struct fd_rb_samp_ctrs));
51bf215546Sopenharmony_ci
52bf215546Sopenharmony_ci   /* Set RB_SAMPLE_COUNT_ADDR to samp->offset plus value of
53bf215546Sopenharmony_ci    * HW_QUERY_BASE_REG register:
54bf215546Sopenharmony_ci    */
55bf215546Sopenharmony_ci   OUT_PKT3(ring, CP_SET_CONSTANT, 3);
56bf215546Sopenharmony_ci   OUT_RING(ring, CP_REG(REG_A3XX_RB_SAMPLE_COUNT_ADDR) | 0x80000000);
57bf215546Sopenharmony_ci   OUT_RING(ring, HW_QUERY_BASE_REG);
58bf215546Sopenharmony_ci   OUT_RING(ring, samp->offset);
59bf215546Sopenharmony_ci
60bf215546Sopenharmony_ci   OUT_PKT0(ring, REG_A3XX_RB_SAMPLE_COUNT_CONTROL, 1);
61bf215546Sopenharmony_ci   OUT_RING(ring, A3XX_RB_SAMPLE_COUNT_CONTROL_COPY);
62bf215546Sopenharmony_ci
63bf215546Sopenharmony_ci   OUT_PKT3(ring, CP_DRAW_INDX, 3);
64bf215546Sopenharmony_ci   OUT_RING(ring, 0x00000000);
65bf215546Sopenharmony_ci   OUT_RING(ring, DRAW(DI_PT_POINTLIST_PSIZE, DI_SRC_SEL_AUTO_INDEX,
66bf215546Sopenharmony_ci                       INDEX_SIZE_IGN, USE_VISIBILITY, 0));
67bf215546Sopenharmony_ci   OUT_RING(ring, 0); /* NumIndices */
68bf215546Sopenharmony_ci
69bf215546Sopenharmony_ci   fd_event_write(batch, ring, ZPASS_DONE);
70bf215546Sopenharmony_ci
71bf215546Sopenharmony_ci   OUT_PKT0(ring, REG_A3XX_RBBM_PERFCTR_CTL, 1);
72bf215546Sopenharmony_ci   OUT_RING(ring, A3XX_RBBM_PERFCTR_CTL_ENABLE);
73bf215546Sopenharmony_ci
74bf215546Sopenharmony_ci   OUT_PKT0(ring, REG_A3XX_VBIF_PERF_CNT_EN, 1);
75bf215546Sopenharmony_ci   OUT_RING(ring, A3XX_VBIF_PERF_CNT_EN_CNT0 | A3XX_VBIF_PERF_CNT_EN_CNT1 |
76bf215546Sopenharmony_ci                     A3XX_VBIF_PERF_CNT_EN_PWRCNT0 |
77bf215546Sopenharmony_ci                     A3XX_VBIF_PERF_CNT_EN_PWRCNT1 |
78bf215546Sopenharmony_ci                     A3XX_VBIF_PERF_CNT_EN_PWRCNT2);
79bf215546Sopenharmony_ci
80bf215546Sopenharmony_ci   return samp;
81bf215546Sopenharmony_ci}
82bf215546Sopenharmony_ci
83bf215546Sopenharmony_cistatic uint64_t
84bf215546Sopenharmony_cicount_samples(const struct fd_rb_samp_ctrs *start,
85bf215546Sopenharmony_ci              const struct fd_rb_samp_ctrs *end)
86bf215546Sopenharmony_ci{
87bf215546Sopenharmony_ci   uint64_t n = 0;
88bf215546Sopenharmony_ci   unsigned i;
89bf215546Sopenharmony_ci
90bf215546Sopenharmony_ci   /* not quite sure what all of these are, possibly different
91bf215546Sopenharmony_ci    * counters for each MRT render target:
92bf215546Sopenharmony_ci    */
93bf215546Sopenharmony_ci   for (i = 0; i < 16; i += 4)
94bf215546Sopenharmony_ci      n += end->ctr[i] - start->ctr[i];
95bf215546Sopenharmony_ci
96bf215546Sopenharmony_ci   return n;
97bf215546Sopenharmony_ci}
98bf215546Sopenharmony_ci
99bf215546Sopenharmony_cistatic void
100bf215546Sopenharmony_ciocclusion_counter_accumulate_result(struct fd_context *ctx, const void *start,
101bf215546Sopenharmony_ci                                    const void *end,
102bf215546Sopenharmony_ci                                    union pipe_query_result *result)
103bf215546Sopenharmony_ci{
104bf215546Sopenharmony_ci   uint64_t n = count_samples(start, end);
105bf215546Sopenharmony_ci   result->u64 += n;
106bf215546Sopenharmony_ci}
107bf215546Sopenharmony_ci
108bf215546Sopenharmony_cistatic void
109bf215546Sopenharmony_ciocclusion_predicate_accumulate_result(struct fd_context *ctx, const void *start,
110bf215546Sopenharmony_ci                                      const void *end,
111bf215546Sopenharmony_ci                                      union pipe_query_result *result)
112bf215546Sopenharmony_ci{
113bf215546Sopenharmony_ci   uint64_t n = count_samples(start, end);
114bf215546Sopenharmony_ci   result->b |= (n > 0);
115bf215546Sopenharmony_ci}
116bf215546Sopenharmony_ci
117bf215546Sopenharmony_cistatic const struct fd_hw_sample_provider occlusion_counter = {
118bf215546Sopenharmony_ci   .query_type = PIPE_QUERY_OCCLUSION_COUNTER,
119bf215546Sopenharmony_ci   .get_sample = occlusion_get_sample,
120bf215546Sopenharmony_ci   .accumulate_result = occlusion_counter_accumulate_result,
121bf215546Sopenharmony_ci};
122bf215546Sopenharmony_ci
123bf215546Sopenharmony_cistatic const struct fd_hw_sample_provider occlusion_predicate = {
124bf215546Sopenharmony_ci   .query_type = PIPE_QUERY_OCCLUSION_PREDICATE,
125bf215546Sopenharmony_ci   .get_sample = occlusion_get_sample,
126bf215546Sopenharmony_ci   .accumulate_result = occlusion_predicate_accumulate_result,
127bf215546Sopenharmony_ci};
128bf215546Sopenharmony_ci
129bf215546Sopenharmony_cistatic const struct fd_hw_sample_provider occlusion_predicate_conservative = {
130bf215546Sopenharmony_ci   .query_type = PIPE_QUERY_OCCLUSION_PREDICATE_CONSERVATIVE,
131bf215546Sopenharmony_ci   .get_sample = occlusion_get_sample,
132bf215546Sopenharmony_ci   .accumulate_result = occlusion_predicate_accumulate_result,
133bf215546Sopenharmony_ci};
134bf215546Sopenharmony_ci
135bf215546Sopenharmony_civoid
136bf215546Sopenharmony_cifd3_query_context_init(struct pipe_context *pctx) disable_thread_safety_analysis
137bf215546Sopenharmony_ci{
138bf215546Sopenharmony_ci   struct fd_context *ctx = fd_context(pctx);
139bf215546Sopenharmony_ci
140bf215546Sopenharmony_ci   ctx->create_query = fd_hw_create_query;
141bf215546Sopenharmony_ci   ctx->query_prepare = fd_hw_query_prepare;
142bf215546Sopenharmony_ci   ctx->query_prepare_tile = fd_hw_query_prepare_tile;
143bf215546Sopenharmony_ci   ctx->query_update_batch = fd_hw_query_update_batch;
144bf215546Sopenharmony_ci
145bf215546Sopenharmony_ci   fd_hw_query_register_provider(pctx, &occlusion_counter);
146bf215546Sopenharmony_ci   fd_hw_query_register_provider(pctx, &occlusion_predicate);
147bf215546Sopenharmony_ci   fd_hw_query_register_provider(pctx, &occlusion_predicate_conservative);
148bf215546Sopenharmony_ci}
149