1 #include <kiba/gpu/buffer.h>
3 #include <kiba/gpu/vulkan/allocator.h>
4 #include <kiba/gpu/vulkan/conv.h>
5 #include <kiba/gpu/vulkan/device.h>
6 #include <kiba/gpu/vulkan/util.h>
7 #include "kiba/gpu/enums.h"
12 VkBufferCreateInfo create_info = {
13 .sType = VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO,
15 .usage = vk_convert_buffer_usage(desc.usage),
16 .sharingMode = VK_SHARING_MODE_EXCLUSIVE,
18 VK_CALL_B8(vkCreateBuffer(device->logical, &create_info, &
vk_alloc.vulkan_callbacks, &buffer->raw));
20 VkMemoryRequirements memory_requirements;
21 vkGetBufferMemoryRequirements(device->logical, buffer->raw, &memory_requirements);
23 VkMemoryPropertyFlagBits properties = 0;
24 if (
KB_FLAGS_ANY_SET(desc.usage, GPU_BUFFER_USAGE_MAP_READ | GPU_BUFFER_USAGE_MAP_WRITE)) {
25 properties |= VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT | VK_MEMORY_PROPERTY_HOST_COHERENT_BIT;
27 properties |= VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT;
30 if (!vk_device_allocate_memory(*device, &buffer->memory, memory_requirements, properties)) {
31 KB_ERROR(
"could not allocate memory for buffer");
34 VK_CALL_B8(vkBindBufferMemory(device->logical, buffer->raw, buffer->memory, 0));
37 vk_device_set_object_name(device->logical, VK_OBJECT_TYPE_BUFFER, buffer->raw, desc.label);
42 if (!buffer->mapped) {
43 VkResult res = vkMapMemory(device->logical, buffer->memory, 0, VK_WHOLE_SIZE, 0, &buffer->mapped);
44 if (res != VK_SUCCESS) {
48 return buffer->mapped;
53 vkUnmapMemory(device->logical, buffer->memory);
55 vkDestroyBuffer(device->logical, buffer->raw, &
vk_alloc.vulkan_callbacks);
56 vkFreeMemory(device->logical, buffer->memory, &
vk_alloc.vulkan_callbacks);
#define KB_FLAGS_ANY_SET(value, flags)
Check if any of the flags are set inside value.
#define KB_NULL
Value of an invalid ptr (nullptr).
#define KB_ERROR(...)
Log entry with error log level.