2 * Copyright 2017 Advanced Micro Devices, 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 * on the rights to use, copy, modify, merge, publish, distribute, sub
9 * license, and/or sell copies of the Software, and to permit persons to whom
10 * the 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 NON-INFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHOR(S) AND/OR THEIR SUPPLIERS BE LIABLE FOR ANY CLAIM,
20 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
21 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
22 * USE OR OTHER DEALINGS IN THE SOFTWARE.
25 /* This file handles register programming of primitive binning. */
27 #include "si_build_pm4.h"
34 struct si_bin_size_map
{
40 typedef struct si_bin_size_map si_bin_size_subtable
[3][10];
42 /* Find the bin size where sum is >= table[i].start and < table[i + 1].start. */
43 static struct uvec2
si_find_bin_size(struct si_screen
*sscreen
,
44 const si_bin_size_subtable table
[],
47 unsigned log_num_rb_per_se
=
48 util_logbase2_ceil(sscreen
->info
.num_render_backends
/
49 sscreen
->info
.max_se
);
50 unsigned log_num_se
= util_logbase2_ceil(sscreen
->info
.max_se
);
53 /* Get the chip-specific subtable. */
54 const struct si_bin_size_map
*subtable
=
55 &table
[log_num_rb_per_se
][log_num_se
][0];
57 for (i
= 0; subtable
[i
].bin_size_x
!= 0; i
++) {
58 if (sum
>= subtable
[i
].start
&& sum
< subtable
[i
+ 1].start
)
62 struct uvec2 size
= {subtable
[i
].bin_size_x
, subtable
[i
].bin_size_y
};
66 static struct uvec2
si_get_color_bin_size(struct si_context
*sctx
,
67 unsigned cb_target_enabled_4bit
)
69 unsigned num_fragments
= sctx
->framebuffer
.nr_color_samples
;
72 /* Compute the sum of all Bpp. */
73 for (unsigned i
= 0; i
< sctx
->framebuffer
.state
.nr_cbufs
; i
++) {
74 if (!(cb_target_enabled_4bit
& (0xf << (i
* 4))))
77 struct r600_texture
*rtex
=
78 (struct r600_texture
*)sctx
->framebuffer
.state
.cbufs
[i
]->texture
;
79 sum
+= rtex
->surface
.bpe
;
82 /* Multiply the sum by some function of the number of samples. */
83 if (num_fragments
>= 2) {
84 if (si_get_ps_iter_samples(sctx
) >= 2)
90 static const si_bin_size_subtable table
[] = {
94 /* One shader engine */
102 /* Two shader engines */
110 /* Four shader engines */
120 /* One shader engine */
128 /* Two shader engines */
136 /* Four shader engines */
148 /* One shader engine */
157 /* Two shader engines */
167 /* Four shader engines */
179 return si_find_bin_size(sctx
->screen
, table
, sum
);
182 static struct uvec2
si_get_depth_bin_size(struct si_context
*sctx
)
184 struct si_state_dsa
*dsa
= sctx
->queued
.named
.dsa
;
186 if (!sctx
->framebuffer
.state
.zsbuf
||
187 (!dsa
->depth_enabled
&& !dsa
->stencil_enabled
)) {
188 /* Return the max size. */
189 struct uvec2 size
= {512, 512};
193 struct r600_texture
*rtex
=
194 (struct r600_texture
*)sctx
->framebuffer
.state
.zsbuf
->texture
;
195 unsigned depth_coeff
= dsa
->depth_enabled
? 5 : 0;
196 unsigned stencil_coeff
= rtex
->surface
.has_stencil
&&
197 dsa
->stencil_enabled
? 1 : 0;
198 unsigned sum
= 4 * (depth_coeff
+ stencil_coeff
) *
199 rtex
->buffer
.b
.b
.nr_samples
;
201 static const si_bin_size_subtable table
[] = {
214 // Two shader engines
224 // Four shader engines
247 // Two shader engines
258 // Four shader engines
283 // Two shader engines
295 // Four shader engines
308 return si_find_bin_size(sctx
->screen
, table
, sum
);
311 static void si_emit_dpbb_disable(struct si_context
*sctx
)
313 radeon_opt_set_context_reg(sctx
, R_028C44_PA_SC_BINNER_CNTL_0
,
314 SI_TRACKED_PA_SC_BINNER_CNTL_0
,
315 S_028C44_BINNING_MODE(V_028C44_DISABLE_BINNING_USE_LEGACY_SC
) |
316 S_028C44_DISABLE_START_OF_PRIM(1));
317 radeon_opt_set_context_reg(sctx
, R_028060_DB_DFSM_CONTROL
,
318 SI_TRACKED_DB_DFSM_CONTROL
,
319 S_028060_PUNCHOUT_MODE(V_028060_FORCE_OFF
) |
320 S_028060_POPS_DRAIN_PS_ON_OVERLAP(1));
323 void si_emit_dpbb_state(struct si_context
*sctx
)
325 struct si_screen
*sscreen
= sctx
->screen
;
326 struct si_state_blend
*blend
= sctx
->queued
.named
.blend
;
327 struct si_state_dsa
*dsa
= sctx
->queued
.named
.dsa
;
328 unsigned db_shader_control
= sctx
->ps_db_shader_control
;
330 assert(sctx
->chip_class
>= GFX9
);
332 if (!sscreen
->dpbb_allowed
|| !blend
|| !dsa
|| sctx
->dpbb_force_off
) {
333 si_emit_dpbb_disable(sctx
);
337 bool ps_can_kill
= G_02880C_KILL_ENABLE(db_shader_control
) ||
338 G_02880C_MASK_EXPORT_ENABLE(db_shader_control
) ||
339 G_02880C_COVERAGE_TO_MASK_ENABLE(db_shader_control
) ||
340 blend
->alpha_to_coverage
;
342 bool db_can_reject_z_trivially
=
343 !G_02880C_Z_EXPORT_ENABLE(db_shader_control
) ||
344 G_02880C_CONSERVATIVE_Z_EXPORT(db_shader_control
) ||
345 G_02880C_DEPTH_BEFORE_SHADER(db_shader_control
);
347 /* Disable DPBB when it's believed to be inefficient. */
349 db_can_reject_z_trivially
&&
350 sctx
->framebuffer
.state
.zsbuf
&&
352 si_emit_dpbb_disable(sctx
);
356 /* Compute the bin size. */
357 /* TODO: We could also look at enabled pixel shader outputs. */
358 unsigned cb_target_enabled_4bit
= sctx
->framebuffer
.colorbuf_enabled_4bit
&
359 blend
->cb_target_enabled_4bit
;
360 struct uvec2 color_bin_size
=
361 si_get_color_bin_size(sctx
, cb_target_enabled_4bit
);
362 struct uvec2 depth_bin_size
= si_get_depth_bin_size(sctx
);
364 unsigned color_area
= color_bin_size
.x
* color_bin_size
.y
;
365 unsigned depth_area
= depth_bin_size
.x
* depth_bin_size
.y
;
367 struct uvec2 bin_size
= color_area
< depth_area
? color_bin_size
370 if (!bin_size
.x
|| !bin_size
.y
) {
371 si_emit_dpbb_disable(sctx
);
375 /* Enable DFSM if it's preferred. */
376 unsigned punchout_mode
= V_028060_FORCE_OFF
;
377 bool disable_start_of_prim
= true;
378 bool zs_eqaa_dfsm_bug
= sctx
->chip_class
== GFX9
&&
379 sctx
->framebuffer
.state
.zsbuf
&&
380 sctx
->framebuffer
.nr_samples
!=
381 MAX2(1, sctx
->framebuffer
.state
.zsbuf
->texture
->nr_samples
);
383 if (sscreen
->dfsm_allowed
&&
385 cb_target_enabled_4bit
&&
386 !G_02880C_KILL_ENABLE(db_shader_control
) &&
387 /* These two also imply that DFSM is disabled when PS writes to memory. */
388 !G_02880C_EXEC_ON_HIER_FAIL(db_shader_control
) &&
389 !G_02880C_EXEC_ON_NOOP(db_shader_control
) &&
390 G_02880C_Z_ORDER(db_shader_control
) == V_02880C_EARLY_Z_THEN_LATE_Z
) {
391 punchout_mode
= V_028060_AUTO
;
392 disable_start_of_prim
= (cb_target_enabled_4bit
&
393 blend
->blend_enable_4bit
) != 0;
396 /* Tunable parameters. Also test with DFSM enabled/disabled. */
397 unsigned context_states_per_bin
; /* allowed range: [0, 5] */
398 unsigned persistent_states_per_bin
; /* allowed range: [0, 31] */
399 unsigned fpovs_per_batch
; /* allowed range: [0, 255], 0 = unlimited */
401 switch (sctx
->family
) {
405 /* Tuned for Raven. Vega might need different values. */
406 context_states_per_bin
= 5;
407 persistent_states_per_bin
= 31;
408 fpovs_per_batch
= 63;
414 /* Emit registers. */
415 struct uvec2 bin_size_extend
= {};
416 if (bin_size
.x
>= 32)
417 bin_size_extend
.x
= util_logbase2(bin_size
.x
) - 5;
418 if (bin_size
.y
>= 32)
419 bin_size_extend
.y
= util_logbase2(bin_size
.y
) - 5;
421 radeon_opt_set_context_reg(
422 sctx
, R_028C44_PA_SC_BINNER_CNTL_0
,
423 SI_TRACKED_PA_SC_BINNER_CNTL_0
,
424 S_028C44_BINNING_MODE(V_028C44_BINNING_ALLOWED
) |
425 S_028C44_BIN_SIZE_X(bin_size
.x
== 16) |
426 S_028C44_BIN_SIZE_Y(bin_size
.y
== 16) |
427 S_028C44_BIN_SIZE_X_EXTEND(bin_size_extend
.x
) |
428 S_028C44_BIN_SIZE_Y_EXTEND(bin_size_extend
.y
) |
429 S_028C44_CONTEXT_STATES_PER_BIN(context_states_per_bin
) |
430 S_028C44_PERSISTENT_STATES_PER_BIN(persistent_states_per_bin
) |
431 S_028C44_DISABLE_START_OF_PRIM(disable_start_of_prim
) |
432 S_028C44_FPOVS_PER_BATCH(fpovs_per_batch
) |
433 S_028C44_OPTIMAL_BIN_SELECTION(1));
434 radeon_opt_set_context_reg(sctx
, R_028060_DB_DFSM_CONTROL
,
435 SI_TRACKED_DB_DFSM_CONTROL
,
436 S_028060_PUNCHOUT_MODE(punchout_mode
) |
437 S_028060_POPS_DRAIN_PS_ON_OVERLAP(1));