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

@@ -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)