Skip to content

Commit

Permalink
[Vulkan] Make compute compile again
Browse files Browse the repository at this point in the history
  • Loading branch information
RobDangerous committed Jan 17, 2024
1 parent 84e3b40 commit 16a8a36
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 221 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
#include "vulkan.h"

#include <kinc/graphics5/commandlist.h>
#include <kinc/graphics5/compute.h>
#include <kinc/graphics5/indexbuffer.h>
#include <kinc/graphics5/pipeline.h>
#include <kinc/graphics5/vertexbuffer.h>
Expand All @@ -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];
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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);
}
223 changes: 16 additions & 207 deletions Backends/Graphics5/Vulkan/Sources/kinc/backend/graphics5/compute.c.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
#include <kinc/backend/compute.h>
#include <kinc/graphics5/compute.h>

#include <kinc/compute/compute.h>
#include <kinc/graphics4/texture.h>
#include <kinc/log.h>
#include <kinc/math/core.h>
Expand All @@ -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;

Expand Down Expand Up @@ -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) {
Expand All @@ -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);
Expand All @@ -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) {
Expand All @@ -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);
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,12 @@

#include <kinc/backend/graphics5/ShaderHash.h>

#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;
Expand All @@ -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];
Expand All @@ -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
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ typedef struct ComputePipelineState5Impl_t {
typedef struct {
int vertexOffset;
int fragmentOffset;
int computeOffset;
} ConstantLocation5Impl;

typedef struct {
Expand Down

0 comments on commit 16a8a36

Please sign in to comment.