kiba-engine
shader.c
1 #include <kiba/core/memory.h>
2 #include <kiba/gpu/shader.h>
3 #include <kiba/gpu/vulkan/allocator.h>
4 #include <kiba/gpu/vulkan/device.h>
5 #include <kiba/gpu/vulkan/util.h>
6 
7 b8 gpu_backend_shader_module_create(struct gpu_backend_shader_module *shader,
8  struct gpu_backend_device *device,
9  struct gpu_shader_module_descriptor desc) {
10  usize size_in_bytes = 0;
11  if (desc.code == KB_NULL || (size_in_bytes = array_size(desc.code)) == 0) {
12  KB_ERROR("provided invalid shader code");
13  return false;
14  }
15  u32 code_size = (size_in_bytes + 3u) & ~3u; // ensure code size is multiple of 4 with enough space for code
16  array_of(u32) vk_code = array_create(u32, code_size / sizeof(u32), &vk_alloc.kiba_alloc);
17  if (vk_code == KB_NULL) {
18  KB_ERROR("cannot reserve memory to transfer shader code");
19  return false;
20  }
21  memory_copy(vk_code, desc.code, size_in_bytes);
22 
23  VkShaderModuleCreateInfo shader_info = {
24  .sType = VK_STRUCTURE_TYPE_SHADER_MODULE_CREATE_INFO,
25  .codeSize = code_size,
26  .pCode = vk_code,
27  };
28  // TODO: this will leak in case its not successfull, could add cleanup option to macro
29  VK_CALL_B8(vkCreateShaderModule(device->logical, &shader_info, &vk_alloc.vulkan_callbacks, &shader->raw));
30  array_destroy(&vk_code);
31  vk_device_set_object_name(device->logical, VK_OBJECT_TYPE_SHADER_MODULE, shader->raw, desc.label);
32  return true;
33 }
34 
35 void gpu_backend_shader_module_destroy(struct gpu_backend_shader_module *shader, struct gpu_backend_device *device) {
36  if (shader->raw != VK_NULL_HANDLE) {
37  vkDestroyShaderModule(device->logical, shader->raw, &vk_alloc.vulkan_callbacks);
38  shader->raw = VK_NULL_HANDLE;
39  }
40 }
void * memory_copy(void *dst, const void *src, usize size)
Copy memory.
Definition: memory.c:83
Lightweight layer between platform and other engine components to enable tracing/monitoring.
#define KB_NULL
Value of an invalid ptr (nullptr).
Definition: defines.h:18
#define KB_ERROR(...)
Log entry with error log level.
Definition: log.h:142