OpenGL: Implement buffers

This commit is contained in:
Lynix 2020-04-26 18:20:36 +02:00
parent 494801282d
commit 0b05feb7e3
13 changed files with 213 additions and 209 deletions

View File

@ -10,9 +10,8 @@
#include <Nazara/Prerequisites.hpp>
#include <Nazara/Utility/AbstractBuffer.hpp>
#include <Nazara/OpenGLRenderer/Config.hpp>
#include <Nazara/OpenGLRenderer/OpenGLDevice.hpp>
#include <Nazara/OpenGLRenderer/Wrapper/Buffer.hpp>
#include <Nazara/OpenGLRenderer/Wrapper/DeviceMemory.hpp>
#include <Nazara/OpenGLRenderer/Wrapper/Fence.hpp>
#include <memory>
#include <vector>
@ -21,34 +20,31 @@ namespace Nz
class NAZARA_OPENGLRENDERER_API OpenGLBuffer : public AbstractBuffer
{
public:
inline OpenGLBuffer(Vk::Device& device, BufferType type);
OpenGLBuffer(OpenGLDevice& device, BufferType type);
OpenGLBuffer(const OpenGLBuffer&) = delete;
OpenGLBuffer(OpenGLBuffer&&) = delete; ///TODO
virtual ~OpenGLBuffer();
OpenGLBuffer(OpenGLBuffer&&) = delete;
~OpenGLBuffer() = default;
bool Fill(const void* data, UInt64 offset, UInt64 size) override;
bool Initialize(UInt64 size, BufferUsageFlags usage) override;
inline VkBuffer GetBuffer();
inline const GL::Buffer& GetBuffer() const;
UInt64 GetSize() const override;
DataStorage GetStorage() const override;
inline BufferType GetType() const;
void* Map(BufferAccess access, UInt64 offset, UInt64 size) override;
bool Unmap() override;
OpenGLBuffer& operator=(const OpenGLBuffer&) = delete;
OpenGLBuffer& operator=(OpenGLBuffer&&) = delete; ///TODO
OpenGLBuffer& operator=(OpenGLBuffer&&) = delete;
private:
GL::Buffer m_buffer;
BufferType m_type;
BufferUsageFlags m_usage;
UInt64 m_size;
VkBuffer m_buffer;
VkBuffer m_stagingBuffer;
VmaAllocation m_allocation;
VmaAllocation m_stagingAllocation;
Vk::Device& m_device;
};
}

View File

@ -7,16 +7,15 @@
namespace Nz
{
inline OpenGLBuffer::OpenGLBuffer(Vk::Device& device, BufferType type) :
m_device(device),
m_type(type)
{
}
inline VkBuffer OpenGLBuffer::GetBuffer()
inline const GL::Buffer& OpenGLBuffer::GetBuffer() const
{
return m_buffer;
}
inline BufferType OpenGLBuffer::GetType() const
{
return m_type;
}
}
#include <Nazara/OpenGLRenderer/DebugOff.hpp>

View File

@ -40,7 +40,8 @@ namespace Nz
std::unique_ptr<Texture> InstantiateTexture(const TextureInfo& params) override;
std::unique_ptr<TextureSampler> InstantiateTextureSampler(const TextureSamplerInfo& params) override;
inline void NotifySamplerDestruction(GLuint texture) const;
inline void NotifyBufferDestruction(GLuint buffer) const;
inline void NotifySamplerDestruction(GLuint sampler) const;
inline void NotifyTextureDestruction(GLuint texture) const;
OpenGLDevice& operator=(const OpenGLDevice&) = delete;

View File

@ -12,10 +12,16 @@ namespace Nz
return *m_referenceContext;
}
inline void OpenGLDevice::NotifySamplerDestruction(GLuint texture) const
inline void OpenGLDevice::NotifyBufferDestruction(GLuint buffer) const
{
for (const GL::Context* context : m_contexts)
context->NotifySamplerDestruction(texture);
context->NotifyBufferDestruction(buffer);
}
inline void OpenGLDevice::NotifySamplerDestruction(GLuint sampler) const
{
for (const GL::Context* context : m_contexts)
context->NotifySamplerDestruction(sampler);
}
inline void OpenGLDevice::NotifyTextureDestruction(GLuint texture) const

View File

@ -20,6 +20,8 @@ namespace Nz
inline GLenum ToOpenGL(SamplerFilter minFilter, SamplerMipmapMode mipmapFilter);
inline GLenum ToOpenGL(SamplerWrap wrapMode);
inline GLenum ToOpenGL(ShaderStageType stageType);
inline GLenum ToOpenGL(GL::BufferTarget bufferTarget);
inline GLenum ToOpenGL(GL::TextureTarget bufferTarget);
//NAZARA_OPENGLRENDERER_API std::string TranslateOpenGLError(GLenum code);
}

View File

@ -79,6 +79,38 @@ namespace Nz
NazaraError("Unhandled ShaderStageType 0x" + String::Number(UnderlyingCast(stageType), 16));
return {};
}
GLenum ToOpenGL(GL::BufferTarget bufferTarget)
{
switch (bufferTarget)
{
case GL::BufferTarget::Array: return GL_ARRAY_BUFFER;
case GL::BufferTarget::CopyRead: return GL_COPY_READ_BUFFER;
case GL::BufferTarget::CopyWrite: return GL_COPY_WRITE_BUFFER;
case GL::BufferTarget::ElementArray: return GL_ELEMENT_ARRAY_BUFFER;
case GL::BufferTarget::PixelPack: return GL_PIXEL_PACK_BUFFER;
case GL::BufferTarget::PixelUnpack: return GL_PIXEL_UNPACK_BUFFER;
case GL::BufferTarget::TransformFeedback: return GL_TRANSFORM_FEEDBACK_BUFFER;
case GL::BufferTarget::Uniform: return GL_UNIFORM_BUFFER;
}
NazaraError("Unhandled GL::BufferTarget 0x" + String::Number(UnderlyingCast(bufferTarget), 16));
return {};
}
GLenum ToOpenGL(GL::TextureTarget textureTarget)
{
switch (textureTarget)
{
case GL::TextureTarget::Cubemap: return GL_TEXTURE_CUBE_MAP;
case GL::TextureTarget::Target2D: return GL_TEXTURE_2D;
case GL::TextureTarget::Target2D_Array: return GL_TEXTURE_2D_ARRAY;
case GL::TextureTarget::Target3D: return GL_TEXTURE_3D;
}
NazaraError("Unhandled GL::TextureTarget 0x" + String::Number(UnderlyingCast(textureTarget), 16));
return {};
}
}
#include <Nazara/OpenGLRenderer/DebugOff.hpp>

View File

@ -4,43 +4,45 @@
#pragma once
#ifndef NAZARA_OPENGLRENDERER_VKBUFFER_HPP
#define NAZARA_OPENGLRENDERER_VKBUFFER_HPP
#ifndef NAZARA_OPENGLRENDERER_GLBUFFER_HPP
#define NAZARA_OPENGLRENDERER_GLBUFFER_HPP
#include <Nazara/Prerequisites.hpp>
#include <Nazara/Core/MovableValue.hpp>
#include <Nazara/OpenGLRenderer/OpenGLDevice.hpp>
#include <Nazara/OpenGLRenderer/Wrapper/DeviceObject.hpp>
namespace Nz
namespace Nz::GL
{
namespace Vk
class Buffer : public DeviceObject<Buffer, GL_BUFFER>
{
class Buffer : public DeviceObject<Buffer, VkBuffer, VkBufferCreateInfo, VK_OBJECT_TYPE_BUFFER>
{
friend DeviceObject;
friend DeviceObject;
public:
Buffer() = default;
Buffer(const Buffer&) = delete;
Buffer(Buffer&&) noexcept = default;
~Buffer() = default;
public:
Buffer() = default;
Buffer(const Buffer&) = delete;
Buffer(Buffer&&) noexcept = default;
~Buffer() = default;
bool BindBufferMemory(VkDeviceMemory memory, VkDeviceSize offset = 0);
inline void* MapRange(GLintptr offset, GLsizeiptr length, GLbitfield access);
using DeviceObject::Create;
inline bool Create(Device& device, VkBufferCreateFlags flags, VkDeviceSize size, VkBufferUsageFlags usage, const VkAllocationCallbacks* allocator = nullptr);
inline void Reset(BufferTarget target, GLsizeiptr size, const void* initialData, GLenum usage);
VkMemoryRequirements GetMemoryRequirements() const;
inline void SubData(GLintptr offset, GLsizeiptr size, const void* data);
Buffer& operator=(const Buffer&) = delete;
Buffer& operator=(Buffer&&) = delete;
inline bool Unmap();
private:
static inline VkResult CreateHelper(Device& device, const VkBufferCreateInfo* createInfo, const VkAllocationCallbacks* allocator, VkBuffer* handle);
static inline void DestroyHelper(Device& device, VkBuffer handle, const VkAllocationCallbacks* allocator);
};
}
Buffer& operator=(const Buffer&) = delete;
Buffer& operator=(Buffer&&) noexcept = default;
private:
static inline GLuint CreateHelper(OpenGLDevice& device, const Context& context);
static inline void DestroyHelper(OpenGLDevice& device, const Context& context, GLuint objectId);
BufferTarget m_target;
};
}
#include <Nazara/OpenGLRenderer/Wrapper/Buffer.inl>
#endif // NAZARA_OPENGLRENDERER_VKBUFFER_HPP
#endif

View File

@ -5,57 +5,54 @@
#include <Nazara/OpenGLRenderer/Wrapper/Buffer.hpp>
#include <Nazara/OpenGLRenderer/Debug.hpp>
namespace Nz
namespace Nz::GL
{
namespace Vk
inline void* Buffer::MapRange(GLintptr offset, GLsizeiptr length, GLbitfield access)
{
inline bool Buffer::BindBufferMemory(VkDeviceMemory memory, VkDeviceSize offset)
{
m_lastErrorCode = m_device->vkBindBufferMemory(*m_device, m_handle, memory, offset);
if (m_lastErrorCode != VK_SUCCESS)
{
NazaraError("Failed to bind buffer memory");
return false;
}
const Context& context = EnsureDeviceContext();
context.BindBuffer(m_target, m_objectId);
return context.glMapBufferRange(ToOpenGL(m_target), offset, length, access);
}
return true;
}
inline void Buffer::Reset(BufferTarget target, GLsizeiptr size, const void* initialData, GLenum usage)
{
m_target = target;
inline bool Buffer::Create(Device& device, VkBufferCreateFlags flags, VkDeviceSize size, VkBufferUsageFlags usage, const VkAllocationCallbacks* allocator)
{
VkBufferCreateInfo createInfo = {
VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO, // VkStructureType sType;
nullptr, // const void* pNext;
flags, // VkBufferCreateFlags flags;
size, // VkDeviceSize size;
usage, // VkBufferUsageFlags usage;
VK_SHARING_MODE_EXCLUSIVE, // VkSharingMode sharingMode;
0, // uint32_t queueFamilyIndexCount;
nullptr // const uint32_t* pQueueFamilyIndices;
};
const Context& context = EnsureDeviceContext();
return Create(device, createInfo, allocator);
}
context.BindBuffer(m_target, m_objectId);
inline VkMemoryRequirements Buffer::GetMemoryRequirements() const
{
NazaraAssert(IsValid(), "Invalid buffer");
context.glBufferData(ToOpenGL(m_target), size, initialData, usage);
}
VkMemoryRequirements memoryRequirements;
m_device->vkGetBufferMemoryRequirements(*m_device, m_handle, &memoryRequirements);
inline void Buffer::SubData(GLintptr offset, GLsizeiptr size, const void* data)
{
const Context& context = EnsureDeviceContext();
context.BindBuffer(m_target, m_objectId);
return memoryRequirements;
}
context.glBufferSubData(ToOpenGL(m_target), offset, size, data);
}
inline VkResult Buffer::CreateHelper(Device& device, const VkBufferCreateInfo* createInfo, const VkAllocationCallbacks* allocator, VkBuffer* handle)
{
return device.vkCreateBuffer(device, createInfo, allocator, handle);
}
inline bool Buffer::Unmap()
{
const Context& context = EnsureDeviceContext();
context.BindBuffer(m_target, m_objectId);
return context.glUnmapBuffer(ToOpenGL(m_target)) == GL_TRUE;
}
inline void Buffer::DestroyHelper(Device& device, VkBuffer handle, const VkAllocationCallbacks* allocator)
{
return device.vkDestroyBuffer(device, handle, allocator);
}
inline GLuint Buffer::CreateHelper(OpenGLDevice& device, const Context& context)
{
GLuint sampler = 0;
context.glGenBuffers(1U, &sampler);
return sampler;
}
inline void Buffer::DestroyHelper(OpenGLDevice& device, const Context& context, GLuint objectId)
{
context.glDeleteBuffers(1U, &objectId);
device.NotifyBufferDestruction(objectId);
}
}

View File

@ -21,6 +21,20 @@ namespace Nz
namespace Nz::GL
{
enum class BufferTarget
{
Array,
CopyRead,
CopyWrite,
ElementArray,
PixelPack,
PixelUnpack,
TransformFeedback,
Uniform,
Max = Uniform
};
enum class ContextType
{
OpenGL,
@ -74,6 +88,7 @@ namespace Nz::GL
inline Context(const OpenGLDevice* device);
virtual ~Context();
void BindBuffer(BufferTarget target, GLuint buffer) const;
void BindSampler(UInt32 textureUnit, GLuint sampler) const;
void BindTexture(TextureTarget target, GLuint texture) const;
void BindTexture(UInt32 textureUnit, TextureTarget target, GLuint texture) const;
@ -89,6 +104,7 @@ namespace Nz::GL
bool Initialize(const ContextParams& params);
inline void NotifyBufferDestruction(GLuint buffer) const;
inline void NotifySamplerDestruction(GLuint sampler) const;
inline void NotifyTextureDestruction(GLuint texture) const;
@ -125,6 +141,7 @@ namespace Nz::GL
std::array<GLuint, UnderlyingCast(TextureTarget::Max) + 1> textureTargets = { 0 };
};
std::array<GLuint, UnderlyingCast(BufferTarget::Max) + 1> bufferTargets = { 0 };
std::vector<TextureUnit> textureUnits;
UInt32 currentTextureUnit = 0;
};

View File

@ -37,6 +37,15 @@ namespace Nz::GL
return m_supportedExtensions.find(extension) != m_supportedExtensions.end();
}
inline void Context::NotifyBufferDestruction(GLuint buffer) const
{
for (GLuint& boundBuffer : m_state.bufferTargets)
{
if (boundBuffer == buffer)
boundBuffer = 0;
}
}
inline void Context::NotifySamplerDestruction(GLuint sampler) const
{
for (auto& unit : m_state.textureUnits)

View File

@ -2,33 +2,25 @@
// This file is part of the "Nazara Engine - OpenGL Renderer"
// For conditions of distribution and use, see copyright notice in Config.hpp
#if 0
#include <Nazara/OpenGLRenderer/OpenGLBuffer.hpp>
#include <Nazara/Core/CallOnExit.hpp>
#include <Nazara/Core/String.hpp>
#include <Nazara/OpenGLRenderer/Wrapper/CommandBuffer.hpp>
#include <Nazara/OpenGLRenderer/Wrapper/QueueHandle.hpp>
#include <vma/vk_mem_alloc.h>
#include <stdexcept>
#include <Nazara/OpenGLRenderer/Debug.hpp>
namespace Nz
{
OpenGLBuffer::~OpenGLBuffer()
OpenGLBuffer::OpenGLBuffer(OpenGLDevice& device, BufferType type) :
m_type(type)
{
vmaDestroyBuffer(m_device.GetMemoryAllocator(), m_buffer, m_allocation);
if (!m_buffer.Create(device))
throw std::runtime_error("failed to create buffer"); //< TODO: Handle error
}
bool OpenGLBuffer::Fill(const void* data, UInt64 offset, UInt64 size)
{
void* ptr = Map(BufferAccess_WriteOnly, offset, size);
if (!ptr)
return false;
Nz::CallOnExit unmapOnExit([this]() { Unmap(); });
std::memcpy(ptr, data, size);
m_buffer.SubData(offset, size, data);
return true;
}
@ -37,37 +29,26 @@ namespace Nz
m_size = size;
m_usage = usage;
VkBufferUsageFlags bufferUsage = ToOpenGL(m_type);
GL::BufferTarget target;
switch (m_type)
{
case BufferType_Index: target = GL::BufferTarget::ElementArray; break;
case BufferType_Uniform: target = GL::BufferTarget::Uniform; break;
case BufferType_Vertex: target = GL::BufferTarget::Array; break;
if ((usage & BufferUsage_DirectMapping) == 0)
bufferUsage |= VK_BUFFER_USAGE_TRANSFER_DST_BIT;
default:
throw std::runtime_error("unknown buffer type 0x" + String::Number(UnderlyingCast(m_type), 16).ToStdString());
}
VkBufferCreateInfo createInfo = {};
createInfo.sType = VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO;
createInfo.size = size;
createInfo.usage = bufferUsage;
GLenum hint = GL_STREAM_COPY;
VmaAllocationCreateInfo allocInfo = {};
if (usage & BufferUsage_DeviceLocal)
{
if (usage & BufferUsage_DirectMapping)
allocInfo.usage = VMA_MEMORY_USAGE_CPU_TO_GPU;
else
allocInfo.usage = VMA_MEMORY_USAGE_GPU_ONLY;
}
else
allocInfo.usage = VMA_MEMORY_USAGE_CPU_ONLY;
hint = GL_STATIC_DRAW;
if (usage & BufferUsage_PersistentMapping)
allocInfo.flags |= VMA_ALLOCATION_CREATE_MAPPED_BIT;
VkResult result = vmaCreateBuffer(m_device.GetMemoryAllocator(), &createInfo, &allocInfo, &m_buffer, &m_allocation, nullptr);
if (result != VK_SUCCESS)
{
NazaraError("Failed to allocate buffer: " + TranslateOpenGLError(result));
return false;
}
if (usage & BufferUsage_DirectMapping)
hint = GL_DYNAMIC_COPY;
m_buffer.Reset(target, size, nullptr, hint);
return true;
}
@ -81,71 +62,41 @@ namespace Nz
return DataStorage_Hardware;
}
void* OpenGLBuffer::Map(BufferAccess /*access*/, UInt64 offset, UInt64 size)
void* OpenGLBuffer::Map(BufferAccess access, UInt64 offset, UInt64 size)
{
if (m_usage & BufferUsage_DirectMapping)
GLbitfield accessBit = 0;
switch (access)
{
void* mappedPtr;
VkResult result = vmaMapMemory(m_device.GetMemoryAllocator(), m_allocation, &mappedPtr);
if (result != VK_SUCCESS)
{
NazaraError("Failed to map buffer: " + TranslateOpenGLError(result));
return nullptr;
}
case BufferAccess_DiscardAndWrite:
accessBit |= GL_MAP_WRITE_BIT;
if (offset == 0 && size == m_size)
accessBit |= GL_MAP_INVALIDATE_BUFFER_BIT;
else
accessBit |= GL_MAP_INVALIDATE_RANGE_BIT;
return static_cast<UInt8*>(mappedPtr) + offset;
break;
case BufferAccess_ReadOnly:
accessBit |= GL_MAP_READ_BIT;
break;
case BufferAccess_ReadWrite:
accessBit |= GL_MAP_READ_BIT | GL_MAP_WRITE_BIT;
break;
case BufferAccess_WriteOnly:
accessBit |= GL_MAP_WRITE_BIT;
break;
default:
break;
}
else
{
VkBufferCreateInfo createInfo = {};
createInfo.sType = VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO;
createInfo.size = size;
createInfo.usage = VK_BUFFER_USAGE_TRANSFER_SRC_BIT;
VmaAllocationCreateInfo allocInfo = {};
allocInfo.flags = VMA_ALLOCATION_CREATE_MAPPED_BIT;
allocInfo.usage = VMA_MEMORY_USAGE_CPU_TO_GPU;
VmaAllocationInfo allocationInfo;
VkResult result = vmaCreateBuffer(m_device.GetMemoryAllocator(), &createInfo, &allocInfo, &m_stagingBuffer, &m_stagingAllocation, &allocationInfo);
if (result != VK_SUCCESS)
{
NazaraError("Failed to allocate staging buffer: " + TranslateOpenGLError(result));
return nullptr;
}
return allocationInfo.pMappedData;
}
return m_buffer.MapRange(offset, size, accessBit);
}
bool OpenGLBuffer::Unmap()
{
if (m_usage & BufferUsage_DirectMapping)
{
vmaUnmapMemory(m_device.GetMemoryAllocator(), m_allocation);
return true;
}
else
{
Vk::AutoCommandBuffer copyCommandBuffer = m_device.AllocateCommandBuffer(QueueType::Transfer);
if (!copyCommandBuffer->Begin(VK_COMMAND_BUFFER_USAGE_ONE_TIME_SUBMIT_BIT))
return false;
copyCommandBuffer->CopyBuffer(m_stagingBuffer, m_buffer, m_size);
if (!copyCommandBuffer->End())
return false;
Vk::QueueHandle transferQueue = m_device.GetQueue(m_device.GetDefaultFamilyIndex(QueueType::Transfer), 0);
if (!transferQueue.Submit(copyCommandBuffer))
return false;
transferQueue.WaitIdle();
vmaDestroyBuffer(m_device.GetMemoryAllocator(), m_stagingBuffer, m_stagingAllocation);
return true;
}
return m_buffer.Unmap();
}
}
#endif

View File

@ -4,6 +4,7 @@
#include <Nazara/OpenGLRenderer/OpenGLDevice.hpp>
#include <Nazara/Renderer/CommandPool.hpp>
#include <Nazara/OpenGLRenderer/OpenGLBuffer.hpp>
#include <Nazara/OpenGLRenderer/OpenGLShaderStage.hpp>
#include <Nazara/OpenGLRenderer/OpenGLTexture.hpp>
#include <Nazara/OpenGLRenderer/Wrapper/Loader.hpp>
@ -42,7 +43,7 @@ namespace Nz
std::unique_ptr<AbstractBuffer> OpenGLDevice::InstantiateBuffer(BufferType type)
{
return {};
return std::make_unique<OpenGLBuffer>(*this, type);
}
std::unique_ptr<CommandPool> OpenGLDevice::InstantiateCommandPool(QueueType queueType)

View File

@ -7,6 +7,7 @@
#include <Nazara/Core/Log.hpp>
#include <Nazara/Core/StringExt.hpp>
#include <Nazara/OpenGLRenderer/OpenGLDevice.hpp>
#include <Nazara/OpenGLRenderer/Utils.hpp>
#include <Nazara/OpenGLRenderer/Wrapper/Loader.hpp>
#include <sstream>
#include <stdexcept>
@ -22,6 +23,19 @@ namespace Nz::GL
m_device->NotifyContextDestruction(*this);
}
void Context::BindBuffer(BufferTarget target, GLuint buffer) const
{
if (m_state.bufferTargets[UnderlyingCast(target)] != buffer)
{
if (!SetCurrentContext(this))
throw std::runtime_error("failed to activate context");
glBindBuffer(ToOpenGL(target), buffer);
m_state.bufferTargets[UnderlyingCast(target)] = buffer;
}
}
void Context::BindSampler(UInt32 textureUnit, GLuint sampler) const
{
if (textureUnit >= m_state.textureUnits.size())
@ -56,30 +70,7 @@ namespace Nz::GL
SetCurrentTextureUnit(textureUnit);
GLenum glTarget;
switch (target)
{
case TextureTarget::Cubemap:
glTarget = GL_TEXTURE_CUBE_MAP;
break;
case TextureTarget::Target2D:
glTarget = GL_TEXTURE_2D;
break;
case TextureTarget::Target2D_Array:
glTarget = GL_TEXTURE_2D_ARRAY;
break;
case TextureTarget::Target3D:
glTarget = GL_TEXTURE_3D;
break;
default:
break;
}
glBindTexture(glTarget, texture);
glBindTexture(ToOpenGL(target), texture);
unit.textureTargets[UnderlyingCast(target)] = texture;
}