OpenGL: Implement buffers
This commit is contained in:
@@ -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
|
||||
|
||||
@@ -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)
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user