summaryrefslogtreecommitdiff
path: root/src/resources/buffer.cpp
blob: 856846621f2e134d3508d23d90bef009b10649eb (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
#include "resources/buffer.h"
#include "vulkan_helper.h"
#include <memory>

namespace iris {

void *Buffer_tt::map() {
    if (!mapped_data.has_value()) {
        CHECK_VULKAN(vmaMapMemory(allocator, allocation, &mapped_data.value()));
    }
    return mapped_data.value();
}

void Buffer_tt::unmap() {
    if (mapped_data.has_value()) {
        vmaUnmapMemory(allocator, allocation);
        mapped_data.reset();
    } else {
        spdlog::warn("Buffer is not mapped, skipping");
    }
}

void Buffer_tt::release() {
    if (handle != VK_NULL_HANDLE) {
        spdlog::debug("Destroying buffer: 0x{:x}", (uint64_t)handle);
        vkDeviceWaitIdle(device);
        vmaDestroyBuffer(allocator, handle, allocation);
        handle = VK_NULL_HANDLE;
    }
}

std::shared_ptr<Buffer_tt> Device::create_buffer(BufferDesc desc) {
    VkBufferCreateInfo buffer_create_info = {
        .sType = VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO,
        .size = desc.size,
        .usage = desc.buffer_usage,
    };
    VmaAllocationCreateInfo alloc_create_info = {
        .usage = desc.memory_usage,
    };
    std::shared_ptr<Buffer_tt> buffer = std::make_shared<Buffer_tt>();
    buffer->device = this->device;
    buffer->allocator = this->allocator;
    buffer->desc = desc;
    CHECK_VULKAN(vmaCreateBuffer(
        allocator,
        &buffer_create_info,
        &alloc_create_info,
        &buffer->handle,
        &buffer->allocation,
        VK_NULL_HANDLE));
    spdlog::debug("Created buffer: 0x{:x}", (uint64_t)buffer->handle);
    return buffer;
}

} // namespace iris