Skip to content
Snippets Groups Projects
Verified Commit 362702b3 authored by Tobias Frisch's avatar Tobias Frisch
Browse files

Added create()-function for buffer to allow the code to handle issues

parent 7f5c76a8
Branches
Tags
1 merge request!18Resolve "Resource Management"
Pipeline #24908 passed
...@@ -44,7 +44,6 @@ namespace vkcv { ...@@ -44,7 +44,6 @@ namespace vkcv {
m_Buffer(other.m_Buffer), m_Buffer(other.m_Buffer),
m_BufferMemory(other.m_BufferMemory), m_BufferMemory(other.m_BufferMemory),
m_Device(other.m_Device), m_Device(other.m_Device),
m_MemoryRequirement(other.m_MemoryRequirement),
m_Type(other.m_Type), m_Type(other.m_Type),
m_Size(other.m_Size), m_Size(other.m_Size),
m_DataP(other.m_DataP) m_DataP(other.m_DataP)
...@@ -52,7 +51,6 @@ namespace vkcv { ...@@ -52,7 +51,6 @@ namespace vkcv {
other.m_Buffer = nullptr; other.m_Buffer = nullptr;
other.m_BufferMemory = nullptr; other.m_BufferMemory = nullptr;
other.m_Device = nullptr; other.m_Device = nullptr;
//other.m_MemoryRequirement = nullptr; // WIP alternative to nullptr has to be found
other.m_Type = vkcv::VERTEX; //set to 0 other.m_Type = vkcv::VERTEX; //set to 0
other.m_Size = 0; other.m_Size = 0;
other.m_DataP = nullptr; other.m_DataP = nullptr;
...@@ -63,7 +61,6 @@ namespace vkcv { ...@@ -63,7 +61,6 @@ namespace vkcv {
m_Buffer = other.m_Buffer; m_Buffer = other.m_Buffer;
m_BufferMemory = other.m_BufferMemory; m_BufferMemory = other.m_BufferMemory;
m_Device = other.m_Device; m_Device = other.m_Device;
m_MemoryRequirement = other.m_MemoryRequirement;
m_Type = other.m_Type; m_Type = other.m_Type;
m_Size = other.m_Size; m_Size = other.m_Size;
m_DataP = other.m_DataP; m_DataP = other.m_DataP;
...@@ -71,7 +68,6 @@ namespace vkcv { ...@@ -71,7 +68,6 @@ namespace vkcv {
other.m_Buffer = nullptr; other.m_Buffer = nullptr;
other.m_BufferMemory = nullptr; other.m_BufferMemory = nullptr;
other.m_Device = nullptr; other.m_Device = nullptr;
//other.m_MemoryRequirement = nullptr; // WIP alternative to nullptr has to be found
other.m_Type = vkcv::VERTEX; //set to 0 other.m_Type = vkcv::VERTEX; //set to 0
other.m_Size = 0; other.m_Size = 0;
other.m_DataP = nullptr; other.m_DataP = nullptr;
...@@ -83,40 +79,106 @@ namespace vkcv { ...@@ -83,40 +79,106 @@ namespace vkcv {
// TODO: we will probably need staging-buffer here later (possible add in BufferManager later?) // TODO: we will probably need staging-buffer here later (possible add in BufferManager later?)
void fill(T* data, size_t count) { void fill(T* data, size_t count) {
const vk::MemoryRequirements requirements = m_Device.getBufferMemoryRequirements(m_Buffer);
// TODO: check if mapped already // TODO: check if mapped already
m_DataP = static_cast<uint8_t*>(m_Device.mapMemory(m_BufferMemory, 0, m_MemoryRequirement.size)); m_DataP = static_cast<uint8_t*>(m_Device.mapMemory(m_BufferMemory, 0, requirements.size));
memcpy(m_DataP, data, sizeof(T) * count); memcpy(m_DataP, data, sizeof(T) * count);
m_Device.unmapMemory(m_BufferMemory); m_Device.unmapMemory(m_BufferMemory);
}; };
void map() { T* map() {
m_DataP = static_cast<uint8_t*>(m_Device.mapMemory(m_BufferMemory, 0, m_MemoryRequirement.size)); const vk::MemoryRequirements requirements = m_Device.getBufferMemoryRequirements(m_Buffer);
m_DataP = static_cast<uint8_t*>(m_Device.mapMemory(m_BufferMemory, 0, requirements.size));
// TODO: make sure to unmap before deallocation // TODO: make sure to unmap before deallocation
return reinterpret_cast<T*>(m_DataP);
}; };
void unmap() { void unmap() {
m_Device.unmapMemory(m_BufferMemory); m_Device.unmapMemory(m_BufferMemory);
// TODO: mark m_DataP as invalid? // TODO: mark m_DataP as invalid?
}; };
/**
* * Create function of #Buffer requires a @p device, a @p physicalDevice, a @p buffer type and a @p size. *
* @param device Vulkan-Device
* @param physicalDevice Vulkan-PhysicalDevice
* @param type Enum type of possible vkcv::BufferType
* @param Size size of data
*/
static Buffer<T> create(vk::Device device, vk::PhysicalDevice physicalDevice, BufferType type, size_t size) {
vk::Buffer buffer = nullptr;
switch (type) {
case VERTEX: {
//create vertex buffer
buffer = device.createBuffer(vk::BufferCreateInfo(vk::BufferCreateFlags(), sizeof(T) * size, vk::BufferUsageFlagBits::eVertexBuffer));
}
default: {
// TODO: maybe an issue
}
}
if (!buffer) {
//TODO: potential issue
}
//get requirements for allocation
const vk::MemoryRequirements requirements = device.getBufferMemoryRequirements(buffer);
//find Memory Type
uint32_t memoryType = searchMemoryType(physicalDevice.getMemoryProperties(), requirements.memoryTypeBits, vk::MemoryPropertyFlagBits::eHostVisible | vk::MemoryPropertyFlagBits::eHostCoherent);
//allocate memory for buffer
vk::DeviceMemory memory = device.allocateMemory(vk::MemoryAllocateInfo(requirements.size, memoryType));
if (!memory) {
//TODO: other potential issue
}
device.bindBufferMemory(buffer, memory, 0);
return Buffer<T>(buffer, memory, device, type, size);
}
private: private:
vk::Buffer m_Buffer; vk::Buffer m_Buffer;
vk::DeviceMemory m_BufferMemory; vk::DeviceMemory m_BufferMemory;
vk::Device m_Device; vk::Device m_Device;
vk::MemoryRequirements m_MemoryRequirement;
BufferType m_Type; BufferType m_Type;
size_t m_Size=0; size_t m_Size=0;
uint8_t* m_DataP; uint8_t* m_DataP;
/**
* * Constructor of #Buffer requires a @p buffer, a @p memory, @p device, @ requirement, a @p buffer type and a @p size.
* @param buffer Vulkan-Buffer
* @param memory Vulkan-DeviceMemory
* @param device Vulkan-Device
* @param requirement Vulkan-MemoryRequirements
* @param type Enum type of possible vkcv::BufferType
* @param Size size of data
*/
Buffer<T>(vk::Buffer buffer, vk::DeviceMemory memory, vk::Device device, BufferType type, size_t size) :
m_Buffer(buffer),
m_BufferMemory(memory),
m_Device(device),
m_Type(type),
m_Size(size),
m_DataP(nullptr)
{}
/** /**
* @brief searches memory type index for buffer allocation, inspired by vulkan tutorial and "https://github.com/KhronosGroup/Vulkan-Hpp/blob/master/samples/utils/utils.hpp" * @brief searches memory type index for buffer allocation, inspired by vulkan tutorial and "https://github.com/KhronosGroup/Vulkan-Hpp/blob/master/samples/utils/utils.hpp"
* @param physicalMemoryProperties Memory Properties of physical device * @param physicalMemoryProperties Memory Properties of physical device
* @param typeBits * @param typeBits
* @param requirements Property flags that are required * @param requirements Property flags that are required
* @return memory type index for Buffer * @return memory type index for Buffer
*/ */
uint32_t searchMemoryType(vk::PhysicalDeviceMemoryProperties const& physicalMemoryProperties, uint32_t typeBits, vk::MemoryPropertyFlags requirements) { static uint32_t searchMemoryType(vk::PhysicalDeviceMemoryProperties const& physicalMemoryProperties, uint32_t typeBits, vk::MemoryPropertyFlags requirements) {
uint32_t memoryTypeIndex = uint32_t(0); uint32_t memoryTypeIndex = uint32_t(0);
for (uint32_t i = 0; i < physicalMemoryProperties.memoryTypeCount; i++) for (uint32_t i = 0; i < physicalMemoryProperties.memoryTypeCount; i++)
{ {
if ((typeBits & 1) && if ((typeBits & 1) &&
...@@ -127,36 +189,8 @@ namespace vkcv { ...@@ -127,36 +189,8 @@ namespace vkcv {
} }
typeBits >>= 1; typeBits >>= 1;
} }
return memoryTypeIndex; return memoryTypeIndex;
};
/**
* * Constructor of #Buffer requires a @p device, a @p physicalDevice, a @p buffer type and a @p size. *
* @param device Vulkan-Device
* @param physicalDevice Vulkan-PhysicalDevice
* @param type Enum type of possible vkcv::BufferType
* @param Size size of data
*/
Buffer<T>(vk::Device device, vk::PhysicalDevice physicalDevice, BufferType type, size_t size) {
m_Type = type;
m_Size = size;
m_Device = device;
m_DataP = nullptr;
switch (m_Type) {
case VERTEX: {
//create vertex buffer
m_Buffer = m_Device.createBuffer(vk::BufferCreateInfo(vk::BufferCreateFlags(), sizeof(T) * m_Size, vk::BufferUsageFlagBits::eVertexBuffer));
}
}
//get requirements for allocation
m_MemoryRequirement = m_Device.getBufferMemoryRequirements(m_Buffer);
//find Memory Type
uint32_t memoryType = searchMemoryType(physicalDevice.getMemoryProperties(), m_MemoryRequirement.memoryTypeBits, vk::MemoryPropertyFlagBits::eHostVisible | vk::MemoryPropertyFlagBits::eHostCoherent);
//allocate memory for buffer
m_BufferMemory = m_Device.allocateMemory(vk::MemoryAllocateInfo(m_MemoryRequirement.size, memoryType));
device.bindBufferMemory(m_Buffer, m_BufferMemory, 0);
} }
}; };
......
...@@ -100,10 +100,9 @@ namespace vkcv ...@@ -100,10 +100,9 @@ namespace vkcv
*/ */
template<typename T> template<typename T>
Buffer<T> createBuffer(vkcv::BufferType bufferType,size_t size) { Buffer<T> createBuffer(vkcv::BufferType bufferType,size_t size) {
Buffer<T> buffer(m_Context.getDevice(), m_Context.getPhysicalDevice(), bufferType, size); return Buffer<T>::create(m_Context.getDevice(), m_Context.getPhysicalDevice(), bufferType, size);
return std::move(buffer); }
//return std::move(Buffer<T>(m_Context.getDevice(),m_Context.getPhysicalDevice, bufferType,size));
};
PassHandle createRenderPass(const Renderpass &pass) ; PassHandle createRenderPass(const Renderpass &pass) ;
PipelineHandle createPipeline(const Pipeline &pipeline); PipelineHandle createPipeline(const Pipeline &pipeline);
......
...@@ -21,6 +21,17 @@ int main(int argc, const char** argv) { ...@@ -21,6 +21,17 @@ int main(int argc, const char** argv) {
const vk::Instance& instance = context.getInstance(); const vk::Instance& instance = context.getInstance();
const vk::PhysicalDevice& physicalDevice = context.getPhysicalDevice(); const vk::PhysicalDevice& physicalDevice = context.getPhysicalDevice();
const vk::Device& device = context.getDevice(); const vk::Device& device = context.getDevice();
struct vec3 {
float x, y, z;
};
auto buffer = core.createBuffer<vec3>(vkcv::BufferType::VERTEX, 3);
vec3* m = buffer.map();
m[0] = { 0, 0, 0 };
m[0] = { 0, 0, 0 };
m[0] = { 0, 0, 0 };
std::cout << "Physical device: " << physicalDevice.getProperties().deviceName << std::endl; std::cout << "Physical device: " << physicalDevice.getProperties().deviceName << std::endl;
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment