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/Prerequisites.hpp>
#include <Nazara/Utility/AbstractBuffer.hpp> #include <Nazara/Utility/AbstractBuffer.hpp>
#include <Nazara/OpenGLRenderer/Config.hpp> #include <Nazara/OpenGLRenderer/Config.hpp>
#include <Nazara/OpenGLRenderer/OpenGLDevice.hpp>
#include <Nazara/OpenGLRenderer/Wrapper/Buffer.hpp> #include <Nazara/OpenGLRenderer/Wrapper/Buffer.hpp>
#include <Nazara/OpenGLRenderer/Wrapper/DeviceMemory.hpp>
#include <Nazara/OpenGLRenderer/Wrapper/Fence.hpp>
#include <memory> #include <memory>
#include <vector> #include <vector>
@ -21,34 +20,31 @@ namespace Nz
class NAZARA_OPENGLRENDERER_API OpenGLBuffer : public AbstractBuffer class NAZARA_OPENGLRENDERER_API OpenGLBuffer : public AbstractBuffer
{ {
public: public:
inline OpenGLBuffer(Vk::Device& device, BufferType type); OpenGLBuffer(OpenGLDevice& device, BufferType type);
OpenGLBuffer(const OpenGLBuffer&) = delete; OpenGLBuffer(const OpenGLBuffer&) = delete;
OpenGLBuffer(OpenGLBuffer&&) = delete; ///TODO OpenGLBuffer(OpenGLBuffer&&) = delete;
virtual ~OpenGLBuffer(); ~OpenGLBuffer() = default;
bool Fill(const void* data, UInt64 offset, UInt64 size) override; bool Fill(const void* data, UInt64 offset, UInt64 size) override;
bool Initialize(UInt64 size, BufferUsageFlags usage) override; bool Initialize(UInt64 size, BufferUsageFlags usage) override;
inline VkBuffer GetBuffer(); inline const GL::Buffer& GetBuffer() const;
UInt64 GetSize() const override; UInt64 GetSize() const override;
DataStorage GetStorage() const override; DataStorage GetStorage() const override;
inline BufferType GetType() const;
void* Map(BufferAccess access, UInt64 offset, UInt64 size) override; void* Map(BufferAccess access, UInt64 offset, UInt64 size) override;
bool Unmap() override; bool Unmap() override;
OpenGLBuffer& operator=(const OpenGLBuffer&) = delete; OpenGLBuffer& operator=(const OpenGLBuffer&) = delete;
OpenGLBuffer& operator=(OpenGLBuffer&&) = delete; ///TODO OpenGLBuffer& operator=(OpenGLBuffer&&) = delete;
private: private:
GL::Buffer m_buffer;
BufferType m_type; BufferType m_type;
BufferUsageFlags m_usage; BufferUsageFlags m_usage;
UInt64 m_size; 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 namespace Nz
{ {
inline OpenGLBuffer::OpenGLBuffer(Vk::Device& device, BufferType type) : inline const GL::Buffer& OpenGLBuffer::GetBuffer() const
m_device(device),
m_type(type)
{
}
inline VkBuffer OpenGLBuffer::GetBuffer()
{ {
return m_buffer; return m_buffer;
} }
inline BufferType OpenGLBuffer::GetType() const
{
return m_type;
}
} }
#include <Nazara/OpenGLRenderer/DebugOff.hpp> #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<Texture> InstantiateTexture(const TextureInfo& params) override;
std::unique_ptr<TextureSampler> InstantiateTextureSampler(const TextureSamplerInfo& 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; inline void NotifyTextureDestruction(GLuint texture) const;
OpenGLDevice& operator=(const OpenGLDevice&) = delete; OpenGLDevice& operator=(const OpenGLDevice&) = delete;

View File

@ -12,10 +12,16 @@ namespace Nz
return *m_referenceContext; return *m_referenceContext;
} }
inline void OpenGLDevice::NotifySamplerDestruction(GLuint texture) const inline void OpenGLDevice::NotifyBufferDestruction(GLuint buffer) const
{ {
for (const GL::Context* context : m_contexts) 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 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(SamplerFilter minFilter, SamplerMipmapMode mipmapFilter);
inline GLenum ToOpenGL(SamplerWrap wrapMode); inline GLenum ToOpenGL(SamplerWrap wrapMode);
inline GLenum ToOpenGL(ShaderStageType stageType); 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); //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)); NazaraError("Unhandled ShaderStageType 0x" + String::Number(UnderlyingCast(stageType), 16));
return {}; 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> #include <Nazara/OpenGLRenderer/DebugOff.hpp>

View File

@ -4,17 +4,17 @@
#pragma once #pragma once
#ifndef NAZARA_OPENGLRENDERER_VKBUFFER_HPP #ifndef NAZARA_OPENGLRENDERER_GLBUFFER_HPP
#define NAZARA_OPENGLRENDERER_VKBUFFER_HPP #define NAZARA_OPENGLRENDERER_GLBUFFER_HPP
#include <Nazara/Prerequisites.hpp> #include <Nazara/Prerequisites.hpp>
#include <Nazara/Core/MovableValue.hpp>
#include <Nazara/OpenGLRenderer/OpenGLDevice.hpp>
#include <Nazara/OpenGLRenderer/Wrapper/DeviceObject.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;
@ -24,23 +24,25 @@ namespace Nz
Buffer(Buffer&&) noexcept = default; Buffer(Buffer&&) noexcept = default;
~Buffer() = default; ~Buffer() = default;
bool BindBufferMemory(VkDeviceMemory memory, VkDeviceSize offset = 0); inline void* MapRange(GLintptr offset, GLsizeiptr length, GLbitfield access);
using DeviceObject::Create; inline void Reset(BufferTarget target, GLsizeiptr size, const void* initialData, GLenum usage);
inline bool Create(Device& device, VkBufferCreateFlags flags, VkDeviceSize size, VkBufferUsageFlags usage, const VkAllocationCallbacks* allocator = nullptr);
VkMemoryRequirements GetMemoryRequirements() const; inline void SubData(GLintptr offset, GLsizeiptr size, const void* data);
inline bool Unmap();
Buffer& operator=(const Buffer&) = delete; Buffer& operator=(const Buffer&) = delete;
Buffer& operator=(Buffer&&) = delete; Buffer& operator=(Buffer&&) noexcept = default;
private: private:
static inline VkResult CreateHelper(Device& device, const VkBufferCreateInfo* createInfo, const VkAllocationCallbacks* allocator, VkBuffer* handle); static inline GLuint CreateHelper(OpenGLDevice& device, const Context& context);
static inline void DestroyHelper(Device& device, VkBuffer handle, const VkAllocationCallbacks* allocator); static inline void DestroyHelper(OpenGLDevice& device, const Context& context, GLuint objectId);
BufferTarget m_target;
}; };
} }
}
#include <Nazara/OpenGLRenderer/Wrapper/Buffer.inl> #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/Wrapper/Buffer.hpp>
#include <Nazara/OpenGLRenderer/Debug.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) const Context& context = EnsureDeviceContext();
{ context.BindBuffer(m_target, m_objectId);
m_lastErrorCode = m_device->vkBindBufferMemory(*m_device, m_handle, memory, offset); return context.glMapBufferRange(ToOpenGL(m_target), offset, length, access);
if (m_lastErrorCode != VK_SUCCESS)
{
NazaraError("Failed to bind buffer memory");
return false;
} }
return true; inline void Buffer::Reset(BufferTarget target, GLsizeiptr size, const void* initialData, GLenum usage)
}
inline bool Buffer::Create(Device& device, VkBufferCreateFlags flags, VkDeviceSize size, VkBufferUsageFlags usage, const VkAllocationCallbacks* allocator)
{ {
VkBufferCreateInfo createInfo = { m_target = target;
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;
};
return Create(device, createInfo, allocator); const Context& context = EnsureDeviceContext();
context.BindBuffer(m_target, m_objectId);
context.glBufferData(ToOpenGL(m_target), size, initialData, usage);
} }
inline VkMemoryRequirements Buffer::GetMemoryRequirements() const inline void Buffer::SubData(GLintptr offset, GLsizeiptr size, const void* data)
{ {
NazaraAssert(IsValid(), "Invalid buffer"); const Context& context = EnsureDeviceContext();
context.BindBuffer(m_target, m_objectId);
VkMemoryRequirements memoryRequirements; context.glBufferSubData(ToOpenGL(m_target), offset, size, data);
m_device->vkGetBufferMemoryRequirements(*m_device, m_handle, &memoryRequirements);
return memoryRequirements;
} }
inline VkResult Buffer::CreateHelper(Device& device, const VkBufferCreateInfo* createInfo, const VkAllocationCallbacks* allocator, VkBuffer* handle) inline bool Buffer::Unmap()
{ {
return device.vkCreateBuffer(device, createInfo, allocator, handle); 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) inline GLuint Buffer::CreateHelper(OpenGLDevice& device, const Context& context)
{ {
return device.vkDestroyBuffer(device, handle, allocator); 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 namespace Nz::GL
{ {
enum class BufferTarget
{
Array,
CopyRead,
CopyWrite,
ElementArray,
PixelPack,
PixelUnpack,
TransformFeedback,
Uniform,
Max = Uniform
};
enum class ContextType enum class ContextType
{ {
OpenGL, OpenGL,
@ -74,6 +88,7 @@ namespace Nz::GL
inline Context(const OpenGLDevice* device); inline Context(const OpenGLDevice* device);
virtual ~Context(); virtual ~Context();
void BindBuffer(BufferTarget target, GLuint buffer) const;
void BindSampler(UInt32 textureUnit, GLuint sampler) const; void BindSampler(UInt32 textureUnit, GLuint sampler) const;
void BindTexture(TextureTarget target, GLuint texture) const; void BindTexture(TextureTarget target, GLuint texture) const;
void BindTexture(UInt32 textureUnit, 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); bool Initialize(const ContextParams& params);
inline void NotifyBufferDestruction(GLuint buffer) const;
inline void NotifySamplerDestruction(GLuint sampler) const; inline void NotifySamplerDestruction(GLuint sampler) const;
inline void NotifyTextureDestruction(GLuint texture) 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(TextureTarget::Max) + 1> textureTargets = { 0 };
}; };
std::array<GLuint, UnderlyingCast(BufferTarget::Max) + 1> bufferTargets = { 0 };
std::vector<TextureUnit> textureUnits; std::vector<TextureUnit> textureUnits;
UInt32 currentTextureUnit = 0; UInt32 currentTextureUnit = 0;
}; };

View File

@ -37,6 +37,15 @@ namespace Nz::GL
return m_supportedExtensions.find(extension) != m_supportedExtensions.end(); 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 inline void Context::NotifySamplerDestruction(GLuint sampler) const
{ {
for (auto& unit : m_state.textureUnits) for (auto& unit : m_state.textureUnits)

View File

@ -2,33 +2,25 @@
// This file is part of the "Nazara Engine - OpenGL Renderer" // This file is part of the "Nazara Engine - OpenGL Renderer"
// For conditions of distribution and use, see copyright notice in Config.hpp // For conditions of distribution and use, see copyright notice in Config.hpp
#if 0
#include <Nazara/OpenGLRenderer/OpenGLBuffer.hpp> #include <Nazara/OpenGLRenderer/OpenGLBuffer.hpp>
#include <Nazara/Core/CallOnExit.hpp> #include <Nazara/Core/CallOnExit.hpp>
#include <Nazara/Core/String.hpp> #include <Nazara/Core/String.hpp>
#include <Nazara/OpenGLRenderer/Wrapper/CommandBuffer.hpp> #include <stdexcept>
#include <Nazara/OpenGLRenderer/Wrapper/QueueHandle.hpp>
#include <vma/vk_mem_alloc.h>
#include <Nazara/OpenGLRenderer/Debug.hpp> #include <Nazara/OpenGLRenderer/Debug.hpp>
namespace Nz 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) bool OpenGLBuffer::Fill(const void* data, UInt64 offset, UInt64 size)
{ {
void* ptr = Map(BufferAccess_WriteOnly, offset, size); m_buffer.SubData(offset, size, data);
if (!ptr)
return false;
Nz::CallOnExit unmapOnExit([this]() { Unmap(); });
std::memcpy(ptr, data, size);
return true; return true;
} }
@ -37,37 +29,26 @@ namespace Nz
m_size = size; m_size = size;
m_usage = usage; 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) default:
bufferUsage |= VK_BUFFER_USAGE_TRANSFER_DST_BIT; throw std::runtime_error("unknown buffer type 0x" + String::Number(UnderlyingCast(m_type), 16).ToStdString());
}
VkBufferCreateInfo createInfo = {}; GLenum hint = GL_STREAM_COPY;
createInfo.sType = VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO;
createInfo.size = size;
createInfo.usage = bufferUsage;
VmaAllocationCreateInfo allocInfo = {};
if (usage & BufferUsage_DeviceLocal) if (usage & BufferUsage_DeviceLocal)
{ hint = GL_STATIC_DRAW;
if (usage & BufferUsage_DirectMapping) if (usage & BufferUsage_DirectMapping)
allocInfo.usage = VMA_MEMORY_USAGE_CPU_TO_GPU; hint = GL_DYNAMIC_COPY;
else
allocInfo.usage = VMA_MEMORY_USAGE_GPU_ONLY;
}
else
allocInfo.usage = VMA_MEMORY_USAGE_CPU_ONLY;
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;
}
m_buffer.Reset(target, size, nullptr, hint);
return true; return true;
} }
@ -81,71 +62,41 @@ namespace Nz
return DataStorage_Hardware; 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; case BufferAccess_DiscardAndWrite:
VkResult result = vmaMapMemory(m_device.GetMemoryAllocator(), m_allocation, &mappedPtr); accessBit |= GL_MAP_WRITE_BIT;
if (result != VK_SUCCESS) if (offset == 0 && size == m_size)
{ accessBit |= GL_MAP_INVALIDATE_BUFFER_BIT;
NazaraError("Failed to map buffer: " + TranslateOpenGLError(result));
return nullptr;
}
return static_cast<UInt8*>(mappedPtr) + offset;
}
else else
{ accessBit |= GL_MAP_INVALIDATE_RANGE_BIT;
VkBufferCreateInfo createInfo = {};
createInfo.sType = VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO;
createInfo.size = size;
createInfo.usage = VK_BUFFER_USAGE_TRANSFER_SRC_BIT;
VmaAllocationCreateInfo allocInfo = {}; break;
allocInfo.flags = VMA_ALLOCATION_CREATE_MAPPED_BIT;
allocInfo.usage = VMA_MEMORY_USAGE_CPU_TO_GPU;
VmaAllocationInfo allocationInfo; case BufferAccess_ReadOnly:
accessBit |= GL_MAP_READ_BIT;
break;
VkResult result = vmaCreateBuffer(m_device.GetMemoryAllocator(), &createInfo, &allocInfo, &m_stagingBuffer, &m_stagingAllocation, &allocationInfo); case BufferAccess_ReadWrite:
if (result != VK_SUCCESS) accessBit |= GL_MAP_READ_BIT | GL_MAP_WRITE_BIT;
{ break;
NazaraError("Failed to allocate staging buffer: " + TranslateOpenGLError(result));
return nullptr; case BufferAccess_WriteOnly:
accessBit |= GL_MAP_WRITE_BIT;
break;
default:
break;
} }
return allocationInfo.pMappedData; return m_buffer.MapRange(offset, size, accessBit);
}
} }
bool OpenGLBuffer::Unmap() bool OpenGLBuffer::Unmap()
{ {
if (m_usage & BufferUsage_DirectMapping) return m_buffer.Unmap();
{
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;
} }
} }
}
#endif

View File

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

View File

@ -7,6 +7,7 @@
#include <Nazara/Core/Log.hpp> #include <Nazara/Core/Log.hpp>
#include <Nazara/Core/StringExt.hpp> #include <Nazara/Core/StringExt.hpp>
#include <Nazara/OpenGLRenderer/OpenGLDevice.hpp> #include <Nazara/OpenGLRenderer/OpenGLDevice.hpp>
#include <Nazara/OpenGLRenderer/Utils.hpp>
#include <Nazara/OpenGLRenderer/Wrapper/Loader.hpp> #include <Nazara/OpenGLRenderer/Wrapper/Loader.hpp>
#include <sstream> #include <sstream>
#include <stdexcept> #include <stdexcept>
@ -22,6 +23,19 @@ namespace Nz::GL
m_device->NotifyContextDestruction(*this); 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 void Context::BindSampler(UInt32 textureUnit, GLuint sampler) const
{ {
if (textureUnit >= m_state.textureUnits.size()) if (textureUnit >= m_state.textureUnits.size())
@ -56,30 +70,7 @@ namespace Nz::GL
SetCurrentTextureUnit(textureUnit); SetCurrentTextureUnit(textureUnit);
GLenum glTarget; glBindTexture(ToOpenGL(target), texture);
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);
unit.textureTargets[UnderlyingCast(target)] = texture; unit.textureTargets[UnderlyingCast(target)] = texture;
} }