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"
44 draw_emit_indirect(struct fd_ringbuffer
*ring
,
46 const struct pipe_draw_info
*info
,
47 unsigned index_offset
)
49 struct fd_resource
*ind
= fd_resource(info
->indirect
->buffer
);
51 if (info
->index_size
) {
52 struct pipe_resource
*idx
= info
->index
.resource
;
53 unsigned max_indicies
= (idx
->width0
- index_offset
) / info
->index_size
;
55 OUT_PKT7(ring
, CP_DRAW_INDX_INDIRECT
, 6);
56 OUT_RING(ring
, draw0
);
57 OUT_RELOC(ring
, fd_resource(idx
)->bo
,
59 OUT_RING(ring
, A5XX_CP_DRAW_INDX_INDIRECT_3_MAX_INDICES(max_indicies
));
60 OUT_RELOC(ring
, ind
->bo
, info
->indirect
->offset
, 0, 0);
62 OUT_PKT7(ring
, CP_DRAW_INDIRECT
, 3);
63 OUT_RING(ring
, draw0
);
64 OUT_RELOC(ring
, ind
->bo
, info
->indirect
->offset
, 0, 0);
69 draw_emit(struct fd_ringbuffer
*ring
,
71 const struct pipe_draw_info
*info
,
72 unsigned index_offset
)
74 if (info
->index_size
) {
75 assert(!info
->has_user_indices
);
77 struct pipe_resource
*idx_buffer
= info
->index
.resource
;
78 uint32_t idx_size
= info
->index_size
* info
->count
;
79 uint32_t idx_offset
= index_offset
+ info
->start
* info
->index_size
;
81 OUT_PKT7(ring
, CP_DRAW_INDX_OFFSET
, 7);
82 OUT_RING(ring
, draw0
);
83 OUT_RING(ring
, info
->instance_count
); /* NumInstances */
84 OUT_RING(ring
, info
->count
); /* NumIndices */
85 OUT_RING(ring
, 0x0); /* XXX */
86 OUT_RELOC(ring
, fd_resource(idx_buffer
)->bo
, idx_offset
, 0, 0);
87 OUT_RING (ring
, idx_size
);
89 OUT_PKT7(ring
, CP_DRAW_INDX_OFFSET
, 3);
90 OUT_RING(ring
, draw0
);
91 OUT_RING(ring
, info
->instance_count
); /* NumInstances */
92 OUT_RING(ring
, info
->count
); /* NumIndices */
96 /* fixup dirty shader state in case some "unrelated" (from the state-
97 * tracker's perspective) state change causes us to switch to a
101 fixup_shader_state(struct fd_context
*ctx
, struct ir3_shader_key
*key
)
103 struct fd6_context
*fd6_ctx
= fd6_context(ctx
);
104 struct ir3_shader_key
*last_key
= &fd6_ctx
->last_key
;
106 if (!ir3_shader_key_equal(last_key
, key
)) {
107 if (ir3_shader_key_changes_fs(last_key
, key
)) {
108 ctx
->dirty_shader
[PIPE_SHADER_FRAGMENT
] |= FD_DIRTY_SHADER_PROG
;
109 ctx
->dirty
|= FD_DIRTY_PROG
;
112 if (ir3_shader_key_changes_vs(last_key
, key
)) {
113 ctx
->dirty_shader
[PIPE_SHADER_VERTEX
] |= FD_DIRTY_SHADER_PROG
;
114 ctx
->dirty
|= FD_DIRTY_PROG
;
117 fd6_ctx
->last_key
= *key
;
122 fd6_draw_vbo(struct fd_context
*ctx
, const struct pipe_draw_info
*info
,
123 unsigned index_offset
)
125 struct fd6_context
*fd6_ctx
= fd6_context(ctx
);
126 struct fd6_emit emit
= {
137 .color_two_side
= ctx
->rasterizer
->light_twoside
,
138 .vclamp_color
= ctx
->rasterizer
->clamp_vertex_color
,
139 .fclamp_color
= ctx
->rasterizer
->clamp_fragment_color
,
140 .rasterflat
= ctx
->rasterizer
->flatshade
,
141 .ucp_enables
= ctx
->rasterizer
->clip_plane_enable
,
142 .has_per_samp
= (fd6_ctx
->fsaturate
|| fd6_ctx
->vsaturate
),
143 .vsaturate_s
= fd6_ctx
->vsaturate_s
,
144 .vsaturate_t
= fd6_ctx
->vsaturate_t
,
145 .vsaturate_r
= fd6_ctx
->vsaturate_r
,
146 .fsaturate_s
= fd6_ctx
->fsaturate_s
,
147 .fsaturate_t
= fd6_ctx
->fsaturate_t
,
148 .fsaturate_r
= fd6_ctx
->fsaturate_r
,
149 .vsamples
= ctx
->tex
[PIPE_SHADER_VERTEX
].samples
,
150 .fsamples
= ctx
->tex
[PIPE_SHADER_FRAGMENT
].samples
,
151 .sample_shading
= (ctx
->min_samples
> 1),
152 .msaa
= (ctx
->framebuffer
.samples
> 1),
155 .rasterflat
= ctx
->rasterizer
->flatshade
,
156 .sprite_coord_enable
= ctx
->rasterizer
->sprite_coord_enable
,
157 .sprite_coord_mode
= ctx
->rasterizer
->sprite_coord_mode
,
160 if (info
->mode
== PIPE_PRIM_PATCHES
) {
161 shader_info
*ds_info
= &emit
.key
.ds
->nir
->info
;
162 switch (ds_info
->tess
.primitive_mode
) {
164 emit
.key
.key
.tessellation
= IR3_TESS_ISOLINES
;
167 emit
.key
.key
.tessellation
= IR3_TESS_TRIANGLES
;
170 emit
.key
.key
.tessellation
= IR3_TESS_QUADS
;
173 unreachable("bad tessmode");
178 emit
.key
.key
.has_gs
= true;
180 fixup_shader_state(ctx
, &emit
.key
.key
);
182 if (!(ctx
->dirty
& FD_DIRTY_PROG
)) {
183 emit
.prog
= fd6_ctx
->prog
;
185 fd6_ctx
->prog
= fd6_emit_get_prog(&emit
);
188 /* bail if compile failed: */
192 emit
.dirty
= ctx
->dirty
; /* *after* fixup_shader_state() */
193 emit
.bs
= fd6_emit_get_prog(&emit
)->bs
;
194 emit
.vs
= fd6_emit_get_prog(&emit
)->vs
;
195 emit
.hs
= fd6_emit_get_prog(&emit
)->hs
;
196 emit
.ds
= fd6_emit_get_prog(&emit
)->ds
;
197 emit
.gs
= fd6_emit_get_prog(&emit
)->gs
;
198 emit
.fs
= fd6_emit_get_prog(&emit
)->fs
;
200 ctx
->stats
.vs_regs
+= ir3_shader_halfregs(emit
.vs
);
201 ctx
->stats
.hs_regs
+= COND(emit
.hs
, ir3_shader_halfregs(emit
.hs
));
202 ctx
->stats
.ds_regs
+= COND(emit
.ds
, ir3_shader_halfregs(emit
.ds
));
203 ctx
->stats
.gs_regs
+= COND(emit
.gs
, ir3_shader_halfregs(emit
.gs
));
204 ctx
->stats
.fs_regs
+= ir3_shader_halfregs(emit
.fs
);
206 /* figure out whether we need to disable LRZ write for binning
207 * pass using draw pass's fs:
209 emit
.no_lrz_write
= emit
.fs
->writes_pos
|| emit
.fs
->no_earlyz
;
211 struct fd_ringbuffer
*ring
= ctx
->batch
->draw
;
212 enum pc_di_primtype primtype
= ctx
->primtypes
[info
->mode
];
214 uint32_t tess_draw0
= 0;
215 if (info
->mode
== PIPE_PRIM_PATCHES
) {
216 shader_info
*ds_info
= &emit
.ds
->shader
->nir
->info
;
217 uint32_t factor_stride
;
220 switch (ds_info
->tess
.primitive_mode
) {
222 patch_type
= TESS_ISOLINES
;
226 patch_type
= TESS_TRIANGLES
;
230 patch_type
= TESS_QUADS
;
234 unreachable("bad tessmode");
237 primtype
= DI_PT_PATCHES0
+ info
->vertices_per_patch
;
238 tess_draw0
|= CP_DRAW_INDX_OFFSET_0_PATCH_TYPE(patch_type
) |
239 CP_DRAW_INDX_OFFSET_0_TESS_ENABLE
;
241 ctx
->batch
->tessellation
= true;
242 ctx
->batch
->tessparam_size
= MAX2(ctx
->batch
->tessparam_size
,
243 emit
.hs
->shader
->output_size
* 4 * info
->count
);
244 ctx
->batch
->tessfactor_size
= MAX2(ctx
->batch
->tessfactor_size
,
245 factor_stride
* info
->count
);
247 if (!ctx
->batch
->tess_addrs_constobj
) {
248 /* Reserve space for the bo address - we'll write them later in
249 * setup_tess_buffers(). We need 2 bo address, but indirect
250 * constant upload needs at least 4 vec4s.
252 unsigned size
= 4 * 16;
254 ctx
->batch
->tess_addrs_constobj
= fd_submit_new_ringbuffer(
255 ctx
->batch
->submit
, size
, FD_RINGBUFFER_STREAMING
);
257 ctx
->batch
->tess_addrs_constobj
->cur
+= size
;
261 fd6_emit_state(ring
, &emit
);
263 OUT_PKT4(ring
, REG_A6XX_VFD_INDEX_OFFSET
, 2);
264 OUT_RING(ring
, info
->index_size
? info
->index_bias
: info
->start
); /* VFD_INDEX_OFFSET */
265 OUT_RING(ring
, info
->start_instance
); /* VFD_INSTANCE_START_OFFSET */
267 OUT_PKT4(ring
, REG_A6XX_PC_RESTART_INDEX
, 1);
268 OUT_RING(ring
, info
->primitive_restart
? /* PC_RESTART_INDEX */
269 info
->restart_index
: 0xffffffff);
271 /* for debug after a lock up, write a unique counter value
272 * to scratch7 for each draw, to make it easier to match up
273 * register dumps to cmdstream. The combination of IB
274 * (scratch6) and DRAW is enough to "triangulate" the
275 * particular draw that caused lockup.
277 emit_marker6(ring
, 7);
280 CP_DRAW_INDX_OFFSET_0_VIS_CULL(USE_VISIBILITY
) |
281 CP_DRAW_INDX_OFFSET_0_PRIM_TYPE(primtype
) |
283 COND(emit
.key
.gs
, CP_DRAW_INDX_OFFSET_0_GS_ENABLE
);
285 if (info
->index_size
) {
287 CP_DRAW_INDX_OFFSET_0_SOURCE_SELECT(DI_SRC_SEL_DMA
) |
288 CP_DRAW_INDX_OFFSET_0_INDEX_SIZE(fd4_size2indextype(info
->index_size
));
291 CP_DRAW_INDX_OFFSET_0_SOURCE_SELECT(DI_SRC_SEL_AUTO_INDEX
);
294 if (info
->indirect
) {
295 draw_emit_indirect(ring
, draw0
, info
, index_offset
);
297 draw_emit(ring
, draw0
, info
, index_offset
);
300 emit_marker6(ring
, 7);
301 fd_reset_wfi(ctx
->batch
);
303 if (emit
.streamout_mask
) {
304 struct fd_ringbuffer
*ring
= ctx
->batch
->draw
;
306 for (unsigned i
= 0; i
< PIPE_MAX_SO_BUFFERS
; i
++) {
307 if (emit
.streamout_mask
& (1 << i
)) {
308 fd6_event_write(ctx
->batch
, ring
, FLUSH_SO_0
+ i
, false);
313 fd_context_all_clean(ctx
);
319 fd6_clear_lrz(struct fd_batch
*batch
, struct fd_resource
*zsbuf
, double depth
)
321 struct fd_ringbuffer
*ring
;
322 struct fd6_context
*fd6_ctx
= fd6_context(batch
->ctx
);
324 // TODO mid-frame clears (ie. app doing crazy stuff)?? Maybe worth
325 // splitting both clear and lrz clear out into their own rb's. And
326 // just throw away any draws prior to clear. (Anything not fullscreen
327 // clear, just fallback to generic path that treats it as a normal
330 if (!batch
->lrz_clear
) {
331 batch
->lrz_clear
= fd_submit_new_ringbuffer(batch
->submit
, 0x1000, 0);
334 ring
= batch
->lrz_clear
;
336 emit_marker6(ring
, 7);
337 OUT_PKT7(ring
, CP_SET_MARKER
, 1);
338 OUT_RING(ring
, A6XX_CP_SET_MARKER_0_MODE(RM6_BYPASS
));
339 emit_marker6(ring
, 7);
343 OUT_PKT4(ring
, REG_A6XX_RB_CCU_CNTL
, 1);
344 OUT_RING(ring
, fd6_ctx
->magic
.RB_CCU_CNTL_bypass
);
346 OUT_PKT4(ring
, REG_A6XX_HLSQ_UPDATE_CNTL
, 1);
347 OUT_RING(ring
, 0x7ffff);
349 emit_marker6(ring
, 7);
350 OUT_PKT7(ring
, CP_SET_MARKER
, 1);
351 OUT_RING(ring
, A6XX_CP_SET_MARKER_0_MODE(0xc));
352 emit_marker6(ring
, 7);
354 OUT_PKT4(ring
, REG_A6XX_RB_UNKNOWN_8C01
, 1);
357 OUT_PKT4(ring
, REG_A6XX_SP_PS_2D_SRC_INFO
, 13);
358 OUT_RING(ring
, 0x00000000);
359 OUT_RING(ring
, 0x00000000);
360 OUT_RING(ring
, 0x00000000);
361 OUT_RING(ring
, 0x00000000);
362 OUT_RING(ring
, 0x00000000);
363 OUT_RING(ring
, 0x00000000);
364 OUT_RING(ring
, 0x00000000);
365 OUT_RING(ring
, 0x00000000);
366 OUT_RING(ring
, 0x00000000);
367 OUT_RING(ring
, 0x00000000);
368 OUT_RING(ring
, 0x00000000);
369 OUT_RING(ring
, 0x00000000);
370 OUT_RING(ring
, 0x00000000);
372 OUT_PKT4(ring
, REG_A6XX_SP_2D_SRC_FORMAT
, 1);
373 OUT_RING(ring
, 0x0000f410);
375 OUT_PKT4(ring
, REG_A6XX_GRAS_2D_BLIT_CNTL
, 1);
376 OUT_RING(ring
, A6XX_GRAS_2D_BLIT_CNTL_COLOR_FORMAT(RB6_R16_UNORM
) |
379 OUT_PKT4(ring
, REG_A6XX_RB_2D_BLIT_CNTL
, 1);
380 OUT_RING(ring
, A6XX_RB_2D_BLIT_CNTL_COLOR_FORMAT(RB6_R16_UNORM
) |
383 fd6_event_write(batch
, ring
, UNK_1D
, true);
384 fd6_event_write(batch
, ring
, PC_CCU_INVALIDATE_COLOR
, false);
386 OUT_PKT4(ring
, REG_A6XX_RB_2D_SRC_SOLID_C0
, 4);
387 OUT_RING(ring
, fui(depth
));
388 OUT_RING(ring
, 0x00000000);
389 OUT_RING(ring
, 0x00000000);
390 OUT_RING(ring
, 0x00000000);
392 OUT_PKT4(ring
, REG_A6XX_RB_2D_DST_INFO
, 9);
393 OUT_RING(ring
, A6XX_RB_2D_DST_INFO_COLOR_FORMAT(RB6_R16_UNORM
) |
394 A6XX_RB_2D_DST_INFO_TILE_MODE(TILE6_LINEAR
) |
395 A6XX_RB_2D_DST_INFO_COLOR_SWAP(WZYX
));
396 OUT_RELOCW(ring
, zsbuf
->lrz
, 0, 0, 0);
397 OUT_RING(ring
, A6XX_RB_2D_DST_SIZE_PITCH(zsbuf
->lrz_pitch
* 2));
398 OUT_RING(ring
, 0x00000000);
399 OUT_RING(ring
, 0x00000000);
400 OUT_RING(ring
, 0x00000000);
401 OUT_RING(ring
, 0x00000000);
402 OUT_RING(ring
, 0x00000000);
404 OUT_PKT4(ring
, REG_A6XX_GRAS_2D_SRC_TL_X
, 4);
405 OUT_RING(ring
, A6XX_GRAS_2D_SRC_TL_X_X(0));
406 OUT_RING(ring
, A6XX_GRAS_2D_SRC_BR_X_X(0));
407 OUT_RING(ring
, A6XX_GRAS_2D_SRC_TL_Y_Y(0));
408 OUT_RING(ring
, A6XX_GRAS_2D_SRC_BR_Y_Y(0));
410 OUT_PKT4(ring
, REG_A6XX_GRAS_2D_DST_TL
, 2);
411 OUT_RING(ring
, A6XX_GRAS_2D_DST_TL_X(0) |
412 A6XX_GRAS_2D_DST_TL_Y(0));
413 OUT_RING(ring
, A6XX_GRAS_2D_DST_BR_X(zsbuf
->lrz_width
- 1) |
414 A6XX_GRAS_2D_DST_BR_Y(zsbuf
->lrz_height
- 1));
416 fd6_event_write(batch
, ring
, 0x3f, false);
420 OUT_PKT4(ring
, REG_A6XX_RB_UNKNOWN_8E04
, 1);
421 OUT_RING(ring
, fd6_ctx
->magic
.RB_UNKNOWN_8E04_blit
);
423 OUT_PKT7(ring
, CP_BLIT
, 1);
424 OUT_RING(ring
, CP_BLIT_0_OP(BLIT_OP_SCALE
));
428 OUT_PKT4(ring
, REG_A6XX_RB_UNKNOWN_8E04
, 1);
429 OUT_RING(ring
, 0x0); /* RB_UNKNOWN_8E04 */
431 fd6_event_write(batch
, ring
, UNK_1D
, true);
432 fd6_event_write(batch
, ring
, FACENESS_FLUSH
, true);
433 fd6_event_write(batch
, ring
, CACHE_FLUSH_TS
, true);
435 fd6_cache_inv(batch
, ring
);
438 static bool is_z32(enum pipe_format format
)
441 case PIPE_FORMAT_Z32_FLOAT_S8X24_UINT
:
442 case PIPE_FORMAT_Z32_UNORM
:
443 case PIPE_FORMAT_Z32_FLOAT
:
451 fd6_clear(struct fd_context
*ctx
, unsigned buffers
,
452 const union pipe_color_union
*color
, double depth
, unsigned stencil
)
454 struct pipe_framebuffer_state
*pfb
= &ctx
->batch
->framebuffer
;
455 const bool has_depth
= pfb
->zsbuf
;
456 unsigned color_buffers
= buffers
>> 2;
459 /* If we're clearing after draws, fallback to 3D pipe clears. We could
460 * use blitter clears in the draw batch but then we'd have to patch up the
461 * gmem offsets. This doesn't seem like a useful thing to optimize for
463 if (ctx
->batch
->num_draws
> 0)
466 foreach_bit(i
, color_buffers
)
467 ctx
->batch
->clear_color
[i
] = *color
;
468 if (buffers
& PIPE_CLEAR_DEPTH
)
469 ctx
->batch
->clear_depth
= depth
;
470 if (buffers
& PIPE_CLEAR_STENCIL
)
471 ctx
->batch
->clear_stencil
= stencil
;
473 ctx
->batch
->fast_cleared
|= buffers
;
475 if (has_depth
&& (buffers
& PIPE_CLEAR_DEPTH
)) {
476 struct fd_resource
*zsbuf
= fd_resource(pfb
->zsbuf
->texture
);
477 if (zsbuf
->lrz
&& !is_z32(pfb
->zsbuf
->format
)) {
478 zsbuf
->lrz_valid
= true;
479 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
;