kiba-engine
vertex_buffer.c
1 #include <kiba/renderer/vulkan/vertex_buffer.h>
2 
3 #include <kiba/core/memory.h>
4 #include <kiba/renderer/vulkan/buffer.h>
5 
7 
8 b8 vulkan_vertex_buffer_create(vulkan_context *context) {
9  /*const vulkan_vertex verticies[] = {
10  {.x = -0.5f, .y = -0.5f, .z = 0.0f, .r = 1.0f, .g = 0.0f, .b = 0.0f, .tx = 1.0f, .ty = 0.0f},
11  { .x = 0.5f, .y = -0.5f, .z = 0.0f, .r = 0.0f, .g = 1.0f, .b = 0.0f, .tx = 0.0f, .ty = 0.0f},
12  { .x = 0.5f, .y = 0.5f, .z = 0.0f, .r = 0.0f, .g = 0.0f, .b = 1.0f, .tx = 0.0f, .ty = 1.0f},
13  {.x = -0.5f, .y = 0.5f, .z = 0.0f, .r = 1.0f, .g = 1.0f, .b = 1.0f, .tx = 1.0f, .ty = 1.0f},
14  {.x = -0.5f, .y = -0.5f, .z = -0.5f, .r = 1.0f, .g = 0.0f, .b = 0.0f, .tx = 1.0f, .ty = 0.0f},
15  { .x = 0.5f, .y = -0.5f, .z = -0.5f, .r = 0.0f, .g = 1.0f, .b = 0.0f, .tx = 0.0f, .ty = 0.0f},
16  { .x = 0.5f, .y = 0.5f, .z = -0.5f, .r = 0.0f, .g = 0.0f, .b = 1.0f, .tx = 0.0f, .ty = 1.0f},
17  {.x = -0.5f, .y = 0.5f, .z = -0.5f, .r = 1.0f, .g = 1.0f, .b = 1.0f, .tx = 1.0f, .ty = 1.0f},
18  };*/
19 
20  file_handle file;
21  // if (!filesystem_open(&file, "./assets/models/sphere_vertices", true, FILE_MODE_READ)) {
22  if (!filesystem_open(&file, "./assets/models/viking_room_vertices", true, FILE_MODE_READ)) {
23  KB_ERROR("could not open vertices file");
24  return false;
25  }
26  b8 ret = false;
27  usize file_size;
28  KB_ASSERT(filesystem_size(&file, &file_size), "must work");
29 
30  VkDeviceSize size = file_size;
31  vulkan_buffer staging;
32  if (vulkan_buffer_create(context,
33  size,
34  VK_BUFFER_USAGE_TRANSFER_SRC_BIT,
35  VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT | VK_MEMORY_PROPERTY_HOST_COHERENT_BIT,
36  &staging)) {
37  usize bytes_read;
38  void *data;
39  VK_CALL_B8(vkMapMemory(context->device.logical, staging.memory, 0, size, 0, &data));
40  if (filesystem_read_all(&file, file_size, data, &bytes_read)) {
41  if (vulkan_buffer_create(context,
42  size,
43  VK_BUFFER_USAGE_TRANSFER_DST_BIT | VK_BUFFER_USAGE_VERTEX_BUFFER_BIT,
44  VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT,
45  &context->vertex_data)) {
46  ret = vulkan_buffer_copy(context, &staging, &context->vertex_data, size);
47  }
48  }
49  vkUnmapMemory(context->device.logical, staging.memory);
50  vulkan_buffer_destroy(context, &staging);
51  }
52  filesystem_close(&file);
53  return ret;
54 }
55 
56 void vulkan_vertex_buffer_destroy(vulkan_context *context) { vulkan_buffer_destroy(context, &context->vertex_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