PeriDyno 1.0.0
Loading...
Searching...
No Matches
VulkanBuffer.cpp
Go to the documentation of this file.
1/*
2* Vulkan buffer class
3*
4* Encapsulates a Vulkan buffer
5*
6* Copyright (C) 2016 by Sascha Willems - www.saschawillems.de
7*
8* This code is licensed under the MIT license (MIT) (http://opensource.org/licenses/MIT)
9*/
10
11#include "VulkanBuffer.h"
12#include "VulkanTools.h"
13#include "VkSystem.h"
14#include "VkContext.h"
15#include "VulkanMemoryAllocator/include/vk_mem_alloc.h"
16
17namespace vks
18{
19 Buffer::Buffer(VkDevice dev)
20 {
22
23 if (dev == nullptr && ctx == VK_NULL_HANDLE) {
24 vks::tools::exitFatal("Vulkan library should be initialized first! \n", 0);
25 }
26
27 device = dev == nullptr ? ctx->deviceHandle() : dev;
28 }
29
30
32 {
33 destroy();
34 }
35
44 VkResult Buffer::map(VkDeviceSize size, VkDeviceSize offset)
45 {
46 if (usePool) {
47 return vmaMapMemory(allocator, allocation, &mapped);
48 } else {
49 return vkMapMemory(device, memory, offset, size, 0, &mapped);
50 }
51 }
52
59 {
60 if (mapped) {
61 if (usePool) {
62 vmaUnmapMemory(allocator, allocation);
63 } else {
64 vkUnmapMemory(device, memory);
65 }
66 mapped = nullptr;
67 }
68 }
69
77 VkResult Buffer::bind(VkDeviceSize offset)
78 {
79 if (usePool) {
80 return vmaBindBufferMemory(allocator, allocation, buffer);
81 } else {
82 return vkBindBufferMemory(device, buffer, memory, offset);
83 }
84 }
85
93 void Buffer::setupDescriptor(VkDeviceSize size, VkDeviceSize offset)
94 {
95 descriptor.offset = offset;
96 descriptor.buffer = buffer;
97 descriptor.range = size;
98 }
99
107 void Buffer::copyTo(void* data, VkDeviceSize size)
108 {
109 assert(mapped);
110 memcpy(mapped, data, size);
111 }
112
123 VkResult Buffer::flush(VkDeviceSize size, VkDeviceSize offset)
124 {
125 if (usePool) {
126 return vmaFlushAllocation(allocator, allocation, this->offset, this->size);
127 } else {
128 VkMappedMemoryRange mappedRange = {};
129 mappedRange.sType = VK_STRUCTURE_TYPE_MAPPED_MEMORY_RANGE;
130 mappedRange.memory = memory;
131 mappedRange.offset = offset;
132 mappedRange.size = size;
133 return vkFlushMappedMemoryRanges(device, 1, &mappedRange);
134 }
135 }
136
147 VkResult Buffer::invalidate(VkDeviceSize size, VkDeviceSize offset)
148 {
149 if (usePool) {
150 return vmaInvalidateAllocation(allocator, allocation, this->offset, this->size);
151 } else {
152 VkMappedMemoryRange mappedRange = {};
153 mappedRange.sType = VK_STRUCTURE_TYPE_MAPPED_MEMORY_RANGE;
154 mappedRange.memory = memory;
155 mappedRange.offset = offset;
156 mappedRange.size = size;
157 return vkInvalidateMappedMemoryRanges(device, 1, &mappedRange);
158 }
159 }
160
165 {
166 unmap();
167 if (usePool) {
168 if (buffer) {
169 vmaDestroyBuffer(allocator, buffer, allocation);
170 buffer = VK_NULL_HANDLE;
171 }
172 } else {
173 if (buffer) {
174 vkDestroyBuffer(device, buffer, nullptr);
175 buffer = VK_NULL_HANDLE;
176 }
177 if (memory) {
178 vkFreeMemory(device, memory, nullptr);
179 memory = VK_NULL_HANDLE;
180 }
181 }
182 size = 0;
183 }
184};
assert(queueCount >=1)
VkContext * currentContext()
Definition VkSystem.h:21
static VkSystem * instance()
Definition VkSystem.cpp:10
VkResult bind(VkDeviceSize offset=0)
VmaAllocation allocation
VkDeviceSize size
void copyTo(void *data, VkDeviceSize size)
VkResult flush(VkDeviceSize size=VK_WHOLE_SIZE, VkDeviceSize offset=0)
VkResult map(VkDeviceSize size=VK_WHOLE_SIZE, VkDeviceSize offset=0)
VkBuffer buffer
VkDeviceMemory memory
Buffer(VkDevice dev=nullptr)
VkResult invalidate(VkDeviceSize size=VK_WHOLE_SIZE, VkDeviceSize offset=0)
VkDevice device
VkDeviceSize offset
VkBool32 usePool
void * mapped
VkDescriptorBufferInfo descriptor
VmaAllocator allocator
void setupDescriptor(VkDeviceSize size=VK_WHOLE_SIZE, VkDeviceSize offset=0)
void exitFatal(const std::string &message, int32_t exitCode)