From 0bc02eee4b2d79a85f67fe53a35f5373cf196da2 Mon Sep 17 00:00:00 2001 From: Faith Ekstrand Date: Thu, 18 Jan 2024 14:49:51 -0600 Subject: [PATCH] nak: Refactor shader upload math Part-of: --- src/nouveau/vulkan/nvk_shader.c | 22 +++++++++++++++------- src/nouveau/vulkan/nvk_shader.h | 4 ++-- 2 files changed, 17 insertions(+), 9 deletions(-) diff --git a/src/nouveau/vulkan/nvk_shader.c b/src/nouveau/vulkan/nvk_shader.c index 6e2d0ba7b80..b58659b6cf8 100644 --- a/src/nouveau/vulkan/nvk_shader.c +++ b/src/nouveau/vulkan/nvk_shader.c @@ -501,23 +501,31 @@ nvk_shader_upload(struct nvk_device *dev, struct nvk_shader *shader) * Kepler+ needs the first instruction to be 0x80 aligned, so we waste 0x30 bytes */ int alignment = dev->pdev->info.cls_eng3d >= KEPLER_A ? 0x80 : 0x40; - int offset = 0; + uint32_t total_size = 0; if (dev->pdev->info.cls_eng3d >= KEPLER_A && dev->pdev->info.cls_eng3d < TURING_A && hdr_size > 0) { - /* offset will be 0x30 */ - offset = alignment - hdr_size; + /* The instructions are what has to be aligned so we need to start at a + * small offset (0x30 B) into the upload area. + */ + total_size = alignment - hdr_size; } - uint32_t total_size = shader->code_size + hdr_size + offset; + const uint32_t hdr_offset = total_size; + total_size += hdr_size; + + const uint32_t code_offset = total_size; + assert(code_offset % alignment == 0); + total_size += shader->code_size; + char *data = malloc(total_size); if (data == NULL) return vk_error(dev, VK_ERROR_OUT_OF_HOST_MEMORY); assert(hdr_size <= sizeof(shader->info.hdr)); - memcpy(data + offset, shader->info.hdr, hdr_size); - memcpy(data + offset + hdr_size, shader->code_ptr, shader->code_size); + memcpy(data + hdr_offset, shader->info.hdr, hdr_size); + memcpy(data + code_offset, shader->code_ptr, shader->code_size); #ifndef NDEBUG if (debug_get_bool_option("NV50_PROG_DEBUG", false)) @@ -528,7 +536,7 @@ nvk_shader_upload(struct nvk_device *dev, struct nvk_shader *shader) total_size, alignment, &shader->upload_addr); if (result == VK_SUCCESS) { shader->upload_size = total_size; - shader->upload_padding = offset; + shader->hdr_offset = hdr_offset; } free(data); diff --git a/src/nouveau/vulkan/nvk_shader.h b/src/nouveau/vulkan/nvk_shader.h index 5804464ed78..33f2950c6a8 100644 --- a/src/nouveau/vulkan/nvk_shader.h +++ b/src/nouveau/vulkan/nvk_shader.h @@ -66,13 +66,13 @@ struct nvk_shader { uint32_t upload_size; uint64_t upload_addr; - uint32_t upload_padding; + uint32_t hdr_offset; }; static inline uint64_t nvk_shader_address(const struct nvk_shader *shader) { - return shader->upload_addr + shader->upload_padding; + return shader->upload_addr + shader->hdr_offset; } static inline bool