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