From 8e84b919d6e01ed592bf7fe6af704ea0f3076a00 Mon Sep 17 00:00:00 2001 From: Colin Cornaby Date: Sun, 20 Aug 2023 11:21:37 -0700 Subject: [PATCH] Preventing redundant binds of samplers by tracking them locally --- .../pfMetalPipeline/plMetalMaterialShaderRef.cpp | 9 +++++++-- .../FeatureLib/pfMetalPipeline/plMetalPipeline.cpp | 5 +++++ .../FeatureLib/pfMetalPipeline/plMetalPipeline.h | 10 ++++++++++ 3 files changed, 22 insertions(+), 2 deletions(-) diff --git a/Sources/Plasma/FeatureLib/pfMetalPipeline/plMetalMaterialShaderRef.cpp b/Sources/Plasma/FeatureLib/pfMetalPipeline/plMetalMaterialShaderRef.cpp index 0f16b27fd4..c4c85ad2d4 100644 --- a/Sources/Plasma/FeatureLib/pfMetalPipeline/plMetalMaterialShaderRef.cpp +++ b/Sources/Plasma/FeatureLib/pfMetalPipeline/plMetalMaterialShaderRef.cpp @@ -311,8 +311,13 @@ void plMetalMaterialShaderRef::IBuildLayerTexture(MTL::RenderCommandEncoder *enc } - MTL::SamplerState* samplerState = fPipeline->fDevice.SampleStateForClampFlags(hsGMatState::hsGMatClampFlags(layer->GetClampFlags())); - encoder->setFragmentSamplerState(samplerState, offsetFromRootLayer); + if (fPipeline->fState.layerStates[offsetFromRootLayer].clampFlag != layer->GetClampFlags()) + { + MTL::SamplerState* samplerState = fPipeline->fDevice.SampleStateForClampFlags(hsGMatState::hsGMatClampFlags(layer->GetClampFlags())); + encoder->setFragmentSamplerState(samplerState, offsetFromRootLayer); + + fPipeline->fState.layerStates[offsetFromRootLayer].clampFlag = hsGMatState::hsGMatClampFlags(layer->GetClampFlags()); + } } uint32_t plMetalMaterialShaderRef::ILayersAtOnce(uint32_t which) diff --git a/Sources/Plasma/FeatureLib/pfMetalPipeline/plMetalPipeline.cpp b/Sources/Plasma/FeatureLib/pfMetalPipeline/plMetalPipeline.cpp index 86f34a269f..83cec3f098 100644 --- a/Sources/Plasma/FeatureLib/pfMetalPipeline/plMetalPipeline.cpp +++ b/Sources/Plasma/FeatureLib/pfMetalPipeline/plMetalPipeline.cpp @@ -4619,4 +4619,9 @@ void plMetalPipeline::plMetalPipelineCurrentState::Reset() fCurrentDepthStencilState = nullptr; fCurrentVertexBuffer = nullptr; fCurrentCullMode.reset(); + + for(auto& layer: layerStates) + { + layer.clampFlag = hsGMatState::hsGMatClampFlags(-1); + } } diff --git a/Sources/Plasma/FeatureLib/pfMetalPipeline/plMetalPipeline.h b/Sources/Plasma/FeatureLib/pfMetalPipeline/plMetalPipeline.h index 20101abe69..bdd4e4e123 100644 --- a/Sources/Plasma/FeatureLib/pfMetalPipeline/plMetalPipeline.h +++ b/Sources/Plasma/FeatureLib/pfMetalPipeline/plMetalPipeline.h @@ -269,7 +269,17 @@ class plMetalPipeline : public pl3DPipeline NS::AutoreleasePool* fCurrentPool; + /// Describes the state for the "fixed function" shader. struct plMetalPipelineCurrentState { + + // notes state of a given layer for a draw pass + // index is the offset from the curent root layer + // for the draw pass, not the overall index in the + // material + struct plMetalPipelineLayerState { + hsGMatState::hsGMatClampFlags clampFlag; + } layerStates[8]; + std::optional fCurrentCullMode; const MTL::RenderPipelineState* fCurrentPipelineState; MTL::Buffer* fCurrentVertexBuffer;