From a2b8a92c72318f1bd8a7a50fe89a87c8a7594c28 Mon Sep 17 00:00:00 2001 From: Samuel Pitoiset Date: Tue, 19 Jul 2022 14:26:20 +0200 Subject: [PATCH] radv: rework shaders ref counting Introduce helpers like for descriptor set layouts. This will also help graphics pipeline libraries. Signed-off-by: Samuel Pitoiset Reviewed-By: Tatsuyuki Ishi Reviewed-by: Bas Nieuwenhuizen Part-of: --- src/amd/vulkan/radv_pipeline.c | 4 ++-- src/amd/vulkan/radv_pipeline_cache.c | 10 +++++----- src/amd/vulkan/radv_shader.c | 7 +++---- src/amd/vulkan/radv_shader.h | 16 ++++++++++++++++ 4 files changed, 26 insertions(+), 11 deletions(-) diff --git a/src/amd/vulkan/radv_pipeline.c b/src/amd/vulkan/radv_pipeline.c index 4081c37bc11..b6758802891 100644 --- a/src/amd/vulkan/radv_pipeline.c +++ b/src/amd/vulkan/radv_pipeline.c @@ -217,10 +217,10 @@ radv_pipeline_destroy(struct radv_device *device, struct radv_pipeline *pipeline for (unsigned i = 0; i < MESA_VULKAN_SHADER_STAGES; ++i) if (pipeline->shaders[i]) - radv_shader_destroy(device, pipeline->shaders[i]); + radv_shader_unref(device, pipeline->shaders[i]); if (pipeline->gs_copy_shader) - radv_shader_destroy(device, pipeline->gs_copy_shader); + radv_shader_unref(device, pipeline->gs_copy_shader); if (pipeline->cs.buf) free(pipeline->cs.buf); diff --git a/src/amd/vulkan/radv_pipeline_cache.c b/src/amd/vulkan/radv_pipeline_cache.c index 2d73211925f..70b2b8a7388 100644 --- a/src/amd/vulkan/radv_pipeline_cache.c +++ b/src/amd/vulkan/radv_pipeline_cache.c @@ -104,7 +104,7 @@ radv_pipeline_cache_finish(struct radv_pipeline_cache *cache) if (cache->hash_table[i]) { for (int j = 0; j < MESA_VULKAN_SHADER_STAGES; ++j) { if (cache->hash_table[i]->shaders[j]) - radv_shader_destroy(cache->device, cache->hash_table[i]->shaders[j]); + radv_shader_unref(cache->device, cache->hash_table[i]->shaders[j]); } if (cache->hash_table[i]->slab) radv_pipeline_slab_destroy(cache->device, cache->hash_table[i]->slab); @@ -432,7 +432,7 @@ radv_create_shaders_from_pipeline_cache( else { for (int i = 0; i < MESA_VULKAN_SHADER_STAGES; ++i) if (entry->shaders[i]) - p_atomic_inc(&entry->shaders[i]->ref_count); + radv_shader_ref(entry->shaders[i]); p_atomic_inc(&entry->slab->ref_count); } @@ -458,10 +458,10 @@ radv_pipeline_cache_insert_shaders(struct radv_device *device, struct radv_pipel if (!entry->shaders[i]) continue; - radv_shader_destroy(cache->device, pipeline->shaders[i]); + radv_shader_unref(cache->device, pipeline->shaders[i]); pipeline->shaders[i] = entry->shaders[i]; - p_atomic_inc(&pipeline->shaders[i]->ref_count); + radv_shader_ref(pipeline->shaders[i]); } radv_pipeline_slab_destroy(cache->device, pipeline->slab); @@ -548,7 +548,7 @@ radv_pipeline_cache_insert_shaders(struct radv_device *device, struct radv_pipel continue; entry->shaders[i] = pipeline->shaders[i]; - p_atomic_inc(&pipeline->shaders[i]->ref_count); + radv_shader_ref(pipeline->shaders[i]); } entry->slab = pipeline->slab; diff --git a/src/amd/vulkan/radv_shader.c b/src/amd/vulkan/radv_shader.c index d389238a033..7da2915bf62 100644 --- a/src/amd/vulkan/radv_shader.c +++ b/src/amd/vulkan/radv_shader.c @@ -1955,7 +1955,7 @@ radv_shader_binary_upload(struct radv_device *device, const struct radv_shader_b }; if (!ac_rtld_upload(&info)) { - radv_shader_destroy(device, shader); + radv_shader_unref(device, shader); ac_rtld_close(&rtld_binary); return false; } @@ -2048,7 +2048,7 @@ radv_shader_create(struct radv_device *device, const struct radv_shader_binary * size_t disasm_size; if (!ac_rtld_get_section_by_name(&rtld_binary, ".AMDGPU.disasm", &disasm_data, &disasm_size)) { - radv_shader_destroy(device, shader); + radv_shader_unref(device, shader); ac_rtld_close(&rtld_binary); return NULL; } @@ -2503,8 +2503,7 @@ radv_create_ps_epilog(struct radv_device *device, const struct radv_ps_epilog_ke void radv_shader_destroy(struct radv_device *device, struct radv_shader *shader) { - if (!p_atomic_dec_zero(&shader->ref_count)) - return; + assert(shader->ref_count == 0); free(shader->spirv); free(shader->nir_string); diff --git a/src/amd/vulkan/radv_shader.h b/src/amd/vulkan/radv_shader.h index 693f72b1d06..16ac499e56b 100644 --- a/src/amd/vulkan/radv_shader.h +++ b/src/amd/vulkan/radv_shader.h @@ -621,6 +621,22 @@ bool radv_can_dump_shader_stats(struct radv_device *device, nir_shader *nir); VkResult radv_dump_shader_stats(struct radv_device *device, struct radv_pipeline *pipeline, gl_shader_stage stage, FILE *output); +static inline struct radv_shader * +radv_shader_ref(struct radv_shader *shader) +{ + assert(shader && shader->ref_count >= 1); + p_atomic_inc(&shader->ref_count); + return shader; +} + +static inline void +radv_shader_unref(struct radv_device *device, struct radv_shader *shader) +{ + assert(shader && shader->ref_count >= 1); + if (p_atomic_dec_zero(&shader->ref_count)) + radv_shader_destroy(device, shader); +} + static inline unsigned calculate_tess_lds_size(enum amd_gfx_level gfx_level, unsigned tcs_num_input_vertices, unsigned tcs_num_output_vertices, unsigned tcs_num_inputs,