OpenGL: Implement commands buffers
This commit is contained in:
@@ -2,13 +2,141 @@
|
||||
// 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/OpenGLCommandBuffer.hpp>
|
||||
#include <Nazara/OpenGLRenderer/OpenGLVaoCache.hpp>
|
||||
#include <Nazara/OpenGLRenderer/Wrapper/Context.hpp>
|
||||
#include <Nazara/OpenGLRenderer/Wrapper/VertexArray.hpp>
|
||||
#include <Nazara/OpenGLRenderer/Debug.hpp>
|
||||
|
||||
namespace Nz
|
||||
{
|
||||
}
|
||||
namespace
|
||||
{
|
||||
void BuildAttrib(GL::OpenGLVaoSetup::Attribs& attrib, ComponentType component)
|
||||
{
|
||||
switch (component)
|
||||
{
|
||||
case ComponentType_Color:
|
||||
attrib.normalized = GL_TRUE;
|
||||
attrib.size = 4;
|
||||
attrib.type = GL_UNSIGNED_BYTE;
|
||||
return;
|
||||
|
||||
#endif
|
||||
case ComponentType_Float1:
|
||||
case ComponentType_Float2:
|
||||
case ComponentType_Float3:
|
||||
case ComponentType_Float4:
|
||||
attrib.normalized = GL_FALSE;
|
||||
attrib.size = (component - ComponentType_Float1 + 1);
|
||||
attrib.type = GL_FLOAT;
|
||||
return;
|
||||
|
||||
case ComponentType_Int1:
|
||||
case ComponentType_Int2:
|
||||
case ComponentType_Int3:
|
||||
case ComponentType_Int4:
|
||||
attrib.normalized = GL_FALSE;
|
||||
attrib.size = (component - ComponentType_Int1 + 1);
|
||||
attrib.type = GL_INT;
|
||||
return;
|
||||
|
||||
case ComponentType_Double1:
|
||||
case ComponentType_Double2:
|
||||
case ComponentType_Double3:
|
||||
case ComponentType_Double4:
|
||||
case ComponentType_Quaternion:
|
||||
break;
|
||||
}
|
||||
|
||||
throw std::runtime_error(("component type 0x" + String::Number(component, 16) + " is not handled").ToStdString());
|
||||
}
|
||||
}
|
||||
|
||||
void OpenGLCommandBuffer::Execute()
|
||||
{
|
||||
const GL::Context* context = GL::Context::GetCurrentContext();
|
||||
|
||||
for (const auto& command : m_commands)
|
||||
{
|
||||
std::visit([&](auto&& command)
|
||||
{
|
||||
using T = std::decay_t<decltype(command)>;
|
||||
|
||||
if constexpr (std::is_same_v<T, BeginDebugRegionData> || std::is_same_v<T, EndDebugRegionData>)
|
||||
{
|
||||
// TODO
|
||||
}
|
||||
else if constexpr (std::is_same_v<T, CopyBufferData>)
|
||||
{
|
||||
context->BindBuffer(GL::BufferTarget::CopyRead, command.source);
|
||||
context->BindBuffer(GL::BufferTarget::CopyWrite, command.target);
|
||||
context->glCopyBufferSubData(GL_COPY_READ_BUFFER, GL_COPY_WRITE_BUFFER, command.sourceOffset, command.targetOffset, command.size);
|
||||
}
|
||||
else if constexpr (std::is_same_v<T, CopyBufferFromMemoryData>)
|
||||
{
|
||||
context->BindBuffer(GL::BufferTarget::CopyWrite, command.target);
|
||||
context->glBufferSubData(GL_COPY_WRITE_BUFFER, command.targetOffset, command.size, command.memory);
|
||||
}
|
||||
else if constexpr (std::is_same_v<T, DrawData>)
|
||||
{
|
||||
ApplyStates(*context, m_currentStates);
|
||||
context->glDrawArraysInstanced(GL_TRIANGLES, command.firstVertex, command.vertexCount, command.instanceCount);
|
||||
}
|
||||
else if constexpr (std::is_same_v<T, DrawIndexedData>)
|
||||
{
|
||||
ApplyStates(*context, m_currentStates);
|
||||
context->glDrawElementsInstanced(GL_TRIANGLES, command.indexCount, GL_UNSIGNED_SHORT, nullptr, command.instanceCount);
|
||||
}
|
||||
else if constexpr (std::is_same_v<T, SetFrameBufferData>)
|
||||
{
|
||||
command.framebuffer->Activate();
|
||||
|
||||
context = GL::Context::GetCurrentContext();
|
||||
context->glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
|
||||
}
|
||||
else
|
||||
static_assert(AlwaysFalse<T>::value, "non-exhaustive visitor");
|
||||
|
||||
}, command);
|
||||
}
|
||||
}
|
||||
|
||||
void OpenGLCommandBuffer::ApplyStates(const GL::Context& context, const DrawStates& states)
|
||||
{
|
||||
states.shaderBindings->Apply(context);
|
||||
states.pipeline->Apply(context);
|
||||
|
||||
if (states.scissorRegion)
|
||||
context.SetScissorBox(states.scissorRegion->x, states.scissorRegion->y, states.scissorRegion->width, states.scissorRegion->height);
|
||||
|
||||
if (states.viewportRegion)
|
||||
context.SetViewport(states.viewportRegion->x, states.viewportRegion->y, states.viewportRegion->width, states.viewportRegion->height);
|
||||
|
||||
GL::OpenGLVaoSetup vaoSetup;
|
||||
vaoSetup.indexBuffer = states.indexBuffer;
|
||||
|
||||
std::uint32_t locationIndex = 0;
|
||||
const std::uint8_t* originPtr = 0;
|
||||
|
||||
for (const auto& bufferData : states.pipeline->GetPipelineInfo().vertexBuffers)
|
||||
{
|
||||
assert(bufferData.binding < states.vertexBuffers.size());
|
||||
const auto& vertexBufferInfo = states.vertexBuffers[bufferData.binding];
|
||||
|
||||
GLsizei stride = GLsizei(bufferData.declaration->GetStride());
|
||||
|
||||
for (const auto& componentInfo : *bufferData.declaration)
|
||||
{
|
||||
auto& bufferAttribute = vaoSetup.vertexAttribs[locationIndex++].emplace();
|
||||
BuildAttrib(bufferAttribute, componentInfo.type);
|
||||
|
||||
bufferAttribute.pointer = originPtr + vertexBufferInfo.offset + componentInfo.offset;
|
||||
bufferAttribute.stride = stride;
|
||||
bufferAttribute.vertexBuffer = vertexBufferInfo.vertexBuffer;
|
||||
}
|
||||
}
|
||||
|
||||
const GL::VertexArray& vao = context.GetVaoCache().Get(vaoSetup);
|
||||
context.BindVertexArray(vao.GetObjectId(), true);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -2,16 +2,12 @@
|
||||
// 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/OpenGLCommandBufferBuilder.hpp>
|
||||
#include <Nazara/Core/StackArray.hpp>
|
||||
#include <Nazara/OpenGLRenderer/OpenGLBuffer.hpp>
|
||||
#include <Nazara/OpenGLRenderer/OpenGLMultipleFramebuffer.hpp>
|
||||
#include <Nazara/OpenGLRenderer/OpenGLCommandBuffer.hpp>
|
||||
#include <Nazara/OpenGLRenderer/OpenGLRenderPass.hpp>
|
||||
#include <Nazara/OpenGLRenderer/OpenGLRenderPipeline.hpp>
|
||||
#include <Nazara/OpenGLRenderer/OpenGLRenderPipelineLayout.hpp>
|
||||
#include <Nazara/OpenGLRenderer/OpenGLSingleFramebuffer.hpp>
|
||||
#include <Nazara/OpenGLRenderer/OpenGLShaderBinding.hpp>
|
||||
#include <Nazara/OpenGLRenderer/OpenGLUploadPool.hpp>
|
||||
#include <Nazara/OpenGLRenderer/Debug.hpp>
|
||||
@@ -20,112 +16,40 @@ namespace Nz
|
||||
{
|
||||
void OpenGLCommandBufferBuilder::BeginDebugRegion(const std::string_view& regionName, const Nz::Color& color)
|
||||
{
|
||||
// Ensure \0 at the end of string
|
||||
StackArray<char> regionNameEOS = NazaraStackArrayNoInit(char, regionName.size() + 1);
|
||||
std::memcpy(regionNameEOS.data(), regionName.data(), regionName.size());
|
||||
regionNameEOS[regionName.size()] = '\0';
|
||||
|
||||
m_commandBuffer.BeginDebugRegion(regionNameEOS.data(), color);
|
||||
m_commandBuffer.BeginDebugRegion(regionName, color);
|
||||
}
|
||||
|
||||
void OpenGLCommandBufferBuilder::BeginRenderPass(const Framebuffer& framebuffer, const RenderPass& renderPass, Nz::Recti renderRect, std::initializer_list<ClearValues> clearValues)
|
||||
{
|
||||
const OpenGLRenderPass& vkRenderPass = static_cast<const OpenGLRenderPass&>(renderPass);
|
||||
|
||||
const Vk::Framebuffer& vkFramebuffer = [&] () -> const Vk::Framebuffer&
|
||||
{
|
||||
const OpenGLFramebuffer& vkFramebuffer = static_cast<const OpenGLFramebuffer&>(framebuffer);
|
||||
switch (vkFramebuffer.GetType())
|
||||
{
|
||||
case OpenGLFramebuffer::Type::Multiple:
|
||||
{
|
||||
const OpenGLMultipleFramebuffer& vkMultipleFramebuffer = static_cast<const OpenGLMultipleFramebuffer&>(vkFramebuffer);
|
||||
m_framebufferCount = std::max(m_framebufferCount, vkMultipleFramebuffer.GetFramebufferCount());
|
||||
return vkMultipleFramebuffer.GetFramebuffer(m_imageIndex);
|
||||
}
|
||||
|
||||
case OpenGLFramebuffer::Type::Single:
|
||||
return static_cast<const OpenGLSingleFramebuffer&>(vkFramebuffer).GetFramebuffer();
|
||||
}
|
||||
|
||||
throw std::runtime_error("Unhandled framebuffer type " + std::to_string(UnderlyingCast(vkFramebuffer.GetType())));
|
||||
}();
|
||||
|
||||
VkRect2D renderArea;
|
||||
renderArea.offset.x = renderRect.x;
|
||||
renderArea.offset.y = renderRect.y;
|
||||
renderArea.extent.width = renderRect.width;
|
||||
renderArea.extent.height = renderRect.height;
|
||||
|
||||
StackArray<VkClearValue> vkClearValues = NazaraStackArray(VkClearValue, clearValues.size());
|
||||
|
||||
std::size_t index = 0;
|
||||
for (const ClearValues& values : clearValues)
|
||||
{
|
||||
auto& vkValues = vkClearValues[index];
|
||||
|
||||
if (PixelFormatInfo::GetContent(vkRenderPass.GetAttachmentFormat(index)) == PixelFormatContent_ColorRGBA)
|
||||
{
|
||||
vkValues.color.float32[0] = values.color.r / 255.f;
|
||||
vkValues.color.float32[1] = values.color.g / 255.f;
|
||||
vkValues.color.float32[2] = values.color.b / 255.f;
|
||||
vkValues.color.float32[3] = values.color.a / 255.f;
|
||||
}
|
||||
else
|
||||
{
|
||||
vkValues.depthStencil.depth = values.depth;
|
||||
vkValues.depthStencil.stencil = values.stencil;
|
||||
}
|
||||
|
||||
index++;
|
||||
}
|
||||
|
||||
VkRenderPassBeginInfo beginInfo = { VK_STRUCTURE_TYPE_RENDER_PASS_BEGIN_INFO };
|
||||
beginInfo.renderPass = vkRenderPass.GetRenderPass();
|
||||
beginInfo.framebuffer = vkFramebuffer;
|
||||
beginInfo.renderArea.offset.x = renderRect.x;
|
||||
beginInfo.renderArea.offset.y = renderRect.y;
|
||||
beginInfo.renderArea.extent.width = renderRect.width;
|
||||
beginInfo.renderArea.extent.height = renderRect.height;
|
||||
beginInfo.clearValueCount = vkClearValues.size();
|
||||
beginInfo.pClearValues = vkClearValues.data();
|
||||
|
||||
m_commandBuffer.BeginRenderPass(beginInfo);
|
||||
|
||||
m_currentRenderPass = &vkRenderPass;
|
||||
m_commandBuffer.SetFramebuffer(static_cast<const OpenGLFramebuffer&>(framebuffer), renderPass, clearValues);
|
||||
}
|
||||
|
||||
void OpenGLCommandBufferBuilder::BindIndexBuffer(Nz::AbstractBuffer* indexBuffer, UInt64 offset)
|
||||
{
|
||||
OpenGLBuffer& vkBuffer = *static_cast<OpenGLBuffer*>(indexBuffer);
|
||||
OpenGLBuffer* glBuffer = static_cast<OpenGLBuffer*>(indexBuffer);
|
||||
|
||||
m_commandBuffer.BindIndexBuffer(vkBuffer.GetBuffer(), offset, VK_INDEX_TYPE_UINT16); //< Fuck me right?
|
||||
m_commandBuffer.BindIndexBuffer(glBuffer->GetBuffer().GetObjectId());
|
||||
}
|
||||
|
||||
void OpenGLCommandBufferBuilder::BindPipeline(const RenderPipeline& pipeline)
|
||||
{
|
||||
if (!m_currentRenderPass)
|
||||
throw std::runtime_error("BindPipeline must be called in a RenderPass");
|
||||
const OpenGLRenderPipeline& glPipeline = static_cast<const OpenGLRenderPipeline&>(pipeline);
|
||||
|
||||
const OpenGLRenderPipeline& vkBinding = static_cast<const OpenGLRenderPipeline&>(pipeline);
|
||||
|
||||
m_commandBuffer.BindPipeline(VK_PIPELINE_BIND_POINT_GRAPHICS, vkBinding.Get(m_currentRenderPass->GetRenderPass()));
|
||||
m_commandBuffer.BindPipeline(&glPipeline);
|
||||
}
|
||||
|
||||
void OpenGLCommandBufferBuilder::BindShaderBinding(const ShaderBinding& binding)
|
||||
{
|
||||
const OpenGLShaderBinding& vkBinding = static_cast<const OpenGLShaderBinding&>(binding);
|
||||
const OpenGLShaderBinding& glBinding = static_cast<const OpenGLShaderBinding&>(binding);
|
||||
|
||||
const OpenGLRenderPipelineLayout& pipelineLayout = vkBinding.GetOwner();
|
||||
|
||||
m_commandBuffer.BindDescriptorSet(VK_PIPELINE_BIND_POINT_GRAPHICS, pipelineLayout.GetPipelineLayout(), 0U, vkBinding.GetDescriptorSet());
|
||||
m_commandBuffer.BindShaderBinding(&glBinding);
|
||||
}
|
||||
|
||||
void OpenGLCommandBufferBuilder::BindVertexBuffer(UInt32 binding, Nz::AbstractBuffer* vertexBuffer, UInt64 offset)
|
||||
{
|
||||
OpenGLBuffer& vkBuffer = *static_cast<OpenGLBuffer*>(vertexBuffer);
|
||||
OpenGLBuffer* glBuffer = static_cast<OpenGLBuffer*>(vertexBuffer);
|
||||
|
||||
m_commandBuffer.BindVertexBuffer(binding, vkBuffer.GetBuffer(), offset);
|
||||
m_commandBuffer.BindVertexBuffer(binding, glBuffer->GetBuffer().GetObjectId(), offset);
|
||||
}
|
||||
|
||||
void OpenGLCommandBufferBuilder::CopyBuffer(const RenderBufferView& source, const RenderBufferView& target, UInt64 size, UInt64 sourceOffset, UInt64 targetOffset)
|
||||
@@ -133,15 +57,14 @@ namespace Nz
|
||||
OpenGLBuffer& sourceBuffer = *static_cast<OpenGLBuffer*>(source.GetBuffer());
|
||||
OpenGLBuffer& targetBuffer = *static_cast<OpenGLBuffer*>(target.GetBuffer());
|
||||
|
||||
m_commandBuffer.CopyBuffer(sourceBuffer.GetBuffer(), targetBuffer.GetBuffer(), size, sourceOffset + source.GetOffset(), targetOffset + target.GetOffset());
|
||||
m_commandBuffer.CopyBuffer(sourceBuffer.GetBuffer().GetObjectId(), targetBuffer.GetBuffer().GetObjectId(), size, sourceOffset + source.GetOffset(), targetOffset + target.GetOffset());
|
||||
}
|
||||
|
||||
void OpenGLCommandBufferBuilder::CopyBuffer(const UploadPool::Allocation& allocation, const RenderBufferView& target, UInt64 size, UInt64 sourceOffset, UInt64 targetOffset)
|
||||
{
|
||||
const auto& vkAllocation = static_cast<const OpenGLUploadPool::OpenGLAllocation&>(allocation);
|
||||
OpenGLBuffer& targetBuffer = *static_cast<OpenGLBuffer*>(target.GetBuffer());
|
||||
|
||||
m_commandBuffer.CopyBuffer(vkAllocation.buffer, targetBuffer.GetBuffer(), size, vkAllocation.offset + sourceOffset, target.GetOffset() + targetOffset);
|
||||
m_commandBuffer.CopyBuffer(allocation, targetBuffer.GetBuffer().GetObjectId(), size, sourceOffset, target.GetOffset() + targetOffset);
|
||||
}
|
||||
|
||||
void OpenGLCommandBufferBuilder::Draw(UInt32 vertexCount, UInt32 instanceCount, UInt32 firstVertex, UInt32 firstInstance)
|
||||
@@ -151,7 +74,7 @@ namespace Nz
|
||||
|
||||
void OpenGLCommandBufferBuilder::DrawIndexed(UInt32 indexCount, UInt32 instanceCount, UInt32 firstVertex, UInt32 firstInstance)
|
||||
{
|
||||
m_commandBuffer.DrawIndexed(indexCount, instanceCount, firstVertex, 0, firstInstance);
|
||||
m_commandBuffer.DrawIndexed(indexCount, instanceCount, firstVertex, firstInstance);
|
||||
}
|
||||
|
||||
void OpenGLCommandBufferBuilder::EndDebugRegion()
|
||||
@@ -161,18 +84,14 @@ namespace Nz
|
||||
|
||||
void OpenGLCommandBufferBuilder::EndRenderPass()
|
||||
{
|
||||
m_commandBuffer.EndRenderPass();
|
||||
m_currentRenderPass = nullptr;
|
||||
}
|
||||
|
||||
void OpenGLCommandBufferBuilder::PreTransferBarrier()
|
||||
{
|
||||
m_commandBuffer.MemoryBarrier(VK_PIPELINE_STAGE_BOTTOM_OF_PIPE_BIT, VK_PIPELINE_STAGE_TRANSFER_BIT, 0U, VK_ACCESS_TRANSFER_READ_BIT);
|
||||
}
|
||||
|
||||
void OpenGLCommandBufferBuilder::PostTransferBarrier()
|
||||
{
|
||||
m_commandBuffer.MemoryBarrier(VK_PIPELINE_STAGE_TRANSFER_BIT, VK_PIPELINE_STAGE_FRAGMENT_SHADER_BIT | VK_PIPELINE_STAGE_VERTEX_SHADER_BIT, VK_ACCESS_TRANSFER_WRITE_BIT, VK_ACCESS_UNIFORM_READ_BIT);
|
||||
}
|
||||
|
||||
void OpenGLCommandBufferBuilder::SetScissor(Nz::Recti scissorRegion)
|
||||
@@ -182,8 +101,6 @@ namespace Nz
|
||||
|
||||
void OpenGLCommandBufferBuilder::SetViewport(Nz::Recti viewportRegion)
|
||||
{
|
||||
m_commandBuffer.SetViewport(Nz::Rectf(viewportRegion), 0.f, 1.f);
|
||||
m_commandBuffer.SetViewport(viewportRegion);
|
||||
}
|
||||
}
|
||||
|
||||
#endif
|
||||
|
||||
@@ -2,41 +2,20 @@
|
||||
// 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/OpenGLCommandPool.hpp>
|
||||
#include <Nazara/OpenGLRenderer/OpenGLCommandBuffer.hpp>
|
||||
#include <Nazara/OpenGLRenderer/OpenGLCommandBufferBuilder.hpp>
|
||||
#include <Nazara/OpenGLRenderer/Wrapper/CommandBuffer.hpp>
|
||||
#include <Nazara/OpenGLRenderer/Debug.hpp>
|
||||
|
||||
namespace Nz
|
||||
{
|
||||
std::unique_ptr<CommandBuffer> OpenGLCommandPool::BuildCommandBuffer(const std::function<void(CommandBufferBuilder& builder)>& callback)
|
||||
{
|
||||
std::vector<Vk::AutoCommandBuffer> commandBuffers;
|
||||
auto BuildCommandBuffer = [&](std::size_t imageIndex)
|
||||
{
|
||||
Vk::AutoCommandBuffer& commandBuffer = commandBuffers.emplace_back(m_commandPool.AllocateCommandBuffer(VK_COMMAND_BUFFER_LEVEL_PRIMARY));
|
||||
std::unique_ptr<OpenGLCommandBuffer> commandBuffer = std::make_unique<OpenGLCommandBuffer>();
|
||||
|
||||
if (!commandBuffer->Begin(VK_COMMAND_BUFFER_USAGE_SIMULTANEOUS_USE_BIT))
|
||||
throw std::runtime_error("failed to begin command buffer: " + TranslateOpenGLError(commandBuffer->GetLastErrorCode()));
|
||||
OpenGLCommandBufferBuilder builder(*commandBuffer);
|
||||
callback(builder);
|
||||
|
||||
OpenGLCommandBufferBuilder builder(commandBuffer.Get(), imageIndex);
|
||||
callback(builder);
|
||||
|
||||
if (!commandBuffer->End())
|
||||
throw std::runtime_error("failed to build command buffer: " + TranslateOpenGLError(commandBuffer->GetLastErrorCode()));
|
||||
|
||||
return builder.GetMaxFramebufferCount();
|
||||
};
|
||||
|
||||
std::size_t maxFramebufferCount = BuildCommandBuffer(0);
|
||||
for (std::size_t i = 1; i < maxFramebufferCount; ++i)
|
||||
BuildCommandBuffer(i);
|
||||
|
||||
return std::make_unique<OpenGLCommandBuffer>(std::move(commandBuffers));
|
||||
return commandBuffer;
|
||||
}
|
||||
}
|
||||
|
||||
#endif
|
||||
|
||||
@@ -3,6 +3,8 @@
|
||||
// For conditions of distribution and use, see copyright notice in Config.hpp
|
||||
|
||||
#include <Nazara/OpenGLRenderer/OpenGLRenderImage.hpp>
|
||||
#include <Nazara/OpenGLRenderer/OpenGLCommandBuffer.hpp>
|
||||
#include <Nazara/OpenGLRenderer/OpenGLCommandBufferBuilder.hpp>
|
||||
#include <Nazara/OpenGLRenderer/OpenGLRenderWindow.hpp>
|
||||
#include <stdexcept>
|
||||
#include <Nazara/OpenGLRenderer/Debug.hpp>
|
||||
@@ -17,6 +19,11 @@ namespace Nz
|
||||
|
||||
void OpenGLRenderImage::Execute(const std::function<void(CommandBufferBuilder& builder)>& callback, QueueTypeFlags queueTypeFlags)
|
||||
{
|
||||
OpenGLCommandBuffer commandBuffer;
|
||||
OpenGLCommandBufferBuilder builder(commandBuffer);
|
||||
callback(builder);
|
||||
|
||||
commandBuffer.Execute();
|
||||
}
|
||||
|
||||
OpenGLUploadPool& OpenGLRenderImage::GetUploadPool()
|
||||
@@ -26,6 +33,8 @@ namespace Nz
|
||||
|
||||
void OpenGLRenderImage::SubmitCommandBuffer(CommandBuffer* commandBuffer, QueueTypeFlags queueTypeFlags)
|
||||
{
|
||||
OpenGLCommandBuffer* oglCommandBuffer = static_cast<OpenGLCommandBuffer*>(commandBuffer);
|
||||
oglCommandBuffer->Execute();
|
||||
}
|
||||
|
||||
void OpenGLRenderImage::Present()
|
||||
|
||||
@@ -13,6 +13,29 @@
|
||||
|
||||
namespace Nz
|
||||
{
|
||||
void OpenGLShaderBinding::Apply(const GL::Context& context) const
|
||||
{
|
||||
std::size_t textureDescriptorCount = m_owner.GetTextureDescriptorCount();
|
||||
std::size_t uniformBufferDescriptorCount = m_owner.GetUniformBufferDescriptorCount();
|
||||
|
||||
for (std::size_t i = 0; i < textureDescriptorCount; ++i)
|
||||
{
|
||||
const auto& textureDescriptor = m_owner.GetTextureDescriptor(m_poolIndex, m_bindingIndex, i);
|
||||
|
||||
UInt32 textureIndex = textureDescriptor.bindingIndex;
|
||||
|
||||
context.BindSampler(textureIndex, textureDescriptor.sampler);
|
||||
context.BindTexture(textureIndex, textureDescriptor.textureTarget, textureDescriptor.texture);
|
||||
}
|
||||
|
||||
for (std::size_t i = 0; i < textureDescriptorCount; ++i)
|
||||
{
|
||||
const auto& uboDescriptor = m_owner.GetUniformBufferDescriptor(m_poolIndex, m_bindingIndex, i);
|
||||
|
||||
context.BindUniformBuffer(uboDescriptor.bindingIndex, uboDescriptor.buffer, uboDescriptor.offset, uboDescriptor.size);
|
||||
}
|
||||
}
|
||||
|
||||
void OpenGLShaderBinding::Update(std::initializer_list<Binding> bindings)
|
||||
{
|
||||
const auto& layoutInfo = m_owner.GetLayoutInfo();
|
||||
@@ -43,8 +66,34 @@ namespace Nz
|
||||
OpenGLTextureSampler& glSampler = *static_cast<OpenGLTextureSampler*>(texBinding.sampler);
|
||||
|
||||
auto& textureDescriptor = m_owner.GetTextureDescriptor(m_poolIndex, m_bindingIndex, resourceIndex);
|
||||
textureDescriptor.bindingIndex = binding.bindingIndex;
|
||||
|
||||
textureDescriptor.texture = glTexture.GetTexture().GetObjectId();
|
||||
textureDescriptor.sampler = glSampler.GetSampler(glTexture.GetLevelCount() > 1).GetObjectId();
|
||||
|
||||
switch (glTexture.GetType())
|
||||
{
|
||||
case ImageType_2D:
|
||||
textureDescriptor.textureTarget = GL::TextureTarget::Target2D;
|
||||
break;
|
||||
|
||||
case ImageType_2D_Array:
|
||||
textureDescriptor.textureTarget = GL::TextureTarget::Target2D_Array;
|
||||
break;
|
||||
|
||||
case ImageType_3D:
|
||||
textureDescriptor.textureTarget = GL::TextureTarget::Target3D;
|
||||
break;
|
||||
|
||||
case ImageType_Cubemap:
|
||||
textureDescriptor.textureTarget = GL::TextureTarget::Cubemap;
|
||||
break;
|
||||
|
||||
case ImageType_1D:
|
||||
case ImageType_1D_Array:
|
||||
default:
|
||||
throw std::runtime_error("unsupported texture type");
|
||||
}
|
||||
break;
|
||||
}
|
||||
|
||||
@@ -60,6 +109,7 @@ namespace Nz
|
||||
throw std::runtime_error("expected uniform buffer");
|
||||
|
||||
auto& uboDescriptor = m_owner.GetUniformBufferDescriptor(m_poolIndex, m_bindingIndex, resourceIndex);
|
||||
uboDescriptor.bindingIndex = binding.bindingIndex;
|
||||
uboDescriptor.buffer = glBuffer.GetBuffer().GetObjectId();
|
||||
uboDescriptor.offset = uboBinding.offset;
|
||||
uboDescriptor.size = uboBinding.range;
|
||||
|
||||
@@ -23,9 +23,9 @@ namespace Nz::GL
|
||||
m_device->NotifyContextDestruction(*this);
|
||||
}
|
||||
|
||||
void Context::BindBuffer(BufferTarget target, GLuint buffer) const
|
||||
void Context::BindBuffer(BufferTarget target, GLuint buffer, bool force) const
|
||||
{
|
||||
if (m_state.bufferTargets[UnderlyingCast(target)] != buffer)
|
||||
if (m_state.bufferTargets[UnderlyingCast(target)] != buffer || force)
|
||||
{
|
||||
if (!SetCurrentContext(this))
|
||||
throw std::runtime_error("failed to activate context");
|
||||
@@ -62,6 +62,18 @@ namespace Nz::GL
|
||||
}
|
||||
}
|
||||
|
||||
void Context::BindProgram(GLuint program) const
|
||||
{
|
||||
if (m_state.boundProgram != program)
|
||||
{
|
||||
if (!SetCurrentContext(this))
|
||||
throw std::runtime_error("failed to activate context");
|
||||
|
||||
glUseProgram(program);
|
||||
m_state.boundProgram = program;
|
||||
}
|
||||
}
|
||||
|
||||
void Context::BindSampler(UInt32 textureUnit, GLuint sampler) const
|
||||
{
|
||||
if (textureUnit >= m_state.textureUnits.size())
|
||||
@@ -102,6 +114,37 @@ namespace Nz::GL
|
||||
}
|
||||
}
|
||||
|
||||
void Context::BindUniformBuffer(UInt32 uboUnit, GLuint buffer, GLintptr offset, GLsizeiptr size) const
|
||||
{
|
||||
if (uboUnit >= m_state.uboUnits.size())
|
||||
throw std::runtime_error("unsupported uniform buffer unit #" + std::to_string(uboUnit));
|
||||
|
||||
auto& unit = m_state.uboUnits[uboUnit];
|
||||
if (unit.buffer != buffer || unit.offset != offset || unit.size != size)
|
||||
{
|
||||
if (!SetCurrentContext(this))
|
||||
throw std::runtime_error("failed to activate context");
|
||||
|
||||
glBindBufferRange(GL_UNIFORM_BUFFER, uboUnit, buffer, offset, size);
|
||||
|
||||
unit.buffer = buffer;
|
||||
unit.offset = offset;
|
||||
unit.size = size;
|
||||
}
|
||||
}
|
||||
|
||||
void Context::BindVertexArray(GLuint vertexArray, bool force) const
|
||||
{
|
||||
if (m_state.boundVertexArray != vertexArray || force)
|
||||
{
|
||||
if (!SetCurrentContext(this))
|
||||
throw std::runtime_error("failed to activate context");
|
||||
|
||||
glBindVertexArray(vertexArray);
|
||||
m_state.boundVertexArray = vertexArray;
|
||||
}
|
||||
}
|
||||
|
||||
bool Context::Initialize(const ContextParams& params)
|
||||
{
|
||||
if (!Activate())
|
||||
@@ -220,9 +263,75 @@ namespace Nz::GL
|
||||
assert(maxTextureUnits > 0);
|
||||
m_state.textureUnits.resize(maxTextureUnits);
|
||||
|
||||
GLint maxUniformBufferUnits = -1;
|
||||
glGetIntegerv(GL_MAX_UNIFORM_BUFFER_BINDINGS, &maxUniformBufferUnits);
|
||||
if (maxUniformBufferUnits < 24) //< OpenGL ES 3.0 requires at least 24 uniform buffers units
|
||||
NazaraWarning("GL_MAX_UNIFORM_BUFFER_BINDINGS is " + std::to_string(maxUniformBufferUnits) + ", >= 24 expected");
|
||||
|
||||
assert(maxUniformBufferUnits > 0);
|
||||
m_state.uboUnits.resize(maxUniformBufferUnits);
|
||||
|
||||
std::array<GLint, 4> res;
|
||||
|
||||
glGetIntegerv(GL_SCISSOR_BOX, res.data());
|
||||
m_state.scissorBox = { res[0], res[1], res[2], res[3] };
|
||||
|
||||
glGetIntegerv(GL_VIEWPORT, res.data());
|
||||
m_state.viewport = { res[0], res[1], res[2], res[3] };
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
void Context::SetCurrentTextureUnit(UInt32 textureUnit) const
|
||||
{
|
||||
if (m_state.currentTextureUnit != textureUnit)
|
||||
{
|
||||
if (!SetCurrentContext(this))
|
||||
throw std::runtime_error("failed to activate context");
|
||||
|
||||
glActiveTexture(GL_TEXTURE0 + textureUnit);
|
||||
m_state.currentTextureUnit = textureUnit;
|
||||
}
|
||||
}
|
||||
|
||||
void Context::SetScissorBox(GLint x, GLint y, GLsizei width, GLsizei height) const
|
||||
{
|
||||
if (m_state.scissorBox.x != x ||
|
||||
m_state.scissorBox.y != y ||
|
||||
m_state.scissorBox.width != width ||
|
||||
m_state.scissorBox.height != height)
|
||||
{
|
||||
if (!SetCurrentContext(this))
|
||||
throw std::runtime_error("failed to activate context");
|
||||
|
||||
glScissor(x, y, width, height);
|
||||
|
||||
m_state.scissorBox.x = x;
|
||||
m_state.scissorBox.y = y;
|
||||
m_state.scissorBox.width = width;
|
||||
m_state.scissorBox.height = height;
|
||||
}
|
||||
}
|
||||
|
||||
void Context::SetViewport(GLint x, GLint y, GLsizei width, GLsizei height) const
|
||||
{
|
||||
if (m_state.viewport.x != x ||
|
||||
m_state.viewport.y != y ||
|
||||
m_state.viewport.width != width ||
|
||||
m_state.viewport.height != height)
|
||||
{
|
||||
if (!SetCurrentContext(this))
|
||||
throw std::runtime_error("failed to activate context");
|
||||
|
||||
glViewport(x, y, width, height);
|
||||
|
||||
m_state.viewport.x = x;
|
||||
m_state.viewport.y = y;
|
||||
m_state.viewport.width = width;
|
||||
m_state.viewport.height = height;
|
||||
}
|
||||
}
|
||||
|
||||
void Context::UpdateStates(const RenderStates& renderStates) const
|
||||
{
|
||||
if (!SetCurrentContext(this))
|
||||
@@ -402,6 +511,11 @@ namespace Nz::GL
|
||||
return true;
|
||||
}
|
||||
|
||||
void Context::OnContextRelease()
|
||||
{
|
||||
m_vaoCache.Clear();
|
||||
}
|
||||
|
||||
bool Context::ImplementFallback(const std::string_view& function)
|
||||
{
|
||||
const Loader& loader = GetLoader();
|
||||
|
||||
Reference in New Issue
Block a user