2 * Copyright (C) 2016 Rob Clark <robclark@freedesktop.org>
3 * Copyright © 2018 Google, Inc.
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:
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
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
25 * Rob Clark <robclark@freedesktop.org>
28 #include "pipe/p_state.h"
29 #include "util/u_string.h"
30 #include "util/u_memory.h"
31 #include "util/u_prim.h"
33 #include "freedreno_state.h"
34 #include "freedreno_resource.h"
37 #include "fd6_context.h"
39 #include "fd6_program.h"
40 #include "fd6_format.h"
45 draw_impl(struct fd_context
*ctx
, struct fd_ringbuffer
*ring
,
46 struct fd6_emit
*emit
, unsigned index_offset
)
48 const struct pipe_draw_info
*info
= emit
->info
;
49 enum pc_di_primtype primtype
= ctx
->primtypes
[info
->mode
];
51 fd6_emit_state(ctx
, ring
, emit
);
53 if (emit
->dirty
& (FD_DIRTY_VTXBUF
| FD_DIRTY_VTXSTATE
))
54 fd6_emit_vertex_bufs(ring
, emit
);
56 OUT_PKT4(ring
, REG_A6XX_VFD_INDEX_OFFSET
, 2);
57 OUT_RING(ring
, info
->index_size
? info
->index_bias
: info
->start
); /* VFD_INDEX_OFFSET */
58 OUT_RING(ring
, info
->start_instance
); /* VFD_INSTANCE_START_OFFSET */
60 OUT_PKT4(ring
, REG_A6XX_PC_RESTART_INDEX
, 1);
61 OUT_RING(ring
, info
->primitive_restart
? /* PC_RESTART_INDEX */
62 info
->restart_index
: 0xffffffff);
64 fd6_emit_render_cntl(ctx
, false, emit
->key
.binning_pass
);
65 fd6_draw_emit(ctx
->batch
, ring
, primtype
,
66 emit
->key
.binning_pass
? IGNORE_VISIBILITY
: USE_VISIBILITY
,
70 /* fixup dirty shader state in case some "unrelated" (from the state-
71 * tracker's perspective) state change causes us to switch to a
75 fixup_shader_state(struct fd_context
*ctx
, struct ir3_shader_key
*key
)
77 struct fd6_context
*fd6_ctx
= fd6_context(ctx
);
78 struct ir3_shader_key
*last_key
= &fd6_ctx
->last_key
;
80 if (!ir3_shader_key_equal(last_key
, key
)) {
81 if (ir3_shader_key_changes_fs(last_key
, key
)) {
82 ctx
->dirty_shader
[PIPE_SHADER_FRAGMENT
] |= FD_DIRTY_SHADER_PROG
;
83 ctx
->dirty
|= FD_DIRTY_PROG
;
86 if (ir3_shader_key_changes_vs(last_key
, key
)) {
87 ctx
->dirty_shader
[PIPE_SHADER_VERTEX
] |= FD_DIRTY_SHADER_PROG
;
88 ctx
->dirty
|= FD_DIRTY_PROG
;
91 fd6_ctx
->last_key
= *key
;
96 fd6_draw_vbo(struct fd_context
*ctx
, const struct pipe_draw_info
*info
,
97 unsigned index_offset
)
99 struct fd6_context
*fd6_ctx
= fd6_context(ctx
);
100 struct fd6_emit emit
= {
101 .debug
= &ctx
->debug
,
106 .color_two_side
= ctx
->rasterizer
->light_twoside
,
107 .vclamp_color
= ctx
->rasterizer
->clamp_vertex_color
,
108 .fclamp_color
= ctx
->rasterizer
->clamp_fragment_color
,
109 .rasterflat
= ctx
->rasterizer
->flatshade
,
110 .half_precision
= ctx
->in_blit
&&
111 fd_half_precision(&ctx
->batch
->framebuffer
),
112 .ucp_enables
= ctx
->rasterizer
->clip_plane_enable
,
113 .has_per_samp
= (fd6_ctx
->fsaturate
|| fd6_ctx
->vsaturate
||
114 fd6_ctx
->fastc_srgb
|| fd6_ctx
->vastc_srgb
),
115 .vsaturate_s
= fd6_ctx
->vsaturate_s
,
116 .vsaturate_t
= fd6_ctx
->vsaturate_t
,
117 .vsaturate_r
= fd6_ctx
->vsaturate_r
,
118 .fsaturate_s
= fd6_ctx
->fsaturate_s
,
119 .fsaturate_t
= fd6_ctx
->fsaturate_t
,
120 .fsaturate_r
= fd6_ctx
->fsaturate_r
,
121 .vastc_srgb
= fd6_ctx
->vastc_srgb
,
122 .fastc_srgb
= fd6_ctx
->fastc_srgb
,
123 .vsamples
= ctx
->tex
[PIPE_SHADER_VERTEX
].samples
,
124 .fsamples
= ctx
->tex
[PIPE_SHADER_FRAGMENT
].samples
,
126 .rasterflat
= ctx
->rasterizer
->flatshade
,
127 .sprite_coord_enable
= ctx
->rasterizer
->sprite_coord_enable
,
128 .sprite_coord_mode
= ctx
->rasterizer
->sprite_coord_mode
,
131 fixup_shader_state(ctx
, &emit
.key
);
133 unsigned dirty
= ctx
->dirty
;
134 const struct ir3_shader_variant
*vp
= fd6_emit_get_vp(&emit
);
135 const struct ir3_shader_variant
*fp
= fd6_emit_get_fp(&emit
);
137 /* do regular pass first, since that is more likely to fail compiling: */
142 ctx
->stats
.vs_regs
+= ir3_shader_halfregs(vp
);
143 ctx
->stats
.fs_regs
+= ir3_shader_halfregs(fp
);
145 /* figure out whether we need to disable LRZ write for binning
146 * pass using draw pass's fp:
148 emit
.no_lrz_write
= fp
->writes_pos
|| fp
->has_kill
;
150 emit
.key
.binning_pass
= false;
153 draw_impl(ctx
, ctx
->batch
->draw
, &emit
, index_offset
);
155 /* and now binning pass: */
156 emit
.key
.binning_pass
= true;
157 emit
.dirty
= dirty
& ~(FD_DIRTY_BLEND
);
158 emit
.vp
= NULL
; /* we changed key so need to refetch vp */
160 draw_impl(ctx
, ctx
->batch
->binning
, &emit
, index_offset
);
162 if (emit
.streamout_mask
) {
163 struct fd_ringbuffer
*ring
= ctx
->batch
->draw
;
165 for (unsigned i
= 0; i
< PIPE_MAX_SO_BUFFERS
; i
++) {
166 if (emit
.streamout_mask
& (1 << i
)) {
167 OUT_PKT7(ring
, CP_EVENT_WRITE
, 1);
168 OUT_RING(ring
, FLUSH_SO_0
+ i
);
173 fd_context_all_clean(ctx
);
178 static bool is_z32(enum pipe_format format
)
181 case PIPE_FORMAT_Z32_FLOAT_S8X24_UINT
:
182 case PIPE_FORMAT_Z32_UNORM
:
183 case PIPE_FORMAT_Z32_FLOAT
:
192 fd6_clear_lrz(struct fd_batch
*batch
, struct fd_resource
*zsbuf
, double depth
)
194 struct fd_ringbuffer
*ring
;
195 uint32_t clear
= util_pack_z(PIPE_FORMAT_Z16_UNORM
, depth
);
197 // TODO mid-frame clears (ie. app doing crazy stuff)?? Maybe worth
198 // splitting both clear and lrz clear out into their own rb's. And
199 // just throw away any draws prior to clear. (Anything not fullscreen
200 // clear, just fallback to generic path that treats it as a normal
203 if (!batch
->lrz_clear
) {
204 batch
->lrz_clear
= fd_ringbuffer_new(batch
->ctx
->pipe
, 0x1000);
205 fd_ringbuffer_set_parent(batch
->lrz_clear
, batch
->gmem
);
208 ring
= batch
->lrz_clear
;
212 OUT_PKT4(ring
, REG_A6XX_RB_CCU_CNTL
, 1);
213 OUT_RING(ring
, 0x10000000);
215 OUT_PKT4(ring
, REG_A6XX_HLSQ_UPDATE_CNTL
, 1);
216 OUT_RING(ring
, 0x20fffff);
218 OUT_PKT4(ring
, REG_A6XX_GRAS_SU_CNTL
, 1);
219 OUT_RING(ring
, A6XX_GRAS_SU_CNTL_LINEHALFWIDTH(0.0));
221 OUT_PKT4(ring
, REG_A6XX_GRAS_CNTL
, 1);
222 OUT_RING(ring
, 0x00000000);
224 OUT_PKT4(ring
, REG_A6XX_GRAS_CL_CNTL
, 1);
225 OUT_RING(ring
, 0x00000181);
227 OUT_PKT4(ring
, REG_A6XX_GRAS_LRZ_CNTL
, 1);
228 OUT_RING(ring
, 0x00000000);
230 OUT_PKT4(ring
, REG_A6XX_RB_MRT_BUF_INFO(0), 5);
231 OUT_RING(ring
, A6XX_RB_MRT_BUF_INFO_COLOR_FORMAT(RB5_R16_UNORM
) |
232 A6XX_RB_MRT_BUF_INFO_COLOR_TILE_MODE(TILE6_LINEAR
) |
233 A6XX_RB_MRT_BUF_INFO_COLOR_SWAP(WZYX
));
234 OUT_RING(ring
, A6XX_RB_MRT_PITCH(zsbuf
->lrz_pitch
* 2));
235 OUT_RING(ring
, A6XX_RB_MRT_ARRAY_PITCH(fd_bo_size(zsbuf
->lrz
)));
236 OUT_RELOCW(ring
, zsbuf
->lrz
, 0x1000, 0, 0);
238 OUT_PKT4(ring
, REG_A6XX_RB_RENDER_CNTL
, 1);
239 OUT_RING(ring
, 0x00000000);
241 OUT_PKT4(ring
, REG_A6XX_RB_DEST_MSAA_CNTL
, 1);
242 OUT_RING(ring
, A6XX_RB_DEST_MSAA_CNTL_SAMPLES(MSAA_ONE
));
244 OUT_PKT4(ring
, REG_A6XX_RB_BLIT_CNTL
, 1);
245 OUT_RING(ring
, A6XX_RB_BLIT_CNTL_BUF(BLIT_MRT0
));
247 OUT_PKT4(ring
, REG_A6XX_RB_CLEAR_CNTL
, 1);
248 OUT_RING(ring
, A6XX_RB_CLEAR_CNTL_FAST_CLEAR
|
249 A6XX_RB_CLEAR_CNTL_MASK(0xf));
251 OUT_PKT4(ring
, REG_A6XX_RB_CLEAR_COLOR_DW0
, 1);
252 OUT_RING(ring
, clear
); /* RB_CLEAR_COLOR_DW0 */
254 OUT_PKT4(ring
, REG_A6XX_VSC_RESOLVE_CNTL
, 2);
255 OUT_RING(ring
, A6XX_VSC_RESOLVE_CNTL_X(zsbuf
->lrz_width
) |
256 A6XX_VSC_RESOLVE_CNTL_Y(zsbuf
->lrz_height
));
257 OUT_RING(ring
, 0x00000000); // XXX UNKNOWN_0CDE
259 OUT_PKT4(ring
, REG_A6XX_RB_CNTL
, 1);
260 OUT_RING(ring
, A6XX_RB_CNTL_BYPASS
);
262 OUT_PKT4(ring
, REG_A6XX_RB_RESOLVE_CNTL_1
, 2);
263 OUT_RING(ring
, A6XX_RB_RESOLVE_CNTL_1_X(0) |
264 A6XX_RB_RESOLVE_CNTL_1_Y(0));
265 OUT_RING(ring
, A6XX_RB_RESOLVE_CNTL_2_X(zsbuf
->lrz_width
- 1) |
266 A6XX_RB_RESOLVE_CNTL_2_Y(zsbuf
->lrz_height
- 1));
268 fd6_emit_blit(batch
->ctx
, ring
);
275 /* Clear with CP_BLIT */
276 WRITE(REG_A6XX_GRAS_2D_BLIT_CNTL
, 0x10f43180);
278 OUT_PKT4(ring
, REG_A6XX_SP_PS_2D_SRC_INFO
, 7);
287 WRITE(0xacc0, 0xf181);
288 WRITE(0xacc0, 0xf181);
290 WRITE(REG_A6XX_GRAS_2D_BLIT_CNTL
, 0x10f43180);
291 WRITE(REG_A6XX_RB_2D_BLIT_CNTL
, 0x10f43180);
293 OUT_PKT4(ring
, REG_A6XX_RB_2D_SRC_SOLID_C0
, 4);
296 OUT_RING(ring
, 0xff);
299 DBG("%x %x %x %x\n", color
->ui
[0], color
->ui
[1], color
->ui
[2], color
->ui
[3]);
301 struct pipe_surface
*psurf
= pfb
->cbufs
[0];
302 struct fd_resource
*rsc
= fd_resource(psurf
->texture
);
303 struct fd_resource_slice
*slice
= fd_resource_slice(rsc
, psurf
->u
.tex
.level
);
305 uint32_t offset
= fd_resource_offset(rsc
, psurf
->u
.tex
.level
,
306 psurf
->u
.tex
.first_layer
);
307 uint32_t stride
= slice
->pitch
* rsc
->cpp
;
309 enum a6xx_color_fmt format
= fd6_pipe2color(pfmt
);
310 OUT_PKT4(ring
, REG_A6XX_RB_2D_DST_INFO
, 9);
312 A6XX_RB_2D_DST_INFO_COLOR_FORMAT(format
) |
313 A6XX_RB_2D_DST_INFO_TILE_MODE(TILE6_LINEAR
) |
314 A6XX_RB_2D_DST_INFO_COLOR_SWAP(WXYZ
));
315 OUT_RELOCW(ring
, rsc
->bo
, offset
, 0, 0); /* RB_2D_DST_LO/HI */
316 OUT_RING(ring
, A6XX_RB_2D_DST_SIZE_PITCH(stride
));
323 OUT_PKT4(ring
, REG_A6XX_GRAS_2D_SRC_TL_X
, 4);
329 OUT_PKT4(ring
, REG_A6XX_GRAS_2D_DST_TL
, 2);
331 A6XX_GRAS_2D_DST_TL_X(ctx
->batch
->max_scissor
.minx
) |
332 A6XX_GRAS_2D_DST_TL_Y(ctx
->batch
->max_scissor
.miny
));
334 A6XX_GRAS_2D_DST_BR_X(ctx
->batch
->max_scissor
.maxx
) |
335 A6XX_GRAS_2D_DST_BR_Y(ctx
->batch
->max_scissor
.maxy
));
337 OUT_PKT7(ring
, CP_BLIT
, 1);
338 OUT_RING(ring
, CP_BLIT_0_OP(BLIT_OP_SCALE
));
343 fd6_clear(struct fd_context
*ctx
, unsigned buffers
,
344 const union pipe_color_union
*color
, double depth
, unsigned stencil
)
346 struct pipe_framebuffer_state
*pfb
= &ctx
->batch
->framebuffer
;
347 struct pipe_scissor_state
*scissor
= fd_context_get_scissor(ctx
);
348 struct fd_ringbuffer
*ring
= ctx
->batch
->draw
;
350 if ((buffers
& (PIPE_CLEAR_DEPTH
| PIPE_CLEAR_STENCIL
)) &&
351 is_z32(pfb
->zsbuf
->format
))
354 ctx
->batch
->max_scissor
.minx
= MIN2(ctx
->batch
->max_scissor
.minx
, scissor
->minx
);
355 ctx
->batch
->max_scissor
.miny
= MIN2(ctx
->batch
->max_scissor
.miny
, scissor
->miny
);
356 ctx
->batch
->max_scissor
.maxx
= MAX2(ctx
->batch
->max_scissor
.maxx
, scissor
->maxx
);
357 ctx
->batch
->max_scissor
.maxy
= MAX2(ctx
->batch
->max_scissor
.maxy
, scissor
->maxy
);
359 fd6_emit_render_cntl(ctx
, true, false);
361 OUT_PKT4(ring
, REG_A6XX_RB_BLIT_SCISSOR_TL
, 2);
362 OUT_RING(ring
, A6XX_RB_BLIT_SCISSOR_TL_X(scissor
->minx
) |
363 A6XX_RB_BLIT_SCISSOR_TL_Y(scissor
->miny
));
364 OUT_RING(ring
, A6XX_RB_BLIT_SCISSOR_BR_X(scissor
->maxx
- 1) |
365 A6XX_RB_BLIT_SCISSOR_BR_Y(scissor
->maxy
- 1));
367 if (buffers
& PIPE_CLEAR_COLOR
) {
368 for (int i
= 0; i
< pfb
->nr_cbufs
; i
++) {
369 union util_color uc
= {0};
374 if (!(buffers
& (PIPE_CLEAR_COLOR0
<< i
)))
377 enum pipe_format pfmt
= pfb
->cbufs
[i
]->format
;
379 // XXX I think RB_CLEAR_COLOR_DWn wants to take into account SWAP??
380 union pipe_color_union swapped
;
381 switch (fd6_pipe2swap(pfmt
)) {
383 swapped
.ui
[0] = color
->ui
[0];
384 swapped
.ui
[1] = color
->ui
[1];
385 swapped
.ui
[2] = color
->ui
[2];
386 swapped
.ui
[3] = color
->ui
[3];
389 swapped
.ui
[2] = color
->ui
[0];
390 swapped
.ui
[1] = color
->ui
[1];
391 swapped
.ui
[0] = color
->ui
[2];
392 swapped
.ui
[3] = color
->ui
[3];
395 swapped
.ui
[3] = color
->ui
[0];
396 swapped
.ui
[0] = color
->ui
[1];
397 swapped
.ui
[1] = color
->ui
[2];
398 swapped
.ui
[2] = color
->ui
[3];
401 swapped
.ui
[3] = color
->ui
[0];
402 swapped
.ui
[2] = color
->ui
[1];
403 swapped
.ui
[1] = color
->ui
[2];
404 swapped
.ui
[0] = color
->ui
[3];
408 if (util_format_is_pure_uint(pfmt
)) {
409 util_format_write_4ui(pfmt
, swapped
.ui
, 0, &uc
, 0, 0, 0, 1, 1);
410 } else if (util_format_is_pure_sint(pfmt
)) {
411 util_format_write_4i(pfmt
, swapped
.i
, 0, &uc
, 0, 0, 0, 1, 1);
413 util_pack_color(swapped
.f
, pfmt
, &uc
);
416 OUT_PKT4(ring
, REG_A6XX_RB_BLIT_DST_INFO
, 1);
417 OUT_RING(ring
, A6XX_RB_BLIT_DST_INFO_TILE_MODE(TILE6_LINEAR
) |
418 A6XX_RB_BLIT_DST_INFO_COLOR_FORMAT(fd6_pipe2color(pfmt
)));
420 OUT_PKT4(ring
, REG_A6XX_RB_BLIT_INFO
, 1);
421 OUT_RING(ring
, A6XX_RB_BLIT_INFO_GMEM
|
422 A6XX_RB_BLIT_INFO_CLEAR_MASK(0xf));
424 OUT_PKT4(ring
, REG_A6XX_RB_BLIT_BASE_GMEM
, 1);
425 OUT_RINGP(ring
, i
, &ctx
->batch
->gmem_patches
);
427 OUT_PKT4(ring
, REG_A6XX_RB_UNKNOWN_88D0
, 1);
430 OUT_PKT4(ring
, REG_A6XX_RB_BLIT_CLEAR_COLOR_DW0
, 4);
431 OUT_RING(ring
, uc
.ui
[0]);
432 OUT_RING(ring
, uc
.ui
[1]);
433 OUT_RING(ring
, uc
.ui
[2]);
434 OUT_RING(ring
, uc
.ui
[3]);
436 fd6_emit_blit(ctx
, ring
);
440 if (pfb
->zsbuf
&& (buffers
& (PIPE_CLEAR_DEPTH
| PIPE_CLEAR_STENCIL
))) {
441 enum pipe_format pfmt
= pfb
->zsbuf
->format
;
442 uint32_t clear
= util_pack_z_stencil(pfmt
, depth
, stencil
);
445 if (buffers
& PIPE_CLEAR_DEPTH
)
448 if (buffers
& PIPE_CLEAR_STENCIL
)
451 OUT_PKT4(ring
, REG_A6XX_RB_BLIT_DST_INFO
, 1);
452 OUT_RING(ring
, A6XX_RB_BLIT_DST_INFO_TILE_MODE(TILE6_LINEAR
) |
453 A6XX_RB_BLIT_DST_INFO_COLOR_FORMAT(fd6_pipe2color(pfmt
)));
455 OUT_PKT4(ring
, REG_A6XX_RB_BLIT_INFO
, 1);
456 OUT_RING(ring
, A6XX_RB_BLIT_INFO_GMEM
|
457 // XXX UNK0 for separate stencil ??
458 A6XX_RB_BLIT_INFO_DEPTH
|
459 A6XX_RB_BLIT_INFO_CLEAR_MASK(mask
));
461 OUT_PKT4(ring
, REG_A6XX_RB_BLIT_BASE_GMEM
, 1);
462 OUT_RINGP(ring
, MAX_RENDER_TARGETS
, &ctx
->batch
->gmem_patches
);
464 OUT_PKT4(ring
, REG_A6XX_RB_UNKNOWN_88D0
, 1);
467 OUT_PKT4(ring
, REG_A6XX_RB_BLIT_CLEAR_COLOR_DW0
, 1);
468 OUT_RING(ring
, clear
);
470 fd6_emit_blit(ctx
, ring
);
473 if (pfb
->zsbuf
&& (buffers
& PIPE_CLEAR_DEPTH
)) {
474 struct fd_resource
*zsbuf
= fd_resource(pfb
->zsbuf
->texture
);
476 zsbuf
->lrz_valid
= true;
477 fd6_clear_lrz(ctx
->batch
, zsbuf
, depth
);
487 fd6_draw_init(struct pipe_context
*pctx
)
489 struct fd_context
*ctx
= fd_context(pctx
);
490 ctx
->draw_vbo
= fd6_draw_vbo
;
491 ctx
->clear
= fd6_clear
;