freedreno/a6xx: fix occlusion query with more than one tile
[mesa.git] / src / gallium / drivers / freedreno / a6xx / fd6_gmem.c
1 /*
2 * Copyright (C) 2016 Rob Clark <robclark@freedesktop.org>
3 * Copyright © 2018 Google, Inc.
4 *
5 * Permission is hereby granted, free of charge, to any person obtaining a
6 * copy of this software and associated documentation files (the "Software"),
7 * to deal in the Software without restriction, including without limitation
8 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
9 * and/or sell copies of the Software, and to permit persons to whom the
10 * Software is furnished to do so, subject to the following conditions:
11 *
12 * The above copyright notice and this permission notice (including the next
13 * paragraph) shall be included in all copies or substantial portions of the
14 * Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22 * SOFTWARE.
23 *
24 * Authors:
25 * Rob Clark <robclark@freedesktop.org>
26 */
27
28 #include <stdio.h>
29
30 #include "pipe/p_state.h"
31 #include "util/u_string.h"
32 #include "util/u_memory.h"
33 #include "util/u_inlines.h"
34 #include "util/format/u_format.h"
35
36 #include "freedreno_draw.h"
37 #include "freedreno_log.h"
38 #include "freedreno_state.h"
39 #include "freedreno_resource.h"
40
41 #include "fd6_blitter.h"
42 #include "fd6_gmem.h"
43 #include "fd6_context.h"
44 #include "fd6_draw.h"
45 #include "fd6_emit.h"
46 #include "fd6_program.h"
47 #include "fd6_format.h"
48 #include "fd6_resource.h"
49 #include "fd6_zsa.h"
50 #include "fd6_pack.h"
51
52 /**
53 * Emits the flags registers, suitable for RB_MRT_FLAG_BUFFER,
54 * RB_DEPTH_FLAG_BUFFER, SP_PS_2D_SRC_FLAGS, and RB_BLIT_FLAG_DST.
55 */
56 void
57 fd6_emit_flag_reference(struct fd_ringbuffer *ring, struct fd_resource *rsc,
58 int level, int layer)
59 {
60 if (fd_resource_ubwc_enabled(rsc, level)) {
61 OUT_RELOC(ring, rsc->bo, fd_resource_ubwc_offset(rsc, level, layer), 0, 0);
62 OUT_RING(ring,
63 A6XX_RB_MRT_FLAG_BUFFER_PITCH_PITCH(fdl_ubwc_pitch(&rsc->layout, level)) |
64 A6XX_RB_MRT_FLAG_BUFFER_PITCH_ARRAY_PITCH(rsc->layout.ubwc_layer_size >> 2));
65 } else {
66 OUT_RING(ring, 0x00000000); /* RB_MRT_FLAG_BUFFER[i].ADDR_LO */
67 OUT_RING(ring, 0x00000000); /* RB_MRT_FLAG_BUFFER[i].ADDR_HI */
68 OUT_RING(ring, 0x00000000);
69 }
70 }
71
72 static void
73 emit_mrt(struct fd_ringbuffer *ring, struct pipe_framebuffer_state *pfb,
74 const struct fd_gmem_stateobj *gmem)
75 {
76 unsigned char mrt_comp[A6XX_MAX_RENDER_TARGETS] = {0};
77 unsigned srgb_cntl = 0;
78 unsigned i;
79
80 unsigned max_layer_index = 0;
81
82 for (i = 0; i < pfb->nr_cbufs; i++) {
83 enum a6xx_format format = 0;
84 enum a3xx_color_swap swap = WZYX;
85 bool sint = false, uint = false;
86 struct fd_resource *rsc = NULL;
87 struct fdl_slice *slice = NULL;
88 uint32_t stride = 0;
89 uint32_t offset;
90 uint32_t tile_mode;
91
92 if (!pfb->cbufs[i])
93 continue;
94
95 mrt_comp[i] = 0xf;
96
97 struct pipe_surface *psurf = pfb->cbufs[i];
98 enum pipe_format pformat = psurf->format;
99 rsc = fd_resource(psurf->texture);
100 if (!rsc->bo)
101 continue;
102
103 uint32_t base = gmem ? gmem->cbuf_base[i] : 0;
104 slice = fd_resource_slice(rsc, psurf->u.tex.level);
105 format = fd6_pipe2color(pformat);
106 sint = util_format_is_pure_sint(pformat);
107 uint = util_format_is_pure_uint(pformat);
108
109 if (util_format_is_srgb(pformat))
110 srgb_cntl |= (1 << i);
111
112 offset = fd_resource_offset(rsc, psurf->u.tex.level,
113 psurf->u.tex.first_layer);
114
115 stride = fd_resource_pitch(rsc, psurf->u.tex.level);
116 swap = fd6_resource_swap(rsc, pformat);
117
118 tile_mode = fd_resource_tile_mode(psurf->texture, psurf->u.tex.level);
119 max_layer_index = psurf->u.tex.last_layer - psurf->u.tex.first_layer;
120
121 debug_assert((offset + slice->size0) <= fd_bo_size(rsc->bo));
122
123 OUT_REG(ring,
124 A6XX_RB_MRT_BUF_INFO(i,
125 .color_format = format,
126 .color_tile_mode = tile_mode,
127 .color_swap = swap),
128 A6XX_RB_MRT_PITCH(i, .a6xx_rb_mrt_pitch = stride),
129 A6XX_RB_MRT_ARRAY_PITCH(i, .a6xx_rb_mrt_array_pitch = slice->size0),
130 A6XX_RB_MRT_BASE(i, .bo = rsc->bo, .bo_offset = offset),
131 A6XX_RB_MRT_BASE_GMEM(i, .unknown = base));
132
133 OUT_REG(ring,
134 A6XX_SP_FS_MRT_REG(i, .color_format = format,
135 .color_sint = sint, .color_uint = uint));
136
137 OUT_PKT4(ring, REG_A6XX_RB_MRT_FLAG_BUFFER(i), 3);
138 fd6_emit_flag_reference(ring, rsc,
139 psurf->u.tex.level, psurf->u.tex.first_layer);
140 }
141
142 OUT_REG(ring, A6XX_RB_SRGB_CNTL(.dword = srgb_cntl));
143 OUT_REG(ring, A6XX_SP_SRGB_CNTL(.dword = srgb_cntl));
144
145 OUT_REG(ring, A6XX_RB_RENDER_COMPONENTS(
146 .rt0 = mrt_comp[0],
147 .rt1 = mrt_comp[1],
148 .rt2 = mrt_comp[2],
149 .rt3 = mrt_comp[3],
150 .rt4 = mrt_comp[4],
151 .rt5 = mrt_comp[5],
152 .rt6 = mrt_comp[6],
153 .rt7 = mrt_comp[7]));
154
155 OUT_REG(ring, A6XX_SP_FS_RENDER_COMPONENTS(
156 .rt0 = mrt_comp[0],
157 .rt1 = mrt_comp[1],
158 .rt2 = mrt_comp[2],
159 .rt3 = mrt_comp[3],
160 .rt4 = mrt_comp[4],
161 .rt5 = mrt_comp[5],
162 .rt6 = mrt_comp[6],
163 .rt7 = mrt_comp[7]));
164
165 OUT_REG(ring, A6XX_GRAS_MAX_LAYER_INDEX(max_layer_index));
166 }
167
168 static void
169 emit_zs(struct fd_ringbuffer *ring, struct pipe_surface *zsbuf,
170 const struct fd_gmem_stateobj *gmem)
171 {
172 if (zsbuf) {
173 struct fd_resource *rsc = fd_resource(zsbuf->texture);
174 enum a6xx_depth_format fmt = fd6_pipe2depth(zsbuf->format);
175 uint32_t stride = fd_resource_pitch(rsc, 0);
176 uint32_t size = fd_resource_slice(rsc, 0)->size0;
177 uint32_t base = gmem ? gmem->zsbuf_base[0] : 0;
178 uint32_t offset = fd_resource_offset(rsc, zsbuf->u.tex.level,
179 zsbuf->u.tex.first_layer);
180
181 OUT_REG(ring,
182 A6XX_RB_DEPTH_BUFFER_INFO(.depth_format = fmt),
183 A6XX_RB_DEPTH_BUFFER_PITCH(.a6xx_rb_depth_buffer_pitch = stride),
184 A6XX_RB_DEPTH_BUFFER_ARRAY_PITCH(.a6xx_rb_depth_buffer_array_pitch = size),
185 A6XX_RB_DEPTH_BUFFER_BASE(.bo = rsc->bo, .bo_offset = offset),
186 A6XX_RB_DEPTH_BUFFER_BASE_GMEM(.dword = base));
187
188 OUT_REG(ring, A6XX_GRAS_SU_DEPTH_BUFFER_INFO(.depth_format = fmt));
189
190 OUT_PKT4(ring, REG_A6XX_RB_DEPTH_FLAG_BUFFER_BASE_LO, 3);
191 fd6_emit_flag_reference(ring, rsc,
192 zsbuf->u.tex.level, zsbuf->u.tex.first_layer);
193
194 if (rsc->lrz) {
195 OUT_REG(ring,
196 A6XX_GRAS_LRZ_BUFFER_BASE(.bo = rsc->lrz),
197 A6XX_GRAS_LRZ_BUFFER_PITCH(.pitch = rsc->lrz_pitch),
198 // XXX a6xx seems to use a different buffer here.. not sure what for..
199 A6XX_GRAS_LRZ_FAST_CLEAR_BUFFER_BASE_LO(0),
200 A6XX_GRAS_LRZ_FAST_CLEAR_BUFFER_BASE_HI(0));
201 } else {
202 OUT_PKT4(ring, REG_A6XX_GRAS_LRZ_BUFFER_BASE_LO, 5);
203 OUT_RING(ring, 0x00000000);
204 OUT_RING(ring, 0x00000000);
205 OUT_RING(ring, 0x00000000); /* GRAS_LRZ_BUFFER_PITCH */
206 OUT_RING(ring, 0x00000000); /* GRAS_LRZ_FAST_CLEAR_BUFFER_BASE_LO */
207 OUT_RING(ring, 0x00000000);
208 }
209
210 /* NOTE: blob emits GRAS_LRZ_CNTL plus GRAZ_LRZ_BUFFER_BASE
211 * plus this CP_EVENT_WRITE at the end in it's own IB..
212 */
213 OUT_PKT7(ring, CP_EVENT_WRITE, 1);
214 OUT_RING(ring, CP_EVENT_WRITE_0_EVENT(UNK_25));
215
216 if (rsc->stencil) {
217 stride = fd_resource_pitch(rsc->stencil, 0);
218 size = fd_resource_slice(rsc->stencil, 0)->size0;
219 uint32_t base = gmem ? gmem->zsbuf_base[1] : 0;
220
221 OUT_REG(ring,
222 A6XX_RB_STENCIL_INFO(.separate_stencil = true),
223 A6XX_RB_STENCIL_BUFFER_PITCH(.a6xx_rb_stencil_buffer_pitch = stride),
224 A6XX_RB_STENCIL_BUFFER_ARRAY_PITCH(.a6xx_rb_stencil_buffer_array_pitch = size),
225 A6XX_RB_STENCIL_BUFFER_BASE(.bo = rsc->stencil->bo),
226 A6XX_RB_STENCIL_BUFFER_BASE_GMEM(.dword = base));
227 } else {
228 OUT_REG(ring, A6XX_RB_STENCIL_INFO(0));
229 }
230 } else {
231 OUT_PKT4(ring, REG_A6XX_RB_DEPTH_BUFFER_INFO, 6);
232 OUT_RING(ring, A6XX_RB_DEPTH_BUFFER_INFO_DEPTH_FORMAT(DEPTH6_NONE));
233 OUT_RING(ring, 0x00000000); /* RB_DEPTH_BUFFER_PITCH */
234 OUT_RING(ring, 0x00000000); /* RB_DEPTH_BUFFER_ARRAY_PITCH */
235 OUT_RING(ring, 0x00000000); /* RB_DEPTH_BUFFER_BASE_LO */
236 OUT_RING(ring, 0x00000000); /* RB_DEPTH_BUFFER_BASE_HI */
237 OUT_RING(ring, 0x00000000); /* RB_DEPTH_BUFFER_BASE_GMEM */
238
239 OUT_REG(ring, A6XX_GRAS_SU_DEPTH_BUFFER_INFO(.depth_format = DEPTH6_NONE));
240
241 OUT_PKT4(ring, REG_A6XX_GRAS_LRZ_BUFFER_BASE_LO, 5);
242 OUT_RING(ring, 0x00000000); /* RB_DEPTH_FLAG_BUFFER_BASE_LO */
243 OUT_RING(ring, 0x00000000); /* RB_DEPTH_FLAG_BUFFER_BASE_HI */
244 OUT_RING(ring, 0x00000000); /* GRAS_LRZ_BUFFER_PITCH */
245 OUT_RING(ring, 0x00000000); /* GRAS_LRZ_FAST_CLEAR_BUFFER_BASE_LO */
246 OUT_RING(ring, 0x00000000); /* GRAS_LRZ_FAST_CLEAR_BUFFER_BASE_HI */
247
248 OUT_REG(ring, A6XX_RB_STENCIL_INFO(0));
249 }
250 }
251
252 static bool
253 use_hw_binning(struct fd_batch *batch)
254 {
255 const struct fd_gmem_stateobj *gmem = batch->gmem_state;
256
257 // TODO figure out hw limits for binning
258
259 return fd_binning_enabled && ((gmem->nbins_x * gmem->nbins_y) >= 2) &&
260 (batch->num_draws > 0);
261 }
262
263 static void
264 patch_fb_read(struct fd_batch *batch)
265 {
266 const struct fd_gmem_stateobj *gmem = batch->gmem_state;
267
268 for (unsigned i = 0; i < fd_patch_num_elements(&batch->fb_read_patches); i++) {
269 struct fd_cs_patch *patch = fd_patch_element(&batch->fb_read_patches, i);
270 *patch->cs = patch->val | A6XX_TEX_CONST_2_PITCH(gmem->bin_w * gmem->cbuf_cpp[0]);
271 }
272 util_dynarray_clear(&batch->fb_read_patches);
273 }
274
275 static void
276 update_render_cntl(struct fd_batch *batch, struct pipe_framebuffer_state *pfb, bool binning)
277 {
278 struct fd_ringbuffer *ring = batch->gmem;
279 uint32_t cntl = 0;
280 bool depth_ubwc_enable = false;
281 uint32_t mrts_ubwc_enable = 0;
282 int i;
283
284 if (pfb->zsbuf) {
285 struct fd_resource *rsc = fd_resource(pfb->zsbuf->texture);
286 depth_ubwc_enable = fd_resource_ubwc_enabled(rsc, pfb->zsbuf->u.tex.level);
287 }
288
289 for (i = 0; i < pfb->nr_cbufs; i++) {
290 if (!pfb->cbufs[i])
291 continue;
292
293 struct pipe_surface *psurf = pfb->cbufs[i];
294 struct fd_resource *rsc = fd_resource(psurf->texture);
295 if (!rsc->bo)
296 continue;
297
298 if (fd_resource_ubwc_enabled(rsc, psurf->u.tex.level))
299 mrts_ubwc_enable |= 1 << i;
300 }
301
302 cntl |= A6XX_RB_RENDER_CNTL_UNK4;
303 if (binning)
304 cntl |= A6XX_RB_RENDER_CNTL_BINNING;
305
306 OUT_PKT7(ring, CP_REG_WRITE, 3);
307 OUT_RING(ring, CP_REG_WRITE_0_TRACKER(TRACK_RENDER_CNTL));
308 OUT_RING(ring, REG_A6XX_RB_RENDER_CNTL);
309 OUT_RING(ring, cntl |
310 COND(depth_ubwc_enable, A6XX_RB_RENDER_CNTL_FLAG_DEPTH) |
311 A6XX_RB_RENDER_CNTL_FLAG_MRTS(mrts_ubwc_enable));
312 }
313
314 /* extra size to store VSC_DRAW_STRM_SIZE: */
315 #define VSC_DRAW_STRM_SIZE(pitch) ((pitch) * 32 + 0x100)
316 #define VSC_PRIM_STRM_SIZE(pitch) ((pitch) * 32)
317
318 static void
319 update_vsc_pipe(struct fd_batch *batch)
320 {
321 struct fd_context *ctx = batch->ctx;
322 struct fd6_context *fd6_ctx = fd6_context(ctx);
323 const struct fd_gmem_stateobj *gmem = batch->gmem_state;
324 struct fd_ringbuffer *ring = batch->gmem;
325 int i;
326
327 if (batch->draw_strm_bits/8 > fd6_ctx->vsc_draw_strm_pitch) {
328 if (fd6_ctx->vsc_draw_strm)
329 fd_bo_del(fd6_ctx->vsc_draw_strm);
330 fd6_ctx->vsc_draw_strm = NULL;
331 /* Note: probably only need to align to 0x40, but aligning stronger
332 * reduces the odds that we will have to realloc again on the next
333 * frame:
334 */
335 fd6_ctx->vsc_draw_strm_pitch = align(batch->draw_strm_bits/8, 0x4000);
336 debug_printf("pre-resize VSC_DRAW_STRM_PITCH to: 0x%x\n",
337 fd6_ctx->vsc_draw_strm_pitch);
338 }
339
340 if (batch->prim_strm_bits/8 > fd6_ctx->vsc_prim_strm_pitch) {
341 if (fd6_ctx->vsc_prim_strm)
342 fd_bo_del(fd6_ctx->vsc_prim_strm);
343 fd6_ctx->vsc_prim_strm = NULL;
344 fd6_ctx->vsc_prim_strm_pitch = align(batch->prim_strm_bits/8, 0x4000);
345 debug_printf("pre-resize VSC_PRIM_STRM_PITCH to: 0x%x\n",
346 fd6_ctx->vsc_prim_strm_pitch);
347 }
348
349 if (!fd6_ctx->vsc_draw_strm) {
350 fd6_ctx->vsc_draw_strm = fd_bo_new(ctx->screen->dev,
351 VSC_DRAW_STRM_SIZE(fd6_ctx->vsc_draw_strm_pitch),
352 DRM_FREEDRENO_GEM_TYPE_KMEM, "vsc_draw_strm");
353 }
354
355 if (!fd6_ctx->vsc_prim_strm) {
356 fd6_ctx->vsc_prim_strm = fd_bo_new(ctx->screen->dev,
357 VSC_PRIM_STRM_SIZE(fd6_ctx->vsc_prim_strm_pitch),
358 DRM_FREEDRENO_GEM_TYPE_KMEM, "vsc_prim_strm");
359 }
360
361 OUT_REG(ring,
362 A6XX_VSC_BIN_SIZE(.width = gmem->bin_w, .height = gmem->bin_h),
363 A6XX_VSC_DRAW_STRM_SIZE_ADDRESS(
364 .bo = fd6_ctx->vsc_draw_strm,
365 .bo_offset = 32 * fd6_ctx->vsc_draw_strm_pitch));
366
367 OUT_REG(ring, A6XX_VSC_BIN_COUNT(.nx = gmem->nbins_x,
368 .ny = gmem->nbins_y));
369
370 OUT_PKT4(ring, REG_A6XX_VSC_PIPE_CONFIG_REG(0), 32);
371 for (i = 0; i < 32; i++) {
372 const struct fd_vsc_pipe *pipe = &gmem->vsc_pipe[i];
373 OUT_RING(ring, A6XX_VSC_PIPE_CONFIG_REG_X(pipe->x) |
374 A6XX_VSC_PIPE_CONFIG_REG_Y(pipe->y) |
375 A6XX_VSC_PIPE_CONFIG_REG_W(pipe->w) |
376 A6XX_VSC_PIPE_CONFIG_REG_H(pipe->h));
377 }
378
379 OUT_REG(ring,
380 A6XX_VSC_PRIM_STRM_ADDRESS(.bo = fd6_ctx->vsc_prim_strm),
381 A6XX_VSC_PRIM_STRM_PITCH(.dword = fd6_ctx->vsc_prim_strm_pitch),
382 A6XX_VSC_PRIM_STRM_LIMIT(.dword = fd6_ctx->vsc_prim_strm_pitch - 64));
383
384 OUT_REG(ring,
385 A6XX_VSC_DRAW_STRM_ADDRESS(.bo = fd6_ctx->vsc_draw_strm),
386 A6XX_VSC_DRAW_STRM_PITCH(.dword = fd6_ctx->vsc_draw_strm_pitch),
387 A6XX_VSC_DRAW_STRM_LIMIT(.dword = fd6_ctx->vsc_draw_strm_pitch - 64));
388 }
389
390 /*
391 * If overflow is detected, either 0x1 (VSC_DRAW_STRM overflow) or 0x3
392 * (VSC_PRIM_STRM overflow) plus the size of the overflowed buffer is
393 * written to control->vsc_overflow. This allows the CPU to
394 * detect which buffer overflowed (and, since the current size is
395 * encoded as well, this protects against already-submitted but
396 * not executed batches from fooling the CPU into increasing the
397 * size again unnecessarily).
398 */
399 static void
400 emit_vsc_overflow_test(struct fd_batch *batch)
401 {
402 struct fd_ringbuffer *ring = batch->gmem;
403 const struct fd_gmem_stateobj *gmem = batch->gmem_state;
404 struct fd6_context *fd6_ctx = fd6_context(batch->ctx);
405
406 debug_assert((fd6_ctx->vsc_draw_strm_pitch & 0x3) == 0);
407 debug_assert((fd6_ctx->vsc_prim_strm_pitch & 0x3) == 0);
408
409 /* Check for overflow, write vsc_scratch if detected: */
410 for (int i = 0; i < gmem->num_vsc_pipes; i++) {
411 OUT_PKT7(ring, CP_COND_WRITE5, 8);
412 OUT_RING(ring, CP_COND_WRITE5_0_FUNCTION(WRITE_GE) |
413 CP_COND_WRITE5_0_WRITE_MEMORY);
414 OUT_RING(ring, CP_COND_WRITE5_1_POLL_ADDR_LO(REG_A6XX_VSC_DRAW_STRM_SIZE_REG(i)));
415 OUT_RING(ring, CP_COND_WRITE5_2_POLL_ADDR_HI(0));
416 OUT_RING(ring, CP_COND_WRITE5_3_REF(fd6_ctx->vsc_draw_strm_pitch - 64));
417 OUT_RING(ring, CP_COND_WRITE5_4_MASK(~0));
418 OUT_RELOC(ring, control_ptr(fd6_ctx, vsc_overflow)); /* WRITE_ADDR_LO/HI */
419 OUT_RING(ring, CP_COND_WRITE5_7_WRITE_DATA(1 + fd6_ctx->vsc_draw_strm_pitch));
420
421 OUT_PKT7(ring, CP_COND_WRITE5, 8);
422 OUT_RING(ring, CP_COND_WRITE5_0_FUNCTION(WRITE_GE) |
423 CP_COND_WRITE5_0_WRITE_MEMORY);
424 OUT_RING(ring, CP_COND_WRITE5_1_POLL_ADDR_LO(REG_A6XX_VSC_PRIM_STRM_SIZE_REG(i)));
425 OUT_RING(ring, CP_COND_WRITE5_2_POLL_ADDR_HI(0));
426 OUT_RING(ring, CP_COND_WRITE5_3_REF(fd6_ctx->vsc_prim_strm_pitch - 64));
427 OUT_RING(ring, CP_COND_WRITE5_4_MASK(~0));
428 OUT_RELOC(ring, control_ptr(fd6_ctx, vsc_overflow)); /* WRITE_ADDR_LO/HI */
429 OUT_RING(ring, CP_COND_WRITE5_7_WRITE_DATA(3 + fd6_ctx->vsc_prim_strm_pitch));
430 }
431
432 OUT_PKT7(ring, CP_WAIT_MEM_WRITES, 0);
433 }
434
435 static void
436 check_vsc_overflow(struct fd_context *ctx)
437 {
438 struct fd6_context *fd6_ctx = fd6_context(ctx);
439 struct fd6_control *control = fd_bo_map(fd6_ctx->control_mem);
440 uint32_t vsc_overflow = control->vsc_overflow;
441
442 if (!vsc_overflow)
443 return;
444
445 /* clear overflow flag: */
446 control->vsc_overflow = 0;
447
448 unsigned buffer = vsc_overflow & 0x3;
449 unsigned size = vsc_overflow & ~0x3;
450
451 if (buffer == 0x1) {
452 /* VSC_DRAW_STRM overflow: */
453
454 if (size < fd6_ctx->vsc_draw_strm_pitch) {
455 /* we've already increased the size, this overflow is
456 * from a batch submitted before resize, but executed
457 * after
458 */
459 return;
460 }
461
462 fd_bo_del(fd6_ctx->vsc_draw_strm);
463 fd6_ctx->vsc_draw_strm = NULL;
464 fd6_ctx->vsc_draw_strm_pitch *= 2;
465
466 debug_printf("resized VSC_DRAW_STRM_PITCH to: 0x%x\n",
467 fd6_ctx->vsc_draw_strm_pitch);
468
469 } else if (buffer == 0x3) {
470 /* VSC_PRIM_STRM overflow: */
471
472 if (size < fd6_ctx->vsc_prim_strm_pitch) {
473 /* we've already increased the size */
474 return;
475 }
476
477 fd_bo_del(fd6_ctx->vsc_prim_strm);
478 fd6_ctx->vsc_prim_strm = NULL;
479 fd6_ctx->vsc_prim_strm_pitch *= 2;
480
481 debug_printf("resized VSC_PRIM_STRM_PITCH to: 0x%x\n",
482 fd6_ctx->vsc_prim_strm_pitch);
483
484 } else {
485 /* NOTE: it's possible, for example, for overflow to corrupt the
486 * control page. I mostly just see this hit if I set initial VSC
487 * buffer size extremely small. Things still seem to recover,
488 * but maybe we should pre-emptively realloc vsc_data/vsc_data2
489 * and hope for different memory placement?
490 */
491 DBG("invalid vsc_overflow value: 0x%08x", vsc_overflow);
492 }
493 }
494
495 /*
496 * Emit conditional CP_INDIRECT_BRANCH based on VSC_STATE[p], ie. the IB
497 * is skipped for tiles that have no visible geometry.
498 */
499 static void
500 emit_conditional_ib(struct fd_batch *batch, const struct fd_tile *tile,
501 struct fd_ringbuffer *target)
502 {
503 struct fd_ringbuffer *ring = batch->gmem;
504
505 if (target->cur == target->start)
506 return;
507
508 emit_marker6(ring, 6);
509
510 unsigned count = fd_ringbuffer_cmd_count(target);
511
512 BEGIN_RING(ring, 5 + 4 * count); /* ensure conditional doesn't get split */
513
514 OUT_PKT7(ring, CP_REG_TEST, 1);
515 OUT_RING(ring, A6XX_CP_REG_TEST_0_REG(REG_A6XX_VSC_STATE_REG(tile->p)) |
516 A6XX_CP_REG_TEST_0_BIT(tile->n) |
517 A6XX_CP_REG_TEST_0_WAIT_FOR_ME);
518
519 OUT_PKT7(ring, CP_COND_REG_EXEC, 2);
520 OUT_RING(ring, CP_COND_REG_EXEC_0_MODE(PRED_TEST));
521 OUT_RING(ring, CP_COND_REG_EXEC_1_DWORDS(4 * count));
522
523 for (unsigned i = 0; i < count; i++) {
524 uint32_t dwords;
525 OUT_PKT7(ring, CP_INDIRECT_BUFFER, 3);
526 dwords = fd_ringbuffer_emit_reloc_ring_full(ring, target, i) / 4;
527 assert(dwords > 0);
528 OUT_RING(ring, dwords);
529 }
530
531 emit_marker6(ring, 6);
532 }
533
534 static void
535 set_scissor(struct fd_ringbuffer *ring, uint32_t x1, uint32_t y1, uint32_t x2, uint32_t y2)
536 {
537 OUT_REG(ring,
538 A6XX_GRAS_SC_WINDOW_SCISSOR_TL(.x = x1, .y = y1),
539 A6XX_GRAS_SC_WINDOW_SCISSOR_BR(.x = x2, .y = y2));
540
541 OUT_REG(ring,
542 A6XX_GRAS_2D_RESOLVE_CNTL_1(.x = x1, .y = y1),
543 A6XX_GRAS_2D_RESOLVE_CNTL_2(.x = x2, .y = y2));
544 }
545
546 static void
547 set_bin_size(struct fd_ringbuffer *ring, uint32_t w, uint32_t h, uint32_t flag)
548 {
549 OUT_REG(ring, A6XX_GRAS_BIN_CONTROL(.binw = w, .binh = h, .dword = flag));
550 OUT_REG(ring, A6XX_RB_BIN_CONTROL(.binw = w, .binh = h, .dword = flag));
551 /* no flag for RB_BIN_CONTROL2... */
552 OUT_REG(ring, A6XX_RB_BIN_CONTROL2(.binw = w, .binh = h));
553 }
554
555 static void
556 emit_binning_pass(struct fd_batch *batch)
557 {
558 struct fd_ringbuffer *ring = batch->gmem;
559 const struct fd_gmem_stateobj *gmem = batch->gmem_state;
560 struct fd6_context *fd6_ctx = fd6_context(batch->ctx);
561
562 debug_assert(!batch->tessellation);
563
564 set_scissor(ring, 0, 0, gmem->width - 1, gmem->height - 1);
565
566 emit_marker6(ring, 7);
567 OUT_PKT7(ring, CP_SET_MARKER, 1);
568 OUT_RING(ring, A6XX_CP_SET_MARKER_0_MODE(RM6_BINNING));
569 emit_marker6(ring, 7);
570
571 OUT_PKT7(ring, CP_SET_VISIBILITY_OVERRIDE, 1);
572 OUT_RING(ring, 0x1);
573
574 OUT_PKT7(ring, CP_SET_MODE, 1);
575 OUT_RING(ring, 0x1);
576
577 OUT_WFI5(ring);
578
579 OUT_REG(ring, A6XX_VFD_MODE_CNTL(.binning_pass = true));
580
581 update_vsc_pipe(batch);
582
583 OUT_PKT4(ring, REG_A6XX_PC_UNKNOWN_9805, 1);
584 OUT_RING(ring, fd6_ctx->magic.PC_UNKNOWN_9805);
585
586 OUT_PKT4(ring, REG_A6XX_SP_UNKNOWN_A0F8, 1);
587 OUT_RING(ring, fd6_ctx->magic.SP_UNKNOWN_A0F8);
588
589 OUT_PKT7(ring, CP_EVENT_WRITE, 1);
590 OUT_RING(ring, UNK_2C);
591
592 OUT_PKT4(ring, REG_A6XX_RB_WINDOW_OFFSET, 1);
593 OUT_RING(ring, A6XX_RB_WINDOW_OFFSET_X(0) |
594 A6XX_RB_WINDOW_OFFSET_Y(0));
595
596 OUT_PKT4(ring, REG_A6XX_SP_TP_WINDOW_OFFSET, 1);
597 OUT_RING(ring, A6XX_SP_TP_WINDOW_OFFSET_X(0) |
598 A6XX_SP_TP_WINDOW_OFFSET_Y(0));
599
600 /* emit IB to binning drawcmds: */
601 fd_log(batch, "GMEM: START BINNING IB");
602 fd6_emit_ib(ring, batch->draw);
603 fd_log(batch, "GMEM: END BINNING IB");
604
605 fd_reset_wfi(batch);
606
607 OUT_PKT7(ring, CP_SET_DRAW_STATE, 3);
608 OUT_RING(ring, CP_SET_DRAW_STATE__0_COUNT(0) |
609 CP_SET_DRAW_STATE__0_DISABLE_ALL_GROUPS |
610 CP_SET_DRAW_STATE__0_GROUP_ID(0));
611 OUT_RING(ring, CP_SET_DRAW_STATE__1_ADDR_LO(0));
612 OUT_RING(ring, CP_SET_DRAW_STATE__2_ADDR_HI(0));
613
614 OUT_PKT7(ring, CP_EVENT_WRITE, 1);
615 OUT_RING(ring, UNK_2D);
616
617 fd6_cache_inv(batch, ring);
618 fd6_cache_flush(batch, ring);
619 fd_wfi(batch, ring);
620
621 OUT_PKT7(ring, CP_WAIT_FOR_ME, 0);
622
623 fd_log(batch, "START VSC OVERFLOW TEST");
624 emit_vsc_overflow_test(batch);
625 fd_log(batch, "END VSC OVERFLOW TEST");
626
627 OUT_PKT7(ring, CP_SET_VISIBILITY_OVERRIDE, 1);
628 OUT_RING(ring, 0x0);
629
630 OUT_PKT7(ring, CP_SET_MODE, 1);
631 OUT_RING(ring, 0x0);
632
633 OUT_WFI5(ring);
634
635 OUT_PKT4(ring, REG_A6XX_RB_CCU_CNTL, 1);
636 OUT_RING(ring, fd6_ctx->magic.RB_CCU_CNTL_gmem);
637 }
638
639 static void
640 emit_msaa(struct fd_ringbuffer *ring, unsigned nr)
641 {
642 enum a3xx_msaa_samples samples = fd_msaa_samples(nr);
643
644 OUT_PKT4(ring, REG_A6XX_SP_TP_RAS_MSAA_CNTL, 2);
645 OUT_RING(ring, A6XX_SP_TP_RAS_MSAA_CNTL_SAMPLES(samples));
646 OUT_RING(ring, A6XX_SP_TP_DEST_MSAA_CNTL_SAMPLES(samples) |
647 COND(samples == MSAA_ONE, A6XX_SP_TP_DEST_MSAA_CNTL_MSAA_DISABLE));
648
649 OUT_PKT4(ring, REG_A6XX_GRAS_RAS_MSAA_CNTL, 2);
650 OUT_RING(ring, A6XX_GRAS_RAS_MSAA_CNTL_SAMPLES(samples));
651 OUT_RING(ring, A6XX_GRAS_DEST_MSAA_CNTL_SAMPLES(samples) |
652 COND(samples == MSAA_ONE, A6XX_GRAS_DEST_MSAA_CNTL_MSAA_DISABLE));
653
654 OUT_PKT4(ring, REG_A6XX_RB_RAS_MSAA_CNTL, 2);
655 OUT_RING(ring, A6XX_RB_RAS_MSAA_CNTL_SAMPLES(samples));
656 OUT_RING(ring, A6XX_RB_DEST_MSAA_CNTL_SAMPLES(samples) |
657 COND(samples == MSAA_ONE, A6XX_RB_DEST_MSAA_CNTL_MSAA_DISABLE));
658
659 OUT_PKT4(ring, REG_A6XX_RB_MSAA_CNTL, 1);
660 OUT_RING(ring, A6XX_RB_MSAA_CNTL_SAMPLES(samples));
661 }
662
663 static void prepare_tile_setup_ib(struct fd_batch *batch);
664 static void prepare_tile_fini_ib(struct fd_batch *batch);
665
666 /* before first tile */
667 static void
668 fd6_emit_tile_init(struct fd_batch *batch)
669 {
670 struct fd_context *ctx = batch->ctx;
671 struct fd_ringbuffer *ring = batch->gmem;
672 struct pipe_framebuffer_state *pfb = &batch->framebuffer;
673 const struct fd_gmem_stateobj *gmem = batch->gmem_state;
674
675 fd6_emit_restore(batch, ring);
676
677 fd6_emit_lrz_flush(ring);
678
679 if (batch->lrz_clear) {
680 fd_log(batch, "START LRZ CLEAR");
681 fd6_emit_ib(ring, batch->lrz_clear);
682 fd_log(batch, "END LRZ CLEAR");
683 }
684
685 fd6_cache_inv(batch, ring);
686
687 prepare_tile_setup_ib(batch);
688 prepare_tile_fini_ib(batch);
689
690 OUT_PKT7(ring, CP_SKIP_IB2_ENABLE_GLOBAL, 1);
691 OUT_RING(ring, 0x0);
692
693 /* blob controls "local" in IB2, but I think that is not required */
694 OUT_PKT7(ring, CP_SKIP_IB2_ENABLE_LOCAL, 1);
695 OUT_RING(ring, 0x1);
696
697 fd_wfi(batch, ring);
698 OUT_PKT4(ring, REG_A6XX_RB_CCU_CNTL, 1);
699 OUT_RING(ring, fd6_context(ctx)->magic.RB_CCU_CNTL_gmem);
700
701 emit_zs(ring, pfb->zsbuf, batch->gmem_state);
702 emit_mrt(ring, pfb, batch->gmem_state);
703 emit_msaa(ring, pfb->samples);
704 patch_fb_read(batch);
705
706 if (use_hw_binning(batch)) {
707 /* enable stream-out during binning pass: */
708 OUT_REG(ring, A6XX_VPC_SO_DISABLE(false));
709
710 set_bin_size(ring, gmem->bin_w, gmem->bin_h,
711 A6XX_RB_BIN_CONTROL_BINNING_PASS | 0x6000000);
712 update_render_cntl(batch, pfb, true);
713 emit_binning_pass(batch);
714
715 /* and disable stream-out for draw pass: */
716 OUT_REG(ring, A6XX_VPC_SO_DISABLE(true));
717
718 /*
719 * NOTE: even if we detect VSC overflow and disable use of
720 * visibility stream in draw pass, it is still safe to execute
721 * the reset of these cmds:
722 */
723
724 // NOTE a618 not setting .USE_VIZ .. from a quick check on a630, it
725 // does not appear that this bit changes much (ie. it isn't actually
726 // .USE_VIZ like previous gens)
727 set_bin_size(ring, gmem->bin_w, gmem->bin_h,
728 A6XX_RB_BIN_CONTROL_USE_VIZ | 0x6000000);
729
730 OUT_PKT4(ring, REG_A6XX_VFD_MODE_CNTL, 1);
731 OUT_RING(ring, 0x0);
732
733 OUT_PKT4(ring, REG_A6XX_PC_UNKNOWN_9805, 1);
734 OUT_RING(ring, fd6_context(ctx)->magic.PC_UNKNOWN_9805);
735
736 OUT_PKT4(ring, REG_A6XX_SP_UNKNOWN_A0F8, 1);
737 OUT_RING(ring, fd6_context(ctx)->magic.SP_UNKNOWN_A0F8);
738
739 OUT_PKT7(ring, CP_SKIP_IB2_ENABLE_GLOBAL, 1);
740 OUT_RING(ring, 0x1);
741 } else {
742 /* no binning pass, so enable stream-out for draw pass:: */
743 OUT_REG(ring, A6XX_VPC_SO_DISABLE(false));
744
745 set_bin_size(ring, gmem->bin_w, gmem->bin_h, 0x6000000);
746 }
747
748 update_render_cntl(batch, pfb, false);
749 }
750
751 static void
752 set_window_offset(struct fd_ringbuffer *ring, uint32_t x1, uint32_t y1)
753 {
754 OUT_PKT4(ring, REG_A6XX_RB_WINDOW_OFFSET, 1);
755 OUT_RING(ring, A6XX_RB_WINDOW_OFFSET_X(x1) |
756 A6XX_RB_WINDOW_OFFSET_Y(y1));
757
758 OUT_PKT4(ring, REG_A6XX_RB_WINDOW_OFFSET2, 1);
759 OUT_RING(ring, A6XX_RB_WINDOW_OFFSET2_X(x1) |
760 A6XX_RB_WINDOW_OFFSET2_Y(y1));
761
762 OUT_PKT4(ring, REG_A6XX_SP_WINDOW_OFFSET, 1);
763 OUT_RING(ring, A6XX_SP_WINDOW_OFFSET_X(x1) |
764 A6XX_SP_WINDOW_OFFSET_Y(y1));
765
766 OUT_PKT4(ring, REG_A6XX_SP_TP_WINDOW_OFFSET, 1);
767 OUT_RING(ring, A6XX_SP_TP_WINDOW_OFFSET_X(x1) |
768 A6XX_SP_TP_WINDOW_OFFSET_Y(y1));
769 }
770
771 /* before mem2gmem */
772 static void
773 fd6_emit_tile_prep(struct fd_batch *batch, const struct fd_tile *tile)
774 {
775 struct fd_context *ctx = batch->ctx;
776 const struct fd_gmem_stateobj *gmem = batch->gmem_state;
777 struct fd6_context *fd6_ctx = fd6_context(ctx);
778 struct fd_ringbuffer *ring = batch->gmem;
779
780 emit_marker6(ring, 7);
781 OUT_PKT7(ring, CP_SET_MARKER, 1);
782 OUT_RING(ring, A6XX_CP_SET_MARKER_0_MODE(RM6_GMEM));
783 emit_marker6(ring, 7);
784
785 uint32_t x1 = tile->xoff;
786 uint32_t y1 = tile->yoff;
787 uint32_t x2 = tile->xoff + tile->bin_w - 1;
788 uint32_t y2 = tile->yoff + tile->bin_h - 1;
789
790 set_scissor(ring, x1, y1, x2, y2);
791
792 if (use_hw_binning(batch)) {
793 const struct fd_vsc_pipe *pipe = &gmem->vsc_pipe[tile->p];
794
795 OUT_PKT7(ring, CP_WAIT_FOR_ME, 0);
796
797 OUT_PKT7(ring, CP_SET_MODE, 1);
798 OUT_RING(ring, 0x0);
799
800 OUT_PKT7(ring, CP_SET_BIN_DATA5, 7);
801 OUT_RING(ring, CP_SET_BIN_DATA5_0_VSC_SIZE(pipe->w * pipe->h) |
802 CP_SET_BIN_DATA5_0_VSC_N(tile->n));
803 OUT_RELOC(ring, fd6_ctx->vsc_draw_strm, /* per-pipe draw-stream address */
804 (tile->p * fd6_ctx->vsc_draw_strm_pitch), 0, 0);
805 OUT_RELOC(ring, fd6_ctx->vsc_draw_strm, /* VSC_DRAW_STRM_ADDRESS + (p * 4) */
806 (tile->p * 4) + (32 * fd6_ctx->vsc_draw_strm_pitch), 0, 0);
807 OUT_RELOC(ring, fd6_ctx->vsc_prim_strm,
808 (tile->p * fd6_ctx->vsc_prim_strm_pitch), 0, 0);
809
810 OUT_PKT7(ring, CP_SET_VISIBILITY_OVERRIDE, 1);
811 OUT_RING(ring, 0x0);
812
813 set_window_offset(ring, x1, y1);
814
815 const struct fd_gmem_stateobj *gmem = batch->gmem_state;
816 set_bin_size(ring, gmem->bin_w, gmem->bin_h, 0x6000000);
817
818 OUT_PKT7(ring, CP_SET_MODE, 1);
819 OUT_RING(ring, 0x0);
820 } else {
821 set_window_offset(ring, x1, y1);
822
823 OUT_PKT7(ring, CP_SET_VISIBILITY_OVERRIDE, 1);
824 OUT_RING(ring, 0x1);
825
826 OUT_PKT7(ring, CP_SET_MODE, 1);
827 OUT_RING(ring, 0x0);
828 }
829 }
830
831 static void
832 set_blit_scissor(struct fd_batch *batch, struct fd_ringbuffer *ring)
833 {
834 struct pipe_scissor_state blit_scissor = batch->max_scissor;
835
836 blit_scissor.minx = ROUND_DOWN_TO(blit_scissor.minx, 16);
837 blit_scissor.miny = ROUND_DOWN_TO(blit_scissor.miny, 4);
838 blit_scissor.maxx = ALIGN(blit_scissor.maxx, 16);
839 blit_scissor.maxy = ALIGN(blit_scissor.maxy, 4);
840
841 OUT_PKT4(ring, REG_A6XX_RB_BLIT_SCISSOR_TL, 2);
842 OUT_RING(ring,
843 A6XX_RB_BLIT_SCISSOR_TL_X(blit_scissor.minx) |
844 A6XX_RB_BLIT_SCISSOR_TL_Y(blit_scissor.miny));
845 OUT_RING(ring,
846 A6XX_RB_BLIT_SCISSOR_BR_X(blit_scissor.maxx - 1) |
847 A6XX_RB_BLIT_SCISSOR_BR_Y(blit_scissor.maxy - 1));
848 }
849
850 static void
851 emit_blit(struct fd_batch *batch,
852 struct fd_ringbuffer *ring,
853 uint32_t base,
854 struct pipe_surface *psurf,
855 bool stencil)
856 {
857 struct fd_resource *rsc = fd_resource(psurf->texture);
858 enum pipe_format pfmt = psurf->format;
859 uint32_t offset;
860 bool ubwc_enabled;
861
862 debug_assert(psurf->u.tex.first_layer == psurf->u.tex.last_layer);
863
864 /* separate stencil case: */
865 if (stencil) {
866 rsc = rsc->stencil;
867 pfmt = rsc->base.format;
868 }
869
870 offset = fd_resource_offset(rsc, psurf->u.tex.level,
871 psurf->u.tex.first_layer);
872 ubwc_enabled = fd_resource_ubwc_enabled(rsc, psurf->u.tex.level);
873
874 debug_assert(psurf->u.tex.first_layer == psurf->u.tex.last_layer);
875
876 enum a6xx_format format = fd6_pipe2color(pfmt);
877 uint32_t stride = fd_resource_pitch(rsc, psurf->u.tex.level);
878 uint32_t size = fd_resource_slice(rsc, psurf->u.tex.level)->size0;
879 enum a3xx_color_swap swap = fd6_resource_swap(rsc, pfmt);
880 enum a3xx_msaa_samples samples =
881 fd_msaa_samples(rsc->base.nr_samples);
882 uint32_t tile_mode = fd_resource_tile_mode(&rsc->base, psurf->u.tex.level);
883
884 OUT_REG(ring,
885 A6XX_RB_BLIT_DST_INFO(.tile_mode = tile_mode, .samples = samples,
886 .color_format = format, .color_swap = swap, .flags = ubwc_enabled),
887 A6XX_RB_BLIT_DST(.bo = rsc->bo, .bo_offset = offset),
888 A6XX_RB_BLIT_DST_PITCH(.a6xx_rb_blit_dst_pitch = stride),
889 A6XX_RB_BLIT_DST_ARRAY_PITCH(.a6xx_rb_blit_dst_array_pitch = size));
890
891 OUT_REG(ring, A6XX_RB_BLIT_BASE_GMEM(.dword = base));
892
893 if (ubwc_enabled) {
894 OUT_PKT4(ring, REG_A6XX_RB_BLIT_FLAG_DST_LO, 3);
895 fd6_emit_flag_reference(ring, rsc,
896 psurf->u.tex.level, psurf->u.tex.first_layer);
897 }
898
899 fd6_emit_blit(batch, ring);
900 }
901
902 static void
903 emit_restore_blit(struct fd_batch *batch,
904 struct fd_ringbuffer *ring,
905 uint32_t base,
906 struct pipe_surface *psurf,
907 unsigned buffer)
908 {
909 bool stencil = (buffer == FD_BUFFER_STENCIL);
910
911 OUT_REG(ring, A6XX_RB_BLIT_INFO(
912 .gmem = true, .unk0 = true,
913 .depth = (buffer == FD_BUFFER_DEPTH),
914 .integer = util_format_is_pure_integer(psurf->format)));
915
916 emit_blit(batch, ring, base, psurf, stencil);
917 }
918
919 static void
920 emit_clears(struct fd_batch *batch, struct fd_ringbuffer *ring)
921 {
922 struct pipe_framebuffer_state *pfb = &batch->framebuffer;
923 const struct fd_gmem_stateobj *gmem = batch->gmem_state;
924 enum a3xx_msaa_samples samples = fd_msaa_samples(pfb->samples);
925
926 uint32_t buffers = batch->fast_cleared;
927
928 if (buffers & PIPE_CLEAR_COLOR) {
929
930 for (int i = 0; i < pfb->nr_cbufs; i++) {
931 union pipe_color_union *color = &batch->clear_color[i];
932 union util_color uc = {0};
933
934 if (!pfb->cbufs[i])
935 continue;
936
937 if (!(buffers & (PIPE_CLEAR_COLOR0 << i)))
938 continue;
939
940 enum pipe_format pfmt = pfb->cbufs[i]->format;
941
942 // XXX I think RB_CLEAR_COLOR_DWn wants to take into account SWAP??
943 union pipe_color_union swapped;
944 switch (fd6_pipe2swap(pfmt)) {
945 case WZYX:
946 swapped.ui[0] = color->ui[0];
947 swapped.ui[1] = color->ui[1];
948 swapped.ui[2] = color->ui[2];
949 swapped.ui[3] = color->ui[3];
950 break;
951 case WXYZ:
952 swapped.ui[2] = color->ui[0];
953 swapped.ui[1] = color->ui[1];
954 swapped.ui[0] = color->ui[2];
955 swapped.ui[3] = color->ui[3];
956 break;
957 case ZYXW:
958 swapped.ui[3] = color->ui[0];
959 swapped.ui[0] = color->ui[1];
960 swapped.ui[1] = color->ui[2];
961 swapped.ui[2] = color->ui[3];
962 break;
963 case XYZW:
964 swapped.ui[3] = color->ui[0];
965 swapped.ui[2] = color->ui[1];
966 swapped.ui[1] = color->ui[2];
967 swapped.ui[0] = color->ui[3];
968 break;
969 }
970
971 util_pack_color_union(pfmt, &uc, &swapped);
972
973 OUT_PKT4(ring, REG_A6XX_RB_BLIT_DST_INFO, 1);
974 OUT_RING(ring, A6XX_RB_BLIT_DST_INFO_TILE_MODE(TILE6_LINEAR) |
975 A6XX_RB_BLIT_DST_INFO_SAMPLES(samples) |
976 A6XX_RB_BLIT_DST_INFO_COLOR_FORMAT(fd6_pipe2color(pfmt)));
977
978 OUT_PKT4(ring, REG_A6XX_RB_BLIT_INFO, 1);
979 OUT_RING(ring, A6XX_RB_BLIT_INFO_GMEM |
980 A6XX_RB_BLIT_INFO_CLEAR_MASK(0xf));
981
982 OUT_PKT4(ring, REG_A6XX_RB_BLIT_BASE_GMEM, 1);
983 OUT_RING(ring, gmem->cbuf_base[i]);
984
985 OUT_PKT4(ring, REG_A6XX_RB_UNKNOWN_88D0, 1);
986 OUT_RING(ring, 0);
987
988 OUT_PKT4(ring, REG_A6XX_RB_BLIT_CLEAR_COLOR_DW0, 4);
989 OUT_RING(ring, uc.ui[0]);
990 OUT_RING(ring, uc.ui[1]);
991 OUT_RING(ring, uc.ui[2]);
992 OUT_RING(ring, uc.ui[3]);
993
994 fd6_emit_blit(batch, ring);
995 }
996 }
997
998 const bool has_depth = pfb->zsbuf;
999 const bool has_separate_stencil =
1000 has_depth && fd_resource(pfb->zsbuf->texture)->stencil;
1001
1002 /* First clear depth or combined depth/stencil. */
1003 if ((has_depth && (buffers & PIPE_CLEAR_DEPTH)) ||
1004 (!has_separate_stencil && (buffers & PIPE_CLEAR_STENCIL))) {
1005 enum pipe_format pfmt = pfb->zsbuf->format;
1006 uint32_t clear_value;
1007 uint32_t mask = 0;
1008
1009 if (has_separate_stencil) {
1010 pfmt = util_format_get_depth_only(pfb->zsbuf->format);
1011 clear_value = util_pack_z(pfmt, batch->clear_depth);
1012 } else {
1013 pfmt = pfb->zsbuf->format;
1014 clear_value = util_pack_z_stencil(pfmt, batch->clear_depth,
1015 batch->clear_stencil);
1016 }
1017
1018 if (buffers & PIPE_CLEAR_DEPTH)
1019 mask |= 0x1;
1020
1021 if (!has_separate_stencil && (buffers & PIPE_CLEAR_STENCIL))
1022 mask |= 0x2;
1023
1024 OUT_PKT4(ring, REG_A6XX_RB_BLIT_DST_INFO, 1);
1025 OUT_RING(ring, A6XX_RB_BLIT_DST_INFO_TILE_MODE(TILE6_LINEAR) |
1026 A6XX_RB_BLIT_DST_INFO_SAMPLES(samples) |
1027 A6XX_RB_BLIT_DST_INFO_COLOR_FORMAT(fd6_pipe2color(pfmt)));
1028
1029 OUT_PKT4(ring, REG_A6XX_RB_BLIT_INFO, 1);
1030 OUT_RING(ring, A6XX_RB_BLIT_INFO_GMEM |
1031 // XXX UNK0 for separate stencil ??
1032 A6XX_RB_BLIT_INFO_DEPTH |
1033 A6XX_RB_BLIT_INFO_CLEAR_MASK(mask));
1034
1035 OUT_PKT4(ring, REG_A6XX_RB_BLIT_BASE_GMEM, 1);
1036 OUT_RING(ring, gmem->zsbuf_base[0]);
1037
1038 OUT_PKT4(ring, REG_A6XX_RB_UNKNOWN_88D0, 1);
1039 OUT_RING(ring, 0);
1040
1041 OUT_PKT4(ring, REG_A6XX_RB_BLIT_CLEAR_COLOR_DW0, 1);
1042 OUT_RING(ring, clear_value);
1043
1044 fd6_emit_blit(batch, ring);
1045 }
1046
1047 /* Then clear the separate stencil buffer in case of 32 bit depth
1048 * formats with separate stencil. */
1049 if (has_separate_stencil && (buffers & PIPE_CLEAR_STENCIL)) {
1050 OUT_PKT4(ring, REG_A6XX_RB_BLIT_DST_INFO, 1);
1051 OUT_RING(ring, A6XX_RB_BLIT_DST_INFO_TILE_MODE(TILE6_LINEAR) |
1052 A6XX_RB_BLIT_DST_INFO_SAMPLES(samples) |
1053 A6XX_RB_BLIT_DST_INFO_COLOR_FORMAT(FMT6_8_UINT));
1054
1055 OUT_PKT4(ring, REG_A6XX_RB_BLIT_INFO, 1);
1056 OUT_RING(ring, A6XX_RB_BLIT_INFO_GMEM |
1057 //A6XX_RB_BLIT_INFO_UNK0 |
1058 A6XX_RB_BLIT_INFO_DEPTH |
1059 A6XX_RB_BLIT_INFO_CLEAR_MASK(0x1));
1060
1061 OUT_PKT4(ring, REG_A6XX_RB_BLIT_BASE_GMEM, 1);
1062 OUT_RING(ring, gmem->zsbuf_base[1]);
1063
1064 OUT_PKT4(ring, REG_A6XX_RB_UNKNOWN_88D0, 1);
1065 OUT_RING(ring, 0);
1066
1067 OUT_PKT4(ring, REG_A6XX_RB_BLIT_CLEAR_COLOR_DW0, 1);
1068 OUT_RING(ring, batch->clear_stencil & 0xff);
1069
1070 fd6_emit_blit(batch, ring);
1071 }
1072 }
1073
1074 /*
1075 * transfer from system memory to gmem
1076 */
1077 static void
1078 emit_restore_blits(struct fd_batch *batch, struct fd_ringbuffer *ring)
1079 {
1080 const struct fd_gmem_stateobj *gmem = batch->gmem_state;
1081 struct pipe_framebuffer_state *pfb = &batch->framebuffer;
1082
1083 if (batch->restore & FD_BUFFER_COLOR) {
1084 unsigned i;
1085 for (i = 0; i < pfb->nr_cbufs; i++) {
1086 if (!pfb->cbufs[i])
1087 continue;
1088 if (!(batch->restore & (PIPE_CLEAR_COLOR0 << i)))
1089 continue;
1090 emit_restore_blit(batch, ring, gmem->cbuf_base[i], pfb->cbufs[i],
1091 FD_BUFFER_COLOR);
1092 }
1093 }
1094
1095 if (batch->restore & (FD_BUFFER_DEPTH | FD_BUFFER_STENCIL)) {
1096 struct fd_resource *rsc = fd_resource(pfb->zsbuf->texture);
1097
1098 if (!rsc->stencil || (batch->restore & FD_BUFFER_DEPTH)) {
1099 emit_restore_blit(batch, ring, gmem->zsbuf_base[0], pfb->zsbuf,
1100 FD_BUFFER_DEPTH);
1101 }
1102 if (rsc->stencil && (batch->restore & FD_BUFFER_STENCIL)) {
1103 emit_restore_blit(batch, ring, gmem->zsbuf_base[1], pfb->zsbuf,
1104 FD_BUFFER_STENCIL);
1105 }
1106 }
1107 }
1108
1109 static void
1110 prepare_tile_setup_ib(struct fd_batch *batch)
1111 {
1112 batch->tile_setup = fd_submit_new_ringbuffer(batch->submit, 0x1000,
1113 FD_RINGBUFFER_STREAMING);
1114
1115 set_blit_scissor(batch, batch->tile_setup);
1116
1117 emit_restore_blits(batch, batch->tile_setup);
1118 emit_clears(batch, batch->tile_setup);
1119 }
1120
1121 /*
1122 * transfer from system memory to gmem
1123 */
1124 static void
1125 fd6_emit_tile_mem2gmem(struct fd_batch *batch, const struct fd_tile *tile)
1126 {
1127 }
1128
1129 /* before IB to rendering cmds: */
1130 static void
1131 fd6_emit_tile_renderprep(struct fd_batch *batch, const struct fd_tile *tile)
1132 {
1133 fd_log(batch, "TILE: START CLEAR/RESTORE");
1134 if (batch->fast_cleared || !use_hw_binning(batch)) {
1135 fd6_emit_ib(batch->gmem, batch->tile_setup);
1136 } else {
1137 emit_conditional_ib(batch, tile, batch->tile_setup);
1138 }
1139 fd_log(batch, "TILE: END CLEAR/RESTORE");
1140 }
1141
1142 static void
1143 emit_resolve_blit(struct fd_batch *batch,
1144 struct fd_ringbuffer *ring,
1145 uint32_t base,
1146 struct pipe_surface *psurf,
1147 unsigned buffer)
1148 {
1149 uint32_t info = 0;
1150 bool stencil = false;
1151
1152 if (!fd_resource(psurf->texture)->valid)
1153 return;
1154
1155 switch (buffer) {
1156 case FD_BUFFER_COLOR:
1157 break;
1158 case FD_BUFFER_STENCIL:
1159 info |= A6XX_RB_BLIT_INFO_UNK0;
1160 stencil = true;
1161 break;
1162 case FD_BUFFER_DEPTH:
1163 info |= A6XX_RB_BLIT_INFO_DEPTH;
1164 break;
1165 }
1166
1167 if (util_format_is_pure_integer(psurf->format))
1168 info |= A6XX_RB_BLIT_INFO_INTEGER;
1169
1170 OUT_PKT4(ring, REG_A6XX_RB_BLIT_INFO, 1);
1171 OUT_RING(ring, info);
1172
1173 emit_blit(batch, ring, base, psurf, stencil);
1174 }
1175
1176 /*
1177 * transfer from gmem to system memory (ie. normal RAM)
1178 */
1179
1180 static void
1181 prepare_tile_fini_ib(struct fd_batch *batch)
1182 {
1183 const struct fd_gmem_stateobj *gmem = batch->gmem_state;
1184 struct pipe_framebuffer_state *pfb = &batch->framebuffer;
1185 struct fd_ringbuffer *ring;
1186
1187 batch->tile_fini = fd_submit_new_ringbuffer(batch->submit, 0x1000,
1188 FD_RINGBUFFER_STREAMING);
1189 ring = batch->tile_fini;
1190
1191 set_blit_scissor(batch, ring);
1192
1193 if (batch->resolve & (FD_BUFFER_DEPTH | FD_BUFFER_STENCIL)) {
1194 struct fd_resource *rsc = fd_resource(pfb->zsbuf->texture);
1195
1196 if (!rsc->stencil || (batch->resolve & FD_BUFFER_DEPTH)) {
1197 emit_resolve_blit(batch, ring,
1198 gmem->zsbuf_base[0], pfb->zsbuf,
1199 FD_BUFFER_DEPTH);
1200 }
1201 if (rsc->stencil && (batch->resolve & FD_BUFFER_STENCIL)) {
1202 emit_resolve_blit(batch, ring,
1203 gmem->zsbuf_base[1], pfb->zsbuf,
1204 FD_BUFFER_STENCIL);
1205 }
1206 }
1207
1208 if (batch->resolve & FD_BUFFER_COLOR) {
1209 unsigned i;
1210 for (i = 0; i < pfb->nr_cbufs; i++) {
1211 if (!pfb->cbufs[i])
1212 continue;
1213 if (!(batch->resolve & (PIPE_CLEAR_COLOR0 << i)))
1214 continue;
1215 emit_resolve_blit(batch, ring, gmem->cbuf_base[i], pfb->cbufs[i],
1216 FD_BUFFER_COLOR);
1217 }
1218 }
1219 }
1220
1221 static void
1222 fd6_emit_tile(struct fd_batch *batch, const struct fd_tile *tile)
1223 {
1224 if (!use_hw_binning(batch)) {
1225 fd6_emit_ib(batch->gmem, batch->draw);
1226 } else {
1227 emit_conditional_ib(batch, tile, batch->draw);
1228 }
1229
1230 if (batch->epilogue)
1231 fd6_emit_ib(batch->gmem, batch->epilogue);
1232 }
1233
1234 static void
1235 fd6_emit_tile_gmem2mem(struct fd_batch *batch, const struct fd_tile *tile)
1236 {
1237 struct fd_ringbuffer *ring = batch->gmem;
1238
1239 if (use_hw_binning(batch)) {
1240 OUT_PKT7(ring, CP_SET_MARKER, 1);
1241 OUT_RING(ring, A6XX_CP_SET_MARKER_0_MODE(RM6_ENDVIS));
1242 }
1243
1244 OUT_PKT7(ring, CP_SET_DRAW_STATE, 3);
1245 OUT_RING(ring, CP_SET_DRAW_STATE__0_COUNT(0) |
1246 CP_SET_DRAW_STATE__0_DISABLE_ALL_GROUPS |
1247 CP_SET_DRAW_STATE__0_GROUP_ID(0));
1248 OUT_RING(ring, CP_SET_DRAW_STATE__1_ADDR_LO(0));
1249 OUT_RING(ring, CP_SET_DRAW_STATE__2_ADDR_HI(0));
1250
1251 OUT_PKT7(ring, CP_SKIP_IB2_ENABLE_LOCAL, 1);
1252 OUT_RING(ring, 0x0);
1253
1254 emit_marker6(ring, 7);
1255 OUT_PKT7(ring, CP_SET_MARKER, 1);
1256 OUT_RING(ring, A6XX_CP_SET_MARKER_0_MODE(RM6_RESOLVE));
1257 emit_marker6(ring, 7);
1258
1259 fd_log(batch, "TILE: START RESOLVE");
1260 if (batch->fast_cleared || !use_hw_binning(batch)) {
1261 fd6_emit_ib(batch->gmem, batch->tile_fini);
1262 } else {
1263 emit_conditional_ib(batch, tile, batch->tile_fini);
1264 }
1265 fd_log(batch, "TILE: END RESOLVE");
1266 }
1267
1268 static void
1269 fd6_emit_tile_fini(struct fd_batch *batch)
1270 {
1271 struct fd_ringbuffer *ring = batch->gmem;
1272
1273 OUT_PKT4(ring, REG_A6XX_GRAS_LRZ_CNTL, 1);
1274 OUT_RING(ring, A6XX_GRAS_LRZ_CNTL_ENABLE);
1275
1276 fd6_emit_lrz_flush(ring);
1277
1278 fd6_event_write(batch, ring, PC_CCU_RESOLVE_TS, true);
1279
1280 if (use_hw_binning(batch)) {
1281 check_vsc_overflow(batch->ctx);
1282 }
1283 }
1284
1285 static void
1286 emit_sysmem_clears(struct fd_batch *batch, struct fd_ringbuffer *ring)
1287 {
1288 struct fd_context *ctx = batch->ctx;
1289 struct pipe_framebuffer_state *pfb = &batch->framebuffer;
1290
1291 uint32_t buffers = batch->fast_cleared;
1292
1293 if (buffers & PIPE_CLEAR_COLOR) {
1294 for (int i = 0; i < pfb->nr_cbufs; i++) {
1295 union pipe_color_union *color = &batch->clear_color[i];
1296
1297 if (!pfb->cbufs[i])
1298 continue;
1299
1300 if (!(buffers & (PIPE_CLEAR_COLOR0 << i)))
1301 continue;
1302
1303 fd6_clear_surface(ctx, ring,
1304 pfb->cbufs[i], pfb->width, pfb->height, color);
1305 }
1306 }
1307 if (buffers & (PIPE_CLEAR_DEPTH | PIPE_CLEAR_STENCIL)) {
1308 union pipe_color_union value = {};
1309
1310 const bool has_depth = pfb->zsbuf;
1311 struct pipe_resource *separate_stencil =
1312 has_depth && fd_resource(pfb->zsbuf->texture)->stencil ?
1313 &fd_resource(pfb->zsbuf->texture)->stencil->base : NULL;
1314
1315 if ((has_depth && (buffers & PIPE_CLEAR_DEPTH)) ||
1316 (!separate_stencil && (buffers & PIPE_CLEAR_STENCIL))) {
1317 value.f[0] = batch->clear_depth;
1318 value.ui[1] = batch->clear_stencil;
1319 fd6_clear_surface(ctx, ring,
1320 pfb->zsbuf, pfb->width, pfb->height, &value);
1321 }
1322
1323 if (separate_stencil && (buffers & PIPE_CLEAR_STENCIL)) {
1324 value.ui[0] = batch->clear_stencil;
1325
1326 struct pipe_surface stencil_surf = *pfb->zsbuf;
1327 stencil_surf.format = PIPE_FORMAT_S8_UINT;
1328 stencil_surf.texture = separate_stencil;
1329
1330 fd6_clear_surface(ctx, ring,
1331 &stencil_surf, pfb->width, pfb->height, &value);
1332 }
1333 }
1334
1335 fd6_event_write(batch, ring, PC_CCU_FLUSH_COLOR_TS, true);
1336 }
1337
1338 static void
1339 setup_tess_buffers(struct fd_batch *batch, struct fd_ringbuffer *ring)
1340 {
1341 struct fd_context *ctx = batch->ctx;
1342
1343 batch->tessfactor_bo = fd_bo_new(ctx->screen->dev,
1344 batch->tessfactor_size,
1345 DRM_FREEDRENO_GEM_TYPE_KMEM, "tessfactor");
1346
1347 batch->tessparam_bo = fd_bo_new(ctx->screen->dev,
1348 batch->tessparam_size,
1349 DRM_FREEDRENO_GEM_TYPE_KMEM, "tessparam");
1350
1351 OUT_PKT4(ring, REG_A6XX_PC_TESSFACTOR_ADDR_LO, 2);
1352 OUT_RELOC(ring, batch->tessfactor_bo, 0, 0, 0);
1353
1354 batch->tess_addrs_constobj->cur = batch->tess_addrs_constobj->start;
1355 OUT_RELOC(batch->tess_addrs_constobj, batch->tessparam_bo, 0, 0, 0);
1356 OUT_RELOC(batch->tess_addrs_constobj, batch->tessfactor_bo, 0, 0, 0);
1357 }
1358
1359 static void
1360 fd6_emit_sysmem_prep(struct fd_batch *batch)
1361 {
1362 struct pipe_framebuffer_state *pfb = &batch->framebuffer;
1363 struct fd_ringbuffer *ring = batch->gmem;
1364
1365 fd6_emit_restore(batch, ring);
1366
1367 if (pfb->width > 0 && pfb->height > 0)
1368 set_scissor(ring, 0, 0, pfb->width - 1, pfb->height - 1);
1369 else
1370 set_scissor(ring, 0, 0, 0, 0);
1371
1372 set_window_offset(ring, 0, 0);
1373
1374 set_bin_size(ring, 0, 0, 0xc00000); /* 0xc00000 = BYPASS? */
1375
1376 emit_sysmem_clears(batch, ring);
1377
1378 fd6_emit_lrz_flush(ring);
1379
1380 if (batch->lrz_clear)
1381 fd6_emit_ib(ring, batch->lrz_clear);
1382
1383 emit_marker6(ring, 7);
1384 OUT_PKT7(ring, CP_SET_MARKER, 1);
1385 OUT_RING(ring, A6XX_CP_SET_MARKER_0_MODE(RM6_BYPASS));
1386 emit_marker6(ring, 7);
1387
1388 if (batch->tessellation)
1389 setup_tess_buffers(batch, ring);
1390
1391 OUT_PKT7(ring, CP_SKIP_IB2_ENABLE_GLOBAL, 1);
1392 OUT_RING(ring, 0x0);
1393
1394 /* blob controls "local" in IB2, but I think that is not required */
1395 OUT_PKT7(ring, CP_SKIP_IB2_ENABLE_LOCAL, 1);
1396 OUT_RING(ring, 0x1);
1397
1398 fd6_event_write(batch, ring, PC_CCU_INVALIDATE_COLOR, false);
1399 fd6_cache_inv(batch, ring);
1400
1401 fd_wfi(batch, ring);
1402 OUT_PKT4(ring, REG_A6XX_RB_CCU_CNTL, 1);
1403 OUT_RING(ring, fd6_context(batch->ctx)->magic.RB_CCU_CNTL_bypass);
1404
1405 /* enable stream-out, with sysmem there is only one pass: */
1406 OUT_REG(ring, A6XX_VPC_SO_DISABLE(false));
1407
1408 OUT_PKT7(ring, CP_SET_VISIBILITY_OVERRIDE, 1);
1409 OUT_RING(ring, 0x1);
1410
1411 emit_zs(ring, pfb->zsbuf, NULL);
1412 emit_mrt(ring, pfb, NULL);
1413 emit_msaa(ring, pfb->samples);
1414
1415 update_render_cntl(batch, pfb, false);
1416 }
1417
1418 static void
1419 fd6_emit_sysmem_fini(struct fd_batch *batch)
1420 {
1421 struct fd_ringbuffer *ring = batch->gmem;
1422
1423 if (batch->epilogue)
1424 fd6_emit_ib(batch->gmem, batch->epilogue);
1425
1426 OUT_PKT7(ring, CP_SKIP_IB2_ENABLE_GLOBAL, 1);
1427 OUT_RING(ring, 0x0);
1428
1429 fd6_emit_lrz_flush(ring);
1430
1431 fd6_event_write(batch, ring, PC_CCU_FLUSH_COLOR_TS, true);
1432 }
1433
1434 void
1435 fd6_gmem_init(struct pipe_context *pctx)
1436 {
1437 struct fd_context *ctx = fd_context(pctx);
1438
1439 ctx->emit_tile_init = fd6_emit_tile_init;
1440 ctx->emit_tile_prep = fd6_emit_tile_prep;
1441 ctx->emit_tile_mem2gmem = fd6_emit_tile_mem2gmem;
1442 ctx->emit_tile_renderprep = fd6_emit_tile_renderprep;
1443 ctx->emit_tile = fd6_emit_tile;
1444 ctx->emit_tile_gmem2mem = fd6_emit_tile_gmem2mem;
1445 ctx->emit_tile_fini = fd6_emit_tile_fini;
1446 ctx->emit_sysmem_prep = fd6_emit_sysmem_prep;
1447 ctx->emit_sysmem_fini = fd6_emit_sysmem_fini;
1448 }