From d2515347f441468c150a2290db11c76746d5db01 Mon Sep 17 00:00:00 2001 From: Boris Brezillon Date: Mon, 23 Sep 2024 17:28:03 +0200 Subject: [PATCH] panvk: Keep our blend shaders in vk_meta_device Now that vk_meta can keep track of VkShaderEXT objects, we can keep our blend shaders in panvk_device::meta and get rid of our custom hash-table. Signed-off-by: Boris Brezillon Reviewed-by: Mary Guillemard Part-of: --- src/panfrost/vulkan/panvk_blend.h | 14 --- src/panfrost/vulkan/panvk_device.h | 1 - src/panfrost/vulkan/panvk_meta.h | 4 + src/panfrost/vulkan/panvk_shader.h | 5 - src/panfrost/vulkan/panvk_vX_blend.c | 147 ++++++++------------------ src/panfrost/vulkan/panvk_vX_device.c | 7 -- 6 files changed, 48 insertions(+), 130 deletions(-) diff --git a/src/panfrost/vulkan/panvk_blend.h b/src/panfrost/vulkan/panvk_blend.h index 53a3cea664e..f98726fe760 100644 --- a/src/panfrost/vulkan/panvk_blend.h +++ b/src/panfrost/vulkan/panvk_blend.h @@ -20,21 +20,7 @@ struct vk_color_blend_state; struct vk_dynamic_graphics_state; struct panvk_device; -struct panvk_blend_shader { - struct pan_blend_shader_key key; - mali_ptr binary; -}; - -struct panvk_blend_shader_cache { - struct panvk_pool bin_pool; - struct hash_table *ht; - simple_mtx_t lock; -}; - #ifdef PAN_ARCH -VkResult panvk_per_arch(blend_shader_cache_init)(struct panvk_device *dev); - -void panvk_per_arch(blend_shader_cache_cleanup)(struct panvk_device *dev); struct panvk_blend_info { bool any_dest_read; diff --git a/src/panfrost/vulkan/panvk_device.h b/src/panfrost/vulkan/panvk_device.h index 61f67c9c863..87780df42b6 100644 --- a/src/panfrost/vulkan/panvk_device.h +++ b/src/panfrost/vulkan/panvk_device.h @@ -59,7 +59,6 @@ struct panvk_device { struct pan_blend_shader_cache blend_shader_cache; } blitter; - struct panvk_blend_shader_cache blend_shader_cache; struct vk_meta_device meta; struct { diff --git a/src/panfrost/vulkan/panvk_meta.h b/src/panfrost/vulkan/panvk_meta.h index 3282e652a76..715e3b687e8 100644 --- a/src/panfrost/vulkan/panvk_meta.h +++ b/src/panfrost/vulkan/panvk_meta.h @@ -12,6 +12,10 @@ #include "vk_format.h" #include "vk_meta.h" +enum panvk_meta_object_key_type { + PANVK_META_OBJECT_KEY_BLEND_SHADER = VK_META_OBJECT_KEY_DRIVER_OFFSET, +}; + static inline VkFormat panvk_meta_get_uint_format_for_blk_size(unsigned blk_sz) { diff --git a/src/panfrost/vulkan/panvk_shader.h b/src/panfrost/vulkan/panvk_shader.h index 64865f7327f..0ec4d50bea3 100644 --- a/src/panfrost/vulkan/panvk_shader.h +++ b/src/panfrost/vulkan/panvk_shader.h @@ -199,11 +199,6 @@ bool panvk_per_arch(nir_lower_descriptors)( struct vk_descriptor_set_layout *const *set_layouts, struct panvk_shader *shader); -enum panvk_internal_shader_type { - PANVK_INTERNAL_SHADER_BLEND, - PANVK_INTERNAL_SHADER_FB_PRELOAD, -}; - /* This a stripped-down version of panvk_shader for internal shaders that * are managed by vk_meta (blend and preload shaders). Those don't need the * complexity inherent to user provided shaders as they're not exposed. */ diff --git a/src/panfrost/vulkan/panvk_vX_blend.c b/src/panfrost/vulkan/panvk_vX_blend.c index 067145b4efc..a5671cfd6a4 100644 --- a/src/panfrost/vulkan/panvk_vX_blend.c +++ b/src/panfrost/vulkan/panvk_vX_blend.c @@ -18,48 +18,10 @@ #include "panvk_device.h" #include "panvk_shader.h" -DERIVE_HASH_TABLE(pan_blend_shader_key); - -VkResult -panvk_per_arch(blend_shader_cache_init)(struct panvk_device *dev) -{ - struct panvk_blend_shader_cache *cache = &dev->blend_shader_cache; - - simple_mtx_init(&cache->lock, mtx_plain); - - struct panvk_pool_properties bin_pool_props = { - .create_flags = PAN_KMOD_BO_FLAG_EXECUTABLE, - .slab_size = 16 * 1024, - .label = "blend shaders", - .owns_bos = true, - .prealloc = false, - .needs_locking = false, - }; - panvk_pool_init(&cache->bin_pool, dev, NULL, &bin_pool_props); - - cache->ht = pan_blend_shader_key_table_create(NULL); - if (!cache->ht) { - panvk_pool_cleanup(&cache->bin_pool); - simple_mtx_destroy(&cache->lock); - return vk_errorf(dev, VK_ERROR_OUT_OF_HOST_MEMORY, - "couldn't create blend shader hash table"); - } - - return VK_SUCCESS; -} - -void -panvk_per_arch(blend_shader_cache_cleanup)(struct panvk_device *dev) -{ - struct panvk_blend_shader_cache *cache = &dev->blend_shader_cache; - - hash_table_foreach_remove(cache->ht, he) - vk_free(&dev->vk.alloc, he->data); - - _mesa_hash_table_destroy(cache->ht, NULL); - panvk_pool_cleanup(&cache->bin_pool); - simple_mtx_destroy(&cache->lock); -} +struct panvk_blend_shader_key { + enum panvk_meta_object_key_type type; + struct pan_blend_shader_key info; +}; static bool lower_load_blend_const(nir_builder *b, nir_instr *instr, UNUSED void *data) @@ -86,44 +48,40 @@ lower_load_blend_const(nir_builder *b, nir_instr *instr, UNUSED void *data) } static VkResult -get_blend_shader_locked(struct panvk_device *dev, - const struct pan_blend_state *state, - nir_alu_type src0_type, nir_alu_type src1_type, - unsigned rt, mali_ptr *shader_addr) +get_blend_shader(struct panvk_device *dev, + const struct pan_blend_state *state, + nir_alu_type src0_type, nir_alu_type src1_type, + unsigned rt, mali_ptr *shader_addr) { struct panvk_physical_device *pdev = to_panvk_physical_device(dev->vk.physical); - struct panvk_blend_shader_cache *cache = &dev->blend_shader_cache; - struct pan_blend_shader_key key = { - .format = state->rts[rt].format, - .src0_type = src0_type, - .src1_type = src1_type, - .rt = rt, - .has_constants = pan_blend_constant_mask(state->rts[rt].equation) != 0, - .logicop_enable = state->logicop_enable, - .logicop_func = state->logicop_func, - .nr_samples = state->rts[rt].nr_samples, - .equation = state->rts[rt].equation, - .alpha_to_one = state->alpha_to_one, + struct panvk_blend_shader_key key = { + .type = PANVK_META_OBJECT_KEY_BLEND_SHADER, + .info = { + .format = state->rts[rt].format, + .src0_type = src0_type, + .src1_type = src1_type, + .rt = rt, + .has_constants = + pan_blend_constant_mask(state->rts[rt].equation) != 0, + .logicop_enable = state->logicop_enable, + .logicop_func = state->logicop_func, + .nr_samples = state->rts[rt].nr_samples, + .equation = state->rts[rt].equation, + .alpha_to_one = state->alpha_to_one, + }, }; + struct panvk_internal_shader *shader; assert(state->logicop_enable || state->alpha_to_one || !pan_blend_is_opaque(state->rts[rt].equation)); assert(state->rts[rt].equation.color_mask != 0); - simple_mtx_assert_locked(&dev->blend_shader_cache.lock); - struct hash_entry *he = _mesa_hash_table_search(cache->ht, &key); - struct panvk_blend_shader *shader = he ? he->data : NULL; - - if (shader) + VkShaderEXT shader_handle = (VkShaderEXT)vk_meta_lookup_object( + &dev->meta, VK_OBJECT_TYPE_SHADER_EXT, &key, sizeof(key)); + if (shader_handle != VK_NULL_HANDLE) goto out; - shader = vk_zalloc(&dev->vk.alloc, sizeof(*shader), 8, - VK_SYSTEM_ALLOCATION_SCOPE_DEVICE); - if (!shader) - return vk_errorf(dev, VK_ERROR_OUT_OF_HOST_MEMORY, - "couldn't allocate blend shader object"); - nir_shader *nir = GENX(pan_blend_create_shader)(state, src0_type, src1_type, rt); @@ -135,55 +93,38 @@ get_blend_shader_locked(struct panvk_device *dev, .gpu_id = pdev->kmod.props.gpu_prod_id, .no_ubo_to_push = true, .is_blend = true, - .blend = - { - .nr_samples = key.nr_samples, - .bifrost_blend_desc = - GENX(pan_blend_get_internal_desc)(key.format, key.rt, 0, false), - }, + .blend = { + .nr_samples = key.info.nr_samples, + .bifrost_blend_desc = + GENX(pan_blend_get_internal_desc)(key.info.format, key.info.rt, 0, + false), + }, }; pan_shader_preprocess(nir, inputs.gpu_id); enum pipe_format rt_formats[8] = {0}; - rt_formats[rt] = key.format; + rt_formats[rt] = key.info.format; NIR_PASS_V(nir, GENX(pan_inline_rt_conversion), rt_formats); - struct pan_shader_info info; - struct util_dynarray binary; - - util_dynarray_init(&binary, nir); - GENX(pan_shader_compile)(nir, &inputs, &binary, &info); - - shader->key = key; - shader->binary = pan_pool_upload_aligned(&cache->bin_pool.base, binary.data, - binary.size, 128); + VkResult result = + panvk_per_arch(create_internal_shader)(dev, nir, &inputs, &shader); ralloc_free(nir); - _mesa_hash_table_insert(cache->ht, &shader->key, shader); + if (result != VK_SUCCESS) + return result; + + shader_handle = (VkShaderEXT)vk_meta_cache_object( + &dev->vk, &dev->meta, &key, sizeof(key), VK_OBJECT_TYPE_SHADER_EXT, + (uint64_t)panvk_internal_shader_to_handle(shader)); out: - *shader_addr = shader->binary; + shader = panvk_internal_shader_from_handle(shader_handle); + *shader_addr = panvk_priv_mem_dev_addr(shader->code_mem); return VK_SUCCESS; } -static VkResult -get_blend_shader(struct panvk_device *dev, const struct pan_blend_state *state, - nir_alu_type src0_type, nir_alu_type src1_type, unsigned rt, - mali_ptr *shader_addr) -{ - struct panvk_blend_shader_cache *cache = &dev->blend_shader_cache; - VkResult result; - - simple_mtx_lock(&cache->lock); - result = get_blend_shader_locked(dev, state, src0_type, src1_type, rt, - shader_addr); - simple_mtx_unlock(&cache->lock); - - return result; -} - static void emit_blend_desc(const struct pan_shader_info *fs_info, mali_ptr fs_code, const struct pan_blend_state *state, unsigned rt_idx, diff --git a/src/panfrost/vulkan/panvk_vX_device.c b/src/panfrost/vulkan/panvk_vX_device.c index 849822fa4ae..2b423f8eef4 100644 --- a/src/panfrost/vulkan/panvk_vX_device.c +++ b/src/panfrost/vulkan/panvk_vX_device.c @@ -319,11 +319,6 @@ panvk_per_arch(create_device)(struct panvk_physical_device *physical_device, vk_device_set_drm_fd(&device->vk, device->kmod.dev->fd); - result = panvk_per_arch(blend_shader_cache_init)(device); - - if (result != VK_SUCCESS) - goto err_free_priv_bos; - panvk_preload_blitter_init(device); result = panvk_meta_init(device); @@ -371,7 +366,6 @@ err_finish_queues: err_cleanup_blitter: panvk_preload_blitter_cleanup(device); - panvk_per_arch(blend_shader_cache_cleanup)(device); err_free_priv_bos: panvk_priv_bo_unref(device->sample_positions); @@ -408,7 +402,6 @@ panvk_per_arch(destroy_device)(struct panvk_device *device, panvk_meta_cleanup(device); panvk_preload_blitter_cleanup(device); - panvk_per_arch(blend_shader_cache_cleanup)(device); panvk_priv_bo_unref(device->tiler_heap); panvk_priv_bo_unref(device->sample_positions); panvk_device_cleanup_mempools(device);