diff options
author | omnisci3nce <17525998+omnisci3nce@users.noreply.github.com> | 2024-03-26 22:30:26 +1100 |
---|---|---|
committer | omnisci3nce <17525998+omnisci3nce@users.noreply.github.com> | 2024-03-26 22:30:26 +1100 |
commit | 90248a622203da205916ac89a1ac144d363cf37d (patch) | |
tree | 517ac2d58eb9e24c56ffb8a7fc229bdb437e780c | |
parent | 9e120a48438c793e8f23fab000189d4af1068eee (diff) |
compile shader to spirv in build. start adding shader module code
-rw-r--r-- | assets/shaders/blinn_phong.vert | 11 | ||||
-rw-r--r-- | src/renderer/backends/backend_vulkan.c | 45 | ||||
-rw-r--r-- | xmake.lua | 14 |
3 files changed, 65 insertions, 5 deletions
diff --git a/assets/shaders/blinn_phong.vert b/assets/shaders/blinn_phong.vert index 6028178..041c3d1 100644 --- a/assets/shaders/blinn_phong.vert +++ b/assets/shaders/blinn_phong.vert @@ -4,10 +4,13 @@ layout (location = 0) in vec3 inPos; layout (location = 1) in vec3 inNormal; layout (location = 2) in vec2 inTexCoords; -uniform mat4 model; -uniform mat4 view; -uniform mat4 projection; -uniform mat4 lightSpaceMatrix; +// Uniform block +layout (std140, binding = 0) uniform MatrixBlock { + mat4 model; + mat4 view; + mat4 projection; + mat4 lightSpaceMatrix; +}; // Output out VS_OUT { diff --git a/src/renderer/backends/backend_vulkan.c b/src/renderer/backends/backend_vulkan.c index 4b7314c..2b6bcd5 100644 --- a/src/renderer/backends/backend_vulkan.c +++ b/src/renderer/backends/backend_vulkan.c @@ -151,6 +151,48 @@ static vulkan_context context; typedef struct vulkan_state { } vulkan_state; +typedef struct vulkan_shader_stage { + VkShaderModuleCreateInfo create_info; + VkShaderModule handle; + VkPipelineShaderStageCreateInfo stage_create_info; +} vulkan_shader_stage; + +typedef struct vulkan_pipeline { + VkPipeline handle; + VkPipelineLayout layout; +} vulkan_pipeline; + +#define SHADER_STAGE_COUNT 2 + +typedef struct vulkan_shader { + // vertex, fragment + vulkan_shader_stage stages[SHADER_STAGE_COUNT]; + vulkan_pipeline pipeline; +} vulkan_shader; + +bool create_shader_module(vulkan_context* context, const char* filename, const char* type_str, + VkShaderStageFlagBits flag, u32 stage_index, + vulkan_shader_stage* shader_stages) { + // char file_name[512]; + + memset(&shader_stages[stage_index].create_info, 0, sizeof(VkShaderModuleCreateInfo)); + shader_stages[stage_index].create_info.sType = VK_STRUCTURE_TYPE_SHADER_MODULE_CREATE_INFO; + + // todo: file input +} + +bool vulkan_object_shader_create(vulkan_context* context, vulkan_shader* out_shader) { + char stage_type_strs[SHADER_STAGE_COUNT][5] = { "vert", "frag" }; + VkShaderStageFlagBits stage_types[SHADER_STAGE_COUNT] = { VK_SHADER_STAGE_VERTEX_BIT, + VK_SHADER_STAGE_FRAGMENT_BIT }; + for (u8 i = 0; i < SHADER_STAGE_COUNT; i++) { + create_shader_module(context, "build/linux/x86_64/debug/blinn_phong.vert.spv", + stage_type_strs[i], stage_types[i], i, out_shader->stages); + } +} +void vulkan_object_shader_destroy(vulkan_context* context, vulkan_shader* shader) {} +void vulkan_object_shader_use(vulkan_context* context, vulkan_shader* shader) {} + bool select_physical_device(vulkan_context* ctx) { u32 physical_device_count = 0; VK_CHECK(vkEnumeratePhysicalDevices(ctx->instance, &physical_device_count, 0)); @@ -980,9 +1022,10 @@ bool gfx_backend_init(renderer* ren) { for (u8 i = 0; i < context.swapchain.max_frames_in_flight; i++) { context.images_in_flight[i] = 0; } - INFO("Sync objects created"); + // Shader modules + INFO("Vulkan renderer initialisation succeeded"); return true; } @@ -63,6 +63,17 @@ local core_sources = { "src/systems/*.c", } +rule("compile_glsl_shaders") + set_extensions(".vert") + on_buildcmd_file(function (target, batchcmds, sourcefile, opt) + local targetfile = path.join(target:targetdir(), path.basename(sourcefile) .. ".vert.spv") + + print("Compiling shader: %s", sourcefile) + batchcmds:vrunv('glslc', {sourcefile, "-o", targetfile}) + batchcmds:add_depfiles(sourcefile) +end) +-- TODO: Metal shaders compilation + -- common configuration for both static and shared libraries target("core_config") set_kind("static") -- kind is required but you can ignore it since it's just for common settings @@ -83,7 +94,10 @@ target("core_config") add_includedirs("src/std/", {public = true}) add_includedirs("src/std/containers", {public = true}) add_includedirs("src/systems/", {public = true}) + add_rules("compile_glsl_shaders") add_files("src/empty.c") -- for some reason we need this on Mac so it doesnt call 'ar' with no files and error + add_files("assets/shaders/*.vert") + -- add_files("assets/shaders/*.frag") set_default(false) -- prevents standalone building of this target target("core_static") |