Skip to content

Commit

Permalink
[Metal] Generalized BeginRenderPass() function in MTCommandContext.
Browse files Browse the repository at this point in the history
This generalizes the functionality of BeginRenderPass() across both primary and secondary command buffers.
  • Loading branch information
LukasBanana committed Jun 6, 2024
1 parent 51363f8 commit 85e2491
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 51 deletions.
10 changes: 9 additions & 1 deletion sources/Renderer/Metal/Command/MTCommandContext.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,13 @@ namespace LLGL
struct Viewport;
struct Scissor;
class Resource;
class RenderTarget;
class MTResourceHeap;
class MTGraphicsPSO;
class MTComputePSO;
class MTPipelineState;
class MTSwapChain;
class MTRenderPass;

struct MTInternalBindingTable
{
Expand Down Expand Up @@ -64,7 +66,12 @@ class MTCommandContext
// Ends the currently bound command encoder.
void Flush();

void BeginRenderPass(MTLRenderPassDescriptor* renderPassDesc, MTSwapChain* swapChainMT);
void BeginRenderPass(
RenderTarget* renderTarget,
const MTRenderPass* renderPassMT,
std::uint32_t numClearValues,
const ClearValue* clearValues
);
void UpdateRenderPass(MTLRenderPassDescriptor* renderPassDesc);
void EndRenderPass();

Expand Down Expand Up @@ -212,6 +219,7 @@ class MTCommandContext

private:

void BeginRenderPassWithDescriptor(MTLRenderPassDescriptor* renderPassDesc, MTSwapChain* swapChainMT);
void BindRenderEncoderWithDescriptor(MTLRenderPassDescriptor* renderPassDesc);
void PauseRenderEncoder();
void ResumeRenderEncoder();
Expand Down
45 changes: 38 additions & 7 deletions sources/Renderer/Metal/Command/MTCommandContext.mm
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,16 @@
#include "../RenderState/MTResourceHeap.h"
#include "../RenderState/MTGraphicsPSO.h"
#include "../RenderState/MTComputePSO.h"
#include "../RenderState/MTRenderPass.h"
#include "../Shader/MTShader.h"
#include "../MTSwapChain.h"
#include "../Texture/MTRenderTarget.h"
#include "../../../Core/Assertion.h"
#include "../../CheckedCast.h"
#include <LLGL/PipelineStateFlags.h>
#include <LLGL/Platform/Platform.h>
#include <LLGL/Utils/ForRange.h>
#include <LLGL/TypeInfo.h>
#include <algorithm>
#include <string.h>

Expand Down Expand Up @@ -72,15 +76,30 @@
}
}

void MTCommandContext::BeginRenderPass(MTLRenderPassDescriptor* renderPassDesc, MTSwapChain* swapChainMT)
void MTCommandContext::BeginRenderPass(
RenderTarget* renderTarget,
const MTRenderPass* renderPassMT,
std::uint32_t numClearValues,
const ClearValue* clearValues)
{
LLGL_ASSERT_PTR(renderPassDesc);

if (!contextState_.isInsideRenderPass)
LLGL_ASSERT_PTR(renderTarget);
if (LLGL::IsInstanceOf<SwapChain>(renderTarget))
{
renderPassDesc_ = (MTLRenderPassDescriptor*)[renderPassDesc copy];
contextState_.isInsideRenderPass = true;
boundSwapChain_ = swapChainMT;
/* Get next render pass descriptor from MetalKit view */
auto* swapChainMT = LLGL_CAST(MTSwapChain*, renderTarget);
if (renderPassMT != nullptr)
BeginRenderPassWithDescriptor(swapChainMT->GetAndUpdateNativeRenderPass(*renderPassMT, numClearValues, clearValues), swapChainMT);
else
BeginRenderPassWithDescriptor(swapChainMT->GetNativeRenderPass(), swapChainMT);
}
else
{
/* Get render pass descriptor from render target */
auto* renderTargetMT = LLGL_CAST(MTRenderTarget*, renderTarget);
if (renderPassMT != nullptr)
BeginRenderPassWithDescriptor(renderTargetMT->GetAndUpdateNativeRenderPass(*renderPassMT, numClearValues, clearValues), nullptr);
else
BeginRenderPassWithDescriptor(renderTargetMT->GetNativeRenderPass(), nullptr);
}
}

Expand Down Expand Up @@ -471,6 +490,18 @@ static NSUInteger GetTessFactorSizeForPatchType(const MTLPatchType patchType)
* ======= Private: =======
*/

void MTCommandContext::BeginRenderPassWithDescriptor(MTLRenderPassDescriptor* renderPassDesc, MTSwapChain* swapChainMT)
{
LLGL_ASSERT_PTR(renderPassDesc);

if (!contextState_.isInsideRenderPass)
{
renderPassDesc_ = (MTLRenderPassDescriptor*)[renderPassDesc copy];
contextState_.isInsideRenderPass = true;
boundSwapChain_ = swapChainMT;
}
}

void MTCommandContext::BindRenderEncoderWithDescriptor(MTLRenderPassDescriptor* renderPassDesc)
{
Flush();
Expand Down
24 changes: 2 additions & 22 deletions sources/Renderer/Metal/Command/MTCommandExecutor.mm
Original file line number Diff line number Diff line change
Expand Up @@ -233,28 +233,8 @@
case MTOpcodeBeginRenderPass:
{
auto* cmd = reinterpret_cast<const MTCmdBeginRenderPass*>(pc);
if (LLGL::IsInstanceOf<SwapChain>(cmd->renderTarget))
{
auto* swapChainMT = LLGL_CAST(MTSwapChain*, cmd->renderTarget);
if (cmd->renderPass != nullptr)
{
auto* clearValues = reinterpret_cast<const ClearValue*>(cmd + 1);
context.BeginRenderPass(swapChainMT->GetAndUpdateNativeRenderPass(*(cmd->renderPass), cmd->numClearValues, clearValues), swapChainMT);
}
else
context.BeginRenderPass(swapChainMT->GetNativeRenderPass(), swapChainMT);
}
else
{
auto* renderTargetMT = LLGL_CAST(MTRenderTarget*, cmd->renderTarget);
if (cmd->renderPass != nullptr)
{
auto* clearValues = reinterpret_cast<const ClearValue*>(cmd + 1);
context.BeginRenderPass(renderTargetMT->GetAndUpdateNativeRenderPass(*(cmd->renderPass), cmd->numClearValues, clearValues), nullptr);
}
else
context.BeginRenderPass(renderTargetMT->GetNativeRenderPass(), nullptr);
}
auto* clearValues = reinterpret_cast<const ClearValue*>(cmd + 1);
context.BeginRenderPass(cmd->renderTarget, cmd->renderPass, cmd->numClearValues, clearValues);
return (sizeof(*cmd) + sizeof(ClearValue)*cmd->numClearValues);
}
case MTOpcodeEndRenderPass:
Expand Down
25 changes: 4 additions & 21 deletions sources/Renderer/Metal/Command/MTDirectCommandBuffer.mm
Original file line number Diff line number Diff line change
Expand Up @@ -489,28 +489,11 @@
/* Put current drawable into queue */
auto& swapChainMT = LLGL_CAST(MTSwapChain&, renderTarget);
QueueDrawable(swapChainMT.GetMTKView().currentDrawable);

/* Get next render pass descriptor from MetalKit view */
if (renderPass != nullptr)
{
auto* renderPassMT = LLGL_CAST(const MTRenderPass*, renderPass);
context_.BeginRenderPass(swapChainMT.GetAndUpdateNativeRenderPass(*renderPassMT, numClearValues, clearValues), &swapChainMT);
}
else
context_.BeginRenderPass(swapChainMT.GetNativeRenderPass(), &swapChainMT);
}
else
{
/* Get render pass descriptor from render target */
auto& renderTargetMT = LLGL_CAST(MTRenderTarget&, renderTarget);
if (renderPass != nullptr)
{
auto* renderPassMT = LLGL_CAST(const MTRenderPass*, renderPass);
context_.BeginRenderPass(renderTargetMT.GetAndUpdateNativeRenderPass(*renderPassMT, numClearValues, clearValues), nullptr);
}
else
context_.BeginRenderPass(renderTargetMT.GetNativeRenderPass(), nullptr);
}

/* Get next render pass descriptor from MetalKit view */
auto* renderPassMT = LLGL_CAST(const MTRenderPass*, renderPass);
context_.BeginRenderPass(&renderTarget, renderPassMT, numClearValues, clearValues);
}

void MTDirectCommandBuffer::EndRenderPass()
Expand Down

0 comments on commit 85e2491

Please sign in to comment.