kiba-engine
index_buffer.c
1 #include <kiba/renderer/vulkan/index_buffer.h>
2 
3 #include <kiba/core/memory.h>
4 #include <kiba/renderer/vulkan/buffer.h>
5 
7 
8 b8 vulkan_index_buffer_create(vulkan_context *context) {
9  // const u32 indicies[] = {0, 1, 2, 2, 3, 0, 4, 5, 6, 6, 7, 4};
10  context->index_count = 0;
11 
12  file_handle file;
13  // if (!filesystem_open(&file, "./assets/models/sphere_indices", true, FILE_MODE_READ)) {
14  if (!filesystem_open(&file, "./assets/models/viking_room_indices", true, FILE_MODE_READ)) {
15  KB_ERROR("could not open indices file");
16  return false;
17  }
18  b8 ret = false;
19  usize file_size;
20  KB_ASSERT(filesystem_size(&file, &file_size), "must work");
21 
22  VkDeviceSize size = file_size;
23  vulkan_buffer staging;
24  if (vulkan_buffer_create(context,
25  size,
26  VK_BUFFER_USAGE_TRANSFER_SRC_BIT,
27  VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT | VK_MEMORY_PROPERTY_HOST_COHERENT_BIT,
28  &staging)) {
29  usize bytes_read;
30  void *data;
31  VK_CALL_B8(vkMapMemory(context->device.logical, staging.memory, 0, size, 0, &data));
32  if (filesystem_read_all(&file, file_size, data, &bytes_read)) {
33  if (vulkan_buffer_create(context,
34  size,
35  VK_BUFFER_USAGE_TRANSFER_DST_BIT | VK_BUFFER_USAGE_INDEX_BUFFER_BIT,
36  VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT,
37  &context->index_data)) {
38  ret = vulkan_buffer_copy(context, &staging, &context->index_data, size);
39  context->index_count = (u32) (bytes_read / sizeof(u32));
40  }
41  }
42  vkUnmapMemory(context->device.logical, staging.memory);
43  vulkan_buffer_destroy(context, &staging);
44  }
45  filesystem_close(&file);
46  return ret;
47 }
48 
49 void vulkan_index_buffer_destroy(vulkan_context *context) { vulkan_buffer_destroy(context, &context->index_data); }
Lightweight layer between platform and other engine components to enable tracing/monitoring.
Interface to access the platform's filesystem.
@ FILE_MODE_READ
Needed to be able to read from a file.
Definition: filesystem.h:17
#define KB_ASSERT(expr,...)
Perform runtime assertion and log failures.
Definition: log.h:133
#define KB_ERROR(...)
Log entry with error log level.
Definition: log.h:142
Handle for a file.
Definition: filesystem.h:25