Avoid redundant bindings/states based on Metal profiler feedback. (#2006)

This commit is contained in:
Tim Sylvester
2024-01-22 07:02:36 -08:00
committed by GitHub
parent cd3de6d3ad
commit 5c304614d6
9 changed files with 90 additions and 35 deletions

View File

@@ -27,6 +27,19 @@ public:
const MTLRenderCommandEncoderPtr& getMetalEncoder() const { return encoder; }
const gfx::RenderPassDescriptor& getDescriptor() const { return descriptor; }
/// Apply the given depth/stencil state, if different from the current value
/// The state may be null, restoring the default state.
void setDepthStencilState(const MTLDepthStencilStatePtr&);
/// Apply the given stencil reference value, if different from the current value
void setStencilReference(int32_t referenceValue);
/// Bind a texture to the fragment location
void setFragmentTexture(const MTLTexturePtr&, int32_t location);
/// Set the sampler for a texture binding
void setFragmentSamplerState(const MTLSamplerStatePtr&, int32_t location);
void endEncoding();
void addDebugSignpost(const char* name) override;
@@ -43,16 +56,22 @@ private:
gfx::RenderPassDescriptor descriptor;
mtl::CommandEncoder& commandEncoder;
MTLRenderCommandEncoderPtr encoder;
MTLDepthStencilStatePtr currentDepthStencilState;
int32_t currentStencilReferenceValue = 0;
std::vector<gfx::DebugGroup<gfx::RenderPass>> debugGroups;
struct BindInfo {
const BufferResource* buf = nullptr;
NS::UInteger size = 0;
NS::UInteger offset = 0;
std::uint16_t version = 0;
};
static constexpr auto maxBinds = 32;
std::array<std::optional<BindInfo>, maxBinds> vertexBinds;
std::array<std::optional<BindInfo>, maxBinds> fragmentBinds;
std::array<MTLTexturePtr, maxBinds> fragmentTextureBindings;
std::array<MTLSamplerStatePtr, maxBinds> fragmentSamplerStates;
};
} // namespace mtl

View File

@@ -54,12 +54,12 @@ public:
/// @brief Bind this texture to the specified location
/// @param renderPass Render pass on which the texture will be assign
/// @param location Location index of texture sampler in a shader
void bind(const RenderPass& renderPass, int32_t location) noexcept;
void bind(RenderPass& renderPass, int32_t location) noexcept;
/// @brief Unbind the texture, if it was bound
/// @param renderPass Render pass from which the texture will be removed
/// @param location Location index of texture sampler in a shader
void unbind(const RenderPass& renderPass, int32_t location) noexcept;
void unbind(RenderPass& renderPass, int32_t location) noexcept;
private:
MTL::PixelFormat getMetalPixelFormat() const noexcept;