OpenGL: Implement commands buffers

This commit is contained in:
Lynix
2020-05-11 14:12:13 +02:00
parent fe5b70ae1c
commit 6a23d51147
19 changed files with 621 additions and 162 deletions

View File

@@ -8,22 +8,138 @@
#define NAZARA_OPENGLRENDERER_OPENGLCOMMANDBUFFER_HPP
#include <Nazara/Prerequisites.hpp>
#include <Nazara/Core/Color.hpp>
#include <Nazara/Math/Rect.hpp>
#include <Nazara/Renderer/CommandBuffer.hpp>
#include <Nazara/Renderer/CommandBufferBuilder.hpp>
#include <Nazara/OpenGLRenderer/Config.hpp>
#include <Nazara/OpenGLRenderer/OpenGLBuffer.hpp>
#include <Nazara/OpenGLRenderer/OpenGLRenderPipeline.hpp>
#include <Nazara/OpenGLRenderer/OpenGLShaderBinding.hpp>
#include <optional>
#include <variant>
#include <vector>
namespace Nz
{
class OpenGLFramebuffer;
class NAZARA_OPENGLRENDERER_API OpenGLCommandBuffer final : public CommandBuffer
{
public:
inline OpenGLCommandBuffer();
OpenGLCommandBuffer() = default;
OpenGLCommandBuffer(const OpenGLCommandBuffer&) = delete;
OpenGLCommandBuffer(OpenGLCommandBuffer&&) noexcept = default;
~OpenGLCommandBuffer() = default;
inline void BeginDebugRegion(const std::string_view& regionName, const Nz::Color& color);
inline void BindIndexBuffer(GLuint indexBuffer, UInt64 offset = 0);
inline void BindPipeline(const OpenGLRenderPipeline* pipeline);
inline void BindShaderBinding(const OpenGLShaderBinding* binding);
inline void BindVertexBuffer(UInt32 binding, GLuint vertexBuffer, UInt64 offset = 0);
inline void CopyBuffer(GLuint source, GLuint target, UInt64 size, UInt64 sourceOffset = 0, UInt64 targetOffset = 0);
inline void CopyBuffer(const UploadPool::Allocation& allocation, GLuint target, UInt64 size, UInt64 sourceOffset = 0, UInt64 targetOffset = 0);
inline void Draw(UInt32 vertexCount, UInt32 instanceCount = 1, UInt32 firstVertex = 0, UInt32 firstInstance = 0);
inline void DrawIndexed(UInt32 indexCount, UInt32 instanceCount = 1, UInt32 firstVertex = 0, UInt32 firstInstance = 0);
inline void EndDebugRegion();
void Execute();
inline void SetFramebuffer(const OpenGLFramebuffer& framebuffer, const RenderPass& renderPass, std::initializer_list<CommandBufferBuilder::ClearValues> clearValues);
inline void SetScissor(Nz::Recti scissorRegion);
inline void SetViewport(Nz::Recti viewportRegion);
OpenGLCommandBuffer& operator=(const OpenGLCommandBuffer&) = delete;
OpenGLCommandBuffer& operator=(OpenGLCommandBuffer&&) = delete;
private:
struct DrawStates;
void ApplyStates(const GL::Context& context, const DrawStates& states);
struct BeginDebugRegionData
{
std::string regionName;
Nz::Color color;
};
struct CopyBufferData
{
GLuint source;
GLuint target;
UInt64 size;
UInt64 sourceOffset;
UInt64 targetOffset;
};
struct CopyBufferFromMemoryData
{
const void* memory;
GLuint target;
UInt64 size;
UInt64 targetOffset;
};
struct DrawStates
{
struct VertexBuffer
{
GLuint vertexBuffer = 0;
UInt64 offset;
};
GLuint indexBuffer = 0;
const OpenGLRenderPipeline* pipeline = nullptr;
const OpenGLShaderBinding* shaderBindings = nullptr;
UInt64 indexBufferOffset;
std::optional<Nz::Recti> scissorRegion;
std::optional<Nz::Recti> viewportRegion;
std::vector<VertexBuffer> vertexBuffers;
};
struct DrawData
{
DrawStates states;
UInt32 firstInstance;
UInt32 firstVertex;
UInt32 instanceCount;
UInt32 vertexCount;
};
struct DrawIndexedData
{
DrawStates states;
UInt32 firstInstance;
UInt32 firstVertex;
UInt32 indexCount;
UInt32 instanceCount;
};
struct EndDebugRegionData
{
};
struct SetFrameBufferData
{
const OpenGLFramebuffer* framebuffer;
};
using CommandData = std::variant<
BeginDebugRegionData,
CopyBufferData,
CopyBufferFromMemoryData,
DrawData,
DrawIndexedData,
EndDebugRegionData,
SetFrameBufferData
>;
DrawStates m_currentStates;
std::vector<CommandData> m_commands;
};
}

View File

@@ -3,10 +3,124 @@
// For conditions of distribution and use, see copyright notice in Config.hpp
#include <Nazara/OpenGLRenderer/OpenGLCommandBuffer.hpp>
#include <Nazara/OpenGLRenderer/OpenGLFramebuffer.hpp>
#include <stdexcept>
#include <Nazara/OpenGLRenderer/Debug.hpp>
namespace Nz
{
inline void OpenGLCommandBuffer::BeginDebugRegion(const std::string_view& regionName, const Nz::Color& color)
{
BeginDebugRegionData beginDebugRegion;
beginDebugRegion.color = color;
beginDebugRegion.regionName = regionName;
m_commands.emplace_back(std::move(beginDebugRegion));
}
inline void OpenGLCommandBuffer::BindIndexBuffer(GLuint indexBuffer, UInt64 offset)
{
m_currentStates.indexBuffer = indexBuffer;
m_currentStates.indexBufferOffset = offset;
}
inline void OpenGLCommandBuffer::BindPipeline(const OpenGLRenderPipeline* pipeline)
{
m_currentStates.pipeline = pipeline;
}
inline void OpenGLCommandBuffer::BindShaderBinding(const OpenGLShaderBinding* binding)
{
m_currentStates.shaderBindings = binding;
}
inline void OpenGLCommandBuffer::BindVertexBuffer(UInt32 binding, GLuint vertexBuffer, UInt64 offset)
{
if (binding >= m_currentStates.vertexBuffers.size())
m_currentStates.vertexBuffers.resize(binding + 1);
auto& vertexBufferData = m_currentStates.vertexBuffers[binding];
vertexBufferData.offset = offset;
vertexBufferData.vertexBuffer = vertexBuffer;
}
inline void OpenGLCommandBuffer::CopyBuffer(GLuint source, GLuint target, UInt64 size, UInt64 sourceOffset, UInt64 targetOffset)
{
CopyBufferData copyBuffer = {
source,
target,
size,
sourceOffset,
targetOffset
};
m_commands.emplace_back(std::move(copyBuffer));
}
inline void OpenGLCommandBuffer::CopyBuffer(const UploadPool::Allocation& allocation, GLuint target, UInt64 size, UInt64 sourceOffset, UInt64 targetOffset)
{
CopyBufferFromMemoryData copyBuffer = {
static_cast<const UInt8*>(allocation.mappedPtr) + sourceOffset,
target,
size,
targetOffset
};
m_commands.emplace_back(std::move(copyBuffer));
}
inline void OpenGLCommandBuffer::Draw(UInt32 vertexCount, UInt32 instanceCount, UInt32 firstVertex, UInt32 firstInstance)
{
if (!m_currentStates.pipeline)
throw std::runtime_error("no pipeline bound");
DrawData draw;
draw.states = m_currentStates;
draw.firstInstance = firstInstance;
draw.firstVertex = firstVertex;
draw.instanceCount = instanceCount;
draw.vertexCount = vertexCount;
m_commands.emplace_back(std::move(draw));
}
inline void OpenGLCommandBuffer::DrawIndexed(UInt32 indexCount, UInt32 instanceCount, UInt32 firstVertex, UInt32 firstInstance)
{
if (!m_currentStates.pipeline)
throw std::runtime_error("no pipeline bound");
DrawIndexedData draw;
draw.states = m_currentStates;
draw.firstInstance = firstInstance;
draw.firstVertex = firstVertex;
draw.indexCount = indexCount;
draw.instanceCount = instanceCount;
m_commands.emplace_back(std::move(draw));
}
inline void OpenGLCommandBuffer::EndDebugRegion()
{
m_commands.emplace_back(EndDebugRegionData{});
}
inline void OpenGLCommandBuffer::SetFramebuffer(const OpenGLFramebuffer& framebuffer, const RenderPass& /*renderPass*/, std::initializer_list<CommandBufferBuilder::ClearValues> clearValues)
{
SetFrameBufferData setFramebuffer;
setFramebuffer.framebuffer = &framebuffer;
m_commands.emplace_back(std::move(setFramebuffer));
}
inline void OpenGLCommandBuffer::SetScissor(Nz::Recti scissorRegion)
{
m_currentStates.scissorRegion = scissorRegion;
}
inline void OpenGLCommandBuffer::SetViewport(Nz::Recti viewportRegion)
{
m_currentStates.viewportRegion = viewportRegion;
}
}
#include <Nazara/OpenGLRenderer/DebugOff.hpp>

View File

@@ -13,12 +13,12 @@
namespace Nz
{
class OpenGLRenderPass;
class OpenGLCommandBuffer;
class NAZARA_OPENGLRENDERER_API OpenGLCommandBufferBuilder final : public CommandBufferBuilder
{
public:
OpenGLCommandBufferBuilder() = default;
inline OpenGLCommandBufferBuilder(OpenGLCommandBuffer& commandBuffer);
OpenGLCommandBufferBuilder(const OpenGLCommandBufferBuilder&) = delete;
OpenGLCommandBufferBuilder(OpenGLCommandBufferBuilder&&) noexcept = default;
~OpenGLCommandBufferBuilder() = default;
@@ -48,9 +48,12 @@ namespace Nz
OpenGLCommandBufferBuilder& operator=(const OpenGLCommandBufferBuilder&) = delete;
OpenGLCommandBufferBuilder& operator=(OpenGLCommandBufferBuilder&&) = delete;
private:
OpenGLCommandBuffer& m_commandBuffer;
};
}
#include <Nazara/OpenGLRenderer/OpenGLCommandBufferBuilder.inl>
#endif // NAZARA_OPENGLRENDERER_OPENGLCOMMANDBUFFERBUILDER_HPP
#endif

View File

@@ -7,6 +7,10 @@
namespace Nz
{
inline OpenGLCommandBufferBuilder::OpenGLCommandBufferBuilder(OpenGLCommandBuffer& commandBuffer) :
m_commandBuffer(commandBuffer)
{
}
}
#include <Nazara/OpenGLRenderer/DebugOff.hpp>

View File

@@ -10,15 +10,13 @@
#include <Nazara/Prerequisites.hpp>
#include <Nazara/Renderer/CommandPool.hpp>
#include <Nazara/OpenGLRenderer/Config.hpp>
#include <Nazara/OpenGLRenderer/Wrapper/CommandPool.hpp>
namespace Nz
{
class NAZARA_OPENGLRENDERER_API OpenGLCommandPool final : public CommandPool
{
public:
inline OpenGLCommandPool(Vk::Device& device, QueueType queueType);
inline OpenGLCommandPool(Vk::Device& device, UInt32 queueFamilyIndex);
OpenGLCommandPool() = default;
OpenGLCommandPool(const OpenGLCommandPool&) = delete;
OpenGLCommandPool(OpenGLCommandPool&&) noexcept = default;
~OpenGLCommandPool() = default;
@@ -27,9 +25,6 @@ namespace Nz
OpenGLCommandPool& operator=(const OpenGLCommandPool&) = delete;
OpenGLCommandPool& operator=(OpenGLCommandPool&&) = delete;
private:
Vk::CommandPool m_commandPool;
};
}

View File

@@ -8,21 +8,6 @@
namespace Nz
{
inline OpenGLCommandPool::OpenGLCommandPool(Vk::Device& device, QueueType queueType)
{
UInt32 queueFamilyIndex = device.GetDefaultFamilyIndex(queueType);
if (queueFamilyIndex == Vk::Device::InvalidQueue)
throw std::runtime_error("QueueType " + std::to_string(UnderlyingCast(queueType)) + " is not supported");
if (!m_commandPool.Create(device, queueFamilyIndex))
throw std::runtime_error("Failed to create command pool: " + TranslateOpenGLError(m_commandPool.GetLastErrorCode()));
}
inline OpenGLCommandPool::OpenGLCommandPool(Vk::Device& device, UInt32 queueFamilyIndex)
{
if (!m_commandPool.Create(device, queueFamilyIndex))
throw std::runtime_error("Failed to create command pool: " + TranslateOpenGLError(m_commandPool.GetLastErrorCode()));
}
}
#include <Nazara/OpenGLRenderer/DebugOff.hpp>

View File

@@ -14,6 +14,7 @@
namespace Nz
{
class OpenGLCommandBuffer;
class OpenGLRenderWindow;
class NAZARA_OPENGLRENDERER_API OpenGLRenderImage : public RenderImage

View File

@@ -12,6 +12,7 @@
#include <Nazara/Renderer/RenderPipelineLayout.hpp>
#include <Nazara/OpenGLRenderer/Config.hpp>
#include <Nazara/OpenGLRenderer/OpenGLShaderBinding.hpp>
#include <Nazara/OpenGLRenderer/Wrapper/Context.hpp>
#include <Nazara/OpenGLRenderer/Wrapper/CoreFunctions.hpp>
#include <memory>
#include <type_traits>
@@ -53,12 +54,15 @@ namespace Nz
struct TextureDescriptor
{
UInt32 bindingIndex;
GLuint texture;
GLuint sampler;
GL::TextureTarget textureTarget;
};
struct UniformBufferDescriptor
{
UInt32 bindingIndex;
GLuint buffer;
GLintptr offset;
GLsizeiptr size;

View File

@@ -9,6 +9,7 @@
#include <Nazara/Prerequisites.hpp>
#include <Nazara/OpenGLRenderer/Config.hpp>
#include <Nazara/OpenGLRenderer/Wrapper/Context.hpp>
#include <Nazara/Renderer/ShaderBinding.hpp>
namespace Nz
@@ -23,6 +24,8 @@ namespace Nz
OpenGLShaderBinding(OpenGLShaderBinding&&) noexcept = default;
~OpenGLShaderBinding() = default;
void Apply(const GL::Context& context) const;
inline std::size_t GetBindingIndex() const;
inline std::size_t GetPoolIndex() const;
inline const OpenGLRenderPipelineLayout& GetOwner() const;

View File

@@ -24,6 +24,8 @@ namespace Nz
OpenGLShaderStage(OpenGLShaderStage&&) noexcept = default;
~OpenGLShaderStage() = default;
inline const GL::Shader& GetShader() const;
OpenGLShaderStage& operator=(const OpenGLShaderStage&) = delete;
OpenGLShaderStage& operator=(OpenGLShaderStage&&) noexcept = default;

View File

@@ -7,6 +7,10 @@
namespace Nz
{
inline const GL::Shader& OpenGLShaderStage::GetShader() const
{
return m_shader;
}
}
#include <Nazara/OpenGLRenderer/DebugOff.hpp>

View File

@@ -9,7 +9,9 @@
#include <Nazara/Prerequisites.hpp>
#include <Nazara/Core/Algorithm.hpp>
#include <Nazara/OpenGLRenderer/OpenGLVaoCache.hpp>
#include <Nazara/OpenGLRenderer/Wrapper/CoreFunctions.hpp>
#include <Nazara/Renderer/RenderStates.hpp>
#include <array>
#include <string>
#include <unordered_set>
@@ -94,17 +96,21 @@ namespace Nz::GL
inline Context(const OpenGLDevice* device);
virtual ~Context();
void BindBuffer(BufferTarget target, GLuint buffer) const;
void BindBuffer(BufferTarget target, GLuint buffer, bool force = false) const;
void BindFramebuffer(GLuint fbo) const;
void BindFramebuffer(FramebufferTarget target, GLuint fbo) const;
void BindProgram(GLuint program) const;
void BindSampler(UInt32 textureUnit, GLuint sampler) const;
void BindTexture(TextureTarget target, GLuint texture) const;
void BindTexture(UInt32 textureUnit, TextureTarget target, GLuint texture) const;
void BindUniformBuffer(UInt32 uboUnit, GLuint buffer, GLintptr offset, GLsizeiptr size) const;
void BindVertexArray(GLuint vertexArray, bool force = false) const;
virtual void EnableVerticalSync(bool enabled) = 0;
inline const OpenGLDevice* GetDevice() const;
inline ExtensionStatus GetExtensionStatus(Extension extension) const;
inline const OpenGLVaoCache& GetVaoCache() const;
inline const ContextParams& GetParams() const;
inline bool IsExtensionSupported(Extension extension) const;
@@ -116,8 +122,11 @@ namespace Nz::GL
inline void NotifyProgramDestruction(GLuint program) const;
inline void NotifySamplerDestruction(GLuint sampler) const;
inline void NotifyTextureDestruction(GLuint texture) const;
inline void NotifyVertexArrayDestruction(GLuint vao) const;
inline void SetCurrentTextureUnit(UInt32 textureUnit) const;
void SetCurrentTextureUnit(UInt32 textureUnit) const;
void SetScissorBox(GLint x, GLint y, GLsizei width, GLsizei height) const;
void SetViewport(GLint x, GLint y, GLsizei width, GLsizei height) const;
virtual void SwapBuffers() = 0;
@@ -134,6 +143,7 @@ namespace Nz::GL
virtual bool Activate() const = 0;
virtual void Desactivate() const = 0;
virtual const Loader& GetLoader() = 0;
void OnContextRelease();
virtual bool ImplementFallback(const std::string_view& function);
@@ -146,23 +156,41 @@ namespace Nz::GL
struct State
{
struct Box
{
GLint x, y;
GLsizei width, height;
};
struct TextureUnit
{
GLuint sampler = 0;
std::array<GLuint, UnderlyingCast(TextureTarget::Max) + 1> textureTargets = { 0 };
};
struct UniformBufferUnit
{
GLuint buffer = 0;
GLintptr offset = 0;
GLsizeiptr size = 0;
};
std::array<GLuint, UnderlyingCast(BufferTarget::Max) + 1> bufferTargets = { 0 };
std::vector<TextureUnit> textureUnits;
std::vector<UniformBufferUnit> uboUnits;
Box scissorBox;
Box viewport;
GLuint boundProgram = 0;
GLuint boundDrawFBO = 0;
GLuint boundReadFBO = 0;
GLuint boundVertexArray = 0;
UInt32 currentTextureUnit = 0;
RenderStates renderStates;
};
std::array<ExtensionStatus, UnderlyingCast(Extension::Max) + 1> m_extensionStatus;
std::unordered_set<std::string> m_supportedExtensions;
OpenGLVaoCache m_vaoCache;
const OpenGLDevice* m_device;
mutable State m_state;
};

View File

@@ -8,6 +8,7 @@
namespace Nz::GL
{
inline Context::Context(const OpenGLDevice* device) :
m_vaoCache(*this),
m_device(device)
{
}
@@ -22,6 +23,11 @@ namespace Nz::GL
return m_extensionStatus[UnderlyingCast(extension)];
}
inline const OpenGLVaoCache& Context::GetVaoCache() const
{
return m_vaoCache;
}
inline const ContextParams& Context::GetParams() const
{
return m_params;
@@ -73,13 +79,10 @@ namespace Nz::GL
}
}
inline void Context::SetCurrentTextureUnit(UInt32 textureUnit) const
inline void Context::NotifyVertexArrayDestruction(GLuint vao) const
{
if (m_state.currentTextureUnit != textureUnit)
{
glActiveTexture(GL_TEXTURE0 + textureUnit);
m_state.currentTextureUnit = textureUnit;
}
if (m_state.boundVertexArray == vao)
m_state.boundVertexArray = 0;
}
}