1 #include <kiba/renderer/vulkan/shader.h>
5 static array_of(u32) vulkan_shader_load_code(
vulkan_context *context,
const char *path, usize *read_bytes) {
6 KB_ASSERT(context,
"context must not be null");
7 KB_ASSERT(path,
"file path must not be null");
12 if (filesystem_size(&file, &size)
13 && (code = array_create(u32, size /
sizeof(code[0]) + 1, &context->alloc.kiba_alloc)) !=
KB_NULL) {
14 array_resize(&code, array_capacity(code));
15 if (filesystem_read_all_text(&file, array_capacity(code) *
sizeof(code[0]), (
char *) code, read_bytes)) {
16 if (size != *read_bytes) {
17 KB_WARN(
"determined size of shader code ({usize}) and actually read data ({usize}) differs",
25 filesystem_close(&file);
30 b8 vulkan_shader_create(
vulkan_context *context,
const char *path, shader_type type) {
31 usize size_in_bytes = 0;
32 array_of(u32) code = vulkan_shader_load_code(context, path, &size_in_bytes);
37 VkShaderModuleCreateInfo shader_info = {
38 .sType = VK_STRUCTURE_TYPE_SHADER_MODULE_CREATE_INFO,
39 .codeSize = size_in_bytes,
40 .pCode = (u32 *) code,
43 VkShaderModule shader_module;
46 vkCreateShaderModule(context->device.logical, &shader_info, &context->alloc.vulkan_callbacks, &shader_module));
48 VkPipelineShaderStageCreateInfo shader_stage_info = {
49 .sType = VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO,
50 .module = shader_module,
54 case SHADER_TYPE_VERTEX:
55 shader_stage_info.stage = VK_SHADER_STAGE_VERTEX_BIT;
57 case SHADER_TYPE_FRAGMENT:
58 shader_stage_info.stage = VK_SHADER_STAGE_FRAGMENT_BIT;
61 KB_FATAL(
"unhandled shader type {i32}", type);
64 array_push(context->pipeline.shaders, shader_stage_info);
#define UNUSED(x)
Mark parameter as unused.
#define KB_NULL
Value of an invalid ptr (nullptr).
Interface to access the platform's filesystem.
@ FILE_MODE_READ
Needed to be able to read from a file.
#define KB_ASSERT(expr,...)
Perform runtime assertion and log failures.
#define KB_FATAL(...)
Log entry with fatal log level.
#define KB_WARN(...)
Log entry with warn log level.