Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mesa
Path: blob/21.2-virgl/src/gallium/drivers/freedreno/freedreno_util.h
4570 views
1
/*
2
* Copyright (C) 2012 Rob Clark <[email protected]>
3
*
4
* Permission is hereby granted, free of charge, to any person obtaining a
5
* copy of this software and associated documentation files (the "Software"),
6
* to deal in the Software without restriction, including without limitation
7
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
8
* and/or sell copies of the Software, and to permit persons to whom the
9
* Software is furnished to do so, subject to the following conditions:
10
*
11
* The above copyright notice and this permission notice (including the next
12
* paragraph) shall be included in all copies or substantial portions of the
13
* Software.
14
*
15
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
* SOFTWARE.
22
*
23
* Authors:
24
* Rob Clark <[email protected]>
25
*/
26
27
#ifndef FREEDRENO_UTIL_H_
28
#define FREEDRENO_UTIL_H_
29
30
#include "drm/freedreno_drmif.h"
31
#include "drm/freedreno_ringbuffer.h"
32
33
#include "pipe/p_format.h"
34
#include "pipe/p_state.h"
35
#include "util/compiler.h"
36
#include "util/half_float.h"
37
#include "util/log.h"
38
#include "util/u_debug.h"
39
#include "util/u_dynarray.h"
40
#include "util/u_math.h"
41
#include "util/u_pack_color.h"
42
43
#include "adreno_common.xml.h"
44
#include "adreno_pm4.xml.h"
45
#include "disasm.h"
46
47
#ifdef __cplusplus
48
extern "C" {
49
#endif
50
51
enum adreno_rb_depth_format fd_pipe2depth(enum pipe_format format);
52
enum pc_di_index_size fd_pipe2index(enum pipe_format format);
53
enum pipe_format fd_gmem_restore_format(enum pipe_format format);
54
enum adreno_rb_blend_factor fd_blend_factor(unsigned factor);
55
enum adreno_pa_su_sc_draw fd_polygon_mode(unsigned mode);
56
enum adreno_stencil_op fd_stencil_op(unsigned op);
57
58
#define A3XX_MAX_MIP_LEVELS 14
59
60
#define A2XX_MAX_RENDER_TARGETS 1
61
#define A3XX_MAX_RENDER_TARGETS 4
62
#define A4XX_MAX_RENDER_TARGETS 8
63
#define A5XX_MAX_RENDER_TARGETS 8
64
#define A6XX_MAX_RENDER_TARGETS 8
65
66
#define MAX_RENDER_TARGETS A6XX_MAX_RENDER_TARGETS
67
68
/* clang-format off */
69
enum fd_debug_flag {
70
FD_DBG_MSGS = BITFIELD_BIT(0),
71
FD_DBG_DISASM = BITFIELD_BIT(1),
72
FD_DBG_DCLEAR = BITFIELD_BIT(2),
73
FD_DBG_DDRAW = BITFIELD_BIT(3),
74
FD_DBG_NOSCIS = BITFIELD_BIT(4),
75
FD_DBG_DIRECT = BITFIELD_BIT(5),
76
FD_DBG_NOBYPASS = BITFIELD_BIT(6),
77
FD_DBG_PERF = BITFIELD_BIT(7),
78
FD_DBG_NOBIN = BITFIELD_BIT(8),
79
FD_DBG_NOGMEM = BITFIELD_BIT(9),
80
FD_DBG_SERIALC = BITFIELD_BIT(10),
81
FD_DBG_SHADERDB = BITFIELD_BIT(11),
82
FD_DBG_FLUSH = BITFIELD_BIT(12),
83
FD_DBG_DEQP = BITFIELD_BIT(13),
84
FD_DBG_INORDER = BITFIELD_BIT(14),
85
FD_DBG_BSTAT = BITFIELD_BIT(15),
86
FD_DBG_NOGROW = BITFIELD_BIT(16),
87
FD_DBG_LRZ = BITFIELD_BIT(17),
88
FD_DBG_NOINDR = BITFIELD_BIT(18),
89
FD_DBG_NOBLIT = BITFIELD_BIT(19),
90
FD_DBG_HIPRIO = BITFIELD_BIT(20),
91
FD_DBG_TTILE = BITFIELD_BIT(21),
92
FD_DBG_PERFC = BITFIELD_BIT(22),
93
FD_DBG_NOUBWC = BITFIELD_BIT(23),
94
FD_DBG_NOLRZ = BITFIELD_BIT(24),
95
FD_DBG_NOTILE = BITFIELD_BIT(25),
96
FD_DBG_LAYOUT = BITFIELD_BIT(26),
97
FD_DBG_NOFP16 = BITFIELD_BIT(27),
98
FD_DBG_NOHW = BITFIELD_BIT(28),
99
};
100
/* clang-format on */
101
102
extern int fd_mesa_debug;
103
extern bool fd_binning_enabled;
104
105
#define FD_DBG(category) unlikely(fd_mesa_debug &FD_DBG_##category)
106
107
#include <unistd.h>
108
#include <sys/types.h>
109
110
#define DBG(fmt, ...) \
111
do { \
112
if (FD_DBG(MSGS)) \
113
mesa_logi("%5d: %s:%d: " fmt, gettid(), __FUNCTION__, __LINE__, \
114
##__VA_ARGS__); \
115
} while (0)
116
117
#define perf_debug_ctx(ctx, ...) \
118
do { \
119
if (FD_DBG(PERF)) \
120
mesa_logw(__VA_ARGS__); \
121
struct fd_context *__c = (ctx); \
122
if (__c) \
123
pipe_debug_message(&__c->debug, PERF_INFO, __VA_ARGS__); \
124
} while (0)
125
126
#define perf_debug(...) perf_debug_ctx(NULL, __VA_ARGS__)
127
128
#define perf_time_ctx(ctx, limit_ns, fmt, ...) \
129
for (struct __perf_time_state __s = \
130
{ \
131
.t = -__perf_get_time(ctx), \
132
}; \
133
!__s.done; ({ \
134
__s.t += __perf_get_time(ctx); \
135
__s.done = true; \
136
if (__s.t > (limit_ns)) { \
137
perf_debug_ctx(ctx, fmt " (%.03f ms)", ##__VA_ARGS__, \
138
(double)__s.t / 1000000.0); \
139
} \
140
}))
141
142
#define perf_time(limit_ns, fmt, ...) \
143
perf_time_ctx(NULL, limit_ns, fmt, ##__VA_ARGS__)
144
145
struct __perf_time_state {
146
int64_t t;
147
bool done;
148
};
149
150
/* static inline would be nice here, except 'struct fd_context' is not
151
* defined yet:
152
*/
153
#define __perf_get_time(ctx) \
154
((FD_DBG(PERF) || ({ \
155
struct fd_context *__c = (ctx); \
156
unlikely(__c && __c->debug.debug_message); \
157
})) \
158
? os_time_get_nano() \
159
: 0)
160
161
struct fd_context;
162
163
/**
164
* A psuedo-variable for defining where various parts of the fd_context
165
* can be safely accessed.
166
*
167
* With threaded_context, certain pctx funcs are called from gallium
168
* front-end/state-tracker (eg. CSO creation), while others are called
169
* from the driver thread. Things called from driver thread can safely
170
* access anything in the ctx, while things called from the fe/st thread
171
* must limit themselves to "safe" things (ie. ctx->screen is safe as it
172
* is immutable, but the blitter_context is not).
173
*/
174
extern lock_cap_t fd_context_access_cap;
175
176
/**
177
* Make the annotation a bit less verbose.. mark fields which should only
178
* be accessed by driver-thread with 'dt'
179
*/
180
#define dt guarded_by(fd_context_access_cap)
181
182
/**
183
* Annotation for entry-point functions only called in driver thread.
184
*
185
* For static functions, apply the annotation to the function declaration.
186
* Otherwise apply to the function prototype.
187
*/
188
#define in_dt assert_cap(fd_context_access_cap)
189
190
/**
191
* Annotation for internal functions which are only called from entry-
192
* point functions (with 'in_dt' annotation) or other internal functions
193
* with the 'assert_dt' annotation.
194
*
195
* For static functions, apply the annotation to the function declaration.
196
* Otherwise apply to the function prototype.
197
*/
198
#define assert_dt requires_cap(fd_context_access_cap)
199
200
/**
201
* Special helpers for context access outside of driver thread. For ex,
202
* pctx->get_query_result() is not called on driver thread, but the
203
* query is guaranteed to be flushed, or the driver thread queue is
204
* guaranteed to be flushed.
205
*
206
* Use with caution!
207
*/
208
static inline void
209
fd_context_access_begin(struct fd_context *ctx)
210
acquire_cap(fd_context_access_cap)
211
{
212
}
213
214
static inline void
215
fd_context_access_end(struct fd_context *ctx) release_cap(fd_context_access_cap)
216
{
217
}
218
219
/* for conditionally setting boolean flag(s): */
220
#define COND(bool, val) ((bool) ? (val) : 0)
221
222
#define CP_REG(reg) ((0x4 << 16) | ((unsigned int)((reg) - (0x2000))))
223
224
static inline uint32_t
225
DRAW(enum pc_di_primtype prim_type, enum pc_di_src_sel source_select,
226
enum pc_di_index_size index_size, enum pc_di_vis_cull_mode vis_cull_mode,
227
uint8_t instances)
228
{
229
return (prim_type << 0) | (source_select << 6) | ((index_size & 1) << 11) |
230
((index_size >> 1) << 13) | (vis_cull_mode << 9) | (1 << 14) |
231
(instances << 24);
232
}
233
234
static inline uint32_t
235
DRAW_A20X(enum pc_di_primtype prim_type,
236
enum pc_di_face_cull_sel faceness_cull_select,
237
enum pc_di_src_sel source_select, enum pc_di_index_size index_size,
238
bool pre_fetch_cull_enable, bool grp_cull_enable, uint16_t count)
239
{
240
return (prim_type << 0) | (source_select << 6) |
241
(faceness_cull_select << 8) | ((index_size & 1) << 11) |
242
((index_size >> 1) << 13) | (pre_fetch_cull_enable << 14) |
243
(grp_cull_enable << 15) | (count << 16);
244
}
245
246
/* for tracking cmdstream positions that need to be patched: */
247
struct fd_cs_patch {
248
uint32_t *cs;
249
uint32_t val;
250
};
251
#define fd_patch_num_elements(buf) ((buf)->size / sizeof(struct fd_cs_patch))
252
#define fd_patch_element(buf, i) \
253
util_dynarray_element(buf, struct fd_cs_patch, i)
254
255
static inline enum pipe_format
256
pipe_surface_format(struct pipe_surface *psurf)
257
{
258
if (!psurf)
259
return PIPE_FORMAT_NONE;
260
return psurf->format;
261
}
262
263
static inline bool
264
fd_surface_half_precision(const struct pipe_surface *psurf)
265
{
266
enum pipe_format format;
267
268
if (!psurf)
269
return true;
270
271
format = psurf->format;
272
273
/* colors are provided in consts, which go through cov.f32f16, which will
274
* break these values
275
*/
276
if (util_format_is_pure_integer(format))
277
return false;
278
279
/* avoid losing precision on 32-bit float formats */
280
if (util_format_is_float(format) &&
281
util_format_get_component_bits(format, UTIL_FORMAT_COLORSPACE_RGB, 0) ==
282
32)
283
return false;
284
285
return true;
286
}
287
288
static inline unsigned
289
fd_sampler_first_level(const struct pipe_sampler_view *view)
290
{
291
if (view->target == PIPE_BUFFER)
292
return 0;
293
return view->u.tex.first_level;
294
}
295
296
static inline unsigned
297
fd_sampler_last_level(const struct pipe_sampler_view *view)
298
{
299
if (view->target == PIPE_BUFFER)
300
return 0;
301
return view->u.tex.last_level;
302
}
303
304
static inline bool
305
fd_half_precision(struct pipe_framebuffer_state *pfb)
306
{
307
unsigned i;
308
309
for (i = 0; i < pfb->nr_cbufs; i++)
310
if (!fd_surface_half_precision(pfb->cbufs[i]))
311
return false;
312
313
return true;
314
}
315
316
static inline void emit_marker(struct fd_ringbuffer *ring, int scratch_idx);
317
318
/* like OUT_RING() but appends a cmdstream patch point to 'buf' */
319
static inline void
320
OUT_RINGP(struct fd_ringbuffer *ring, uint32_t data, struct util_dynarray *buf)
321
{
322
if (LOG_DWORDS) {
323
DBG("ring[%p]: OUT_RINGP %04x: %08x", ring,
324
(uint32_t)(ring->cur - ring->start), data);
325
}
326
util_dynarray_append(buf, struct fd_cs_patch,
327
((struct fd_cs_patch){
328
.cs = ring->cur++,
329
.val = data,
330
}));
331
}
332
333
static inline void
334
__OUT_IB(struct fd_ringbuffer *ring, bool prefetch,
335
struct fd_ringbuffer *target)
336
{
337
if (target->cur == target->start)
338
return;
339
340
unsigned count = fd_ringbuffer_cmd_count(target);
341
342
/* for debug after a lock up, write a unique counter value
343
* to scratch6 for each IB, to make it easier to match up
344
* register dumps to cmdstream. The combination of IB and
345
* DRAW (scratch7) is enough to "triangulate" the particular
346
* draw that caused lockup.
347
*/
348
emit_marker(ring, 6);
349
350
for (unsigned i = 0; i < count; i++) {
351
uint32_t dwords;
352
OUT_PKT3(ring, prefetch ? CP_INDIRECT_BUFFER_PFE : CP_INDIRECT_BUFFER_PFD,
353
2);
354
dwords = fd_ringbuffer_emit_reloc_ring_full(ring, target, i) / 4;
355
assert(dwords > 0);
356
OUT_RING(ring, dwords);
357
OUT_PKT2(ring);
358
}
359
360
emit_marker(ring, 6);
361
}
362
363
static inline void
364
__OUT_IB5(struct fd_ringbuffer *ring, struct fd_ringbuffer *target)
365
{
366
if (target->cur == target->start)
367
return;
368
369
unsigned count = fd_ringbuffer_cmd_count(target);
370
371
for (unsigned i = 0; i < count; i++) {
372
uint32_t dwords;
373
OUT_PKT7(ring, CP_INDIRECT_BUFFER, 3);
374
dwords = fd_ringbuffer_emit_reloc_ring_full(ring, target, i) / 4;
375
assert(dwords > 0);
376
OUT_RING(ring, dwords);
377
}
378
}
379
380
/* CP_SCRATCH_REG4 is used to hold base address for query results: */
381
// XXX annoyingly scratch regs move on a5xx.. and additionally different
382
// packet types.. so freedreno_query_hw is going to need a bit of
383
// rework..
384
#define HW_QUERY_BASE_REG REG_AXXX_CP_SCRATCH_REG4
385
386
#ifdef DEBUG
387
#define __EMIT_MARKER 1
388
#else
389
#define __EMIT_MARKER 0
390
#endif
391
392
static inline void
393
emit_marker(struct fd_ringbuffer *ring, int scratch_idx)
394
{
395
extern int32_t marker_cnt;
396
unsigned reg = REG_AXXX_CP_SCRATCH_REG0 + scratch_idx;
397
assert(reg != HW_QUERY_BASE_REG);
398
if (reg == HW_QUERY_BASE_REG)
399
return;
400
if (__EMIT_MARKER) {
401
OUT_WFI5(ring);
402
OUT_PKT0(ring, reg, 1);
403
OUT_RING(ring, p_atomic_inc_return(&marker_cnt));
404
}
405
}
406
407
static inline uint32_t
408
pack_rgba(enum pipe_format format, const float *rgba)
409
{
410
union util_color uc;
411
util_pack_color(rgba, format, &uc);
412
return uc.ui[0];
413
}
414
415
/*
416
* swap - swap value of @a and @b
417
*/
418
#define swap(a, b) \
419
do { \
420
__typeof(a) __tmp = (a); \
421
(a) = (b); \
422
(b) = __tmp; \
423
} while (0)
424
425
#define BIT(bit) (1u << bit)
426
427
/*
428
* a3xx+ helpers:
429
*/
430
431
static inline enum a3xx_msaa_samples
432
fd_msaa_samples(unsigned samples)
433
{
434
switch (samples) {
435
default:
436
debug_assert(0);
437
#if defined(NDEBUG) || defined(DEBUG)
438
FALLTHROUGH;
439
#endif
440
case 0:
441
case 1:
442
return MSAA_ONE;
443
case 2:
444
return MSAA_TWO;
445
case 4:
446
return MSAA_FOUR;
447
case 8:
448
return MSAA_EIGHT;
449
}
450
}
451
452
/*
453
* a4xx+ helpers:
454
*/
455
456
static inline enum a4xx_state_block
457
fd4_stage2shadersb(gl_shader_stage type)
458
{
459
switch (type) {
460
case MESA_SHADER_VERTEX:
461
return SB4_VS_SHADER;
462
case MESA_SHADER_FRAGMENT:
463
return SB4_FS_SHADER;
464
case MESA_SHADER_COMPUTE:
465
case MESA_SHADER_KERNEL:
466
return SB4_CS_SHADER;
467
default:
468
unreachable("bad shader type");
469
return (enum a4xx_state_block) ~0;
470
}
471
}
472
473
static inline enum a4xx_index_size
474
fd4_size2indextype(unsigned index_size)
475
{
476
switch (index_size) {
477
case 1:
478
return INDEX4_SIZE_8_BIT;
479
case 2:
480
return INDEX4_SIZE_16_BIT;
481
case 4:
482
return INDEX4_SIZE_32_BIT;
483
}
484
DBG("unsupported index size: %d", index_size);
485
assert(0);
486
return INDEX4_SIZE_32_BIT;
487
}
488
489
#ifdef __cplusplus
490
}
491
#endif
492
493
#endif /* FREEDRENO_UTIL_H_ */
494
495