Path: blob/21.2-virgl/src/gallium/drivers/radeonsi/si_state_binning.c
4570 views
/*1* Copyright 2017 Advanced Micro Devices, Inc.2* All Rights Reserved.3*4* Permission is hereby granted, free of charge, to any person obtaining a5* copy of this software and associated documentation files (the "Software"),6* to deal in the Software without restriction, including without limitation7* on the rights to use, copy, modify, merge, publish, distribute, sub8* license, and/or sell copies of the Software, and to permit persons to whom9* the Software is furnished to do so, subject to the following conditions:10*11* The above copyright notice and this permission notice (including the next12* paragraph) shall be included in all copies or substantial portions of the13* Software.14*15* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR16* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,17* FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL18* THE AUTHOR(S) AND/OR THEIR SUPPLIERS BE LIABLE FOR ANY CLAIM,19* DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR20* OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE21* USE OR OTHER DEALINGS IN THE SOFTWARE.22*/2324/* This file handles register programming of primitive binning. */2526#include "si_build_pm4.h"27#include "sid.h"2829struct uvec2 {30unsigned x, y;31};3233struct si_bin_size_map {34unsigned start;35unsigned bin_size_x;36unsigned bin_size_y;37};3839typedef struct si_bin_size_map si_bin_size_subtable[3][10];4041/* Find the bin size where sum is >= table[i].start and < table[i + 1].start. */42static struct uvec2 si_find_bin_size(struct si_screen *sscreen, const si_bin_size_subtable table[],43unsigned sum)44{45unsigned log_num_rb_per_se =46util_logbase2_ceil(sscreen->info.max_render_backends / sscreen->info.max_se);47unsigned log_num_se = util_logbase2_ceil(sscreen->info.max_se);48unsigned i;4950/* Get the chip-specific subtable. */51const struct si_bin_size_map *subtable = &table[log_num_rb_per_se][log_num_se][0];5253for (i = 0; subtable[i].bin_size_x != 0; i++) {54if (sum >= subtable[i].start && sum < subtable[i + 1].start)55break;56}5758struct uvec2 size = {subtable[i].bin_size_x, subtable[i].bin_size_y};59return size;60}6162static struct uvec2 si_get_color_bin_size(struct si_context *sctx, unsigned cb_target_enabled_4bit)63{64unsigned num_fragments = sctx->framebuffer.nr_color_samples;65unsigned sum = 0;6667/* Compute the sum of all Bpp. */68for (unsigned i = 0; i < sctx->framebuffer.state.nr_cbufs; i++) {69if (!(cb_target_enabled_4bit & (0xf << (i * 4))))70continue;7172struct si_texture *tex = (struct si_texture *)sctx->framebuffer.state.cbufs[i]->texture;73sum += tex->surface.bpe;74}7576/* Multiply the sum by some function of the number of samples. */77if (num_fragments >= 2) {78if (si_get_ps_iter_samples(sctx) >= 2)79sum *= num_fragments;80else81sum *= 2;82}8384static const si_bin_size_subtable table[] = {85{86/* One RB / SE */87{88/* One shader engine */89{0, 128, 128},90{1, 64, 128},91{2, 32, 128},92{3, 16, 128},93{17, 0, 0},94},95{96/* Two shader engines */97{0, 128, 128},98{2, 64, 128},99{3, 32, 128},100{5, 16, 128},101{17, 0, 0},102},103{104/* Four shader engines */105{0, 128, 128},106{3, 64, 128},107{5, 16, 128},108{17, 0, 0},109},110},111{112/* Two RB / SE */113{114/* One shader engine */115{0, 128, 128},116{2, 64, 128},117{3, 32, 128},118{9, 16, 128},119{33, 0, 0},120},121{122/* Two shader engines */123{0, 128, 128},124{3, 64, 128},125{5, 32, 128},126{9, 16, 128},127{33, 0, 0},128},129{130/* Four shader engines */131{0, 256, 256},132{2, 128, 256},133{3, 128, 128},134{5, 64, 128},135{9, 16, 128},136{33, 0, 0},137},138},139{140/* Four RB / SE */141{142/* One shader engine */143{0, 128, 256},144{2, 128, 128},145{3, 64, 128},146{5, 32, 128},147{9, 16, 128},148{17, 0, 0},149},150{151/* Two shader engines */152{0, 256, 256},153{2, 128, 256},154{3, 128, 128},155{5, 64, 128},156{9, 32, 128},157{17, 16, 128},158{33, 0, 0},159},160{161/* Four shader engines */162{0, 256, 512},163{2, 128, 512},164{3, 64, 512},165{5, 32, 512},166{9, 32, 256},167{17, 32, 128},168{33, 0, 0},169},170},171};172173return si_find_bin_size(sctx->screen, table, sum);174}175176static struct uvec2 si_get_depth_bin_size(struct si_context *sctx)177{178struct si_state_dsa *dsa = sctx->queued.named.dsa;179180if (!sctx->framebuffer.state.zsbuf || (!dsa->depth_enabled && !dsa->stencil_enabled)) {181/* Return the max size. */182struct uvec2 size = {512, 512};183return size;184}185186struct si_texture *tex = (struct si_texture *)sctx->framebuffer.state.zsbuf->texture;187unsigned depth_coeff = dsa->depth_enabled ? 5 : 0;188unsigned stencil_coeff = tex->surface.has_stencil && dsa->stencil_enabled ? 1 : 0;189unsigned sum = 4 * (depth_coeff + stencil_coeff) * MAX2(tex->buffer.b.b.nr_samples, 1);190191static const si_bin_size_subtable table[] = {192{193// One RB / SE194{195// One shader engine196{0, 64, 512},197{2, 64, 256},198{4, 64, 128},199{7, 32, 128},200{13, 16, 128},201{49, 0, 0},202},203{204// Two shader engines205{0, 128, 512},206{2, 64, 512},207{4, 64, 256},208{7, 64, 128},209{13, 32, 128},210{25, 16, 128},211{49, 0, 0},212},213{214// Four shader engines215{0, 256, 512},216{2, 128, 512},217{4, 64, 512},218{7, 64, 256},219{13, 64, 128},220{25, 16, 128},221{49, 0, 0},222},223},224{225// Two RB / SE226{227// One shader engine228{0, 128, 512},229{2, 64, 512},230{4, 64, 256},231{7, 64, 128},232{13, 32, 128},233{25, 16, 128},234{97, 0, 0},235},236{237// Two shader engines238{0, 256, 512},239{2, 128, 512},240{4, 64, 512},241{7, 64, 256},242{13, 64, 128},243{25, 32, 128},244{49, 16, 128},245{97, 0, 0},246},247{248// Four shader engines249{0, 512, 512},250{2, 256, 512},251{4, 128, 512},252{7, 64, 512},253{13, 64, 256},254{25, 64, 128},255{49, 16, 128},256{97, 0, 0},257},258},259{260// Four RB / SE261{262// One shader engine263{0, 256, 512},264{2, 128, 512},265{4, 64, 512},266{7, 64, 256},267{13, 64, 128},268{25, 32, 128},269{49, 16, 128},270{193, 0, 0},271},272{273// Two shader engines274{0, 512, 512},275{2, 256, 512},276{4, 128, 512},277{7, 64, 512},278{13, 64, 256},279{25, 64, 128},280{49, 32, 128},281{97, 16, 128},282{193, 0, 0},283},284{285// Four shader engines286{0, 512, 512},287{4, 256, 512},288{7, 128, 512},289{13, 64, 512},290{25, 32, 512},291{49, 32, 256},292{97, 16, 128},293{193, 0, 0},294},295},296};297298return si_find_bin_size(sctx->screen, table, sum);299}300301static void gfx10_get_bin_sizes(struct si_context *sctx, unsigned cb_target_enabled_4bit,302struct uvec2 *color_bin_size, struct uvec2 *depth_bin_size)303{304const unsigned ZsTagSize = 64;305const unsigned ZsNumTags = 312;306const unsigned CcTagSize = 1024;307const unsigned CcReadTags = 31;308const unsigned FcTagSize = 256;309const unsigned FcReadTags = 44;310311const unsigned num_rbs = sctx->screen->info.max_render_backends;312const unsigned num_pipes = MAX2(num_rbs, sctx->screen->info.num_tcc_blocks);313314const unsigned depthBinSizeTagPart =315((ZsNumTags * num_rbs / num_pipes) * (ZsTagSize * num_pipes));316const unsigned colorBinSizeTagPart =317((CcReadTags * num_rbs / num_pipes) * (CcTagSize * num_pipes));318const unsigned fmaskBinSizeTagPart =319((FcReadTags * num_rbs / num_pipes) * (FcTagSize * num_pipes));320321const unsigned minBinSizeX = 128;322const unsigned minBinSizeY = 64;323324const unsigned num_fragments = sctx->framebuffer.nr_color_samples;325const unsigned num_samples = sctx->framebuffer.nr_samples;326const bool ps_iter_sample = si_get_ps_iter_samples(sctx) >= 2;327328/* Calculate cColor and cFmask(if applicable) */329unsigned cColor = 0;330unsigned cFmask = 0;331bool has_fmask = false;332333for (unsigned i = 0; i < sctx->framebuffer.state.nr_cbufs; i++) {334if (!sctx->framebuffer.state.cbufs[i])335continue;336337struct si_texture *tex = (struct si_texture *)sctx->framebuffer.state.cbufs[i]->texture;338const unsigned mmrt = num_fragments == 1 ? 1 : (ps_iter_sample ? num_fragments : 2);339340cColor += tex->surface.bpe * mmrt;341if (num_samples >= 2 /* if FMASK is bound */) {342const unsigned fragmentsLog2 = util_logbase2(num_fragments);343const unsigned samplesLog2 = util_logbase2(num_samples);344345static const unsigned cFmaskMrt[4 /* fragments */][5 /* samples */] = {346{0, 1, 1, 1, 2}, /* fragments = 1 */347{0, 1, 1, 2, 4}, /* fragments = 2 */348{0, 1, 1, 4, 8}, /* fragments = 4 */349{0, 1, 2, 4, 8} /* fragments = 8 */350};351cFmask += cFmaskMrt[fragmentsLog2][samplesLog2];352has_fmask = true;353}354}355cColor = MAX2(cColor, 1u);356357const unsigned colorLog2Pixels = util_logbase2(colorBinSizeTagPart / cColor);358const unsigned colorBinSizeX = 1 << ((colorLog2Pixels + 1) / 2); /* round up width */359const unsigned colorBinSizeY = 1 << (colorLog2Pixels / 2); /* round down height */360361unsigned binSizeX = colorBinSizeX;362unsigned binSizeY = colorBinSizeY;363364if (has_fmask) {365cFmask = MAX2(cFmask, 1u);366367const unsigned fmaskLog2Pixels = util_logbase2(fmaskBinSizeTagPart / cFmask);368const unsigned fmaskBinSizeX = 1 << ((fmaskLog2Pixels + 1) / 2); /* round up width */369const unsigned fmaskBinSizeY = 1 << (fmaskLog2Pixels / 2); /* round down height */370371/* use the smaller of the Color vs. Fmask bin sizes */372if (fmaskLog2Pixels < colorLog2Pixels) {373binSizeX = fmaskBinSizeX;374binSizeY = fmaskBinSizeY;375}376}377378/* Return size adjusted for minimum bin size */379color_bin_size->x = MAX2(binSizeX, minBinSizeX);380color_bin_size->y = MAX2(binSizeY, minBinSizeY);381382if (!sctx->framebuffer.state.zsbuf) {383/* Set to max sizes when no depth buffer is bound. */384depth_bin_size->x = 512;385depth_bin_size->y = 512;386} else {387struct si_texture *zstex = (struct si_texture *)sctx->framebuffer.state.zsbuf->texture;388struct si_state_dsa *dsa = sctx->queued.named.dsa;389390const unsigned cPerDepthSample = dsa->depth_enabled ? 5 : 0;391const unsigned cPerStencilSample = dsa->stencil_enabled ? 1 : 0;392const unsigned cDepth =393(cPerDepthSample + cPerStencilSample) * MAX2(zstex->buffer.b.b.nr_samples, 1);394395const unsigned depthLog2Pixels = util_logbase2(depthBinSizeTagPart / MAX2(cDepth, 1u));396unsigned depthBinSizeX = 1 << ((depthLog2Pixels + 1) / 2);397unsigned depthBinSizeY = 1 << (depthLog2Pixels / 2);398399depth_bin_size->x = MAX2(depthBinSizeX, minBinSizeX);400depth_bin_size->y = MAX2(depthBinSizeY, minBinSizeY);401}402}403404static void si_emit_dpbb_disable(struct si_context *sctx)405{406radeon_begin(&sctx->gfx_cs);407408if (sctx->chip_class >= GFX10) {409struct uvec2 bin_size = {};410struct uvec2 bin_size_extend = {};411412bin_size.x = 128;413bin_size.y = sctx->framebuffer.min_bytes_per_pixel <= 4 ? 128 : 64;414415if (bin_size.x >= 32)416bin_size_extend.x = util_logbase2(bin_size.x) - 5;417if (bin_size.y >= 32)418bin_size_extend.y = util_logbase2(bin_size.y) - 5;419420radeon_opt_set_context_reg(421sctx, R_028C44_PA_SC_BINNER_CNTL_0, SI_TRACKED_PA_SC_BINNER_CNTL_0,422S_028C44_BINNING_MODE(V_028C44_DISABLE_BINNING_USE_NEW_SC) |423S_028C44_BIN_SIZE_X(bin_size.x == 16) | S_028C44_BIN_SIZE_Y(bin_size.y == 16) |424S_028C44_BIN_SIZE_X_EXTEND(bin_size_extend.x) |425S_028C44_BIN_SIZE_Y_EXTEND(bin_size_extend.y) | S_028C44_DISABLE_START_OF_PRIM(1) |426S_028C44_FLUSH_ON_BINNING_TRANSITION(sctx->last_binning_enabled != 0));427} else {428radeon_opt_set_context_reg(429sctx, R_028C44_PA_SC_BINNER_CNTL_0, SI_TRACKED_PA_SC_BINNER_CNTL_0,430S_028C44_BINNING_MODE(V_028C44_DISABLE_BINNING_USE_LEGACY_SC) |431S_028C44_DISABLE_START_OF_PRIM(1) |432S_028C44_FLUSH_ON_BINNING_TRANSITION((sctx->family == CHIP_VEGA12 ||433sctx->family == CHIP_VEGA20 ||434sctx->family >= CHIP_RAVEN2) &&435sctx->last_binning_enabled != 0));436}437radeon_end_update_context_roll(sctx);438439sctx->last_binning_enabled = false;440}441442void si_emit_dpbb_state(struct si_context *sctx)443{444struct si_screen *sscreen = sctx->screen;445struct si_state_blend *blend = sctx->queued.named.blend;446struct si_state_dsa *dsa = sctx->queued.named.dsa;447unsigned db_shader_control = sctx->ps_db_shader_control;448449assert(sctx->chip_class >= GFX9);450451if (!sscreen->dpbb_allowed || sctx->dpbb_force_off) {452si_emit_dpbb_disable(sctx);453return;454}455456bool ps_can_kill =457G_02880C_KILL_ENABLE(db_shader_control) || G_02880C_MASK_EXPORT_ENABLE(db_shader_control) ||458G_02880C_COVERAGE_TO_MASK_ENABLE(db_shader_control) || blend->alpha_to_coverage;459460bool db_can_reject_z_trivially = !G_02880C_Z_EXPORT_ENABLE(db_shader_control) ||461G_02880C_CONSERVATIVE_Z_EXPORT(db_shader_control) ||462G_02880C_DEPTH_BEFORE_SHADER(db_shader_control);463464/* Disable DPBB when it's believed to be inefficient. */465if (sscreen->info.max_render_backends > 4 && ps_can_kill && db_can_reject_z_trivially &&466sctx->framebuffer.state.zsbuf && dsa->db_can_write) {467si_emit_dpbb_disable(sctx);468return;469}470471/* Compute the bin size. */472/* TODO: We could also look at enabled pixel shader outputs. */473unsigned cb_target_enabled_4bit =474sctx->framebuffer.colorbuf_enabled_4bit & blend->cb_target_enabled_4bit;475struct uvec2 color_bin_size, depth_bin_size;476477if (sctx->chip_class >= GFX10) {478gfx10_get_bin_sizes(sctx, cb_target_enabled_4bit, &color_bin_size, &depth_bin_size);479} else {480color_bin_size = si_get_color_bin_size(sctx, cb_target_enabled_4bit);481depth_bin_size = si_get_depth_bin_size(sctx);482}483484unsigned color_area = color_bin_size.x * color_bin_size.y;485unsigned depth_area = depth_bin_size.x * depth_bin_size.y;486487struct uvec2 bin_size = color_area < depth_area ? color_bin_size : depth_bin_size;488489if (!bin_size.x || !bin_size.y) {490si_emit_dpbb_disable(sctx);491return;492}493494/* Tunable parameters. */495unsigned fpovs_per_batch = 63; /* allowed range: [0, 255], 0 = unlimited */496497/* Emit registers. */498struct uvec2 bin_size_extend = {};499if (bin_size.x >= 32)500bin_size_extend.x = util_logbase2(bin_size.x) - 5;501if (bin_size.y >= 32)502bin_size_extend.y = util_logbase2(bin_size.y) - 5;503504radeon_begin(&sctx->gfx_cs);505radeon_opt_set_context_reg(506sctx, R_028C44_PA_SC_BINNER_CNTL_0, SI_TRACKED_PA_SC_BINNER_CNTL_0,507S_028C44_BINNING_MODE(V_028C44_BINNING_ALLOWED) | S_028C44_BIN_SIZE_X(bin_size.x == 16) |508S_028C44_BIN_SIZE_Y(bin_size.y == 16) | S_028C44_BIN_SIZE_X_EXTEND(bin_size_extend.x) |509S_028C44_BIN_SIZE_Y_EXTEND(bin_size_extend.y) |510S_028C44_CONTEXT_STATES_PER_BIN(sscreen->pbb_context_states_per_bin - 1) |511S_028C44_PERSISTENT_STATES_PER_BIN(sscreen->pbb_persistent_states_per_bin - 1) |512S_028C44_DISABLE_START_OF_PRIM(1) |513S_028C44_FPOVS_PER_BATCH(fpovs_per_batch) | S_028C44_OPTIMAL_BIN_SELECTION(1) |514S_028C44_FLUSH_ON_BINNING_TRANSITION((sctx->family == CHIP_VEGA12 ||515sctx->family == CHIP_VEGA20 ||516sctx->family >= CHIP_RAVEN2) &&517sctx->last_binning_enabled != 1));518radeon_end_update_context_roll(sctx);519520sctx->last_binning_enabled = true;521}522523524