Samuel Pitoiset | d4d7773 | 2017-09-01 11:41:18 +0200 | [diff] [blame] | 1 | /* |
| 2 | * Copyright © 2016 Red Hat. |
| 3 | * Copyright © 2016 Bas Nieuwenhuizen |
| 4 | * |
| 5 | * based in part on anv driver which is: |
| 6 | * Copyright © 2015 Intel Corporation |
| 7 | * |
| 8 | * Permission is hereby granted, free of charge, to any person obtaining a |
| 9 | * copy of this software and associated documentation files (the "Software"), |
| 10 | * to deal in the Software without restriction, including without limitation |
| 11 | * the rights to use, copy, modify, merge, publish, distribute, sublicense, |
| 12 | * and/or sell copies of the Software, and to permit persons to whom the |
| 13 | * Software is furnished to do so, subject to the following conditions: |
| 14 | * |
| 15 | * The above copyright notice and this permission notice (including the next |
| 16 | * paragraph) shall be included in all copies or substantial portions of the |
| 17 | * Software. |
| 18 | * |
| 19 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 20 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 21 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL |
| 22 | * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 23 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING |
| 24 | * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS |
| 25 | * IN THE SOFTWARE. |
| 26 | */ |
| 27 | |
| 28 | #include "util/mesa-sha1.h" |
| 29 | #include "util/u_atomic.h" |
| 30 | #include "radv_debug.h" |
| 31 | #include "radv_private.h" |
| 32 | #include "radv_shader.h" |
| 33 | #include "nir/nir.h" |
| 34 | #include "nir/nir_builder.h" |
| 35 | #include "spirv/nir_spirv.h" |
| 36 | |
| 37 | #include <llvm-c/Core.h> |
| 38 | #include <llvm-c/TargetMachine.h> |
| 39 | |
| 40 | #include "sid.h" |
| 41 | #include "gfx9d.h" |
Samuel Pitoiset | d4d7773 | 2017-09-01 11:41:18 +0200 | [diff] [blame] | 42 | #include "ac_binary.h" |
| 43 | #include "ac_llvm_util.h" |
| 44 | #include "ac_nir_to_llvm.h" |
| 45 | #include "vk_format.h" |
| 46 | #include "util/debug.h" |
| 47 | #include "ac_exp_param.h" |
| 48 | |
Alex Smith | de88979 | 2017-10-27 14:25:05 +0100 | [diff] [blame] | 49 | #include "util/string_buffer.h" |
| 50 | |
Samuel Pitoiset | d4d7773 | 2017-09-01 11:41:18 +0200 | [diff] [blame] | 51 | static const struct nir_shader_compiler_options nir_options = { |
| 52 | .vertex_id_zero_based = true, |
| 53 | .lower_scmp = true, |
| 54 | .lower_flrp32 = true, |
Timothy Arceri | f0d74ec | 2018-01-12 11:12:09 +1100 | [diff] [blame] | 55 | .lower_flrp64 = true, |
Bas Nieuwenhuizen | 5240fdd | 2018-01-21 17:13:26 +0100 | [diff] [blame^] | 56 | .lower_device_index_to_zero = true, |
Samuel Pitoiset | d4d7773 | 2017-09-01 11:41:18 +0200 | [diff] [blame] | 57 | .lower_fsat = true, |
| 58 | .lower_fdiv = true, |
| 59 | .lower_sub = true, |
| 60 | .lower_pack_snorm_2x16 = true, |
| 61 | .lower_pack_snorm_4x8 = true, |
| 62 | .lower_pack_unorm_2x16 = true, |
| 63 | .lower_pack_unorm_4x8 = true, |
| 64 | .lower_unpack_snorm_2x16 = true, |
| 65 | .lower_unpack_snorm_4x8 = true, |
| 66 | .lower_unpack_unorm_2x16 = true, |
| 67 | .lower_unpack_unorm_4x8 = true, |
| 68 | .lower_extract_byte = true, |
| 69 | .lower_extract_word = true, |
Dave Airlie | 2c61594 | 2017-10-04 06:33:02 +1000 | [diff] [blame] | 70 | .lower_ffma = true, |
Samuel Pitoiset | 7aa008d | 2018-02-02 19:04:57 +0100 | [diff] [blame] | 71 | .lower_fpow = true, |
Timothy Arceri | 5b8de4b | 2018-01-08 10:37:27 +1100 | [diff] [blame] | 72 | .vs_inputs_dual_locations = true, |
Samuel Pitoiset | d4d7773 | 2017-09-01 11:41:18 +0200 | [diff] [blame] | 73 | .max_unroll_iterations = 32 |
| 74 | }; |
| 75 | |
| 76 | VkResult radv_CreateShaderModule( |
| 77 | VkDevice _device, |
| 78 | const VkShaderModuleCreateInfo* pCreateInfo, |
| 79 | const VkAllocationCallbacks* pAllocator, |
| 80 | VkShaderModule* pShaderModule) |
| 81 | { |
| 82 | RADV_FROM_HANDLE(radv_device, device, _device); |
| 83 | struct radv_shader_module *module; |
| 84 | |
| 85 | assert(pCreateInfo->sType == VK_STRUCTURE_TYPE_SHADER_MODULE_CREATE_INFO); |
| 86 | assert(pCreateInfo->flags == 0); |
| 87 | |
| 88 | module = vk_alloc2(&device->alloc, pAllocator, |
| 89 | sizeof(*module) + pCreateInfo->codeSize, 8, |
| 90 | VK_SYSTEM_ALLOCATION_SCOPE_OBJECT); |
| 91 | if (module == NULL) |
| 92 | return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY); |
| 93 | |
| 94 | module->nir = NULL; |
| 95 | module->size = pCreateInfo->codeSize; |
| 96 | memcpy(module->data, pCreateInfo->pCode, module->size); |
| 97 | |
| 98 | _mesa_sha1_compute(module->data, module->size, module->sha1); |
| 99 | |
| 100 | *pShaderModule = radv_shader_module_to_handle(module); |
| 101 | |
| 102 | return VK_SUCCESS; |
| 103 | } |
| 104 | |
| 105 | void radv_DestroyShaderModule( |
| 106 | VkDevice _device, |
| 107 | VkShaderModule _module, |
| 108 | const VkAllocationCallbacks* pAllocator) |
| 109 | { |
| 110 | RADV_FROM_HANDLE(radv_device, device, _device); |
| 111 | RADV_FROM_HANDLE(radv_shader_module, module, _module); |
| 112 | |
| 113 | if (!module) |
| 114 | return; |
| 115 | |
| 116 | vk_free2(&device->alloc, pAllocator, module); |
| 117 | } |
| 118 | |
Bas Nieuwenhuizen | 06f0504 | 2017-02-09 00:12:10 +0100 | [diff] [blame] | 119 | void |
Samuel Pitoiset | d4d7773 | 2017-09-01 11:41:18 +0200 | [diff] [blame] | 120 | radv_optimize_nir(struct nir_shader *shader) |
| 121 | { |
| 122 | bool progress; |
| 123 | |
| 124 | do { |
| 125 | progress = false; |
| 126 | |
| 127 | NIR_PASS_V(shader, nir_lower_vars_to_ssa); |
| 128 | NIR_PASS_V(shader, nir_lower_64bit_pack); |
| 129 | NIR_PASS_V(shader, nir_lower_alu_to_scalar); |
| 130 | NIR_PASS_V(shader, nir_lower_phis_to_scalar); |
| 131 | |
| 132 | NIR_PASS(progress, shader, nir_copy_prop); |
| 133 | NIR_PASS(progress, shader, nir_opt_remove_phis); |
| 134 | NIR_PASS(progress, shader, nir_opt_dce); |
| 135 | if (nir_opt_trivial_continues(shader)) { |
| 136 | progress = true; |
| 137 | NIR_PASS(progress, shader, nir_copy_prop); |
Dave Airlie | 64d9bd1 | 2017-09-13 03:49:31 +0100 | [diff] [blame] | 138 | NIR_PASS(progress, shader, nir_opt_remove_phis); |
Samuel Pitoiset | d4d7773 | 2017-09-01 11:41:18 +0200 | [diff] [blame] | 139 | NIR_PASS(progress, shader, nir_opt_dce); |
| 140 | } |
| 141 | NIR_PASS(progress, shader, nir_opt_if); |
| 142 | NIR_PASS(progress, shader, nir_opt_dead_cf); |
| 143 | NIR_PASS(progress, shader, nir_opt_cse); |
| 144 | NIR_PASS(progress, shader, nir_opt_peephole_select, 8); |
| 145 | NIR_PASS(progress, shader, nir_opt_algebraic); |
| 146 | NIR_PASS(progress, shader, nir_opt_constant_folding); |
| 147 | NIR_PASS(progress, shader, nir_opt_undef); |
| 148 | NIR_PASS(progress, shader, nir_opt_conditional_discard); |
| 149 | if (shader->options->max_unroll_iterations) { |
| 150 | NIR_PASS(progress, shader, nir_opt_loop_unroll, 0); |
| 151 | } |
| 152 | } while (progress); |
Samuel Pitoiset | 3488a3f | 2018-01-29 17:19:18 +0100 | [diff] [blame] | 153 | |
| 154 | NIR_PASS(progress, shader, nir_opt_shrink_load); |
Samuel Pitoiset | d4d7773 | 2017-09-01 11:41:18 +0200 | [diff] [blame] | 155 | } |
| 156 | |
| 157 | nir_shader * |
| 158 | radv_shader_compile_to_nir(struct radv_device *device, |
| 159 | struct radv_shader_module *module, |
| 160 | const char *entrypoint_name, |
| 161 | gl_shader_stage stage, |
Samuel Pitoiset | 47efc52 | 2017-09-01 12:09:56 +0200 | [diff] [blame] | 162 | const VkSpecializationInfo *spec_info) |
Samuel Pitoiset | d4d7773 | 2017-09-01 11:41:18 +0200 | [diff] [blame] | 163 | { |
| 164 | if (strcmp(entrypoint_name, "main") != 0) { |
| 165 | radv_finishme("Multiple shaders per module not really supported"); |
| 166 | } |
| 167 | |
| 168 | nir_shader *nir; |
| 169 | nir_function *entry_point; |
| 170 | if (module->nir) { |
| 171 | /* Some things such as our meta clear/blit code will give us a NIR |
| 172 | * shader directly. In that case, we just ignore the SPIR-V entirely |
| 173 | * and just use the NIR shader */ |
| 174 | nir = module->nir; |
| 175 | nir->options = &nir_options; |
| 176 | nir_validate_shader(nir); |
| 177 | |
| 178 | assert(exec_list_length(&nir->functions) == 1); |
| 179 | struct exec_node *node = exec_list_get_head(&nir->functions); |
| 180 | entry_point = exec_node_data(nir_function, node, node); |
| 181 | } else { |
| 182 | uint32_t *spirv = (uint32_t *) module->data; |
| 183 | assert(module->size % 4 == 0); |
| 184 | |
Timothy Arceri | 7664aaf | 2017-10-11 11:59:20 +1100 | [diff] [blame] | 185 | if (device->instance->debug_flags & RADV_DEBUG_DUMP_SPIRV) |
Samuel Pitoiset | 844ae72 | 2017-09-22 16:56:40 +0200 | [diff] [blame] | 186 | radv_print_spirv(spirv, module->size, stderr); |
Samuel Pitoiset | d4d7773 | 2017-09-01 11:41:18 +0200 | [diff] [blame] | 187 | |
| 188 | uint32_t num_spec_entries = 0; |
| 189 | struct nir_spirv_specialization *spec_entries = NULL; |
| 190 | if (spec_info && spec_info->mapEntryCount > 0) { |
| 191 | num_spec_entries = spec_info->mapEntryCount; |
| 192 | spec_entries = malloc(num_spec_entries * sizeof(*spec_entries)); |
| 193 | for (uint32_t i = 0; i < num_spec_entries; i++) { |
| 194 | VkSpecializationMapEntry entry = spec_info->pMapEntries[i]; |
| 195 | const void *data = spec_info->pData + entry.offset; |
| 196 | assert(data + entry.size <= spec_info->pData + spec_info->dataSize); |
| 197 | |
| 198 | spec_entries[i].id = spec_info->pMapEntries[i].constantID; |
| 199 | if (spec_info->dataSize == 8) |
| 200 | spec_entries[i].data64 = *(const uint64_t *)data; |
| 201 | else |
| 202 | spec_entries[i].data32 = *(const uint32_t *)data; |
| 203 | } |
| 204 | } |
Jason Ekstrand | e19c623 | 2017-10-18 17:28:19 -0700 | [diff] [blame] | 205 | const struct spirv_to_nir_options spirv_options = { |
| 206 | .caps = { |
Bas Nieuwenhuizen | 5240fdd | 2018-01-21 17:13:26 +0100 | [diff] [blame^] | 207 | .device_group = true, |
Jason Ekstrand | e19c623 | 2017-10-18 17:28:19 -0700 | [diff] [blame] | 208 | .draw_parameters = true, |
| 209 | .float64 = true, |
| 210 | .image_read_without_format = true, |
| 211 | .image_write_without_format = true, |
| 212 | .tessellation = true, |
| 213 | .int64 = true, |
| 214 | .multiview = true, |
Bas Nieuwenhuizen | 8f9af58 | 2018-01-21 15:06:10 +0100 | [diff] [blame] | 215 | .subgroup_basic = true, |
Jason Ekstrand | e19c623 | 2017-10-18 17:28:19 -0700 | [diff] [blame] | 216 | .variable_pointers = true, |
| 217 | }, |
Samuel Pitoiset | d4d7773 | 2017-09-01 11:41:18 +0200 | [diff] [blame] | 218 | }; |
| 219 | entry_point = spirv_to_nir(spirv, module->size / 4, |
| 220 | spec_entries, num_spec_entries, |
Jason Ekstrand | e19c623 | 2017-10-18 17:28:19 -0700 | [diff] [blame] | 221 | stage, entrypoint_name, |
| 222 | &spirv_options, &nir_options); |
Samuel Pitoiset | d4d7773 | 2017-09-01 11:41:18 +0200 | [diff] [blame] | 223 | nir = entry_point->shader; |
Jason Ekstrand | 59fb59a | 2017-09-14 19:52:38 -0700 | [diff] [blame] | 224 | assert(nir->info.stage == stage); |
Samuel Pitoiset | d4d7773 | 2017-09-01 11:41:18 +0200 | [diff] [blame] | 225 | nir_validate_shader(nir); |
| 226 | |
| 227 | free(spec_entries); |
| 228 | |
| 229 | /* We have to lower away local constant initializers right before we |
| 230 | * inline functions. That way they get properly initialized at the top |
| 231 | * of the function and not at the top of its caller. |
| 232 | */ |
| 233 | NIR_PASS_V(nir, nir_lower_constant_initializers, nir_var_local); |
| 234 | NIR_PASS_V(nir, nir_lower_returns); |
| 235 | NIR_PASS_V(nir, nir_inline_functions); |
| 236 | |
| 237 | /* Pick off the single entrypoint that we want */ |
| 238 | foreach_list_typed_safe(nir_function, func, node, &nir->functions) { |
| 239 | if (func != entry_point) |
| 240 | exec_node_remove(&func->node); |
| 241 | } |
| 242 | assert(exec_list_length(&nir->functions) == 1); |
| 243 | entry_point->name = ralloc_strdup(entry_point, "main"); |
| 244 | |
| 245 | NIR_PASS_V(nir, nir_remove_dead_variables, |
| 246 | nir_var_shader_in | nir_var_shader_out | nir_var_system_value); |
| 247 | |
| 248 | /* Now that we've deleted all but the main function, we can go ahead and |
| 249 | * lower the rest of the constant initializers. |
| 250 | */ |
| 251 | NIR_PASS_V(nir, nir_lower_constant_initializers, ~0); |
| 252 | NIR_PASS_V(nir, nir_lower_system_values); |
| 253 | NIR_PASS_V(nir, nir_lower_clip_cull_distance_arrays); |
| 254 | } |
| 255 | |
| 256 | /* Vulkan uses the separate-shader linking model */ |
| 257 | nir->info.separate_shader = true; |
| 258 | |
| 259 | nir_shader_gather_info(nir, entry_point->impl); |
| 260 | |
Samuel Pitoiset | d4d7773 | 2017-09-01 11:41:18 +0200 | [diff] [blame] | 261 | static const nir_lower_tex_options tex_options = { |
| 262 | .lower_txp = ~0, |
| 263 | }; |
| 264 | |
| 265 | nir_lower_tex(nir, &tex_options); |
| 266 | |
| 267 | nir_lower_vars_to_ssa(nir); |
| 268 | nir_lower_var_copies(nir); |
| 269 | nir_lower_global_vars_to_local(nir); |
| 270 | nir_remove_dead_variables(nir, nir_var_local); |
Timothy Arceri | 0f2c734 | 2018-03-05 11:13:11 +1100 | [diff] [blame] | 271 | ac_lower_indirect_derefs(nir, device->physical_device->rad_info.chip_class); |
Bas Nieuwenhuizen | 8f9af58 | 2018-01-21 15:06:10 +0100 | [diff] [blame] | 272 | nir_lower_subgroups(nir, &(struct nir_lower_subgroups_options) { |
| 273 | .subgroup_size = 64, |
| 274 | .ballot_bit_size = 64, |
| 275 | .lower_to_scalar = 1, |
| 276 | .lower_subgroup_masks = 1, |
| 277 | .lower_shuffle = 1, |
| 278 | .lower_quad = 1, |
| 279 | }); |
| 280 | |
Samuel Pitoiset | d4d7773 | 2017-09-01 11:41:18 +0200 | [diff] [blame] | 281 | radv_optimize_nir(nir); |
| 282 | |
Samuel Pitoiset | d4d7773 | 2017-09-01 11:41:18 +0200 | [diff] [blame] | 283 | return nir; |
| 284 | } |
| 285 | |
| 286 | void * |
| 287 | radv_alloc_shader_memory(struct radv_device *device, |
| 288 | struct radv_shader_variant *shader) |
| 289 | { |
| 290 | mtx_lock(&device->shader_slab_mutex); |
| 291 | list_for_each_entry(struct radv_shader_slab, slab, &device->shader_slabs, slabs) { |
| 292 | uint64_t offset = 0; |
| 293 | list_for_each_entry(struct radv_shader_variant, s, &slab->shaders, slab_list) { |
| 294 | if (s->bo_offset - offset >= shader->code_size) { |
| 295 | shader->bo = slab->bo; |
| 296 | shader->bo_offset = offset; |
| 297 | list_addtail(&shader->slab_list, &s->slab_list); |
| 298 | mtx_unlock(&device->shader_slab_mutex); |
| 299 | return slab->ptr + offset; |
| 300 | } |
| 301 | offset = align_u64(s->bo_offset + s->code_size, 256); |
| 302 | } |
| 303 | if (slab->size - offset >= shader->code_size) { |
| 304 | shader->bo = slab->bo; |
| 305 | shader->bo_offset = offset; |
| 306 | list_addtail(&shader->slab_list, &slab->shaders); |
| 307 | mtx_unlock(&device->shader_slab_mutex); |
| 308 | return slab->ptr + offset; |
| 309 | } |
| 310 | } |
| 311 | |
| 312 | mtx_unlock(&device->shader_slab_mutex); |
| 313 | struct radv_shader_slab *slab = calloc(1, sizeof(struct radv_shader_slab)); |
| 314 | |
| 315 | slab->size = 256 * 1024; |
| 316 | slab->bo = device->ws->buffer_create(device->ws, slab->size, 256, |
Samuel Pitoiset | a3c2a86 | 2018-01-04 15:19:47 +0100 | [diff] [blame] | 317 | RADEON_DOMAIN_VRAM, |
| 318 | RADEON_FLAG_NO_INTERPROCESS_SHARING | |
| 319 | device->physical_device->cpdma_prefetch_writes_memory ? |
| 320 | 0 : RADEON_FLAG_READ_ONLY); |
Samuel Pitoiset | d4d7773 | 2017-09-01 11:41:18 +0200 | [diff] [blame] | 321 | slab->ptr = (char*)device->ws->buffer_map(slab->bo); |
| 322 | list_inithead(&slab->shaders); |
| 323 | |
| 324 | mtx_lock(&device->shader_slab_mutex); |
| 325 | list_add(&slab->slabs, &device->shader_slabs); |
| 326 | |
| 327 | shader->bo = slab->bo; |
| 328 | shader->bo_offset = 0; |
| 329 | list_add(&shader->slab_list, &slab->shaders); |
| 330 | mtx_unlock(&device->shader_slab_mutex); |
| 331 | return slab->ptr; |
| 332 | } |
| 333 | |
| 334 | void |
| 335 | radv_destroy_shader_slabs(struct radv_device *device) |
| 336 | { |
| 337 | list_for_each_entry_safe(struct radv_shader_slab, slab, &device->shader_slabs, slabs) { |
| 338 | device->ws->buffer_destroy(slab->bo); |
| 339 | free(slab); |
| 340 | } |
| 341 | mtx_destroy(&device->shader_slab_mutex); |
| 342 | } |
| 343 | |
| 344 | static void |
| 345 | radv_fill_shader_variant(struct radv_device *device, |
| 346 | struct radv_shader_variant *variant, |
| 347 | struct ac_shader_binary *binary, |
| 348 | gl_shader_stage stage) |
| 349 | { |
| 350 | bool scratch_enabled = variant->config.scratch_bytes_per_wave > 0; |
| 351 | unsigned vgpr_comp_cnt = 0; |
| 352 | |
| 353 | if (scratch_enabled && !device->llvm_supports_spill) |
| 354 | radv_finishme("shader scratch support only available with LLVM 4.0"); |
| 355 | |
| 356 | variant->code_size = binary->code_size; |
| 357 | variant->rsrc2 = S_00B12C_USER_SGPR(variant->info.num_user_sgprs) | |
| 358 | S_00B12C_SCRATCH_EN(scratch_enabled); |
| 359 | |
Bas Nieuwenhuizen | 228325f | 2017-10-18 00:59:16 +0200 | [diff] [blame] | 360 | variant->rsrc1 = S_00B848_VGPRS((variant->config.num_vgprs - 1) / 4) | |
| 361 | S_00B848_SGPRS((variant->config.num_sgprs - 1) / 8) | |
| 362 | S_00B848_DX10_CLAMP(1) | |
| 363 | S_00B848_FLOAT_MODE(variant->config.float_mode); |
| 364 | |
Samuel Pitoiset | d4d7773 | 2017-09-01 11:41:18 +0200 | [diff] [blame] | 365 | switch (stage) { |
| 366 | case MESA_SHADER_TESS_EVAL: |
| 367 | vgpr_comp_cnt = 3; |
Bas Nieuwenhuizen | 228325f | 2017-10-18 00:59:16 +0200 | [diff] [blame] | 368 | variant->rsrc2 |= S_00B12C_OC_LDS_EN(1); |
| 369 | break; |
Samuel Pitoiset | d4d7773 | 2017-09-01 11:41:18 +0200 | [diff] [blame] | 370 | case MESA_SHADER_TESS_CTRL: |
Bas Nieuwenhuizen | 228325f | 2017-10-18 00:59:16 +0200 | [diff] [blame] | 371 | if (device->physical_device->rad_info.chip_class >= GFX9) |
| 372 | vgpr_comp_cnt = variant->info.vs.vgpr_comp_cnt; |
| 373 | else |
| 374 | variant->rsrc2 |= S_00B12C_OC_LDS_EN(1); |
Samuel Pitoiset | d4d7773 | 2017-09-01 11:41:18 +0200 | [diff] [blame] | 375 | break; |
| 376 | case MESA_SHADER_VERTEX: |
| 377 | case MESA_SHADER_GEOMETRY: |
| 378 | vgpr_comp_cnt = variant->info.vs.vgpr_comp_cnt; |
| 379 | break; |
| 380 | case MESA_SHADER_FRAGMENT: |
| 381 | break; |
Samuel Pitoiset | 2294d35 | 2017-12-14 16:48:03 +0100 | [diff] [blame] | 382 | case MESA_SHADER_COMPUTE: { |
| 383 | struct ac_shader_info *info = &variant->info.info; |
Samuel Pitoiset | d4d7773 | 2017-09-01 11:41:18 +0200 | [diff] [blame] | 384 | variant->rsrc2 |= |
Samuel Pitoiset | 4237c3d | 2017-12-18 22:06:38 +0100 | [diff] [blame] | 385 | S_00B84C_TGID_X_EN(info->cs.uses_block_id[0]) | |
| 386 | S_00B84C_TGID_Y_EN(info->cs.uses_block_id[1]) | |
| 387 | S_00B84C_TGID_Z_EN(info->cs.uses_block_id[2]) | |
| 388 | S_00B84C_TIDIG_COMP_CNT(info->cs.uses_thread_id[2] ? 2 : |
| 389 | info->cs.uses_thread_id[1] ? 1 : 0) | |
Samuel Pitoiset | 90c3bf0 | 2017-12-14 17:32:41 +0100 | [diff] [blame] | 390 | S_00B84C_TG_SIZE_EN(info->cs.uses_local_invocation_idx) | |
Samuel Pitoiset | d4d7773 | 2017-09-01 11:41:18 +0200 | [diff] [blame] | 391 | S_00B84C_LDS_SIZE(variant->config.lds_size); |
| 392 | break; |
Samuel Pitoiset | 2294d35 | 2017-12-14 16:48:03 +0100 | [diff] [blame] | 393 | } |
Samuel Pitoiset | d4d7773 | 2017-09-01 11:41:18 +0200 | [diff] [blame] | 394 | default: |
| 395 | unreachable("unsupported shader type"); |
| 396 | break; |
| 397 | } |
| 398 | |
Bas Nieuwenhuizen | 228325f | 2017-10-18 00:59:16 +0200 | [diff] [blame] | 399 | if (device->physical_device->rad_info.chip_class >= GFX9 && |
Bas Nieuwenhuizen | 73749ca | 2017-10-20 02:24:24 +0200 | [diff] [blame] | 400 | stage == MESA_SHADER_GEOMETRY) { |
Samuel Pitoiset | 2670ebb | 2017-12-20 20:56:57 +0100 | [diff] [blame] | 401 | struct ac_shader_info *info = &variant->info.info; |
Samuel Pitoiset | 232c418 | 2018-01-09 16:01:09 +0100 | [diff] [blame] | 402 | unsigned es_type = variant->info.gs.es_type; |
Samuel Pitoiset | 4e701cf | 2018-01-09 16:01:10 +0100 | [diff] [blame] | 403 | unsigned gs_vgpr_comp_cnt, es_vgpr_comp_cnt; |
| 404 | |
| 405 | if (es_type == MESA_SHADER_VERTEX) { |
| 406 | es_vgpr_comp_cnt = variant->info.vs.vgpr_comp_cnt; |
| 407 | } else if (es_type == MESA_SHADER_TESS_EVAL) { |
| 408 | es_vgpr_comp_cnt = 3; |
| 409 | } else { |
Bas Nieuwenhuizen | 0f89f9b | 2018-01-17 23:23:02 +0100 | [diff] [blame] | 410 | unreachable("invalid shader ES type"); |
Samuel Pitoiset | 4e701cf | 2018-01-09 16:01:10 +0100 | [diff] [blame] | 411 | } |
Samuel Pitoiset | 2670ebb | 2017-12-20 20:56:57 +0100 | [diff] [blame] | 412 | |
| 413 | /* If offsets 4, 5 are used, GS_VGPR_COMP_CNT is ignored and |
| 414 | * VGPR[0:4] are always loaded. |
| 415 | */ |
| 416 | if (info->uses_invocation_id) |
| 417 | gs_vgpr_comp_cnt = 3; /* VGPR3 contains InvocationID. */ |
| 418 | else if (info->uses_prim_id) |
| 419 | gs_vgpr_comp_cnt = 2; /* VGPR2 contains PrimitiveID. */ |
Samuel Pitoiset | b462ceb | 2018-01-05 17:18:52 +0100 | [diff] [blame] | 420 | else if (variant->info.gs.vertices_in >= 3) |
| 421 | gs_vgpr_comp_cnt = 1; /* VGPR1 contains offsets 2, 3 */ |
Samuel Pitoiset | 2670ebb | 2017-12-20 20:56:57 +0100 | [diff] [blame] | 422 | else |
Samuel Pitoiset | b462ceb | 2018-01-05 17:18:52 +0100 | [diff] [blame] | 423 | gs_vgpr_comp_cnt = 0; /* VGPR0 contains offsets 0, 1 */ |
Samuel Pitoiset | 2670ebb | 2017-12-20 20:56:57 +0100 | [diff] [blame] | 424 | |
Samuel Pitoiset | 2670ebb | 2017-12-20 20:56:57 +0100 | [diff] [blame] | 425 | variant->rsrc1 |= S_00B228_GS_VGPR_COMP_CNT(gs_vgpr_comp_cnt); |
Samuel Pitoiset | 4e701cf | 2018-01-09 16:01:10 +0100 | [diff] [blame] | 426 | variant->rsrc2 |= S_00B22C_ES_VGPR_COMP_CNT(es_vgpr_comp_cnt) | |
Samuel Pitoiset | 232c418 | 2018-01-09 16:01:09 +0100 | [diff] [blame] | 427 | S_00B22C_OC_LDS_EN(es_type == MESA_SHADER_TESS_EVAL); |
Bas Nieuwenhuizen | 73749ca | 2017-10-20 02:24:24 +0200 | [diff] [blame] | 428 | } else if (device->physical_device->rad_info.chip_class >= GFX9 && |
Bas Nieuwenhuizen | 228325f | 2017-10-18 00:59:16 +0200 | [diff] [blame] | 429 | stage == MESA_SHADER_TESS_CTRL) |
| 430 | variant->rsrc1 |= S_00B428_LS_VGPR_COMP_CNT(vgpr_comp_cnt); |
| 431 | else |
| 432 | variant->rsrc1 |= S_00B128_VGPR_COMP_CNT(vgpr_comp_cnt); |
Samuel Pitoiset | d4d7773 | 2017-09-01 11:41:18 +0200 | [diff] [blame] | 433 | |
| 434 | void *ptr = radv_alloc_shader_memory(device, variant); |
| 435 | memcpy(ptr, binary->code, binary->code_size); |
| 436 | } |
| 437 | |
Samuel Pitoiset | 92db23f | 2017-09-01 16:51:12 +0200 | [diff] [blame] | 438 | static struct radv_shader_variant * |
| 439 | shader_variant_create(struct radv_device *device, |
Samuel Pitoiset | a2a350a | 2017-09-22 16:44:08 +0200 | [diff] [blame] | 440 | struct radv_shader_module *module, |
Bas Nieuwenhuizen | ce03c11 | 2017-10-16 13:18:02 +0200 | [diff] [blame] | 441 | struct nir_shader * const *shaders, |
| 442 | int shader_count, |
Samuel Pitoiset | 92db23f | 2017-09-01 16:51:12 +0200 | [diff] [blame] | 443 | gl_shader_stage stage, |
| 444 | struct ac_nir_compiler_options *options, |
| 445 | bool gs_copy_shader, |
| 446 | void **code_out, |
| 447 | unsigned *code_size_out) |
Samuel Pitoiset | d4d7773 | 2017-09-01 11:41:18 +0200 | [diff] [blame] | 448 | { |
Samuel Pitoiset | d4d7773 | 2017-09-01 11:41:18 +0200 | [diff] [blame] | 449 | enum radeon_family chip_family = device->physical_device->rad_info.family; |
Samuel Pitoiset | 921986b | 2017-11-30 22:16:09 +0100 | [diff] [blame] | 450 | bool dump_shaders = radv_can_dump_shader(device, module); |
Samuel Pitoiset | 92db23f | 2017-09-01 16:51:12 +0200 | [diff] [blame] | 451 | enum ac_target_machine_options tm_options = 0; |
| 452 | struct radv_shader_variant *variant; |
| 453 | struct ac_shader_binary binary; |
Samuel Pitoiset | d4d7773 | 2017-09-01 11:41:18 +0200 | [diff] [blame] | 454 | LLVMTargetMachineRef tm; |
Samuel Pitoiset | 92db23f | 2017-09-01 16:51:12 +0200 | [diff] [blame] | 455 | |
| 456 | variant = calloc(1, sizeof(struct radv_shader_variant)); |
Samuel Pitoiset | d4d7773 | 2017-09-01 11:41:18 +0200 | [diff] [blame] | 457 | if (!variant) |
| 458 | return NULL; |
| 459 | |
Samuel Pitoiset | 92db23f | 2017-09-01 16:51:12 +0200 | [diff] [blame] | 460 | options->family = chip_family; |
| 461 | options->chip_class = device->physical_device->rad_info.chip_class; |
Samuel Pitoiset | 33e6e5e | 2018-01-19 12:12:02 +0100 | [diff] [blame] | 462 | options->dump_preoptir = radv_can_dump_shader(device, module) && |
| 463 | device->instance->debug_flags & RADV_DEBUG_PREOPTIR; |
Samuel Pitoiset | d4d7773 | 2017-09-01 11:41:18 +0200 | [diff] [blame] | 464 | |
Samuel Pitoiset | 92db23f | 2017-09-01 16:51:12 +0200 | [diff] [blame] | 465 | if (options->supports_spill) |
Samuel Pitoiset | d4d7773 | 2017-09-01 11:41:18 +0200 | [diff] [blame] | 466 | tm_options |= AC_TM_SUPPORTS_SPILL; |
| 467 | if (device->instance->perftest_flags & RADV_PERFTEST_SISCHED) |
| 468 | tm_options |= AC_TM_SISCHED; |
| 469 | tm = ac_create_target_machine(chip_family, tm_options); |
Samuel Pitoiset | 92db23f | 2017-09-01 16:51:12 +0200 | [diff] [blame] | 470 | |
| 471 | if (gs_copy_shader) { |
Bas Nieuwenhuizen | ce03c11 | 2017-10-16 13:18:02 +0200 | [diff] [blame] | 472 | assert(shader_count == 1); |
| 473 | ac_create_gs_copy_shader(tm, *shaders, &binary, &variant->config, |
Samuel Pitoiset | 92db23f | 2017-09-01 16:51:12 +0200 | [diff] [blame] | 474 | &variant->info, options, dump_shaders); |
| 475 | } else { |
| 476 | ac_compile_nir_shader(tm, &binary, &variant->config, |
Bas Nieuwenhuizen | ce03c11 | 2017-10-16 13:18:02 +0200 | [diff] [blame] | 477 | &variant->info, shaders, shader_count, options, |
Samuel Pitoiset | 92db23f | 2017-09-01 16:51:12 +0200 | [diff] [blame] | 478 | dump_shaders); |
| 479 | } |
| 480 | |
Samuel Pitoiset | d4d7773 | 2017-09-01 11:41:18 +0200 | [diff] [blame] | 481 | LLVMDisposeTargetMachine(tm); |
| 482 | |
Samuel Pitoiset | 92db23f | 2017-09-01 16:51:12 +0200 | [diff] [blame] | 483 | radv_fill_shader_variant(device, variant, &binary, stage); |
Samuel Pitoiset | d4d7773 | 2017-09-01 11:41:18 +0200 | [diff] [blame] | 484 | |
| 485 | if (code_out) { |
| 486 | *code_out = binary.code; |
| 487 | *code_size_out = binary.code_size; |
| 488 | } else |
| 489 | free(binary.code); |
| 490 | free(binary.config); |
| 491 | free(binary.rodata); |
| 492 | free(binary.global_symbol_offsets); |
| 493 | free(binary.relocs); |
Samuel Pitoiset | d4d7773 | 2017-09-01 11:41:18 +0200 | [diff] [blame] | 494 | variant->ref_count = 1; |
Samuel Pitoiset | 885d757 | 2017-09-01 13:45:33 +0200 | [diff] [blame] | 495 | |
Alex Smith | de88979 | 2017-10-27 14:25:05 +0100 | [diff] [blame] | 496 | if (device->keep_shader_info) { |
Samuel Pitoiset | 885d757 | 2017-09-01 13:45:33 +0200 | [diff] [blame] | 497 | variant->disasm_string = binary.disasm_string; |
Samuel Pitoiset | a2a350a | 2017-09-22 16:44:08 +0200 | [diff] [blame] | 498 | if (!gs_copy_shader && !module->nir) { |
Bas Nieuwenhuizen | ce03c11 | 2017-10-16 13:18:02 +0200 | [diff] [blame] | 499 | variant->nir = *shaders; |
Samuel Pitoiset | 844ae72 | 2017-09-22 16:56:40 +0200 | [diff] [blame] | 500 | variant->spirv = (uint32_t *)module->data; |
| 501 | variant->spirv_size = module->size; |
Samuel Pitoiset | a2a350a | 2017-09-22 16:44:08 +0200 | [diff] [blame] | 502 | } |
Samuel Pitoiset | 885d757 | 2017-09-01 13:45:33 +0200 | [diff] [blame] | 503 | } else { |
| 504 | free(binary.disasm_string); |
| 505 | } |
| 506 | |
Samuel Pitoiset | d4d7773 | 2017-09-01 11:41:18 +0200 | [diff] [blame] | 507 | return variant; |
| 508 | } |
| 509 | |
| 510 | struct radv_shader_variant * |
Samuel Pitoiset | 92db23f | 2017-09-01 16:51:12 +0200 | [diff] [blame] | 511 | radv_shader_variant_create(struct radv_device *device, |
Samuel Pitoiset | a2a350a | 2017-09-22 16:44:08 +0200 | [diff] [blame] | 512 | struct radv_shader_module *module, |
Bas Nieuwenhuizen | ce03c11 | 2017-10-16 13:18:02 +0200 | [diff] [blame] | 513 | struct nir_shader *const *shaders, |
| 514 | int shader_count, |
Samuel Pitoiset | 92db23f | 2017-09-01 16:51:12 +0200 | [diff] [blame] | 515 | struct radv_pipeline_layout *layout, |
| 516 | const struct ac_shader_variant_key *key, |
| 517 | void **code_out, |
| 518 | unsigned *code_size_out) |
| 519 | { |
| 520 | struct ac_nir_compiler_options options = {0}; |
| 521 | |
| 522 | options.layout = layout; |
| 523 | if (key) |
| 524 | options.key = *key; |
| 525 | |
Timothy Arceri | 7664aaf | 2017-10-11 11:59:20 +1100 | [diff] [blame] | 526 | options.unsafe_math = !!(device->instance->debug_flags & RADV_DEBUG_UNSAFE_MATH); |
Samuel Pitoiset | 92db23f | 2017-09-01 16:51:12 +0200 | [diff] [blame] | 527 | options.supports_spill = device->llvm_supports_spill; |
| 528 | |
Jason Ekstrand | 59fb59a | 2017-09-14 19:52:38 -0700 | [diff] [blame] | 529 | return shader_variant_create(device, module, shaders, shader_count, shaders[shader_count - 1]->info.stage, |
Samuel Pitoiset | 92db23f | 2017-09-01 16:51:12 +0200 | [diff] [blame] | 530 | &options, false, code_out, code_size_out); |
| 531 | } |
| 532 | |
| 533 | struct radv_shader_variant * |
| 534 | radv_create_gs_copy_shader(struct radv_device *device, |
| 535 | struct nir_shader *shader, |
| 536 | void **code_out, |
| 537 | unsigned *code_size_out, |
Samuel Pitoiset | 47efc52 | 2017-09-01 12:09:56 +0200 | [diff] [blame] | 538 | bool multiview) |
Samuel Pitoiset | d4d7773 | 2017-09-01 11:41:18 +0200 | [diff] [blame] | 539 | { |
Samuel Pitoiset | d4d7773 | 2017-09-01 11:41:18 +0200 | [diff] [blame] | 540 | struct ac_nir_compiler_options options = {0}; |
Samuel Pitoiset | 92db23f | 2017-09-01 16:51:12 +0200 | [diff] [blame] | 541 | |
Samuel Pitoiset | d4d7773 | 2017-09-01 11:41:18 +0200 | [diff] [blame] | 542 | options.key.has_multiview_view_index = multiview; |
Samuel Pitoiset | d4d7773 | 2017-09-01 11:41:18 +0200 | [diff] [blame] | 543 | |
Bas Nieuwenhuizen | ce03c11 | 2017-10-16 13:18:02 +0200 | [diff] [blame] | 544 | return shader_variant_create(device, NULL, &shader, 1, MESA_SHADER_VERTEX, |
Samuel Pitoiset | 92db23f | 2017-09-01 16:51:12 +0200 | [diff] [blame] | 545 | &options, true, code_out, code_size_out); |
Samuel Pitoiset | d4d7773 | 2017-09-01 11:41:18 +0200 | [diff] [blame] | 546 | } |
| 547 | |
| 548 | void |
| 549 | radv_shader_variant_destroy(struct radv_device *device, |
| 550 | struct radv_shader_variant *variant) |
| 551 | { |
| 552 | if (!p_atomic_dec_zero(&variant->ref_count)) |
| 553 | return; |
| 554 | |
| 555 | mtx_lock(&device->shader_slab_mutex); |
| 556 | list_del(&variant->slab_list); |
| 557 | mtx_unlock(&device->shader_slab_mutex); |
| 558 | |
Samuel Pitoiset | a2a350a | 2017-09-22 16:44:08 +0200 | [diff] [blame] | 559 | ralloc_free(variant->nir); |
Samuel Pitoiset | 885d757 | 2017-09-01 13:45:33 +0200 | [diff] [blame] | 560 | free(variant->disasm_string); |
Samuel Pitoiset | d4d7773 | 2017-09-01 11:41:18 +0200 | [diff] [blame] | 561 | free(variant); |
| 562 | } |
| 563 | |
Samuel Pitoiset | d4d7773 | 2017-09-01 11:41:18 +0200 | [diff] [blame] | 564 | const char * |
| 565 | radv_get_shader_name(struct radv_shader_variant *var, gl_shader_stage stage) |
| 566 | { |
| 567 | switch (stage) { |
| 568 | case MESA_SHADER_VERTEX: return var->info.vs.as_ls ? "Vertex Shader as LS" : var->info.vs.as_es ? "Vertex Shader as ES" : "Vertex Shader as VS"; |
| 569 | case MESA_SHADER_GEOMETRY: return "Geometry Shader"; |
| 570 | case MESA_SHADER_FRAGMENT: return "Pixel Shader"; |
| 571 | case MESA_SHADER_COMPUTE: return "Compute Shader"; |
| 572 | case MESA_SHADER_TESS_CTRL: return "Tessellation Control Shader"; |
| 573 | case MESA_SHADER_TESS_EVAL: return var->info.tes.as_es ? "Tessellation Evaluation Shader as ES" : "Tessellation Evaluation Shader as VS"; |
| 574 | default: |
| 575 | return "Unknown shader"; |
| 576 | }; |
| 577 | } |
| 578 | |
Alex Smith | de88979 | 2017-10-27 14:25:05 +0100 | [diff] [blame] | 579 | static uint32_t |
| 580 | get_total_sgprs(struct radv_device *device) |
| 581 | { |
| 582 | if (device->physical_device->rad_info.chip_class >= VI) |
| 583 | return 800; |
| 584 | else |
| 585 | return 512; |
| 586 | } |
| 587 | |
| 588 | static void |
| 589 | generate_shader_stats(struct radv_device *device, |
| 590 | struct radv_shader_variant *variant, |
| 591 | gl_shader_stage stage, |
| 592 | struct _mesa_string_buffer *buf) |
Samuel Pitoiset | 80b8d9f | 2017-09-05 15:34:07 +0200 | [diff] [blame] | 593 | { |
| 594 | unsigned lds_increment = device->physical_device->rad_info.chip_class >= CIK ? 512 : 256; |
| 595 | struct ac_shader_config *conf; |
| 596 | unsigned max_simd_waves; |
| 597 | unsigned lds_per_wave = 0; |
| 598 | |
| 599 | switch (device->physical_device->rad_info.family) { |
| 600 | /* These always have 8 waves: */ |
| 601 | case CHIP_POLARIS10: |
| 602 | case CHIP_POLARIS11: |
| 603 | case CHIP_POLARIS12: |
| 604 | max_simd_waves = 8; |
| 605 | break; |
| 606 | default: |
| 607 | max_simd_waves = 10; |
| 608 | } |
| 609 | |
| 610 | conf = &variant->config; |
| 611 | |
| 612 | if (stage == MESA_SHADER_FRAGMENT) { |
| 613 | lds_per_wave = conf->lds_size * lds_increment + |
| 614 | align(variant->info.fs.num_interp * 48, |
| 615 | lds_increment); |
| 616 | } |
| 617 | |
Alex Smith | de88979 | 2017-10-27 14:25:05 +0100 | [diff] [blame] | 618 | if (conf->num_sgprs) |
| 619 | max_simd_waves = MIN2(max_simd_waves, get_total_sgprs(device) / conf->num_sgprs); |
Samuel Pitoiset | 80b8d9f | 2017-09-05 15:34:07 +0200 | [diff] [blame] | 620 | |
| 621 | if (conf->num_vgprs) |
| 622 | max_simd_waves = MIN2(max_simd_waves, 256 / conf->num_vgprs); |
| 623 | |
| 624 | /* LDS is 64KB per CU (4 SIMDs), divided into 16KB blocks per SIMD |
| 625 | * that PS can use. |
| 626 | */ |
| 627 | if (lds_per_wave) |
| 628 | max_simd_waves = MIN2(max_simd_waves, 16384 / lds_per_wave); |
| 629 | |
Samuel Pitoiset | 80b8d9f | 2017-09-05 15:34:07 +0200 | [diff] [blame] | 630 | if (stage == MESA_SHADER_FRAGMENT) { |
Alex Smith | de88979 | 2017-10-27 14:25:05 +0100 | [diff] [blame] | 631 | _mesa_string_buffer_printf(buf, "*** SHADER CONFIG ***\n" |
| 632 | "SPI_PS_INPUT_ADDR = 0x%04x\n" |
| 633 | "SPI_PS_INPUT_ENA = 0x%04x\n", |
| 634 | conf->spi_ps_input_addr, conf->spi_ps_input_ena); |
Samuel Pitoiset | 80b8d9f | 2017-09-05 15:34:07 +0200 | [diff] [blame] | 635 | } |
| 636 | |
Alex Smith | de88979 | 2017-10-27 14:25:05 +0100 | [diff] [blame] | 637 | _mesa_string_buffer_printf(buf, "*** SHADER STATS ***\n" |
| 638 | "SGPRS: %d\n" |
| 639 | "VGPRS: %d\n" |
| 640 | "Spilled SGPRs: %d\n" |
| 641 | "Spilled VGPRs: %d\n" |
Samuel Pitoiset | e96e6f6 | 2018-03-01 22:12:56 +0100 | [diff] [blame] | 642 | "PrivMem VGPRS: %d\n" |
Alex Smith | de88979 | 2017-10-27 14:25:05 +0100 | [diff] [blame] | 643 | "Code Size: %d bytes\n" |
| 644 | "LDS: %d blocks\n" |
| 645 | "Scratch: %d bytes per wave\n" |
| 646 | "Max Waves: %d\n" |
| 647 | "********************\n\n\n", |
| 648 | conf->num_sgprs, conf->num_vgprs, |
Samuel Pitoiset | e96e6f6 | 2018-03-01 22:12:56 +0100 | [diff] [blame] | 649 | conf->spilled_sgprs, conf->spilled_vgprs, |
| 650 | variant->info.private_mem_vgprs, variant->code_size, |
Alex Smith | de88979 | 2017-10-27 14:25:05 +0100 | [diff] [blame] | 651 | conf->lds_size, conf->scratch_bytes_per_wave, |
| 652 | max_simd_waves); |
| 653 | } |
| 654 | |
| 655 | void |
| 656 | radv_shader_dump_stats(struct radv_device *device, |
| 657 | struct radv_shader_variant *variant, |
| 658 | gl_shader_stage stage, |
| 659 | FILE *file) |
| 660 | { |
| 661 | struct _mesa_string_buffer *buf = _mesa_string_buffer_create(NULL, 256); |
| 662 | |
| 663 | generate_shader_stats(device, variant, stage, buf); |
| 664 | |
| 665 | fprintf(file, "\n%s:\n", radv_get_shader_name(variant, stage)); |
Alex Smith | 134a40d | 2017-10-30 08:38:14 +0000 | [diff] [blame] | 666 | fprintf(file, "%s", buf->buf); |
Alex Smith | de88979 | 2017-10-27 14:25:05 +0100 | [diff] [blame] | 667 | |
| 668 | _mesa_string_buffer_destroy(buf); |
| 669 | } |
| 670 | |
| 671 | VkResult |
| 672 | radv_GetShaderInfoAMD(VkDevice _device, |
| 673 | VkPipeline _pipeline, |
| 674 | VkShaderStageFlagBits shaderStage, |
| 675 | VkShaderInfoTypeAMD infoType, |
| 676 | size_t* pInfoSize, |
| 677 | void* pInfo) |
| 678 | { |
| 679 | RADV_FROM_HANDLE(radv_device, device, _device); |
| 680 | RADV_FROM_HANDLE(radv_pipeline, pipeline, _pipeline); |
| 681 | gl_shader_stage stage = vk_to_mesa_shader_stage(shaderStage); |
| 682 | struct radv_shader_variant *variant = pipeline->shaders[stage]; |
| 683 | struct _mesa_string_buffer *buf; |
| 684 | VkResult result = VK_SUCCESS; |
| 685 | |
| 686 | /* Spec doesn't indicate what to do if the stage is invalid, so just |
| 687 | * return no info for this. */ |
| 688 | if (!variant) |
Samuel Pitoiset | cd64a4f | 2017-11-10 09:17:58 +0100 | [diff] [blame] | 689 | return vk_error(VK_ERROR_FEATURE_NOT_PRESENT); |
Alex Smith | de88979 | 2017-10-27 14:25:05 +0100 | [diff] [blame] | 690 | |
| 691 | switch (infoType) { |
| 692 | case VK_SHADER_INFO_TYPE_STATISTICS_AMD: |
| 693 | if (!pInfo) { |
| 694 | *pInfoSize = sizeof(VkShaderStatisticsInfoAMD); |
| 695 | } else { |
| 696 | unsigned lds_multiplier = device->physical_device->rad_info.chip_class >= CIK ? 512 : 256; |
| 697 | struct ac_shader_config *conf = &variant->config; |
| 698 | |
| 699 | VkShaderStatisticsInfoAMD statistics = {}; |
| 700 | statistics.shaderStageMask = shaderStage; |
| 701 | statistics.numPhysicalVgprs = 256; |
| 702 | statistics.numPhysicalSgprs = get_total_sgprs(device); |
| 703 | statistics.numAvailableSgprs = statistics.numPhysicalSgprs; |
| 704 | |
| 705 | if (stage == MESA_SHADER_COMPUTE) { |
| 706 | unsigned *local_size = variant->nir->info.cs.local_size; |
| 707 | unsigned workgroup_size = local_size[0] * local_size[1] * local_size[2]; |
| 708 | |
| 709 | statistics.numAvailableVgprs = statistics.numPhysicalVgprs / |
| 710 | ceil(workgroup_size / statistics.numPhysicalVgprs); |
| 711 | |
| 712 | statistics.computeWorkGroupSize[0] = local_size[0]; |
| 713 | statistics.computeWorkGroupSize[1] = local_size[1]; |
| 714 | statistics.computeWorkGroupSize[2] = local_size[2]; |
| 715 | } else { |
| 716 | statistics.numAvailableVgprs = statistics.numPhysicalVgprs; |
| 717 | } |
| 718 | |
| 719 | statistics.resourceUsage.numUsedVgprs = conf->num_vgprs; |
| 720 | statistics.resourceUsage.numUsedSgprs = conf->num_sgprs; |
| 721 | statistics.resourceUsage.ldsSizePerLocalWorkGroup = 32768; |
| 722 | statistics.resourceUsage.ldsUsageSizeInBytes = conf->lds_size * lds_multiplier; |
| 723 | statistics.resourceUsage.scratchMemUsageInBytes = conf->scratch_bytes_per_wave; |
| 724 | |
| 725 | size_t size = *pInfoSize; |
| 726 | *pInfoSize = sizeof(statistics); |
| 727 | |
| 728 | memcpy(pInfo, &statistics, MIN2(size, *pInfoSize)); |
| 729 | |
| 730 | if (size < *pInfoSize) |
| 731 | result = VK_INCOMPLETE; |
| 732 | } |
| 733 | |
| 734 | break; |
| 735 | case VK_SHADER_INFO_TYPE_DISASSEMBLY_AMD: |
| 736 | buf = _mesa_string_buffer_create(NULL, 1024); |
| 737 | |
| 738 | _mesa_string_buffer_printf(buf, "%s:\n", radv_get_shader_name(variant, stage)); |
| 739 | _mesa_string_buffer_printf(buf, "%s\n\n", variant->disasm_string); |
| 740 | generate_shader_stats(device, variant, stage, buf); |
| 741 | |
| 742 | /* Need to include the null terminator. */ |
| 743 | size_t length = buf->length + 1; |
| 744 | |
| 745 | if (!pInfo) { |
| 746 | *pInfoSize = length; |
| 747 | } else { |
| 748 | size_t size = *pInfoSize; |
| 749 | *pInfoSize = length; |
| 750 | |
| 751 | memcpy(pInfo, buf->buf, MIN2(size, length)); |
| 752 | |
| 753 | if (size < length) |
| 754 | result = VK_INCOMPLETE; |
| 755 | } |
| 756 | |
| 757 | _mesa_string_buffer_destroy(buf); |
| 758 | break; |
| 759 | default: |
| 760 | /* VK_SHADER_INFO_TYPE_BINARY_AMD unimplemented for now. */ |
| 761 | result = VK_ERROR_FEATURE_NOT_PRESENT; |
| 762 | break; |
| 763 | } |
| 764 | |
| 765 | return result; |
Samuel Pitoiset | 80b8d9f | 2017-09-05 15:34:07 +0200 | [diff] [blame] | 766 | } |