1bf215546Sopenharmony_ci/* 2bf215546Sopenharmony_ci * Copyright 2012 Advanced Micro Devices, Inc. 3bf215546Sopenharmony_ci * All Rights Reserved. 4bf215546Sopenharmony_ci * 5bf215546Sopenharmony_ci * Permission is hereby granted, free of charge, to any person obtaining a 6bf215546Sopenharmony_ci * copy of this software and associated documentation files (the "Software"), 7bf215546Sopenharmony_ci * to deal in the Software without restriction, including without limitation 8bf215546Sopenharmony_ci * on the rights to use, copy, modify, merge, publish, distribute, sub 9bf215546Sopenharmony_ci * license, and/or sell copies of the Software, and to permit persons to whom 10bf215546Sopenharmony_ci * the Software is furnished to do so, subject to the following conditions: 11bf215546Sopenharmony_ci * 12bf215546Sopenharmony_ci * The above copyright notice and this permission notice (including the next 13bf215546Sopenharmony_ci * paragraph) shall be included in all copies or substantial portions of the 14bf215546Sopenharmony_ci * Software. 15bf215546Sopenharmony_ci * 16bf215546Sopenharmony_ci * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17bf215546Sopenharmony_ci * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18bf215546Sopenharmony_ci * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL 19bf215546Sopenharmony_ci * THE AUTHOR(S) AND/OR THEIR SUPPLIERS BE LIABLE FOR ANY CLAIM, 20bf215546Sopenharmony_ci * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR 21bf215546Sopenharmony_ci * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE 22bf215546Sopenharmony_ci * USE OR OTHER DEALINGS IN THE SOFTWARE. 23bf215546Sopenharmony_ci */ 24bf215546Sopenharmony_ci 25bf215546Sopenharmony_ci#include "ac_nir.h" 26bf215546Sopenharmony_ci#include "ac_shader_util.h" 27bf215546Sopenharmony_ci#include "compiler/nir/nir_serialize.h" 28bf215546Sopenharmony_ci#include "nir/tgsi_to_nir.h" 29bf215546Sopenharmony_ci#include "si_build_pm4.h" 30bf215546Sopenharmony_ci#include "sid.h" 31bf215546Sopenharmony_ci#include "util/crc32.h" 32bf215546Sopenharmony_ci#include "util/disk_cache.h" 33bf215546Sopenharmony_ci#include "util/hash_table.h" 34bf215546Sopenharmony_ci#include "util/mesa-sha1.h" 35bf215546Sopenharmony_ci#include "util/u_async_debug.h" 36bf215546Sopenharmony_ci#include "util/u_memory.h" 37bf215546Sopenharmony_ci#include "util/u_prim.h" 38bf215546Sopenharmony_ci#include "tgsi/tgsi_from_mesa.h" 39bf215546Sopenharmony_ci 40bf215546Sopenharmony_ciunsigned si_determine_wave_size(struct si_screen *sscreen, struct si_shader *shader) 41bf215546Sopenharmony_ci{ 42bf215546Sopenharmony_ci /* There are a few uses that pass shader=NULL here, expecting the default compute wave size. */ 43bf215546Sopenharmony_ci struct si_shader_info *info = shader ? &shader->selector->info : NULL; 44bf215546Sopenharmony_ci gl_shader_stage stage = shader ? shader->selector->stage : MESA_SHADER_COMPUTE; 45bf215546Sopenharmony_ci 46bf215546Sopenharmony_ci if (sscreen->info.gfx_level < GFX10) 47bf215546Sopenharmony_ci return 64; 48bf215546Sopenharmony_ci 49bf215546Sopenharmony_ci /* Legacy GS only supports Wave64. */ 50bf215546Sopenharmony_ci if ((stage == MESA_SHADER_VERTEX && shader->key.ge.as_es && !shader->key.ge.as_ngg) || 51bf215546Sopenharmony_ci (stage == MESA_SHADER_TESS_EVAL && shader->key.ge.as_es && !shader->key.ge.as_ngg) || 52bf215546Sopenharmony_ci (stage == MESA_SHADER_GEOMETRY && !shader->key.ge.as_ngg)) 53bf215546Sopenharmony_ci return 64; 54bf215546Sopenharmony_ci 55bf215546Sopenharmony_ci /* Small workgroups use Wave32 unconditionally. */ 56bf215546Sopenharmony_ci if (stage == MESA_SHADER_COMPUTE && info && 57bf215546Sopenharmony_ci !info->base.workgroup_size_variable && 58bf215546Sopenharmony_ci info->base.workgroup_size[0] * 59bf215546Sopenharmony_ci info->base.workgroup_size[1] * 60bf215546Sopenharmony_ci info->base.workgroup_size[2] <= 32) 61bf215546Sopenharmony_ci return 32; 62bf215546Sopenharmony_ci 63bf215546Sopenharmony_ci /* Debug flags. */ 64bf215546Sopenharmony_ci unsigned dbg_wave_size = 0; 65bf215546Sopenharmony_ci if (sscreen->debug_flags & 66bf215546Sopenharmony_ci (stage == MESA_SHADER_COMPUTE ? DBG(W32_CS) : 67bf215546Sopenharmony_ci stage == MESA_SHADER_FRAGMENT ? DBG(W32_PS) | DBG(W32_PS_DISCARD) : DBG(W32_GE))) 68bf215546Sopenharmony_ci dbg_wave_size = 32; 69bf215546Sopenharmony_ci 70bf215546Sopenharmony_ci if (sscreen->debug_flags & 71bf215546Sopenharmony_ci (stage == MESA_SHADER_COMPUTE ? DBG(W64_CS) : 72bf215546Sopenharmony_ci stage == MESA_SHADER_FRAGMENT ? DBG(W64_PS) : DBG(W64_GE))) { 73bf215546Sopenharmony_ci assert(!dbg_wave_size); 74bf215546Sopenharmony_ci dbg_wave_size = 64; 75bf215546Sopenharmony_ci } 76bf215546Sopenharmony_ci 77bf215546Sopenharmony_ci /* Shader profiles. */ 78bf215546Sopenharmony_ci unsigned profile_wave_size = 0; 79bf215546Sopenharmony_ci if (info && info->options & SI_PROFILE_WAVE32) 80bf215546Sopenharmony_ci profile_wave_size = 32; 81bf215546Sopenharmony_ci 82bf215546Sopenharmony_ci if (info && info->options & SI_PROFILE_WAVE64) { 83bf215546Sopenharmony_ci assert(!profile_wave_size); 84bf215546Sopenharmony_ci profile_wave_size = 64; 85bf215546Sopenharmony_ci } 86bf215546Sopenharmony_ci 87bf215546Sopenharmony_ci if (profile_wave_size) { 88bf215546Sopenharmony_ci /* Only debug flags override shader profiles. */ 89bf215546Sopenharmony_ci if (dbg_wave_size) 90bf215546Sopenharmony_ci return dbg_wave_size; 91bf215546Sopenharmony_ci 92bf215546Sopenharmony_ci return profile_wave_size; 93bf215546Sopenharmony_ci } 94bf215546Sopenharmony_ci 95bf215546Sopenharmony_ci /* LLVM 13 has a bug that causes compile failures with discard in Wave32 96bf215546Sopenharmony_ci * in some cases. Alpha test in Wave32 is luckily unaffected. 97bf215546Sopenharmony_ci */ 98bf215546Sopenharmony_ci if (stage == MESA_SHADER_FRAGMENT && info->base.fs.uses_discard && 99bf215546Sopenharmony_ci !(info && info->options & SI_PROFILE_IGNORE_LLVM13_DISCARD_BUG) && 100bf215546Sopenharmony_ci LLVM_VERSION_MAJOR == 13 && !(sscreen->debug_flags & DBG(W32_PS_DISCARD))) 101bf215546Sopenharmony_ci return 64; 102bf215546Sopenharmony_ci 103bf215546Sopenharmony_ci /* Debug flags except w32psdiscard don't override the discard bug workaround, 104bf215546Sopenharmony_ci * but they override everything else. 105bf215546Sopenharmony_ci */ 106bf215546Sopenharmony_ci if (dbg_wave_size) 107bf215546Sopenharmony_ci return dbg_wave_size; 108bf215546Sopenharmony_ci 109bf215546Sopenharmony_ci /* Pixel shaders without interp instructions don't suffer from reduced interpolation 110bf215546Sopenharmony_ci * performance in Wave32, so use Wave32. This helps Piano and Voloplosion. 111bf215546Sopenharmony_ci */ 112bf215546Sopenharmony_ci if (stage == MESA_SHADER_FRAGMENT && !info->num_inputs) 113bf215546Sopenharmony_ci return 32; 114bf215546Sopenharmony_ci 115bf215546Sopenharmony_ci /* There are a few very rare cases where VS is better with Wave32, and there are no known 116bf215546Sopenharmony_ci * cases where Wave64 is better. 117bf215546Sopenharmony_ci * Wave32 is disabled for GFX10 when culling is active as a workaround for #6457. I don't 118bf215546Sopenharmony_ci * know why this helps. 119bf215546Sopenharmony_ci */ 120bf215546Sopenharmony_ci if (stage <= MESA_SHADER_GEOMETRY && 121bf215546Sopenharmony_ci !(sscreen->info.gfx_level == GFX10 && shader && shader->key.ge.opt.ngg_culling)) 122bf215546Sopenharmony_ci return 32; 123bf215546Sopenharmony_ci 124bf215546Sopenharmony_ci /* TODO: Merged shaders must use the same wave size because the driver doesn't recompile 125bf215546Sopenharmony_ci * individual shaders of merged shaders to match the wave size between them. 126bf215546Sopenharmony_ci */ 127bf215546Sopenharmony_ci bool merged_shader = stage <= MESA_SHADER_GEOMETRY && shader && !shader->is_gs_copy_shader && 128bf215546Sopenharmony_ci (shader->key.ge.as_ls || shader->key.ge.as_es || 129bf215546Sopenharmony_ci stage == MESA_SHADER_TESS_CTRL || stage == MESA_SHADER_GEOMETRY); 130bf215546Sopenharmony_ci 131bf215546Sopenharmony_ci /* Divergent loops in Wave64 can end up having too many iterations in one half of the wave 132bf215546Sopenharmony_ci * while the other half is idling but occupying VGPRs, preventing other waves from launching. 133bf215546Sopenharmony_ci * Wave32 eliminates the idling half to allow the next wave to start. 134bf215546Sopenharmony_ci */ 135bf215546Sopenharmony_ci if (!merged_shader && info && info->has_divergent_loop) 136bf215546Sopenharmony_ci return 32; 137bf215546Sopenharmony_ci 138bf215546Sopenharmony_ci return 64; 139bf215546Sopenharmony_ci} 140bf215546Sopenharmony_ci 141bf215546Sopenharmony_ci/* SHADER_CACHE */ 142bf215546Sopenharmony_ci 143bf215546Sopenharmony_ci/** 144bf215546Sopenharmony_ci * Return the IR key for the shader cache. 145bf215546Sopenharmony_ci */ 146bf215546Sopenharmony_civoid si_get_ir_cache_key(struct si_shader_selector *sel, bool ngg, bool es, 147bf215546Sopenharmony_ci unsigned wave_size, unsigned char ir_sha1_cache_key[20]) 148bf215546Sopenharmony_ci{ 149bf215546Sopenharmony_ci struct blob blob = {}; 150bf215546Sopenharmony_ci unsigned ir_size; 151bf215546Sopenharmony_ci void *ir_binary; 152bf215546Sopenharmony_ci 153bf215546Sopenharmony_ci if (sel->nir_binary) { 154bf215546Sopenharmony_ci ir_binary = sel->nir_binary; 155bf215546Sopenharmony_ci ir_size = sel->nir_size; 156bf215546Sopenharmony_ci } else { 157bf215546Sopenharmony_ci assert(sel->nir); 158bf215546Sopenharmony_ci 159bf215546Sopenharmony_ci blob_init(&blob); 160bf215546Sopenharmony_ci /* Keep debug info if NIR debug prints are in use. */ 161bf215546Sopenharmony_ci nir_serialize(&blob, sel->nir, NIR_DEBUG(PRINT) == 0); 162bf215546Sopenharmony_ci ir_binary = blob.data; 163bf215546Sopenharmony_ci ir_size = blob.size; 164bf215546Sopenharmony_ci } 165bf215546Sopenharmony_ci 166bf215546Sopenharmony_ci /* These settings affect the compilation, but they are not derived 167bf215546Sopenharmony_ci * from the input shader IR. 168bf215546Sopenharmony_ci */ 169bf215546Sopenharmony_ci unsigned shader_variant_flags = 0; 170bf215546Sopenharmony_ci 171bf215546Sopenharmony_ci if (ngg) 172bf215546Sopenharmony_ci shader_variant_flags |= 1 << 0; 173bf215546Sopenharmony_ci if (sel->nir) 174bf215546Sopenharmony_ci shader_variant_flags |= 1 << 1; 175bf215546Sopenharmony_ci if (wave_size == 32) 176bf215546Sopenharmony_ci shader_variant_flags |= 1 << 2; 177bf215546Sopenharmony_ci if (sel->stage == MESA_SHADER_FRAGMENT && 178bf215546Sopenharmony_ci /* Derivatives imply helper invocations so check for needs_quad_helper_invocations. */ 179bf215546Sopenharmony_ci sel->info.base.fs.needs_quad_helper_invocations && 180bf215546Sopenharmony_ci sel->info.base.fs.uses_discard && 181bf215546Sopenharmony_ci sel->screen->debug_flags & DBG(FS_CORRECT_DERIVS_AFTER_KILL)) 182bf215546Sopenharmony_ci shader_variant_flags |= 1 << 3; 183bf215546Sopenharmony_ci /* use_ngg_culling disables NGG passthrough for non-culling shaders to reduce context 184bf215546Sopenharmony_ci * rolls, which can be changed with AMD_DEBUG=nonggc or AMD_DEBUG=nggc. 185bf215546Sopenharmony_ci */ 186bf215546Sopenharmony_ci if (sel->screen->use_ngg_culling) 187bf215546Sopenharmony_ci shader_variant_flags |= 1 << 4; 188bf215546Sopenharmony_ci if (sel->screen->record_llvm_ir) 189bf215546Sopenharmony_ci shader_variant_flags |= 1 << 5; 190bf215546Sopenharmony_ci 191bf215546Sopenharmony_ci /* bit gap */ 192bf215546Sopenharmony_ci 193bf215546Sopenharmony_ci if (sel->screen->options.no_infinite_interp) 194bf215546Sopenharmony_ci shader_variant_flags |= 1 << 7; 195bf215546Sopenharmony_ci if (sel->screen->options.clamp_div_by_zero) 196bf215546Sopenharmony_ci shader_variant_flags |= 1 << 8; 197bf215546Sopenharmony_ci if ((sel->stage == MESA_SHADER_VERTEX || 198bf215546Sopenharmony_ci sel->stage == MESA_SHADER_TESS_EVAL || 199bf215546Sopenharmony_ci sel->stage == MESA_SHADER_GEOMETRY) && 200bf215546Sopenharmony_ci !es && 201bf215546Sopenharmony_ci sel->screen->options.vrs2x2) 202bf215546Sopenharmony_ci shader_variant_flags |= 1 << 10; 203bf215546Sopenharmony_ci if (sel->screen->options.inline_uniforms) 204bf215546Sopenharmony_ci shader_variant_flags |= 1 << 11; 205bf215546Sopenharmony_ci 206bf215546Sopenharmony_ci struct mesa_sha1 ctx; 207bf215546Sopenharmony_ci _mesa_sha1_init(&ctx); 208bf215546Sopenharmony_ci _mesa_sha1_update(&ctx, &shader_variant_flags, 4); 209bf215546Sopenharmony_ci _mesa_sha1_update(&ctx, ir_binary, ir_size); 210bf215546Sopenharmony_ci _mesa_sha1_final(&ctx, ir_sha1_cache_key); 211bf215546Sopenharmony_ci 212bf215546Sopenharmony_ci if (ir_binary == blob.data) 213bf215546Sopenharmony_ci blob_finish(&blob); 214bf215546Sopenharmony_ci} 215bf215546Sopenharmony_ci 216bf215546Sopenharmony_ci/** Copy "data" to "ptr" and return the next dword following copied data. */ 217bf215546Sopenharmony_cistatic uint32_t *write_data(uint32_t *ptr, const void *data, unsigned size) 218bf215546Sopenharmony_ci{ 219bf215546Sopenharmony_ci /* data may be NULL if size == 0 */ 220bf215546Sopenharmony_ci if (size) 221bf215546Sopenharmony_ci memcpy(ptr, data, size); 222bf215546Sopenharmony_ci ptr += DIV_ROUND_UP(size, 4); 223bf215546Sopenharmony_ci return ptr; 224bf215546Sopenharmony_ci} 225bf215546Sopenharmony_ci 226bf215546Sopenharmony_ci/** Read data from "ptr". Return the next dword following the data. */ 227bf215546Sopenharmony_cistatic uint32_t *read_data(uint32_t *ptr, void *data, unsigned size) 228bf215546Sopenharmony_ci{ 229bf215546Sopenharmony_ci memcpy(data, ptr, size); 230bf215546Sopenharmony_ci ptr += DIV_ROUND_UP(size, 4); 231bf215546Sopenharmony_ci return ptr; 232bf215546Sopenharmony_ci} 233bf215546Sopenharmony_ci 234bf215546Sopenharmony_ci/** 235bf215546Sopenharmony_ci * Write the size as uint followed by the data. Return the next dword 236bf215546Sopenharmony_ci * following the copied data. 237bf215546Sopenharmony_ci */ 238bf215546Sopenharmony_cistatic uint32_t *write_chunk(uint32_t *ptr, const void *data, unsigned size) 239bf215546Sopenharmony_ci{ 240bf215546Sopenharmony_ci *ptr++ = size; 241bf215546Sopenharmony_ci return write_data(ptr, data, size); 242bf215546Sopenharmony_ci} 243bf215546Sopenharmony_ci 244bf215546Sopenharmony_ci/** 245bf215546Sopenharmony_ci * Read the size as uint followed by the data. Return both via parameters. 246bf215546Sopenharmony_ci * Return the next dword following the data. 247bf215546Sopenharmony_ci */ 248bf215546Sopenharmony_cistatic uint32_t *read_chunk(uint32_t *ptr, void **data, unsigned *size) 249bf215546Sopenharmony_ci{ 250bf215546Sopenharmony_ci *size = *ptr++; 251bf215546Sopenharmony_ci assert(*data == NULL); 252bf215546Sopenharmony_ci if (!*size) 253bf215546Sopenharmony_ci return ptr; 254bf215546Sopenharmony_ci *data = malloc(*size); 255bf215546Sopenharmony_ci return read_data(ptr, *data, *size); 256bf215546Sopenharmony_ci} 257bf215546Sopenharmony_ci 258bf215546Sopenharmony_ci/** 259bf215546Sopenharmony_ci * Return the shader binary in a buffer. The first 4 bytes contain its size 260bf215546Sopenharmony_ci * as integer. 261bf215546Sopenharmony_ci */ 262bf215546Sopenharmony_cistatic uint32_t *si_get_shader_binary(struct si_shader *shader) 263bf215546Sopenharmony_ci{ 264bf215546Sopenharmony_ci /* There is always a size of data followed by the data itself. */ 265bf215546Sopenharmony_ci unsigned llvm_ir_size = 266bf215546Sopenharmony_ci shader->binary.llvm_ir_string ? strlen(shader->binary.llvm_ir_string) + 1 : 0; 267bf215546Sopenharmony_ci 268bf215546Sopenharmony_ci /* Refuse to allocate overly large buffers and guard against integer 269bf215546Sopenharmony_ci * overflow. */ 270bf215546Sopenharmony_ci if (shader->binary.elf_size > UINT_MAX / 4 || llvm_ir_size > UINT_MAX / 4) 271bf215546Sopenharmony_ci return NULL; 272bf215546Sopenharmony_ci 273bf215546Sopenharmony_ci unsigned size = 4 + /* total size */ 274bf215546Sopenharmony_ci 4 + /* CRC32 of the data below */ 275bf215546Sopenharmony_ci align(sizeof(shader->config), 4) + align(sizeof(shader->info), 4) + 4 + 276bf215546Sopenharmony_ci align(shader->binary.elf_size, 4) + 4 + align(llvm_ir_size, 4); 277bf215546Sopenharmony_ci uint32_t *buffer = (uint32_t*)CALLOC(1, size); 278bf215546Sopenharmony_ci uint32_t *ptr = buffer; 279bf215546Sopenharmony_ci 280bf215546Sopenharmony_ci if (!buffer) 281bf215546Sopenharmony_ci return NULL; 282bf215546Sopenharmony_ci 283bf215546Sopenharmony_ci *ptr++ = size; 284bf215546Sopenharmony_ci ptr++; /* CRC32 is calculated at the end. */ 285bf215546Sopenharmony_ci 286bf215546Sopenharmony_ci ptr = write_data(ptr, &shader->config, sizeof(shader->config)); 287bf215546Sopenharmony_ci ptr = write_data(ptr, &shader->info, sizeof(shader->info)); 288bf215546Sopenharmony_ci ptr = write_chunk(ptr, shader->binary.elf_buffer, shader->binary.elf_size); 289bf215546Sopenharmony_ci ptr = write_chunk(ptr, shader->binary.llvm_ir_string, llvm_ir_size); 290bf215546Sopenharmony_ci assert((char *)ptr - (char *)buffer == (ptrdiff_t)size); 291bf215546Sopenharmony_ci 292bf215546Sopenharmony_ci /* Compute CRC32. */ 293bf215546Sopenharmony_ci ptr = buffer; 294bf215546Sopenharmony_ci ptr++; 295bf215546Sopenharmony_ci *ptr = util_hash_crc32(ptr + 1, size - 8); 296bf215546Sopenharmony_ci 297bf215546Sopenharmony_ci return buffer; 298bf215546Sopenharmony_ci} 299bf215546Sopenharmony_ci 300bf215546Sopenharmony_cistatic bool si_load_shader_binary(struct si_shader *shader, void *binary) 301bf215546Sopenharmony_ci{ 302bf215546Sopenharmony_ci uint32_t *ptr = (uint32_t *)binary; 303bf215546Sopenharmony_ci uint32_t size = *ptr++; 304bf215546Sopenharmony_ci uint32_t crc32 = *ptr++; 305bf215546Sopenharmony_ci unsigned chunk_size; 306bf215546Sopenharmony_ci unsigned elf_size; 307bf215546Sopenharmony_ci 308bf215546Sopenharmony_ci if (util_hash_crc32(ptr, size - 8) != crc32) { 309bf215546Sopenharmony_ci fprintf(stderr, "radeonsi: binary shader has invalid CRC32\n"); 310bf215546Sopenharmony_ci return false; 311bf215546Sopenharmony_ci } 312bf215546Sopenharmony_ci 313bf215546Sopenharmony_ci ptr = read_data(ptr, &shader->config, sizeof(shader->config)); 314bf215546Sopenharmony_ci ptr = read_data(ptr, &shader->info, sizeof(shader->info)); 315bf215546Sopenharmony_ci ptr = read_chunk(ptr, (void **)&shader->binary.elf_buffer, &elf_size); 316bf215546Sopenharmony_ci shader->binary.elf_size = elf_size; 317bf215546Sopenharmony_ci ptr = read_chunk(ptr, (void **)&shader->binary.llvm_ir_string, &chunk_size); 318bf215546Sopenharmony_ci 319bf215546Sopenharmony_ci if (!shader->is_gs_copy_shader && 320bf215546Sopenharmony_ci shader->selector->stage == MESA_SHADER_GEOMETRY && !shader->key.ge.as_ngg) { 321bf215546Sopenharmony_ci shader->gs_copy_shader = CALLOC_STRUCT(si_shader); 322bf215546Sopenharmony_ci if (!shader->gs_copy_shader) 323bf215546Sopenharmony_ci return false; 324bf215546Sopenharmony_ci 325bf215546Sopenharmony_ci shader->gs_copy_shader->is_gs_copy_shader = true; 326bf215546Sopenharmony_ci 327bf215546Sopenharmony_ci if (!si_load_shader_binary(shader->gs_copy_shader, (uint8_t*)binary + size)) { 328bf215546Sopenharmony_ci FREE(shader->gs_copy_shader); 329bf215546Sopenharmony_ci shader->gs_copy_shader = NULL; 330bf215546Sopenharmony_ci return false; 331bf215546Sopenharmony_ci } 332bf215546Sopenharmony_ci 333bf215546Sopenharmony_ci util_queue_fence_init(&shader->gs_copy_shader->ready); 334bf215546Sopenharmony_ci shader->gs_copy_shader->selector = shader->selector; 335bf215546Sopenharmony_ci shader->gs_copy_shader->is_gs_copy_shader = true; 336bf215546Sopenharmony_ci shader->gs_copy_shader->wave_size = 337bf215546Sopenharmony_ci si_determine_wave_size(shader->selector->screen, shader->gs_copy_shader); 338bf215546Sopenharmony_ci 339bf215546Sopenharmony_ci si_shader_binary_upload(shader->selector->screen, shader->gs_copy_shader, 0); 340bf215546Sopenharmony_ci } 341bf215546Sopenharmony_ci 342bf215546Sopenharmony_ci return true; 343bf215546Sopenharmony_ci} 344bf215546Sopenharmony_ci 345bf215546Sopenharmony_ci/** 346bf215546Sopenharmony_ci * Insert a shader into the cache. It's assumed the shader is not in the cache. 347bf215546Sopenharmony_ci * Use si_shader_cache_load_shader before calling this. 348bf215546Sopenharmony_ci */ 349bf215546Sopenharmony_civoid si_shader_cache_insert_shader(struct si_screen *sscreen, unsigned char ir_sha1_cache_key[20], 350bf215546Sopenharmony_ci struct si_shader *shader, bool insert_into_disk_cache) 351bf215546Sopenharmony_ci{ 352bf215546Sopenharmony_ci uint32_t *hw_binary; 353bf215546Sopenharmony_ci struct hash_entry *entry; 354bf215546Sopenharmony_ci uint8_t key[CACHE_KEY_SIZE]; 355bf215546Sopenharmony_ci bool memory_cache_full = sscreen->shader_cache_size >= sscreen->shader_cache_max_size; 356bf215546Sopenharmony_ci 357bf215546Sopenharmony_ci if (!insert_into_disk_cache && memory_cache_full) 358bf215546Sopenharmony_ci return; 359bf215546Sopenharmony_ci 360bf215546Sopenharmony_ci entry = _mesa_hash_table_search(sscreen->shader_cache, ir_sha1_cache_key); 361bf215546Sopenharmony_ci if (entry) 362bf215546Sopenharmony_ci return; /* already added */ 363bf215546Sopenharmony_ci 364bf215546Sopenharmony_ci hw_binary = si_get_shader_binary(shader); 365bf215546Sopenharmony_ci if (!hw_binary) 366bf215546Sopenharmony_ci return; 367bf215546Sopenharmony_ci 368bf215546Sopenharmony_ci unsigned size = *hw_binary; 369bf215546Sopenharmony_ci 370bf215546Sopenharmony_ci if (shader->selector->stage == MESA_SHADER_GEOMETRY && !shader->key.ge.as_ngg) { 371bf215546Sopenharmony_ci uint32_t *gs_copy_binary = si_get_shader_binary(shader->gs_copy_shader); 372bf215546Sopenharmony_ci if (!gs_copy_binary) { 373bf215546Sopenharmony_ci FREE(hw_binary); 374bf215546Sopenharmony_ci return; 375bf215546Sopenharmony_ci } 376bf215546Sopenharmony_ci 377bf215546Sopenharmony_ci /* Combine both binaries. */ 378bf215546Sopenharmony_ci size += *gs_copy_binary; 379bf215546Sopenharmony_ci uint32_t *combined_binary = (uint32_t*)MALLOC(size); 380bf215546Sopenharmony_ci if (!combined_binary) { 381bf215546Sopenharmony_ci FREE(hw_binary); 382bf215546Sopenharmony_ci FREE(gs_copy_binary); 383bf215546Sopenharmony_ci return; 384bf215546Sopenharmony_ci } 385bf215546Sopenharmony_ci 386bf215546Sopenharmony_ci memcpy(combined_binary, hw_binary, *hw_binary); 387bf215546Sopenharmony_ci memcpy(combined_binary + *hw_binary / 4, gs_copy_binary, *gs_copy_binary); 388bf215546Sopenharmony_ci FREE(hw_binary); 389bf215546Sopenharmony_ci FREE(gs_copy_binary); 390bf215546Sopenharmony_ci hw_binary = combined_binary; 391bf215546Sopenharmony_ci } 392bf215546Sopenharmony_ci 393bf215546Sopenharmony_ci if (!memory_cache_full) { 394bf215546Sopenharmony_ci if (_mesa_hash_table_insert(sscreen->shader_cache, 395bf215546Sopenharmony_ci mem_dup(ir_sha1_cache_key, 20), 396bf215546Sopenharmony_ci hw_binary) == NULL) { 397bf215546Sopenharmony_ci FREE(hw_binary); 398bf215546Sopenharmony_ci return; 399bf215546Sopenharmony_ci } 400bf215546Sopenharmony_ci 401bf215546Sopenharmony_ci sscreen->shader_cache_size += size; 402bf215546Sopenharmony_ci } 403bf215546Sopenharmony_ci 404bf215546Sopenharmony_ci if (sscreen->disk_shader_cache && insert_into_disk_cache) { 405bf215546Sopenharmony_ci disk_cache_compute_key(sscreen->disk_shader_cache, ir_sha1_cache_key, 20, key); 406bf215546Sopenharmony_ci disk_cache_put(sscreen->disk_shader_cache, key, hw_binary, size, NULL); 407bf215546Sopenharmony_ci } 408bf215546Sopenharmony_ci 409bf215546Sopenharmony_ci if (memory_cache_full) 410bf215546Sopenharmony_ci FREE(hw_binary); 411bf215546Sopenharmony_ci} 412bf215546Sopenharmony_ci 413bf215546Sopenharmony_cibool si_shader_cache_load_shader(struct si_screen *sscreen, unsigned char ir_sha1_cache_key[20], 414bf215546Sopenharmony_ci struct si_shader *shader) 415bf215546Sopenharmony_ci{ 416bf215546Sopenharmony_ci struct hash_entry *entry = _mesa_hash_table_search(sscreen->shader_cache, ir_sha1_cache_key); 417bf215546Sopenharmony_ci 418bf215546Sopenharmony_ci if (entry) { 419bf215546Sopenharmony_ci if (si_load_shader_binary(shader, entry->data)) { 420bf215546Sopenharmony_ci p_atomic_inc(&sscreen->num_memory_shader_cache_hits); 421bf215546Sopenharmony_ci return true; 422bf215546Sopenharmony_ci } 423bf215546Sopenharmony_ci } 424bf215546Sopenharmony_ci p_atomic_inc(&sscreen->num_memory_shader_cache_misses); 425bf215546Sopenharmony_ci 426bf215546Sopenharmony_ci if (!sscreen->disk_shader_cache) 427bf215546Sopenharmony_ci return false; 428bf215546Sopenharmony_ci 429bf215546Sopenharmony_ci unsigned char sha1[CACHE_KEY_SIZE]; 430bf215546Sopenharmony_ci disk_cache_compute_key(sscreen->disk_shader_cache, ir_sha1_cache_key, 20, sha1); 431bf215546Sopenharmony_ci 432bf215546Sopenharmony_ci size_t total_size; 433bf215546Sopenharmony_ci uint32_t *buffer = (uint32_t*)disk_cache_get(sscreen->disk_shader_cache, sha1, &total_size); 434bf215546Sopenharmony_ci if (buffer) { 435bf215546Sopenharmony_ci unsigned size = *buffer; 436bf215546Sopenharmony_ci unsigned gs_copy_binary_size = 0; 437bf215546Sopenharmony_ci 438bf215546Sopenharmony_ci /* The GS copy shader binary is after the GS binary. */ 439bf215546Sopenharmony_ci if (shader->selector->stage == MESA_SHADER_GEOMETRY && !shader->key.ge.as_ngg) 440bf215546Sopenharmony_ci gs_copy_binary_size = buffer[size / 4]; 441bf215546Sopenharmony_ci 442bf215546Sopenharmony_ci if (total_size >= sizeof(uint32_t) && size + gs_copy_binary_size == total_size) { 443bf215546Sopenharmony_ci if (si_load_shader_binary(shader, buffer)) { 444bf215546Sopenharmony_ci free(buffer); 445bf215546Sopenharmony_ci si_shader_cache_insert_shader(sscreen, ir_sha1_cache_key, shader, false); 446bf215546Sopenharmony_ci p_atomic_inc(&sscreen->num_disk_shader_cache_hits); 447bf215546Sopenharmony_ci return true; 448bf215546Sopenharmony_ci } 449bf215546Sopenharmony_ci } else { 450bf215546Sopenharmony_ci /* Something has gone wrong discard the item from the cache and 451bf215546Sopenharmony_ci * rebuild/link from source. 452bf215546Sopenharmony_ci */ 453bf215546Sopenharmony_ci assert(!"Invalid radeonsi shader disk cache item!"); 454bf215546Sopenharmony_ci disk_cache_remove(sscreen->disk_shader_cache, sha1); 455bf215546Sopenharmony_ci } 456bf215546Sopenharmony_ci } 457bf215546Sopenharmony_ci 458bf215546Sopenharmony_ci free(buffer); 459bf215546Sopenharmony_ci p_atomic_inc(&sscreen->num_disk_shader_cache_misses); 460bf215546Sopenharmony_ci return false; 461bf215546Sopenharmony_ci} 462bf215546Sopenharmony_ci 463bf215546Sopenharmony_cistatic uint32_t si_shader_cache_key_hash(const void *key) 464bf215546Sopenharmony_ci{ 465bf215546Sopenharmony_ci /* Take the first dword of SHA1. */ 466bf215546Sopenharmony_ci return *(uint32_t *)key; 467bf215546Sopenharmony_ci} 468bf215546Sopenharmony_ci 469bf215546Sopenharmony_cistatic bool si_shader_cache_key_equals(const void *a, const void *b) 470bf215546Sopenharmony_ci{ 471bf215546Sopenharmony_ci /* Compare SHA1s. */ 472bf215546Sopenharmony_ci return memcmp(a, b, 20) == 0; 473bf215546Sopenharmony_ci} 474bf215546Sopenharmony_ci 475bf215546Sopenharmony_cistatic void si_destroy_shader_cache_entry(struct hash_entry *entry) 476bf215546Sopenharmony_ci{ 477bf215546Sopenharmony_ci FREE((void *)entry->key); 478bf215546Sopenharmony_ci FREE(entry->data); 479bf215546Sopenharmony_ci} 480bf215546Sopenharmony_ci 481bf215546Sopenharmony_cibool si_init_shader_cache(struct si_screen *sscreen) 482bf215546Sopenharmony_ci{ 483bf215546Sopenharmony_ci (void)simple_mtx_init(&sscreen->shader_cache_mutex, mtx_plain); 484bf215546Sopenharmony_ci sscreen->shader_cache = 485bf215546Sopenharmony_ci _mesa_hash_table_create(NULL, si_shader_cache_key_hash, si_shader_cache_key_equals); 486bf215546Sopenharmony_ci sscreen->shader_cache_size = 0; 487bf215546Sopenharmony_ci /* Maximum size: 64MB on 32 bits, 1GB else */ 488bf215546Sopenharmony_ci sscreen->shader_cache_max_size = ((sizeof(void *) == 4) ? 64 : 1024) * 1024 * 1024; 489bf215546Sopenharmony_ci 490bf215546Sopenharmony_ci return sscreen->shader_cache != NULL; 491bf215546Sopenharmony_ci} 492bf215546Sopenharmony_ci 493bf215546Sopenharmony_civoid si_destroy_shader_cache(struct si_screen *sscreen) 494bf215546Sopenharmony_ci{ 495bf215546Sopenharmony_ci if (sscreen->shader_cache) 496bf215546Sopenharmony_ci _mesa_hash_table_destroy(sscreen->shader_cache, si_destroy_shader_cache_entry); 497bf215546Sopenharmony_ci simple_mtx_destroy(&sscreen->shader_cache_mutex); 498bf215546Sopenharmony_ci} 499bf215546Sopenharmony_ci 500bf215546Sopenharmony_ci/* SHADER STATES */ 501bf215546Sopenharmony_ci 502bf215546Sopenharmony_cibool si_shader_mem_ordered(struct si_shader *shader) 503bf215546Sopenharmony_ci{ 504bf215546Sopenharmony_ci if (shader->selector->screen->info.gfx_level < GFX10) 505bf215546Sopenharmony_ci return false; 506bf215546Sopenharmony_ci 507bf215546Sopenharmony_ci /* Return true if both types of VMEM that return something are used. */ 508bf215546Sopenharmony_ci return shader->info.uses_vmem_sampler_or_bvh && 509bf215546Sopenharmony_ci (shader->info.uses_vmem_load_other || 510bf215546Sopenharmony_ci shader->config.scratch_bytes_per_wave); 511bf215546Sopenharmony_ci} 512bf215546Sopenharmony_ci 513bf215546Sopenharmony_cistatic void si_set_tesseval_regs(struct si_screen *sscreen, const struct si_shader_selector *tes, 514bf215546Sopenharmony_ci struct si_shader *shader) 515bf215546Sopenharmony_ci{ 516bf215546Sopenharmony_ci const struct si_shader_info *info = &tes->info; 517bf215546Sopenharmony_ci enum tess_primitive_mode tes_prim_mode = info->base.tess._primitive_mode; 518bf215546Sopenharmony_ci unsigned tes_spacing = info->base.tess.spacing; 519bf215546Sopenharmony_ci bool tes_vertex_order_cw = !info->base.tess.ccw; 520bf215546Sopenharmony_ci bool tes_point_mode = info->base.tess.point_mode; 521bf215546Sopenharmony_ci unsigned type, partitioning, topology, distribution_mode; 522bf215546Sopenharmony_ci 523bf215546Sopenharmony_ci switch (tes_prim_mode) { 524bf215546Sopenharmony_ci case TESS_PRIMITIVE_ISOLINES: 525bf215546Sopenharmony_ci type = V_028B6C_TESS_ISOLINE; 526bf215546Sopenharmony_ci break; 527bf215546Sopenharmony_ci case TESS_PRIMITIVE_TRIANGLES: 528bf215546Sopenharmony_ci type = V_028B6C_TESS_TRIANGLE; 529bf215546Sopenharmony_ci break; 530bf215546Sopenharmony_ci case TESS_PRIMITIVE_QUADS: 531bf215546Sopenharmony_ci type = V_028B6C_TESS_QUAD; 532bf215546Sopenharmony_ci break; 533bf215546Sopenharmony_ci default: 534bf215546Sopenharmony_ci assert(0); 535bf215546Sopenharmony_ci return; 536bf215546Sopenharmony_ci } 537bf215546Sopenharmony_ci 538bf215546Sopenharmony_ci switch (tes_spacing) { 539bf215546Sopenharmony_ci case TESS_SPACING_FRACTIONAL_ODD: 540bf215546Sopenharmony_ci partitioning = V_028B6C_PART_FRAC_ODD; 541bf215546Sopenharmony_ci break; 542bf215546Sopenharmony_ci case TESS_SPACING_FRACTIONAL_EVEN: 543bf215546Sopenharmony_ci partitioning = V_028B6C_PART_FRAC_EVEN; 544bf215546Sopenharmony_ci break; 545bf215546Sopenharmony_ci case TESS_SPACING_EQUAL: 546bf215546Sopenharmony_ci partitioning = V_028B6C_PART_INTEGER; 547bf215546Sopenharmony_ci break; 548bf215546Sopenharmony_ci default: 549bf215546Sopenharmony_ci assert(0); 550bf215546Sopenharmony_ci return; 551bf215546Sopenharmony_ci } 552bf215546Sopenharmony_ci 553bf215546Sopenharmony_ci if (tes_point_mode) 554bf215546Sopenharmony_ci topology = V_028B6C_OUTPUT_POINT; 555bf215546Sopenharmony_ci else if (tes_prim_mode == TESS_PRIMITIVE_ISOLINES) 556bf215546Sopenharmony_ci topology = V_028B6C_OUTPUT_LINE; 557bf215546Sopenharmony_ci else if (tes_vertex_order_cw) 558bf215546Sopenharmony_ci /* for some reason, this must be the other way around */ 559bf215546Sopenharmony_ci topology = V_028B6C_OUTPUT_TRIANGLE_CCW; 560bf215546Sopenharmony_ci else 561bf215546Sopenharmony_ci topology = V_028B6C_OUTPUT_TRIANGLE_CW; 562bf215546Sopenharmony_ci 563bf215546Sopenharmony_ci if (sscreen->info.has_distributed_tess) { 564bf215546Sopenharmony_ci if (sscreen->info.family == CHIP_FIJI || sscreen->info.family >= CHIP_POLARIS10) 565bf215546Sopenharmony_ci distribution_mode = V_028B6C_TRAPEZOIDS; 566bf215546Sopenharmony_ci else 567bf215546Sopenharmony_ci distribution_mode = V_028B6C_DONUTS; 568bf215546Sopenharmony_ci } else 569bf215546Sopenharmony_ci distribution_mode = V_028B6C_NO_DIST; 570bf215546Sopenharmony_ci 571bf215546Sopenharmony_ci shader->vgt_tf_param = S_028B6C_TYPE(type) | S_028B6C_PARTITIONING(partitioning) | 572bf215546Sopenharmony_ci S_028B6C_TOPOLOGY(topology) | 573bf215546Sopenharmony_ci S_028B6C_DISTRIBUTION_MODE(distribution_mode); 574bf215546Sopenharmony_ci} 575bf215546Sopenharmony_ci 576bf215546Sopenharmony_ci/* Polaris needs different VTX_REUSE_DEPTH settings depending on 577bf215546Sopenharmony_ci * whether the "fractional odd" tessellation spacing is used. 578bf215546Sopenharmony_ci * 579bf215546Sopenharmony_ci * Possible VGT configurations and which state should set the register: 580bf215546Sopenharmony_ci * 581bf215546Sopenharmony_ci * Reg set in | VGT shader configuration | Value 582bf215546Sopenharmony_ci * ------------------------------------------------------ 583bf215546Sopenharmony_ci * VS as VS | VS | 30 584bf215546Sopenharmony_ci * VS as ES | ES -> GS -> VS | 30 585bf215546Sopenharmony_ci * TES as VS | LS -> HS -> VS | 14 or 30 586bf215546Sopenharmony_ci * TES as ES | LS -> HS -> ES -> GS -> VS | 14 or 30 587bf215546Sopenharmony_ci */ 588bf215546Sopenharmony_cistatic void polaris_set_vgt_vertex_reuse(struct si_screen *sscreen, struct si_shader_selector *sel, 589bf215546Sopenharmony_ci struct si_shader *shader) 590bf215546Sopenharmony_ci{ 591bf215546Sopenharmony_ci if (sscreen->info.family < CHIP_POLARIS10 || sscreen->info.gfx_level >= GFX10) 592bf215546Sopenharmony_ci return; 593bf215546Sopenharmony_ci 594bf215546Sopenharmony_ci /* VS as VS, or VS as ES: */ 595bf215546Sopenharmony_ci if ((sel->stage == MESA_SHADER_VERTEX && 596bf215546Sopenharmony_ci (!shader->key.ge.as_ls && !shader->is_gs_copy_shader)) || 597bf215546Sopenharmony_ci /* TES as VS, or TES as ES: */ 598bf215546Sopenharmony_ci sel->stage == MESA_SHADER_TESS_EVAL) { 599bf215546Sopenharmony_ci unsigned vtx_reuse_depth = 30; 600bf215546Sopenharmony_ci 601bf215546Sopenharmony_ci if (sel->stage == MESA_SHADER_TESS_EVAL && 602bf215546Sopenharmony_ci sel->info.base.tess.spacing == TESS_SPACING_FRACTIONAL_ODD) 603bf215546Sopenharmony_ci vtx_reuse_depth = 14; 604bf215546Sopenharmony_ci 605bf215546Sopenharmony_ci shader->vgt_vertex_reuse_block_cntl = vtx_reuse_depth; 606bf215546Sopenharmony_ci } 607bf215546Sopenharmony_ci} 608bf215546Sopenharmony_ci 609bf215546Sopenharmony_cistatic struct si_pm4_state *si_get_shader_pm4_state(struct si_shader *shader) 610bf215546Sopenharmony_ci{ 611bf215546Sopenharmony_ci si_pm4_clear_state(&shader->pm4); 612bf215546Sopenharmony_ci shader->pm4.is_shader = true; 613bf215546Sopenharmony_ci return &shader->pm4; 614bf215546Sopenharmony_ci} 615bf215546Sopenharmony_ci 616bf215546Sopenharmony_cistatic unsigned si_get_num_vs_user_sgprs(struct si_shader *shader, 617bf215546Sopenharmony_ci unsigned num_always_on_user_sgprs) 618bf215546Sopenharmony_ci{ 619bf215546Sopenharmony_ci struct si_shader_selector *vs = 620bf215546Sopenharmony_ci shader->previous_stage_sel ? shader->previous_stage_sel : shader->selector; 621bf215546Sopenharmony_ci unsigned num_vbos_in_user_sgprs = vs->info.num_vbos_in_user_sgprs; 622bf215546Sopenharmony_ci 623bf215546Sopenharmony_ci /* 1 SGPR is reserved for the vertex buffer pointer. */ 624bf215546Sopenharmony_ci assert(num_always_on_user_sgprs <= SI_SGPR_VS_VB_DESCRIPTOR_FIRST - 1); 625bf215546Sopenharmony_ci 626bf215546Sopenharmony_ci if (num_vbos_in_user_sgprs) 627bf215546Sopenharmony_ci return SI_SGPR_VS_VB_DESCRIPTOR_FIRST + num_vbos_in_user_sgprs * 4; 628bf215546Sopenharmony_ci 629bf215546Sopenharmony_ci /* Add the pointer to VBO descriptors. */ 630bf215546Sopenharmony_ci return num_always_on_user_sgprs + 1; 631bf215546Sopenharmony_ci} 632bf215546Sopenharmony_ci 633bf215546Sopenharmony_ci/* Return VGPR_COMP_CNT for the API vertex shader. This can be hw LS, LSHS, ES, ESGS, VS. */ 634bf215546Sopenharmony_cistatic unsigned si_get_vs_vgpr_comp_cnt(struct si_screen *sscreen, struct si_shader *shader, 635bf215546Sopenharmony_ci bool legacy_vs_prim_id) 636bf215546Sopenharmony_ci{ 637bf215546Sopenharmony_ci assert(shader->selector->stage == MESA_SHADER_VERTEX || 638bf215546Sopenharmony_ci (shader->previous_stage_sel && shader->previous_stage_sel->stage == MESA_SHADER_VERTEX)); 639bf215546Sopenharmony_ci 640bf215546Sopenharmony_ci /* GFX6-9 LS (VertexID, RelAutoIndex, InstanceID / StepRate0, InstanceID) 641bf215546Sopenharmony_ci * GFX6-9 ES,VS (VertexID, InstanceID / StepRate0, VSPrimID, InstanceID) 642bf215546Sopenharmony_ci * GFX10-11 LS (VertexID, RelAutoIndex, UserVGPR1, UserVGPR2 or InstanceID) 643bf215546Sopenharmony_ci * GFX10-11 ES,VS (VertexID, UserVGPR1, UserVGPR2 or VSPrimID, UserVGPR3 or InstanceID) 644bf215546Sopenharmony_ci */ 645bf215546Sopenharmony_ci bool is_ls = shader->selector->stage == MESA_SHADER_TESS_CTRL || shader->key.ge.as_ls; 646bf215546Sopenharmony_ci unsigned max = 0; 647bf215546Sopenharmony_ci 648bf215546Sopenharmony_ci if (shader->info.uses_instanceid) { 649bf215546Sopenharmony_ci if (sscreen->info.gfx_level >= GFX10) 650bf215546Sopenharmony_ci max = MAX2(max, 3); 651bf215546Sopenharmony_ci else if (is_ls) 652bf215546Sopenharmony_ci max = MAX2(max, 2); /* use (InstanceID / StepRate0) because StepRate0 == 1 */ 653bf215546Sopenharmony_ci else 654bf215546Sopenharmony_ci max = MAX2(max, 1); /* use (InstanceID / StepRate0) because StepRate0 == 1 */ 655bf215546Sopenharmony_ci } 656bf215546Sopenharmony_ci 657bf215546Sopenharmony_ci if (legacy_vs_prim_id) 658bf215546Sopenharmony_ci max = MAX2(max, 2); /* VSPrimID */ 659bf215546Sopenharmony_ci 660bf215546Sopenharmony_ci /* GFX11: We prefer to compute RelAutoIndex using (WaveID * WaveSize + ThreadID). 661bf215546Sopenharmony_ci * Older chips didn't have WaveID in LS. 662bf215546Sopenharmony_ci */ 663bf215546Sopenharmony_ci if (is_ls && sscreen->info.gfx_level <= GFX10_3) 664bf215546Sopenharmony_ci max = MAX2(max, 1); /* RelAutoIndex */ 665bf215546Sopenharmony_ci 666bf215546Sopenharmony_ci return max; 667bf215546Sopenharmony_ci} 668bf215546Sopenharmony_ci 669bf215546Sopenharmony_ciunsigned si_get_shader_prefetch_size(struct si_shader *shader) 670bf215546Sopenharmony_ci{ 671bf215546Sopenharmony_ci /* Return 0 for some A0 chips only. Other chips don't need it. */ 672bf215546Sopenharmony_ci if ((shader->selector->screen->info.family == CHIP_GFX1100 || 673bf215546Sopenharmony_ci shader->selector->screen->info.family == CHIP_GFX1102 || 674bf215546Sopenharmony_ci shader->selector->screen->info.family == CHIP_GFX1103) && 675bf215546Sopenharmony_ci shader->selector->screen->info.chip_rev == 0) 676bf215546Sopenharmony_ci return 0; 677bf215546Sopenharmony_ci 678bf215546Sopenharmony_ci /* inst_pref_size is calculated in cache line size granularity */ 679bf215546Sopenharmony_ci assert(!(shader->bo->b.b.width0 & 0x7f)); 680bf215546Sopenharmony_ci return MIN2(shader->bo->b.b.width0, 8064) / 128; 681bf215546Sopenharmony_ci} 682bf215546Sopenharmony_ci 683bf215546Sopenharmony_cistatic void si_shader_ls(struct si_screen *sscreen, struct si_shader *shader) 684bf215546Sopenharmony_ci{ 685bf215546Sopenharmony_ci struct si_pm4_state *pm4; 686bf215546Sopenharmony_ci uint64_t va; 687bf215546Sopenharmony_ci 688bf215546Sopenharmony_ci assert(sscreen->info.gfx_level <= GFX8); 689bf215546Sopenharmony_ci 690bf215546Sopenharmony_ci pm4 = si_get_shader_pm4_state(shader); 691bf215546Sopenharmony_ci if (!pm4) 692bf215546Sopenharmony_ci return; 693bf215546Sopenharmony_ci 694bf215546Sopenharmony_ci va = shader->bo->gpu_address; 695bf215546Sopenharmony_ci si_pm4_set_reg(pm4, R_00B520_SPI_SHADER_PGM_LO_LS, va >> 8); 696bf215546Sopenharmony_ci 697bf215546Sopenharmony_ci shader->config.rsrc1 = S_00B528_VGPRS((shader->config.num_vgprs - 1) / 4) | 698bf215546Sopenharmony_ci S_00B528_SGPRS((shader->config.num_sgprs - 1) / 8) | 699bf215546Sopenharmony_ci S_00B528_VGPR_COMP_CNT(si_get_vs_vgpr_comp_cnt(sscreen, shader, false)) | 700bf215546Sopenharmony_ci S_00B528_DX10_CLAMP(1) | S_00B528_FLOAT_MODE(shader->config.float_mode); 701bf215546Sopenharmony_ci shader->config.rsrc2 = 702bf215546Sopenharmony_ci S_00B52C_USER_SGPR(si_get_num_vs_user_sgprs(shader, SI_VS_NUM_USER_SGPR)) | 703bf215546Sopenharmony_ci S_00B52C_SCRATCH_EN(shader->config.scratch_bytes_per_wave > 0); 704bf215546Sopenharmony_ci} 705bf215546Sopenharmony_ci 706bf215546Sopenharmony_cistatic void si_shader_hs(struct si_screen *sscreen, struct si_shader *shader) 707bf215546Sopenharmony_ci{ 708bf215546Sopenharmony_ci struct si_pm4_state *pm4; 709bf215546Sopenharmony_ci uint64_t va; 710bf215546Sopenharmony_ci 711bf215546Sopenharmony_ci pm4 = si_get_shader_pm4_state(shader); 712bf215546Sopenharmony_ci if (!pm4) 713bf215546Sopenharmony_ci return; 714bf215546Sopenharmony_ci 715bf215546Sopenharmony_ci va = shader->bo->gpu_address; 716bf215546Sopenharmony_ci 717bf215546Sopenharmony_ci if (sscreen->info.gfx_level >= GFX9) { 718bf215546Sopenharmony_ci if (sscreen->info.gfx_level >= GFX11) { 719bf215546Sopenharmony_ci ac_set_reg_cu_en(pm4, R_00B404_SPI_SHADER_PGM_RSRC4_HS, 720bf215546Sopenharmony_ci S_00B404_INST_PREF_SIZE(si_get_shader_prefetch_size(shader)) | 721bf215546Sopenharmony_ci S_00B404_CU_EN(0xffff), 722bf215546Sopenharmony_ci C_00B404_CU_EN, 16, &sscreen->info, 723bf215546Sopenharmony_ci (void (*)(void*, unsigned, uint32_t))si_pm4_set_reg_idx3); 724bf215546Sopenharmony_ci } 725bf215546Sopenharmony_ci if (sscreen->info.gfx_level >= GFX10) { 726bf215546Sopenharmony_ci si_pm4_set_reg(pm4, R_00B520_SPI_SHADER_PGM_LO_LS, va >> 8); 727bf215546Sopenharmony_ci } else { 728bf215546Sopenharmony_ci si_pm4_set_reg(pm4, R_00B410_SPI_SHADER_PGM_LO_LS, va >> 8); 729bf215546Sopenharmony_ci } 730bf215546Sopenharmony_ci 731bf215546Sopenharmony_ci unsigned num_user_sgprs = si_get_num_vs_user_sgprs(shader, GFX9_TCS_NUM_USER_SGPR); 732bf215546Sopenharmony_ci 733bf215546Sopenharmony_ci shader->config.rsrc2 = S_00B42C_USER_SGPR(num_user_sgprs) | 734bf215546Sopenharmony_ci S_00B42C_SCRATCH_EN(shader->config.scratch_bytes_per_wave > 0); 735bf215546Sopenharmony_ci 736bf215546Sopenharmony_ci if (sscreen->info.gfx_level >= GFX10) 737bf215546Sopenharmony_ci shader->config.rsrc2 |= S_00B42C_USER_SGPR_MSB_GFX10(num_user_sgprs >> 5); 738bf215546Sopenharmony_ci else 739bf215546Sopenharmony_ci shader->config.rsrc2 |= S_00B42C_USER_SGPR_MSB_GFX9(num_user_sgprs >> 5); 740bf215546Sopenharmony_ci } else { 741bf215546Sopenharmony_ci si_pm4_set_reg(pm4, R_00B420_SPI_SHADER_PGM_LO_HS, va >> 8); 742bf215546Sopenharmony_ci si_pm4_set_reg(pm4, R_00B424_SPI_SHADER_PGM_HI_HS, 743bf215546Sopenharmony_ci S_00B424_MEM_BASE(sscreen->info.address32_hi >> 8)); 744bf215546Sopenharmony_ci 745bf215546Sopenharmony_ci shader->config.rsrc2 = S_00B42C_USER_SGPR(GFX6_TCS_NUM_USER_SGPR) | S_00B42C_OC_LDS_EN(1) | 746bf215546Sopenharmony_ci S_00B42C_SCRATCH_EN(shader->config.scratch_bytes_per_wave > 0); 747bf215546Sopenharmony_ci } 748bf215546Sopenharmony_ci 749bf215546Sopenharmony_ci si_pm4_set_reg( 750bf215546Sopenharmony_ci pm4, R_00B428_SPI_SHADER_PGM_RSRC1_HS, 751bf215546Sopenharmony_ci S_00B428_VGPRS((shader->config.num_vgprs - 1) / (shader->wave_size == 32 ? 8 : 4)) | 752bf215546Sopenharmony_ci (sscreen->info.gfx_level <= GFX9 ? S_00B428_SGPRS((shader->config.num_sgprs - 1) / 8) 753bf215546Sopenharmony_ci : 0) | 754bf215546Sopenharmony_ci S_00B428_DX10_CLAMP(1) | S_00B428_MEM_ORDERED(si_shader_mem_ordered(shader)) | 755bf215546Sopenharmony_ci S_00B428_WGP_MODE(sscreen->info.gfx_level >= GFX10) | 756bf215546Sopenharmony_ci S_00B428_FLOAT_MODE(shader->config.float_mode) | 757bf215546Sopenharmony_ci S_00B428_LS_VGPR_COMP_CNT(sscreen->info.gfx_level >= GFX9 758bf215546Sopenharmony_ci ? si_get_vs_vgpr_comp_cnt(sscreen, shader, false) 759bf215546Sopenharmony_ci : 0)); 760bf215546Sopenharmony_ci 761bf215546Sopenharmony_ci if (sscreen->info.gfx_level <= GFX8) { 762bf215546Sopenharmony_ci si_pm4_set_reg(pm4, R_00B42C_SPI_SHADER_PGM_RSRC2_HS, shader->config.rsrc2); 763bf215546Sopenharmony_ci } 764bf215546Sopenharmony_ci} 765bf215546Sopenharmony_ci 766bf215546Sopenharmony_cistatic void si_emit_shader_es(struct si_context *sctx) 767bf215546Sopenharmony_ci{ 768bf215546Sopenharmony_ci struct si_shader *shader = sctx->queued.named.es; 769bf215546Sopenharmony_ci if (!shader) 770bf215546Sopenharmony_ci return; 771bf215546Sopenharmony_ci 772bf215546Sopenharmony_ci radeon_begin(&sctx->gfx_cs); 773bf215546Sopenharmony_ci radeon_opt_set_context_reg(sctx, R_028AAC_VGT_ESGS_RING_ITEMSIZE, 774bf215546Sopenharmony_ci SI_TRACKED_VGT_ESGS_RING_ITEMSIZE, 775bf215546Sopenharmony_ci shader->selector->info.esgs_itemsize / 4); 776bf215546Sopenharmony_ci 777bf215546Sopenharmony_ci if (shader->selector->stage == MESA_SHADER_TESS_EVAL) 778bf215546Sopenharmony_ci radeon_opt_set_context_reg(sctx, R_028B6C_VGT_TF_PARAM, SI_TRACKED_VGT_TF_PARAM, 779bf215546Sopenharmony_ci shader->vgt_tf_param); 780bf215546Sopenharmony_ci 781bf215546Sopenharmony_ci if (shader->vgt_vertex_reuse_block_cntl) 782bf215546Sopenharmony_ci radeon_opt_set_context_reg(sctx, R_028C58_VGT_VERTEX_REUSE_BLOCK_CNTL, 783bf215546Sopenharmony_ci SI_TRACKED_VGT_VERTEX_REUSE_BLOCK_CNTL, 784bf215546Sopenharmony_ci shader->vgt_vertex_reuse_block_cntl); 785bf215546Sopenharmony_ci radeon_end_update_context_roll(sctx); 786bf215546Sopenharmony_ci} 787bf215546Sopenharmony_ci 788bf215546Sopenharmony_cistatic void si_shader_es(struct si_screen *sscreen, struct si_shader *shader) 789bf215546Sopenharmony_ci{ 790bf215546Sopenharmony_ci struct si_pm4_state *pm4; 791bf215546Sopenharmony_ci unsigned num_user_sgprs; 792bf215546Sopenharmony_ci unsigned vgpr_comp_cnt; 793bf215546Sopenharmony_ci uint64_t va; 794bf215546Sopenharmony_ci unsigned oc_lds_en; 795bf215546Sopenharmony_ci 796bf215546Sopenharmony_ci assert(sscreen->info.gfx_level <= GFX8); 797bf215546Sopenharmony_ci 798bf215546Sopenharmony_ci pm4 = si_get_shader_pm4_state(shader); 799bf215546Sopenharmony_ci if (!pm4) 800bf215546Sopenharmony_ci return; 801bf215546Sopenharmony_ci 802bf215546Sopenharmony_ci pm4->atom.emit = si_emit_shader_es; 803bf215546Sopenharmony_ci va = shader->bo->gpu_address; 804bf215546Sopenharmony_ci 805bf215546Sopenharmony_ci if (shader->selector->stage == MESA_SHADER_VERTEX) { 806bf215546Sopenharmony_ci vgpr_comp_cnt = si_get_vs_vgpr_comp_cnt(sscreen, shader, false); 807bf215546Sopenharmony_ci num_user_sgprs = si_get_num_vs_user_sgprs(shader, SI_VS_NUM_USER_SGPR); 808bf215546Sopenharmony_ci } else if (shader->selector->stage == MESA_SHADER_TESS_EVAL) { 809bf215546Sopenharmony_ci vgpr_comp_cnt = shader->selector->info.uses_primid ? 3 : 2; 810bf215546Sopenharmony_ci num_user_sgprs = SI_TES_NUM_USER_SGPR; 811bf215546Sopenharmony_ci } else 812bf215546Sopenharmony_ci unreachable("invalid shader selector type"); 813bf215546Sopenharmony_ci 814bf215546Sopenharmony_ci oc_lds_en = shader->selector->stage == MESA_SHADER_TESS_EVAL ? 1 : 0; 815bf215546Sopenharmony_ci 816bf215546Sopenharmony_ci si_pm4_set_reg(pm4, R_00B320_SPI_SHADER_PGM_LO_ES, va >> 8); 817bf215546Sopenharmony_ci si_pm4_set_reg(pm4, R_00B324_SPI_SHADER_PGM_HI_ES, 818bf215546Sopenharmony_ci S_00B324_MEM_BASE(sscreen->info.address32_hi >> 8)); 819bf215546Sopenharmony_ci si_pm4_set_reg(pm4, R_00B328_SPI_SHADER_PGM_RSRC1_ES, 820bf215546Sopenharmony_ci S_00B328_VGPRS((shader->config.num_vgprs - 1) / 4) | 821bf215546Sopenharmony_ci S_00B328_SGPRS((shader->config.num_sgprs - 1) / 8) | 822bf215546Sopenharmony_ci S_00B328_VGPR_COMP_CNT(vgpr_comp_cnt) | S_00B328_DX10_CLAMP(1) | 823bf215546Sopenharmony_ci S_00B328_FLOAT_MODE(shader->config.float_mode)); 824bf215546Sopenharmony_ci si_pm4_set_reg(pm4, R_00B32C_SPI_SHADER_PGM_RSRC2_ES, 825bf215546Sopenharmony_ci S_00B32C_USER_SGPR(num_user_sgprs) | S_00B32C_OC_LDS_EN(oc_lds_en) | 826bf215546Sopenharmony_ci S_00B32C_SCRATCH_EN(shader->config.scratch_bytes_per_wave > 0)); 827bf215546Sopenharmony_ci 828bf215546Sopenharmony_ci if (shader->selector->stage == MESA_SHADER_TESS_EVAL) 829bf215546Sopenharmony_ci si_set_tesseval_regs(sscreen, shader->selector, shader); 830bf215546Sopenharmony_ci 831bf215546Sopenharmony_ci polaris_set_vgt_vertex_reuse(sscreen, shader->selector, shader); 832bf215546Sopenharmony_ci} 833bf215546Sopenharmony_ci 834bf215546Sopenharmony_civoid gfx9_get_gs_info(struct si_shader_selector *es, struct si_shader_selector *gs, 835bf215546Sopenharmony_ci struct gfx9_gs_info *out) 836bf215546Sopenharmony_ci{ 837bf215546Sopenharmony_ci unsigned gs_num_invocations = MAX2(gs->info.base.gs.invocations, 1); 838bf215546Sopenharmony_ci unsigned input_prim = gs->info.base.gs.input_primitive; 839bf215546Sopenharmony_ci bool uses_adjacency = 840bf215546Sopenharmony_ci input_prim >= PIPE_PRIM_LINES_ADJACENCY && input_prim <= PIPE_PRIM_TRIANGLE_STRIP_ADJACENCY; 841bf215546Sopenharmony_ci 842bf215546Sopenharmony_ci /* All these are in dwords: */ 843bf215546Sopenharmony_ci /* We can't allow using the whole LDS, because GS waves compete with 844bf215546Sopenharmony_ci * other shader stages for LDS space. */ 845bf215546Sopenharmony_ci const unsigned max_lds_size = 8 * 1024; 846bf215546Sopenharmony_ci const unsigned esgs_itemsize = es->info.esgs_itemsize / 4; 847bf215546Sopenharmony_ci unsigned esgs_lds_size; 848bf215546Sopenharmony_ci 849bf215546Sopenharmony_ci /* All these are per subgroup: */ 850bf215546Sopenharmony_ci const unsigned max_out_prims = 32 * 1024; 851bf215546Sopenharmony_ci const unsigned max_es_verts = 255; 852bf215546Sopenharmony_ci const unsigned ideal_gs_prims = 64; 853bf215546Sopenharmony_ci unsigned max_gs_prims, gs_prims; 854bf215546Sopenharmony_ci unsigned min_es_verts, es_verts, worst_case_es_verts; 855bf215546Sopenharmony_ci 856bf215546Sopenharmony_ci if (uses_adjacency || gs_num_invocations > 1) 857bf215546Sopenharmony_ci max_gs_prims = 127 / gs_num_invocations; 858bf215546Sopenharmony_ci else 859bf215546Sopenharmony_ci max_gs_prims = 255; 860bf215546Sopenharmony_ci 861bf215546Sopenharmony_ci /* MAX_PRIMS_PER_SUBGROUP = gs_prims * max_vert_out * gs_invocations. 862bf215546Sopenharmony_ci * Make sure we don't go over the maximum value. 863bf215546Sopenharmony_ci */ 864bf215546Sopenharmony_ci if (gs->info.base.gs.vertices_out > 0) { 865bf215546Sopenharmony_ci max_gs_prims = 866bf215546Sopenharmony_ci MIN2(max_gs_prims, max_out_prims / (gs->info.base.gs.vertices_out * gs_num_invocations)); 867bf215546Sopenharmony_ci } 868bf215546Sopenharmony_ci assert(max_gs_prims > 0); 869bf215546Sopenharmony_ci 870bf215546Sopenharmony_ci /* If the primitive has adjacency, halve the number of vertices 871bf215546Sopenharmony_ci * that will be reused in multiple primitives. 872bf215546Sopenharmony_ci */ 873bf215546Sopenharmony_ci min_es_verts = gs->info.gs_input_verts_per_prim / (uses_adjacency ? 2 : 1); 874bf215546Sopenharmony_ci 875bf215546Sopenharmony_ci gs_prims = MIN2(ideal_gs_prims, max_gs_prims); 876bf215546Sopenharmony_ci worst_case_es_verts = MIN2(min_es_verts * gs_prims, max_es_verts); 877bf215546Sopenharmony_ci 878bf215546Sopenharmony_ci /* Compute ESGS LDS size based on the worst case number of ES vertices 879bf215546Sopenharmony_ci * needed to create the target number of GS prims per subgroup. 880bf215546Sopenharmony_ci */ 881bf215546Sopenharmony_ci esgs_lds_size = esgs_itemsize * worst_case_es_verts; 882bf215546Sopenharmony_ci 883bf215546Sopenharmony_ci /* If total LDS usage is too big, refactor partitions based on ratio 884bf215546Sopenharmony_ci * of ESGS item sizes. 885bf215546Sopenharmony_ci */ 886bf215546Sopenharmony_ci if (esgs_lds_size > max_lds_size) { 887bf215546Sopenharmony_ci /* Our target GS Prims Per Subgroup was too large. Calculate 888bf215546Sopenharmony_ci * the maximum number of GS Prims Per Subgroup that will fit 889bf215546Sopenharmony_ci * into LDS, capped by the maximum that the hardware can support. 890bf215546Sopenharmony_ci */ 891bf215546Sopenharmony_ci gs_prims = MIN2((max_lds_size / (esgs_itemsize * min_es_verts)), max_gs_prims); 892bf215546Sopenharmony_ci assert(gs_prims > 0); 893bf215546Sopenharmony_ci worst_case_es_verts = MIN2(min_es_verts * gs_prims, max_es_verts); 894bf215546Sopenharmony_ci 895bf215546Sopenharmony_ci esgs_lds_size = esgs_itemsize * worst_case_es_verts; 896bf215546Sopenharmony_ci assert(esgs_lds_size <= max_lds_size); 897bf215546Sopenharmony_ci } 898bf215546Sopenharmony_ci 899bf215546Sopenharmony_ci /* Now calculate remaining ESGS information. */ 900bf215546Sopenharmony_ci if (esgs_lds_size) 901bf215546Sopenharmony_ci es_verts = MIN2(esgs_lds_size / esgs_itemsize, max_es_verts); 902bf215546Sopenharmony_ci else 903bf215546Sopenharmony_ci es_verts = max_es_verts; 904bf215546Sopenharmony_ci 905bf215546Sopenharmony_ci /* Vertices for adjacency primitives are not always reused, so restore 906bf215546Sopenharmony_ci * it for ES_VERTS_PER_SUBGRP. 907bf215546Sopenharmony_ci */ 908bf215546Sopenharmony_ci min_es_verts = gs->info.gs_input_verts_per_prim; 909bf215546Sopenharmony_ci 910bf215546Sopenharmony_ci /* For normal primitives, the VGT only checks if they are past the ES 911bf215546Sopenharmony_ci * verts per subgroup after allocating a full GS primitive and if they 912bf215546Sopenharmony_ci * are, kick off a new subgroup. But if those additional ES verts are 913bf215546Sopenharmony_ci * unique (e.g. not reused) we need to make sure there is enough LDS 914bf215546Sopenharmony_ci * space to account for those ES verts beyond ES_VERTS_PER_SUBGRP. 915bf215546Sopenharmony_ci */ 916bf215546Sopenharmony_ci es_verts -= min_es_verts - 1; 917bf215546Sopenharmony_ci 918bf215546Sopenharmony_ci out->es_verts_per_subgroup = es_verts; 919bf215546Sopenharmony_ci out->gs_prims_per_subgroup = gs_prims; 920bf215546Sopenharmony_ci out->gs_inst_prims_in_subgroup = gs_prims * gs_num_invocations; 921bf215546Sopenharmony_ci out->max_prims_per_subgroup = out->gs_inst_prims_in_subgroup * gs->info.base.gs.vertices_out; 922bf215546Sopenharmony_ci out->esgs_ring_size = esgs_lds_size; 923bf215546Sopenharmony_ci 924bf215546Sopenharmony_ci assert(out->max_prims_per_subgroup <= max_out_prims); 925bf215546Sopenharmony_ci} 926bf215546Sopenharmony_ci 927bf215546Sopenharmony_cistatic void si_emit_shader_gs(struct si_context *sctx) 928bf215546Sopenharmony_ci{ 929bf215546Sopenharmony_ci struct si_shader *shader = sctx->queued.named.gs; 930bf215546Sopenharmony_ci if (!shader) 931bf215546Sopenharmony_ci return; 932bf215546Sopenharmony_ci 933bf215546Sopenharmony_ci radeon_begin(&sctx->gfx_cs); 934bf215546Sopenharmony_ci 935bf215546Sopenharmony_ci /* R_028A60_VGT_GSVS_RING_OFFSET_1, R_028A64_VGT_GSVS_RING_OFFSET_2 936bf215546Sopenharmony_ci * R_028A68_VGT_GSVS_RING_OFFSET_3 */ 937bf215546Sopenharmony_ci radeon_opt_set_context_reg3( 938bf215546Sopenharmony_ci sctx, R_028A60_VGT_GSVS_RING_OFFSET_1, SI_TRACKED_VGT_GSVS_RING_OFFSET_1, 939bf215546Sopenharmony_ci shader->ctx_reg.gs.vgt_gsvs_ring_offset_1, shader->ctx_reg.gs.vgt_gsvs_ring_offset_2, 940bf215546Sopenharmony_ci shader->ctx_reg.gs.vgt_gsvs_ring_offset_3); 941bf215546Sopenharmony_ci 942bf215546Sopenharmony_ci /* R_028AB0_VGT_GSVS_RING_ITEMSIZE */ 943bf215546Sopenharmony_ci radeon_opt_set_context_reg(sctx, R_028AB0_VGT_GSVS_RING_ITEMSIZE, 944bf215546Sopenharmony_ci SI_TRACKED_VGT_GSVS_RING_ITEMSIZE, 945bf215546Sopenharmony_ci shader->ctx_reg.gs.vgt_gsvs_ring_itemsize); 946bf215546Sopenharmony_ci 947bf215546Sopenharmony_ci /* R_028B38_VGT_GS_MAX_VERT_OUT */ 948bf215546Sopenharmony_ci radeon_opt_set_context_reg(sctx, R_028B38_VGT_GS_MAX_VERT_OUT, SI_TRACKED_VGT_GS_MAX_VERT_OUT, 949bf215546Sopenharmony_ci shader->ctx_reg.gs.vgt_gs_max_vert_out); 950bf215546Sopenharmony_ci 951bf215546Sopenharmony_ci /* R_028B5C_VGT_GS_VERT_ITEMSIZE, R_028B60_VGT_GS_VERT_ITEMSIZE_1 952bf215546Sopenharmony_ci * R_028B64_VGT_GS_VERT_ITEMSIZE_2, R_028B68_VGT_GS_VERT_ITEMSIZE_3 */ 953bf215546Sopenharmony_ci radeon_opt_set_context_reg4( 954bf215546Sopenharmony_ci sctx, R_028B5C_VGT_GS_VERT_ITEMSIZE, SI_TRACKED_VGT_GS_VERT_ITEMSIZE, 955bf215546Sopenharmony_ci shader->ctx_reg.gs.vgt_gs_vert_itemsize, shader->ctx_reg.gs.vgt_gs_vert_itemsize_1, 956bf215546Sopenharmony_ci shader->ctx_reg.gs.vgt_gs_vert_itemsize_2, shader->ctx_reg.gs.vgt_gs_vert_itemsize_3); 957bf215546Sopenharmony_ci 958bf215546Sopenharmony_ci /* R_028B90_VGT_GS_INSTANCE_CNT */ 959bf215546Sopenharmony_ci radeon_opt_set_context_reg(sctx, R_028B90_VGT_GS_INSTANCE_CNT, SI_TRACKED_VGT_GS_INSTANCE_CNT, 960bf215546Sopenharmony_ci shader->ctx_reg.gs.vgt_gs_instance_cnt); 961bf215546Sopenharmony_ci 962bf215546Sopenharmony_ci if (sctx->gfx_level >= GFX9) { 963bf215546Sopenharmony_ci /* R_028A44_VGT_GS_ONCHIP_CNTL */ 964bf215546Sopenharmony_ci radeon_opt_set_context_reg(sctx, R_028A44_VGT_GS_ONCHIP_CNTL, SI_TRACKED_VGT_GS_ONCHIP_CNTL, 965bf215546Sopenharmony_ci shader->ctx_reg.gs.vgt_gs_onchip_cntl); 966bf215546Sopenharmony_ci /* R_028A94_VGT_GS_MAX_PRIMS_PER_SUBGROUP */ 967bf215546Sopenharmony_ci radeon_opt_set_context_reg(sctx, R_028A94_VGT_GS_MAX_PRIMS_PER_SUBGROUP, 968bf215546Sopenharmony_ci SI_TRACKED_VGT_GS_MAX_PRIMS_PER_SUBGROUP, 969bf215546Sopenharmony_ci shader->ctx_reg.gs.vgt_gs_max_prims_per_subgroup); 970bf215546Sopenharmony_ci /* R_028AAC_VGT_ESGS_RING_ITEMSIZE */ 971bf215546Sopenharmony_ci radeon_opt_set_context_reg(sctx, R_028AAC_VGT_ESGS_RING_ITEMSIZE, 972bf215546Sopenharmony_ci SI_TRACKED_VGT_ESGS_RING_ITEMSIZE, 973bf215546Sopenharmony_ci shader->ctx_reg.gs.vgt_esgs_ring_itemsize); 974bf215546Sopenharmony_ci 975bf215546Sopenharmony_ci if (shader->key.ge.part.gs.es->stage == MESA_SHADER_TESS_EVAL) 976bf215546Sopenharmony_ci radeon_opt_set_context_reg(sctx, R_028B6C_VGT_TF_PARAM, SI_TRACKED_VGT_TF_PARAM, 977bf215546Sopenharmony_ci shader->vgt_tf_param); 978bf215546Sopenharmony_ci if (shader->vgt_vertex_reuse_block_cntl) 979bf215546Sopenharmony_ci radeon_opt_set_context_reg(sctx, R_028C58_VGT_VERTEX_REUSE_BLOCK_CNTL, 980bf215546Sopenharmony_ci SI_TRACKED_VGT_VERTEX_REUSE_BLOCK_CNTL, 981bf215546Sopenharmony_ci shader->vgt_vertex_reuse_block_cntl); 982bf215546Sopenharmony_ci } 983bf215546Sopenharmony_ci radeon_end_update_context_roll(sctx); 984bf215546Sopenharmony_ci 985bf215546Sopenharmony_ci /* These don't cause any context rolls. */ 986bf215546Sopenharmony_ci if (sctx->screen->info.spi_cu_en_has_effect) { 987bf215546Sopenharmony_ci if (sctx->gfx_level >= GFX7) { 988bf215546Sopenharmony_ci ac_set_reg_cu_en(&sctx->gfx_cs, R_00B21C_SPI_SHADER_PGM_RSRC3_GS, 989bf215546Sopenharmony_ci shader->ctx_reg.gs.spi_shader_pgm_rsrc3_gs, 990bf215546Sopenharmony_ci C_00B21C_CU_EN, 0, &sctx->screen->info, 991bf215546Sopenharmony_ci (void (*)(void*, unsigned, uint32_t)) 992bf215546Sopenharmony_ci (sctx->gfx_level >= GFX10 ? radeon_set_sh_reg_idx3_func : radeon_set_sh_reg_func)); 993bf215546Sopenharmony_ci sctx->tracked_regs.reg_saved &= ~BITFIELD64_BIT(SI_TRACKED_SPI_SHADER_PGM_RSRC3_GS); 994bf215546Sopenharmony_ci } 995bf215546Sopenharmony_ci if (sctx->gfx_level >= GFX10) { 996bf215546Sopenharmony_ci ac_set_reg_cu_en(&sctx->gfx_cs, R_00B204_SPI_SHADER_PGM_RSRC4_GS, 997bf215546Sopenharmony_ci shader->ctx_reg.gs.spi_shader_pgm_rsrc4_gs, 998bf215546Sopenharmony_ci C_00B204_CU_EN_GFX10, 16, &sctx->screen->info, 999bf215546Sopenharmony_ci (void (*)(void*, unsigned, uint32_t)) 1000bf215546Sopenharmony_ci (sctx->gfx_level >= GFX10 ? radeon_set_sh_reg_idx3_func : radeon_set_sh_reg_func)); 1001bf215546Sopenharmony_ci sctx->tracked_regs.reg_saved &= ~BITFIELD64_BIT(SI_TRACKED_SPI_SHADER_PGM_RSRC4_GS); 1002bf215546Sopenharmony_ci } 1003bf215546Sopenharmony_ci } else { 1004bf215546Sopenharmony_ci radeon_begin_again(&sctx->gfx_cs); 1005bf215546Sopenharmony_ci if (sctx->gfx_level >= GFX7) { 1006bf215546Sopenharmony_ci radeon_opt_set_sh_reg_idx3(sctx, R_00B21C_SPI_SHADER_PGM_RSRC3_GS, 1007bf215546Sopenharmony_ci SI_TRACKED_SPI_SHADER_PGM_RSRC3_GS, 1008bf215546Sopenharmony_ci shader->ctx_reg.gs.spi_shader_pgm_rsrc3_gs); 1009bf215546Sopenharmony_ci } 1010bf215546Sopenharmony_ci if (sctx->gfx_level >= GFX10) { 1011bf215546Sopenharmony_ci radeon_opt_set_sh_reg_idx3(sctx, R_00B204_SPI_SHADER_PGM_RSRC4_GS, 1012bf215546Sopenharmony_ci SI_TRACKED_SPI_SHADER_PGM_RSRC4_GS, 1013bf215546Sopenharmony_ci shader->ctx_reg.gs.spi_shader_pgm_rsrc4_gs); 1014bf215546Sopenharmony_ci } 1015bf215546Sopenharmony_ci radeon_end(); 1016bf215546Sopenharmony_ci } 1017bf215546Sopenharmony_ci} 1018bf215546Sopenharmony_ci 1019bf215546Sopenharmony_cistatic void si_shader_gs(struct si_screen *sscreen, struct si_shader *shader) 1020bf215546Sopenharmony_ci{ 1021bf215546Sopenharmony_ci struct si_shader_selector *sel = shader->selector; 1022bf215546Sopenharmony_ci const ubyte *num_components = sel->info.num_stream_output_components; 1023bf215546Sopenharmony_ci unsigned gs_num_invocations = sel->info.base.gs.invocations; 1024bf215546Sopenharmony_ci struct si_pm4_state *pm4; 1025bf215546Sopenharmony_ci uint64_t va; 1026bf215546Sopenharmony_ci unsigned max_stream = util_last_bit(sel->info.base.gs.active_stream_mask); 1027bf215546Sopenharmony_ci unsigned offset; 1028bf215546Sopenharmony_ci 1029bf215546Sopenharmony_ci assert(sscreen->info.gfx_level < GFX11); /* gfx11 doesn't have the legacy pipeline */ 1030bf215546Sopenharmony_ci 1031bf215546Sopenharmony_ci pm4 = si_get_shader_pm4_state(shader); 1032bf215546Sopenharmony_ci if (!pm4) 1033bf215546Sopenharmony_ci return; 1034bf215546Sopenharmony_ci 1035bf215546Sopenharmony_ci pm4->atom.emit = si_emit_shader_gs; 1036bf215546Sopenharmony_ci 1037bf215546Sopenharmony_ci offset = num_components[0] * sel->info.base.gs.vertices_out; 1038bf215546Sopenharmony_ci shader->ctx_reg.gs.vgt_gsvs_ring_offset_1 = offset; 1039bf215546Sopenharmony_ci 1040bf215546Sopenharmony_ci if (max_stream >= 2) 1041bf215546Sopenharmony_ci offset += num_components[1] * sel->info.base.gs.vertices_out; 1042bf215546Sopenharmony_ci shader->ctx_reg.gs.vgt_gsvs_ring_offset_2 = offset; 1043bf215546Sopenharmony_ci 1044bf215546Sopenharmony_ci if (max_stream >= 3) 1045bf215546Sopenharmony_ci offset += num_components[2] * sel->info.base.gs.vertices_out; 1046bf215546Sopenharmony_ci shader->ctx_reg.gs.vgt_gsvs_ring_offset_3 = offset; 1047bf215546Sopenharmony_ci 1048bf215546Sopenharmony_ci if (max_stream >= 4) 1049bf215546Sopenharmony_ci offset += num_components[3] * sel->info.base.gs.vertices_out; 1050bf215546Sopenharmony_ci shader->ctx_reg.gs.vgt_gsvs_ring_itemsize = offset; 1051bf215546Sopenharmony_ci 1052bf215546Sopenharmony_ci /* The GSVS_RING_ITEMSIZE register takes 15 bits */ 1053bf215546Sopenharmony_ci assert(offset < (1 << 15)); 1054bf215546Sopenharmony_ci 1055bf215546Sopenharmony_ci shader->ctx_reg.gs.vgt_gs_max_vert_out = sel->info.base.gs.vertices_out; 1056bf215546Sopenharmony_ci 1057bf215546Sopenharmony_ci shader->ctx_reg.gs.vgt_gs_vert_itemsize = num_components[0]; 1058bf215546Sopenharmony_ci shader->ctx_reg.gs.vgt_gs_vert_itemsize_1 = (max_stream >= 2) ? num_components[1] : 0; 1059bf215546Sopenharmony_ci shader->ctx_reg.gs.vgt_gs_vert_itemsize_2 = (max_stream >= 3) ? num_components[2] : 0; 1060bf215546Sopenharmony_ci shader->ctx_reg.gs.vgt_gs_vert_itemsize_3 = (max_stream >= 4) ? num_components[3] : 0; 1061bf215546Sopenharmony_ci 1062bf215546Sopenharmony_ci shader->ctx_reg.gs.vgt_gs_instance_cnt = 1063bf215546Sopenharmony_ci S_028B90_CNT(MIN2(gs_num_invocations, 127)) | S_028B90_ENABLE(gs_num_invocations > 0); 1064bf215546Sopenharmony_ci 1065bf215546Sopenharmony_ci /* Copy over fields from the GS copy shader to make them easily accessible from GS. */ 1066bf215546Sopenharmony_ci shader->pa_cl_vs_out_cntl = shader->gs_copy_shader->pa_cl_vs_out_cntl; 1067bf215546Sopenharmony_ci 1068bf215546Sopenharmony_ci va = shader->bo->gpu_address; 1069bf215546Sopenharmony_ci 1070bf215546Sopenharmony_ci if (sscreen->info.gfx_level >= GFX9) { 1071bf215546Sopenharmony_ci unsigned input_prim = sel->info.base.gs.input_primitive; 1072bf215546Sopenharmony_ci gl_shader_stage es_stage = shader->key.ge.part.gs.es->stage; 1073bf215546Sopenharmony_ci unsigned es_vgpr_comp_cnt, gs_vgpr_comp_cnt; 1074bf215546Sopenharmony_ci 1075bf215546Sopenharmony_ci if (es_stage == MESA_SHADER_VERTEX) { 1076bf215546Sopenharmony_ci es_vgpr_comp_cnt = si_get_vs_vgpr_comp_cnt(sscreen, shader, false); 1077bf215546Sopenharmony_ci } else if (es_stage == MESA_SHADER_TESS_EVAL) 1078bf215546Sopenharmony_ci es_vgpr_comp_cnt = shader->key.ge.part.gs.es->info.uses_primid ? 3 : 2; 1079bf215546Sopenharmony_ci else 1080bf215546Sopenharmony_ci unreachable("invalid shader selector type"); 1081bf215546Sopenharmony_ci 1082bf215546Sopenharmony_ci /* If offsets 4, 5 are used, GS_VGPR_COMP_CNT is ignored and 1083bf215546Sopenharmony_ci * VGPR[0:4] are always loaded. 1084bf215546Sopenharmony_ci */ 1085bf215546Sopenharmony_ci if (sel->info.uses_invocationid) 1086bf215546Sopenharmony_ci gs_vgpr_comp_cnt = 3; /* VGPR3 contains InvocationID. */ 1087bf215546Sopenharmony_ci else if (sel->info.uses_primid) 1088bf215546Sopenharmony_ci gs_vgpr_comp_cnt = 2; /* VGPR2 contains PrimitiveID. */ 1089bf215546Sopenharmony_ci else if (input_prim >= PIPE_PRIM_TRIANGLES) 1090bf215546Sopenharmony_ci gs_vgpr_comp_cnt = 1; /* VGPR1 contains offsets 2, 3 */ 1091bf215546Sopenharmony_ci else 1092bf215546Sopenharmony_ci gs_vgpr_comp_cnt = 0; /* VGPR0 contains offsets 0, 1 */ 1093bf215546Sopenharmony_ci 1094bf215546Sopenharmony_ci unsigned num_user_sgprs; 1095bf215546Sopenharmony_ci if (es_stage == MESA_SHADER_VERTEX) 1096bf215546Sopenharmony_ci num_user_sgprs = si_get_num_vs_user_sgprs(shader, GFX9_GS_NUM_USER_SGPR); 1097bf215546Sopenharmony_ci else 1098bf215546Sopenharmony_ci num_user_sgprs = GFX9_GS_NUM_USER_SGPR; 1099bf215546Sopenharmony_ci 1100bf215546Sopenharmony_ci if (sscreen->info.gfx_level >= GFX10) { 1101bf215546Sopenharmony_ci si_pm4_set_reg(pm4, R_00B320_SPI_SHADER_PGM_LO_ES, va >> 8); 1102bf215546Sopenharmony_ci } else { 1103bf215546Sopenharmony_ci si_pm4_set_reg(pm4, R_00B210_SPI_SHADER_PGM_LO_ES, va >> 8); 1104bf215546Sopenharmony_ci } 1105bf215546Sopenharmony_ci 1106bf215546Sopenharmony_ci uint32_t rsrc1 = S_00B228_VGPRS((shader->config.num_vgprs - 1) / 4) | S_00B228_DX10_CLAMP(1) | 1107bf215546Sopenharmony_ci S_00B228_MEM_ORDERED(si_shader_mem_ordered(shader)) | 1108bf215546Sopenharmony_ci S_00B228_WGP_MODE(sscreen->info.gfx_level >= GFX10) | 1109bf215546Sopenharmony_ci S_00B228_FLOAT_MODE(shader->config.float_mode) | 1110bf215546Sopenharmony_ci S_00B228_GS_VGPR_COMP_CNT(gs_vgpr_comp_cnt); 1111bf215546Sopenharmony_ci uint32_t rsrc2 = S_00B22C_USER_SGPR(num_user_sgprs) | 1112bf215546Sopenharmony_ci S_00B22C_ES_VGPR_COMP_CNT(es_vgpr_comp_cnt) | 1113bf215546Sopenharmony_ci S_00B22C_OC_LDS_EN(es_stage == MESA_SHADER_TESS_EVAL) | 1114bf215546Sopenharmony_ci S_00B22C_LDS_SIZE(shader->config.lds_size) | 1115bf215546Sopenharmony_ci S_00B22C_SCRATCH_EN(shader->config.scratch_bytes_per_wave > 0); 1116bf215546Sopenharmony_ci 1117bf215546Sopenharmony_ci if (sscreen->info.gfx_level >= GFX10) { 1118bf215546Sopenharmony_ci rsrc2 |= S_00B22C_USER_SGPR_MSB_GFX10(num_user_sgprs >> 5); 1119bf215546Sopenharmony_ci } else { 1120bf215546Sopenharmony_ci rsrc1 |= S_00B228_SGPRS((shader->config.num_sgprs - 1) / 8); 1121bf215546Sopenharmony_ci rsrc2 |= S_00B22C_USER_SGPR_MSB_GFX9(num_user_sgprs >> 5); 1122bf215546Sopenharmony_ci } 1123bf215546Sopenharmony_ci 1124bf215546Sopenharmony_ci si_pm4_set_reg(pm4, R_00B228_SPI_SHADER_PGM_RSRC1_GS, rsrc1); 1125bf215546Sopenharmony_ci si_pm4_set_reg(pm4, R_00B22C_SPI_SHADER_PGM_RSRC2_GS, rsrc2); 1126bf215546Sopenharmony_ci 1127bf215546Sopenharmony_ci shader->ctx_reg.gs.spi_shader_pgm_rsrc3_gs = S_00B21C_CU_EN(0xffff) | 1128bf215546Sopenharmony_ci S_00B21C_WAVE_LIMIT(0x3F); 1129bf215546Sopenharmony_ci shader->ctx_reg.gs.spi_shader_pgm_rsrc4_gs = 1130bf215546Sopenharmony_ci (sscreen->info.gfx_level >= GFX11 ? S_00B204_CU_EN_GFX11(1) : S_00B204_CU_EN_GFX10(0xffff)) | 1131bf215546Sopenharmony_ci S_00B204_SPI_SHADER_LATE_ALLOC_GS_GFX10(0); 1132bf215546Sopenharmony_ci 1133bf215546Sopenharmony_ci shader->ctx_reg.gs.vgt_gs_onchip_cntl = 1134bf215546Sopenharmony_ci S_028A44_ES_VERTS_PER_SUBGRP(shader->gs_info.es_verts_per_subgroup) | 1135bf215546Sopenharmony_ci S_028A44_GS_PRIMS_PER_SUBGRP(shader->gs_info.gs_prims_per_subgroup) | 1136bf215546Sopenharmony_ci S_028A44_GS_INST_PRIMS_IN_SUBGRP(shader->gs_info.gs_inst_prims_in_subgroup); 1137bf215546Sopenharmony_ci shader->ctx_reg.gs.vgt_gs_max_prims_per_subgroup = 1138bf215546Sopenharmony_ci S_028A94_MAX_PRIMS_PER_SUBGROUP(shader->gs_info.max_prims_per_subgroup); 1139bf215546Sopenharmony_ci shader->ctx_reg.gs.vgt_esgs_ring_itemsize = shader->key.ge.part.gs.es->info.esgs_itemsize / 4; 1140bf215546Sopenharmony_ci 1141bf215546Sopenharmony_ci if (es_stage == MESA_SHADER_TESS_EVAL) 1142bf215546Sopenharmony_ci si_set_tesseval_regs(sscreen, shader->key.ge.part.gs.es, shader); 1143bf215546Sopenharmony_ci 1144bf215546Sopenharmony_ci polaris_set_vgt_vertex_reuse(sscreen, shader->key.ge.part.gs.es, shader); 1145bf215546Sopenharmony_ci } else { 1146bf215546Sopenharmony_ci shader->ctx_reg.gs.spi_shader_pgm_rsrc3_gs = S_00B21C_CU_EN(0xffff) | 1147bf215546Sopenharmony_ci S_00B21C_WAVE_LIMIT(0x3F); 1148bf215546Sopenharmony_ci 1149bf215546Sopenharmony_ci si_pm4_set_reg(pm4, R_00B220_SPI_SHADER_PGM_LO_GS, va >> 8); 1150bf215546Sopenharmony_ci si_pm4_set_reg(pm4, R_00B224_SPI_SHADER_PGM_HI_GS, 1151bf215546Sopenharmony_ci S_00B224_MEM_BASE(sscreen->info.address32_hi >> 8)); 1152bf215546Sopenharmony_ci 1153bf215546Sopenharmony_ci si_pm4_set_reg(pm4, R_00B228_SPI_SHADER_PGM_RSRC1_GS, 1154bf215546Sopenharmony_ci S_00B228_VGPRS((shader->config.num_vgprs - 1) / 4) | 1155bf215546Sopenharmony_ci S_00B228_SGPRS((shader->config.num_sgprs - 1) / 8) | 1156bf215546Sopenharmony_ci S_00B228_DX10_CLAMP(1) | S_00B228_FLOAT_MODE(shader->config.float_mode)); 1157bf215546Sopenharmony_ci si_pm4_set_reg(pm4, R_00B22C_SPI_SHADER_PGM_RSRC2_GS, 1158bf215546Sopenharmony_ci S_00B22C_USER_SGPR(GFX6_GS_NUM_USER_SGPR) | 1159bf215546Sopenharmony_ci S_00B22C_SCRATCH_EN(shader->config.scratch_bytes_per_wave > 0)); 1160bf215546Sopenharmony_ci } 1161bf215546Sopenharmony_ci} 1162bf215546Sopenharmony_ci 1163bf215546Sopenharmony_cibool gfx10_is_ngg_passthrough(struct si_shader *shader) 1164bf215546Sopenharmony_ci{ 1165bf215546Sopenharmony_ci struct si_shader_selector *sel = shader->selector; 1166bf215546Sopenharmony_ci 1167bf215546Sopenharmony_ci /* Never use NGG passthrough if culling is possible even when it's not used by this shader, 1168bf215546Sopenharmony_ci * so that we don't get context rolls when enabling and disabling NGG passthrough. 1169bf215546Sopenharmony_ci */ 1170bf215546Sopenharmony_ci if (sel->screen->use_ngg_culling) 1171bf215546Sopenharmony_ci return false; 1172bf215546Sopenharmony_ci 1173bf215546Sopenharmony_ci /* The definition of NGG passthrough is: 1174bf215546Sopenharmony_ci * - user GS is turned off (no amplification, no GS instancing, and no culling) 1175bf215546Sopenharmony_ci * - VGT_ESGS_RING_ITEMSIZE is ignored (behaving as if it was equal to 1) 1176bf215546Sopenharmony_ci * - vertex indices are packed into 1 VGPR 1177bf215546Sopenharmony_ci * - Navi23 and later chips can optionally skip the gs_alloc_req message 1178bf215546Sopenharmony_ci * 1179bf215546Sopenharmony_ci * NGG passthrough still allows the use of LDS. 1180bf215546Sopenharmony_ci */ 1181bf215546Sopenharmony_ci return sel->stage != MESA_SHADER_GEOMETRY && !shader->key.ge.opt.ngg_culling; 1182bf215546Sopenharmony_ci} 1183bf215546Sopenharmony_ci 1184bf215546Sopenharmony_ci/* Common tail code for NGG primitive shaders. */ 1185bf215546Sopenharmony_cistatic void gfx10_emit_shader_ngg_tail(struct si_context *sctx, struct si_shader *shader) 1186bf215546Sopenharmony_ci{ 1187bf215546Sopenharmony_ci radeon_begin(&sctx->gfx_cs); 1188bf215546Sopenharmony_ci radeon_opt_set_context_reg(sctx, R_0287FC_GE_MAX_OUTPUT_PER_SUBGROUP, 1189bf215546Sopenharmony_ci SI_TRACKED_GE_MAX_OUTPUT_PER_SUBGROUP, 1190bf215546Sopenharmony_ci shader->ctx_reg.ngg.ge_max_output_per_subgroup); 1191bf215546Sopenharmony_ci radeon_opt_set_context_reg(sctx, R_028B4C_GE_NGG_SUBGRP_CNTL, SI_TRACKED_GE_NGG_SUBGRP_CNTL, 1192bf215546Sopenharmony_ci shader->ctx_reg.ngg.ge_ngg_subgrp_cntl); 1193bf215546Sopenharmony_ci radeon_opt_set_context_reg(sctx, R_028A84_VGT_PRIMITIVEID_EN, SI_TRACKED_VGT_PRIMITIVEID_EN, 1194bf215546Sopenharmony_ci shader->ctx_reg.ngg.vgt_primitiveid_en); 1195bf215546Sopenharmony_ci if (sctx->gfx_level < GFX11) { 1196bf215546Sopenharmony_ci radeon_opt_set_context_reg(sctx, R_028A44_VGT_GS_ONCHIP_CNTL, SI_TRACKED_VGT_GS_ONCHIP_CNTL, 1197bf215546Sopenharmony_ci shader->ctx_reg.ngg.vgt_gs_onchip_cntl); 1198bf215546Sopenharmony_ci } 1199bf215546Sopenharmony_ci radeon_opt_set_context_reg(sctx, R_028B90_VGT_GS_INSTANCE_CNT, SI_TRACKED_VGT_GS_INSTANCE_CNT, 1200bf215546Sopenharmony_ci shader->ctx_reg.ngg.vgt_gs_instance_cnt); 1201bf215546Sopenharmony_ci radeon_opt_set_context_reg(sctx, R_028AAC_VGT_ESGS_RING_ITEMSIZE, 1202bf215546Sopenharmony_ci SI_TRACKED_VGT_ESGS_RING_ITEMSIZE, 1203bf215546Sopenharmony_ci shader->ctx_reg.ngg.vgt_esgs_ring_itemsize); 1204bf215546Sopenharmony_ci radeon_opt_set_context_reg(sctx, R_0286C4_SPI_VS_OUT_CONFIG, SI_TRACKED_SPI_VS_OUT_CONFIG, 1205bf215546Sopenharmony_ci shader->ctx_reg.ngg.spi_vs_out_config); 1206bf215546Sopenharmony_ci radeon_opt_set_context_reg2( 1207bf215546Sopenharmony_ci sctx, R_028708_SPI_SHADER_IDX_FORMAT, SI_TRACKED_SPI_SHADER_IDX_FORMAT, 1208bf215546Sopenharmony_ci shader->ctx_reg.ngg.spi_shader_idx_format, shader->ctx_reg.ngg.spi_shader_pos_format); 1209bf215546Sopenharmony_ci radeon_opt_set_context_reg(sctx, R_028818_PA_CL_VTE_CNTL, SI_TRACKED_PA_CL_VTE_CNTL, 1210bf215546Sopenharmony_ci shader->ctx_reg.ngg.pa_cl_vte_cntl); 1211bf215546Sopenharmony_ci radeon_opt_set_context_reg(sctx, R_028838_PA_CL_NGG_CNTL, SI_TRACKED_PA_CL_NGG_CNTL, 1212bf215546Sopenharmony_ci shader->ctx_reg.ngg.pa_cl_ngg_cntl); 1213bf215546Sopenharmony_ci 1214bf215546Sopenharmony_ci radeon_end_update_context_roll(sctx); 1215bf215546Sopenharmony_ci 1216bf215546Sopenharmony_ci /* These don't cause a context roll. */ 1217bf215546Sopenharmony_ci radeon_begin_again(&sctx->gfx_cs); 1218bf215546Sopenharmony_ci radeon_opt_set_uconfig_reg(sctx, R_030980_GE_PC_ALLOC, SI_TRACKED_GE_PC_ALLOC, 1219bf215546Sopenharmony_ci shader->ctx_reg.ngg.ge_pc_alloc); 1220bf215546Sopenharmony_ci if (sctx->screen->info.spi_cu_en_has_effect) { 1221bf215546Sopenharmony_ci radeon_end(); 1222bf215546Sopenharmony_ci ac_set_reg_cu_en(&sctx->gfx_cs, R_00B21C_SPI_SHADER_PGM_RSRC3_GS, 1223bf215546Sopenharmony_ci shader->ctx_reg.ngg.spi_shader_pgm_rsrc3_gs, 1224bf215546Sopenharmony_ci C_00B21C_CU_EN, 0, &sctx->screen->info, 1225bf215546Sopenharmony_ci (void (*)(void*, unsigned, uint32_t)) 1226bf215546Sopenharmony_ci (sctx->gfx_level >= GFX10 ? radeon_set_sh_reg_idx3_func : radeon_set_sh_reg_func)); 1227bf215546Sopenharmony_ci ac_set_reg_cu_en(&sctx->gfx_cs, R_00B204_SPI_SHADER_PGM_RSRC4_GS, 1228bf215546Sopenharmony_ci shader->ctx_reg.ngg.spi_shader_pgm_rsrc4_gs, 1229bf215546Sopenharmony_ci C_00B204_CU_EN_GFX10, 16, &sctx->screen->info, 1230bf215546Sopenharmony_ci (void (*)(void*, unsigned, uint32_t)) 1231bf215546Sopenharmony_ci (sctx->gfx_level >= GFX10 ? radeon_set_sh_reg_idx3_func : radeon_set_sh_reg_func)); 1232bf215546Sopenharmony_ci sctx->tracked_regs.reg_saved &= ~BITFIELD64_BIT(SI_TRACKED_SPI_SHADER_PGM_RSRC4_GS) & 1233bf215546Sopenharmony_ci ~BITFIELD64_BIT(SI_TRACKED_SPI_SHADER_PGM_RSRC3_GS); 1234bf215546Sopenharmony_ci } else { 1235bf215546Sopenharmony_ci radeon_opt_set_sh_reg_idx3(sctx, R_00B21C_SPI_SHADER_PGM_RSRC3_GS, 1236bf215546Sopenharmony_ci SI_TRACKED_SPI_SHADER_PGM_RSRC3_GS, 1237bf215546Sopenharmony_ci shader->ctx_reg.ngg.spi_shader_pgm_rsrc3_gs); 1238bf215546Sopenharmony_ci radeon_opt_set_sh_reg_idx3(sctx, R_00B204_SPI_SHADER_PGM_RSRC4_GS, 1239bf215546Sopenharmony_ci SI_TRACKED_SPI_SHADER_PGM_RSRC4_GS, 1240bf215546Sopenharmony_ci shader->ctx_reg.ngg.spi_shader_pgm_rsrc4_gs); 1241bf215546Sopenharmony_ci radeon_end(); 1242bf215546Sopenharmony_ci } 1243bf215546Sopenharmony_ci} 1244bf215546Sopenharmony_ci 1245bf215546Sopenharmony_cistatic void gfx10_emit_shader_ngg_notess_nogs(struct si_context *sctx) 1246bf215546Sopenharmony_ci{ 1247bf215546Sopenharmony_ci struct si_shader *shader = sctx->queued.named.gs; 1248bf215546Sopenharmony_ci if (!shader) 1249bf215546Sopenharmony_ci return; 1250bf215546Sopenharmony_ci 1251bf215546Sopenharmony_ci gfx10_emit_shader_ngg_tail(sctx, shader); 1252bf215546Sopenharmony_ci} 1253bf215546Sopenharmony_ci 1254bf215546Sopenharmony_cistatic void gfx10_emit_shader_ngg_tess_nogs(struct si_context *sctx) 1255bf215546Sopenharmony_ci{ 1256bf215546Sopenharmony_ci struct si_shader *shader = sctx->queued.named.gs; 1257bf215546Sopenharmony_ci if (!shader) 1258bf215546Sopenharmony_ci return; 1259bf215546Sopenharmony_ci 1260bf215546Sopenharmony_ci radeon_begin(&sctx->gfx_cs); 1261bf215546Sopenharmony_ci radeon_opt_set_context_reg(sctx, R_028B6C_VGT_TF_PARAM, SI_TRACKED_VGT_TF_PARAM, 1262bf215546Sopenharmony_ci shader->vgt_tf_param); 1263bf215546Sopenharmony_ci radeon_end_update_context_roll(sctx); 1264bf215546Sopenharmony_ci 1265bf215546Sopenharmony_ci gfx10_emit_shader_ngg_tail(sctx, shader); 1266bf215546Sopenharmony_ci} 1267bf215546Sopenharmony_ci 1268bf215546Sopenharmony_cistatic void gfx10_emit_shader_ngg_notess_gs(struct si_context *sctx) 1269bf215546Sopenharmony_ci{ 1270bf215546Sopenharmony_ci struct si_shader *shader = sctx->queued.named.gs; 1271bf215546Sopenharmony_ci if (!shader) 1272bf215546Sopenharmony_ci return; 1273bf215546Sopenharmony_ci 1274bf215546Sopenharmony_ci radeon_begin(&sctx->gfx_cs); 1275bf215546Sopenharmony_ci radeon_opt_set_context_reg(sctx, R_028B38_VGT_GS_MAX_VERT_OUT, SI_TRACKED_VGT_GS_MAX_VERT_OUT, 1276bf215546Sopenharmony_ci shader->ctx_reg.ngg.vgt_gs_max_vert_out); 1277bf215546Sopenharmony_ci radeon_end_update_context_roll(sctx); 1278bf215546Sopenharmony_ci 1279bf215546Sopenharmony_ci gfx10_emit_shader_ngg_tail(sctx, shader); 1280bf215546Sopenharmony_ci} 1281bf215546Sopenharmony_ci 1282bf215546Sopenharmony_cistatic void gfx10_emit_shader_ngg_tess_gs(struct si_context *sctx) 1283bf215546Sopenharmony_ci{ 1284bf215546Sopenharmony_ci struct si_shader *shader = sctx->queued.named.gs; 1285bf215546Sopenharmony_ci 1286bf215546Sopenharmony_ci if (!shader) 1287bf215546Sopenharmony_ci return; 1288bf215546Sopenharmony_ci 1289bf215546Sopenharmony_ci radeon_begin(&sctx->gfx_cs); 1290bf215546Sopenharmony_ci radeon_opt_set_context_reg(sctx, R_028B38_VGT_GS_MAX_VERT_OUT, SI_TRACKED_VGT_GS_MAX_VERT_OUT, 1291bf215546Sopenharmony_ci shader->ctx_reg.ngg.vgt_gs_max_vert_out); 1292bf215546Sopenharmony_ci radeon_opt_set_context_reg(sctx, R_028B6C_VGT_TF_PARAM, SI_TRACKED_VGT_TF_PARAM, 1293bf215546Sopenharmony_ci shader->vgt_tf_param); 1294bf215546Sopenharmony_ci radeon_end_update_context_roll(sctx); 1295bf215546Sopenharmony_ci 1296bf215546Sopenharmony_ci gfx10_emit_shader_ngg_tail(sctx, shader); 1297bf215546Sopenharmony_ci} 1298bf215546Sopenharmony_ci 1299bf215546Sopenharmony_ciunsigned si_get_input_prim(const struct si_shader_selector *gs, const union si_shader_key *key) 1300bf215546Sopenharmony_ci{ 1301bf215546Sopenharmony_ci if (gs->stage == MESA_SHADER_GEOMETRY) 1302bf215546Sopenharmony_ci return gs->info.base.gs.input_primitive; 1303bf215546Sopenharmony_ci 1304bf215546Sopenharmony_ci if (gs->stage == MESA_SHADER_TESS_EVAL) { 1305bf215546Sopenharmony_ci if (gs->info.base.tess.point_mode) 1306bf215546Sopenharmony_ci return PIPE_PRIM_POINTS; 1307bf215546Sopenharmony_ci if (gs->info.base.tess._primitive_mode == TESS_PRIMITIVE_ISOLINES) 1308bf215546Sopenharmony_ci return PIPE_PRIM_LINES; 1309bf215546Sopenharmony_ci return PIPE_PRIM_TRIANGLES; 1310bf215546Sopenharmony_ci } 1311bf215546Sopenharmony_ci 1312bf215546Sopenharmony_ci if (key->ge.opt.ngg_culling & SI_NGG_CULL_LINES) 1313bf215546Sopenharmony_ci return PIPE_PRIM_LINES; 1314bf215546Sopenharmony_ci 1315bf215546Sopenharmony_ci return PIPE_PRIM_TRIANGLES; /* worst case for all callers */ 1316bf215546Sopenharmony_ci} 1317bf215546Sopenharmony_ci 1318bf215546Sopenharmony_cistatic unsigned si_get_vs_out_cntl(const struct si_shader_selector *sel, 1319bf215546Sopenharmony_ci const struct si_shader *shader, bool ngg) 1320bf215546Sopenharmony_ci{ 1321bf215546Sopenharmony_ci /* Clip distances can be killed, but cull distances can't. */ 1322bf215546Sopenharmony_ci unsigned clipcull_mask = (sel->info.clipdist_mask & ~shader->key.ge.opt.kill_clip_distances) | 1323bf215546Sopenharmony_ci sel->info.culldist_mask; 1324bf215546Sopenharmony_ci bool writes_psize = sel->info.writes_psize && !shader->key.ge.opt.kill_pointsize; 1325bf215546Sopenharmony_ci bool misc_vec_ena = writes_psize || (sel->info.writes_edgeflag && !ngg) || 1326bf215546Sopenharmony_ci sel->screen->options.vrs2x2 || 1327bf215546Sopenharmony_ci sel->info.writes_layer || sel->info.writes_viewport_index; 1328bf215546Sopenharmony_ci 1329bf215546Sopenharmony_ci return S_02881C_VS_OUT_CCDIST0_VEC_ENA((clipcull_mask & 0x0F) != 0) | 1330bf215546Sopenharmony_ci S_02881C_VS_OUT_CCDIST1_VEC_ENA((clipcull_mask & 0xF0) != 0) | 1331bf215546Sopenharmony_ci S_02881C_USE_VTX_POINT_SIZE(writes_psize) | 1332bf215546Sopenharmony_ci S_02881C_USE_VTX_EDGE_FLAG(sel->info.writes_edgeflag && !ngg) | 1333bf215546Sopenharmony_ci S_02881C_USE_VTX_VRS_RATE(sel->screen->options.vrs2x2) | 1334bf215546Sopenharmony_ci S_02881C_USE_VTX_RENDER_TARGET_INDX(sel->info.writes_layer) | 1335bf215546Sopenharmony_ci S_02881C_USE_VTX_VIEWPORT_INDX(sel->info.writes_viewport_index) | 1336bf215546Sopenharmony_ci S_02881C_VS_OUT_MISC_VEC_ENA(misc_vec_ena) | 1337bf215546Sopenharmony_ci S_02881C_VS_OUT_MISC_SIDE_BUS_ENA(misc_vec_ena); 1338bf215546Sopenharmony_ci} 1339bf215546Sopenharmony_ci 1340bf215546Sopenharmony_ci/** 1341bf215546Sopenharmony_ci * Prepare the PM4 image for \p shader, which will run as a merged ESGS shader 1342bf215546Sopenharmony_ci * in NGG mode. 1343bf215546Sopenharmony_ci */ 1344bf215546Sopenharmony_cistatic void gfx10_shader_ngg(struct si_screen *sscreen, struct si_shader *shader) 1345bf215546Sopenharmony_ci{ 1346bf215546Sopenharmony_ci const struct si_shader_selector *gs_sel = shader->selector; 1347bf215546Sopenharmony_ci const struct si_shader_info *gs_info = &gs_sel->info; 1348bf215546Sopenharmony_ci const gl_shader_stage gs_stage = shader->selector->stage; 1349bf215546Sopenharmony_ci const struct si_shader_selector *es_sel = 1350bf215546Sopenharmony_ci shader->previous_stage_sel ? shader->previous_stage_sel : shader->selector; 1351bf215546Sopenharmony_ci const struct si_shader_info *es_info = &es_sel->info; 1352bf215546Sopenharmony_ci const gl_shader_stage es_stage = es_sel->stage; 1353bf215546Sopenharmony_ci unsigned num_user_sgprs; 1354bf215546Sopenharmony_ci unsigned nparams, es_vgpr_comp_cnt, gs_vgpr_comp_cnt; 1355bf215546Sopenharmony_ci uint64_t va; 1356bf215546Sopenharmony_ci bool window_space = gs_sel->stage == MESA_SHADER_VERTEX ? 1357bf215546Sopenharmony_ci gs_info->base.vs.window_space_position : 0; 1358bf215546Sopenharmony_ci bool es_enable_prim_id = shader->key.ge.mono.u.vs_export_prim_id || es_info->uses_primid; 1359bf215546Sopenharmony_ci unsigned gs_num_invocations = gs_sel->stage == MESA_SHADER_GEOMETRY ? 1360bf215546Sopenharmony_ci MAX2(gs_info->base.gs.invocations, 1) : 0; 1361bf215546Sopenharmony_ci unsigned input_prim = si_get_input_prim(gs_sel, &shader->key); 1362bf215546Sopenharmony_ci bool break_wave_at_eoi = false; 1363bf215546Sopenharmony_ci struct si_pm4_state *pm4 = si_get_shader_pm4_state(shader); 1364bf215546Sopenharmony_ci if (!pm4) 1365bf215546Sopenharmony_ci return; 1366bf215546Sopenharmony_ci 1367bf215546Sopenharmony_ci if (es_stage == MESA_SHADER_TESS_EVAL) { 1368bf215546Sopenharmony_ci pm4->atom.emit = gs_stage == MESA_SHADER_GEOMETRY ? gfx10_emit_shader_ngg_tess_gs 1369bf215546Sopenharmony_ci : gfx10_emit_shader_ngg_tess_nogs; 1370bf215546Sopenharmony_ci } else { 1371bf215546Sopenharmony_ci pm4->atom.emit = gs_stage == MESA_SHADER_GEOMETRY ? gfx10_emit_shader_ngg_notess_gs 1372bf215546Sopenharmony_ci : gfx10_emit_shader_ngg_notess_nogs; 1373bf215546Sopenharmony_ci } 1374bf215546Sopenharmony_ci 1375bf215546Sopenharmony_ci va = shader->bo->gpu_address; 1376bf215546Sopenharmony_ci 1377bf215546Sopenharmony_ci if (es_stage == MESA_SHADER_VERTEX) { 1378bf215546Sopenharmony_ci es_vgpr_comp_cnt = si_get_vs_vgpr_comp_cnt(sscreen, shader, false); 1379bf215546Sopenharmony_ci 1380bf215546Sopenharmony_ci if (es_info->base.vs.blit_sgprs_amd) { 1381bf215546Sopenharmony_ci num_user_sgprs = 1382bf215546Sopenharmony_ci SI_SGPR_VS_BLIT_DATA + es_info->base.vs.blit_sgprs_amd; 1383bf215546Sopenharmony_ci } else { 1384bf215546Sopenharmony_ci num_user_sgprs = si_get_num_vs_user_sgprs(shader, GFX9_GS_NUM_USER_SGPR); 1385bf215546Sopenharmony_ci } 1386bf215546Sopenharmony_ci } else { 1387bf215546Sopenharmony_ci assert(es_stage == MESA_SHADER_TESS_EVAL); 1388bf215546Sopenharmony_ci es_vgpr_comp_cnt = es_enable_prim_id ? 3 : 2; 1389bf215546Sopenharmony_ci num_user_sgprs = GFX9_GS_NUM_USER_SGPR; 1390bf215546Sopenharmony_ci 1391bf215546Sopenharmony_ci if (es_enable_prim_id || gs_info->uses_primid) 1392bf215546Sopenharmony_ci break_wave_at_eoi = true; 1393bf215546Sopenharmony_ci } 1394bf215546Sopenharmony_ci 1395bf215546Sopenharmony_ci /* If offsets 4, 5 are used, GS_VGPR_COMP_CNT is ignored and 1396bf215546Sopenharmony_ci * VGPR[0:4] are always loaded. 1397bf215546Sopenharmony_ci * 1398bf215546Sopenharmony_ci * Vertex shaders always need to load VGPR3, because they need to 1399bf215546Sopenharmony_ci * pass edge flags for decomposed primitives (such as quads) to the PA 1400bf215546Sopenharmony_ci * for the GL_LINE polygon mode to skip rendering lines on inner edges. 1401bf215546Sopenharmony_ci */ 1402bf215546Sopenharmony_ci if (gs_info->uses_invocationid || 1403bf215546Sopenharmony_ci (gfx10_edgeflags_have_effect(shader) && !gfx10_is_ngg_passthrough(shader))) 1404bf215546Sopenharmony_ci gs_vgpr_comp_cnt = 3; /* VGPR3 contains InvocationID, edge flags. */ 1405bf215546Sopenharmony_ci else if ((gs_stage == MESA_SHADER_GEOMETRY && gs_info->uses_primid) || 1406bf215546Sopenharmony_ci (gs_stage == MESA_SHADER_VERTEX && shader->key.ge.mono.u.vs_export_prim_id)) 1407bf215546Sopenharmony_ci gs_vgpr_comp_cnt = 2; /* VGPR2 contains PrimitiveID. */ 1408bf215546Sopenharmony_ci else if (input_prim >= PIPE_PRIM_TRIANGLES && !gfx10_is_ngg_passthrough(shader)) 1409bf215546Sopenharmony_ci gs_vgpr_comp_cnt = 1; /* VGPR1 contains offsets 2, 3 */ 1410bf215546Sopenharmony_ci else 1411bf215546Sopenharmony_ci gs_vgpr_comp_cnt = 0; /* VGPR0 contains offsets 0, 1 */ 1412bf215546Sopenharmony_ci 1413bf215546Sopenharmony_ci unsigned late_alloc_wave64, cu_mask; 1414bf215546Sopenharmony_ci 1415bf215546Sopenharmony_ci ac_compute_late_alloc(&sscreen->info, true, shader->key.ge.opt.ngg_culling, 1416bf215546Sopenharmony_ci shader->config.scratch_bytes_per_wave > 0, 1417bf215546Sopenharmony_ci &late_alloc_wave64, &cu_mask); 1418bf215546Sopenharmony_ci 1419bf215546Sopenharmony_ci si_pm4_set_reg(pm4, R_00B320_SPI_SHADER_PGM_LO_ES, va >> 8); 1420bf215546Sopenharmony_ci si_pm4_set_reg( 1421bf215546Sopenharmony_ci pm4, R_00B228_SPI_SHADER_PGM_RSRC1_GS, 1422bf215546Sopenharmony_ci S_00B228_VGPRS((shader->config.num_vgprs - 1) / (shader->wave_size == 32 ? 8 : 4)) | 1423bf215546Sopenharmony_ci S_00B228_FLOAT_MODE(shader->config.float_mode) | S_00B228_DX10_CLAMP(1) | 1424bf215546Sopenharmony_ci S_00B228_MEM_ORDERED(si_shader_mem_ordered(shader)) | 1425bf215546Sopenharmony_ci /* Disable the WGP mode on gfx10.3 because it can hang. (it happened on VanGogh) 1426bf215546Sopenharmony_ci * Let's disable it on all chips that disable exactly 1 CU per SA for GS. */ 1427bf215546Sopenharmony_ci S_00B228_WGP_MODE(sscreen->info.gfx_level == GFX10) | 1428bf215546Sopenharmony_ci S_00B228_GS_VGPR_COMP_CNT(gs_vgpr_comp_cnt)); 1429bf215546Sopenharmony_ci si_pm4_set_reg(pm4, R_00B22C_SPI_SHADER_PGM_RSRC2_GS, 1430bf215546Sopenharmony_ci S_00B22C_SCRATCH_EN(shader->config.scratch_bytes_per_wave > 0) | 1431bf215546Sopenharmony_ci S_00B22C_USER_SGPR(num_user_sgprs) | 1432bf215546Sopenharmony_ci S_00B22C_ES_VGPR_COMP_CNT(es_vgpr_comp_cnt) | 1433bf215546Sopenharmony_ci S_00B22C_USER_SGPR_MSB_GFX10(num_user_sgprs >> 5) | 1434bf215546Sopenharmony_ci S_00B22C_OC_LDS_EN(es_stage == MESA_SHADER_TESS_EVAL) | 1435bf215546Sopenharmony_ci S_00B22C_LDS_SIZE(shader->config.lds_size)); 1436bf215546Sopenharmony_ci 1437bf215546Sopenharmony_ci shader->ctx_reg.ngg.spi_shader_pgm_rsrc3_gs = S_00B21C_CU_EN(cu_mask) | 1438bf215546Sopenharmony_ci S_00B21C_WAVE_LIMIT(0x3F); 1439bf215546Sopenharmony_ci if (sscreen->info.gfx_level >= GFX11) { 1440bf215546Sopenharmony_ci shader->ctx_reg.ngg.spi_shader_pgm_rsrc4_gs = 1441bf215546Sopenharmony_ci S_00B204_CU_EN_GFX11(0x1) | S_00B204_SPI_SHADER_LATE_ALLOC_GS_GFX10(late_alloc_wave64) | 1442bf215546Sopenharmony_ci S_00B204_INST_PREF_SIZE(si_get_shader_prefetch_size(shader)); 1443bf215546Sopenharmony_ci } else { 1444bf215546Sopenharmony_ci shader->ctx_reg.ngg.spi_shader_pgm_rsrc4_gs = 1445bf215546Sopenharmony_ci S_00B204_CU_EN_GFX10(0xffff) | S_00B204_SPI_SHADER_LATE_ALLOC_GS_GFX10(late_alloc_wave64); 1446bf215546Sopenharmony_ci } 1447bf215546Sopenharmony_ci 1448bf215546Sopenharmony_ci nparams = MAX2(shader->info.nr_param_exports, 1); 1449bf215546Sopenharmony_ci shader->ctx_reg.ngg.spi_vs_out_config = 1450bf215546Sopenharmony_ci S_0286C4_VS_EXPORT_COUNT(nparams - 1) | 1451bf215546Sopenharmony_ci S_0286C4_NO_PC_EXPORT(shader->info.nr_param_exports == 0); 1452bf215546Sopenharmony_ci 1453bf215546Sopenharmony_ci shader->ctx_reg.ngg.spi_shader_idx_format = 1454bf215546Sopenharmony_ci S_028708_IDX0_EXPORT_FORMAT(V_028708_SPI_SHADER_1COMP); 1455bf215546Sopenharmony_ci shader->ctx_reg.ngg.spi_shader_pos_format = 1456bf215546Sopenharmony_ci S_02870C_POS0_EXPORT_FORMAT(V_02870C_SPI_SHADER_4COMP) | 1457bf215546Sopenharmony_ci S_02870C_POS1_EXPORT_FORMAT(shader->info.nr_pos_exports > 1 ? V_02870C_SPI_SHADER_4COMP 1458bf215546Sopenharmony_ci : V_02870C_SPI_SHADER_NONE) | 1459bf215546Sopenharmony_ci S_02870C_POS2_EXPORT_FORMAT(shader->info.nr_pos_exports > 2 ? V_02870C_SPI_SHADER_4COMP 1460bf215546Sopenharmony_ci : V_02870C_SPI_SHADER_NONE) | 1461bf215546Sopenharmony_ci S_02870C_POS3_EXPORT_FORMAT(shader->info.nr_pos_exports > 3 ? V_02870C_SPI_SHADER_4COMP 1462bf215546Sopenharmony_ci : V_02870C_SPI_SHADER_NONE); 1463bf215546Sopenharmony_ci 1464bf215546Sopenharmony_ci shader->ctx_reg.ngg.vgt_primitiveid_en = 1465bf215546Sopenharmony_ci S_028A84_PRIMITIVEID_EN(es_enable_prim_id) | 1466bf215546Sopenharmony_ci S_028A84_NGG_DISABLE_PROVOK_REUSE(shader->key.ge.mono.u.vs_export_prim_id || 1467bf215546Sopenharmony_ci gs_sel->info.writes_primid); 1468bf215546Sopenharmony_ci 1469bf215546Sopenharmony_ci if (gs_stage == MESA_SHADER_GEOMETRY) { 1470bf215546Sopenharmony_ci shader->ctx_reg.ngg.vgt_esgs_ring_itemsize = es_sel->info.esgs_itemsize / 4; 1471bf215546Sopenharmony_ci shader->ctx_reg.ngg.vgt_gs_max_vert_out = gs_sel->info.base.gs.vertices_out; 1472bf215546Sopenharmony_ci } else { 1473bf215546Sopenharmony_ci shader->ctx_reg.ngg.vgt_esgs_ring_itemsize = 1; 1474bf215546Sopenharmony_ci } 1475bf215546Sopenharmony_ci 1476bf215546Sopenharmony_ci if (es_stage == MESA_SHADER_TESS_EVAL) 1477bf215546Sopenharmony_ci si_set_tesseval_regs(sscreen, es_sel, shader); 1478bf215546Sopenharmony_ci 1479bf215546Sopenharmony_ci shader->ctx_reg.ngg.ge_max_output_per_subgroup = 1480bf215546Sopenharmony_ci S_0287FC_MAX_VERTS_PER_SUBGROUP(shader->ngg.max_out_verts); 1481bf215546Sopenharmony_ci shader->ctx_reg.ngg.ge_ngg_subgrp_cntl = S_028B4C_PRIM_AMP_FACTOR(shader->ngg.prim_amp_factor) | 1482bf215546Sopenharmony_ci S_028B4C_THDS_PER_SUBGRP(0); /* for fast launch */ 1483bf215546Sopenharmony_ci shader->ctx_reg.ngg.vgt_gs_instance_cnt = 1484bf215546Sopenharmony_ci S_028B90_CNT(gs_num_invocations) | S_028B90_ENABLE(gs_num_invocations > 1) | 1485bf215546Sopenharmony_ci S_028B90_EN_MAX_VERT_OUT_PER_GS_INSTANCE(shader->ngg.max_vert_out_per_gs_instance); 1486bf215546Sopenharmony_ci 1487bf215546Sopenharmony_ci /* Output hw-generated edge flags if needed and pass them via the prim 1488bf215546Sopenharmony_ci * export to prevent drawing lines on internal edges of decomposed 1489bf215546Sopenharmony_ci * primitives (such as quads) with polygon mode = lines. 1490bf215546Sopenharmony_ci */ 1491bf215546Sopenharmony_ci shader->ctx_reg.ngg.pa_cl_ngg_cntl = 1492bf215546Sopenharmony_ci S_028838_INDEX_BUF_EDGE_FLAG_ENA(gfx10_edgeflags_have_effect(shader)) | 1493bf215546Sopenharmony_ci /* Reuse for NGG. */ 1494bf215546Sopenharmony_ci S_028838_VERTEX_REUSE_DEPTH(sscreen->info.gfx_level >= GFX10_3 ? 30 : 0); 1495bf215546Sopenharmony_ci shader->pa_cl_vs_out_cntl = si_get_vs_out_cntl(shader->selector, shader, true); 1496bf215546Sopenharmony_ci 1497bf215546Sopenharmony_ci /* Oversubscribe PC. This improves performance when there are too many varyings. */ 1498bf215546Sopenharmony_ci unsigned oversub_pc_factor = 1; 1499bf215546Sopenharmony_ci 1500bf215546Sopenharmony_ci if (shader->key.ge.opt.ngg_culling) { 1501bf215546Sopenharmony_ci /* Be more aggressive with NGG culling. */ 1502bf215546Sopenharmony_ci if (shader->info.nr_param_exports > 4) 1503bf215546Sopenharmony_ci oversub_pc_factor = 4; 1504bf215546Sopenharmony_ci else if (shader->info.nr_param_exports > 2) 1505bf215546Sopenharmony_ci oversub_pc_factor = 3; 1506bf215546Sopenharmony_ci else 1507bf215546Sopenharmony_ci oversub_pc_factor = 2; 1508bf215546Sopenharmony_ci } 1509bf215546Sopenharmony_ci 1510bf215546Sopenharmony_ci unsigned oversub_pc_lines = 1511bf215546Sopenharmony_ci late_alloc_wave64 ? (sscreen->info.pc_lines / 4) * oversub_pc_factor : 0; 1512bf215546Sopenharmony_ci shader->ctx_reg.ngg.ge_pc_alloc = S_030980_OVERSUB_EN(oversub_pc_lines > 0) | 1513bf215546Sopenharmony_ci S_030980_NUM_PC_LINES(oversub_pc_lines - 1); 1514bf215546Sopenharmony_ci 1515bf215546Sopenharmony_ci if (sscreen->info.gfx_level >= GFX11) { 1516bf215546Sopenharmony_ci shader->ge_cntl = S_03096C_PRIMS_PER_SUBGRP(shader->ngg.max_gsprims) | 1517bf215546Sopenharmony_ci S_03096C_VERTS_PER_SUBGRP(shader->ngg.hw_max_esverts) | 1518bf215546Sopenharmony_ci S_03096C_BREAK_PRIMGRP_AT_EOI(break_wave_at_eoi) | 1519bf215546Sopenharmony_ci S_03096C_PRIM_GRP_SIZE_GFX11(252 / MAX2(shader->ngg.prim_amp_factor, 1)); 1520bf215546Sopenharmony_ci } else { 1521bf215546Sopenharmony_ci shader->ge_cntl = S_03096C_PRIM_GRP_SIZE_GFX10(shader->ngg.max_gsprims) | 1522bf215546Sopenharmony_ci S_03096C_VERT_GRP_SIZE(shader->ngg.hw_max_esverts) | 1523bf215546Sopenharmony_ci S_03096C_BREAK_WAVE_AT_EOI(break_wave_at_eoi); 1524bf215546Sopenharmony_ci 1525bf215546Sopenharmony_ci shader->ctx_reg.ngg.vgt_gs_onchip_cntl = 1526bf215546Sopenharmony_ci S_028A44_ES_VERTS_PER_SUBGRP(shader->ngg.hw_max_esverts) | 1527bf215546Sopenharmony_ci S_028A44_GS_PRIMS_PER_SUBGRP(shader->ngg.max_gsprims) | 1528bf215546Sopenharmony_ci S_028A44_GS_INST_PRIMS_IN_SUBGRP(shader->ngg.max_gsprims * gs_num_invocations); 1529bf215546Sopenharmony_ci } 1530bf215546Sopenharmony_ci 1531bf215546Sopenharmony_ci /* On gfx10, the GE only checks against the maximum number of ES verts after 1532bf215546Sopenharmony_ci * allocating a full GS primitive. So we need to ensure that whenever 1533bf215546Sopenharmony_ci * this check passes, there is enough space for a full primitive without 1534bf215546Sopenharmony_ci * vertex reuse. VERT_GRP_SIZE=256 doesn't need this. We should always get 256 1535bf215546Sopenharmony_ci * if we have enough LDS. 1536bf215546Sopenharmony_ci * 1537bf215546Sopenharmony_ci * Tessellation is unaffected because it always sets GE_CNTL.VERT_GRP_SIZE = 0. 1538bf215546Sopenharmony_ci */ 1539bf215546Sopenharmony_ci if ((sscreen->info.gfx_level == GFX10) && 1540bf215546Sopenharmony_ci (es_stage == MESA_SHADER_VERTEX || gs_stage == MESA_SHADER_VERTEX) && /* = no tess */ 1541bf215546Sopenharmony_ci shader->ngg.hw_max_esverts != 256 && 1542bf215546Sopenharmony_ci shader->ngg.hw_max_esverts > 5) { 1543bf215546Sopenharmony_ci /* This could be based on the input primitive type. 5 is the worst case 1544bf215546Sopenharmony_ci * for primitive types with adjacency. 1545bf215546Sopenharmony_ci */ 1546bf215546Sopenharmony_ci shader->ge_cntl &= C_03096C_VERT_GRP_SIZE; 1547bf215546Sopenharmony_ci shader->ge_cntl |= S_03096C_VERT_GRP_SIZE(shader->ngg.hw_max_esverts - 5); 1548bf215546Sopenharmony_ci } 1549bf215546Sopenharmony_ci 1550bf215546Sopenharmony_ci if (window_space) { 1551bf215546Sopenharmony_ci shader->ctx_reg.ngg.pa_cl_vte_cntl = S_028818_VTX_XY_FMT(1) | S_028818_VTX_Z_FMT(1); 1552bf215546Sopenharmony_ci } else { 1553bf215546Sopenharmony_ci shader->ctx_reg.ngg.pa_cl_vte_cntl = 1554bf215546Sopenharmony_ci S_028818_VTX_W0_FMT(1) | S_028818_VPORT_X_SCALE_ENA(1) | S_028818_VPORT_X_OFFSET_ENA(1) | 1555bf215546Sopenharmony_ci S_028818_VPORT_Y_SCALE_ENA(1) | S_028818_VPORT_Y_OFFSET_ENA(1) | 1556bf215546Sopenharmony_ci S_028818_VPORT_Z_SCALE_ENA(1) | S_028818_VPORT_Z_OFFSET_ENA(1); 1557bf215546Sopenharmony_ci } 1558bf215546Sopenharmony_ci 1559bf215546Sopenharmony_ci shader->ctx_reg.ngg.vgt_stages.u.ngg = 1; 1560bf215546Sopenharmony_ci shader->ctx_reg.ngg.vgt_stages.u.streamout = si_shader_uses_streamout(shader); 1561bf215546Sopenharmony_ci shader->ctx_reg.ngg.vgt_stages.u.ngg_passthrough = gfx10_is_ngg_passthrough(shader); 1562bf215546Sopenharmony_ci shader->ctx_reg.ngg.vgt_stages.u.gs_wave32 = shader->wave_size == 32; 1563bf215546Sopenharmony_ci} 1564bf215546Sopenharmony_ci 1565bf215546Sopenharmony_cistatic void si_emit_shader_vs(struct si_context *sctx) 1566bf215546Sopenharmony_ci{ 1567bf215546Sopenharmony_ci struct si_shader *shader = sctx->queued.named.vs; 1568bf215546Sopenharmony_ci if (!shader) 1569bf215546Sopenharmony_ci return; 1570bf215546Sopenharmony_ci 1571bf215546Sopenharmony_ci radeon_begin(&sctx->gfx_cs); 1572bf215546Sopenharmony_ci radeon_opt_set_context_reg(sctx, R_028A40_VGT_GS_MODE, SI_TRACKED_VGT_GS_MODE, 1573bf215546Sopenharmony_ci shader->ctx_reg.vs.vgt_gs_mode); 1574bf215546Sopenharmony_ci radeon_opt_set_context_reg(sctx, R_028A84_VGT_PRIMITIVEID_EN, SI_TRACKED_VGT_PRIMITIVEID_EN, 1575bf215546Sopenharmony_ci shader->ctx_reg.vs.vgt_primitiveid_en); 1576bf215546Sopenharmony_ci 1577bf215546Sopenharmony_ci if (sctx->gfx_level <= GFX8) { 1578bf215546Sopenharmony_ci radeon_opt_set_context_reg(sctx, R_028AB4_VGT_REUSE_OFF, SI_TRACKED_VGT_REUSE_OFF, 1579bf215546Sopenharmony_ci shader->ctx_reg.vs.vgt_reuse_off); 1580bf215546Sopenharmony_ci } 1581bf215546Sopenharmony_ci 1582bf215546Sopenharmony_ci radeon_opt_set_context_reg(sctx, R_0286C4_SPI_VS_OUT_CONFIG, SI_TRACKED_SPI_VS_OUT_CONFIG, 1583bf215546Sopenharmony_ci shader->ctx_reg.vs.spi_vs_out_config); 1584bf215546Sopenharmony_ci 1585bf215546Sopenharmony_ci radeon_opt_set_context_reg(sctx, R_02870C_SPI_SHADER_POS_FORMAT, 1586bf215546Sopenharmony_ci SI_TRACKED_SPI_SHADER_POS_FORMAT, 1587bf215546Sopenharmony_ci shader->ctx_reg.vs.spi_shader_pos_format); 1588bf215546Sopenharmony_ci 1589bf215546Sopenharmony_ci radeon_opt_set_context_reg(sctx, R_028818_PA_CL_VTE_CNTL, SI_TRACKED_PA_CL_VTE_CNTL, 1590bf215546Sopenharmony_ci shader->ctx_reg.vs.pa_cl_vte_cntl); 1591bf215546Sopenharmony_ci 1592bf215546Sopenharmony_ci if (shader->selector->stage == MESA_SHADER_TESS_EVAL) 1593bf215546Sopenharmony_ci radeon_opt_set_context_reg(sctx, R_028B6C_VGT_TF_PARAM, SI_TRACKED_VGT_TF_PARAM, 1594bf215546Sopenharmony_ci shader->vgt_tf_param); 1595bf215546Sopenharmony_ci 1596bf215546Sopenharmony_ci if (shader->vgt_vertex_reuse_block_cntl) 1597bf215546Sopenharmony_ci radeon_opt_set_context_reg(sctx, R_028C58_VGT_VERTEX_REUSE_BLOCK_CNTL, 1598bf215546Sopenharmony_ci SI_TRACKED_VGT_VERTEX_REUSE_BLOCK_CNTL, 1599bf215546Sopenharmony_ci shader->vgt_vertex_reuse_block_cntl); 1600bf215546Sopenharmony_ci 1601bf215546Sopenharmony_ci /* Required programming for tessellation. (legacy pipeline only) */ 1602bf215546Sopenharmony_ci if (sctx->gfx_level >= GFX10 && shader->selector->stage == MESA_SHADER_TESS_EVAL) { 1603bf215546Sopenharmony_ci radeon_opt_set_context_reg(sctx, R_028A44_VGT_GS_ONCHIP_CNTL, 1604bf215546Sopenharmony_ci SI_TRACKED_VGT_GS_ONCHIP_CNTL, 1605bf215546Sopenharmony_ci S_028A44_ES_VERTS_PER_SUBGRP(250) | 1606bf215546Sopenharmony_ci S_028A44_GS_PRIMS_PER_SUBGRP(126) | 1607bf215546Sopenharmony_ci S_028A44_GS_INST_PRIMS_IN_SUBGRP(126)); 1608bf215546Sopenharmony_ci } 1609bf215546Sopenharmony_ci 1610bf215546Sopenharmony_ci radeon_end_update_context_roll(sctx); 1611bf215546Sopenharmony_ci 1612bf215546Sopenharmony_ci /* GE_PC_ALLOC is not a context register, so it doesn't cause a context roll. */ 1613bf215546Sopenharmony_ci if (sctx->gfx_level >= GFX10) { 1614bf215546Sopenharmony_ci radeon_begin_again(&sctx->gfx_cs); 1615bf215546Sopenharmony_ci radeon_opt_set_uconfig_reg(sctx, R_030980_GE_PC_ALLOC, SI_TRACKED_GE_PC_ALLOC, 1616bf215546Sopenharmony_ci shader->ctx_reg.vs.ge_pc_alloc); 1617bf215546Sopenharmony_ci radeon_end(); 1618bf215546Sopenharmony_ci } 1619bf215546Sopenharmony_ci} 1620bf215546Sopenharmony_ci 1621bf215546Sopenharmony_ci/** 1622bf215546Sopenharmony_ci * Compute the state for \p shader, which will run as a vertex shader on the 1623bf215546Sopenharmony_ci * hardware. 1624bf215546Sopenharmony_ci * 1625bf215546Sopenharmony_ci * If \p gs is non-NULL, it points to the geometry shader for which this shader 1626bf215546Sopenharmony_ci * is the copy shader. 1627bf215546Sopenharmony_ci */ 1628bf215546Sopenharmony_cistatic void si_shader_vs(struct si_screen *sscreen, struct si_shader *shader, 1629bf215546Sopenharmony_ci struct si_shader_selector *gs) 1630bf215546Sopenharmony_ci{ 1631bf215546Sopenharmony_ci const struct si_shader_info *info = &shader->selector->info; 1632bf215546Sopenharmony_ci struct si_pm4_state *pm4; 1633bf215546Sopenharmony_ci unsigned num_user_sgprs, vgpr_comp_cnt; 1634bf215546Sopenharmony_ci uint64_t va; 1635bf215546Sopenharmony_ci unsigned nparams, oc_lds_en; 1636bf215546Sopenharmony_ci bool window_space = shader->selector->stage == MESA_SHADER_VERTEX ? 1637bf215546Sopenharmony_ci info->base.vs.window_space_position : 0; 1638bf215546Sopenharmony_ci bool enable_prim_id = shader->key.ge.mono.u.vs_export_prim_id || info->uses_primid; 1639bf215546Sopenharmony_ci 1640bf215546Sopenharmony_ci assert(sscreen->info.gfx_level < GFX11); 1641bf215546Sopenharmony_ci 1642bf215546Sopenharmony_ci pm4 = si_get_shader_pm4_state(shader); 1643bf215546Sopenharmony_ci if (!pm4) 1644bf215546Sopenharmony_ci return; 1645bf215546Sopenharmony_ci 1646bf215546Sopenharmony_ci pm4->atom.emit = si_emit_shader_vs; 1647bf215546Sopenharmony_ci 1648bf215546Sopenharmony_ci /* We always write VGT_GS_MODE in the VS state, because every switch 1649bf215546Sopenharmony_ci * between different shader pipelines involving a different GS or no 1650bf215546Sopenharmony_ci * GS at all involves a switch of the VS (different GS use different 1651bf215546Sopenharmony_ci * copy shaders). On the other hand, when the API switches from a GS to 1652bf215546Sopenharmony_ci * no GS and then back to the same GS used originally, the GS state is 1653bf215546Sopenharmony_ci * not sent again. 1654bf215546Sopenharmony_ci */ 1655bf215546Sopenharmony_ci if (!gs) { 1656bf215546Sopenharmony_ci unsigned mode = V_028A40_GS_OFF; 1657bf215546Sopenharmony_ci 1658bf215546Sopenharmony_ci /* PrimID needs GS scenario A. */ 1659bf215546Sopenharmony_ci if (enable_prim_id) 1660bf215546Sopenharmony_ci mode = V_028A40_GS_SCENARIO_A; 1661bf215546Sopenharmony_ci 1662bf215546Sopenharmony_ci shader->ctx_reg.vs.vgt_gs_mode = S_028A40_MODE(mode); 1663bf215546Sopenharmony_ci shader->ctx_reg.vs.vgt_primitiveid_en = enable_prim_id; 1664bf215546Sopenharmony_ci } else { 1665bf215546Sopenharmony_ci shader->ctx_reg.vs.vgt_gs_mode = 1666bf215546Sopenharmony_ci ac_vgt_gs_mode(gs->info.base.gs.vertices_out, sscreen->info.gfx_level); 1667bf215546Sopenharmony_ci shader->ctx_reg.vs.vgt_primitiveid_en = 0; 1668bf215546Sopenharmony_ci } 1669bf215546Sopenharmony_ci 1670bf215546Sopenharmony_ci if (sscreen->info.gfx_level <= GFX8) { 1671bf215546Sopenharmony_ci /* Reuse needs to be set off if we write oViewport. */ 1672bf215546Sopenharmony_ci shader->ctx_reg.vs.vgt_reuse_off = S_028AB4_REUSE_OFF(info->writes_viewport_index); 1673bf215546Sopenharmony_ci } 1674bf215546Sopenharmony_ci 1675bf215546Sopenharmony_ci va = shader->bo->gpu_address; 1676bf215546Sopenharmony_ci 1677bf215546Sopenharmony_ci if (gs) { 1678bf215546Sopenharmony_ci vgpr_comp_cnt = 0; /* only VertexID is needed for GS-COPY. */ 1679bf215546Sopenharmony_ci num_user_sgprs = SI_GSCOPY_NUM_USER_SGPR; 1680bf215546Sopenharmony_ci } else if (shader->selector->stage == MESA_SHADER_VERTEX) { 1681bf215546Sopenharmony_ci vgpr_comp_cnt = si_get_vs_vgpr_comp_cnt(sscreen, shader, enable_prim_id); 1682bf215546Sopenharmony_ci 1683bf215546Sopenharmony_ci if (info->base.vs.blit_sgprs_amd) { 1684bf215546Sopenharmony_ci num_user_sgprs = SI_SGPR_VS_BLIT_DATA + info->base.vs.blit_sgprs_amd; 1685bf215546Sopenharmony_ci } else { 1686bf215546Sopenharmony_ci num_user_sgprs = si_get_num_vs_user_sgprs(shader, SI_VS_NUM_USER_SGPR); 1687bf215546Sopenharmony_ci } 1688bf215546Sopenharmony_ci } else if (shader->selector->stage == MESA_SHADER_TESS_EVAL) { 1689bf215546Sopenharmony_ci vgpr_comp_cnt = enable_prim_id ? 3 : 2; 1690bf215546Sopenharmony_ci num_user_sgprs = SI_TES_NUM_USER_SGPR; 1691bf215546Sopenharmony_ci } else 1692bf215546Sopenharmony_ci unreachable("invalid shader selector type"); 1693bf215546Sopenharmony_ci 1694bf215546Sopenharmony_ci /* VS is required to export at least one param. */ 1695bf215546Sopenharmony_ci nparams = MAX2(shader->info.nr_param_exports, 1); 1696bf215546Sopenharmony_ci shader->ctx_reg.vs.spi_vs_out_config = S_0286C4_VS_EXPORT_COUNT(nparams - 1); 1697bf215546Sopenharmony_ci 1698bf215546Sopenharmony_ci if (sscreen->info.gfx_level >= GFX10) { 1699bf215546Sopenharmony_ci shader->ctx_reg.vs.spi_vs_out_config |= 1700bf215546Sopenharmony_ci S_0286C4_NO_PC_EXPORT(shader->info.nr_param_exports == 0); 1701bf215546Sopenharmony_ci } 1702bf215546Sopenharmony_ci 1703bf215546Sopenharmony_ci shader->ctx_reg.vs.spi_shader_pos_format = 1704bf215546Sopenharmony_ci S_02870C_POS0_EXPORT_FORMAT(V_02870C_SPI_SHADER_4COMP) | 1705bf215546Sopenharmony_ci S_02870C_POS1_EXPORT_FORMAT(shader->info.nr_pos_exports > 1 ? V_02870C_SPI_SHADER_4COMP 1706bf215546Sopenharmony_ci : V_02870C_SPI_SHADER_NONE) | 1707bf215546Sopenharmony_ci S_02870C_POS2_EXPORT_FORMAT(shader->info.nr_pos_exports > 2 ? V_02870C_SPI_SHADER_4COMP 1708bf215546Sopenharmony_ci : V_02870C_SPI_SHADER_NONE) | 1709bf215546Sopenharmony_ci S_02870C_POS3_EXPORT_FORMAT(shader->info.nr_pos_exports > 3 ? V_02870C_SPI_SHADER_4COMP 1710bf215546Sopenharmony_ci : V_02870C_SPI_SHADER_NONE); 1711bf215546Sopenharmony_ci unsigned late_alloc_wave64, cu_mask; 1712bf215546Sopenharmony_ci ac_compute_late_alloc(&sscreen->info, false, false, 1713bf215546Sopenharmony_ci shader->config.scratch_bytes_per_wave > 0, 1714bf215546Sopenharmony_ci &late_alloc_wave64, &cu_mask); 1715bf215546Sopenharmony_ci 1716bf215546Sopenharmony_ci shader->ctx_reg.vs.ge_pc_alloc = S_030980_OVERSUB_EN(late_alloc_wave64 > 0) | 1717bf215546Sopenharmony_ci S_030980_NUM_PC_LINES(sscreen->info.pc_lines / 4 - 1); 1718bf215546Sopenharmony_ci shader->pa_cl_vs_out_cntl = si_get_vs_out_cntl(shader->selector, shader, false); 1719bf215546Sopenharmony_ci 1720bf215546Sopenharmony_ci oc_lds_en = shader->selector->stage == MESA_SHADER_TESS_EVAL ? 1 : 0; 1721bf215546Sopenharmony_ci 1722bf215546Sopenharmony_ci if (sscreen->info.gfx_level >= GFX7) { 1723bf215546Sopenharmony_ci ac_set_reg_cu_en(pm4, R_00B118_SPI_SHADER_PGM_RSRC3_VS, 1724bf215546Sopenharmony_ci S_00B118_CU_EN(cu_mask) | S_00B118_WAVE_LIMIT(0x3F), 1725bf215546Sopenharmony_ci C_00B118_CU_EN, 0, &sscreen->info, 1726bf215546Sopenharmony_ci (void (*)(void*, unsigned, uint32_t)) 1727bf215546Sopenharmony_ci (sscreen->info.gfx_level >= GFX10 ? si_pm4_set_reg_idx3 : si_pm4_set_reg)); 1728bf215546Sopenharmony_ci si_pm4_set_reg(pm4, R_00B11C_SPI_SHADER_LATE_ALLOC_VS, S_00B11C_LIMIT(late_alloc_wave64)); 1729bf215546Sopenharmony_ci } 1730bf215546Sopenharmony_ci 1731bf215546Sopenharmony_ci si_pm4_set_reg(pm4, R_00B120_SPI_SHADER_PGM_LO_VS, va >> 8); 1732bf215546Sopenharmony_ci si_pm4_set_reg(pm4, R_00B124_SPI_SHADER_PGM_HI_VS, 1733bf215546Sopenharmony_ci S_00B124_MEM_BASE(sscreen->info.address32_hi >> 8)); 1734bf215546Sopenharmony_ci 1735bf215546Sopenharmony_ci uint32_t rsrc1 = 1736bf215546Sopenharmony_ci S_00B128_VGPRS((shader->config.num_vgprs - 1) / (shader->wave_size == 32 ? 8 : 4)) | 1737bf215546Sopenharmony_ci S_00B128_VGPR_COMP_CNT(vgpr_comp_cnt) | S_00B128_DX10_CLAMP(1) | 1738bf215546Sopenharmony_ci S_00B128_MEM_ORDERED(si_shader_mem_ordered(shader)) | 1739bf215546Sopenharmony_ci S_00B128_FLOAT_MODE(shader->config.float_mode); 1740bf215546Sopenharmony_ci uint32_t rsrc2 = S_00B12C_USER_SGPR(num_user_sgprs) | S_00B12C_OC_LDS_EN(oc_lds_en) | 1741bf215546Sopenharmony_ci S_00B12C_SCRATCH_EN(shader->config.scratch_bytes_per_wave > 0); 1742bf215546Sopenharmony_ci 1743bf215546Sopenharmony_ci if (sscreen->info.gfx_level >= GFX10) 1744bf215546Sopenharmony_ci rsrc2 |= S_00B12C_USER_SGPR_MSB_GFX10(num_user_sgprs >> 5); 1745bf215546Sopenharmony_ci else if (sscreen->info.gfx_level == GFX9) 1746bf215546Sopenharmony_ci rsrc2 |= S_00B12C_USER_SGPR_MSB_GFX9(num_user_sgprs >> 5); 1747bf215546Sopenharmony_ci 1748bf215546Sopenharmony_ci if (sscreen->info.gfx_level <= GFX9) 1749bf215546Sopenharmony_ci rsrc1 |= S_00B128_SGPRS((shader->config.num_sgprs - 1) / 8); 1750bf215546Sopenharmony_ci 1751bf215546Sopenharmony_ci if (!sscreen->use_ngg_streamout && si_shader_uses_streamout(shader)) { 1752bf215546Sopenharmony_ci rsrc2 |= S_00B12C_SO_BASE0_EN(!!shader->selector->info.base.xfb_stride[0]) | 1753bf215546Sopenharmony_ci S_00B12C_SO_BASE1_EN(!!shader->selector->info.base.xfb_stride[1]) | 1754bf215546Sopenharmony_ci S_00B12C_SO_BASE2_EN(!!shader->selector->info.base.xfb_stride[2]) | 1755bf215546Sopenharmony_ci S_00B12C_SO_BASE3_EN(!!shader->selector->info.base.xfb_stride[3]) | 1756bf215546Sopenharmony_ci S_00B12C_SO_EN(1); 1757bf215546Sopenharmony_ci } 1758bf215546Sopenharmony_ci 1759bf215546Sopenharmony_ci si_pm4_set_reg(pm4, R_00B128_SPI_SHADER_PGM_RSRC1_VS, rsrc1); 1760bf215546Sopenharmony_ci si_pm4_set_reg(pm4, R_00B12C_SPI_SHADER_PGM_RSRC2_VS, rsrc2); 1761bf215546Sopenharmony_ci 1762bf215546Sopenharmony_ci if (window_space) 1763bf215546Sopenharmony_ci shader->ctx_reg.vs.pa_cl_vte_cntl = S_028818_VTX_XY_FMT(1) | S_028818_VTX_Z_FMT(1); 1764bf215546Sopenharmony_ci else 1765bf215546Sopenharmony_ci shader->ctx_reg.vs.pa_cl_vte_cntl = 1766bf215546Sopenharmony_ci S_028818_VTX_W0_FMT(1) | S_028818_VPORT_X_SCALE_ENA(1) | S_028818_VPORT_X_OFFSET_ENA(1) | 1767bf215546Sopenharmony_ci S_028818_VPORT_Y_SCALE_ENA(1) | S_028818_VPORT_Y_OFFSET_ENA(1) | 1768bf215546Sopenharmony_ci S_028818_VPORT_Z_SCALE_ENA(1) | S_028818_VPORT_Z_OFFSET_ENA(1); 1769bf215546Sopenharmony_ci 1770bf215546Sopenharmony_ci if (shader->selector->stage == MESA_SHADER_TESS_EVAL) 1771bf215546Sopenharmony_ci si_set_tesseval_regs(sscreen, shader->selector, shader); 1772bf215546Sopenharmony_ci 1773bf215546Sopenharmony_ci polaris_set_vgt_vertex_reuse(sscreen, shader->selector, shader); 1774bf215546Sopenharmony_ci} 1775bf215546Sopenharmony_ci 1776bf215546Sopenharmony_cistatic unsigned si_get_spi_shader_col_format(struct si_shader *shader) 1777bf215546Sopenharmony_ci{ 1778bf215546Sopenharmony_ci unsigned spi_shader_col_format = shader->key.ps.part.epilog.spi_shader_col_format; 1779bf215546Sopenharmony_ci unsigned value = 0, num_mrts = 0; 1780bf215546Sopenharmony_ci unsigned i, num_targets = (util_last_bit(spi_shader_col_format) + 3) / 4; 1781bf215546Sopenharmony_ci 1782bf215546Sopenharmony_ci /* Remove holes in spi_shader_col_format. */ 1783bf215546Sopenharmony_ci for (i = 0; i < num_targets; i++) { 1784bf215546Sopenharmony_ci unsigned spi_format = (spi_shader_col_format >> (i * 4)) & 0xf; 1785bf215546Sopenharmony_ci 1786bf215546Sopenharmony_ci if (spi_format) { 1787bf215546Sopenharmony_ci value |= spi_format << (num_mrts * 4); 1788bf215546Sopenharmony_ci num_mrts++; 1789bf215546Sopenharmony_ci } 1790bf215546Sopenharmony_ci } 1791bf215546Sopenharmony_ci 1792bf215546Sopenharmony_ci return value; 1793bf215546Sopenharmony_ci} 1794bf215546Sopenharmony_ci 1795bf215546Sopenharmony_cistatic void si_emit_shader_ps(struct si_context *sctx) 1796bf215546Sopenharmony_ci{ 1797bf215546Sopenharmony_ci struct si_shader *shader = sctx->queued.named.ps; 1798bf215546Sopenharmony_ci if (!shader) 1799bf215546Sopenharmony_ci return; 1800bf215546Sopenharmony_ci 1801bf215546Sopenharmony_ci radeon_begin(&sctx->gfx_cs); 1802bf215546Sopenharmony_ci /* R_0286CC_SPI_PS_INPUT_ENA, R_0286D0_SPI_PS_INPUT_ADDR*/ 1803bf215546Sopenharmony_ci radeon_opt_set_context_reg2(sctx, R_0286CC_SPI_PS_INPUT_ENA, SI_TRACKED_SPI_PS_INPUT_ENA, 1804bf215546Sopenharmony_ci shader->ctx_reg.ps.spi_ps_input_ena, 1805bf215546Sopenharmony_ci shader->ctx_reg.ps.spi_ps_input_addr); 1806bf215546Sopenharmony_ci 1807bf215546Sopenharmony_ci radeon_opt_set_context_reg(sctx, R_0286E0_SPI_BARYC_CNTL, SI_TRACKED_SPI_BARYC_CNTL, 1808bf215546Sopenharmony_ci shader->ctx_reg.ps.spi_baryc_cntl); 1809bf215546Sopenharmony_ci radeon_opt_set_context_reg(sctx, R_0286D8_SPI_PS_IN_CONTROL, SI_TRACKED_SPI_PS_IN_CONTROL, 1810bf215546Sopenharmony_ci shader->ctx_reg.ps.spi_ps_in_control); 1811bf215546Sopenharmony_ci 1812bf215546Sopenharmony_ci /* R_028710_SPI_SHADER_Z_FORMAT, R_028714_SPI_SHADER_COL_FORMAT */ 1813bf215546Sopenharmony_ci radeon_opt_set_context_reg2(sctx, R_028710_SPI_SHADER_Z_FORMAT, SI_TRACKED_SPI_SHADER_Z_FORMAT, 1814bf215546Sopenharmony_ci shader->ctx_reg.ps.spi_shader_z_format, 1815bf215546Sopenharmony_ci shader->ctx_reg.ps.spi_shader_col_format); 1816bf215546Sopenharmony_ci 1817bf215546Sopenharmony_ci radeon_opt_set_context_reg(sctx, R_02823C_CB_SHADER_MASK, SI_TRACKED_CB_SHADER_MASK, 1818bf215546Sopenharmony_ci shader->ctx_reg.ps.cb_shader_mask); 1819bf215546Sopenharmony_ci radeon_end_update_context_roll(sctx); 1820bf215546Sopenharmony_ci} 1821bf215546Sopenharmony_ci 1822bf215546Sopenharmony_cistatic void si_shader_ps(struct si_screen *sscreen, struct si_shader *shader) 1823bf215546Sopenharmony_ci{ 1824bf215546Sopenharmony_ci struct si_shader_info *info = &shader->selector->info; 1825bf215546Sopenharmony_ci struct si_pm4_state *pm4; 1826bf215546Sopenharmony_ci unsigned spi_ps_in_control, spi_shader_col_format, cb_shader_mask; 1827bf215546Sopenharmony_ci unsigned spi_baryc_cntl = S_0286E0_FRONT_FACE_ALL_BITS(1); 1828bf215546Sopenharmony_ci uint64_t va; 1829bf215546Sopenharmony_ci unsigned input_ena = shader->config.spi_ps_input_ena; 1830bf215546Sopenharmony_ci 1831bf215546Sopenharmony_ci /* we need to enable at least one of them, otherwise we hang the GPU */ 1832bf215546Sopenharmony_ci assert(G_0286CC_PERSP_SAMPLE_ENA(input_ena) || G_0286CC_PERSP_CENTER_ENA(input_ena) || 1833bf215546Sopenharmony_ci G_0286CC_PERSP_CENTROID_ENA(input_ena) || G_0286CC_PERSP_PULL_MODEL_ENA(input_ena) || 1834bf215546Sopenharmony_ci G_0286CC_LINEAR_SAMPLE_ENA(input_ena) || G_0286CC_LINEAR_CENTER_ENA(input_ena) || 1835bf215546Sopenharmony_ci G_0286CC_LINEAR_CENTROID_ENA(input_ena) || G_0286CC_LINE_STIPPLE_TEX_ENA(input_ena)); 1836bf215546Sopenharmony_ci /* POS_W_FLOAT_ENA requires one of the perspective weights. */ 1837bf215546Sopenharmony_ci assert(!G_0286CC_POS_W_FLOAT_ENA(input_ena) || G_0286CC_PERSP_SAMPLE_ENA(input_ena) || 1838bf215546Sopenharmony_ci G_0286CC_PERSP_CENTER_ENA(input_ena) || G_0286CC_PERSP_CENTROID_ENA(input_ena) || 1839bf215546Sopenharmony_ci G_0286CC_PERSP_PULL_MODEL_ENA(input_ena)); 1840bf215546Sopenharmony_ci 1841bf215546Sopenharmony_ci /* Validate interpolation optimization flags (read as implications). */ 1842bf215546Sopenharmony_ci assert(!shader->key.ps.part.prolog.bc_optimize_for_persp || 1843bf215546Sopenharmony_ci (G_0286CC_PERSP_CENTER_ENA(input_ena) && G_0286CC_PERSP_CENTROID_ENA(input_ena))); 1844bf215546Sopenharmony_ci assert(!shader->key.ps.part.prolog.bc_optimize_for_linear || 1845bf215546Sopenharmony_ci (G_0286CC_LINEAR_CENTER_ENA(input_ena) && G_0286CC_LINEAR_CENTROID_ENA(input_ena))); 1846bf215546Sopenharmony_ci assert(!shader->key.ps.part.prolog.force_persp_center_interp || 1847bf215546Sopenharmony_ci (!G_0286CC_PERSP_SAMPLE_ENA(input_ena) && !G_0286CC_PERSP_CENTROID_ENA(input_ena))); 1848bf215546Sopenharmony_ci assert(!shader->key.ps.part.prolog.force_linear_center_interp || 1849bf215546Sopenharmony_ci (!G_0286CC_LINEAR_SAMPLE_ENA(input_ena) && !G_0286CC_LINEAR_CENTROID_ENA(input_ena))); 1850bf215546Sopenharmony_ci assert(!shader->key.ps.part.prolog.force_persp_sample_interp || 1851bf215546Sopenharmony_ci (!G_0286CC_PERSP_CENTER_ENA(input_ena) && !G_0286CC_PERSP_CENTROID_ENA(input_ena))); 1852bf215546Sopenharmony_ci assert(!shader->key.ps.part.prolog.force_linear_sample_interp || 1853bf215546Sopenharmony_ci (!G_0286CC_LINEAR_CENTER_ENA(input_ena) && !G_0286CC_LINEAR_CENTROID_ENA(input_ena))); 1854bf215546Sopenharmony_ci 1855bf215546Sopenharmony_ci /* Validate cases when the optimizations are off (read as implications). */ 1856bf215546Sopenharmony_ci assert(shader->key.ps.part.prolog.bc_optimize_for_persp || 1857bf215546Sopenharmony_ci !G_0286CC_PERSP_CENTER_ENA(input_ena) || !G_0286CC_PERSP_CENTROID_ENA(input_ena)); 1858bf215546Sopenharmony_ci assert(shader->key.ps.part.prolog.bc_optimize_for_linear || 1859bf215546Sopenharmony_ci !G_0286CC_LINEAR_CENTER_ENA(input_ena) || !G_0286CC_LINEAR_CENTROID_ENA(input_ena)); 1860bf215546Sopenharmony_ci 1861bf215546Sopenharmony_ci /* DB_SHADER_CONTROL */ 1862bf215546Sopenharmony_ci unsigned db_shader_control = 1863bf215546Sopenharmony_ci S_02880C_Z_EXPORT_ENABLE(info->writes_z) | 1864bf215546Sopenharmony_ci S_02880C_STENCIL_TEST_VAL_EXPORT_ENABLE(info->writes_stencil) | 1865bf215546Sopenharmony_ci S_02880C_MASK_EXPORT_ENABLE(info->writes_samplemask) | 1866bf215546Sopenharmony_ci S_02880C_KILL_ENABLE(si_shader_uses_discard(shader)); 1867bf215546Sopenharmony_ci 1868bf215546Sopenharmony_ci switch (info->base.fs.depth_layout) { 1869bf215546Sopenharmony_ci case FRAG_DEPTH_LAYOUT_GREATER: 1870bf215546Sopenharmony_ci db_shader_control |= S_02880C_CONSERVATIVE_Z_EXPORT(V_02880C_EXPORT_GREATER_THAN_Z); 1871bf215546Sopenharmony_ci break; 1872bf215546Sopenharmony_ci case FRAG_DEPTH_LAYOUT_LESS: 1873bf215546Sopenharmony_ci db_shader_control |= S_02880C_CONSERVATIVE_Z_EXPORT(V_02880C_EXPORT_LESS_THAN_Z); 1874bf215546Sopenharmony_ci break; 1875bf215546Sopenharmony_ci default:; 1876bf215546Sopenharmony_ci } 1877bf215546Sopenharmony_ci 1878bf215546Sopenharmony_ci /* Z_ORDER, EXEC_ON_HIER_FAIL and EXEC_ON_NOOP should be set as following: 1879bf215546Sopenharmony_ci * 1880bf215546Sopenharmony_ci * | early Z/S | writes_mem | allow_ReZ? | Z_ORDER | EXEC_ON_HIER_FAIL | EXEC_ON_NOOP 1881bf215546Sopenharmony_ci * --|-----------|------------|------------|--------------------|-------------------|------------- 1882bf215546Sopenharmony_ci * 1a| false | false | true | EarlyZ_Then_ReZ | 0 | 0 1883bf215546Sopenharmony_ci * 1b| false | false | false | EarlyZ_Then_LateZ | 0 | 0 1884bf215546Sopenharmony_ci * 2 | false | true | n/a | LateZ | 1 | 0 1885bf215546Sopenharmony_ci * 3 | true | false | n/a | EarlyZ_Then_LateZ | 0 | 0 1886bf215546Sopenharmony_ci * 4 | true | true | n/a | EarlyZ_Then_LateZ | 0 | 1 1887bf215546Sopenharmony_ci * 1888bf215546Sopenharmony_ci * In cases 3 and 4, HW will force Z_ORDER to EarlyZ regardless of what's set in the register. 1889bf215546Sopenharmony_ci * In case 2, NOOP_CULL is a don't care field. In case 2, 3 and 4, ReZ doesn't make sense. 1890bf215546Sopenharmony_ci * 1891bf215546Sopenharmony_ci * Don't use ReZ without profiling !!! 1892bf215546Sopenharmony_ci * 1893bf215546Sopenharmony_ci * ReZ decreases performance by 15% in DiRT: Showdown on Ultra settings, which has pretty complex 1894bf215546Sopenharmony_ci * shaders. 1895bf215546Sopenharmony_ci */ 1896bf215546Sopenharmony_ci if (info->base.fs.early_fragment_tests) { 1897bf215546Sopenharmony_ci /* Cases 3, 4. */ 1898bf215546Sopenharmony_ci db_shader_control |= S_02880C_DEPTH_BEFORE_SHADER(1) | 1899bf215546Sopenharmony_ci S_02880C_Z_ORDER(V_02880C_EARLY_Z_THEN_LATE_Z) | 1900bf215546Sopenharmony_ci S_02880C_EXEC_ON_NOOP(info->base.writes_memory); 1901bf215546Sopenharmony_ci } else if (info->base.writes_memory) { 1902bf215546Sopenharmony_ci /* Case 2. */ 1903bf215546Sopenharmony_ci db_shader_control |= S_02880C_Z_ORDER(V_02880C_LATE_Z) | S_02880C_EXEC_ON_HIER_FAIL(1); 1904bf215546Sopenharmony_ci } else { 1905bf215546Sopenharmony_ci /* Case 1. */ 1906bf215546Sopenharmony_ci db_shader_control |= S_02880C_Z_ORDER(V_02880C_EARLY_Z_THEN_LATE_Z); 1907bf215546Sopenharmony_ci } 1908bf215546Sopenharmony_ci 1909bf215546Sopenharmony_ci if (info->base.fs.post_depth_coverage) 1910bf215546Sopenharmony_ci db_shader_control |= S_02880C_PRE_SHADER_DEPTH_COVERAGE_ENABLE(1); 1911bf215546Sopenharmony_ci 1912bf215546Sopenharmony_ci shader->ctx_reg.ps.db_shader_control = db_shader_control; 1913bf215546Sopenharmony_ci 1914bf215546Sopenharmony_ci pm4 = si_get_shader_pm4_state(shader); 1915bf215546Sopenharmony_ci if (!pm4) 1916bf215546Sopenharmony_ci return; 1917bf215546Sopenharmony_ci 1918bf215546Sopenharmony_ci /* If multiple state sets are allowed to be in a bin, break the batch on a new PS. */ 1919bf215546Sopenharmony_ci if (sscreen->dpbb_allowed && 1920bf215546Sopenharmony_ci (sscreen->pbb_context_states_per_bin > 1 || 1921bf215546Sopenharmony_ci sscreen->pbb_persistent_states_per_bin > 1)) { 1922bf215546Sopenharmony_ci si_pm4_cmd_add(pm4, PKT3(PKT3_EVENT_WRITE, 0, 0)); 1923bf215546Sopenharmony_ci si_pm4_cmd_add(pm4, EVENT_TYPE(V_028A90_BREAK_BATCH) | EVENT_INDEX(0)); 1924bf215546Sopenharmony_ci } 1925bf215546Sopenharmony_ci 1926bf215546Sopenharmony_ci pm4->atom.emit = si_emit_shader_ps; 1927bf215546Sopenharmony_ci 1928bf215546Sopenharmony_ci /* SPI_BARYC_CNTL.POS_FLOAT_LOCATION 1929bf215546Sopenharmony_ci * Possible vaules: 1930bf215546Sopenharmony_ci * 0 -> Position = pixel center 1931bf215546Sopenharmony_ci * 1 -> Position = pixel centroid 1932bf215546Sopenharmony_ci * 2 -> Position = at sample position 1933bf215546Sopenharmony_ci * 1934bf215546Sopenharmony_ci * From GLSL 4.5 specification, section 7.1: 1935bf215546Sopenharmony_ci * "The variable gl_FragCoord is available as an input variable from 1936bf215546Sopenharmony_ci * within fragment shaders and it holds the window relative coordinates 1937bf215546Sopenharmony_ci * (x, y, z, 1/w) values for the fragment. If multi-sampling, this 1938bf215546Sopenharmony_ci * value can be for any location within the pixel, or one of the 1939bf215546Sopenharmony_ci * fragment samples. The use of centroid does not further restrict 1940bf215546Sopenharmony_ci * this value to be inside the current primitive." 1941bf215546Sopenharmony_ci * 1942bf215546Sopenharmony_ci * Meaning that centroid has no effect and we can return anything within 1943bf215546Sopenharmony_ci * the pixel. Thus, return the value at sample position, because that's 1944bf215546Sopenharmony_ci * the most accurate one shaders can get. 1945bf215546Sopenharmony_ci */ 1946bf215546Sopenharmony_ci spi_baryc_cntl |= S_0286E0_POS_FLOAT_LOCATION(2); 1947bf215546Sopenharmony_ci 1948bf215546Sopenharmony_ci if (info->base.fs.pixel_center_integer) 1949bf215546Sopenharmony_ci spi_baryc_cntl |= S_0286E0_POS_FLOAT_ULC(1); 1950bf215546Sopenharmony_ci 1951bf215546Sopenharmony_ci spi_shader_col_format = si_get_spi_shader_col_format(shader); 1952bf215546Sopenharmony_ci cb_shader_mask = ac_get_cb_shader_mask(shader->key.ps.part.epilog.spi_shader_col_format); 1953bf215546Sopenharmony_ci 1954bf215546Sopenharmony_ci /* Ensure that some export memory is always allocated, for two reasons: 1955bf215546Sopenharmony_ci * 1956bf215546Sopenharmony_ci * 1) Correctness: The hardware ignores the EXEC mask if no export 1957bf215546Sopenharmony_ci * memory is allocated, so KILL and alpha test do not work correctly 1958bf215546Sopenharmony_ci * without this. 1959bf215546Sopenharmony_ci * 2) Performance: Every shader needs at least a NULL export, even when 1960bf215546Sopenharmony_ci * it writes no color/depth output. The NULL export instruction 1961bf215546Sopenharmony_ci * stalls without this setting. 1962bf215546Sopenharmony_ci * 1963bf215546Sopenharmony_ci * Don't add this to CB_SHADER_MASK. 1964bf215546Sopenharmony_ci * 1965bf215546Sopenharmony_ci * GFX10 supports pixel shaders without exports by setting both 1966bf215546Sopenharmony_ci * the color and Z formats to SPI_SHADER_ZERO. The hw will skip export 1967bf215546Sopenharmony_ci * instructions if any are present. 1968bf215546Sopenharmony_ci */ 1969bf215546Sopenharmony_ci bool has_mrtz = info->writes_z || info->writes_stencil || info->writes_samplemask; 1970bf215546Sopenharmony_ci 1971bf215546Sopenharmony_ci if (!spi_shader_col_format && !has_mrtz) { 1972bf215546Sopenharmony_ci if (sscreen->info.gfx_level >= GFX10) { 1973bf215546Sopenharmony_ci if (G_02880C_KILL_ENABLE(db_shader_control)) 1974bf215546Sopenharmony_ci spi_shader_col_format = V_028714_SPI_SHADER_32_R; 1975bf215546Sopenharmony_ci } else { 1976bf215546Sopenharmony_ci spi_shader_col_format = V_028714_SPI_SHADER_32_R; 1977bf215546Sopenharmony_ci } 1978bf215546Sopenharmony_ci } 1979bf215546Sopenharmony_ci 1980bf215546Sopenharmony_ci shader->ctx_reg.ps.spi_ps_input_ena = input_ena; 1981bf215546Sopenharmony_ci shader->ctx_reg.ps.spi_ps_input_addr = shader->config.spi_ps_input_addr; 1982bf215546Sopenharmony_ci 1983bf215546Sopenharmony_ci unsigned num_interp = si_get_ps_num_interp(shader); 1984bf215546Sopenharmony_ci 1985bf215546Sopenharmony_ci /* Set interpolation controls. */ 1986bf215546Sopenharmony_ci spi_ps_in_control = S_0286D8_NUM_INTERP(num_interp) | 1987bf215546Sopenharmony_ci S_0286D8_PS_W32_EN(shader->wave_size == 32); 1988bf215546Sopenharmony_ci 1989bf215546Sopenharmony_ci /* Enable PARAM_GEN for point smoothing. 1990bf215546Sopenharmony_ci * Gfx11 workaround when there are no PS inputs but LDS is used. 1991bf215546Sopenharmony_ci */ 1992bf215546Sopenharmony_ci if ((sscreen->info.gfx_level == GFX11 && !num_interp && shader->config.lds_size) || 1993bf215546Sopenharmony_ci shader->key.ps.mono.point_smoothing) 1994bf215546Sopenharmony_ci spi_ps_in_control |= S_0286D8_PARAM_GEN(1); 1995bf215546Sopenharmony_ci 1996bf215546Sopenharmony_ci shader->ctx_reg.ps.num_interp = num_interp; 1997bf215546Sopenharmony_ci shader->ctx_reg.ps.spi_baryc_cntl = spi_baryc_cntl; 1998bf215546Sopenharmony_ci shader->ctx_reg.ps.spi_ps_in_control = spi_ps_in_control; 1999bf215546Sopenharmony_ci shader->ctx_reg.ps.spi_shader_z_format = 2000bf215546Sopenharmony_ci ac_get_spi_shader_z_format(info->writes_z, info->writes_stencil, info->writes_samplemask, 2001bf215546Sopenharmony_ci shader->key.ps.part.epilog.alpha_to_coverage_via_mrtz); 2002bf215546Sopenharmony_ci shader->ctx_reg.ps.spi_shader_col_format = spi_shader_col_format; 2003bf215546Sopenharmony_ci shader->ctx_reg.ps.cb_shader_mask = cb_shader_mask; 2004bf215546Sopenharmony_ci 2005bf215546Sopenharmony_ci va = shader->bo->gpu_address; 2006bf215546Sopenharmony_ci si_pm4_set_reg(pm4, R_00B020_SPI_SHADER_PGM_LO_PS, va >> 8); 2007bf215546Sopenharmony_ci si_pm4_set_reg(pm4, R_00B024_SPI_SHADER_PGM_HI_PS, 2008bf215546Sopenharmony_ci S_00B024_MEM_BASE(sscreen->info.address32_hi >> 8)); 2009bf215546Sopenharmony_ci 2010bf215546Sopenharmony_ci uint32_t rsrc1 = 2011bf215546Sopenharmony_ci S_00B028_VGPRS((shader->config.num_vgprs - 1) / (shader->wave_size == 32 ? 8 : 4)) | 2012bf215546Sopenharmony_ci S_00B028_DX10_CLAMP(1) | S_00B028_MEM_ORDERED(si_shader_mem_ordered(shader)) | 2013bf215546Sopenharmony_ci S_00B028_FLOAT_MODE(shader->config.float_mode); 2014bf215546Sopenharmony_ci 2015bf215546Sopenharmony_ci if (sscreen->info.gfx_level < GFX10) { 2016bf215546Sopenharmony_ci rsrc1 |= S_00B028_SGPRS((shader->config.num_sgprs - 1) / 8); 2017bf215546Sopenharmony_ci } 2018bf215546Sopenharmony_ci 2019bf215546Sopenharmony_ci si_pm4_set_reg(pm4, R_00B028_SPI_SHADER_PGM_RSRC1_PS, rsrc1); 2020bf215546Sopenharmony_ci si_pm4_set_reg(pm4, R_00B02C_SPI_SHADER_PGM_RSRC2_PS, 2021bf215546Sopenharmony_ci S_00B02C_EXTRA_LDS_SIZE(shader->config.lds_size) | 2022bf215546Sopenharmony_ci S_00B02C_USER_SGPR(SI_PS_NUM_USER_SGPR) | 2023bf215546Sopenharmony_ci S_00B32C_SCRATCH_EN(shader->config.scratch_bytes_per_wave > 0)); 2024bf215546Sopenharmony_ci 2025bf215546Sopenharmony_ci if (sscreen->info.gfx_level >= GFX11) { 2026bf215546Sopenharmony_ci unsigned cu_mask_ps = gfx103_get_cu_mask_ps(sscreen); 2027bf215546Sopenharmony_ci 2028bf215546Sopenharmony_ci ac_set_reg_cu_en(pm4, R_00B004_SPI_SHADER_PGM_RSRC4_PS, 2029bf215546Sopenharmony_ci S_00B004_INST_PREF_SIZE(si_get_shader_prefetch_size(shader)) | 2030bf215546Sopenharmony_ci S_00B004_CU_EN(cu_mask_ps >> 16), 2031bf215546Sopenharmony_ci C_00B004_CU_EN, 16, &sscreen->info, 2032bf215546Sopenharmony_ci (void (*)(void*, unsigned, uint32_t))si_pm4_set_reg_idx3); 2033bf215546Sopenharmony_ci } 2034bf215546Sopenharmony_ci} 2035bf215546Sopenharmony_ci 2036bf215546Sopenharmony_cistatic void si_shader_init_pm4_state(struct si_screen *sscreen, struct si_shader *shader) 2037bf215546Sopenharmony_ci{ 2038bf215546Sopenharmony_ci assert(shader->wave_size); 2039bf215546Sopenharmony_ci 2040bf215546Sopenharmony_ci switch (shader->selector->stage) { 2041bf215546Sopenharmony_ci case MESA_SHADER_VERTEX: 2042bf215546Sopenharmony_ci if (shader->key.ge.as_ls) 2043bf215546Sopenharmony_ci si_shader_ls(sscreen, shader); 2044bf215546Sopenharmony_ci else if (shader->key.ge.as_es) 2045bf215546Sopenharmony_ci si_shader_es(sscreen, shader); 2046bf215546Sopenharmony_ci else if (shader->key.ge.as_ngg) 2047bf215546Sopenharmony_ci gfx10_shader_ngg(sscreen, shader); 2048bf215546Sopenharmony_ci else 2049bf215546Sopenharmony_ci si_shader_vs(sscreen, shader, NULL); 2050bf215546Sopenharmony_ci break; 2051bf215546Sopenharmony_ci case MESA_SHADER_TESS_CTRL: 2052bf215546Sopenharmony_ci si_shader_hs(sscreen, shader); 2053bf215546Sopenharmony_ci break; 2054bf215546Sopenharmony_ci case MESA_SHADER_TESS_EVAL: 2055bf215546Sopenharmony_ci if (shader->key.ge.as_es) 2056bf215546Sopenharmony_ci si_shader_es(sscreen, shader); 2057bf215546Sopenharmony_ci else if (shader->key.ge.as_ngg) 2058bf215546Sopenharmony_ci gfx10_shader_ngg(sscreen, shader); 2059bf215546Sopenharmony_ci else 2060bf215546Sopenharmony_ci si_shader_vs(sscreen, shader, NULL); 2061bf215546Sopenharmony_ci break; 2062bf215546Sopenharmony_ci case MESA_SHADER_GEOMETRY: 2063bf215546Sopenharmony_ci if (shader->key.ge.as_ngg) { 2064bf215546Sopenharmony_ci gfx10_shader_ngg(sscreen, shader); 2065bf215546Sopenharmony_ci } else { 2066bf215546Sopenharmony_ci /* VS must be initialized first because GS uses its fields. */ 2067bf215546Sopenharmony_ci si_shader_vs(sscreen, shader->gs_copy_shader, shader->selector); 2068bf215546Sopenharmony_ci si_shader_gs(sscreen, shader); 2069bf215546Sopenharmony_ci } 2070bf215546Sopenharmony_ci break; 2071bf215546Sopenharmony_ci case MESA_SHADER_FRAGMENT: 2072bf215546Sopenharmony_ci si_shader_ps(sscreen, shader); 2073bf215546Sopenharmony_ci break; 2074bf215546Sopenharmony_ci default: 2075bf215546Sopenharmony_ci assert(0); 2076bf215546Sopenharmony_ci } 2077bf215546Sopenharmony_ci} 2078bf215546Sopenharmony_ci 2079bf215546Sopenharmony_cistatic void si_clear_vs_key_inputs(struct si_context *sctx, union si_shader_key *key, 2080bf215546Sopenharmony_ci struct si_vs_prolog_bits *prolog_key) 2081bf215546Sopenharmony_ci{ 2082bf215546Sopenharmony_ci prolog_key->instance_divisor_is_one = 0; 2083bf215546Sopenharmony_ci prolog_key->instance_divisor_is_fetched = 0; 2084bf215546Sopenharmony_ci key->ge.mono.vs_fetch_opencode = 0; 2085bf215546Sopenharmony_ci memset(key->ge.mono.vs_fix_fetch, 0, sizeof(key->ge.mono.vs_fix_fetch)); 2086bf215546Sopenharmony_ci} 2087bf215546Sopenharmony_ci 2088bf215546Sopenharmony_civoid si_vs_key_update_inputs(struct si_context *sctx) 2089bf215546Sopenharmony_ci{ 2090bf215546Sopenharmony_ci struct si_shader_selector *vs = sctx->shader.vs.cso; 2091bf215546Sopenharmony_ci struct si_vertex_elements *elts = sctx->vertex_elements; 2092bf215546Sopenharmony_ci union si_shader_key *key = &sctx->shader.vs.key; 2093bf215546Sopenharmony_ci 2094bf215546Sopenharmony_ci if (!vs) 2095bf215546Sopenharmony_ci return; 2096bf215546Sopenharmony_ci 2097bf215546Sopenharmony_ci if (vs->info.base.vs.blit_sgprs_amd) { 2098bf215546Sopenharmony_ci si_clear_vs_key_inputs(sctx, key, &key->ge.part.vs.prolog); 2099bf215546Sopenharmony_ci key->ge.opt.prefer_mono = 0; 2100bf215546Sopenharmony_ci sctx->uses_nontrivial_vs_prolog = false; 2101bf215546Sopenharmony_ci return; 2102bf215546Sopenharmony_ci } 2103bf215546Sopenharmony_ci 2104bf215546Sopenharmony_ci bool uses_nontrivial_vs_prolog = false; 2105bf215546Sopenharmony_ci 2106bf215546Sopenharmony_ci if (elts->instance_divisor_is_one || elts->instance_divisor_is_fetched) 2107bf215546Sopenharmony_ci uses_nontrivial_vs_prolog = true; 2108bf215546Sopenharmony_ci 2109bf215546Sopenharmony_ci key->ge.part.vs.prolog.instance_divisor_is_one = elts->instance_divisor_is_one; 2110bf215546Sopenharmony_ci key->ge.part.vs.prolog.instance_divisor_is_fetched = elts->instance_divisor_is_fetched; 2111bf215546Sopenharmony_ci key->ge.opt.prefer_mono = elts->instance_divisor_is_fetched; 2112bf215546Sopenharmony_ci 2113bf215546Sopenharmony_ci unsigned count_mask = (1 << vs->info.num_inputs) - 1; 2114bf215546Sopenharmony_ci unsigned fix = elts->fix_fetch_always & count_mask; 2115bf215546Sopenharmony_ci unsigned opencode = elts->fix_fetch_opencode & count_mask; 2116bf215546Sopenharmony_ci 2117bf215546Sopenharmony_ci if (sctx->vertex_buffer_unaligned & elts->vb_alignment_check_mask) { 2118bf215546Sopenharmony_ci uint32_t mask = elts->fix_fetch_unaligned & count_mask; 2119bf215546Sopenharmony_ci while (mask) { 2120bf215546Sopenharmony_ci unsigned i = u_bit_scan(&mask); 2121bf215546Sopenharmony_ci unsigned log_hw_load_size = 1 + ((elts->hw_load_is_dword >> i) & 1); 2122bf215546Sopenharmony_ci unsigned vbidx = elts->vertex_buffer_index[i]; 2123bf215546Sopenharmony_ci struct pipe_vertex_buffer *vb = &sctx->vertex_buffer[vbidx]; 2124bf215546Sopenharmony_ci unsigned align_mask = (1 << log_hw_load_size) - 1; 2125bf215546Sopenharmony_ci if (vb->buffer_offset & align_mask || vb->stride & align_mask) { 2126bf215546Sopenharmony_ci fix |= 1 << i; 2127bf215546Sopenharmony_ci opencode |= 1 << i; 2128bf215546Sopenharmony_ci } 2129bf215546Sopenharmony_ci } 2130bf215546Sopenharmony_ci } 2131bf215546Sopenharmony_ci 2132bf215546Sopenharmony_ci memset(key->ge.mono.vs_fix_fetch, 0, sizeof(key->ge.mono.vs_fix_fetch)); 2133bf215546Sopenharmony_ci 2134bf215546Sopenharmony_ci while (fix) { 2135bf215546Sopenharmony_ci unsigned i = u_bit_scan(&fix); 2136bf215546Sopenharmony_ci uint8_t fix_fetch = elts->fix_fetch[i]; 2137bf215546Sopenharmony_ci 2138bf215546Sopenharmony_ci key->ge.mono.vs_fix_fetch[i].bits = fix_fetch; 2139bf215546Sopenharmony_ci if (fix_fetch) 2140bf215546Sopenharmony_ci uses_nontrivial_vs_prolog = true; 2141bf215546Sopenharmony_ci } 2142bf215546Sopenharmony_ci key->ge.mono.vs_fetch_opencode = opencode; 2143bf215546Sopenharmony_ci if (opencode) 2144bf215546Sopenharmony_ci uses_nontrivial_vs_prolog = true; 2145bf215546Sopenharmony_ci 2146bf215546Sopenharmony_ci sctx->uses_nontrivial_vs_prolog = uses_nontrivial_vs_prolog; 2147bf215546Sopenharmony_ci 2148bf215546Sopenharmony_ci /* draw_vertex_state (display lists) requires a trivial VS prolog that ignores 2149bf215546Sopenharmony_ci * the current vertex buffers and vertex elements. 2150bf215546Sopenharmony_ci * 2151bf215546Sopenharmony_ci * We just computed the prolog key because we needed to set uses_nontrivial_vs_prolog, 2152bf215546Sopenharmony_ci * so that we know whether the VS prolog should be updated when we switch from 2153bf215546Sopenharmony_ci * draw_vertex_state to draw_vbo. Now clear the VS prolog for draw_vertex_state. 2154bf215546Sopenharmony_ci * This should happen rarely because the VS prolog should be trivial in most 2155bf215546Sopenharmony_ci * cases. 2156bf215546Sopenharmony_ci */ 2157bf215546Sopenharmony_ci if (uses_nontrivial_vs_prolog && sctx->force_trivial_vs_prolog) 2158bf215546Sopenharmony_ci si_clear_vs_key_inputs(sctx, key, &key->ge.part.vs.prolog); 2159bf215546Sopenharmony_ci} 2160bf215546Sopenharmony_ci 2161bf215546Sopenharmony_civoid si_get_vs_key_inputs(struct si_context *sctx, union si_shader_key *key, 2162bf215546Sopenharmony_ci struct si_vs_prolog_bits *prolog_key) 2163bf215546Sopenharmony_ci{ 2164bf215546Sopenharmony_ci prolog_key->instance_divisor_is_one = sctx->shader.vs.key.ge.part.vs.prolog.instance_divisor_is_one; 2165bf215546Sopenharmony_ci prolog_key->instance_divisor_is_fetched = sctx->shader.vs.key.ge.part.vs.prolog.instance_divisor_is_fetched; 2166bf215546Sopenharmony_ci 2167bf215546Sopenharmony_ci key->ge.mono.vs_fetch_opencode = sctx->shader.vs.key.ge.mono.vs_fetch_opencode; 2168bf215546Sopenharmony_ci memcpy(key->ge.mono.vs_fix_fetch, sctx->shader.vs.key.ge.mono.vs_fix_fetch, 2169bf215546Sopenharmony_ci sizeof(key->ge.mono.vs_fix_fetch)); 2170bf215546Sopenharmony_ci} 2171bf215546Sopenharmony_ci 2172bf215546Sopenharmony_civoid si_update_ps_inputs_read_or_disabled(struct si_context *sctx) 2173bf215546Sopenharmony_ci{ 2174bf215546Sopenharmony_ci struct si_shader_selector *ps = sctx->shader.ps.cso; 2175bf215546Sopenharmony_ci 2176bf215546Sopenharmony_ci /* Find out if PS is disabled. */ 2177bf215546Sopenharmony_ci bool ps_disabled = true; 2178bf215546Sopenharmony_ci if (ps) { 2179bf215546Sopenharmony_ci bool ps_modifies_zs = ps->info.base.fs.uses_discard || 2180bf215546Sopenharmony_ci ps->info.writes_z || 2181bf215546Sopenharmony_ci ps->info.writes_stencil || 2182bf215546Sopenharmony_ci ps->info.writes_samplemask || 2183bf215546Sopenharmony_ci sctx->queued.named.blend->alpha_to_coverage || 2184bf215546Sopenharmony_ci sctx->queued.named.dsa->alpha_func != PIPE_FUNC_ALWAYS || 2185bf215546Sopenharmony_ci sctx->queued.named.rasterizer->poly_stipple_enable || 2186bf215546Sopenharmony_ci sctx->queued.named.rasterizer->point_smooth; 2187bf215546Sopenharmony_ci unsigned ps_colormask = si_get_total_colormask(sctx); 2188bf215546Sopenharmony_ci 2189bf215546Sopenharmony_ci ps_disabled = sctx->queued.named.rasterizer->rasterizer_discard || 2190bf215546Sopenharmony_ci (!ps_colormask && !ps_modifies_zs && !ps->info.base.writes_memory); 2191bf215546Sopenharmony_ci } 2192bf215546Sopenharmony_ci 2193bf215546Sopenharmony_ci sctx->ps_inputs_read_or_disabled = ps_disabled ? 0 : ps->info.inputs_read; 2194bf215546Sopenharmony_ci} 2195bf215546Sopenharmony_ci 2196bf215546Sopenharmony_cistatic void si_get_vs_key_outputs(struct si_context *sctx, struct si_shader_selector *vs, 2197bf215546Sopenharmony_ci union si_shader_key *key) 2198bf215546Sopenharmony_ci{ 2199bf215546Sopenharmony_ci key->ge.opt.kill_clip_distances = vs->info.clipdist_mask & ~sctx->queued.named.rasterizer->clip_plane_enable; 2200bf215546Sopenharmony_ci 2201bf215546Sopenharmony_ci /* Find out which VS outputs aren't used by the PS. */ 2202bf215546Sopenharmony_ci uint64_t outputs_written = vs->info.outputs_written_before_ps; 2203bf215546Sopenharmony_ci uint64_t linked = outputs_written & sctx->ps_inputs_read_or_disabled; 2204bf215546Sopenharmony_ci 2205bf215546Sopenharmony_ci key->ge.opt.kill_outputs = ~linked & outputs_written; 2206bf215546Sopenharmony_ci key->ge.opt.ngg_culling = sctx->ngg_culling; 2207bf215546Sopenharmony_ci key->ge.mono.u.vs_export_prim_id = vs->stage != MESA_SHADER_GEOMETRY && 2208bf215546Sopenharmony_ci sctx->shader.ps.cso && sctx->shader.ps.cso->info.uses_primid; 2209bf215546Sopenharmony_ci key->ge.opt.kill_pointsize = vs->info.writes_psize && 2210bf215546Sopenharmony_ci sctx->current_rast_prim != PIPE_PRIM_POINTS && 2211bf215546Sopenharmony_ci !sctx->queued.named.rasterizer->polygon_mode_is_points; 2212bf215546Sopenharmony_ci key->ge.opt.remove_streamout = vs->info.enabled_streamout_buffer_mask && 2213bf215546Sopenharmony_ci !sctx->streamout.enabled_mask; 2214bf215546Sopenharmony_ci} 2215bf215546Sopenharmony_ci 2216bf215546Sopenharmony_cistatic void si_clear_vs_key_outputs(struct si_context *sctx, struct si_shader_selector *vs, 2217bf215546Sopenharmony_ci union si_shader_key *key) 2218bf215546Sopenharmony_ci{ 2219bf215546Sopenharmony_ci key->ge.opt.kill_clip_distances = 0; 2220bf215546Sopenharmony_ci key->ge.opt.kill_outputs = 0; 2221bf215546Sopenharmony_ci key->ge.opt.remove_streamout = 0; 2222bf215546Sopenharmony_ci key->ge.opt.ngg_culling = 0; 2223bf215546Sopenharmony_ci key->ge.mono.u.vs_export_prim_id = 0; 2224bf215546Sopenharmony_ci key->ge.opt.kill_pointsize = 0; 2225bf215546Sopenharmony_ci} 2226bf215546Sopenharmony_ci 2227bf215546Sopenharmony_civoid si_ps_key_update_framebuffer(struct si_context *sctx) 2228bf215546Sopenharmony_ci{ 2229bf215546Sopenharmony_ci struct si_shader_selector *sel = sctx->shader.ps.cso; 2230bf215546Sopenharmony_ci union si_shader_key *key = &sctx->shader.ps.key; 2231bf215546Sopenharmony_ci 2232bf215546Sopenharmony_ci if (!sel) 2233bf215546Sopenharmony_ci return; 2234bf215546Sopenharmony_ci 2235bf215546Sopenharmony_ci if (sel->info.color0_writes_all_cbufs && 2236bf215546Sopenharmony_ci sel->info.colors_written == 0x1) 2237bf215546Sopenharmony_ci key->ps.part.epilog.last_cbuf = MAX2(sctx->framebuffer.state.nr_cbufs, 1) - 1; 2238bf215546Sopenharmony_ci else 2239bf215546Sopenharmony_ci key->ps.part.epilog.last_cbuf = 0; 2240bf215546Sopenharmony_ci 2241bf215546Sopenharmony_ci /* ps_uses_fbfetch is true only if the color buffer is bound. */ 2242bf215546Sopenharmony_ci if (sctx->ps_uses_fbfetch) { 2243bf215546Sopenharmony_ci struct pipe_surface *cb0 = sctx->framebuffer.state.cbufs[0]; 2244bf215546Sopenharmony_ci struct pipe_resource *tex = cb0->texture; 2245bf215546Sopenharmony_ci 2246bf215546Sopenharmony_ci /* 1D textures are allocated and used as 2D on GFX9. */ 2247bf215546Sopenharmony_ci key->ps.mono.fbfetch_msaa = sctx->framebuffer.nr_samples > 1; 2248bf215546Sopenharmony_ci key->ps.mono.fbfetch_is_1D = 2249bf215546Sopenharmony_ci sctx->gfx_level != GFX9 && 2250bf215546Sopenharmony_ci (tex->target == PIPE_TEXTURE_1D || tex->target == PIPE_TEXTURE_1D_ARRAY); 2251bf215546Sopenharmony_ci key->ps.mono.fbfetch_layered = 2252bf215546Sopenharmony_ci tex->target == PIPE_TEXTURE_1D_ARRAY || tex->target == PIPE_TEXTURE_2D_ARRAY || 2253bf215546Sopenharmony_ci tex->target == PIPE_TEXTURE_CUBE || tex->target == PIPE_TEXTURE_CUBE_ARRAY || 2254bf215546Sopenharmony_ci tex->target == PIPE_TEXTURE_3D; 2255bf215546Sopenharmony_ci } else { 2256bf215546Sopenharmony_ci key->ps.mono.fbfetch_msaa = 0; 2257bf215546Sopenharmony_ci key->ps.mono.fbfetch_is_1D = 0; 2258bf215546Sopenharmony_ci key->ps.mono.fbfetch_layered = 0; 2259bf215546Sopenharmony_ci } 2260bf215546Sopenharmony_ci} 2261bf215546Sopenharmony_ci 2262bf215546Sopenharmony_civoid si_ps_key_update_framebuffer_blend(struct si_context *sctx) 2263bf215546Sopenharmony_ci{ 2264bf215546Sopenharmony_ci struct si_shader_selector *sel = sctx->shader.ps.cso; 2265bf215546Sopenharmony_ci union si_shader_key *key = &sctx->shader.ps.key; 2266bf215546Sopenharmony_ci struct si_state_blend *blend = sctx->queued.named.blend; 2267bf215546Sopenharmony_ci 2268bf215546Sopenharmony_ci if (!sel) 2269bf215546Sopenharmony_ci return; 2270bf215546Sopenharmony_ci 2271bf215546Sopenharmony_ci /* Select the shader color format based on whether 2272bf215546Sopenharmony_ci * blending or alpha are needed. 2273bf215546Sopenharmony_ci */ 2274bf215546Sopenharmony_ci key->ps.part.epilog.spi_shader_col_format = 2275bf215546Sopenharmony_ci (blend->blend_enable_4bit & blend->need_src_alpha_4bit & 2276bf215546Sopenharmony_ci sctx->framebuffer.spi_shader_col_format_blend_alpha) | 2277bf215546Sopenharmony_ci (blend->blend_enable_4bit & ~blend->need_src_alpha_4bit & 2278bf215546Sopenharmony_ci sctx->framebuffer.spi_shader_col_format_blend) | 2279bf215546Sopenharmony_ci (~blend->blend_enable_4bit & blend->need_src_alpha_4bit & 2280bf215546Sopenharmony_ci sctx->framebuffer.spi_shader_col_format_alpha) | 2281bf215546Sopenharmony_ci (~blend->blend_enable_4bit & ~blend->need_src_alpha_4bit & 2282bf215546Sopenharmony_ci sctx->framebuffer.spi_shader_col_format); 2283bf215546Sopenharmony_ci key->ps.part.epilog.spi_shader_col_format &= blend->cb_target_enabled_4bit; 2284bf215546Sopenharmony_ci 2285bf215546Sopenharmony_ci key->ps.part.epilog.dual_src_blend_swizzle = sctx->gfx_level >= GFX11 && 2286bf215546Sopenharmony_ci blend->dual_src_blend && 2287bf215546Sopenharmony_ci (sel->info.colors_written_4bit & 0xff) == 0xff; 2288bf215546Sopenharmony_ci 2289bf215546Sopenharmony_ci /* The output for dual source blending should have 2290bf215546Sopenharmony_ci * the same format as the first output. 2291bf215546Sopenharmony_ci */ 2292bf215546Sopenharmony_ci if (blend->dual_src_blend) { 2293bf215546Sopenharmony_ci key->ps.part.epilog.spi_shader_col_format |= 2294bf215546Sopenharmony_ci (key->ps.part.epilog.spi_shader_col_format & 0xf) << 4; 2295bf215546Sopenharmony_ci } 2296bf215546Sopenharmony_ci 2297bf215546Sopenharmony_ci /* If alpha-to-coverage is enabled, we have to export alpha 2298bf215546Sopenharmony_ci * even if there is no color buffer. 2299bf215546Sopenharmony_ci */ 2300bf215546Sopenharmony_ci if (!(key->ps.part.epilog.spi_shader_col_format & 0xf) && blend->alpha_to_coverage) 2301bf215546Sopenharmony_ci key->ps.part.epilog.spi_shader_col_format |= V_028710_SPI_SHADER_32_AR; 2302bf215546Sopenharmony_ci 2303bf215546Sopenharmony_ci /* On GFX6 and GFX7 except Hawaii, the CB doesn't clamp outputs 2304bf215546Sopenharmony_ci * to the range supported by the type if a channel has less 2305bf215546Sopenharmony_ci * than 16 bits and the export format is 16_ABGR. 2306bf215546Sopenharmony_ci */ 2307bf215546Sopenharmony_ci if (sctx->gfx_level <= GFX7 && sctx->family != CHIP_HAWAII) { 2308bf215546Sopenharmony_ci key->ps.part.epilog.color_is_int8 = sctx->framebuffer.color_is_int8; 2309bf215546Sopenharmony_ci key->ps.part.epilog.color_is_int10 = sctx->framebuffer.color_is_int10; 2310bf215546Sopenharmony_ci } 2311bf215546Sopenharmony_ci 2312bf215546Sopenharmony_ci /* Disable unwritten outputs (if WRITE_ALL_CBUFS isn't enabled). */ 2313bf215546Sopenharmony_ci if (!key->ps.part.epilog.last_cbuf) { 2314bf215546Sopenharmony_ci key->ps.part.epilog.spi_shader_col_format &= sel->info.colors_written_4bit; 2315bf215546Sopenharmony_ci key->ps.part.epilog.color_is_int8 &= sel->info.colors_written; 2316bf215546Sopenharmony_ci key->ps.part.epilog.color_is_int10 &= sel->info.colors_written; 2317bf215546Sopenharmony_ci } 2318bf215546Sopenharmony_ci 2319bf215546Sopenharmony_ci /* Eliminate shader code computing output values that are unused. 2320bf215546Sopenharmony_ci * This enables dead code elimination between shader parts. 2321bf215546Sopenharmony_ci * Check if any output is eliminated. 2322bf215546Sopenharmony_ci * 2323bf215546Sopenharmony_ci * Dual source blending never has color buffer 1 enabled, so ignore it. 2324bf215546Sopenharmony_ci * 2325bf215546Sopenharmony_ci * On gfx11, pixel shaders that write memory should be compiled with an inlined epilog, 2326bf215546Sopenharmony_ci * so that the compiler can see s_endpgm and deallocates VGPRs before memory stores return. 2327bf215546Sopenharmony_ci */ 2328bf215546Sopenharmony_ci if (sel->info.colors_written_4bit & 2329bf215546Sopenharmony_ci (blend->dual_src_blend ? 0xffffff0f : 0xffffffff) & 2330bf215546Sopenharmony_ci ~(sctx->framebuffer.colorbuf_enabled_4bit & blend->cb_target_enabled_4bit)) 2331bf215546Sopenharmony_ci key->ps.opt.prefer_mono = 1; 2332bf215546Sopenharmony_ci else if (sctx->gfx_level >= GFX11 && sel->info.base.writes_memory) 2333bf215546Sopenharmony_ci key->ps.opt.prefer_mono = 1; 2334bf215546Sopenharmony_ci else 2335bf215546Sopenharmony_ci key->ps.opt.prefer_mono = 0; 2336bf215546Sopenharmony_ci} 2337bf215546Sopenharmony_ci 2338bf215546Sopenharmony_civoid si_ps_key_update_blend_rasterizer(struct si_context *sctx) 2339bf215546Sopenharmony_ci{ 2340bf215546Sopenharmony_ci union si_shader_key *key = &sctx->shader.ps.key; 2341bf215546Sopenharmony_ci struct si_state_blend *blend = sctx->queued.named.blend; 2342bf215546Sopenharmony_ci struct si_state_rasterizer *rs = sctx->queued.named.rasterizer; 2343bf215546Sopenharmony_ci struct si_shader_selector *ps = sctx->shader.ps.cso; 2344bf215546Sopenharmony_ci 2345bf215546Sopenharmony_ci if (!ps) 2346bf215546Sopenharmony_ci return; 2347bf215546Sopenharmony_ci 2348bf215546Sopenharmony_ci key->ps.part.epilog.alpha_to_one = blend->alpha_to_one && rs->multisample_enable; 2349bf215546Sopenharmony_ci key->ps.part.epilog.alpha_to_coverage_via_mrtz = 2350bf215546Sopenharmony_ci sctx->gfx_level >= GFX11 && blend->alpha_to_coverage && rs->multisample_enable && 2351bf215546Sopenharmony_ci (ps->info.writes_z || ps->info.writes_stencil || ps->info.writes_samplemask); 2352bf215546Sopenharmony_ci} 2353bf215546Sopenharmony_ci 2354bf215546Sopenharmony_civoid si_ps_key_update_rasterizer(struct si_context *sctx) 2355bf215546Sopenharmony_ci{ 2356bf215546Sopenharmony_ci struct si_shader_selector *sel = sctx->shader.ps.cso; 2357bf215546Sopenharmony_ci union si_shader_key *key = &sctx->shader.ps.key; 2358bf215546Sopenharmony_ci struct si_state_rasterizer *rs = sctx->queued.named.rasterizer; 2359bf215546Sopenharmony_ci 2360bf215546Sopenharmony_ci if (!sel) 2361bf215546Sopenharmony_ci return; 2362bf215546Sopenharmony_ci 2363bf215546Sopenharmony_ci key->ps.part.prolog.color_two_side = rs->two_side && sel->info.colors_read; 2364bf215546Sopenharmony_ci key->ps.part.prolog.flatshade_colors = rs->flatshade && sel->info.uses_interp_color; 2365bf215546Sopenharmony_ci key->ps.part.epilog.clamp_color = rs->clamp_fragment_color; 2366bf215546Sopenharmony_ci} 2367bf215546Sopenharmony_ci 2368bf215546Sopenharmony_civoid si_ps_key_update_dsa(struct si_context *sctx) 2369bf215546Sopenharmony_ci{ 2370bf215546Sopenharmony_ci union si_shader_key *key = &sctx->shader.ps.key; 2371bf215546Sopenharmony_ci 2372bf215546Sopenharmony_ci key->ps.part.epilog.alpha_func = sctx->queued.named.dsa->alpha_func; 2373bf215546Sopenharmony_ci} 2374bf215546Sopenharmony_ci 2375bf215546Sopenharmony_cistatic void si_ps_key_update_primtype_shader_rasterizer_framebuffer(struct si_context *sctx) 2376bf215546Sopenharmony_ci{ 2377bf215546Sopenharmony_ci union si_shader_key *key = &sctx->shader.ps.key; 2378bf215546Sopenharmony_ci struct si_state_rasterizer *rs = sctx->queued.named.rasterizer; 2379bf215546Sopenharmony_ci 2380bf215546Sopenharmony_ci bool is_poly = !util_prim_is_points_or_lines(sctx->current_rast_prim); 2381bf215546Sopenharmony_ci bool is_line = util_prim_is_lines(sctx->current_rast_prim); 2382bf215546Sopenharmony_ci 2383bf215546Sopenharmony_ci key->ps.part.prolog.poly_stipple = rs->poly_stipple_enable && is_poly; 2384bf215546Sopenharmony_ci key->ps.mono.poly_line_smoothing = 2385bf215546Sopenharmony_ci ((is_poly && rs->poly_smooth) || (is_line && rs->line_smooth)) && 2386bf215546Sopenharmony_ci sctx->framebuffer.nr_samples <= 1; 2387bf215546Sopenharmony_ci 2388bf215546Sopenharmony_ci key->ps.mono.point_smoothing = rs->point_smooth && 2389bf215546Sopenharmony_ci sctx->current_rast_prim == PIPE_PRIM_POINTS; 2390bf215546Sopenharmony_ci} 2391bf215546Sopenharmony_ci 2392bf215546Sopenharmony_civoid si_ps_key_update_sample_shading(struct si_context *sctx) 2393bf215546Sopenharmony_ci{ 2394bf215546Sopenharmony_ci struct si_shader_selector *sel = sctx->shader.ps.cso; 2395bf215546Sopenharmony_ci union si_shader_key *key = &sctx->shader.ps.key; 2396bf215546Sopenharmony_ci 2397bf215546Sopenharmony_ci if (!sel) 2398bf215546Sopenharmony_ci return; 2399bf215546Sopenharmony_ci 2400bf215546Sopenharmony_ci if (sctx->ps_iter_samples > 1 && sel->info.reads_samplemask) 2401bf215546Sopenharmony_ci key->ps.part.prolog.samplemask_log_ps_iter = util_logbase2(sctx->ps_iter_samples); 2402bf215546Sopenharmony_ci else 2403bf215546Sopenharmony_ci key->ps.part.prolog.samplemask_log_ps_iter = 0; 2404bf215546Sopenharmony_ci} 2405bf215546Sopenharmony_ci 2406bf215546Sopenharmony_civoid si_ps_key_update_framebuffer_rasterizer_sample_shading(struct si_context *sctx) 2407bf215546Sopenharmony_ci{ 2408bf215546Sopenharmony_ci struct si_shader_selector *sel = sctx->shader.ps.cso; 2409bf215546Sopenharmony_ci union si_shader_key *key = &sctx->shader.ps.key; 2410bf215546Sopenharmony_ci struct si_state_rasterizer *rs = sctx->queued.named.rasterizer; 2411bf215546Sopenharmony_ci 2412bf215546Sopenharmony_ci if (!sel) 2413bf215546Sopenharmony_ci return; 2414bf215546Sopenharmony_ci 2415bf215546Sopenharmony_ci bool uses_persp_center = sel->info.uses_persp_center || 2416bf215546Sopenharmony_ci (!rs->flatshade && sel->info.uses_persp_center_color); 2417bf215546Sopenharmony_ci bool uses_persp_centroid = sel->info.uses_persp_centroid || 2418bf215546Sopenharmony_ci (!rs->flatshade && sel->info.uses_persp_centroid_color); 2419bf215546Sopenharmony_ci bool uses_persp_sample = sel->info.uses_persp_sample || 2420bf215546Sopenharmony_ci (!rs->flatshade && sel->info.uses_persp_sample_color); 2421bf215546Sopenharmony_ci 2422bf215546Sopenharmony_ci if (rs->force_persample_interp && rs->multisample_enable && 2423bf215546Sopenharmony_ci sctx->framebuffer.nr_samples > 1 && sctx->ps_iter_samples > 1) { 2424bf215546Sopenharmony_ci key->ps.part.prolog.force_persp_sample_interp = 2425bf215546Sopenharmony_ci uses_persp_center || uses_persp_centroid; 2426bf215546Sopenharmony_ci 2427bf215546Sopenharmony_ci key->ps.part.prolog.force_linear_sample_interp = 2428bf215546Sopenharmony_ci sel->info.uses_linear_center || sel->info.uses_linear_centroid; 2429bf215546Sopenharmony_ci 2430bf215546Sopenharmony_ci key->ps.part.prolog.force_persp_center_interp = 0; 2431bf215546Sopenharmony_ci key->ps.part.prolog.force_linear_center_interp = 0; 2432bf215546Sopenharmony_ci key->ps.part.prolog.bc_optimize_for_persp = 0; 2433bf215546Sopenharmony_ci key->ps.part.prolog.bc_optimize_for_linear = 0; 2434bf215546Sopenharmony_ci key->ps.mono.interpolate_at_sample_force_center = 0; 2435bf215546Sopenharmony_ci } else if (rs->multisample_enable && sctx->framebuffer.nr_samples > 1) { 2436bf215546Sopenharmony_ci key->ps.part.prolog.force_persp_sample_interp = 0; 2437bf215546Sopenharmony_ci key->ps.part.prolog.force_linear_sample_interp = 0; 2438bf215546Sopenharmony_ci key->ps.part.prolog.force_persp_center_interp = 0; 2439bf215546Sopenharmony_ci key->ps.part.prolog.force_linear_center_interp = 0; 2440bf215546Sopenharmony_ci key->ps.part.prolog.bc_optimize_for_persp = 2441bf215546Sopenharmony_ci uses_persp_center && uses_persp_centroid; 2442bf215546Sopenharmony_ci key->ps.part.prolog.bc_optimize_for_linear = 2443bf215546Sopenharmony_ci sel->info.uses_linear_center && sel->info.uses_linear_centroid; 2444bf215546Sopenharmony_ci key->ps.mono.interpolate_at_sample_force_center = 0; 2445bf215546Sopenharmony_ci } else { 2446bf215546Sopenharmony_ci key->ps.part.prolog.force_persp_sample_interp = 0; 2447bf215546Sopenharmony_ci key->ps.part.prolog.force_linear_sample_interp = 0; 2448bf215546Sopenharmony_ci 2449bf215546Sopenharmony_ci /* Make sure SPI doesn't compute more than 1 pair 2450bf215546Sopenharmony_ci * of (i,j), which is the optimization here. */ 2451bf215546Sopenharmony_ci key->ps.part.prolog.force_persp_center_interp = uses_persp_center + 2452bf215546Sopenharmony_ci uses_persp_centroid + 2453bf215546Sopenharmony_ci uses_persp_sample > 1; 2454bf215546Sopenharmony_ci 2455bf215546Sopenharmony_ci key->ps.part.prolog.force_linear_center_interp = sel->info.uses_linear_center + 2456bf215546Sopenharmony_ci sel->info.uses_linear_centroid + 2457bf215546Sopenharmony_ci sel->info.uses_linear_sample > 1; 2458bf215546Sopenharmony_ci key->ps.part.prolog.bc_optimize_for_persp = 0; 2459bf215546Sopenharmony_ci key->ps.part.prolog.bc_optimize_for_linear = 0; 2460bf215546Sopenharmony_ci key->ps.mono.interpolate_at_sample_force_center = sel->info.uses_interp_at_sample; 2461bf215546Sopenharmony_ci } 2462bf215546Sopenharmony_ci} 2463bf215546Sopenharmony_ci 2464bf215546Sopenharmony_ci/* Compute the key for the hw shader variant */ 2465bf215546Sopenharmony_cistatic inline void si_shader_selector_key(struct pipe_context *ctx, struct si_shader_selector *sel, 2466bf215546Sopenharmony_ci union si_shader_key *key) 2467bf215546Sopenharmony_ci{ 2468bf215546Sopenharmony_ci struct si_context *sctx = (struct si_context *)ctx; 2469bf215546Sopenharmony_ci 2470bf215546Sopenharmony_ci switch (sel->stage) { 2471bf215546Sopenharmony_ci case MESA_SHADER_VERTEX: 2472bf215546Sopenharmony_ci if (!sctx->shader.tes.cso && !sctx->shader.gs.cso) 2473bf215546Sopenharmony_ci si_get_vs_key_outputs(sctx, sel, key); 2474bf215546Sopenharmony_ci else 2475bf215546Sopenharmony_ci si_clear_vs_key_outputs(sctx, sel, key); 2476bf215546Sopenharmony_ci break; 2477bf215546Sopenharmony_ci case MESA_SHADER_TESS_CTRL: 2478bf215546Sopenharmony_ci if (sctx->gfx_level >= GFX9) { 2479bf215546Sopenharmony_ci si_get_vs_key_inputs(sctx, key, &key->ge.part.tcs.ls_prolog); 2480bf215546Sopenharmony_ci key->ge.part.tcs.ls = sctx->shader.vs.cso; 2481bf215546Sopenharmony_ci } 2482bf215546Sopenharmony_ci break; 2483bf215546Sopenharmony_ci case MESA_SHADER_TESS_EVAL: 2484bf215546Sopenharmony_ci if (!sctx->shader.gs.cso) 2485bf215546Sopenharmony_ci si_get_vs_key_outputs(sctx, sel, key); 2486bf215546Sopenharmony_ci else 2487bf215546Sopenharmony_ci si_clear_vs_key_outputs(sctx, sel, key); 2488bf215546Sopenharmony_ci break; 2489bf215546Sopenharmony_ci case MESA_SHADER_GEOMETRY: 2490bf215546Sopenharmony_ci if (sctx->gfx_level >= GFX9) { 2491bf215546Sopenharmony_ci if (sctx->shader.tes.cso) { 2492bf215546Sopenharmony_ci si_clear_vs_key_inputs(sctx, key, &key->ge.part.gs.vs_prolog); 2493bf215546Sopenharmony_ci key->ge.part.gs.es = sctx->shader.tes.cso; 2494bf215546Sopenharmony_ci } else { 2495bf215546Sopenharmony_ci si_get_vs_key_inputs(sctx, key, &key->ge.part.gs.vs_prolog); 2496bf215546Sopenharmony_ci key->ge.part.gs.es = sctx->shader.vs.cso; 2497bf215546Sopenharmony_ci } 2498bf215546Sopenharmony_ci 2499bf215546Sopenharmony_ci /* Only NGG can eliminate GS outputs, because the code is shared with VS. */ 2500bf215546Sopenharmony_ci if (sctx->ngg) 2501bf215546Sopenharmony_ci si_get_vs_key_outputs(sctx, sel, key); 2502bf215546Sopenharmony_ci else 2503bf215546Sopenharmony_ci si_clear_vs_key_outputs(sctx, sel, key); 2504bf215546Sopenharmony_ci } 2505bf215546Sopenharmony_ci break; 2506bf215546Sopenharmony_ci case MESA_SHADER_FRAGMENT: 2507bf215546Sopenharmony_ci si_ps_key_update_primtype_shader_rasterizer_framebuffer(sctx); 2508bf215546Sopenharmony_ci break; 2509bf215546Sopenharmony_ci default: 2510bf215546Sopenharmony_ci assert(0); 2511bf215546Sopenharmony_ci } 2512bf215546Sopenharmony_ci} 2513bf215546Sopenharmony_ci 2514bf215546Sopenharmony_cistatic void si_build_shader_variant(struct si_shader *shader, int thread_index, bool low_priority) 2515bf215546Sopenharmony_ci{ 2516bf215546Sopenharmony_ci struct si_shader_selector *sel = shader->selector; 2517bf215546Sopenharmony_ci struct si_screen *sscreen = sel->screen; 2518bf215546Sopenharmony_ci struct ac_llvm_compiler *compiler; 2519bf215546Sopenharmony_ci struct util_debug_callback *debug = &shader->compiler_ctx_state.debug; 2520bf215546Sopenharmony_ci 2521bf215546Sopenharmony_ci if (thread_index >= 0) { 2522bf215546Sopenharmony_ci if (low_priority) { 2523bf215546Sopenharmony_ci assert(thread_index < (int)ARRAY_SIZE(sscreen->compiler_lowp)); 2524bf215546Sopenharmony_ci compiler = &sscreen->compiler_lowp[thread_index]; 2525bf215546Sopenharmony_ci } else { 2526bf215546Sopenharmony_ci assert(thread_index < (int)ARRAY_SIZE(sscreen->compiler)); 2527bf215546Sopenharmony_ci compiler = &sscreen->compiler[thread_index]; 2528bf215546Sopenharmony_ci } 2529bf215546Sopenharmony_ci if (!debug->async) 2530bf215546Sopenharmony_ci debug = NULL; 2531bf215546Sopenharmony_ci } else { 2532bf215546Sopenharmony_ci assert(!low_priority); 2533bf215546Sopenharmony_ci compiler = shader->compiler_ctx_state.compiler; 2534bf215546Sopenharmony_ci } 2535bf215546Sopenharmony_ci 2536bf215546Sopenharmony_ci if (!compiler->passes) 2537bf215546Sopenharmony_ci si_init_compiler(sscreen, compiler); 2538bf215546Sopenharmony_ci 2539bf215546Sopenharmony_ci if (unlikely(!si_create_shader_variant(sscreen, compiler, shader, debug))) { 2540bf215546Sopenharmony_ci PRINT_ERR("Failed to build shader variant (type=%u)\n", sel->stage); 2541bf215546Sopenharmony_ci shader->compilation_failed = true; 2542bf215546Sopenharmony_ci return; 2543bf215546Sopenharmony_ci } 2544bf215546Sopenharmony_ci 2545bf215546Sopenharmony_ci if (shader->compiler_ctx_state.is_debug_context) { 2546bf215546Sopenharmony_ci FILE *f = open_memstream(&shader->shader_log, &shader->shader_log_size); 2547bf215546Sopenharmony_ci if (f) { 2548bf215546Sopenharmony_ci si_shader_dump(sscreen, shader, NULL, f, false); 2549bf215546Sopenharmony_ci fclose(f); 2550bf215546Sopenharmony_ci } 2551bf215546Sopenharmony_ci } 2552bf215546Sopenharmony_ci 2553bf215546Sopenharmony_ci si_shader_init_pm4_state(sscreen, shader); 2554bf215546Sopenharmony_ci} 2555bf215546Sopenharmony_ci 2556bf215546Sopenharmony_cistatic void si_build_shader_variant_low_priority(void *job, void *gdata, int thread_index) 2557bf215546Sopenharmony_ci{ 2558bf215546Sopenharmony_ci struct si_shader *shader = (struct si_shader *)job; 2559bf215546Sopenharmony_ci 2560bf215546Sopenharmony_ci assert(thread_index >= 0); 2561bf215546Sopenharmony_ci 2562bf215546Sopenharmony_ci si_build_shader_variant(shader, thread_index, true); 2563bf215546Sopenharmony_ci} 2564bf215546Sopenharmony_ci 2565bf215546Sopenharmony_ci/* This should be const, but C++ doesn't allow implicit zero-initialization with const. */ 2566bf215546Sopenharmony_cistatic union si_shader_key zeroed; 2567bf215546Sopenharmony_ci 2568bf215546Sopenharmony_cistatic bool si_check_missing_main_part(struct si_screen *sscreen, struct si_shader_selector *sel, 2569bf215546Sopenharmony_ci struct si_compiler_ctx_state *compiler_state, 2570bf215546Sopenharmony_ci const union si_shader_key *key) 2571bf215546Sopenharmony_ci{ 2572bf215546Sopenharmony_ci struct si_shader **mainp = si_get_main_shader_part(sel, key); 2573bf215546Sopenharmony_ci 2574bf215546Sopenharmony_ci if (!*mainp) { 2575bf215546Sopenharmony_ci struct si_shader *main_part = CALLOC_STRUCT(si_shader); 2576bf215546Sopenharmony_ci 2577bf215546Sopenharmony_ci if (!main_part) 2578bf215546Sopenharmony_ci return false; 2579bf215546Sopenharmony_ci 2580bf215546Sopenharmony_ci /* We can leave the fence as permanently signaled because the 2581bf215546Sopenharmony_ci * main part becomes visible globally only after it has been 2582bf215546Sopenharmony_ci * compiled. */ 2583bf215546Sopenharmony_ci util_queue_fence_init(&main_part->ready); 2584bf215546Sopenharmony_ci 2585bf215546Sopenharmony_ci main_part->selector = sel; 2586bf215546Sopenharmony_ci if (sel->stage <= MESA_SHADER_GEOMETRY) { 2587bf215546Sopenharmony_ci main_part->key.ge.as_es = key->ge.as_es; 2588bf215546Sopenharmony_ci main_part->key.ge.as_ls = key->ge.as_ls; 2589bf215546Sopenharmony_ci main_part->key.ge.as_ngg = key->ge.as_ngg; 2590bf215546Sopenharmony_ci } 2591bf215546Sopenharmony_ci main_part->is_monolithic = false; 2592bf215546Sopenharmony_ci main_part->wave_size = si_determine_wave_size(sscreen, main_part); 2593bf215546Sopenharmony_ci 2594bf215546Sopenharmony_ci if (!si_compile_shader(sscreen, compiler_state->compiler, main_part, 2595bf215546Sopenharmony_ci &compiler_state->debug)) { 2596bf215546Sopenharmony_ci FREE(main_part); 2597bf215546Sopenharmony_ci return false; 2598bf215546Sopenharmony_ci } 2599bf215546Sopenharmony_ci *mainp = main_part; 2600bf215546Sopenharmony_ci } 2601bf215546Sopenharmony_ci return true; 2602bf215546Sopenharmony_ci} 2603bf215546Sopenharmony_ci 2604bf215546Sopenharmony_ci/* A helper to copy *key to *local_key and return local_key. */ 2605bf215546Sopenharmony_citemplate<typename SHADER_KEY_TYPE> 2606bf215546Sopenharmony_cistatic ALWAYS_INLINE const SHADER_KEY_TYPE * 2607bf215546Sopenharmony_ciuse_local_key_copy(const SHADER_KEY_TYPE *key, SHADER_KEY_TYPE *local_key, unsigned key_size) 2608bf215546Sopenharmony_ci{ 2609bf215546Sopenharmony_ci if (key != local_key) 2610bf215546Sopenharmony_ci memcpy(local_key, key, key_size); 2611bf215546Sopenharmony_ci 2612bf215546Sopenharmony_ci return local_key; 2613bf215546Sopenharmony_ci} 2614bf215546Sopenharmony_ci 2615bf215546Sopenharmony_ci#define NO_INLINE_UNIFORMS false 2616bf215546Sopenharmony_ci 2617bf215546Sopenharmony_ci/** 2618bf215546Sopenharmony_ci * Select a shader variant according to the shader key. 2619bf215546Sopenharmony_ci * 2620bf215546Sopenharmony_ci * This uses a C++ template to compute the optimal memcmp size at compile time, which is important 2621bf215546Sopenharmony_ci * for getting inlined memcmp. The memcmp size depends on the shader key type and whether inlined 2622bf215546Sopenharmony_ci * uniforms are enabled. 2623bf215546Sopenharmony_ci */ 2624bf215546Sopenharmony_citemplate<bool INLINE_UNIFORMS = true, typename SHADER_KEY_TYPE> 2625bf215546Sopenharmony_cistatic int si_shader_select_with_key(struct si_context *sctx, struct si_shader_ctx_state *state, 2626bf215546Sopenharmony_ci const SHADER_KEY_TYPE *key) 2627bf215546Sopenharmony_ci{ 2628bf215546Sopenharmony_ci struct si_screen *sscreen = sctx->screen; 2629bf215546Sopenharmony_ci struct si_shader_selector *sel = state->cso; 2630bf215546Sopenharmony_ci struct si_shader_selector *previous_stage_sel = NULL; 2631bf215546Sopenharmony_ci struct si_shader *current = state->current; 2632bf215546Sopenharmony_ci struct si_shader *shader = NULL; 2633bf215546Sopenharmony_ci const SHADER_KEY_TYPE *zeroed_key = (SHADER_KEY_TYPE*)&zeroed; 2634bf215546Sopenharmony_ci 2635bf215546Sopenharmony_ci /* "opt" must be the last field and "inlined_uniform_values" must be the last field inside opt. 2636bf215546Sopenharmony_ci * If there is padding, insert the padding manually before opt or inside opt. 2637bf215546Sopenharmony_ci */ 2638bf215546Sopenharmony_ci STATIC_ASSERT(offsetof(SHADER_KEY_TYPE, opt) + sizeof(key->opt) == sizeof(*key)); 2639bf215546Sopenharmony_ci STATIC_ASSERT(offsetof(SHADER_KEY_TYPE, opt.inlined_uniform_values) + 2640bf215546Sopenharmony_ci sizeof(key->opt.inlined_uniform_values) == sizeof(*key)); 2641bf215546Sopenharmony_ci 2642bf215546Sopenharmony_ci const unsigned key_size_no_uniforms = sizeof(*key) - sizeof(key->opt.inlined_uniform_values); 2643bf215546Sopenharmony_ci /* Don't compare inlined_uniform_values if uniform inlining is disabled. */ 2644bf215546Sopenharmony_ci const unsigned key_size = INLINE_UNIFORMS ? sizeof(*key) : key_size_no_uniforms; 2645bf215546Sopenharmony_ci const unsigned key_opt_size = 2646bf215546Sopenharmony_ci INLINE_UNIFORMS ? sizeof(key->opt) : 2647bf215546Sopenharmony_ci sizeof(key->opt) - sizeof(key->opt.inlined_uniform_values); 2648bf215546Sopenharmony_ci 2649bf215546Sopenharmony_ci /* si_shader_select_with_key must not modify 'key' because it would affect future shaders. 2650bf215546Sopenharmony_ci * If we need to modify it for this specific shader (eg: to disable optimizations), we 2651bf215546Sopenharmony_ci * use a copy. 2652bf215546Sopenharmony_ci */ 2653bf215546Sopenharmony_ci SHADER_KEY_TYPE local_key; 2654bf215546Sopenharmony_ci 2655bf215546Sopenharmony_ci if (unlikely(sscreen->debug_flags & DBG(NO_OPT_VARIANT))) { 2656bf215546Sopenharmony_ci /* Disable shader variant optimizations. */ 2657bf215546Sopenharmony_ci key = use_local_key_copy<SHADER_KEY_TYPE>(key, &local_key, key_size); 2658bf215546Sopenharmony_ci memset(&local_key.opt, 0, key_opt_size); 2659bf215546Sopenharmony_ci } 2660bf215546Sopenharmony_ci 2661bf215546Sopenharmony_ciagain: 2662bf215546Sopenharmony_ci /* Check if we don't need to change anything. 2663bf215546Sopenharmony_ci * This path is also used for most shaders that don't need multiple 2664bf215546Sopenharmony_ci * variants, it will cost just a computation of the key and this 2665bf215546Sopenharmony_ci * test. */ 2666bf215546Sopenharmony_ci if (likely(current && memcmp(¤t->key, key, key_size) == 0)) { 2667bf215546Sopenharmony_ci if (unlikely(!util_queue_fence_is_signalled(¤t->ready))) { 2668bf215546Sopenharmony_ci if (current->is_optimized) { 2669bf215546Sopenharmony_ci key = use_local_key_copy(key, &local_key, key_size); 2670bf215546Sopenharmony_ci memset(&local_key.opt, 0, key_opt_size); 2671bf215546Sopenharmony_ci goto current_not_ready; 2672bf215546Sopenharmony_ci } 2673bf215546Sopenharmony_ci 2674bf215546Sopenharmony_ci util_queue_fence_wait(¤t->ready); 2675bf215546Sopenharmony_ci } 2676bf215546Sopenharmony_ci 2677bf215546Sopenharmony_ci return current->compilation_failed ? -1 : 0; 2678bf215546Sopenharmony_ci } 2679bf215546Sopenharmony_cicurrent_not_ready: 2680bf215546Sopenharmony_ci 2681bf215546Sopenharmony_ci /* This must be done before the mutex is locked, because async GS 2682bf215546Sopenharmony_ci * compilation calls this function too, and therefore must enter 2683bf215546Sopenharmony_ci * the mutex first. 2684bf215546Sopenharmony_ci */ 2685bf215546Sopenharmony_ci util_queue_fence_wait(&sel->ready); 2686bf215546Sopenharmony_ci 2687bf215546Sopenharmony_ci simple_mtx_lock(&sel->mutex); 2688bf215546Sopenharmony_ci 2689bf215546Sopenharmony_ci int variant_count = 0; 2690bf215546Sopenharmony_ci const int max_inline_uniforms_variants = 5; 2691bf215546Sopenharmony_ci 2692bf215546Sopenharmony_ci /* Find the shader variant. */ 2693bf215546Sopenharmony_ci const unsigned cnt = sel->variants_count; 2694bf215546Sopenharmony_ci for (unsigned i = 0; i < cnt; i++) { 2695bf215546Sopenharmony_ci const SHADER_KEY_TYPE *iter_key = (const SHADER_KEY_TYPE *)&sel->keys[i]; 2696bf215546Sopenharmony_ci 2697bf215546Sopenharmony_ci if (memcmp(iter_key, key, key_size_no_uniforms) == 0) { 2698bf215546Sopenharmony_ci struct si_shader *iter = sel->variants[i]; 2699bf215546Sopenharmony_ci 2700bf215546Sopenharmony_ci /* Check the inlined uniform values separately, and count 2701bf215546Sopenharmony_ci * the number of variants based on them. 2702bf215546Sopenharmony_ci */ 2703bf215546Sopenharmony_ci if (key->opt.inline_uniforms && 2704bf215546Sopenharmony_ci memcmp(iter_key->opt.inlined_uniform_values, 2705bf215546Sopenharmony_ci key->opt.inlined_uniform_values, 2706bf215546Sopenharmony_ci MAX_INLINABLE_UNIFORMS * 4) != 0) { 2707bf215546Sopenharmony_ci if (variant_count++ > max_inline_uniforms_variants) { 2708bf215546Sopenharmony_ci key = use_local_key_copy(key, &local_key, key_size); 2709bf215546Sopenharmony_ci /* Too many variants. Disable inlining for this shader. */ 2710bf215546Sopenharmony_ci local_key.opt.inline_uniforms = 0; 2711bf215546Sopenharmony_ci memset(local_key.opt.inlined_uniform_values, 0, MAX_INLINABLE_UNIFORMS * 4); 2712bf215546Sopenharmony_ci simple_mtx_unlock(&sel->mutex); 2713bf215546Sopenharmony_ci goto again; 2714bf215546Sopenharmony_ci } 2715bf215546Sopenharmony_ci continue; 2716bf215546Sopenharmony_ci } 2717bf215546Sopenharmony_ci 2718bf215546Sopenharmony_ci simple_mtx_unlock(&sel->mutex); 2719bf215546Sopenharmony_ci 2720bf215546Sopenharmony_ci if (unlikely(!util_queue_fence_is_signalled(&iter->ready))) { 2721bf215546Sopenharmony_ci /* If it's an optimized shader and its compilation has 2722bf215546Sopenharmony_ci * been started but isn't done, use the unoptimized 2723bf215546Sopenharmony_ci * shader so as not to cause a stall due to compilation. 2724bf215546Sopenharmony_ci */ 2725bf215546Sopenharmony_ci if (iter->is_optimized) { 2726bf215546Sopenharmony_ci key = use_local_key_copy(key, &local_key, key_size); 2727bf215546Sopenharmony_ci memset(&local_key.opt, 0, key_opt_size); 2728bf215546Sopenharmony_ci goto again; 2729bf215546Sopenharmony_ci } 2730bf215546Sopenharmony_ci 2731bf215546Sopenharmony_ci util_queue_fence_wait(&iter->ready); 2732bf215546Sopenharmony_ci } 2733bf215546Sopenharmony_ci 2734bf215546Sopenharmony_ci if (iter->compilation_failed) { 2735bf215546Sopenharmony_ci return -1; /* skip the draw call */ 2736bf215546Sopenharmony_ci } 2737bf215546Sopenharmony_ci 2738bf215546Sopenharmony_ci state->current = sel->variants[i]; 2739bf215546Sopenharmony_ci return 0; 2740bf215546Sopenharmony_ci } 2741bf215546Sopenharmony_ci } 2742bf215546Sopenharmony_ci 2743bf215546Sopenharmony_ci /* Build a new shader. */ 2744bf215546Sopenharmony_ci shader = CALLOC_STRUCT(si_shader); 2745bf215546Sopenharmony_ci if (!shader) { 2746bf215546Sopenharmony_ci simple_mtx_unlock(&sel->mutex); 2747bf215546Sopenharmony_ci return -ENOMEM; 2748bf215546Sopenharmony_ci } 2749bf215546Sopenharmony_ci 2750bf215546Sopenharmony_ci util_queue_fence_init(&shader->ready); 2751bf215546Sopenharmony_ci 2752bf215546Sopenharmony_ci if (!sctx->compiler.passes) 2753bf215546Sopenharmony_ci si_init_compiler(sctx->screen, &sctx->compiler); 2754bf215546Sopenharmony_ci 2755bf215546Sopenharmony_ci shader->selector = sel; 2756bf215546Sopenharmony_ci *((SHADER_KEY_TYPE*)&shader->key) = *key; 2757bf215546Sopenharmony_ci shader->wave_size = si_determine_wave_size(sscreen, shader); 2758bf215546Sopenharmony_ci shader->compiler_ctx_state.compiler = &sctx->compiler; 2759bf215546Sopenharmony_ci shader->compiler_ctx_state.debug = sctx->debug; 2760bf215546Sopenharmony_ci shader->compiler_ctx_state.is_debug_context = sctx->is_debug; 2761bf215546Sopenharmony_ci 2762bf215546Sopenharmony_ci /* If this is a merged shader, get the first shader's selector. */ 2763bf215546Sopenharmony_ci if (sscreen->info.gfx_level >= GFX9) { 2764bf215546Sopenharmony_ci if (sel->stage == MESA_SHADER_TESS_CTRL) 2765bf215546Sopenharmony_ci previous_stage_sel = ((struct si_shader_key_ge*)key)->part.tcs.ls; 2766bf215546Sopenharmony_ci else if (sel->stage == MESA_SHADER_GEOMETRY) 2767bf215546Sopenharmony_ci previous_stage_sel = ((struct si_shader_key_ge*)key)->part.gs.es; 2768bf215546Sopenharmony_ci 2769bf215546Sopenharmony_ci /* We need to wait for the previous shader. */ 2770bf215546Sopenharmony_ci if (previous_stage_sel) 2771bf215546Sopenharmony_ci util_queue_fence_wait(&previous_stage_sel->ready); 2772bf215546Sopenharmony_ci } 2773bf215546Sopenharmony_ci 2774bf215546Sopenharmony_ci bool is_pure_monolithic = 2775bf215546Sopenharmony_ci sscreen->use_monolithic_shaders || memcmp(&key->mono, &zeroed_key->mono, sizeof(key->mono)) != 0; 2776bf215546Sopenharmony_ci 2777bf215546Sopenharmony_ci /* Compile the main shader part if it doesn't exist. This can happen 2778bf215546Sopenharmony_ci * if the initial guess was wrong. 2779bf215546Sopenharmony_ci */ 2780bf215546Sopenharmony_ci if (!is_pure_monolithic) { 2781bf215546Sopenharmony_ci bool ok = true; 2782bf215546Sopenharmony_ci 2783bf215546Sopenharmony_ci /* Make sure the main shader part is present. This is needed 2784bf215546Sopenharmony_ci * for shaders that can be compiled as VS, LS, or ES, and only 2785bf215546Sopenharmony_ci * one of them is compiled at creation. 2786bf215546Sopenharmony_ci * 2787bf215546Sopenharmony_ci * It is also needed for GS, which can be compiled as non-NGG 2788bf215546Sopenharmony_ci * and NGG. 2789bf215546Sopenharmony_ci * 2790bf215546Sopenharmony_ci * For merged shaders, check that the starting shader's main 2791bf215546Sopenharmony_ci * part is present. 2792bf215546Sopenharmony_ci */ 2793bf215546Sopenharmony_ci if (previous_stage_sel) { 2794bf215546Sopenharmony_ci union si_shader_key shader1_key = zeroed; 2795bf215546Sopenharmony_ci 2796bf215546Sopenharmony_ci if (sel->stage == MESA_SHADER_TESS_CTRL) { 2797bf215546Sopenharmony_ci shader1_key.ge.as_ls = 1; 2798bf215546Sopenharmony_ci } else if (sel->stage == MESA_SHADER_GEOMETRY) { 2799bf215546Sopenharmony_ci shader1_key.ge.as_es = 1; 2800bf215546Sopenharmony_ci shader1_key.ge.as_ngg = ((struct si_shader_key_ge*)key)->as_ngg; /* for Wave32 vs Wave64 */ 2801bf215546Sopenharmony_ci } else { 2802bf215546Sopenharmony_ci assert(0); 2803bf215546Sopenharmony_ci } 2804bf215546Sopenharmony_ci 2805bf215546Sopenharmony_ci simple_mtx_lock(&previous_stage_sel->mutex); 2806bf215546Sopenharmony_ci ok = si_check_missing_main_part(sscreen, previous_stage_sel, &shader->compiler_ctx_state, 2807bf215546Sopenharmony_ci &shader1_key); 2808bf215546Sopenharmony_ci simple_mtx_unlock(&previous_stage_sel->mutex); 2809bf215546Sopenharmony_ci } 2810bf215546Sopenharmony_ci 2811bf215546Sopenharmony_ci if (ok) { 2812bf215546Sopenharmony_ci ok = si_check_missing_main_part(sscreen, sel, &shader->compiler_ctx_state, 2813bf215546Sopenharmony_ci (union si_shader_key*)key); 2814bf215546Sopenharmony_ci } 2815bf215546Sopenharmony_ci 2816bf215546Sopenharmony_ci if (!ok) { 2817bf215546Sopenharmony_ci FREE(shader); 2818bf215546Sopenharmony_ci simple_mtx_unlock(&sel->mutex); 2819bf215546Sopenharmony_ci return -ENOMEM; /* skip the draw call */ 2820bf215546Sopenharmony_ci } 2821bf215546Sopenharmony_ci } 2822bf215546Sopenharmony_ci 2823bf215546Sopenharmony_ci if (sel->variants_count == sel->variants_max_count) { 2824bf215546Sopenharmony_ci sel->variants_max_count += 2; 2825bf215546Sopenharmony_ci sel->variants = (struct si_shader**) 2826bf215546Sopenharmony_ci realloc(sel->variants, sel->variants_max_count * sizeof(struct si_shader*)); 2827bf215546Sopenharmony_ci sel->keys = (union si_shader_key*) 2828bf215546Sopenharmony_ci realloc(sel->keys, sel->variants_max_count * sizeof(union si_shader_key)); 2829bf215546Sopenharmony_ci } 2830bf215546Sopenharmony_ci 2831bf215546Sopenharmony_ci /* Keep the reference to the 1st shader of merged shaders, so that 2832bf215546Sopenharmony_ci * Gallium can't destroy it before we destroy the 2nd shader. 2833bf215546Sopenharmony_ci * 2834bf215546Sopenharmony_ci * Set sctx = NULL, because it's unused if we're not releasing 2835bf215546Sopenharmony_ci * the shader, and we don't have any sctx here. 2836bf215546Sopenharmony_ci */ 2837bf215546Sopenharmony_ci si_shader_selector_reference(NULL, &shader->previous_stage_sel, previous_stage_sel); 2838bf215546Sopenharmony_ci 2839bf215546Sopenharmony_ci /* Monolithic-only shaders don't make a distinction between optimized 2840bf215546Sopenharmony_ci * and unoptimized. */ 2841bf215546Sopenharmony_ci shader->is_monolithic = 2842bf215546Sopenharmony_ci is_pure_monolithic || memcmp(&key->opt, &zeroed_key->opt, key_opt_size) != 0; 2843bf215546Sopenharmony_ci 2844bf215546Sopenharmony_ci shader->is_optimized = !is_pure_monolithic && 2845bf215546Sopenharmony_ci memcmp(&key->opt, &zeroed_key->opt, key_opt_size) != 0; 2846bf215546Sopenharmony_ci 2847bf215546Sopenharmony_ci /* If it's an optimized shader, compile it asynchronously. */ 2848bf215546Sopenharmony_ci if (shader->is_optimized) { 2849bf215546Sopenharmony_ci /* Compile it asynchronously. */ 2850bf215546Sopenharmony_ci util_queue_add_job(&sscreen->shader_compiler_queue_low_priority, shader, &shader->ready, 2851bf215546Sopenharmony_ci si_build_shader_variant_low_priority, NULL, 0); 2852bf215546Sopenharmony_ci 2853bf215546Sopenharmony_ci /* Add only after the ready fence was reset, to guard against a 2854bf215546Sopenharmony_ci * race with si_bind_XX_shader. */ 2855bf215546Sopenharmony_ci sel->variants[sel->variants_count] = shader; 2856bf215546Sopenharmony_ci sel->keys[sel->variants_count] = shader->key; 2857bf215546Sopenharmony_ci sel->variants_count++; 2858bf215546Sopenharmony_ci 2859bf215546Sopenharmony_ci /* Use the default (unoptimized) shader for now. */ 2860bf215546Sopenharmony_ci key = use_local_key_copy(key, &local_key, key_size); 2861bf215546Sopenharmony_ci memset(&local_key.opt, 0, key_opt_size); 2862bf215546Sopenharmony_ci simple_mtx_unlock(&sel->mutex); 2863bf215546Sopenharmony_ci 2864bf215546Sopenharmony_ci if (sscreen->options.sync_compile) 2865bf215546Sopenharmony_ci util_queue_fence_wait(&shader->ready); 2866bf215546Sopenharmony_ci 2867bf215546Sopenharmony_ci goto again; 2868bf215546Sopenharmony_ci } 2869bf215546Sopenharmony_ci 2870bf215546Sopenharmony_ci /* Reset the fence before adding to the variant list. */ 2871bf215546Sopenharmony_ci util_queue_fence_reset(&shader->ready); 2872bf215546Sopenharmony_ci 2873bf215546Sopenharmony_ci sel->variants[sel->variants_count] = shader; 2874bf215546Sopenharmony_ci sel->keys[sel->variants_count] = shader->key; 2875bf215546Sopenharmony_ci sel->variants_count++; 2876bf215546Sopenharmony_ci 2877bf215546Sopenharmony_ci simple_mtx_unlock(&sel->mutex); 2878bf215546Sopenharmony_ci 2879bf215546Sopenharmony_ci assert(!shader->is_optimized); 2880bf215546Sopenharmony_ci si_build_shader_variant(shader, -1, false); 2881bf215546Sopenharmony_ci 2882bf215546Sopenharmony_ci util_queue_fence_signal(&shader->ready); 2883bf215546Sopenharmony_ci 2884bf215546Sopenharmony_ci if (!shader->compilation_failed) 2885bf215546Sopenharmony_ci state->current = shader; 2886bf215546Sopenharmony_ci 2887bf215546Sopenharmony_ci return shader->compilation_failed ? -1 : 0; 2888bf215546Sopenharmony_ci} 2889bf215546Sopenharmony_ci 2890bf215546Sopenharmony_ciint si_shader_select(struct pipe_context *ctx, struct si_shader_ctx_state *state) 2891bf215546Sopenharmony_ci{ 2892bf215546Sopenharmony_ci struct si_context *sctx = (struct si_context *)ctx; 2893bf215546Sopenharmony_ci 2894bf215546Sopenharmony_ci si_shader_selector_key(ctx, state->cso, &state->key); 2895bf215546Sopenharmony_ci 2896bf215546Sopenharmony_ci if (state->cso->stage == MESA_SHADER_FRAGMENT) { 2897bf215546Sopenharmony_ci if (state->key.ps.opt.inline_uniforms) 2898bf215546Sopenharmony_ci return si_shader_select_with_key(sctx, state, &state->key.ps); 2899bf215546Sopenharmony_ci else 2900bf215546Sopenharmony_ci return si_shader_select_with_key<NO_INLINE_UNIFORMS>(sctx, state, &state->key.ps); 2901bf215546Sopenharmony_ci } else { 2902bf215546Sopenharmony_ci if (state->key.ge.opt.inline_uniforms) { 2903bf215546Sopenharmony_ci return si_shader_select_with_key(sctx, state, &state->key.ge); 2904bf215546Sopenharmony_ci } else { 2905bf215546Sopenharmony_ci return si_shader_select_with_key<NO_INLINE_UNIFORMS>(sctx, state, &state->key.ge); 2906bf215546Sopenharmony_ci } 2907bf215546Sopenharmony_ci } 2908bf215546Sopenharmony_ci} 2909bf215546Sopenharmony_ci 2910bf215546Sopenharmony_cistatic void si_parse_next_shader_property(const struct si_shader_info *info, 2911bf215546Sopenharmony_ci union si_shader_key *key) 2912bf215546Sopenharmony_ci{ 2913bf215546Sopenharmony_ci gl_shader_stage next_shader = info->base.next_stage; 2914bf215546Sopenharmony_ci 2915bf215546Sopenharmony_ci switch (info->base.stage) { 2916bf215546Sopenharmony_ci case MESA_SHADER_VERTEX: 2917bf215546Sopenharmony_ci switch (next_shader) { 2918bf215546Sopenharmony_ci case MESA_SHADER_GEOMETRY: 2919bf215546Sopenharmony_ci key->ge.as_es = 1; 2920bf215546Sopenharmony_ci break; 2921bf215546Sopenharmony_ci case MESA_SHADER_TESS_CTRL: 2922bf215546Sopenharmony_ci case MESA_SHADER_TESS_EVAL: 2923bf215546Sopenharmony_ci key->ge.as_ls = 1; 2924bf215546Sopenharmony_ci break; 2925bf215546Sopenharmony_ci default: 2926bf215546Sopenharmony_ci /* If POSITION isn't written, it can only be a HW VS 2927bf215546Sopenharmony_ci * if streamout is used. If streamout isn't used, 2928bf215546Sopenharmony_ci * assume that it's a HW LS. (the next shader is TCS) 2929bf215546Sopenharmony_ci * This heuristic is needed for separate shader objects. 2930bf215546Sopenharmony_ci */ 2931bf215546Sopenharmony_ci if (!info->writes_position && !info->enabled_streamout_buffer_mask) 2932bf215546Sopenharmony_ci key->ge.as_ls = 1; 2933bf215546Sopenharmony_ci } 2934bf215546Sopenharmony_ci break; 2935bf215546Sopenharmony_ci 2936bf215546Sopenharmony_ci case MESA_SHADER_TESS_EVAL: 2937bf215546Sopenharmony_ci if (next_shader == MESA_SHADER_GEOMETRY || !info->writes_position) 2938bf215546Sopenharmony_ci key->ge.as_es = 1; 2939bf215546Sopenharmony_ci break; 2940bf215546Sopenharmony_ci 2941bf215546Sopenharmony_ci default:; 2942bf215546Sopenharmony_ci } 2943bf215546Sopenharmony_ci} 2944bf215546Sopenharmony_ci 2945bf215546Sopenharmony_ci/** 2946bf215546Sopenharmony_ci * Compile the main shader part or the monolithic shader as part of 2947bf215546Sopenharmony_ci * si_shader_selector initialization. Since it can be done asynchronously, 2948bf215546Sopenharmony_ci * there is no way to report compile failures to applications. 2949bf215546Sopenharmony_ci */ 2950bf215546Sopenharmony_cistatic void si_init_shader_selector_async(void *job, void *gdata, int thread_index) 2951bf215546Sopenharmony_ci{ 2952bf215546Sopenharmony_ci struct si_shader_selector *sel = (struct si_shader_selector *)job; 2953bf215546Sopenharmony_ci struct si_screen *sscreen = sel->screen; 2954bf215546Sopenharmony_ci struct ac_llvm_compiler *compiler; 2955bf215546Sopenharmony_ci struct util_debug_callback *debug = &sel->compiler_ctx_state.debug; 2956bf215546Sopenharmony_ci 2957bf215546Sopenharmony_ci assert(!debug->debug_message || debug->async); 2958bf215546Sopenharmony_ci assert(thread_index >= 0); 2959bf215546Sopenharmony_ci assert(thread_index < (int)ARRAY_SIZE(sscreen->compiler)); 2960bf215546Sopenharmony_ci compiler = &sscreen->compiler[thread_index]; 2961bf215546Sopenharmony_ci 2962bf215546Sopenharmony_ci if (!compiler->passes) 2963bf215546Sopenharmony_ci si_init_compiler(sscreen, compiler); 2964bf215546Sopenharmony_ci 2965bf215546Sopenharmony_ci /* Serialize NIR to save memory. Monolithic shader variants 2966bf215546Sopenharmony_ci * have to deserialize NIR before compilation. 2967bf215546Sopenharmony_ci */ 2968bf215546Sopenharmony_ci if (sel->nir) { 2969bf215546Sopenharmony_ci struct blob blob; 2970bf215546Sopenharmony_ci size_t size; 2971bf215546Sopenharmony_ci 2972bf215546Sopenharmony_ci blob_init(&blob); 2973bf215546Sopenharmony_ci /* true = remove optional debugging data to increase 2974bf215546Sopenharmony_ci * the likehood of getting more shader cache hits. 2975bf215546Sopenharmony_ci * It also drops variable names, so we'll save more memory. 2976bf215546Sopenharmony_ci * If NIR debug prints are used we don't strip to get more 2977bf215546Sopenharmony_ci * useful logs. 2978bf215546Sopenharmony_ci */ 2979bf215546Sopenharmony_ci nir_serialize(&blob, sel->nir, NIR_DEBUG(PRINT) == 0); 2980bf215546Sopenharmony_ci blob_finish_get_buffer(&blob, &sel->nir_binary, &size); 2981bf215546Sopenharmony_ci sel->nir_size = size; 2982bf215546Sopenharmony_ci } 2983bf215546Sopenharmony_ci 2984bf215546Sopenharmony_ci /* Compile the main shader part for use with a prolog and/or epilog. 2985bf215546Sopenharmony_ci * If this fails, the driver will try to compile a monolithic shader 2986bf215546Sopenharmony_ci * on demand. 2987bf215546Sopenharmony_ci */ 2988bf215546Sopenharmony_ci if (!sscreen->use_monolithic_shaders) { 2989bf215546Sopenharmony_ci struct si_shader *shader = CALLOC_STRUCT(si_shader); 2990bf215546Sopenharmony_ci unsigned char ir_sha1_cache_key[20]; 2991bf215546Sopenharmony_ci 2992bf215546Sopenharmony_ci if (!shader) { 2993bf215546Sopenharmony_ci fprintf(stderr, "radeonsi: can't allocate a main shader part\n"); 2994bf215546Sopenharmony_ci return; 2995bf215546Sopenharmony_ci } 2996bf215546Sopenharmony_ci 2997bf215546Sopenharmony_ci /* We can leave the fence signaled because use of the default 2998bf215546Sopenharmony_ci * main part is guarded by the selector's ready fence. */ 2999bf215546Sopenharmony_ci util_queue_fence_init(&shader->ready); 3000bf215546Sopenharmony_ci 3001bf215546Sopenharmony_ci shader->selector = sel; 3002bf215546Sopenharmony_ci shader->is_monolithic = false; 3003bf215546Sopenharmony_ci si_parse_next_shader_property(&sel->info, &shader->key); 3004bf215546Sopenharmony_ci 3005bf215546Sopenharmony_ci if (sel->stage <= MESA_SHADER_GEOMETRY && 3006bf215546Sopenharmony_ci sscreen->use_ngg && (!sel->info.enabled_streamout_buffer_mask || 3007bf215546Sopenharmony_ci sscreen->use_ngg_streamout) && 3008bf215546Sopenharmony_ci ((sel->stage == MESA_SHADER_VERTEX && !shader->key.ge.as_ls) || 3009bf215546Sopenharmony_ci sel->stage == MESA_SHADER_TESS_EVAL || sel->stage == MESA_SHADER_GEOMETRY)) 3010bf215546Sopenharmony_ci shader->key.ge.as_ngg = 1; 3011bf215546Sopenharmony_ci 3012bf215546Sopenharmony_ci shader->wave_size = si_determine_wave_size(sscreen, shader); 3013bf215546Sopenharmony_ci 3014bf215546Sopenharmony_ci if (sel->nir) { 3015bf215546Sopenharmony_ci if (sel->stage <= MESA_SHADER_GEOMETRY) { 3016bf215546Sopenharmony_ci si_get_ir_cache_key(sel, shader->key.ge.as_ngg, shader->key.ge.as_es, 3017bf215546Sopenharmony_ci shader->wave_size, ir_sha1_cache_key); 3018bf215546Sopenharmony_ci } else { 3019bf215546Sopenharmony_ci si_get_ir_cache_key(sel, false, false, shader->wave_size, ir_sha1_cache_key); 3020bf215546Sopenharmony_ci } 3021bf215546Sopenharmony_ci } 3022bf215546Sopenharmony_ci 3023bf215546Sopenharmony_ci /* Try to load the shader from the shader cache. */ 3024bf215546Sopenharmony_ci simple_mtx_lock(&sscreen->shader_cache_mutex); 3025bf215546Sopenharmony_ci 3026bf215546Sopenharmony_ci if (si_shader_cache_load_shader(sscreen, ir_sha1_cache_key, shader)) { 3027bf215546Sopenharmony_ci simple_mtx_unlock(&sscreen->shader_cache_mutex); 3028bf215546Sopenharmony_ci si_shader_dump_stats_for_shader_db(sscreen, shader, debug); 3029bf215546Sopenharmony_ci } else { 3030bf215546Sopenharmony_ci simple_mtx_unlock(&sscreen->shader_cache_mutex); 3031bf215546Sopenharmony_ci 3032bf215546Sopenharmony_ci /* Compile the shader if it hasn't been loaded from the cache. */ 3033bf215546Sopenharmony_ci if (!si_compile_shader(sscreen, compiler, shader, debug)) { 3034bf215546Sopenharmony_ci FREE(shader); 3035bf215546Sopenharmony_ci fprintf(stderr, "radeonsi: can't compile a main shader part\n"); 3036bf215546Sopenharmony_ci return; 3037bf215546Sopenharmony_ci } 3038bf215546Sopenharmony_ci 3039bf215546Sopenharmony_ci simple_mtx_lock(&sscreen->shader_cache_mutex); 3040bf215546Sopenharmony_ci si_shader_cache_insert_shader(sscreen, ir_sha1_cache_key, shader, true); 3041bf215546Sopenharmony_ci simple_mtx_unlock(&sscreen->shader_cache_mutex); 3042bf215546Sopenharmony_ci } 3043bf215546Sopenharmony_ci 3044bf215546Sopenharmony_ci *si_get_main_shader_part(sel, &shader->key) = shader; 3045bf215546Sopenharmony_ci 3046bf215546Sopenharmony_ci /* Unset "outputs_written" flags for outputs converted to 3047bf215546Sopenharmony_ci * DEFAULT_VAL, so that later inter-shader optimizations don't 3048bf215546Sopenharmony_ci * try to eliminate outputs that don't exist in the final 3049bf215546Sopenharmony_ci * shader. 3050bf215546Sopenharmony_ci * 3051bf215546Sopenharmony_ci * This is only done if non-monolithic shaders are enabled. 3052bf215546Sopenharmony_ci */ 3053bf215546Sopenharmony_ci if ((sel->stage == MESA_SHADER_VERTEX || 3054bf215546Sopenharmony_ci sel->stage == MESA_SHADER_TESS_EVAL || 3055bf215546Sopenharmony_ci sel->stage == MESA_SHADER_GEOMETRY) && 3056bf215546Sopenharmony_ci !shader->key.ge.as_ls && !shader->key.ge.as_es) { 3057bf215546Sopenharmony_ci unsigned i; 3058bf215546Sopenharmony_ci 3059bf215546Sopenharmony_ci for (i = 0; i < sel->info.num_outputs; i++) { 3060bf215546Sopenharmony_ci unsigned semantic = sel->info.output_semantic[i]; 3061bf215546Sopenharmony_ci unsigned ps_input_cntl = shader->info.vs_output_ps_input_cntl[semantic]; 3062bf215546Sopenharmony_ci 3063bf215546Sopenharmony_ci /* OFFSET=0x20 means DEFAULT_VAL, which means VS doesn't export it. */ 3064bf215546Sopenharmony_ci if (G_028644_OFFSET(ps_input_cntl) != 0x20) 3065bf215546Sopenharmony_ci continue; 3066bf215546Sopenharmony_ci 3067bf215546Sopenharmony_ci unsigned id; 3068bf215546Sopenharmony_ci 3069bf215546Sopenharmony_ci /* Remove the output from the mask. */ 3070bf215546Sopenharmony_ci if ((semantic <= VARYING_SLOT_VAR31 || semantic >= VARYING_SLOT_VAR0_16BIT) && 3071bf215546Sopenharmony_ci semantic != VARYING_SLOT_POS && 3072bf215546Sopenharmony_ci semantic != VARYING_SLOT_PSIZ && 3073bf215546Sopenharmony_ci semantic != VARYING_SLOT_CLIP_VERTEX && 3074bf215546Sopenharmony_ci semantic != VARYING_SLOT_EDGE) { 3075bf215546Sopenharmony_ci id = si_shader_io_get_unique_index(semantic, true); 3076bf215546Sopenharmony_ci sel->info.outputs_written_before_ps &= ~(1ull << id); 3077bf215546Sopenharmony_ci } 3078bf215546Sopenharmony_ci } 3079bf215546Sopenharmony_ci } 3080bf215546Sopenharmony_ci } 3081bf215546Sopenharmony_ci 3082bf215546Sopenharmony_ci /* Free NIR. We only keep serialized NIR after this point. */ 3083bf215546Sopenharmony_ci if (sel->nir) { 3084bf215546Sopenharmony_ci ralloc_free(sel->nir); 3085bf215546Sopenharmony_ci sel->nir = NULL; 3086bf215546Sopenharmony_ci } 3087bf215546Sopenharmony_ci} 3088bf215546Sopenharmony_ci 3089bf215546Sopenharmony_civoid si_schedule_initial_compile(struct si_context *sctx, gl_shader_stage stage, 3090bf215546Sopenharmony_ci struct util_queue_fence *ready_fence, 3091bf215546Sopenharmony_ci struct si_compiler_ctx_state *compiler_ctx_state, void *job, 3092bf215546Sopenharmony_ci util_queue_execute_func execute) 3093bf215546Sopenharmony_ci{ 3094bf215546Sopenharmony_ci util_queue_fence_init(ready_fence); 3095bf215546Sopenharmony_ci 3096bf215546Sopenharmony_ci struct util_async_debug_callback async_debug; 3097bf215546Sopenharmony_ci bool debug = (sctx->debug.debug_message && !sctx->debug.async) || sctx->is_debug || 3098bf215546Sopenharmony_ci si_can_dump_shader(sctx->screen, stage); 3099bf215546Sopenharmony_ci 3100bf215546Sopenharmony_ci if (debug) { 3101bf215546Sopenharmony_ci u_async_debug_init(&async_debug); 3102bf215546Sopenharmony_ci compiler_ctx_state->debug = async_debug.base; 3103bf215546Sopenharmony_ci } 3104bf215546Sopenharmony_ci 3105bf215546Sopenharmony_ci util_queue_add_job(&sctx->screen->shader_compiler_queue, job, ready_fence, execute, NULL, 0); 3106bf215546Sopenharmony_ci 3107bf215546Sopenharmony_ci if (debug) { 3108bf215546Sopenharmony_ci util_queue_fence_wait(ready_fence); 3109bf215546Sopenharmony_ci u_async_debug_drain(&async_debug, &sctx->debug); 3110bf215546Sopenharmony_ci u_async_debug_cleanup(&async_debug); 3111bf215546Sopenharmony_ci } 3112bf215546Sopenharmony_ci 3113bf215546Sopenharmony_ci if (sctx->screen->options.sync_compile) 3114bf215546Sopenharmony_ci util_queue_fence_wait(ready_fence); 3115bf215546Sopenharmony_ci} 3116bf215546Sopenharmony_ci 3117bf215546Sopenharmony_ci/* Return descriptor slot usage masks from the given shader info. */ 3118bf215546Sopenharmony_civoid si_get_active_slot_masks(struct si_screen *sscreen, const struct si_shader_info *info, 3119bf215546Sopenharmony_ci uint64_t *const_and_shader_buffers, uint64_t *samplers_and_images) 3120bf215546Sopenharmony_ci{ 3121bf215546Sopenharmony_ci unsigned start, num_shaderbufs, num_constbufs, num_images, num_msaa_images, num_samplers; 3122bf215546Sopenharmony_ci 3123bf215546Sopenharmony_ci num_shaderbufs = info->base.num_ssbos; 3124bf215546Sopenharmony_ci num_constbufs = info->base.num_ubos; 3125bf215546Sopenharmony_ci /* two 8-byte images share one 16-byte slot */ 3126bf215546Sopenharmony_ci num_images = align(info->base.num_images, 2); 3127bf215546Sopenharmony_ci num_msaa_images = align(BITSET_LAST_BIT(info->base.msaa_images), 2); 3128bf215546Sopenharmony_ci num_samplers = BITSET_LAST_BIT(info->base.textures_used); 3129bf215546Sopenharmony_ci 3130bf215546Sopenharmony_ci /* The layout is: sb[last] ... sb[0], cb[0] ... cb[last] */ 3131bf215546Sopenharmony_ci start = si_get_shaderbuf_slot(num_shaderbufs - 1); 3132bf215546Sopenharmony_ci *const_and_shader_buffers = u_bit_consecutive64(start, num_shaderbufs + num_constbufs); 3133bf215546Sopenharmony_ci 3134bf215546Sopenharmony_ci /* The layout is: 3135bf215546Sopenharmony_ci * - fmask[last] ... fmask[0] go to [15-last .. 15] 3136bf215546Sopenharmony_ci * - image[last] ... image[0] go to [31-last .. 31] 3137bf215546Sopenharmony_ci * - sampler[0] ... sampler[last] go to [32 .. 32+last*2] 3138bf215546Sopenharmony_ci * 3139bf215546Sopenharmony_ci * FMASKs for images are placed separately, because MSAA images are rare, 3140bf215546Sopenharmony_ci * and so we can benefit from a better cache hit rate if we keep image 3141bf215546Sopenharmony_ci * descriptors together. 3142bf215546Sopenharmony_ci */ 3143bf215546Sopenharmony_ci if (sscreen->info.gfx_level < GFX11 && num_msaa_images) 3144bf215546Sopenharmony_ci num_images = SI_NUM_IMAGES + num_msaa_images; /* add FMASK descriptors */ 3145bf215546Sopenharmony_ci 3146bf215546Sopenharmony_ci start = si_get_image_slot(num_images - 1) / 2; 3147bf215546Sopenharmony_ci *samplers_and_images = u_bit_consecutive64(start, num_images / 2 + num_samplers); 3148bf215546Sopenharmony_ci} 3149bf215546Sopenharmony_ci 3150bf215546Sopenharmony_cistatic void *si_create_shader_selector(struct pipe_context *ctx, 3151bf215546Sopenharmony_ci const struct pipe_shader_state *state) 3152bf215546Sopenharmony_ci{ 3153bf215546Sopenharmony_ci struct si_screen *sscreen = (struct si_screen *)ctx->screen; 3154bf215546Sopenharmony_ci struct si_context *sctx = (struct si_context *)ctx; 3155bf215546Sopenharmony_ci struct si_shader_selector *sel = CALLOC_STRUCT(si_shader_selector); 3156bf215546Sopenharmony_ci 3157bf215546Sopenharmony_ci if (!sel) 3158bf215546Sopenharmony_ci return NULL; 3159bf215546Sopenharmony_ci 3160bf215546Sopenharmony_ci sel->screen = sscreen; 3161bf215546Sopenharmony_ci sel->compiler_ctx_state.debug = sctx->debug; 3162bf215546Sopenharmony_ci sel->compiler_ctx_state.is_debug_context = sctx->is_debug; 3163bf215546Sopenharmony_ci sel->variants_max_count = 2; 3164bf215546Sopenharmony_ci sel->keys = (union si_shader_key *) 3165bf215546Sopenharmony_ci realloc(NULL, sel->variants_max_count * sizeof(union si_shader_key)); 3166bf215546Sopenharmony_ci sel->variants = (struct si_shader **) 3167bf215546Sopenharmony_ci realloc(NULL, sel->variants_max_count * sizeof(struct si_shader *)); 3168bf215546Sopenharmony_ci 3169bf215546Sopenharmony_ci if (state->type == PIPE_SHADER_IR_TGSI) { 3170bf215546Sopenharmony_ci sel->nir = tgsi_to_nir(state->tokens, ctx->screen, true); 3171bf215546Sopenharmony_ci } else { 3172bf215546Sopenharmony_ci assert(state->type == PIPE_SHADER_IR_NIR); 3173bf215546Sopenharmony_ci sel->nir = (nir_shader*)state->ir.nir; 3174bf215546Sopenharmony_ci } 3175bf215546Sopenharmony_ci 3176bf215546Sopenharmony_ci si_nir_scan_shader(sscreen, sel->nir, &sel->info); 3177bf215546Sopenharmony_ci 3178bf215546Sopenharmony_ci sel->stage = sel->nir->info.stage; 3179bf215546Sopenharmony_ci const enum pipe_shader_type type = pipe_shader_type_from_mesa(sel->stage); 3180bf215546Sopenharmony_ci sel->pipe_shader_type = type; 3181bf215546Sopenharmony_ci sel->const_and_shader_buf_descriptors_index = 3182bf215546Sopenharmony_ci si_const_and_shader_buffer_descriptors_idx(type); 3183bf215546Sopenharmony_ci sel->sampler_and_images_descriptors_index = 3184bf215546Sopenharmony_ci si_sampler_and_image_descriptors_idx(type); 3185bf215546Sopenharmony_ci 3186bf215546Sopenharmony_ci p_atomic_inc(&sscreen->num_shaders_created); 3187bf215546Sopenharmony_ci si_get_active_slot_masks(sscreen, &sel->info, &sel->active_const_and_shader_buffers, 3188bf215546Sopenharmony_ci &sel->active_samplers_and_images); 3189bf215546Sopenharmony_ci 3190bf215546Sopenharmony_ci switch (sel->stage) { 3191bf215546Sopenharmony_ci case MESA_SHADER_GEOMETRY: 3192bf215546Sopenharmony_ci /* Only possibilities: POINTS, LINE_STRIP, TRIANGLES */ 3193bf215546Sopenharmony_ci sel->rast_prim = (enum pipe_prim_type)sel->info.base.gs.output_primitive; 3194bf215546Sopenharmony_ci if (util_rast_prim_is_triangles(sel->rast_prim)) 3195bf215546Sopenharmony_ci sel->rast_prim = PIPE_PRIM_TRIANGLES; 3196bf215546Sopenharmony_ci 3197bf215546Sopenharmony_ci /* EN_MAX_VERT_OUT_PER_GS_INSTANCE does not work with tesselation so 3198bf215546Sopenharmony_ci * we can't split workgroups. Disable ngg if any of the following conditions is true: 3199bf215546Sopenharmony_ci * - num_invocations * gs.vertices_out > 256 3200bf215546Sopenharmony_ci * - LDS usage is too high 3201bf215546Sopenharmony_ci */ 3202bf215546Sopenharmony_ci sel->tess_turns_off_ngg = sscreen->info.gfx_level >= GFX10 && 3203bf215546Sopenharmony_ci sscreen->info.gfx_level <= GFX10_3 && 3204bf215546Sopenharmony_ci (sel->info.base.gs.invocations * sel->info.base.gs.vertices_out > 256 || 3205bf215546Sopenharmony_ci sel->info.base.gs.invocations * sel->info.base.gs.vertices_out * 3206bf215546Sopenharmony_ci (sel->info.num_outputs * 4 + 1) > 6500 /* max dw per GS primitive */); 3207bf215546Sopenharmony_ci break; 3208bf215546Sopenharmony_ci 3209bf215546Sopenharmony_ci case MESA_SHADER_VERTEX: 3210bf215546Sopenharmony_ci case MESA_SHADER_TESS_EVAL: 3211bf215546Sopenharmony_ci if (sel->stage == MESA_SHADER_TESS_EVAL) { 3212bf215546Sopenharmony_ci if (sel->info.base.tess.point_mode) 3213bf215546Sopenharmony_ci sel->rast_prim = PIPE_PRIM_POINTS; 3214bf215546Sopenharmony_ci else if (sel->info.base.tess._primitive_mode == TESS_PRIMITIVE_ISOLINES) 3215bf215546Sopenharmony_ci sel->rast_prim = PIPE_PRIM_LINE_STRIP; 3216bf215546Sopenharmony_ci else 3217bf215546Sopenharmony_ci sel->rast_prim = PIPE_PRIM_TRIANGLES; 3218bf215546Sopenharmony_ci } else { 3219bf215546Sopenharmony_ci sel->rast_prim = PIPE_PRIM_TRIANGLES; 3220bf215546Sopenharmony_ci } 3221bf215546Sopenharmony_ci break; 3222bf215546Sopenharmony_ci default:; 3223bf215546Sopenharmony_ci } 3224bf215546Sopenharmony_ci 3225bf215546Sopenharmony_ci bool ngg_culling_allowed = 3226bf215546Sopenharmony_ci sscreen->info.gfx_level >= GFX10 && 3227bf215546Sopenharmony_ci sscreen->use_ngg_culling && 3228bf215546Sopenharmony_ci sel->info.writes_position && 3229bf215546Sopenharmony_ci !sel->info.writes_viewport_index && /* cull only against viewport 0 */ 3230bf215546Sopenharmony_ci !sel->info.base.writes_memory && 3231bf215546Sopenharmony_ci /* NGG GS supports culling with streamout because it culls after streamout. */ 3232bf215546Sopenharmony_ci (sel->stage == MESA_SHADER_GEOMETRY || !sel->info.enabled_streamout_buffer_mask) && 3233bf215546Sopenharmony_ci (sel->stage != MESA_SHADER_GEOMETRY || sel->info.num_stream_output_components[0]) && 3234bf215546Sopenharmony_ci (sel->stage != MESA_SHADER_VERTEX || 3235bf215546Sopenharmony_ci (!sel->info.base.vs.blit_sgprs_amd && 3236bf215546Sopenharmony_ci !sel->info.base.vs.window_space_position)); 3237bf215546Sopenharmony_ci 3238bf215546Sopenharmony_ci sel->ngg_cull_vert_threshold = UINT_MAX; /* disabled (changed below) */ 3239bf215546Sopenharmony_ci 3240bf215546Sopenharmony_ci if (ngg_culling_allowed) { 3241bf215546Sopenharmony_ci if (sel->stage == MESA_SHADER_VERTEX) { 3242bf215546Sopenharmony_ci if (sscreen->debug_flags & DBG(ALWAYS_NGG_CULLING_ALL)) 3243bf215546Sopenharmony_ci sel->ngg_cull_vert_threshold = 0; /* always enabled */ 3244bf215546Sopenharmony_ci else 3245bf215546Sopenharmony_ci sel->ngg_cull_vert_threshold = 128; 3246bf215546Sopenharmony_ci } else if (sel->stage == MESA_SHADER_TESS_EVAL || 3247bf215546Sopenharmony_ci sel->stage == MESA_SHADER_GEOMETRY) { 3248bf215546Sopenharmony_ci if (sel->rast_prim != PIPE_PRIM_POINTS) 3249bf215546Sopenharmony_ci sel->ngg_cull_vert_threshold = 0; /* always enabled */ 3250bf215546Sopenharmony_ci } 3251bf215546Sopenharmony_ci } 3252bf215546Sopenharmony_ci 3253bf215546Sopenharmony_ci (void)simple_mtx_init(&sel->mutex, mtx_plain); 3254bf215546Sopenharmony_ci 3255bf215546Sopenharmony_ci si_schedule_initial_compile(sctx, sel->stage, &sel->ready, &sel->compiler_ctx_state, 3256bf215546Sopenharmony_ci sel, si_init_shader_selector_async); 3257bf215546Sopenharmony_ci return sel; 3258bf215546Sopenharmony_ci} 3259bf215546Sopenharmony_ci 3260bf215546Sopenharmony_cistatic void *si_create_shader(struct pipe_context *ctx, const struct pipe_shader_state *state) 3261bf215546Sopenharmony_ci{ 3262bf215546Sopenharmony_ci struct si_context *sctx = (struct si_context *)ctx; 3263bf215546Sopenharmony_ci struct si_screen *sscreen = (struct si_screen *)ctx->screen; 3264bf215546Sopenharmony_ci bool cache_hit; 3265bf215546Sopenharmony_ci struct si_shader_selector *sel = (struct si_shader_selector *)util_live_shader_cache_get( 3266bf215546Sopenharmony_ci ctx, &sscreen->live_shader_cache, state, &cache_hit); 3267bf215546Sopenharmony_ci 3268bf215546Sopenharmony_ci if (sel && cache_hit && sctx->debug.debug_message) { 3269bf215546Sopenharmony_ci if (sel->main_shader_part) 3270bf215546Sopenharmony_ci si_shader_dump_stats_for_shader_db(sscreen, sel->main_shader_part, &sctx->debug); 3271bf215546Sopenharmony_ci if (sel->main_shader_part_ls) 3272bf215546Sopenharmony_ci si_shader_dump_stats_for_shader_db(sscreen, sel->main_shader_part_ls, &sctx->debug); 3273bf215546Sopenharmony_ci if (sel->main_shader_part_es) 3274bf215546Sopenharmony_ci si_shader_dump_stats_for_shader_db(sscreen, sel->main_shader_part_es, &sctx->debug); 3275bf215546Sopenharmony_ci if (sel->main_shader_part_ngg) 3276bf215546Sopenharmony_ci si_shader_dump_stats_for_shader_db(sscreen, sel->main_shader_part_ngg, &sctx->debug); 3277bf215546Sopenharmony_ci if (sel->main_shader_part_ngg_es) 3278bf215546Sopenharmony_ci si_shader_dump_stats_for_shader_db(sscreen, sel->main_shader_part_ngg_es, &sctx->debug); 3279bf215546Sopenharmony_ci } 3280bf215546Sopenharmony_ci return sel; 3281bf215546Sopenharmony_ci} 3282bf215546Sopenharmony_ci 3283bf215546Sopenharmony_cistatic void si_update_streamout_state(struct si_context *sctx) 3284bf215546Sopenharmony_ci{ 3285bf215546Sopenharmony_ci struct si_shader_selector *shader_with_so = si_get_vs(sctx)->cso; 3286bf215546Sopenharmony_ci 3287bf215546Sopenharmony_ci if (!shader_with_so) 3288bf215546Sopenharmony_ci return; 3289bf215546Sopenharmony_ci 3290bf215546Sopenharmony_ci sctx->streamout.enabled_stream_buffers_mask = shader_with_so->info.enabled_streamout_buffer_mask; 3291bf215546Sopenharmony_ci sctx->streamout.stride_in_dw = shader_with_so->info.base.xfb_stride; 3292bf215546Sopenharmony_ci 3293bf215546Sopenharmony_ci /* GDS must be allocated when any GDS instructions are used, otherwise it hangs. */ 3294bf215546Sopenharmony_ci if (sctx->screen->use_ngg_streamout && shader_with_so->info.enabled_streamout_buffer_mask) 3295bf215546Sopenharmony_ci si_allocate_gds(sctx); 3296bf215546Sopenharmony_ci} 3297bf215546Sopenharmony_ci 3298bf215546Sopenharmony_cistatic void si_update_clip_regs(struct si_context *sctx, struct si_shader_selector *old_hw_vs, 3299bf215546Sopenharmony_ci struct si_shader *old_hw_vs_variant, 3300bf215546Sopenharmony_ci struct si_shader_selector *next_hw_vs, 3301bf215546Sopenharmony_ci struct si_shader *next_hw_vs_variant) 3302bf215546Sopenharmony_ci{ 3303bf215546Sopenharmony_ci if (next_hw_vs && 3304bf215546Sopenharmony_ci (!old_hw_vs || 3305bf215546Sopenharmony_ci (old_hw_vs->stage == MESA_SHADER_VERTEX && old_hw_vs->info.base.vs.window_space_position) != 3306bf215546Sopenharmony_ci (next_hw_vs->stage == MESA_SHADER_VERTEX && next_hw_vs->info.base.vs.window_space_position) || 3307bf215546Sopenharmony_ci old_hw_vs->info.clipdist_mask != next_hw_vs->info.clipdist_mask || 3308bf215546Sopenharmony_ci old_hw_vs->info.culldist_mask != next_hw_vs->info.culldist_mask || !old_hw_vs_variant || 3309bf215546Sopenharmony_ci !next_hw_vs_variant || 3310bf215546Sopenharmony_ci old_hw_vs_variant->pa_cl_vs_out_cntl != next_hw_vs_variant->pa_cl_vs_out_cntl)) 3311bf215546Sopenharmony_ci si_mark_atom_dirty(sctx, &sctx->atoms.s.clip_regs); 3312bf215546Sopenharmony_ci} 3313bf215546Sopenharmony_ci 3314bf215546Sopenharmony_cistatic void si_update_rasterized_prim(struct si_context *sctx) 3315bf215546Sopenharmony_ci{ 3316bf215546Sopenharmony_ci enum pipe_prim_type rast_prim; 3317bf215546Sopenharmony_ci 3318bf215546Sopenharmony_ci if (sctx->shader.gs.cso) { 3319bf215546Sopenharmony_ci /* Only possibilities: POINTS, LINE_STRIP, TRIANGLES */ 3320bf215546Sopenharmony_ci rast_prim = sctx->shader.gs.cso->rast_prim; 3321bf215546Sopenharmony_ci } else if (sctx->shader.tes.cso) { 3322bf215546Sopenharmony_ci /* Only possibilities: POINTS, LINE_STRIP, TRIANGLES */ 3323bf215546Sopenharmony_ci rast_prim = sctx->shader.tes.cso->rast_prim; 3324bf215546Sopenharmony_ci } else { 3325bf215546Sopenharmony_ci /* Determined by draw calls. */ 3326bf215546Sopenharmony_ci return; 3327bf215546Sopenharmony_ci } 3328bf215546Sopenharmony_ci 3329bf215546Sopenharmony_ci if (rast_prim != sctx->current_rast_prim) { 3330bf215546Sopenharmony_ci if (util_prim_is_points_or_lines(sctx->current_rast_prim) != 3331bf215546Sopenharmony_ci util_prim_is_points_or_lines(rast_prim)) 3332bf215546Sopenharmony_ci si_mark_atom_dirty(sctx, &sctx->atoms.s.guardband); 3333bf215546Sopenharmony_ci 3334bf215546Sopenharmony_ci sctx->current_rast_prim = rast_prim; 3335bf215546Sopenharmony_ci } 3336bf215546Sopenharmony_ci} 3337bf215546Sopenharmony_ci 3338bf215546Sopenharmony_cistatic void si_update_common_shader_state(struct si_context *sctx, struct si_shader_selector *sel, 3339bf215546Sopenharmony_ci enum pipe_shader_type type) 3340bf215546Sopenharmony_ci{ 3341bf215546Sopenharmony_ci si_set_active_descriptors_for_shader(sctx, sel); 3342bf215546Sopenharmony_ci 3343bf215546Sopenharmony_ci sctx->uses_bindless_samplers = si_shader_uses_bindless_samplers(sctx->shader.vs.cso) || 3344bf215546Sopenharmony_ci si_shader_uses_bindless_samplers(sctx->shader.gs.cso) || 3345bf215546Sopenharmony_ci si_shader_uses_bindless_samplers(sctx->shader.ps.cso) || 3346bf215546Sopenharmony_ci si_shader_uses_bindless_samplers(sctx->shader.tcs.cso) || 3347bf215546Sopenharmony_ci si_shader_uses_bindless_samplers(sctx->shader.tes.cso); 3348bf215546Sopenharmony_ci sctx->uses_bindless_images = si_shader_uses_bindless_images(sctx->shader.vs.cso) || 3349bf215546Sopenharmony_ci si_shader_uses_bindless_images(sctx->shader.gs.cso) || 3350bf215546Sopenharmony_ci si_shader_uses_bindless_images(sctx->shader.ps.cso) || 3351bf215546Sopenharmony_ci si_shader_uses_bindless_images(sctx->shader.tcs.cso) || 3352bf215546Sopenharmony_ci si_shader_uses_bindless_images(sctx->shader.tes.cso); 3353bf215546Sopenharmony_ci 3354bf215546Sopenharmony_ci if (type == PIPE_SHADER_VERTEX || type == PIPE_SHADER_TESS_EVAL || type == PIPE_SHADER_GEOMETRY) 3355bf215546Sopenharmony_ci sctx->ngg_culling = 0; /* this will be enabled on the first draw if needed */ 3356bf215546Sopenharmony_ci 3357bf215546Sopenharmony_ci si_invalidate_inlinable_uniforms(sctx, type); 3358bf215546Sopenharmony_ci sctx->do_update_shaders = true; 3359bf215546Sopenharmony_ci} 3360bf215546Sopenharmony_ci 3361bf215546Sopenharmony_cistatic void si_bind_vs_shader(struct pipe_context *ctx, void *state) 3362bf215546Sopenharmony_ci{ 3363bf215546Sopenharmony_ci struct si_context *sctx = (struct si_context *)ctx; 3364bf215546Sopenharmony_ci struct si_shader_selector *old_hw_vs = si_get_vs(sctx)->cso; 3365bf215546Sopenharmony_ci struct si_shader *old_hw_vs_variant = si_get_vs(sctx)->current; 3366bf215546Sopenharmony_ci struct si_shader_selector *sel = (struct si_shader_selector*)state; 3367bf215546Sopenharmony_ci 3368bf215546Sopenharmony_ci if (sctx->shader.vs.cso == sel) 3369bf215546Sopenharmony_ci return; 3370bf215546Sopenharmony_ci 3371bf215546Sopenharmony_ci sctx->shader.vs.cso = sel; 3372bf215546Sopenharmony_ci sctx->shader.vs.current = (sel && sel->variants_count) ? sel->variants[0] : NULL; 3373bf215546Sopenharmony_ci sctx->num_vs_blit_sgprs = sel ? sel->info.base.vs.blit_sgprs_amd : 0; 3374bf215546Sopenharmony_ci sctx->vs_uses_draw_id = sel ? sel->info.uses_drawid : false; 3375bf215546Sopenharmony_ci 3376bf215546Sopenharmony_ci if (si_update_ngg(sctx)) 3377bf215546Sopenharmony_ci si_shader_change_notify(sctx); 3378bf215546Sopenharmony_ci 3379bf215546Sopenharmony_ci si_update_common_shader_state(sctx, sel, PIPE_SHADER_VERTEX); 3380bf215546Sopenharmony_ci si_select_draw_vbo(sctx); 3381bf215546Sopenharmony_ci si_update_vs_viewport_state(sctx); 3382bf215546Sopenharmony_ci si_update_streamout_state(sctx); 3383bf215546Sopenharmony_ci si_update_clip_regs(sctx, old_hw_vs, old_hw_vs_variant, si_get_vs(sctx)->cso, 3384bf215546Sopenharmony_ci si_get_vs(sctx)->current); 3385bf215546Sopenharmony_ci si_update_rasterized_prim(sctx); 3386bf215546Sopenharmony_ci si_vs_key_update_inputs(sctx); 3387bf215546Sopenharmony_ci 3388bf215546Sopenharmony_ci if (sctx->screen->dpbb_allowed) { 3389bf215546Sopenharmony_ci bool force_off = sel && sel->info.options & SI_PROFILE_VS_NO_BINNING; 3390bf215546Sopenharmony_ci 3391bf215546Sopenharmony_ci if (force_off != sctx->dpbb_force_off_profile_vs) { 3392bf215546Sopenharmony_ci sctx->dpbb_force_off_profile_vs = force_off; 3393bf215546Sopenharmony_ci si_mark_atom_dirty(sctx, &sctx->atoms.s.dpbb_state); 3394bf215546Sopenharmony_ci } 3395bf215546Sopenharmony_ci } 3396bf215546Sopenharmony_ci} 3397bf215546Sopenharmony_ci 3398bf215546Sopenharmony_cistatic void si_update_tess_uses_prim_id(struct si_context *sctx) 3399bf215546Sopenharmony_ci{ 3400bf215546Sopenharmony_ci sctx->ia_multi_vgt_param_key.u.tess_uses_prim_id = 3401bf215546Sopenharmony_ci (sctx->shader.tes.cso && sctx->shader.tes.cso->info.uses_primid) || 3402bf215546Sopenharmony_ci (sctx->shader.tcs.cso && sctx->shader.tcs.cso->info.uses_primid) || 3403bf215546Sopenharmony_ci (sctx->shader.gs.cso && sctx->shader.gs.cso->info.uses_primid) || 3404bf215546Sopenharmony_ci (sctx->shader.ps.cso && !sctx->shader.gs.cso && sctx->shader.ps.cso->info.uses_primid); 3405bf215546Sopenharmony_ci} 3406bf215546Sopenharmony_ci 3407bf215546Sopenharmony_cibool si_update_ngg(struct si_context *sctx) 3408bf215546Sopenharmony_ci{ 3409bf215546Sopenharmony_ci if (!sctx->screen->use_ngg) { 3410bf215546Sopenharmony_ci assert(!sctx->ngg); 3411bf215546Sopenharmony_ci return false; 3412bf215546Sopenharmony_ci } 3413bf215546Sopenharmony_ci 3414bf215546Sopenharmony_ci bool new_ngg = true; 3415bf215546Sopenharmony_ci 3416bf215546Sopenharmony_ci if (sctx->shader.gs.cso && sctx->shader.tes.cso && sctx->shader.gs.cso->tess_turns_off_ngg) { 3417bf215546Sopenharmony_ci new_ngg = false; 3418bf215546Sopenharmony_ci } else if (!sctx->screen->use_ngg_streamout) { 3419bf215546Sopenharmony_ci struct si_shader_selector *last = si_get_vs(sctx)->cso; 3420bf215546Sopenharmony_ci 3421bf215546Sopenharmony_ci if ((last && last->info.enabled_streamout_buffer_mask) || 3422bf215546Sopenharmony_ci sctx->streamout.prims_gen_query_enabled) 3423bf215546Sopenharmony_ci new_ngg = false; 3424bf215546Sopenharmony_ci } 3425bf215546Sopenharmony_ci 3426bf215546Sopenharmony_ci if (new_ngg != sctx->ngg) { 3427bf215546Sopenharmony_ci /* Transitioning from NGG to legacy GS requires VGT_FLUSH on Navi10-14. 3428bf215546Sopenharmony_ci * VGT_FLUSH is also emitted at the beginning of IBs when legacy GS ring 3429bf215546Sopenharmony_ci * pointers are set. 3430bf215546Sopenharmony_ci */ 3431bf215546Sopenharmony_ci if (sctx->screen->info.has_vgt_flush_ngg_legacy_bug && !new_ngg) { 3432bf215546Sopenharmony_ci sctx->flags |= SI_CONTEXT_VGT_FLUSH; 3433bf215546Sopenharmony_ci if (sctx->gfx_level == GFX10) { 3434bf215546Sopenharmony_ci /* Workaround for https://gitlab.freedesktop.org/mesa/mesa/-/issues/2941 */ 3435bf215546Sopenharmony_ci si_flush_gfx_cs(sctx, RADEON_FLUSH_ASYNC_START_NEXT_GFX_IB_NOW, NULL); 3436bf215546Sopenharmony_ci } 3437bf215546Sopenharmony_ci } 3438bf215546Sopenharmony_ci 3439bf215546Sopenharmony_ci sctx->ngg = new_ngg; 3440bf215546Sopenharmony_ci sctx->last_gs_out_prim = -1; /* reset this so that it gets updated */ 3441bf215546Sopenharmony_ci si_select_draw_vbo(sctx); 3442bf215546Sopenharmony_ci return true; 3443bf215546Sopenharmony_ci } 3444bf215546Sopenharmony_ci return false; 3445bf215546Sopenharmony_ci} 3446bf215546Sopenharmony_ci 3447bf215546Sopenharmony_cistatic void si_bind_gs_shader(struct pipe_context *ctx, void *state) 3448bf215546Sopenharmony_ci{ 3449bf215546Sopenharmony_ci struct si_context *sctx = (struct si_context *)ctx; 3450bf215546Sopenharmony_ci struct si_shader_selector *old_hw_vs = si_get_vs(sctx)->cso; 3451bf215546Sopenharmony_ci struct si_shader *old_hw_vs_variant = si_get_vs(sctx)->current; 3452bf215546Sopenharmony_ci struct si_shader_selector *sel = (struct si_shader_selector*)state; 3453bf215546Sopenharmony_ci bool enable_changed = !!sctx->shader.gs.cso != !!sel; 3454bf215546Sopenharmony_ci bool ngg_changed; 3455bf215546Sopenharmony_ci 3456bf215546Sopenharmony_ci if (sctx->shader.gs.cso == sel) 3457bf215546Sopenharmony_ci return; 3458bf215546Sopenharmony_ci 3459bf215546Sopenharmony_ci sctx->shader.gs.cso = sel; 3460bf215546Sopenharmony_ci sctx->shader.gs.current = (sel && sel->variants_count) ? sel->variants[0] : NULL; 3461bf215546Sopenharmony_ci sctx->ia_multi_vgt_param_key.u.uses_gs = sel != NULL; 3462bf215546Sopenharmony_ci 3463bf215546Sopenharmony_ci si_update_common_shader_state(sctx, sel, PIPE_SHADER_GEOMETRY); 3464bf215546Sopenharmony_ci si_select_draw_vbo(sctx); 3465bf215546Sopenharmony_ci sctx->last_gs_out_prim = -1; /* reset this so that it gets updated */ 3466bf215546Sopenharmony_ci 3467bf215546Sopenharmony_ci ngg_changed = si_update_ngg(sctx); 3468bf215546Sopenharmony_ci if (ngg_changed || enable_changed) 3469bf215546Sopenharmony_ci si_shader_change_notify(sctx); 3470bf215546Sopenharmony_ci if (enable_changed) { 3471bf215546Sopenharmony_ci if (sctx->ia_multi_vgt_param_key.u.uses_tess) 3472bf215546Sopenharmony_ci si_update_tess_uses_prim_id(sctx); 3473bf215546Sopenharmony_ci } 3474bf215546Sopenharmony_ci si_update_vs_viewport_state(sctx); 3475bf215546Sopenharmony_ci si_update_streamout_state(sctx); 3476bf215546Sopenharmony_ci si_update_clip_regs(sctx, old_hw_vs, old_hw_vs_variant, si_get_vs(sctx)->cso, 3477bf215546Sopenharmony_ci si_get_vs(sctx)->current); 3478bf215546Sopenharmony_ci si_update_rasterized_prim(sctx); 3479bf215546Sopenharmony_ci} 3480bf215546Sopenharmony_ci 3481bf215546Sopenharmony_cistatic void si_bind_tcs_shader(struct pipe_context *ctx, void *state) 3482bf215546Sopenharmony_ci{ 3483bf215546Sopenharmony_ci struct si_context *sctx = (struct si_context *)ctx; 3484bf215546Sopenharmony_ci struct si_shader_selector *sel = (struct si_shader_selector*)state; 3485bf215546Sopenharmony_ci bool enable_changed = !!sctx->shader.tcs.cso != !!sel; 3486bf215546Sopenharmony_ci 3487bf215546Sopenharmony_ci /* Note it could happen that user shader sel is same as fixed function shader, 3488bf215546Sopenharmony_ci * so we should update this field even sctx->shader.tcs.cso == sel. 3489bf215546Sopenharmony_ci */ 3490bf215546Sopenharmony_ci sctx->is_user_tcs = !!sel; 3491bf215546Sopenharmony_ci 3492bf215546Sopenharmony_ci if (sctx->shader.tcs.cso == sel) 3493bf215546Sopenharmony_ci return; 3494bf215546Sopenharmony_ci 3495bf215546Sopenharmony_ci sctx->shader.tcs.cso = sel; 3496bf215546Sopenharmony_ci sctx->shader.tcs.current = (sel && sel->variants_count) ? sel->variants[0] : NULL; 3497bf215546Sopenharmony_ci sctx->shader.tcs.key.ge.part.tcs.epilog.invoc0_tess_factors_are_def = 3498bf215546Sopenharmony_ci sel ? sel->info.tessfactors_are_def_in_all_invocs : 0; 3499bf215546Sopenharmony_ci si_update_tess_uses_prim_id(sctx); 3500bf215546Sopenharmony_ci 3501bf215546Sopenharmony_ci si_update_common_shader_state(sctx, sel, PIPE_SHADER_TESS_CTRL); 3502bf215546Sopenharmony_ci 3503bf215546Sopenharmony_ci if (enable_changed) 3504bf215546Sopenharmony_ci sctx->last_tcs = NULL; /* invalidate derived tess state */ 3505bf215546Sopenharmony_ci} 3506bf215546Sopenharmony_ci 3507bf215546Sopenharmony_cistatic void si_bind_tes_shader(struct pipe_context *ctx, void *state) 3508bf215546Sopenharmony_ci{ 3509bf215546Sopenharmony_ci struct si_context *sctx = (struct si_context *)ctx; 3510bf215546Sopenharmony_ci struct si_shader_selector *old_hw_vs = si_get_vs(sctx)->cso; 3511bf215546Sopenharmony_ci struct si_shader *old_hw_vs_variant = si_get_vs(sctx)->current; 3512bf215546Sopenharmony_ci struct si_shader_selector *sel = (struct si_shader_selector*)state; 3513bf215546Sopenharmony_ci bool enable_changed = !!sctx->shader.tes.cso != !!sel; 3514bf215546Sopenharmony_ci 3515bf215546Sopenharmony_ci if (sctx->shader.tes.cso == sel) 3516bf215546Sopenharmony_ci return; 3517bf215546Sopenharmony_ci 3518bf215546Sopenharmony_ci sctx->shader.tes.cso = sel; 3519bf215546Sopenharmony_ci sctx->shader.tes.current = (sel && sel->variants_count) ? sel->variants[0] : NULL; 3520bf215546Sopenharmony_ci sctx->ia_multi_vgt_param_key.u.uses_tess = sel != NULL; 3521bf215546Sopenharmony_ci si_update_tess_uses_prim_id(sctx); 3522bf215546Sopenharmony_ci 3523bf215546Sopenharmony_ci sctx->shader.tcs.key.ge.part.tcs.epilog.prim_mode = 3524bf215546Sopenharmony_ci sel ? sel->info.base.tess._primitive_mode : 0; 3525bf215546Sopenharmony_ci 3526bf215546Sopenharmony_ci sctx->shader.tcs.key.ge.part.tcs.epilog.tes_reads_tess_factors = 3527bf215546Sopenharmony_ci sel ? sel->info.reads_tess_factors : 0; 3528bf215546Sopenharmony_ci 3529bf215546Sopenharmony_ci si_update_common_shader_state(sctx, sel, PIPE_SHADER_TESS_EVAL); 3530bf215546Sopenharmony_ci si_select_draw_vbo(sctx); 3531bf215546Sopenharmony_ci sctx->last_gs_out_prim = -1; /* reset this so that it gets updated */ 3532bf215546Sopenharmony_ci 3533bf215546Sopenharmony_ci bool ngg_changed = si_update_ngg(sctx); 3534bf215546Sopenharmony_ci if (ngg_changed || enable_changed) 3535bf215546Sopenharmony_ci si_shader_change_notify(sctx); 3536bf215546Sopenharmony_ci if (enable_changed) 3537bf215546Sopenharmony_ci sctx->last_tes_sh_base = -1; /* invalidate derived tess state */ 3538bf215546Sopenharmony_ci si_update_vs_viewport_state(sctx); 3539bf215546Sopenharmony_ci si_update_streamout_state(sctx); 3540bf215546Sopenharmony_ci si_update_clip_regs(sctx, old_hw_vs, old_hw_vs_variant, si_get_vs(sctx)->cso, 3541bf215546Sopenharmony_ci si_get_vs(sctx)->current); 3542bf215546Sopenharmony_ci si_update_rasterized_prim(sctx); 3543bf215546Sopenharmony_ci} 3544bf215546Sopenharmony_ci 3545bf215546Sopenharmony_civoid si_update_vrs_flat_shading(struct si_context *sctx) 3546bf215546Sopenharmony_ci{ 3547bf215546Sopenharmony_ci if (sctx->gfx_level >= GFX10_3 && sctx->shader.ps.cso) { 3548bf215546Sopenharmony_ci struct si_state_rasterizer *rs = sctx->queued.named.rasterizer; 3549bf215546Sopenharmony_ci struct si_shader_info *info = &sctx->shader.ps.cso->info; 3550bf215546Sopenharmony_ci bool allow_flat_shading = info->allow_flat_shading; 3551bf215546Sopenharmony_ci 3552bf215546Sopenharmony_ci if (allow_flat_shading && 3553bf215546Sopenharmony_ci (rs->line_smooth || rs->poly_smooth || rs->poly_stipple_enable || 3554bf215546Sopenharmony_ci rs->point_smooth || (!rs->flatshade && info->uses_interp_color))) 3555bf215546Sopenharmony_ci allow_flat_shading = false; 3556bf215546Sopenharmony_ci 3557bf215546Sopenharmony_ci if (sctx->allow_flat_shading != allow_flat_shading) { 3558bf215546Sopenharmony_ci sctx->allow_flat_shading = allow_flat_shading; 3559bf215546Sopenharmony_ci si_mark_atom_dirty(sctx, &sctx->atoms.s.db_render_state); 3560bf215546Sopenharmony_ci } 3561bf215546Sopenharmony_ci } 3562bf215546Sopenharmony_ci} 3563bf215546Sopenharmony_ci 3564bf215546Sopenharmony_cistatic void si_bind_ps_shader(struct pipe_context *ctx, void *state) 3565bf215546Sopenharmony_ci{ 3566bf215546Sopenharmony_ci struct si_context *sctx = (struct si_context *)ctx; 3567bf215546Sopenharmony_ci struct si_shader_selector *old_sel = sctx->shader.ps.cso; 3568bf215546Sopenharmony_ci struct si_shader_selector *sel = (struct si_shader_selector*)state; 3569bf215546Sopenharmony_ci 3570bf215546Sopenharmony_ci /* skip if supplied shader is one already in use */ 3571bf215546Sopenharmony_ci if (old_sel == sel) 3572bf215546Sopenharmony_ci return; 3573bf215546Sopenharmony_ci 3574bf215546Sopenharmony_ci sctx->shader.ps.cso = sel; 3575bf215546Sopenharmony_ci sctx->shader.ps.current = (sel && sel->variants_count) ? sel->variants[0] : NULL; 3576bf215546Sopenharmony_ci 3577bf215546Sopenharmony_ci si_update_common_shader_state(sctx, sel, PIPE_SHADER_FRAGMENT); 3578bf215546Sopenharmony_ci if (sel) { 3579bf215546Sopenharmony_ci if (sctx->ia_multi_vgt_param_key.u.uses_tess) 3580bf215546Sopenharmony_ci si_update_tess_uses_prim_id(sctx); 3581bf215546Sopenharmony_ci 3582bf215546Sopenharmony_ci if (!old_sel || old_sel->info.colors_written != sel->info.colors_written) 3583bf215546Sopenharmony_ci si_mark_atom_dirty(sctx, &sctx->atoms.s.cb_render_state); 3584bf215546Sopenharmony_ci 3585bf215546Sopenharmony_ci if (sctx->screen->has_out_of_order_rast && 3586bf215546Sopenharmony_ci (!old_sel || old_sel->info.base.writes_memory != sel->info.base.writes_memory || 3587bf215546Sopenharmony_ci old_sel->info.base.fs.early_fragment_tests != 3588bf215546Sopenharmony_ci sel->info.base.fs.early_fragment_tests)) 3589bf215546Sopenharmony_ci si_mark_atom_dirty(sctx, &sctx->atoms.s.msaa_config); 3590bf215546Sopenharmony_ci } 3591bf215546Sopenharmony_ci si_update_ps_colorbuf0_slot(sctx); 3592bf215546Sopenharmony_ci 3593bf215546Sopenharmony_ci si_ps_key_update_framebuffer(sctx); 3594bf215546Sopenharmony_ci si_ps_key_update_framebuffer_blend(sctx); 3595bf215546Sopenharmony_ci si_ps_key_update_blend_rasterizer(sctx); 3596bf215546Sopenharmony_ci si_ps_key_update_rasterizer(sctx); 3597bf215546Sopenharmony_ci si_ps_key_update_dsa(sctx); 3598bf215546Sopenharmony_ci si_ps_key_update_sample_shading(sctx); 3599bf215546Sopenharmony_ci si_ps_key_update_framebuffer_rasterizer_sample_shading(sctx); 3600bf215546Sopenharmony_ci si_update_ps_inputs_read_or_disabled(sctx); 3601bf215546Sopenharmony_ci si_update_vrs_flat_shading(sctx); 3602bf215546Sopenharmony_ci 3603bf215546Sopenharmony_ci if (sctx->screen->dpbb_allowed) { 3604bf215546Sopenharmony_ci bool force_off = sel && sel->info.options & SI_PROFILE_PS_NO_BINNING; 3605bf215546Sopenharmony_ci 3606bf215546Sopenharmony_ci if (force_off != sctx->dpbb_force_off_profile_ps) { 3607bf215546Sopenharmony_ci sctx->dpbb_force_off_profile_ps = force_off; 3608bf215546Sopenharmony_ci si_mark_atom_dirty(sctx, &sctx->atoms.s.dpbb_state); 3609bf215546Sopenharmony_ci } 3610bf215546Sopenharmony_ci } 3611bf215546Sopenharmony_ci} 3612bf215546Sopenharmony_ci 3613bf215546Sopenharmony_cistatic void si_delete_shader(struct si_context *sctx, struct si_shader *shader) 3614bf215546Sopenharmony_ci{ 3615bf215546Sopenharmony_ci if (shader->is_optimized) { 3616bf215546Sopenharmony_ci util_queue_drop_job(&sctx->screen->shader_compiler_queue_low_priority, &shader->ready); 3617bf215546Sopenharmony_ci } 3618bf215546Sopenharmony_ci 3619bf215546Sopenharmony_ci util_queue_fence_destroy(&shader->ready); 3620bf215546Sopenharmony_ci 3621bf215546Sopenharmony_ci /* If destroyed shaders were not unbound, the next compiled 3622bf215546Sopenharmony_ci * shader variant could get the same pointer address and so 3623bf215546Sopenharmony_ci * binding it to the same shader stage would be considered 3624bf215546Sopenharmony_ci * a no-op, causing random behavior. 3625bf215546Sopenharmony_ci */ 3626bf215546Sopenharmony_ci int state_index = -1; 3627bf215546Sopenharmony_ci 3628bf215546Sopenharmony_ci switch (shader->selector->stage) { 3629bf215546Sopenharmony_ci case MESA_SHADER_VERTEX: 3630bf215546Sopenharmony_ci if (shader->key.ge.as_ls) { 3631bf215546Sopenharmony_ci if (sctx->gfx_level <= GFX8) 3632bf215546Sopenharmony_ci state_index = SI_STATE_IDX(ls); 3633bf215546Sopenharmony_ci } else if (shader->key.ge.as_es) { 3634bf215546Sopenharmony_ci if (sctx->gfx_level <= GFX8) 3635bf215546Sopenharmony_ci state_index = SI_STATE_IDX(es); 3636bf215546Sopenharmony_ci } else if (shader->key.ge.as_ngg) { 3637bf215546Sopenharmony_ci state_index = SI_STATE_IDX(gs); 3638bf215546Sopenharmony_ci } else { 3639bf215546Sopenharmony_ci state_index = SI_STATE_IDX(vs); 3640bf215546Sopenharmony_ci } 3641bf215546Sopenharmony_ci break; 3642bf215546Sopenharmony_ci case MESA_SHADER_TESS_CTRL: 3643bf215546Sopenharmony_ci state_index = SI_STATE_IDX(hs); 3644bf215546Sopenharmony_ci break; 3645bf215546Sopenharmony_ci case MESA_SHADER_TESS_EVAL: 3646bf215546Sopenharmony_ci if (shader->key.ge.as_es) { 3647bf215546Sopenharmony_ci if (sctx->gfx_level <= GFX8) 3648bf215546Sopenharmony_ci state_index = SI_STATE_IDX(es); 3649bf215546Sopenharmony_ci } else if (shader->key.ge.as_ngg) { 3650bf215546Sopenharmony_ci state_index = SI_STATE_IDX(gs); 3651bf215546Sopenharmony_ci } else { 3652bf215546Sopenharmony_ci state_index = SI_STATE_IDX(vs); 3653bf215546Sopenharmony_ci } 3654bf215546Sopenharmony_ci break; 3655bf215546Sopenharmony_ci case MESA_SHADER_GEOMETRY: 3656bf215546Sopenharmony_ci if (shader->is_gs_copy_shader) 3657bf215546Sopenharmony_ci state_index = SI_STATE_IDX(vs); 3658bf215546Sopenharmony_ci else 3659bf215546Sopenharmony_ci state_index = SI_STATE_IDX(gs); 3660bf215546Sopenharmony_ci break; 3661bf215546Sopenharmony_ci case MESA_SHADER_FRAGMENT: 3662bf215546Sopenharmony_ci state_index = SI_STATE_IDX(ps); 3663bf215546Sopenharmony_ci break; 3664bf215546Sopenharmony_ci default:; 3665bf215546Sopenharmony_ci } 3666bf215546Sopenharmony_ci 3667bf215546Sopenharmony_ci if (shader->gs_copy_shader) 3668bf215546Sopenharmony_ci si_delete_shader(sctx, shader->gs_copy_shader); 3669bf215546Sopenharmony_ci 3670bf215546Sopenharmony_ci si_shader_selector_reference(sctx, &shader->previous_stage_sel, NULL); 3671bf215546Sopenharmony_ci si_shader_destroy(shader); 3672bf215546Sopenharmony_ci si_pm4_free_state(sctx, &shader->pm4, state_index); 3673bf215546Sopenharmony_ci} 3674bf215546Sopenharmony_ci 3675bf215546Sopenharmony_cistatic void si_destroy_shader_selector(struct pipe_context *ctx, void *cso) 3676bf215546Sopenharmony_ci{ 3677bf215546Sopenharmony_ci struct si_context *sctx = (struct si_context *)ctx; 3678bf215546Sopenharmony_ci struct si_shader_selector *sel = (struct si_shader_selector *)cso; 3679bf215546Sopenharmony_ci enum pipe_shader_type type = pipe_shader_type_from_mesa(sel->stage); 3680bf215546Sopenharmony_ci 3681bf215546Sopenharmony_ci util_queue_drop_job(&sctx->screen->shader_compiler_queue, &sel->ready); 3682bf215546Sopenharmony_ci 3683bf215546Sopenharmony_ci if (sctx->shaders[type].cso == sel) { 3684bf215546Sopenharmony_ci sctx->shaders[type].cso = NULL; 3685bf215546Sopenharmony_ci sctx->shaders[type].current = NULL; 3686bf215546Sopenharmony_ci } 3687bf215546Sopenharmony_ci 3688bf215546Sopenharmony_ci for (unsigned i = 0; i < sel->variants_count; i++) { 3689bf215546Sopenharmony_ci si_delete_shader(sctx, sel->variants[i]); 3690bf215546Sopenharmony_ci } 3691bf215546Sopenharmony_ci 3692bf215546Sopenharmony_ci if (sel->main_shader_part) 3693bf215546Sopenharmony_ci si_delete_shader(sctx, sel->main_shader_part); 3694bf215546Sopenharmony_ci if (sel->main_shader_part_ls) 3695bf215546Sopenharmony_ci si_delete_shader(sctx, sel->main_shader_part_ls); 3696bf215546Sopenharmony_ci if (sel->main_shader_part_es) 3697bf215546Sopenharmony_ci si_delete_shader(sctx, sel->main_shader_part_es); 3698bf215546Sopenharmony_ci if (sel->main_shader_part_ngg) 3699bf215546Sopenharmony_ci si_delete_shader(sctx, sel->main_shader_part_ngg); 3700bf215546Sopenharmony_ci 3701bf215546Sopenharmony_ci free(sel->keys); 3702bf215546Sopenharmony_ci free(sel->variants); 3703bf215546Sopenharmony_ci 3704bf215546Sopenharmony_ci util_queue_fence_destroy(&sel->ready); 3705bf215546Sopenharmony_ci simple_mtx_destroy(&sel->mutex); 3706bf215546Sopenharmony_ci ralloc_free(sel->nir); 3707bf215546Sopenharmony_ci free(sel->nir_binary); 3708bf215546Sopenharmony_ci free(sel); 3709bf215546Sopenharmony_ci} 3710bf215546Sopenharmony_ci 3711bf215546Sopenharmony_cistatic void si_delete_shader_selector(struct pipe_context *ctx, void *state) 3712bf215546Sopenharmony_ci{ 3713bf215546Sopenharmony_ci struct si_context *sctx = (struct si_context *)ctx; 3714bf215546Sopenharmony_ci struct si_shader_selector *sel = (struct si_shader_selector *)state; 3715bf215546Sopenharmony_ci 3716bf215546Sopenharmony_ci si_shader_selector_reference(sctx, &sel, NULL); 3717bf215546Sopenharmony_ci} 3718bf215546Sopenharmony_ci 3719bf215546Sopenharmony_ci/** 3720bf215546Sopenharmony_ci * Writing CONFIG or UCONFIG VGT registers requires VGT_FLUSH before that. 3721bf215546Sopenharmony_ci */ 3722bf215546Sopenharmony_cistatic void si_cs_preamble_add_vgt_flush(struct si_context *sctx, bool tmz) 3723bf215546Sopenharmony_ci{ 3724bf215546Sopenharmony_ci struct si_pm4_state *pm4 = tmz ? sctx->cs_preamble_state_tmz : sctx->cs_preamble_state; 3725bf215546Sopenharmony_ci bool *has_vgt_flush = tmz ? &sctx->cs_preamble_has_vgt_flush_tmz : 3726bf215546Sopenharmony_ci &sctx->cs_preamble_has_vgt_flush; 3727bf215546Sopenharmony_ci 3728bf215546Sopenharmony_ci /* We shouldn't get here if registers are shadowed. */ 3729bf215546Sopenharmony_ci assert(!sctx->shadowed_regs); 3730bf215546Sopenharmony_ci 3731bf215546Sopenharmony_ci if (*has_vgt_flush) 3732bf215546Sopenharmony_ci return; 3733bf215546Sopenharmony_ci 3734bf215546Sopenharmony_ci /* Done by Vulkan before VGT_FLUSH. */ 3735bf215546Sopenharmony_ci si_pm4_cmd_add(pm4, PKT3(PKT3_EVENT_WRITE, 0, 0)); 3736bf215546Sopenharmony_ci si_pm4_cmd_add(pm4, EVENT_TYPE(V_028A90_VS_PARTIAL_FLUSH) | EVENT_INDEX(4)); 3737bf215546Sopenharmony_ci 3738bf215546Sopenharmony_ci /* VGT_FLUSH is required even if VGT is idle. It resets VGT pointers. */ 3739bf215546Sopenharmony_ci si_pm4_cmd_add(pm4, PKT3(PKT3_EVENT_WRITE, 0, 0)); 3740bf215546Sopenharmony_ci si_pm4_cmd_add(pm4, EVENT_TYPE(V_028A90_VGT_FLUSH) | EVENT_INDEX(0)); 3741bf215546Sopenharmony_ci 3742bf215546Sopenharmony_ci *has_vgt_flush = true; 3743bf215546Sopenharmony_ci} 3744bf215546Sopenharmony_ci 3745bf215546Sopenharmony_ci/** 3746bf215546Sopenharmony_ci * Writing CONFIG or UCONFIG VGT registers requires VGT_FLUSH before that. 3747bf215546Sopenharmony_ci */ 3748bf215546Sopenharmony_cistatic void si_emit_vgt_flush(struct radeon_cmdbuf *cs) 3749bf215546Sopenharmony_ci{ 3750bf215546Sopenharmony_ci radeon_begin(cs); 3751bf215546Sopenharmony_ci 3752bf215546Sopenharmony_ci /* This is required before VGT_FLUSH. */ 3753bf215546Sopenharmony_ci radeon_emit(PKT3(PKT3_EVENT_WRITE, 0, 0)); 3754bf215546Sopenharmony_ci radeon_emit(EVENT_TYPE(V_028A90_VS_PARTIAL_FLUSH) | EVENT_INDEX(4)); 3755bf215546Sopenharmony_ci 3756bf215546Sopenharmony_ci /* VGT_FLUSH is required even if VGT is idle. It resets VGT pointers. */ 3757bf215546Sopenharmony_ci radeon_emit(PKT3(PKT3_EVENT_WRITE, 0, 0)); 3758bf215546Sopenharmony_ci radeon_emit(EVENT_TYPE(V_028A90_VGT_FLUSH) | EVENT_INDEX(0)); 3759bf215546Sopenharmony_ci radeon_end(); 3760bf215546Sopenharmony_ci} 3761bf215546Sopenharmony_ci 3762bf215546Sopenharmony_ci/* Initialize state related to ESGS / GSVS ring buffers */ 3763bf215546Sopenharmony_cibool si_update_gs_ring_buffers(struct si_context *sctx) 3764bf215546Sopenharmony_ci{ 3765bf215546Sopenharmony_ci assert(sctx->gfx_level < GFX11); 3766bf215546Sopenharmony_ci 3767bf215546Sopenharmony_ci struct si_shader_selector *es = 3768bf215546Sopenharmony_ci sctx->shader.tes.cso ? sctx->shader.tes.cso : sctx->shader.vs.cso; 3769bf215546Sopenharmony_ci struct si_shader_selector *gs = sctx->shader.gs.cso; 3770bf215546Sopenharmony_ci 3771bf215546Sopenharmony_ci /* Chip constants. */ 3772bf215546Sopenharmony_ci unsigned num_se = sctx->screen->info.max_se; 3773bf215546Sopenharmony_ci unsigned wave_size = 64; 3774bf215546Sopenharmony_ci unsigned max_gs_waves = 32 * num_se; /* max 32 per SE on GCN */ 3775bf215546Sopenharmony_ci /* On GFX6-GFX7, the value comes from VGT_GS_VERTEX_REUSE = 16. 3776bf215546Sopenharmony_ci * On GFX8+, the value comes from VGT_VERTEX_REUSE_BLOCK_CNTL = 30 (+2). 3777bf215546Sopenharmony_ci */ 3778bf215546Sopenharmony_ci unsigned gs_vertex_reuse = (sctx->gfx_level >= GFX8 ? 32 : 16) * num_se; 3779bf215546Sopenharmony_ci unsigned alignment = 256 * num_se; 3780bf215546Sopenharmony_ci /* The maximum size is 63.999 MB per SE. */ 3781bf215546Sopenharmony_ci unsigned max_size = ((unsigned)(63.999 * 1024 * 1024) & ~255) * num_se; 3782bf215546Sopenharmony_ci 3783bf215546Sopenharmony_ci /* Calculate the minimum size. */ 3784bf215546Sopenharmony_ci unsigned min_esgs_ring_size = align(es->info.esgs_itemsize * gs_vertex_reuse * wave_size, alignment); 3785bf215546Sopenharmony_ci 3786bf215546Sopenharmony_ci /* These are recommended sizes, not minimum sizes. */ 3787bf215546Sopenharmony_ci unsigned esgs_ring_size = 3788bf215546Sopenharmony_ci max_gs_waves * 2 * wave_size * es->info.esgs_itemsize * gs->info.gs_input_verts_per_prim; 3789bf215546Sopenharmony_ci unsigned gsvs_ring_size = max_gs_waves * 2 * wave_size * gs->info.max_gsvs_emit_size; 3790bf215546Sopenharmony_ci 3791bf215546Sopenharmony_ci min_esgs_ring_size = align(min_esgs_ring_size, alignment); 3792bf215546Sopenharmony_ci esgs_ring_size = align(esgs_ring_size, alignment); 3793bf215546Sopenharmony_ci gsvs_ring_size = align(gsvs_ring_size, alignment); 3794bf215546Sopenharmony_ci 3795bf215546Sopenharmony_ci esgs_ring_size = CLAMP(esgs_ring_size, min_esgs_ring_size, max_size); 3796bf215546Sopenharmony_ci gsvs_ring_size = MIN2(gsvs_ring_size, max_size); 3797bf215546Sopenharmony_ci 3798bf215546Sopenharmony_ci /* Some rings don't have to be allocated if shaders don't use them. 3799bf215546Sopenharmony_ci * (e.g. no varyings between ES and GS or GS and VS) 3800bf215546Sopenharmony_ci * 3801bf215546Sopenharmony_ci * GFX9 doesn't have the ESGS ring. 3802bf215546Sopenharmony_ci */ 3803bf215546Sopenharmony_ci bool update_esgs = sctx->gfx_level <= GFX8 && esgs_ring_size && 3804bf215546Sopenharmony_ci (!sctx->esgs_ring || sctx->esgs_ring->width0 < esgs_ring_size); 3805bf215546Sopenharmony_ci bool update_gsvs = 3806bf215546Sopenharmony_ci gsvs_ring_size && (!sctx->gsvs_ring || sctx->gsvs_ring->width0 < gsvs_ring_size); 3807bf215546Sopenharmony_ci 3808bf215546Sopenharmony_ci if (!update_esgs && !update_gsvs) 3809bf215546Sopenharmony_ci return true; 3810bf215546Sopenharmony_ci 3811bf215546Sopenharmony_ci if (update_esgs) { 3812bf215546Sopenharmony_ci pipe_resource_reference(&sctx->esgs_ring, NULL); 3813bf215546Sopenharmony_ci sctx->esgs_ring = 3814bf215546Sopenharmony_ci pipe_aligned_buffer_create(sctx->b.screen, 3815bf215546Sopenharmony_ci PIPE_RESOURCE_FLAG_UNMAPPABLE | SI_RESOURCE_FLAG_DRIVER_INTERNAL | 3816bf215546Sopenharmony_ci SI_RESOURCE_FLAG_DISCARDABLE, 3817bf215546Sopenharmony_ci PIPE_USAGE_DEFAULT, 3818bf215546Sopenharmony_ci esgs_ring_size, sctx->screen->info.pte_fragment_size); 3819bf215546Sopenharmony_ci if (!sctx->esgs_ring) 3820bf215546Sopenharmony_ci return false; 3821bf215546Sopenharmony_ci } 3822bf215546Sopenharmony_ci 3823bf215546Sopenharmony_ci if (update_gsvs) { 3824bf215546Sopenharmony_ci pipe_resource_reference(&sctx->gsvs_ring, NULL); 3825bf215546Sopenharmony_ci sctx->gsvs_ring = 3826bf215546Sopenharmony_ci pipe_aligned_buffer_create(sctx->b.screen, 3827bf215546Sopenharmony_ci PIPE_RESOURCE_FLAG_UNMAPPABLE | SI_RESOURCE_FLAG_DRIVER_INTERNAL | 3828bf215546Sopenharmony_ci SI_RESOURCE_FLAG_DISCARDABLE, 3829bf215546Sopenharmony_ci PIPE_USAGE_DEFAULT, 3830bf215546Sopenharmony_ci gsvs_ring_size, sctx->screen->info.pte_fragment_size); 3831bf215546Sopenharmony_ci if (!sctx->gsvs_ring) 3832bf215546Sopenharmony_ci return false; 3833bf215546Sopenharmony_ci } 3834bf215546Sopenharmony_ci 3835bf215546Sopenharmony_ci /* Set ring bindings. */ 3836bf215546Sopenharmony_ci if (sctx->esgs_ring) { 3837bf215546Sopenharmony_ci assert(sctx->gfx_level <= GFX8); 3838bf215546Sopenharmony_ci si_set_ring_buffer(sctx, SI_RING_ESGS, sctx->esgs_ring, 0, sctx->esgs_ring->width0, false, 3839bf215546Sopenharmony_ci false, 0, 0, 0); 3840bf215546Sopenharmony_ci } 3841bf215546Sopenharmony_ci if (sctx->gsvs_ring) { 3842bf215546Sopenharmony_ci si_set_ring_buffer(sctx, SI_RING_GSVS, sctx->gsvs_ring, 0, sctx->gsvs_ring->width0, false, 3843bf215546Sopenharmony_ci false, 0, 0, 0); 3844bf215546Sopenharmony_ci } 3845bf215546Sopenharmony_ci 3846bf215546Sopenharmony_ci if (sctx->shadowed_regs) { 3847bf215546Sopenharmony_ci /* These registers will be shadowed, so set them only once. */ 3848bf215546Sopenharmony_ci struct radeon_cmdbuf *cs = &sctx->gfx_cs; 3849bf215546Sopenharmony_ci 3850bf215546Sopenharmony_ci assert(sctx->gfx_level >= GFX7); 3851bf215546Sopenharmony_ci 3852bf215546Sopenharmony_ci si_emit_vgt_flush(cs); 3853bf215546Sopenharmony_ci 3854bf215546Sopenharmony_ci radeon_begin(cs); 3855bf215546Sopenharmony_ci 3856bf215546Sopenharmony_ci /* Set the GS registers. */ 3857bf215546Sopenharmony_ci if (sctx->esgs_ring) { 3858bf215546Sopenharmony_ci assert(sctx->gfx_level <= GFX8); 3859bf215546Sopenharmony_ci radeon_set_uconfig_reg(R_030900_VGT_ESGS_RING_SIZE, 3860bf215546Sopenharmony_ci sctx->esgs_ring->width0 / 256); 3861bf215546Sopenharmony_ci } 3862bf215546Sopenharmony_ci if (sctx->gsvs_ring) { 3863bf215546Sopenharmony_ci radeon_set_uconfig_reg(R_030904_VGT_GSVS_RING_SIZE, 3864bf215546Sopenharmony_ci sctx->gsvs_ring->width0 / 256); 3865bf215546Sopenharmony_ci } 3866bf215546Sopenharmony_ci radeon_end(); 3867bf215546Sopenharmony_ci return true; 3868bf215546Sopenharmony_ci } 3869bf215546Sopenharmony_ci 3870bf215546Sopenharmony_ci /* The codepath without register shadowing. */ 3871bf215546Sopenharmony_ci for (unsigned tmz = 0; tmz <= 1; tmz++) { 3872bf215546Sopenharmony_ci struct si_pm4_state *pm4 = tmz ? sctx->cs_preamble_state_tmz : sctx->cs_preamble_state; 3873bf215546Sopenharmony_ci uint16_t *gs_ring_state_dw_offset = tmz ? &sctx->gs_ring_state_dw_offset_tmz : 3874bf215546Sopenharmony_ci &sctx->gs_ring_state_dw_offset; 3875bf215546Sopenharmony_ci unsigned old_ndw = 0; 3876bf215546Sopenharmony_ci 3877bf215546Sopenharmony_ci si_cs_preamble_add_vgt_flush(sctx, tmz); 3878bf215546Sopenharmony_ci 3879bf215546Sopenharmony_ci if (!*gs_ring_state_dw_offset) { 3880bf215546Sopenharmony_ci /* We are here for the first time. The packets will be added. */ 3881bf215546Sopenharmony_ci *gs_ring_state_dw_offset = pm4->ndw; 3882bf215546Sopenharmony_ci } else { 3883bf215546Sopenharmony_ci /* We have been here before. Overwrite the previous packets. */ 3884bf215546Sopenharmony_ci old_ndw = pm4->ndw; 3885bf215546Sopenharmony_ci pm4->ndw = *gs_ring_state_dw_offset; 3886bf215546Sopenharmony_ci } 3887bf215546Sopenharmony_ci 3888bf215546Sopenharmony_ci /* Unallocated rings are written to reserve the space in the pm4 3889bf215546Sopenharmony_ci * (to be able to overwrite them later). */ 3890bf215546Sopenharmony_ci if (sctx->gfx_level >= GFX7) { 3891bf215546Sopenharmony_ci if (sctx->gfx_level <= GFX8) 3892bf215546Sopenharmony_ci si_pm4_set_reg(pm4, R_030900_VGT_ESGS_RING_SIZE, 3893bf215546Sopenharmony_ci sctx->esgs_ring ? sctx->esgs_ring->width0 / 256 : 0); 3894bf215546Sopenharmony_ci si_pm4_set_reg(pm4, R_030904_VGT_GSVS_RING_SIZE, 3895bf215546Sopenharmony_ci sctx->gsvs_ring ? sctx->gsvs_ring->width0 / 256 : 0); 3896bf215546Sopenharmony_ci } else { 3897bf215546Sopenharmony_ci si_pm4_set_reg(pm4, R_0088C8_VGT_ESGS_RING_SIZE, 3898bf215546Sopenharmony_ci sctx->esgs_ring ? sctx->esgs_ring->width0 / 256 : 0); 3899bf215546Sopenharmony_ci si_pm4_set_reg(pm4, R_0088CC_VGT_GSVS_RING_SIZE, 3900bf215546Sopenharmony_ci sctx->gsvs_ring ? sctx->gsvs_ring->width0 / 256 : 0); 3901bf215546Sopenharmony_ci } 3902bf215546Sopenharmony_ci 3903bf215546Sopenharmony_ci if (old_ndw) { 3904bf215546Sopenharmony_ci pm4->ndw = old_ndw; 3905bf215546Sopenharmony_ci pm4->last_opcode = 255; /* invalid opcode (we don't save the last opcode) */ 3906bf215546Sopenharmony_ci } 3907bf215546Sopenharmony_ci } 3908bf215546Sopenharmony_ci 3909bf215546Sopenharmony_ci /* Flush the context to re-emit both cs_preamble states. */ 3910bf215546Sopenharmony_ci sctx->last_preamble = NULL; /* flag that the preamble has changed */ 3911bf215546Sopenharmony_ci sctx->initial_gfx_cs_size = 0; /* force flush */ 3912bf215546Sopenharmony_ci si_flush_gfx_cs(sctx, RADEON_FLUSH_ASYNC_START_NEXT_GFX_IB_NOW, NULL); 3913bf215546Sopenharmony_ci 3914bf215546Sopenharmony_ci return true; 3915bf215546Sopenharmony_ci} 3916bf215546Sopenharmony_ci 3917bf215546Sopenharmony_cistatic void si_shader_lock(struct si_shader *shader) 3918bf215546Sopenharmony_ci{ 3919bf215546Sopenharmony_ci simple_mtx_lock(&shader->selector->mutex); 3920bf215546Sopenharmony_ci if (shader->previous_stage_sel) { 3921bf215546Sopenharmony_ci assert(shader->previous_stage_sel != shader->selector); 3922bf215546Sopenharmony_ci simple_mtx_lock(&shader->previous_stage_sel->mutex); 3923bf215546Sopenharmony_ci } 3924bf215546Sopenharmony_ci} 3925bf215546Sopenharmony_ci 3926bf215546Sopenharmony_cistatic void si_shader_unlock(struct si_shader *shader) 3927bf215546Sopenharmony_ci{ 3928bf215546Sopenharmony_ci if (shader->previous_stage_sel) 3929bf215546Sopenharmony_ci simple_mtx_unlock(&shader->previous_stage_sel->mutex); 3930bf215546Sopenharmony_ci simple_mtx_unlock(&shader->selector->mutex); 3931bf215546Sopenharmony_ci} 3932bf215546Sopenharmony_ci 3933bf215546Sopenharmony_ci/** 3934bf215546Sopenharmony_ci * @returns 1 if \p sel has been updated to use a new scratch buffer 3935bf215546Sopenharmony_ci * 0 if not 3936bf215546Sopenharmony_ci * < 0 if there was a failure 3937bf215546Sopenharmony_ci */ 3938bf215546Sopenharmony_cistatic int si_update_scratch_buffer(struct si_context *sctx, struct si_shader *shader) 3939bf215546Sopenharmony_ci{ 3940bf215546Sopenharmony_ci uint64_t scratch_va = sctx->scratch_buffer->gpu_address; 3941bf215546Sopenharmony_ci 3942bf215546Sopenharmony_ci if (!shader) 3943bf215546Sopenharmony_ci return 0; 3944bf215546Sopenharmony_ci 3945bf215546Sopenharmony_ci /* This shader doesn't need a scratch buffer */ 3946bf215546Sopenharmony_ci if (shader->config.scratch_bytes_per_wave == 0) 3947bf215546Sopenharmony_ci return 0; 3948bf215546Sopenharmony_ci 3949bf215546Sopenharmony_ci /* Prevent race conditions when updating: 3950bf215546Sopenharmony_ci * - si_shader::scratch_bo 3951bf215546Sopenharmony_ci * - si_shader::binary::code 3952bf215546Sopenharmony_ci * - si_shader::previous_stage::binary::code. 3953bf215546Sopenharmony_ci */ 3954bf215546Sopenharmony_ci si_shader_lock(shader); 3955bf215546Sopenharmony_ci 3956bf215546Sopenharmony_ci /* This shader is already configured to use the current 3957bf215546Sopenharmony_ci * scratch buffer. */ 3958bf215546Sopenharmony_ci if (shader->scratch_bo == sctx->scratch_buffer) { 3959bf215546Sopenharmony_ci si_shader_unlock(shader); 3960bf215546Sopenharmony_ci return 0; 3961bf215546Sopenharmony_ci } 3962bf215546Sopenharmony_ci 3963bf215546Sopenharmony_ci assert(sctx->scratch_buffer); 3964bf215546Sopenharmony_ci 3965bf215546Sopenharmony_ci /* Replace the shader bo with a new bo that has the relocs applied. */ 3966bf215546Sopenharmony_ci if (!si_shader_binary_upload(sctx->screen, shader, scratch_va)) { 3967bf215546Sopenharmony_ci si_shader_unlock(shader); 3968bf215546Sopenharmony_ci return -1; 3969bf215546Sopenharmony_ci } 3970bf215546Sopenharmony_ci 3971bf215546Sopenharmony_ci /* Update the shader state to use the new shader bo. */ 3972bf215546Sopenharmony_ci si_shader_init_pm4_state(sctx->screen, shader); 3973bf215546Sopenharmony_ci 3974bf215546Sopenharmony_ci si_resource_reference(&shader->scratch_bo, sctx->scratch_buffer); 3975bf215546Sopenharmony_ci 3976bf215546Sopenharmony_ci si_shader_unlock(shader); 3977bf215546Sopenharmony_ci return 1; 3978bf215546Sopenharmony_ci} 3979bf215546Sopenharmony_ci 3980bf215546Sopenharmony_cistatic bool si_update_scratch_relocs(struct si_context *sctx) 3981bf215546Sopenharmony_ci{ 3982bf215546Sopenharmony_ci int r; 3983bf215546Sopenharmony_ci 3984bf215546Sopenharmony_ci /* Update the shaders, so that they are using the latest scratch. 3985bf215546Sopenharmony_ci * The scratch buffer may have been changed since these shaders were 3986bf215546Sopenharmony_ci * last used, so we still need to try to update them, even if they 3987bf215546Sopenharmony_ci * require scratch buffers smaller than the current size. 3988bf215546Sopenharmony_ci */ 3989bf215546Sopenharmony_ci r = si_update_scratch_buffer(sctx, sctx->shader.ps.current); 3990bf215546Sopenharmony_ci if (r < 0) 3991bf215546Sopenharmony_ci return false; 3992bf215546Sopenharmony_ci if (r == 1) 3993bf215546Sopenharmony_ci si_pm4_bind_state(sctx, ps, sctx->shader.ps.current); 3994bf215546Sopenharmony_ci 3995bf215546Sopenharmony_ci r = si_update_scratch_buffer(sctx, sctx->shader.gs.current); 3996bf215546Sopenharmony_ci if (r < 0) 3997bf215546Sopenharmony_ci return false; 3998bf215546Sopenharmony_ci if (r == 1) 3999bf215546Sopenharmony_ci si_pm4_bind_state(sctx, gs, sctx->shader.gs.current); 4000bf215546Sopenharmony_ci 4001bf215546Sopenharmony_ci r = si_update_scratch_buffer(sctx, sctx->shader.tcs.current); 4002bf215546Sopenharmony_ci if (r < 0) 4003bf215546Sopenharmony_ci return false; 4004bf215546Sopenharmony_ci if (r == 1) 4005bf215546Sopenharmony_ci si_pm4_bind_state(sctx, hs, sctx->shader.tcs.current); 4006bf215546Sopenharmony_ci 4007bf215546Sopenharmony_ci /* VS can be bound as LS, ES, or VS. */ 4008bf215546Sopenharmony_ci r = si_update_scratch_buffer(sctx, sctx->shader.vs.current); 4009bf215546Sopenharmony_ci if (r < 0) 4010bf215546Sopenharmony_ci return false; 4011bf215546Sopenharmony_ci if (r == 1) { 4012bf215546Sopenharmony_ci if (sctx->shader.vs.current->key.ge.as_ls) 4013bf215546Sopenharmony_ci si_pm4_bind_state(sctx, ls, sctx->shader.vs.current); 4014bf215546Sopenharmony_ci else if (sctx->shader.vs.current->key.ge.as_es) 4015bf215546Sopenharmony_ci si_pm4_bind_state(sctx, es, sctx->shader.vs.current); 4016bf215546Sopenharmony_ci else if (sctx->shader.vs.current->key.ge.as_ngg) 4017bf215546Sopenharmony_ci si_pm4_bind_state(sctx, gs, sctx->shader.vs.current); 4018bf215546Sopenharmony_ci else 4019bf215546Sopenharmony_ci si_pm4_bind_state(sctx, vs, sctx->shader.vs.current); 4020bf215546Sopenharmony_ci } 4021bf215546Sopenharmony_ci 4022bf215546Sopenharmony_ci /* TES can be bound as ES or VS. */ 4023bf215546Sopenharmony_ci r = si_update_scratch_buffer(sctx, sctx->shader.tes.current); 4024bf215546Sopenharmony_ci if (r < 0) 4025bf215546Sopenharmony_ci return false; 4026bf215546Sopenharmony_ci if (r == 1) { 4027bf215546Sopenharmony_ci if (sctx->shader.tes.current->key.ge.as_es) 4028bf215546Sopenharmony_ci si_pm4_bind_state(sctx, es, sctx->shader.tes.current); 4029bf215546Sopenharmony_ci else if (sctx->shader.tes.current->key.ge.as_ngg) 4030bf215546Sopenharmony_ci si_pm4_bind_state(sctx, gs, sctx->shader.tes.current); 4031bf215546Sopenharmony_ci else 4032bf215546Sopenharmony_ci si_pm4_bind_state(sctx, vs, sctx->shader.tes.current); 4033bf215546Sopenharmony_ci } 4034bf215546Sopenharmony_ci 4035bf215546Sopenharmony_ci return true; 4036bf215546Sopenharmony_ci} 4037bf215546Sopenharmony_ci 4038bf215546Sopenharmony_cibool si_update_spi_tmpring_size(struct si_context *sctx, unsigned bytes) 4039bf215546Sopenharmony_ci{ 4040bf215546Sopenharmony_ci unsigned spi_tmpring_size; 4041bf215546Sopenharmony_ci ac_get_scratch_tmpring_size(&sctx->screen->info, bytes, 4042bf215546Sopenharmony_ci &sctx->max_seen_scratch_bytes_per_wave, &spi_tmpring_size); 4043bf215546Sopenharmony_ci 4044bf215546Sopenharmony_ci unsigned scratch_needed_size = sctx->max_seen_scratch_bytes_per_wave * 4045bf215546Sopenharmony_ci sctx->screen->info.max_scratch_waves; 4046bf215546Sopenharmony_ci 4047bf215546Sopenharmony_ci if (scratch_needed_size > 0) { 4048bf215546Sopenharmony_ci if (!sctx->scratch_buffer || scratch_needed_size > sctx->scratch_buffer->b.b.width0) { 4049bf215546Sopenharmony_ci /* Create a bigger scratch buffer */ 4050bf215546Sopenharmony_ci si_resource_reference(&sctx->scratch_buffer, NULL); 4051bf215546Sopenharmony_ci 4052bf215546Sopenharmony_ci sctx->scratch_buffer = si_aligned_buffer_create( 4053bf215546Sopenharmony_ci &sctx->screen->b, 4054bf215546Sopenharmony_ci PIPE_RESOURCE_FLAG_UNMAPPABLE | SI_RESOURCE_FLAG_DRIVER_INTERNAL | 4055bf215546Sopenharmony_ci SI_RESOURCE_FLAG_DISCARDABLE, 4056bf215546Sopenharmony_ci PIPE_USAGE_DEFAULT, scratch_needed_size, 4057bf215546Sopenharmony_ci sctx->screen->info.pte_fragment_size); 4058bf215546Sopenharmony_ci if (!sctx->scratch_buffer) 4059bf215546Sopenharmony_ci return false; 4060bf215546Sopenharmony_ci 4061bf215546Sopenharmony_ci si_context_add_resource_size(sctx, &sctx->scratch_buffer->b.b); 4062bf215546Sopenharmony_ci } 4063bf215546Sopenharmony_ci 4064bf215546Sopenharmony_ci if (sctx->gfx_level < GFX11 && !si_update_scratch_relocs(sctx)) 4065bf215546Sopenharmony_ci return false; 4066bf215546Sopenharmony_ci } 4067bf215546Sopenharmony_ci 4068bf215546Sopenharmony_ci if (spi_tmpring_size != sctx->spi_tmpring_size) { 4069bf215546Sopenharmony_ci sctx->spi_tmpring_size = spi_tmpring_size; 4070bf215546Sopenharmony_ci si_mark_atom_dirty(sctx, &sctx->atoms.s.scratch_state); 4071bf215546Sopenharmony_ci } 4072bf215546Sopenharmony_ci return true; 4073bf215546Sopenharmony_ci} 4074bf215546Sopenharmony_ci 4075bf215546Sopenharmony_civoid si_init_tess_factor_ring(struct si_context *sctx) 4076bf215546Sopenharmony_ci{ 4077bf215546Sopenharmony_ci assert(!sctx->tess_rings); 4078bf215546Sopenharmony_ci 4079bf215546Sopenharmony_ci /* The address must be aligned to 2^19, because the shader only 4080bf215546Sopenharmony_ci * receives the high 13 bits. Align it to 2MB to match the GPU page size. 4081bf215546Sopenharmony_ci */ 4082bf215546Sopenharmony_ci sctx->tess_rings = pipe_aligned_buffer_create(sctx->b.screen, 4083bf215546Sopenharmony_ci PIPE_RESOURCE_FLAG_UNMAPPABLE | 4084bf215546Sopenharmony_ci SI_RESOURCE_FLAG_32BIT | 4085bf215546Sopenharmony_ci SI_RESOURCE_FLAG_DRIVER_INTERNAL | 4086bf215546Sopenharmony_ci SI_RESOURCE_FLAG_DISCARDABLE, 4087bf215546Sopenharmony_ci PIPE_USAGE_DEFAULT, 4088bf215546Sopenharmony_ci sctx->screen->hs.tess_offchip_ring_size + 4089bf215546Sopenharmony_ci sctx->screen->hs.tess_factor_ring_size, 4090bf215546Sopenharmony_ci 2 * 1024 * 1024); 4091bf215546Sopenharmony_ci if (!sctx->tess_rings) 4092bf215546Sopenharmony_ci return; 4093bf215546Sopenharmony_ci 4094bf215546Sopenharmony_ci if (sctx->screen->info.has_tmz_support) { 4095bf215546Sopenharmony_ci sctx->tess_rings_tmz = pipe_aligned_buffer_create(sctx->b.screen, 4096bf215546Sopenharmony_ci PIPE_RESOURCE_FLAG_UNMAPPABLE | 4097bf215546Sopenharmony_ci PIPE_RESOURCE_FLAG_ENCRYPTED | 4098bf215546Sopenharmony_ci SI_RESOURCE_FLAG_32BIT | 4099bf215546Sopenharmony_ci SI_RESOURCE_FLAG_DRIVER_INTERNAL | 4100bf215546Sopenharmony_ci SI_RESOURCE_FLAG_DISCARDABLE, 4101bf215546Sopenharmony_ci PIPE_USAGE_DEFAULT, 4102bf215546Sopenharmony_ci sctx->screen->hs.tess_offchip_ring_size + 4103bf215546Sopenharmony_ci sctx->screen->hs.tess_factor_ring_size, 4104bf215546Sopenharmony_ci 2 * 1024 * 1024); 4105bf215546Sopenharmony_ci } 4106bf215546Sopenharmony_ci 4107bf215546Sopenharmony_ci uint64_t factor_va = 4108bf215546Sopenharmony_ci si_resource(sctx->tess_rings)->gpu_address + sctx->screen->hs.tess_offchip_ring_size; 4109bf215546Sopenharmony_ci 4110bf215546Sopenharmony_ci unsigned tf_ring_size_field = sctx->screen->hs.tess_factor_ring_size / 4; 4111bf215546Sopenharmony_ci if (sctx->gfx_level >= GFX11) 4112bf215546Sopenharmony_ci tf_ring_size_field /= sctx->screen->info.max_se; 4113bf215546Sopenharmony_ci 4114bf215546Sopenharmony_ci assert((tf_ring_size_field & C_030938_SIZE) == 0); 4115bf215546Sopenharmony_ci 4116bf215546Sopenharmony_ci if (sctx->shadowed_regs) { 4117bf215546Sopenharmony_ci /* These registers will be shadowed, so set them only once. */ 4118bf215546Sopenharmony_ci /* TODO: tmz + shadowed_regs support */ 4119bf215546Sopenharmony_ci struct radeon_cmdbuf *cs = &sctx->gfx_cs; 4120bf215546Sopenharmony_ci 4121bf215546Sopenharmony_ci assert(sctx->gfx_level >= GFX7); 4122bf215546Sopenharmony_ci 4123bf215546Sopenharmony_ci radeon_add_to_buffer_list(sctx, &sctx->gfx_cs, si_resource(sctx->tess_rings), 4124bf215546Sopenharmony_ci RADEON_USAGE_READWRITE | RADEON_PRIO_SHADER_RINGS); 4125bf215546Sopenharmony_ci si_emit_vgt_flush(cs); 4126bf215546Sopenharmony_ci 4127bf215546Sopenharmony_ci /* Set tessellation registers. */ 4128bf215546Sopenharmony_ci radeon_begin(cs); 4129bf215546Sopenharmony_ci radeon_set_uconfig_reg(R_030938_VGT_TF_RING_SIZE, 4130bf215546Sopenharmony_ci S_030938_SIZE(tf_ring_size_field)); 4131bf215546Sopenharmony_ci radeon_set_uconfig_reg(R_030940_VGT_TF_MEMORY_BASE, factor_va >> 8); 4132bf215546Sopenharmony_ci if (sctx->gfx_level >= GFX10) { 4133bf215546Sopenharmony_ci radeon_set_uconfig_reg(R_030984_VGT_TF_MEMORY_BASE_HI, 4134bf215546Sopenharmony_ci S_030984_BASE_HI(factor_va >> 40)); 4135bf215546Sopenharmony_ci } else if (sctx->gfx_level == GFX9) { 4136bf215546Sopenharmony_ci radeon_set_uconfig_reg(R_030944_VGT_TF_MEMORY_BASE_HI, 4137bf215546Sopenharmony_ci S_030944_BASE_HI(factor_va >> 40)); 4138bf215546Sopenharmony_ci } 4139bf215546Sopenharmony_ci radeon_set_uconfig_reg(R_03093C_VGT_HS_OFFCHIP_PARAM, 4140bf215546Sopenharmony_ci sctx->screen->hs.hs_offchip_param); 4141bf215546Sopenharmony_ci radeon_end(); 4142bf215546Sopenharmony_ci return; 4143bf215546Sopenharmony_ci } 4144bf215546Sopenharmony_ci 4145bf215546Sopenharmony_ci /* The codepath without register shadowing is below. */ 4146bf215546Sopenharmony_ci /* Add these registers to cs_preamble_state. */ 4147bf215546Sopenharmony_ci for (unsigned tmz = 0; tmz <= 1; tmz++) { 4148bf215546Sopenharmony_ci struct si_pm4_state *pm4 = tmz ? sctx->cs_preamble_state_tmz : sctx->cs_preamble_state; 4149bf215546Sopenharmony_ci struct pipe_resource *tf_ring = tmz ? sctx->tess_rings_tmz : sctx->tess_rings; 4150bf215546Sopenharmony_ci 4151bf215546Sopenharmony_ci if (!tf_ring) 4152bf215546Sopenharmony_ci continue; /* TMZ not supported */ 4153bf215546Sopenharmony_ci 4154bf215546Sopenharmony_ci uint64_t va = si_resource(tf_ring)->gpu_address + sctx->screen->hs.tess_offchip_ring_size; 4155bf215546Sopenharmony_ci 4156bf215546Sopenharmony_ci si_cs_preamble_add_vgt_flush(sctx, tmz); 4157bf215546Sopenharmony_ci 4158bf215546Sopenharmony_ci if (sctx->gfx_level >= GFX7) { 4159bf215546Sopenharmony_ci si_pm4_set_reg(pm4, R_030938_VGT_TF_RING_SIZE, S_030938_SIZE(tf_ring_size_field)); 4160bf215546Sopenharmony_ci si_pm4_set_reg(pm4, R_03093C_VGT_HS_OFFCHIP_PARAM, sctx->screen->hs.hs_offchip_param); 4161bf215546Sopenharmony_ci si_pm4_set_reg(pm4, R_030940_VGT_TF_MEMORY_BASE, va >> 8); 4162bf215546Sopenharmony_ci if (sctx->gfx_level >= GFX10) 4163bf215546Sopenharmony_ci si_pm4_set_reg(pm4, R_030984_VGT_TF_MEMORY_BASE_HI, S_030984_BASE_HI(va >> 40)); 4164bf215546Sopenharmony_ci else if (sctx->gfx_level == GFX9) 4165bf215546Sopenharmony_ci si_pm4_set_reg(pm4, R_030944_VGT_TF_MEMORY_BASE_HI, S_030944_BASE_HI(va >> 40)); 4166bf215546Sopenharmony_ci } else { 4167bf215546Sopenharmony_ci si_pm4_set_reg(pm4, R_008988_VGT_TF_RING_SIZE, S_008988_SIZE(tf_ring_size_field)); 4168bf215546Sopenharmony_ci si_pm4_set_reg(pm4, R_0089B8_VGT_TF_MEMORY_BASE, factor_va >> 8); 4169bf215546Sopenharmony_ci si_pm4_set_reg(pm4, R_0089B0_VGT_HS_OFFCHIP_PARAM, sctx->screen->hs.hs_offchip_param); 4170bf215546Sopenharmony_ci } 4171bf215546Sopenharmony_ci } 4172bf215546Sopenharmony_ci 4173bf215546Sopenharmony_ci /* Flush the context to re-emit the cs_preamble state. 4174bf215546Sopenharmony_ci * This is done only once in a lifetime of a context. 4175bf215546Sopenharmony_ci */ 4176bf215546Sopenharmony_ci sctx->last_preamble = NULL; /* flag that the preamble has changed */ 4177bf215546Sopenharmony_ci sctx->initial_gfx_cs_size = 0; /* force flush */ 4178bf215546Sopenharmony_ci si_flush_gfx_cs(sctx, RADEON_FLUSH_ASYNC_START_NEXT_GFX_IB_NOW, NULL); 4179bf215546Sopenharmony_ci} 4180bf215546Sopenharmony_ci 4181bf215546Sopenharmony_cistruct si_pm4_state *si_build_vgt_shader_config(struct si_screen *screen, union si_vgt_stages_key key) 4182bf215546Sopenharmony_ci{ 4183bf215546Sopenharmony_ci struct si_pm4_state *pm4 = CALLOC_STRUCT(si_pm4_state); 4184bf215546Sopenharmony_ci uint32_t stages = 0; 4185bf215546Sopenharmony_ci 4186bf215546Sopenharmony_ci if (key.u.tess) { 4187bf215546Sopenharmony_ci stages |= S_028B54_LS_EN(V_028B54_LS_STAGE_ON) | S_028B54_HS_EN(1) | S_028B54_DYNAMIC_HS(1); 4188bf215546Sopenharmony_ci 4189bf215546Sopenharmony_ci if (key.u.gs) 4190bf215546Sopenharmony_ci stages |= S_028B54_ES_EN(V_028B54_ES_STAGE_DS) | S_028B54_GS_EN(1); 4191bf215546Sopenharmony_ci else if (key.u.ngg) 4192bf215546Sopenharmony_ci stages |= S_028B54_ES_EN(V_028B54_ES_STAGE_DS); 4193bf215546Sopenharmony_ci else 4194bf215546Sopenharmony_ci stages |= S_028B54_VS_EN(V_028B54_VS_STAGE_DS); 4195bf215546Sopenharmony_ci } else if (key.u.gs) { 4196bf215546Sopenharmony_ci stages |= S_028B54_ES_EN(V_028B54_ES_STAGE_REAL) | S_028B54_GS_EN(1); 4197bf215546Sopenharmony_ci } else if (key.u.ngg) { 4198bf215546Sopenharmony_ci stages |= S_028B54_ES_EN(V_028B54_ES_STAGE_REAL); 4199bf215546Sopenharmony_ci } 4200bf215546Sopenharmony_ci 4201bf215546Sopenharmony_ci if (key.u.ngg) { 4202bf215546Sopenharmony_ci stages |= S_028B54_PRIMGEN_EN(1) | 4203bf215546Sopenharmony_ci S_028B54_NGG_WAVE_ID_EN(key.u.streamout) | 4204bf215546Sopenharmony_ci S_028B54_PRIMGEN_PASSTHRU_EN(key.u.ngg_passthrough) | 4205bf215546Sopenharmony_ci S_028B54_PRIMGEN_PASSTHRU_NO_MSG(key.u.ngg_passthrough && 4206bf215546Sopenharmony_ci screen->info.family >= CHIP_NAVI23); 4207bf215546Sopenharmony_ci } else if (key.u.gs) 4208bf215546Sopenharmony_ci stages |= S_028B54_VS_EN(V_028B54_VS_STAGE_COPY_SHADER); 4209bf215546Sopenharmony_ci 4210bf215546Sopenharmony_ci if (screen->info.gfx_level >= GFX9) 4211bf215546Sopenharmony_ci stages |= S_028B54_MAX_PRIMGRP_IN_WAVE(2); 4212bf215546Sopenharmony_ci 4213bf215546Sopenharmony_ci if (screen->info.gfx_level >= GFX10) { 4214bf215546Sopenharmony_ci stages |= S_028B54_HS_W32_EN(key.u.hs_wave32) | 4215bf215546Sopenharmony_ci S_028B54_GS_W32_EN(key.u.gs_wave32) | 4216bf215546Sopenharmony_ci S_028B54_VS_W32_EN(key.u.vs_wave32); 4217bf215546Sopenharmony_ci /* Legacy GS only supports Wave64. Read it as an implication. */ 4218bf215546Sopenharmony_ci assert(!(key.u.gs && !key.u.ngg) || !key.u.gs_wave32); 4219bf215546Sopenharmony_ci } 4220bf215546Sopenharmony_ci 4221bf215546Sopenharmony_ci si_pm4_set_reg(pm4, R_028B54_VGT_SHADER_STAGES_EN, stages); 4222bf215546Sopenharmony_ci return pm4; 4223bf215546Sopenharmony_ci} 4224bf215546Sopenharmony_ci 4225bf215546Sopenharmony_cistatic void si_emit_scratch_state(struct si_context *sctx) 4226bf215546Sopenharmony_ci{ 4227bf215546Sopenharmony_ci struct radeon_cmdbuf *cs = &sctx->gfx_cs; 4228bf215546Sopenharmony_ci 4229bf215546Sopenharmony_ci radeon_begin(cs); 4230bf215546Sopenharmony_ci if (sctx->gfx_level >= GFX11) { 4231bf215546Sopenharmony_ci radeon_set_context_reg_seq(R_0286E8_SPI_TMPRING_SIZE, 3); 4232bf215546Sopenharmony_ci radeon_emit(sctx->spi_tmpring_size); /* SPI_TMPRING_SIZE */ 4233bf215546Sopenharmony_ci radeon_emit(sctx->scratch_buffer->gpu_address >> 8); /* SPI_GFX_SCRATCH_BASE_LO */ 4234bf215546Sopenharmony_ci radeon_emit(sctx->scratch_buffer->gpu_address >> 40); /* SPI_GFX_SCRATCH_BASE_HI */ 4235bf215546Sopenharmony_ci } else { 4236bf215546Sopenharmony_ci radeon_set_context_reg(R_0286E8_SPI_TMPRING_SIZE, sctx->spi_tmpring_size); 4237bf215546Sopenharmony_ci } 4238bf215546Sopenharmony_ci radeon_end(); 4239bf215546Sopenharmony_ci 4240bf215546Sopenharmony_ci if (sctx->scratch_buffer) { 4241bf215546Sopenharmony_ci radeon_add_to_buffer_list(sctx, &sctx->gfx_cs, sctx->scratch_buffer, 4242bf215546Sopenharmony_ci RADEON_USAGE_READWRITE | RADEON_PRIO_SCRATCH_BUFFER); 4243bf215546Sopenharmony_ci } 4244bf215546Sopenharmony_ci} 4245bf215546Sopenharmony_ci 4246bf215546Sopenharmony_cistruct si_fixed_func_tcs_shader_key { 4247bf215546Sopenharmony_ci uint64_t outputs_written; 4248bf215546Sopenharmony_ci uint8_t vertices_out; 4249bf215546Sopenharmony_ci}; 4250bf215546Sopenharmony_ci 4251bf215546Sopenharmony_cistatic uint32_t si_fixed_func_tcs_shader_key_hash(const void *key) 4252bf215546Sopenharmony_ci{ 4253bf215546Sopenharmony_ci return _mesa_hash_data(key, sizeof(struct si_fixed_func_tcs_shader_key)); 4254bf215546Sopenharmony_ci} 4255bf215546Sopenharmony_ci 4256bf215546Sopenharmony_cistatic bool si_fixed_func_tcs_shader_key_equals(const void *a, const void *b) 4257bf215546Sopenharmony_ci{ 4258bf215546Sopenharmony_ci return memcmp(a, b, sizeof(struct si_fixed_func_tcs_shader_key)) == 0; 4259bf215546Sopenharmony_ci} 4260bf215546Sopenharmony_ci 4261bf215546Sopenharmony_cibool si_set_tcs_to_fixed_func_shader(struct si_context *sctx) 4262bf215546Sopenharmony_ci{ 4263bf215546Sopenharmony_ci if (!sctx->fixed_func_tcs_shader_cache) { 4264bf215546Sopenharmony_ci sctx->fixed_func_tcs_shader_cache = _mesa_hash_table_create( 4265bf215546Sopenharmony_ci NULL, si_fixed_func_tcs_shader_key_hash, 4266bf215546Sopenharmony_ci si_fixed_func_tcs_shader_key_equals); 4267bf215546Sopenharmony_ci } 4268bf215546Sopenharmony_ci 4269bf215546Sopenharmony_ci struct si_fixed_func_tcs_shader_key key; 4270bf215546Sopenharmony_ci key.outputs_written = sctx->shader.vs.cso->info.outputs_written; 4271bf215546Sopenharmony_ci key.vertices_out = sctx->patch_vertices; 4272bf215546Sopenharmony_ci 4273bf215546Sopenharmony_ci struct hash_entry *entry = _mesa_hash_table_search( 4274bf215546Sopenharmony_ci sctx->fixed_func_tcs_shader_cache, &key); 4275bf215546Sopenharmony_ci 4276bf215546Sopenharmony_ci struct si_shader_selector *tcs; 4277bf215546Sopenharmony_ci if (entry) 4278bf215546Sopenharmony_ci tcs = (struct si_shader_selector *)entry->data; 4279bf215546Sopenharmony_ci else { 4280bf215546Sopenharmony_ci tcs = (struct si_shader_selector *)si_create_passthrough_tcs(sctx); 4281bf215546Sopenharmony_ci if (!tcs) 4282bf215546Sopenharmony_ci return false; 4283bf215546Sopenharmony_ci _mesa_hash_table_insert(sctx->fixed_func_tcs_shader_cache, &key, (void *)tcs); 4284bf215546Sopenharmony_ci } 4285bf215546Sopenharmony_ci 4286bf215546Sopenharmony_ci sctx->shader.tcs.cso = tcs; 4287bf215546Sopenharmony_ci sctx->shader.tcs.key.ge.part.tcs.epilog.invoc0_tess_factors_are_def = 4288bf215546Sopenharmony_ci tcs->info.tessfactors_are_def_in_all_invocs; 4289bf215546Sopenharmony_ci 4290bf215546Sopenharmony_ci return true; 4291bf215546Sopenharmony_ci} 4292bf215546Sopenharmony_ci 4293bf215546Sopenharmony_civoid si_init_screen_live_shader_cache(struct si_screen *sscreen) 4294bf215546Sopenharmony_ci{ 4295bf215546Sopenharmony_ci util_live_shader_cache_init(&sscreen->live_shader_cache, si_create_shader_selector, 4296bf215546Sopenharmony_ci si_destroy_shader_selector); 4297bf215546Sopenharmony_ci} 4298bf215546Sopenharmony_ci 4299bf215546Sopenharmony_civoid si_init_shader_functions(struct si_context *sctx) 4300bf215546Sopenharmony_ci{ 4301bf215546Sopenharmony_ci sctx->atoms.s.scratch_state.emit = si_emit_scratch_state; 4302bf215546Sopenharmony_ci 4303bf215546Sopenharmony_ci sctx->b.create_vs_state = si_create_shader; 4304bf215546Sopenharmony_ci sctx->b.create_tcs_state = si_create_shader; 4305bf215546Sopenharmony_ci sctx->b.create_tes_state = si_create_shader; 4306bf215546Sopenharmony_ci sctx->b.create_gs_state = si_create_shader; 4307bf215546Sopenharmony_ci sctx->b.create_fs_state = si_create_shader; 4308bf215546Sopenharmony_ci 4309bf215546Sopenharmony_ci sctx->b.bind_vs_state = si_bind_vs_shader; 4310bf215546Sopenharmony_ci sctx->b.bind_tcs_state = si_bind_tcs_shader; 4311bf215546Sopenharmony_ci sctx->b.bind_tes_state = si_bind_tes_shader; 4312bf215546Sopenharmony_ci sctx->b.bind_gs_state = si_bind_gs_shader; 4313bf215546Sopenharmony_ci sctx->b.bind_fs_state = si_bind_ps_shader; 4314bf215546Sopenharmony_ci 4315bf215546Sopenharmony_ci sctx->b.delete_vs_state = si_delete_shader_selector; 4316bf215546Sopenharmony_ci sctx->b.delete_tcs_state = si_delete_shader_selector; 4317bf215546Sopenharmony_ci sctx->b.delete_tes_state = si_delete_shader_selector; 4318bf215546Sopenharmony_ci sctx->b.delete_gs_state = si_delete_shader_selector; 4319bf215546Sopenharmony_ci sctx->b.delete_fs_state = si_delete_shader_selector; 4320bf215546Sopenharmony_ci} 4321