e2115454d8682100c154b2688873c9cecee1faeb
[mesa.git] / src / gallium / drivers / freedreno / a4xx / fd4_gmem.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/u_format.h"
34
35 #include "freedreno_draw.h"
36 #include "freedreno_state.h"
37 #include "freedreno_resource.h"
38
39 #include "fd4_gmem.h"
40 #include "fd4_context.h"
41 #include "fd4_draw.h"
42 #include "fd4_emit.h"
43 #include "fd4_program.h"
44 #include "fd4_format.h"
45 #include "fd4_zsa.h"
46
47 static void
48 emit_mrt(struct fd_ringbuffer *ring, unsigned nr_bufs,
49 struct pipe_surface **bufs, uint32_t *bases,
50 uint32_t bin_w, bool decode_srgb)
51 {
52 enum a4xx_tile_mode tile_mode;
53 unsigned i;
54
55 if (bin_w) {
56 tile_mode = 2;
57 } else {
58 tile_mode = TILE4_LINEAR;
59 }
60
61 for (i = 0; i < A4XX_MAX_RENDER_TARGETS; i++) {
62 enum a4xx_color_fmt format = 0;
63 enum a3xx_color_swap swap = WZYX;
64 bool srgb = false;
65 struct fd_resource *rsc = NULL;
66 struct fd_resource_slice *slice = NULL;
67 uint32_t stride = 0;
68 uint32_t base = 0;
69 uint32_t offset = 0;
70
71 if ((i < nr_bufs) && bufs[i]) {
72 struct pipe_surface *psurf = bufs[i];
73 enum pipe_format pformat = psurf->format;
74
75 rsc = fd_resource(psurf->texture);
76
77 /* In case we're drawing to Z32F_S8, the "color" actually goes to
78 * the stencil
79 */
80 if (rsc->stencil) {
81 rsc = rsc->stencil;
82 pformat = rsc->base.b.format;
83 if (bases)
84 bases++;
85 }
86
87 slice = fd_resource_slice(rsc, psurf->u.tex.level);
88 format = fd4_pipe2color(pformat);
89 swap = fd4_pipe2swap(pformat);
90
91 if (decode_srgb)
92 srgb = util_format_is_srgb(pformat);
93 else
94 pformat = util_format_linear(pformat);
95
96 debug_assert(psurf->u.tex.first_layer == psurf->u.tex.last_layer);
97
98 offset = fd_resource_offset(rsc, psurf->u.tex.level,
99 psurf->u.tex.first_layer);
100
101 if (bin_w) {
102 stride = bin_w * rsc->cpp;
103
104 if (bases) {
105 base = bases[i];
106 }
107 } else {
108 stride = slice->pitch * rsc->cpp;
109 }
110 } else if ((i < nr_bufs) && bases) {
111 base = bases[i];
112 }
113
114 OUT_PKT0(ring, REG_A4XX_RB_MRT_BUF_INFO(i), 3);
115 OUT_RING(ring, A4XX_RB_MRT_BUF_INFO_COLOR_FORMAT(format) |
116 A4XX_RB_MRT_BUF_INFO_COLOR_TILE_MODE(tile_mode) |
117 A4XX_RB_MRT_BUF_INFO_COLOR_BUF_PITCH(stride) |
118 A4XX_RB_MRT_BUF_INFO_COLOR_SWAP(swap) |
119 COND(srgb, A4XX_RB_MRT_BUF_INFO_COLOR_SRGB));
120 if (bin_w || (i >= nr_bufs) || !bufs[i]) {
121 OUT_RING(ring, base);
122 OUT_RING(ring, A4XX_RB_MRT_CONTROL3_STRIDE(stride));
123 } else {
124 OUT_RELOCW(ring, rsc->bo, offset, 0, 0);
125 /* RB_MRT[i].CONTROL3.STRIDE not emitted by c2d..
126 * not sure if we need to skip it for bypass or
127 * not.
128 */
129 OUT_RING(ring, A4XX_RB_MRT_CONTROL3_STRIDE(0));
130 }
131 }
132 }
133
134 static bool
135 use_hw_binning(struct fd_context *ctx)
136 {
137 struct fd_gmem_stateobj *gmem = &ctx->gmem;
138 struct pipe_framebuffer_state *pfb = &ctx->framebuffer;
139
140 /* this seems to be a hw bug.. but this hack fixes piglit fbo-maxsize: */
141 if ((pfb->width > 4096) && (pfb->height > 4096))
142 return false;
143
144 return fd_binning_enabled && ((gmem->nbins_x * gmem->nbins_y) > 2);
145 }
146
147 /* transfer from gmem to system memory (ie. normal RAM) */
148
149 static void
150 emit_gmem2mem_surf(struct fd_context *ctx, bool stencil,
151 uint32_t base, struct pipe_surface *psurf)
152 {
153 struct fd_ringbuffer *ring = ctx->ring;
154 struct fd_resource *rsc = fd_resource(psurf->texture);
155 enum pipe_format pformat = psurf->format;
156 struct fd_resource_slice *slice;
157 uint32_t offset;
158
159 if (stencil) {
160 debug_assert(rsc->stencil);
161 rsc = rsc->stencil;
162 pformat = rsc->base.b.format;
163 }
164
165 slice = &rsc->slices[psurf->u.tex.level];
166 offset = fd_resource_offset(rsc, psurf->u.tex.level,
167 psurf->u.tex.first_layer);
168
169 debug_assert(psurf->u.tex.first_layer == psurf->u.tex.last_layer);
170
171 OUT_PKT0(ring, REG_A4XX_RB_COPY_CONTROL, 4);
172 OUT_RING(ring, A4XX_RB_COPY_CONTROL_MSAA_RESOLVE(MSAA_ONE) |
173 A4XX_RB_COPY_CONTROL_MODE(RB_COPY_RESOLVE) |
174 A4XX_RB_COPY_CONTROL_GMEM_BASE(base));
175 OUT_RELOCW(ring, rsc->bo, offset, 0, 0); /* RB_COPY_DEST_BASE */
176 OUT_RING(ring, A4XX_RB_COPY_DEST_PITCH_PITCH(slice->pitch * rsc->cpp));
177 OUT_RING(ring, A4XX_RB_COPY_DEST_INFO_TILE(TILE4_LINEAR) |
178 A4XX_RB_COPY_DEST_INFO_FORMAT(fd4_pipe2color(pformat)) |
179 A4XX_RB_COPY_DEST_INFO_COMPONENT_ENABLE(0xf) |
180 A4XX_RB_COPY_DEST_INFO_ENDIAN(ENDIAN_NONE) |
181 A4XX_RB_COPY_DEST_INFO_SWAP(fd4_pipe2swap(pformat)));
182
183 fd4_draw(ctx, ring, DI_PT_RECTLIST, IGNORE_VISIBILITY,
184 DI_SRC_SEL_AUTO_INDEX, 2, 1, INDEX_SIZE_IGN, 0, 0, NULL);
185 }
186
187 static void
188 fd4_emit_tile_gmem2mem(struct fd_context *ctx, struct fd_tile *tile)
189 {
190 struct fd4_context *fd4_ctx = fd4_context(ctx);
191 struct fd_gmem_stateobj *gmem = &ctx->gmem;
192 struct fd_ringbuffer *ring = ctx->ring;
193 struct pipe_framebuffer_state *pfb = &ctx->framebuffer;
194 struct fd4_emit emit = {
195 .debug = &ctx->debug,
196 .vtx = &fd4_ctx->solid_vbuf_state,
197 .prog = &ctx->solid_prog,
198 .key = {
199 .half_precision = true,
200 },
201 };
202
203 OUT_PKT0(ring, REG_A4XX_RB_DEPTH_CONTROL, 1);
204 OUT_RING(ring, A4XX_RB_DEPTH_CONTROL_ZFUNC(FUNC_NEVER));
205
206 OUT_PKT0(ring, REG_A4XX_RB_STENCIL_CONTROL, 2);
207 OUT_RING(ring, A4XX_RB_STENCIL_CONTROL_FUNC(FUNC_NEVER) |
208 A4XX_RB_STENCIL_CONTROL_FAIL(STENCIL_KEEP) |
209 A4XX_RB_STENCIL_CONTROL_ZPASS(STENCIL_KEEP) |
210 A4XX_RB_STENCIL_CONTROL_ZFAIL(STENCIL_KEEP) |
211 A4XX_RB_STENCIL_CONTROL_FUNC_BF(FUNC_NEVER) |
212 A4XX_RB_STENCIL_CONTROL_FAIL_BF(STENCIL_KEEP) |
213 A4XX_RB_STENCIL_CONTROL_ZPASS_BF(STENCIL_KEEP) |
214 A4XX_RB_STENCIL_CONTROL_ZFAIL_BF(STENCIL_KEEP));
215 OUT_RING(ring, 0x00000000); /* RB_STENCIL_CONTROL2 */
216
217 OUT_PKT0(ring, REG_A4XX_RB_STENCILREFMASK, 2);
218 OUT_RING(ring, 0xff000000 |
219 A4XX_RB_STENCILREFMASK_STENCILREF(0) |
220 A4XX_RB_STENCILREFMASK_STENCILMASK(0) |
221 A4XX_RB_STENCILREFMASK_STENCILWRITEMASK(0xff));
222 OUT_RING(ring, 0xff000000 |
223 A4XX_RB_STENCILREFMASK_BF_STENCILREF(0) |
224 A4XX_RB_STENCILREFMASK_BF_STENCILMASK(0) |
225 A4XX_RB_STENCILREFMASK_BF_STENCILWRITEMASK(0xff));
226
227 OUT_PKT0(ring, REG_A4XX_GRAS_SU_MODE_CONTROL, 1);
228 OUT_RING(ring, A4XX_GRAS_SU_MODE_CONTROL_LINEHALFWIDTH(0));
229
230 fd_wfi(ctx, ring);
231
232 OUT_PKT0(ring, REG_A4XX_GRAS_CL_CLIP_CNTL, 1);
233 OUT_RING(ring, 0x80000); /* GRAS_CL_CLIP_CNTL */
234
235 OUT_PKT0(ring, REG_A4XX_GRAS_CL_VPORT_XOFFSET_0, 6);
236 OUT_RING(ring, A4XX_GRAS_CL_VPORT_XOFFSET_0((float)pfb->width/2.0));
237 OUT_RING(ring, A4XX_GRAS_CL_VPORT_XSCALE_0((float)pfb->width/2.0));
238 OUT_RING(ring, A4XX_GRAS_CL_VPORT_YOFFSET_0((float)pfb->height/2.0));
239 OUT_RING(ring, A4XX_GRAS_CL_VPORT_YSCALE_0(-(float)pfb->height/2.0));
240 OUT_RING(ring, A4XX_GRAS_CL_VPORT_ZOFFSET_0(0.0));
241 OUT_RING(ring, A4XX_GRAS_CL_VPORT_ZSCALE_0(1.0));
242
243 OUT_PKT0(ring, REG_A4XX_RB_RENDER_CONTROL, 1);
244 OUT_RING(ring, A4XX_RB_RENDER_CONTROL_DISABLE_COLOR_PIPE |
245 0xa); /* XXX */
246
247 OUT_PKT0(ring, REG_A4XX_GRAS_SC_CONTROL, 1);
248 OUT_RING(ring, A4XX_GRAS_SC_CONTROL_RENDER_MODE(RB_RESOLVE_PASS) |
249 A4XX_GRAS_SC_CONTROL_MSAA_DISABLE |
250 A4XX_GRAS_SC_CONTROL_MSAA_SAMPLES(MSAA_ONE) |
251 A4XX_GRAS_SC_CONTROL_RASTER_MODE(1));
252
253 OUT_PKT0(ring, REG_A4XX_PC_PRIM_VTX_CNTL, 1);
254 OUT_RING(ring, A4XX_PC_PRIM_VTX_CNTL_PROVOKING_VTX_LAST);
255
256 OUT_PKT0(ring, REG_A4XX_GRAS_ALPHA_CONTROL, 1);
257 OUT_RING(ring, 0x00000002);
258
259 OUT_PKT0(ring, REG_A4XX_GRAS_SC_WINDOW_SCISSOR_BR, 2);
260 OUT_RING(ring, A4XX_GRAS_SC_WINDOW_SCISSOR_BR_X(pfb->width - 1) |
261 A4XX_GRAS_SC_WINDOW_SCISSOR_BR_Y(pfb->height - 1));
262 OUT_RING(ring, A4XX_GRAS_SC_WINDOW_SCISSOR_TL_X(0) |
263 A4XX_GRAS_SC_WINDOW_SCISSOR_TL_Y(0));
264
265 OUT_PKT0(ring, REG_A4XX_VFD_INDEX_OFFSET, 2);
266 OUT_RING(ring, 0); /* VFD_INDEX_OFFSET */
267 OUT_RING(ring, 0); /* ??? UNKNOWN_2209 */
268
269 fd4_program_emit(ring, &emit, 0, NULL);
270 fd4_emit_vertex_bufs(ring, &emit);
271
272 if (ctx->resolve & (FD_BUFFER_DEPTH | FD_BUFFER_STENCIL)) {
273 struct fd_resource *rsc = fd_resource(pfb->zsbuf->texture);
274 if (!rsc->stencil || (ctx->resolve & FD_BUFFER_DEPTH))
275 emit_gmem2mem_surf(ctx, false, ctx->gmem.zsbuf_base[0], pfb->zsbuf);
276 if (rsc->stencil && (ctx->resolve & FD_BUFFER_STENCIL))
277 emit_gmem2mem_surf(ctx, true, ctx->gmem.zsbuf_base[1], pfb->zsbuf);
278 }
279
280 if (ctx->resolve & FD_BUFFER_COLOR) {
281 unsigned i;
282 for (i = 0; i < pfb->nr_cbufs; i++) {
283 if (!pfb->cbufs[i])
284 continue;
285 if (!(ctx->resolve & (PIPE_CLEAR_COLOR0 << i)))
286 continue;
287 emit_gmem2mem_surf(ctx, false, gmem->cbuf_base[i], pfb->cbufs[i]);
288 }
289 }
290
291 OUT_PKT0(ring, REG_A4XX_GRAS_SC_CONTROL, 1);
292 OUT_RING(ring, A4XX_GRAS_SC_CONTROL_RENDER_MODE(RB_RENDERING_PASS) |
293 A4XX_GRAS_SC_CONTROL_MSAA_DISABLE |
294 A4XX_GRAS_SC_CONTROL_MSAA_SAMPLES(MSAA_ONE) |
295 A4XX_GRAS_SC_CONTROL_RASTER_MODE(0));
296 }
297
298 /* transfer from system memory to gmem */
299
300 static void
301 emit_mem2gmem_surf(struct fd_context *ctx, uint32_t *bases,
302 struct pipe_surface **bufs, uint32_t nr_bufs, uint32_t bin_w)
303 {
304 struct fd_ringbuffer *ring = ctx->ring;
305 struct pipe_surface *zsbufs[2];
306
307 emit_mrt(ring, nr_bufs, bufs, bases, bin_w, false);
308
309 if (bufs[0] && (bufs[0]->format == PIPE_FORMAT_Z32_FLOAT_S8X24_UINT)) {
310 /* The gmem_restore_tex logic will put the first buffer's stencil
311 * as color. Supply it with the proper information to make that
312 * happen.
313 */
314 zsbufs[0] = zsbufs[1] = bufs[0];
315 bufs = zsbufs;
316 nr_bufs = 2;
317 }
318
319 fd4_emit_gmem_restore_tex(ring, nr_bufs, bufs);
320
321 fd4_draw(ctx, ring, DI_PT_RECTLIST, IGNORE_VISIBILITY,
322 DI_SRC_SEL_AUTO_INDEX, 2, 1, INDEX_SIZE_IGN, 0, 0, NULL);
323 }
324
325 static void
326 fd4_emit_tile_mem2gmem(struct fd_context *ctx, struct fd_tile *tile)
327 {
328 struct fd4_context *fd4_ctx = fd4_context(ctx);
329 struct fd_gmem_stateobj *gmem = &ctx->gmem;
330 struct fd_ringbuffer *ring = ctx->ring;
331 struct pipe_framebuffer_state *pfb = &ctx->framebuffer;
332 struct fd4_emit emit = {
333 .debug = &ctx->debug,
334 .vtx = &fd4_ctx->blit_vbuf_state,
335 .sprite_coord_enable = 1,
336 /* NOTE: They all use the same VP, this is for vtx bufs. */
337 .prog = &ctx->blit_prog[0],
338 .key = {
339 .half_precision = fd_half_precision(pfb),
340 },
341 .no_decode_srgb = true,
342 };
343 unsigned char mrt_comp[A4XX_MAX_RENDER_TARGETS] = {0};
344 float x0, y0, x1, y1;
345 unsigned bin_w = tile->bin_w;
346 unsigned bin_h = tile->bin_h;
347 unsigned i;
348
349 /* write texture coordinates to vertexbuf: */
350 x0 = ((float)tile->xoff) / ((float)pfb->width);
351 x1 = ((float)tile->xoff + bin_w) / ((float)pfb->width);
352 y0 = ((float)tile->yoff) / ((float)pfb->height);
353 y1 = ((float)tile->yoff + bin_h) / ((float)pfb->height);
354
355 OUT_PKT3(ring, CP_MEM_WRITE, 5);
356 OUT_RELOCW(ring, fd_resource(fd4_ctx->blit_texcoord_vbuf)->bo, 0, 0, 0);
357 OUT_RING(ring, fui(x0));
358 OUT_RING(ring, fui(y0));
359 OUT_RING(ring, fui(x1));
360 OUT_RING(ring, fui(y1));
361
362 for (i = 0; i < A4XX_MAX_RENDER_TARGETS; i++) {
363 mrt_comp[i] = ((i < pfb->nr_cbufs) && pfb->cbufs[i]) ? 0xf : 0;
364
365 OUT_PKT0(ring, REG_A4XX_RB_MRT_CONTROL(i), 1);
366 OUT_RING(ring, A4XX_RB_MRT_CONTROL_ROP_CODE(ROP_COPY) |
367 A4XX_RB_MRT_CONTROL_COMPONENT_ENABLE(0xf));
368
369 OUT_PKT0(ring, REG_A4XX_RB_MRT_BLEND_CONTROL(i), 1);
370 OUT_RING(ring, A4XX_RB_MRT_BLEND_CONTROL_RGB_SRC_FACTOR(FACTOR_ONE) |
371 A4XX_RB_MRT_BLEND_CONTROL_RGB_BLEND_OPCODE(BLEND_DST_PLUS_SRC) |
372 A4XX_RB_MRT_BLEND_CONTROL_RGB_DEST_FACTOR(FACTOR_ZERO) |
373 A4XX_RB_MRT_BLEND_CONTROL_ALPHA_SRC_FACTOR(FACTOR_ONE) |
374 A4XX_RB_MRT_BLEND_CONTROL_ALPHA_BLEND_OPCODE(BLEND_DST_PLUS_SRC) |
375 A4XX_RB_MRT_BLEND_CONTROL_ALPHA_DEST_FACTOR(FACTOR_ZERO));
376 }
377
378 OUT_PKT0(ring, REG_A4XX_RB_RENDER_COMPONENTS, 1);
379 OUT_RING(ring, A4XX_RB_RENDER_COMPONENTS_RT0(mrt_comp[0]) |
380 A4XX_RB_RENDER_COMPONENTS_RT1(mrt_comp[1]) |
381 A4XX_RB_RENDER_COMPONENTS_RT2(mrt_comp[2]) |
382 A4XX_RB_RENDER_COMPONENTS_RT3(mrt_comp[3]) |
383 A4XX_RB_RENDER_COMPONENTS_RT4(mrt_comp[4]) |
384 A4XX_RB_RENDER_COMPONENTS_RT5(mrt_comp[5]) |
385 A4XX_RB_RENDER_COMPONENTS_RT6(mrt_comp[6]) |
386 A4XX_RB_RENDER_COMPONENTS_RT7(mrt_comp[7]));
387
388 OUT_PKT0(ring, REG_A4XX_RB_RENDER_CONTROL, 1);
389 OUT_RING(ring, 0x8); /* XXX RB_RENDER_CONTROL */
390
391 OUT_PKT0(ring, REG_A4XX_RB_DEPTH_CONTROL, 1);
392 OUT_RING(ring, A4XX_RB_DEPTH_CONTROL_ZFUNC(FUNC_LESS));
393
394 OUT_PKT0(ring, REG_A4XX_GRAS_CL_CLIP_CNTL, 1);
395 OUT_RING(ring, 0x280000); /* XXX GRAS_CL_CLIP_CNTL */
396
397 OUT_PKT0(ring, REG_A4XX_GRAS_SU_MODE_CONTROL, 1);
398 OUT_RING(ring, A4XX_GRAS_SU_MODE_CONTROL_LINEHALFWIDTH(0) |
399 A4XX_GRAS_SU_MODE_CONTROL_RENDERING_PASS);
400
401 OUT_PKT0(ring, REG_A4XX_GRAS_CL_VPORT_XOFFSET_0, 6);
402 OUT_RING(ring, A4XX_GRAS_CL_VPORT_XOFFSET_0((float)bin_w/2.0));
403 OUT_RING(ring, A4XX_GRAS_CL_VPORT_XSCALE_0((float)bin_w/2.0));
404 OUT_RING(ring, A4XX_GRAS_CL_VPORT_YOFFSET_0((float)bin_h/2.0));
405 OUT_RING(ring, A4XX_GRAS_CL_VPORT_YSCALE_0(-(float)bin_h/2.0));
406 OUT_RING(ring, A4XX_GRAS_CL_VPORT_ZOFFSET_0(0.0));
407 OUT_RING(ring, A4XX_GRAS_CL_VPORT_ZSCALE_0(1.0));
408
409 OUT_PKT0(ring, REG_A4XX_GRAS_SC_WINDOW_SCISSOR_BR, 2);
410 OUT_RING(ring, A4XX_GRAS_SC_WINDOW_SCISSOR_BR_X(bin_w - 1) |
411 A4XX_GRAS_SC_WINDOW_SCISSOR_BR_Y(bin_h - 1));
412 OUT_RING(ring, A4XX_GRAS_SC_WINDOW_SCISSOR_TL_X(0) |
413 A4XX_GRAS_SC_WINDOW_SCISSOR_TL_Y(0));
414
415 OUT_PKT0(ring, REG_A4XX_GRAS_SC_SCREEN_SCISSOR_TL, 2);
416 OUT_RING(ring, A4XX_GRAS_SC_SCREEN_SCISSOR_TL_X(0) |
417 A4XX_GRAS_SC_SCREEN_SCISSOR_TL_Y(0));
418 OUT_RING(ring, A4XX_GRAS_SC_SCREEN_SCISSOR_BR_X(bin_w - 1) |
419 A4XX_GRAS_SC_SCREEN_SCISSOR_BR_Y(bin_h - 1));
420
421 OUT_PKT0(ring, REG_A4XX_RB_MODE_CONTROL, 1);
422 OUT_RING(ring, A4XX_RB_MODE_CONTROL_WIDTH(gmem->bin_w) |
423 A4XX_RB_MODE_CONTROL_HEIGHT(gmem->bin_h));
424
425 OUT_PKT0(ring, REG_A4XX_RB_STENCIL_CONTROL, 2);
426 OUT_RING(ring, A4XX_RB_STENCIL_CONTROL_FUNC(FUNC_ALWAYS) |
427 A4XX_RB_STENCIL_CONTROL_FAIL(STENCIL_KEEP) |
428 A4XX_RB_STENCIL_CONTROL_ZPASS(STENCIL_KEEP) |
429 A4XX_RB_STENCIL_CONTROL_ZFAIL(STENCIL_KEEP) |
430 A4XX_RB_STENCIL_CONTROL_FUNC_BF(FUNC_ALWAYS) |
431 A4XX_RB_STENCIL_CONTROL_FAIL_BF(STENCIL_KEEP) |
432 A4XX_RB_STENCIL_CONTROL_ZPASS_BF(STENCIL_KEEP) |
433 A4XX_RB_STENCIL_CONTROL_ZFAIL_BF(STENCIL_KEEP));
434 OUT_RING(ring, 0x00000000); /* RB_STENCIL_CONTROL2 */
435
436 OUT_PKT0(ring, REG_A4XX_GRAS_SC_CONTROL, 1);
437 OUT_RING(ring, A4XX_GRAS_SC_CONTROL_RENDER_MODE(RB_RENDERING_PASS) |
438 A4XX_GRAS_SC_CONTROL_MSAA_DISABLE |
439 A4XX_GRAS_SC_CONTROL_MSAA_SAMPLES(MSAA_ONE) |
440 A4XX_GRAS_SC_CONTROL_RASTER_MODE(1));
441
442 OUT_PKT0(ring, REG_A4XX_PC_PRIM_VTX_CNTL, 1);
443 OUT_RING(ring, A4XX_PC_PRIM_VTX_CNTL_PROVOKING_VTX_LAST |
444 A4XX_PC_PRIM_VTX_CNTL_VAROUT(1));
445
446 OUT_PKT0(ring, REG_A4XX_VFD_INDEX_OFFSET, 2);
447 OUT_RING(ring, 0); /* VFD_INDEX_OFFSET */
448 OUT_RING(ring, 0); /* ??? UNKNOWN_2209 */
449
450 fd4_emit_vertex_bufs(ring, &emit);
451
452 /* for gmem pitch/base calculations, we need to use the non-
453 * truncated tile sizes:
454 */
455 bin_w = gmem->bin_w;
456 bin_h = gmem->bin_h;
457
458 if (fd_gmem_needs_restore(ctx, tile, FD_BUFFER_COLOR)) {
459 emit.prog = &ctx->blit_prog[pfb->nr_cbufs - 1];
460 emit.fp = NULL; /* frag shader changed so clear cache */
461 fd4_program_emit(ring, &emit, pfb->nr_cbufs, pfb->cbufs);
462 emit_mem2gmem_surf(ctx, gmem->cbuf_base, pfb->cbufs, pfb->nr_cbufs, bin_w);
463 }
464
465 if (fd_gmem_needs_restore(ctx, tile, FD_BUFFER_DEPTH | FD_BUFFER_STENCIL)) {
466 switch (pfb->zsbuf->format) {
467 case PIPE_FORMAT_Z32_FLOAT_S8X24_UINT:
468 case PIPE_FORMAT_Z32_FLOAT:
469 emit.prog = (pfb->zsbuf->format == PIPE_FORMAT_Z32_FLOAT) ?
470 &ctx->blit_z : &ctx->blit_zs;
471 emit.key.half_precision = false;
472
473 OUT_PKT0(ring, REG_A4XX_RB_DEPTH_CONTROL, 1);
474 OUT_RING(ring, A4XX_RB_DEPTH_CONTROL_Z_ENABLE |
475 A4XX_RB_DEPTH_CONTROL_Z_WRITE_ENABLE |
476 A4XX_RB_DEPTH_CONTROL_ZFUNC(FUNC_ALWAYS) |
477 A4XX_RB_DEPTH_CONTROL_EARLY_Z_DISABLE);
478
479 OUT_PKT0(ring, REG_A4XX_GRAS_ALPHA_CONTROL, 1);
480 OUT_RING(ring, A4XX_GRAS_ALPHA_CONTROL_ALPHA_TEST_ENABLE);
481
482 OUT_PKT0(ring, REG_A4XX_GRAS_CL_CLIP_CNTL, 1);
483 OUT_RING(ring, 0x80000); /* GRAS_CL_CLIP_CNTL */
484
485 break;
486 default:
487 /* Non-float can use a regular color write. It's split over 8-bit
488 * components, so half precision is always sufficient.
489 */
490 emit.prog = &ctx->blit_prog[0];
491 emit.key.half_precision = true;
492 break;
493 }
494 emit.fp = NULL; /* frag shader changed so clear cache */
495 fd4_program_emit(ring, &emit, 1, &pfb->zsbuf);
496 emit_mem2gmem_surf(ctx, gmem->zsbuf_base, &pfb->zsbuf, 1, bin_w);
497 }
498
499 OUT_PKT0(ring, REG_A4XX_GRAS_SC_CONTROL, 1);
500 OUT_RING(ring, A4XX_GRAS_SC_CONTROL_RENDER_MODE(RB_RENDERING_PASS) |
501 A4XX_GRAS_SC_CONTROL_MSAA_SAMPLES(MSAA_ONE) |
502 A4XX_GRAS_SC_CONTROL_RASTER_MODE(0));
503
504 OUT_PKT0(ring, REG_A4XX_RB_MODE_CONTROL, 1);
505 OUT_RING(ring, A4XX_RB_MODE_CONTROL_WIDTH(gmem->bin_w) |
506 A4XX_RB_MODE_CONTROL_HEIGHT(gmem->bin_h) |
507 0x00010000); /* XXX */
508 }
509
510 static void
511 patch_draws(struct fd_context *ctx, enum pc_di_vis_cull_mode vismode)
512 {
513 unsigned i;
514 for (i = 0; i < fd_patch_num_elements(&ctx->draw_patches); i++) {
515 struct fd_cs_patch *patch = fd_patch_element(&ctx->draw_patches, i);
516 *patch->cs = patch->val | DRAW4(0, 0, 0, vismode);
517 }
518 util_dynarray_resize(&ctx->draw_patches, 0);
519 }
520
521 /* for rendering directly to system memory: */
522 static void
523 fd4_emit_sysmem_prep(struct fd_context *ctx)
524 {
525 struct pipe_framebuffer_state *pfb = &ctx->framebuffer;
526 struct fd_ringbuffer *ring = ctx->ring;
527
528 fd4_emit_restore(ctx);
529
530 OUT_PKT0(ring, REG_A4XX_RB_FRAME_BUFFER_DIMENSION, 1);
531 OUT_RING(ring, A4XX_RB_FRAME_BUFFER_DIMENSION_WIDTH(pfb->width) |
532 A4XX_RB_FRAME_BUFFER_DIMENSION_HEIGHT(pfb->height));
533
534 emit_mrt(ring, pfb->nr_cbufs, pfb->cbufs, NULL, 0, true);
535
536 /* setup scissor/offset for current tile: */
537 OUT_PKT0(ring, REG_A4XX_RB_BIN_OFFSET, 1);
538 OUT_RING(ring, A4XX_RB_BIN_OFFSET_X(0) |
539 A4XX_RB_BIN_OFFSET_Y(0));
540
541 OUT_PKT0(ring, REG_A4XX_GRAS_SC_SCREEN_SCISSOR_TL, 2);
542 OUT_RING(ring, A4XX_GRAS_SC_SCREEN_SCISSOR_TL_X(0) |
543 A4XX_GRAS_SC_SCREEN_SCISSOR_TL_Y(0));
544 OUT_RING(ring, A4XX_GRAS_SC_SCREEN_SCISSOR_BR_X(pfb->width - 1) |
545 A4XX_GRAS_SC_SCREEN_SCISSOR_BR_Y(pfb->height - 1));
546
547 OUT_PKT0(ring, REG_A4XX_RB_MODE_CONTROL, 1);
548 OUT_RING(ring, A4XX_RB_MODE_CONTROL_WIDTH(0) |
549 A4XX_RB_MODE_CONTROL_HEIGHT(0) |
550 0x00c00000); /* XXX */
551
552 OUT_PKT0(ring, REG_A4XX_RB_RENDER_CONTROL, 1);
553 OUT_RING(ring, 0x8);
554
555 patch_draws(ctx, IGNORE_VISIBILITY);
556 }
557
558 static void
559 update_vsc_pipe(struct fd_context *ctx)
560 {
561 struct fd4_context *fd4_ctx = fd4_context(ctx);
562 struct fd_ringbuffer *ring = ctx->ring;
563 int i;
564
565 OUT_PKT0(ring, REG_A4XX_VSC_SIZE_ADDRESS, 1);
566 OUT_RELOCW(ring, fd4_ctx->vsc_size_mem, 0, 0, 0); /* VSC_SIZE_ADDRESS */
567
568 OUT_PKT0(ring, REG_A4XX_VSC_PIPE_CONFIG_REG(0), 8);
569 for (i = 0; i < 8; i++) {
570 struct fd_vsc_pipe *pipe = &ctx->pipe[i];
571 OUT_RING(ring, A4XX_VSC_PIPE_CONFIG_REG_X(pipe->x) |
572 A4XX_VSC_PIPE_CONFIG_REG_Y(pipe->y) |
573 A4XX_VSC_PIPE_CONFIG_REG_W(pipe->w) |
574 A4XX_VSC_PIPE_CONFIG_REG_H(pipe->h));
575 }
576
577 OUT_PKT0(ring, REG_A4XX_VSC_PIPE_DATA_ADDRESS_REG(0), 8);
578 for (i = 0; i < 8; i++) {
579 struct fd_vsc_pipe *pipe = &ctx->pipe[i];
580 if (!pipe->bo) {
581 pipe->bo = fd_bo_new(ctx->dev, 0x40000,
582 DRM_FREEDRENO_GEM_TYPE_KMEM);
583 }
584 OUT_RELOCW(ring, pipe->bo, 0, 0, 0); /* VSC_PIPE_DATA_ADDRESS[i] */
585 }
586
587 OUT_PKT0(ring, REG_A4XX_VSC_PIPE_DATA_LENGTH_REG(0), 8);
588 for (i = 0; i < 8; i++) {
589 struct fd_vsc_pipe *pipe = &ctx->pipe[i];
590 OUT_RING(ring, fd_bo_size(pipe->bo) - 32); /* VSC_PIPE_DATA_LENGTH[i] */
591 }
592 }
593
594 static void
595 emit_binning_pass(struct fd_context *ctx)
596 {
597 struct fd_gmem_stateobj *gmem = &ctx->gmem;
598 struct pipe_framebuffer_state *pfb = &ctx->framebuffer;
599 struct fd_ringbuffer *ring = ctx->ring;
600 int i;
601
602 uint32_t x1 = gmem->minx;
603 uint32_t y1 = gmem->miny;
604 uint32_t x2 = gmem->minx + gmem->width - 1;
605 uint32_t y2 = gmem->miny + gmem->height - 1;
606
607 OUT_PKT0(ring, REG_A4XX_PC_BINNING_COMMAND, 1);
608 OUT_RING(ring, A4XX_PC_BINNING_COMMAND_BINNING_ENABLE);
609
610 OUT_PKT0(ring, REG_A4XX_GRAS_SC_CONTROL, 1);
611 OUT_RING(ring, A4XX_GRAS_SC_CONTROL_RENDER_MODE(RB_TILING_PASS) |
612 A4XX_GRAS_SC_CONTROL_MSAA_DISABLE |
613 A4XX_GRAS_SC_CONTROL_MSAA_SAMPLES(MSAA_ONE) |
614 A4XX_GRAS_SC_CONTROL_RASTER_MODE(0));
615
616 OUT_PKT0(ring, REG_A4XX_RB_FRAME_BUFFER_DIMENSION, 1);
617 OUT_RING(ring, A4XX_RB_FRAME_BUFFER_DIMENSION_WIDTH(pfb->width) |
618 A4XX_RB_FRAME_BUFFER_DIMENSION_HEIGHT(pfb->height));
619
620 /* setup scissor/offset for whole screen: */
621 OUT_PKT0(ring, REG_A4XX_RB_BIN_OFFSET, 1);
622 OUT_RING(ring, A4XX_RB_BIN_OFFSET_X(x1) |
623 A4XX_RB_BIN_OFFSET_Y(y1));
624
625 OUT_PKT0(ring, REG_A4XX_GRAS_SC_SCREEN_SCISSOR_TL, 2);
626 OUT_RING(ring, A4XX_GRAS_SC_SCREEN_SCISSOR_TL_X(x1) |
627 A4XX_GRAS_SC_SCREEN_SCISSOR_TL_Y(y1));
628 OUT_RING(ring, A4XX_GRAS_SC_SCREEN_SCISSOR_BR_X(x2) |
629 A4XX_GRAS_SC_SCREEN_SCISSOR_BR_Y(y2));
630
631 for (i = 0; i < A4XX_MAX_RENDER_TARGETS; i++) {
632 OUT_PKT0(ring, REG_A4XX_RB_MRT_CONTROL(i), 1);
633 OUT_RING(ring, A4XX_RB_MRT_CONTROL_ROP_CODE(ROP_CLEAR) |
634 A4XX_RB_MRT_CONTROL_COMPONENT_ENABLE(0xf));
635 }
636
637 /* emit IB to binning drawcmds: */
638 ctx->emit_ib(ring, ctx->binning_start, ctx->binning_end);
639
640 fd_reset_wfi(ctx);
641 fd_wfi(ctx, ring);
642
643 /* and then put stuff back the way it was: */
644
645 OUT_PKT0(ring, REG_A4XX_PC_BINNING_COMMAND, 1);
646 OUT_RING(ring, 0x00000000);
647
648 OUT_PKT0(ring, REG_A4XX_GRAS_SC_CONTROL, 1);
649 OUT_RING(ring, A4XX_GRAS_SC_CONTROL_RENDER_MODE(RB_RENDERING_PASS) |
650 A4XX_GRAS_SC_CONTROL_MSAA_DISABLE |
651 A4XX_GRAS_SC_CONTROL_MSAA_SAMPLES(MSAA_ONE) |
652 A4XX_GRAS_SC_CONTROL_RASTER_MODE(0));
653
654 fd_event_write(ctx, ring, CACHE_FLUSH);
655 fd_wfi(ctx, ring);
656 }
657
658 /* before first tile */
659 static void
660 fd4_emit_tile_init(struct fd_context *ctx)
661 {
662 struct fd_ringbuffer *ring = ctx->ring;
663 struct fd_gmem_stateobj *gmem = &ctx->gmem;
664
665 fd4_emit_restore(ctx);
666
667 OUT_PKT0(ring, REG_A4XX_VSC_BIN_SIZE, 1);
668 OUT_RING(ring, A4XX_VSC_BIN_SIZE_WIDTH(gmem->bin_w) |
669 A4XX_VSC_BIN_SIZE_HEIGHT(gmem->bin_h));
670
671 update_vsc_pipe(ctx);
672
673 if (use_hw_binning(ctx)) {
674 OUT_PKT0(ring, REG_A4XX_RB_MODE_CONTROL, 1);
675 OUT_RING(ring, A4XX_RB_MODE_CONTROL_WIDTH(gmem->bin_w) |
676 A4XX_RB_MODE_CONTROL_HEIGHT(gmem->bin_h));
677
678 OUT_PKT0(ring, REG_A4XX_RB_RENDER_CONTROL, 1);
679 OUT_RING(ring, A4XX_RB_RENDER_CONTROL_BINNING_PASS |
680 A4XX_RB_RENDER_CONTROL_DISABLE_COLOR_PIPE |
681 0x8);
682
683 /* emit hw binning pass: */
684 emit_binning_pass(ctx);
685
686 patch_draws(ctx, USE_VISIBILITY);
687 } else {
688 patch_draws(ctx, IGNORE_VISIBILITY);
689 }
690
691 OUT_PKT0(ring, REG_A4XX_RB_MODE_CONTROL, 1);
692 OUT_RING(ring, A4XX_RB_MODE_CONTROL_WIDTH(gmem->bin_w) |
693 A4XX_RB_MODE_CONTROL_HEIGHT(gmem->bin_h) |
694 A4XX_RB_MODE_CONTROL_ENABLE_GMEM);
695 }
696
697 /* before mem2gmem */
698 static void
699 fd4_emit_tile_prep(struct fd_context *ctx, struct fd_tile *tile)
700 {
701 struct fd_ringbuffer *ring = ctx->ring;
702 struct pipe_framebuffer_state *pfb = &ctx->framebuffer;
703 struct fd_gmem_stateobj *gmem = &ctx->gmem;
704
705 if (pfb->zsbuf) {
706 struct fd_resource *rsc = fd_resource(pfb->zsbuf->texture);
707 uint32_t cpp = rsc->cpp;
708
709 OUT_PKT0(ring, REG_A4XX_RB_DEPTH_INFO, 3);
710 OUT_RING(ring, A4XX_RB_DEPTH_INFO_DEPTH_BASE(gmem->zsbuf_base[0]) |
711 A4XX_RB_DEPTH_INFO_DEPTH_FORMAT(fd4_pipe2depth(pfb->zsbuf->format)));
712 OUT_RING(ring, A4XX_RB_DEPTH_PITCH(cpp * gmem->bin_w));
713 OUT_RING(ring, A4XX_RB_DEPTH_PITCH2(cpp * gmem->bin_w));
714
715 OUT_PKT0(ring, REG_A4XX_RB_STENCIL_INFO, 2);
716 if (rsc->stencil) {
717 OUT_RING(ring, A4XX_RB_STENCIL_INFO_SEPARATE_STENCIL |
718 A4XX_RB_STENCIL_INFO_STENCIL_BASE(gmem->zsbuf_base[1]));
719 OUT_RING(ring, A4XX_RB_STENCIL_PITCH(rsc->stencil->cpp * gmem->bin_w));
720 } else {
721 OUT_RING(ring, 0x00000000);
722 OUT_RING(ring, 0x00000000);
723 }
724 } else {
725 OUT_PKT0(ring, REG_A4XX_RB_DEPTH_INFO, 3);
726 OUT_RING(ring, 0x00000000);
727 OUT_RING(ring, 0x00000000);
728 OUT_RING(ring, 0x00000000);
729
730 OUT_PKT0(ring, REG_A4XX_RB_STENCIL_INFO, 2);
731 OUT_RING(ring, 0); /* RB_STENCIL_INFO */
732 OUT_RING(ring, 0); /* RB_STENCIL_PITCH */
733 }
734
735 OUT_PKT0(ring, REG_A4XX_GRAS_DEPTH_CONTROL, 1);
736 if (pfb->zsbuf) {
737 OUT_RING(ring, A4XX_GRAS_DEPTH_CONTROL_FORMAT(
738 fd4_pipe2depth(pfb->zsbuf->format)));
739 } else {
740 OUT_RING(ring, A4XX_GRAS_DEPTH_CONTROL_FORMAT(DEPTH4_NONE));
741 }
742
743 if (ctx->needs_rb_fbd) {
744 fd_wfi(ctx, ring);
745 OUT_PKT0(ring, REG_A4XX_RB_FRAME_BUFFER_DIMENSION, 1);
746 OUT_RING(ring, A4XX_RB_FRAME_BUFFER_DIMENSION_WIDTH(pfb->width) |
747 A4XX_RB_FRAME_BUFFER_DIMENSION_HEIGHT(pfb->height));
748 ctx->needs_rb_fbd = false;
749 }
750 }
751
752 /* before IB to rendering cmds: */
753 static void
754 fd4_emit_tile_renderprep(struct fd_context *ctx, struct fd_tile *tile)
755 {
756 struct fd4_context *fd4_ctx = fd4_context(ctx);
757 struct fd_ringbuffer *ring = ctx->ring;
758 struct fd_gmem_stateobj *gmem = &ctx->gmem;
759 struct pipe_framebuffer_state *pfb = &ctx->framebuffer;
760
761 uint32_t x1 = tile->xoff;
762 uint32_t y1 = tile->yoff;
763 uint32_t x2 = tile->xoff + tile->bin_w - 1;
764 uint32_t y2 = tile->yoff + tile->bin_h - 1;
765
766 if (use_hw_binning(ctx)) {
767 struct fd_vsc_pipe *pipe = &ctx->pipe[tile->p];
768
769 assert(pipe->w * pipe->h);
770
771 fd_event_write(ctx, ring, HLSQ_FLUSH);
772 fd_wfi(ctx, ring);
773
774 OUT_PKT0(ring, REG_A4XX_PC_VSTREAM_CONTROL, 1);
775 OUT_RING(ring, A4XX_PC_VSTREAM_CONTROL_SIZE(pipe->w * pipe->h) |
776 A4XX_PC_VSTREAM_CONTROL_N(tile->n));
777
778 OUT_PKT3(ring, CP_SET_BIN_DATA, 2);
779 OUT_RELOCW(ring, pipe->bo, 0, 0, 0); /* BIN_DATA_ADDR <- VSC_PIPE[p].DATA_ADDRESS */
780 OUT_RELOCW(ring, fd4_ctx->vsc_size_mem, /* BIN_SIZE_ADDR <- VSC_SIZE_ADDRESS + (p * 4) */
781 (tile->p * 4), 0, 0);
782 } else {
783 OUT_PKT0(ring, REG_A4XX_PC_VSTREAM_CONTROL, 1);
784 OUT_RING(ring, 0x00000000);
785 }
786
787 OUT_PKT3(ring, CP_SET_BIN, 3);
788 OUT_RING(ring, 0x00000000);
789 OUT_RING(ring, CP_SET_BIN_1_X1(x1) | CP_SET_BIN_1_Y1(y1));
790 OUT_RING(ring, CP_SET_BIN_2_X2(x2) | CP_SET_BIN_2_Y2(y2));
791
792 emit_mrt(ring, pfb->nr_cbufs, pfb->cbufs, gmem->cbuf_base, gmem->bin_w, true);
793
794 /* setup scissor/offset for current tile: */
795 OUT_PKT0(ring, REG_A4XX_RB_BIN_OFFSET, 1);
796 OUT_RING(ring, A4XX_RB_BIN_OFFSET_X(tile->xoff) |
797 A4XX_RB_BIN_OFFSET_Y(tile->yoff));
798
799 OUT_PKT0(ring, REG_A4XX_GRAS_SC_SCREEN_SCISSOR_TL, 2);
800 OUT_RING(ring, A4XX_GRAS_SC_SCREEN_SCISSOR_TL_X(x1) |
801 A4XX_GRAS_SC_SCREEN_SCISSOR_TL_Y(y1));
802 OUT_RING(ring, A4XX_GRAS_SC_SCREEN_SCISSOR_BR_X(x2) |
803 A4XX_GRAS_SC_SCREEN_SCISSOR_BR_Y(y2));
804
805 OUT_PKT0(ring, REG_A4XX_RB_RENDER_CONTROL, 1);
806 OUT_RING(ring, 0x8);
807 }
808
809 void
810 fd4_gmem_init(struct pipe_context *pctx)
811 {
812 struct fd_context *ctx = fd_context(pctx);
813
814 ctx->emit_sysmem_prep = fd4_emit_sysmem_prep;
815 ctx->emit_tile_init = fd4_emit_tile_init;
816 ctx->emit_tile_prep = fd4_emit_tile_prep;
817 ctx->emit_tile_mem2gmem = fd4_emit_tile_mem2gmem;
818 ctx->emit_tile_renderprep = fd4_emit_tile_renderprep;
819 ctx->emit_tile_gmem2mem = fd4_emit_tile_gmem2mem;
820 }