From 16a8a3615504b1dc6d4e83beb96bed1293672a13 Mon Sep 17 00:00:00 2001 From: Robert Konrad Date: Wed, 17 Jan 2024 14:13:56 +0100 Subject: [PATCH] [Vulkan] Make compute compile again --- .../kinc/backend/graphics5/commandlist.c.h | 19 ++ .../kinc/backend/graphics5/compute.c.h | 223 ++---------------- .../kinc/backend/{ => graphics5}/compute.h | 17 +- .../Sources/kinc/backend/graphics5/pipeline.h | 1 + 4 files changed, 39 insertions(+), 221 deletions(-) rename Backends/Graphics5/Vulkan/Sources/kinc/backend/{ => graphics5}/compute.h (67%) diff --git a/Backends/Graphics5/Vulkan/Sources/kinc/backend/graphics5/commandlist.c.h b/Backends/Graphics5/Vulkan/Sources/kinc/backend/graphics5/commandlist.c.h index 45659392b..417ca4f0e 100644 --- a/Backends/Graphics5/Vulkan/Sources/kinc/backend/graphics5/commandlist.c.h +++ b/Backends/Graphics5/Vulkan/Sources/kinc/backend/graphics5/commandlist.c.h @@ -2,6 +2,7 @@ #include "vulkan.h" #include +#include #include #include #include @@ -22,6 +23,7 @@ kinc_g5_render_target_t *currentRenderTargets[8] = {NULL, NULL, NULL, NULL, NULL static bool onBackBuffer = false; static uint32_t lastVertexConstantBufferOffset = 0; static uint32_t lastFragmentConstantBufferOffset = 0; +static uint32_t lastComputeConstantBufferOffset = 0; static kinc_g5_pipeline_t *currentPipeline = NULL; static int mrtIndex = 0; static VkFramebuffer mrtFramebuffer[16]; @@ -804,6 +806,14 @@ void kinc_g5_command_list_set_fragment_constant_buffer(kinc_g5_command_list_t *l vkCmdBindDescriptorSets(list->impl._buffer, VK_PIPELINE_BIND_POINT_GRAPHICS, currentPipeline->impl.pipeline_layout, 0, 1, &descriptor_set, 2, offsets); } +void kinc_g5_command_list_set_compute_constant_buffer(kinc_g5_command_list_t *list, struct kinc_g5_constant_buffer *buffer, int offset, size_t size) { + lastComputeConstantBufferOffset = offset; + + VkDescriptorSet descriptor_set = getDescriptorSet(); + uint32_t offsets[1] = {lastComputeConstantBufferOffset}; + vkCmdBindDescriptorSets(list->impl._buffer, VK_PIPELINE_BIND_POINT_COMPUTE, currentPipeline->impl.pipeline_layout, 0, 1, &descriptor_set, 1, offsets); +} + static bool wait_for_framebuffer = false; static void command_list_should_wait_for_framebuffer(void) { @@ -889,3 +899,12 @@ void kinc_g5_command_list_set_texture_from_render_target_depth(kinc_g5_command_l vulkanRenderTargets[unit.stages[KINC_G5_SHADER_TYPE_FRAGMENT]] = target; vulkanTextures[unit.stages[KINC_G5_SHADER_TYPE_FRAGMENT]] = NULL; } + +void kinc_g5_command_list_set_compute_shader(kinc_g5_command_list_t *list, kinc_g5_compute_shader *shader) { + vkCmdBindPipeline(list->impl._buffer, VK_PIPELINE_BIND_POINT_COMPUTE, shader->impl.pipeline); + vkCmdBindDescriptorSets(list->impl._buffer, VK_PIPELINE_BIND_POINT_COMPUTE, shader->impl.pipeline_layout, 0, 1, &shader->impl.descriptor_set, 0, 0); +} + +void kinc_g5_command_list_compute(kinc_g5_command_list_t *list, int x, int y, int z) { + vkCmdDispatch(list->impl._buffer, x, y, z); +} diff --git a/Backends/Graphics5/Vulkan/Sources/kinc/backend/graphics5/compute.c.h b/Backends/Graphics5/Vulkan/Sources/kinc/backend/graphics5/compute.c.h index 079147ffc..dc989f144 100644 --- a/Backends/Graphics5/Vulkan/Sources/kinc/backend/graphics5/compute.c.h +++ b/Backends/Graphics5/Vulkan/Sources/kinc/backend/graphics5/compute.c.h @@ -1,6 +1,5 @@ -#include +#include -#include #include #include #include @@ -14,115 +13,7 @@ static int getMultipleOf16(int value) { return ret; } -static void setInt(uint8_t *constants, uint32_t offset, uint32_t size, int value) { - if (size == 0) - return; - int *ints = (int *)&constants[offset]; - ints[0] = value; -} - -static void setFloat(uint8_t *constants, uint32_t offset, uint32_t size, float value) { - if (size == 0) - return; - float *floats = (float *)&constants[offset]; - floats[0] = value; -} - -static void setFloat2(uint8_t *constants, uint32_t offset, uint32_t size, float value1, float value2) { - if (size == 0) - return; - float *floats = (float *)&constants[offset]; - floats[0] = value1; - floats[1] = value2; -} - -static void setFloat3(uint8_t *constants, uint32_t offset, uint32_t size, float value1, float value2, float value3) { - if (size == 0) - return; - float *floats = (float *)&constants[offset]; - floats[0] = value1; - floats[1] = value2; - floats[2] = value3; -} - -static void setFloat4(uint8_t *constants, uint32_t offset, uint32_t size, float value1, float value2, float value3, float value4) { - if (size == 0) - return; - float *floats = (float *)&constants[offset]; - floats[0] = value1; - floats[1] = value2; - floats[2] = value3; - floats[3] = value4; -} - -static void setFloats(uint8_t *constants, uint32_t offset, uint32_t size, uint8_t columns, uint8_t rows, float *values, int count) { - if (size == 0) - return; - float *floats = (float *)&constants[offset]; - if (columns == 4 && rows == 4) { - for (int i = 0; i < count / 16 && i < (int)size / 4; ++i) { - for (int y = 0; y < 4; ++y) { - for (int x = 0; x < 4; ++x) { - floats[i * 16 + x + y * 4] = values[i * 16 + y + x * 4]; - } - } - } - } - else if (columns == 3 && rows == 3) { - for (int i = 0; i < count / 9 && i < (int)size / 3; ++i) { - for (int y = 0; y < 4; ++y) { - for (int x = 0; x < 4; ++x) { - floats[i * 12 + x + y * 4] = values[i * 9 + y + x * 3]; - } - } - } - } - else if (columns == 2 && rows == 2) { - for (int i = 0; i < count / 4 && i < (int)size / 2; ++i) { - for (int y = 0; y < 4; ++y) { - for (int x = 0; x < 4; ++x) { - floats[i * 8 + x + y * 4] = values[i * 4 + y + x * 2]; - } - } - } - } - else { - for (int i = 0; i < count && i * 4 < (int)size; ++i) { - floats[i] = values[i]; - } - } -} - -static void setBool(uint8_t *constants, uint32_t offset, uint32_t size, bool value) { - if (size == 0) - return; - int *ints = (int *)&constants[offset]; - ints[0] = value ? 1 : 0; -} - -static void setMatrix4(uint8_t *constants, uint32_t offset, uint32_t size, kinc_matrix4x4_t *value) { - if (size == 0) - return; - float *floats = (float *)&constants[offset]; - for (int y = 0; y < 4; ++y) { - for (int x = 0; x < 4; ++x) { - floats[x + y * 4] = kinc_matrix4x4_get(value, y, x); - } - } -} - -static void setMatrix3(uint8_t *constants, uint32_t offset, uint32_t size, kinc_matrix3x3_t *value) { - if (size == 0) - return; - float *floats = (float *)&constants[offset]; - for (int y = 0; y < 3; ++y) { - for (int x = 0; x < 3; ++x) { - floats[x + y * 4] = kinc_matrix3x3_get(value, y, x); - } - } -} - -void kinc_compute_shader_init(kinc_compute_shader_t *shader, void *_data, int length) { +void kinc_g5_compute_shader_init(kinc_g5_compute_shader *shader, void *_data, int length) { unsigned index = 0; uint8_t *data = (uint8_t *)_data; @@ -251,7 +142,7 @@ void kinc_compute_shader_init(kinc_compute_shader_t *shader, void *_data, int le } } -void kinc_compute_shader_destroy(kinc_compute_shader_t *shader) {} +void kinc_g5_compute_shader_destroy(kinc_g5_compute_shader *shader) {} static kinc_compute_internal_shader_constant_t *findComputeConstant(kinc_compute_internal_shader_constant_t *constants, uint32_t hash) { for (int i = 0; i < 64; ++i) { @@ -271,33 +162,27 @@ static kinc_internal_hash_index_t *findComputeTextureUnit(kinc_internal_hash_ind return NULL; } -kinc_compute_constant_location_t kinc_compute_shader_get_constant_location(kinc_compute_shader_t *shader, const char *name) { - kinc_compute_constant_location_t location; +kinc_g5_constant_location_t kinc_g5_compute_shader_get_constant_location(kinc_g5_compute_shader *shader, const char *name) { + kinc_g5_constant_location_t location = {0}; uint32_t hash = kinc_internal_hash_name((unsigned char *)name); kinc_compute_internal_shader_constant_t *constant = findComputeConstant(shader->impl.constants, hash); if (constant == NULL) { - location.impl.offset = 0; - location.impl.size = 0; - location.impl.columns = 0; - location.impl.rows = 0; + location.impl.computeOffset = 0; } else { - location.impl.offset = constant->offset; - location.impl.size = constant->size; - location.impl.columns = constant->columns; - location.impl.rows = constant->rows; + location.impl.computeOffset = constant->offset; } - if (location.impl.size == 0) { + if (location.impl.computeOffset == 0) { kinc_log(KINC_LOG_LEVEL_WARNING, "Uniform %s not found.", name); } return location; } -kinc_compute_texture_unit_t kinc_compute_shader_get_texture_unit(kinc_compute_shader_t *shader, const char *name) { +kinc_g5_texture_unit_t kinc_g5_compute_shader_get_texture_unit(kinc_g5_compute_shader *shader, const char *name) { char unitName[64]; int unitOffset = 0; size_t len = strlen(name); @@ -311,10 +196,12 @@ kinc_compute_texture_unit_t kinc_compute_shader_get_texture_unit(kinc_compute_sh uint32_t hash = kinc_internal_hash_name((unsigned char *)unitName); - kinc_compute_texture_unit_t unit; - kinc_internal_hash_index_t *vertexUnit = findComputeTextureUnit(shader->impl.textures, hash); - if (vertexUnit == NULL) { - unit.impl.unit = -1; + kinc_g5_texture_unit_t unit; + for (int i = 0; i < KINC_G5_SHADER_TYPE_COUNT; ++i) { + unit.stages[i] = -1; + } + kinc_internal_hash_index_t *compute_unit = findComputeTextureUnit(shader->impl.textures, hash); + if (compute_unit == NULL) { #ifndef NDEBUG static int notFoundCount = 0; if (notFoundCount < 10) { @@ -328,85 +215,7 @@ kinc_compute_texture_unit_t kinc_compute_shader_get_texture_unit(kinc_compute_sh #endif } else { - unit.impl.unit = vertexUnit->index + unitOffset; + unit.stages[KINC_G5_SHADER_TYPE_COMPUTE] = compute_unit->index + unitOffset; } return unit; } - -void kinc_compute_set_bool(kinc_compute_constant_location_t location, bool value) { - setBool(constantsMemory, location.impl.offset, location.impl.size, value); -} - -void kinc_compute_set_int(kinc_compute_constant_location_t location, int value) { - setInt(constantsMemory, location.impl.offset, location.impl.size, value); -} - -void kinc_compute_set_float(kinc_compute_constant_location_t location, float value) { - setFloat(constantsMemory, location.impl.offset, location.impl.size, value); -} - -void kinc_compute_set_float2(kinc_compute_constant_location_t location, float value1, float value2) { - setFloat2(constantsMemory, location.impl.offset, location.impl.size, value1, value2); -} - -void kinc_compute_set_float3(kinc_compute_constant_location_t location, float value1, float value2, float value3) { - setFloat3(constantsMemory, location.impl.offset, location.impl.size, value1, value2, value3); -} - -void kinc_compute_set_float4(kinc_compute_constant_location_t location, float value1, float value2, float value3, float value4) { - setFloat4(constantsMemory, location.impl.offset, location.impl.size, value1, value2, value3, value4); -} - -void kinc_compute_set_floats(kinc_compute_constant_location_t location, float *values, int count) { - setFloats(constantsMemory, location.impl.offset, location.impl.size, location.impl.columns, location.impl.rows, values, count); -} - -void kinc_compute_set_matrix4(kinc_compute_constant_location_t location, kinc_matrix4x4_t *value) { - setMatrix4(constantsMemory, location.impl.offset, location.impl.size, value); -} - -void kinc_compute_set_matrix3(kinc_compute_constant_location_t location, kinc_matrix3x3_t *value) { - setMatrix3(constantsMemory, location.impl.offset, location.impl.size, value); -} - -void kinc_compute_set_texture(kinc_compute_texture_unit_t unit, struct kinc_g4_texture *texture, kinc_compute_access_t access) { - // ID3D12ShaderResourceView *nullView = nullptr; - // context->PSSetShaderResources(0, 1, &nullView); - - // context->CSSetUnorderedAccessViews(unit.impl.unit, 1, &texture->impl.computeView, nullptr); -} - -void kinc_compute_set_render_target(kinc_compute_texture_unit_t unit, struct kinc_g4_render_target *texture, kinc_compute_access_t access) {} - -void kinc_compute_set_sampled_texture(kinc_compute_texture_unit_t unit, struct kinc_g4_texture *texture) {} - -void kinc_compute_set_sampled_render_target(kinc_compute_texture_unit_t unit, struct kinc_g4_render_target *target) {} - -void kinc_compute_set_sampled_depth_from_render_target(kinc_compute_texture_unit_t unit, struct kinc_g4_render_target *target) {} - -void kinc_compute_set_texture_addressing(kinc_compute_texture_unit_t unit, kinc_g4_texture_direction_t dir, kinc_g4_texture_addressing_t addressing) {} - -void kinc_compute_set_texture_magnification_filter(kinc_compute_texture_unit_t unit, kinc_g4_texture_filter_t filter) {} - -void kinc_compute_set_texture_minification_filter(kinc_compute_texture_unit_t unit, kinc_g4_texture_filter_t filter) {} - -void kinc_compute_set_texture_mipmap_filter(kinc_compute_texture_unit_t unit, kinc_g4_mipmap_filter_t filter) {} - -void kinc_compute_set_texture3d_addressing(kinc_compute_texture_unit_t unit, kinc_g4_texture_direction_t dir, kinc_g4_texture_addressing_t addressing) {} - -void kinc_compute_set_texture3d_magnification_filter(kinc_compute_texture_unit_t unit, kinc_g4_texture_filter_t filter) {} - -void kinc_compute_set_texture3d_minification_filter(kinc_compute_texture_unit_t unit, kinc_g4_texture_filter_t filter) {} - -void kinc_compute_set_texture3d_mipmap_filter(kinc_compute_texture_unit_t unit, kinc_g4_mipmap_filter_t filter) {} - -void kinc_compute_set_shader(kinc_compute_shader_t *shader) { - VkCommandBuffer command_buffer = {0}; - vkCmdBindPipeline(command_buffer, VK_PIPELINE_BIND_POINT_COMPUTE, shader->impl.pipeline); - vkCmdBindDescriptorSets(command_buffer, VK_PIPELINE_BIND_POINT_COMPUTE, shader->impl.pipeline_layout, 0, 1, &shader->impl.descriptor_set, 0, 0); -} - -void kinc_compute(int x, int y, int z) { - VkCommandBuffer command_buffer = {0}; - vkCmdDispatch(command_buffer, x, y, z); -} diff --git a/Backends/Graphics5/Vulkan/Sources/kinc/backend/compute.h b/Backends/Graphics5/Vulkan/Sources/kinc/backend/graphics5/compute.h similarity index 67% rename from Backends/Graphics5/Vulkan/Sources/kinc/backend/compute.h rename to Backends/Graphics5/Vulkan/Sources/kinc/backend/graphics5/compute.h index 9ef264213..9fc229a33 100644 --- a/Backends/Graphics5/Vulkan/Sources/kinc/backend/compute.h +++ b/Backends/Graphics5/Vulkan/Sources/kinc/backend/graphics5/compute.h @@ -2,23 +2,12 @@ #include -#include "graphics5/MiniVulkan.h" +#include "MiniVulkan.h" #ifdef __cplusplus extern "C" { #endif -typedef struct { - uint32_t offset; - uint32_t size; - uint8_t columns; - uint8_t rows; -} kinc_compute_constant_location_impl_t; - -typedef struct { - int unit; -} kinc_compute_texture_unit_impl_t; - typedef struct { uint32_t hash; uint32_t offset; @@ -27,7 +16,7 @@ typedef struct { uint8_t rows; } kinc_compute_internal_shader_constant_t; -typedef struct { +typedef struct kinc_g5_compute_shader_impl { kinc_compute_internal_shader_constant_t constants[64]; int constantsSize; kinc_internal_hash_index_t attributes[64]; @@ -38,7 +27,7 @@ typedef struct { VkDescriptorSet descriptor_set; VkPipelineLayout pipeline_layout; VkPipeline pipeline; -} kinc_compute_shader_impl_t; +} kinc_g5_compute_shader_impl; #ifdef __cplusplus } diff --git a/Backends/Graphics5/Vulkan/Sources/kinc/backend/graphics5/pipeline.h b/Backends/Graphics5/Vulkan/Sources/kinc/backend/graphics5/pipeline.h index 0a1894b09..26f0314d3 100644 --- a/Backends/Graphics5/Vulkan/Sources/kinc/backend/graphics5/pipeline.h +++ b/Backends/Graphics5/Vulkan/Sources/kinc/backend/graphics5/pipeline.h @@ -37,6 +37,7 @@ typedef struct ComputePipelineState5Impl_t { typedef struct { int vertexOffset; int fragmentOffset; + int computeOffset; } ConstantLocation5Impl; typedef struct {