13ab0e8466eb3eeca456ab3a0fac23e8c0f5a27f
[mesa.git] / src / gallium / drivers / freedreno / freedreno_query_sw.c
1 /* -*- mode: C; c-file-style: "k&r"; tab-width 4; indent-tabs-mode: t; -*- */
2
3 /*
4 * Copyright (C) 2014 Rob Clark <robclark@freedesktop.org>
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the "Software"),
8 * to deal in the Software without restriction, including without limitation
9 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10 * and/or sell copies of the Software, and to permit persons to whom the
11 * Software is furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice (including the next
14 * paragraph) shall be included in all copies or substantial portions of the
15 * Software.
16 *
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
20 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
23 * SOFTWARE.
24 *
25 * Authors:
26 * Rob Clark <robclark@freedesktop.org>
27 */
28
29 #include "pipe/p_state.h"
30 #include "util/u_string.h"
31 #include "util/u_memory.h"
32 #include "util/u_inlines.h"
33 #include "util/os_time.h"
34
35 #include "freedreno_query_sw.h"
36 #include "freedreno_context.h"
37 #include "freedreno_util.h"
38
39 /*
40 * SW Queries:
41 *
42 * In the core, we have some support for basic sw counters
43 */
44
45 static void
46 fd_sw_destroy_query(struct fd_context *ctx, struct fd_query *q)
47 {
48 struct fd_sw_query *sq = fd_sw_query(q);
49 free(sq);
50 }
51
52 static uint64_t
53 read_counter(struct fd_context *ctx, int type)
54 {
55 switch (type) {
56 case PIPE_QUERY_PRIMITIVES_GENERATED:
57 return ctx->stats.prims_generated;
58 case PIPE_QUERY_PRIMITIVES_EMITTED:
59 return ctx->stats.prims_emitted;
60 case FD_QUERY_DRAW_CALLS:
61 return ctx->stats.draw_calls;
62 case FD_QUERY_BATCH_TOTAL:
63 return ctx->stats.batch_total;
64 case FD_QUERY_BATCH_SYSMEM:
65 return ctx->stats.batch_sysmem;
66 case FD_QUERY_BATCH_GMEM:
67 return ctx->stats.batch_gmem;
68 case FD_QUERY_BATCH_NONDRAW:
69 return ctx->stats.batch_nondraw;
70 case FD_QUERY_BATCH_RESTORE:
71 return ctx->stats.batch_restore;
72 case FD_QUERY_STAGING_UPLOADS:
73 return ctx->stats.staging_uploads;
74 case FD_QUERY_SHADOW_UPLOADS:
75 return ctx->stats.shadow_uploads;
76 case FD_QUERY_VS_REGS:
77 return ctx->stats.vs_regs;
78 case FD_QUERY_FS_REGS:
79 return ctx->stats.fs_regs;
80 }
81 return 0;
82 }
83
84 static bool
85 is_time_rate_query(struct fd_query *q)
86 {
87 switch (q->type) {
88 case FD_QUERY_BATCH_TOTAL:
89 case FD_QUERY_BATCH_SYSMEM:
90 case FD_QUERY_BATCH_GMEM:
91 case FD_QUERY_BATCH_NONDRAW:
92 case FD_QUERY_BATCH_RESTORE:
93 case FD_QUERY_STAGING_UPLOADS:
94 case FD_QUERY_SHADOW_UPLOADS:
95 return true;
96 default:
97 return false;
98 }
99 }
100
101 static bool
102 is_draw_rate_query(struct fd_query *q)
103 {
104 switch (q->type) {
105 case FD_QUERY_VS_REGS:
106 case FD_QUERY_FS_REGS:
107 return true;
108 default:
109 return false;
110 }
111 }
112
113 static boolean
114 fd_sw_begin_query(struct fd_context *ctx, struct fd_query *q)
115 {
116 struct fd_sw_query *sq = fd_sw_query(q);
117 sq->begin_value = read_counter(ctx, q->type);
118 if (is_time_rate_query(q)) {
119 sq->begin_time = os_time_get();
120 } else if (is_draw_rate_query(q)) {
121 sq->begin_time = ctx->stats.draw_calls;
122 }
123 return true;
124 }
125
126 static void
127 fd_sw_end_query(struct fd_context *ctx, struct fd_query *q)
128 {
129 struct fd_sw_query *sq = fd_sw_query(q);
130 sq->end_value = read_counter(ctx, q->type);
131 if (is_time_rate_query(q)) {
132 sq->end_time = os_time_get();
133 } else if (is_draw_rate_query(q)) {
134 sq->end_time = ctx->stats.draw_calls;
135 }
136 }
137
138 static boolean
139 fd_sw_get_query_result(struct fd_context *ctx, struct fd_query *q,
140 boolean wait, union pipe_query_result *result)
141 {
142 struct fd_sw_query *sq = fd_sw_query(q);
143
144 result->u64 = sq->end_value - sq->begin_value;
145
146 if (is_time_rate_query(q)) {
147 double fps = (result->u64 * 1000000) /
148 (double)(sq->end_time - sq->begin_time);
149 result->u64 = (uint64_t)fps;
150 } else if (is_draw_rate_query(q)) {
151 double avg = ((double)result->u64) /
152 (double)(sq->end_time - sq->begin_time);
153 result->f = avg;
154 }
155
156 return true;
157 }
158
159 static const struct fd_query_funcs sw_query_funcs = {
160 .destroy_query = fd_sw_destroy_query,
161 .begin_query = fd_sw_begin_query,
162 .end_query = fd_sw_end_query,
163 .get_query_result = fd_sw_get_query_result,
164 };
165
166 struct fd_query *
167 fd_sw_create_query(struct fd_context *ctx, unsigned query_type)
168 {
169 struct fd_sw_query *sq;
170 struct fd_query *q;
171
172 switch (query_type) {
173 case PIPE_QUERY_PRIMITIVES_GENERATED:
174 case PIPE_QUERY_PRIMITIVES_EMITTED:
175 case FD_QUERY_DRAW_CALLS:
176 case FD_QUERY_BATCH_TOTAL:
177 case FD_QUERY_BATCH_SYSMEM:
178 case FD_QUERY_BATCH_GMEM:
179 case FD_QUERY_BATCH_NONDRAW:
180 case FD_QUERY_BATCH_RESTORE:
181 case FD_QUERY_STAGING_UPLOADS:
182 case FD_QUERY_SHADOW_UPLOADS:
183 case FD_QUERY_VS_REGS:
184 case FD_QUERY_FS_REGS:
185 break;
186 default:
187 return NULL;
188 }
189
190 sq = CALLOC_STRUCT(fd_sw_query);
191 if (!sq)
192 return NULL;
193
194 q = &sq->base;
195 q->funcs = &sw_query_funcs;
196 q->type = query_type;
197
198 return q;
199 }