Skip to content

Commit

Permalink
[Vulkan] Parse compute shaders correctly
Browse files Browse the repository at this point in the history
  • Loading branch information
RobDangerous committed Jan 18, 2024
1 parent 997f9c8 commit 2dd10a1
Show file tree
Hide file tree
Showing 6 changed files with 36 additions and 106 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -902,7 +902,7 @@ void kinc_g5_command_list_set_texture_from_render_target_depth(kinc_g5_command_l

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);
//**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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@
#include <kinc/log.h>
#include <kinc/math/core.h>

static void parse_shader(uint32_t *shader_source, int shader_length, kinc_internal_named_number *locations, kinc_internal_named_number *textureBindings,
kinc_internal_named_number *uniformOffsets);

static uint8_t constantsMemory[1024 * 4];

static int getMultipleOf16(int value) {
Expand All @@ -14,61 +17,12 @@ static int getMultipleOf16(int value) {
}

void kinc_g5_compute_shader_init(kinc_g5_compute_shader *shader, void *_data, int length) {
unsigned index = 0;
uint8_t *data = (uint8_t *)_data;

memset(&shader->impl.attributes, 0, sizeof(shader->impl.attributes));
int attributesCount = data[index++];
for (int i = 0; i < attributesCount; ++i) {
unsigned char name[256];
for (unsigned i2 = 0; i2 < 255; ++i2) {
name[i2] = data[index++];
if (name[i2] == 0)
break;
}
shader->impl.attributes[i].hash = kinc_internal_hash_name(name);
shader->impl.attributes[i].index = data[index++];
}

memset(&shader->impl.textures, 0, sizeof(shader->impl.textures));
uint8_t texCount = data[index++];
for (unsigned i = 0; i < texCount; ++i) {
unsigned char name[256];
for (unsigned i2 = 0; i2 < 255; ++i2) {
name[i2] = data[index++];
if (name[i2] == 0)
break;
}
shader->impl.textures[i].hash = kinc_internal_hash_name(name);
shader->impl.textures[i].index = data[index++];
}
memset(shader->impl.locations, 0, sizeof(kinc_internal_named_number) * KINC_INTERNAL_NAMED_NUMBER_COUNT);
memset(shader->impl.offsets, 0, sizeof(kinc_internal_named_number) * KINC_INTERNAL_NAMED_NUMBER_COUNT);
memset(shader->impl.texture_bindings, 0, sizeof(kinc_internal_named_number) * KINC_INTERNAL_NAMED_NUMBER_COUNT);
parse_shader((uint32_t *)_data, length, shader->impl.locations, shader->impl.texture_bindings, shader->impl.offsets);

memset(&shader->impl.constants, 0, sizeof(shader->impl.constants));
uint8_t constantCount = data[index++];
shader->impl.constantsSize = 0;
for (unsigned i = 0; i < constantCount; ++i) {
unsigned char name[256];
for (unsigned i2 = 0; i2 < 255; ++i2) {
name[i2] = data[index++];
if (name[i2] == 0)
break;
}
kinc_compute_internal_shader_constant_t constant;
constant.hash = kinc_internal_hash_name(name);
constant.offset = *(uint32_t *)&data[index];
index += 4;
constant.size = *(uint32_t *)&data[index];
index += 4;
constant.columns = data[index];
index += 1;
constant.rows = data[index];
index += 1;

shader->impl.constants[i] = constant;
shader->impl.constantsSize = constant.offset + constant.size;
}

shader->impl.length = (int)(length - index);
/*shader->impl.length = (int)(length - index);
shader->impl.data = (uint8_t *)malloc(shader->impl.length);
assert(shader->impl.data != NULL);
memcpy(shader->impl.data, &data[index], shader->impl.length);
Expand Down Expand Up @@ -109,7 +63,7 @@ void kinc_g5_compute_shader_init(kinc_g5_compute_shader *shader, void *_data, in
layout_bindings[2].pImmutableSamplers = NULL;
layout_bindings[2].stageFlags = VK_SHADER_STAGE_COMPUTE_BIT;*/

VkDescriptorSetLayoutCreateInfo layoutInfo = {0};
/*VkDescriptorSetLayoutCreateInfo layoutInfo = {0};
layoutInfo.sType = VK_STRUCTURE_TYPE_DESCRIPTOR_SET_LAYOUT_CREATE_INFO;
layoutInfo.bindingCount = 1;
layoutInfo.pBindings = layout_bindings;
Expand Down Expand Up @@ -139,35 +93,17 @@ void kinc_g5_compute_shader_init(kinc_g5_compute_shader *shader, void *_data, in
if (vkCreateComputePipelines(vk_ctx.device, VK_NULL_HANDLE, 1, &pipeline_info, NULL, &shader->impl.pipeline) != VK_SUCCESS) {
kinc_log(KINC_LOG_LEVEL_WARNING, "Could not initialize compute shader.");
return;
}
}*/
}

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) {
if (constants[i].hash == hash) {
return &constants[i];
}
}
return NULL;
}

static kinc_internal_hash_index_t *findComputeTextureUnit(kinc_internal_hash_index_t *units, uint32_t hash) {
for (int i = 0; i < 64; ++i) {
if (units[i].hash == hash) {
return &units[i];
}
}
return NULL;
}

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);
/*kinc_compute_internal_shader_constant_t *constant = findComputeConstant(shader->impl.constants, hash);
if (constant == NULL) {
location.impl.computeOffset = 0;
}
Expand All @@ -177,7 +113,7 @@ kinc_g5_constant_location_t kinc_g5_compute_shader_get_constant_location(kinc_g5
if (location.impl.computeOffset == 0) {
kinc_log(KINC_LOG_LEVEL_WARNING, "Uniform %s not found.", name);
}
}*/

return location;
}
Expand All @@ -200,7 +136,7 @@ kinc_g5_texture_unit_t kinc_g5_compute_shader_get_texture_unit(kinc_g5_compute_s
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);
/*kinc_internal_hash_index_t *compute_unit = findComputeTextureUnit(shader->impl.textures, hash);
if (compute_unit == NULL) {
#ifndef NDEBUG
static int notFoundCount = 0;
Expand All @@ -216,6 +152,6 @@ kinc_g5_texture_unit_t kinc_g5_compute_shader_get_texture_unit(kinc_g5_compute_s
}
else {
unit.stages[KINC_G5_SHADER_TYPE_COMPUTE] = compute_unit->index + unitOffset;
}
}*/
return unit;
}
21 changes: 6 additions & 15 deletions Backends/Graphics5/Vulkan/Sources/kinc/backend/graphics5/compute.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,29 +4,20 @@

#include "MiniVulkan.h"

#include "named_number.h"

#ifdef __cplusplus
extern "C" {
#endif

typedef struct {
uint32_t hash;
uint32_t offset;
uint32_t size;
uint8_t columns;
uint8_t rows;
} kinc_compute_internal_shader_constant_t;

typedef struct kinc_g5_compute_shader_impl {
kinc_compute_internal_shader_constant_t constants[64];
int constantsSize;
kinc_internal_hash_index_t attributes[64];
kinc_internal_hash_index_t textures[64];
uint8_t *data;
int length;
kinc_internal_named_number locations[KINC_INTERNAL_NAMED_NUMBER_COUNT];
kinc_internal_named_number texture_bindings[KINC_INTERNAL_NAMED_NUMBER_COUNT];
kinc_internal_named_number offsets[KINC_INTERNAL_NAMED_NUMBER_COUNT];

VkDescriptorSet descriptor_set;
VkPipelineLayout pipeline_layout;
VkPipeline pipeline;
VkShaderModule shader_module;
} kinc_g5_compute_shader_impl;

#ifdef __cplusplus
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#pragma once

#define KINC_INTERNAL_NAMED_NUMBER_COUNT 32

typedef struct {
char name[256];
uint32_t number;
} kinc_internal_named_number;
Original file line number Diff line number Diff line change
Expand Up @@ -122,16 +122,16 @@ static void add_offset(uint32_t id, uint32_t offset) {
++offsets_size;
}

static void parseShader(kinc_g5_shader_t *shader, kinc_internal_named_number *locations, kinc_internal_named_number *textureBindings,
static void parse_shader(uint32_t*shader_source, int shader_length, kinc_internal_named_number *locations, kinc_internal_named_number *textureBindings,
kinc_internal_named_number *uniformOffsets) {
names_size = 0;
memberNames_size = 0;
locs_size = 0;
bindings_size = 0;
offsets_size = 0;

uint32_t *spirv = (uint32_t *)shader->impl.source;
int spirvsize = shader->impl.length / 4;
uint32_t *spirv = (uint32_t *)shader_source;
int spirvsize = shader_length / 4;
int index = 0;

uint32_t magicNumber = spirv[index++];
Expand Down Expand Up @@ -389,8 +389,8 @@ void kinc_g5_pipeline_compile(kinc_g5_pipeline_t *pipeline) {
memset(pipeline->impl.fragmentLocations, 0, sizeof(kinc_internal_named_number) * KINC_INTERNAL_NAMED_NUMBER_COUNT);
memset(pipeline->impl.fragmentOffsets, 0, sizeof(kinc_internal_named_number) * KINC_INTERNAL_NAMED_NUMBER_COUNT);
memset(pipeline->impl.textureBindings, 0, sizeof(kinc_internal_named_number) * KINC_INTERNAL_NAMED_NUMBER_COUNT);
parseShader(pipeline->vertexShader, pipeline->impl.vertexLocations, pipeline->impl.textureBindings, pipeline->impl.vertexOffsets);
parseShader(pipeline->fragmentShader, pipeline->impl.fragmentLocations, pipeline->impl.textureBindings, pipeline->impl.fragmentOffsets);
parse_shader((uint32_t*)pipeline->vertexShader->impl.source, pipeline->vertexShader->impl.length, pipeline->impl.vertexLocations, pipeline->impl.textureBindings, pipeline->impl.vertexOffsets);
parse_shader((uint32_t*)pipeline->fragmentShader->impl.source, pipeline->fragmentShader->impl.length, pipeline->impl.fragmentLocations, pipeline->impl.textureBindings, pipeline->impl.fragmentOffsets);

VkPipelineLayoutCreateInfo pPipelineLayoutCreateInfo = {0};
pPipelineLayoutCreateInfo.sType = VK_STRUCTURE_TYPE_PIPELINE_LAYOUT_CREATE_INFO;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,9 @@

#include "MiniVulkan.h"

struct kinc_g5_shader;

#define KINC_INTERNAL_NAMED_NUMBER_COUNT 32
#include "named_number.h"

typedef struct {
char name[256];
uint32_t number;
} kinc_internal_named_number;
struct kinc_g5_shader;

typedef struct PipelineState5Impl_s {
const char **textures;
Expand Down

0 comments on commit 2dd10a1

Please sign in to comment.