More stencil caching (#1755)

This commit is contained in:
Tim Sylvester
2023-10-16 07:22:54 -07:00
committed by GitHub
parent dc77b6d23f
commit ca8893690d
9 changed files with 96 additions and 67 deletions

View File

@@ -22,6 +22,7 @@ public:
BufferResource(MTLDevicePtr device, const void* raw, std::size_t size, MTL::ResourceOptions usage);
BufferResource(const BufferResource&);
BufferResource(BufferResource&&);
BufferResource& operator=(BufferResource&&);
void update(const void* data, std::size_t size, std::size_t offset);

View File

@@ -107,9 +107,9 @@ public:
void clearStencilBuffer(int32_t) override;
MTLDepthStencilStatePtr makeDepthStencilState(const gfx::DepthMode& depthMode,
const gfx::StencilMode& stencilMode,
const mtl::RenderPass& renderPass) const;
MTLDepthStencilStatePtr makeDepthStencilState(const gfx::DepthMode&,
const gfx::StencilMode&,
const gfx::Renderable&) const;
virtual bool emplaceOrUpdateUniformBuffer(gfx::UniformBufferPtr&, const void* data, std::size_t size);
@@ -130,6 +130,12 @@ private:
std::optional<BufferResource> tileVertexBuffer;
std::optional<BufferResource> tileIndexBuffer;
gfx::ShaderProgramBasePtr clipMaskShader;
MTLDepthStencilStatePtr clipMaskDepthStencilState;
MTLRenderPipelineStatePtr clipMaskPipelineState;
BufferResource clipMaskUniformsBuffer;
const gfx::Renderable* stencilStateRenderable = nullptr;
gfx::RenderingStats stats;
};

View File

@@ -55,7 +55,7 @@ public:
static constexpr std::string_view Name{"GenericMTLShader"};
const std::string_view typeName() const noexcept override { return Name; }
MTLRenderPipelineStatePtr getRenderPipelineState(const gfx::RenderPassDescriptor&,
MTLRenderPipelineStatePtr getRenderPipelineState(const gfx::Renderable&,
const MTLVertexDescriptorPtr&,
const gfx::ColorMode& colorMode) const;

View File

@@ -29,6 +29,13 @@ BufferResource::BufferResource(BufferResource&& other)
buffer(std::move(other.buffer)),
usage(other.usage) {}
BufferResource& BufferResource::operator=(BufferResource&& other) {
device = std::move(other.device);
buffer = std::move(other.buffer);
usage = other.usage;
return *this;
}
void BufferResource::update(const void* data, std::size_t size, std::size_t offset) {
assert(buffer && (data || size == 0));
if (buffer && data && size > 0) {

View File

@@ -223,16 +223,19 @@ bool Context::renderTileClippingMasks(gfx::RenderPass& renderPass,
RenderStaticData& staticData,
const std::vector<shaders::ClipUBO>& tileUBOs) {
using ShaderClass = shaders::ShaderSource<shaders::BuiltIn::ClippingMaskProgram, gfx::Backend::Type::Metal>;
const auto group = staticData.shaders->getShaderGroup("ClippingMaskProgram");
if (!group) {
return false;
if (!clipMaskShader) {
const auto group = staticData.shaders->getShaderGroup("ClippingMaskProgram");
if (group) {
clipMaskShader = std::static_pointer_cast<gfx::ShaderProgramBase>(group->getOrCreateShader(*this, {}));
}
}
const auto shader = std::static_pointer_cast<gfx::ShaderProgramBase>(group->getOrCreateShader(*this, {}));
if (!shader) {
if (!clipMaskShader) {
assert(!"Failed to create shader for clip masking");
return false;
}
const auto& mtlShader = static_cast<const mtl::ShaderProgram&>(*shader);
const auto& mtlShader = static_cast<const mtl::ShaderProgram&>(*clipMaskShader);
const auto& mtlRenderPass = static_cast<mtl::RenderPass&>(renderPass);
const auto& encoder = mtlRenderPass.getMetalEncoder();
const auto colorMode = gfx::ColorMode::disabled();
@@ -251,51 +254,76 @@ bool Context::renderTileClippingMasks(gfx::RenderPass& renderPass,
if (!indexRes) {
return false;
}
assert(indexCount * sizeof(std::uint16_t) == indexRes->getSizeInBytes());
if (auto depthStencilState = makeDepthStencilState(clipMaskDepthMode, clipMaskStencilMode, mtlRenderPass)) {
encoder->setDepthStencilState(depthStencilState.get());
} else {
assert(!"Failed to create depth-stencil state");
return false;
}
// A vertex descriptor tells Metal what's in the vertex buffer
auto vertDesc = NS::RetainPtr(MTL::VertexDescriptor::vertexDescriptor());
auto attribDesc = NS::TransferPtr(MTL::VertexAttributeDescriptor::alloc()->init());
auto layoutDesc = NS::TransferPtr(MTL::VertexBufferLayoutDescriptor::alloc()->init());
if (!vertDesc || !attribDesc || !layoutDesc) {
return false;
}
attribDesc->setBufferIndex(ShaderClass::attributes[0].index);
attribDesc->setOffset(0);
attribDesc->setFormat(MTL::VertexFormatShort2);
layoutDesc->setStride(static_cast<NS::UInteger>(vertexSize));
layoutDesc->setStepFunction(MTL::VertexStepFunctionPerVertex);
layoutDesc->setStepRate(1);
vertDesc->attributes()->setObject(attribDesc.get(), 0);
vertDesc->layouts()->setObject(layoutDesc.get(), 0);
// Create a render pipeline state, telling Metal how to render the primitives
const auto& renderPassDescriptor = mtlRenderPass.getDescriptor();
if (auto state = mtlShader.getRenderPipelineState(renderPassDescriptor, vertDesc, colorMode)) {
encoder->setRenderPipelineState(state.get());
const auto& renderable = renderPassDescriptor.renderable;
if (stencilStateRenderable != &renderable) {
// We're on a new renderable, invalidate objects constructed for the previous one.
clipMaskPipelineState.reset();
clipMaskDepthStencilState.reset();
stencilStateRenderable = &renderable;
}
// Create the depth-stencil state, if necessary.
if (!clipMaskDepthStencilState) {
if (auto depthStencilState = makeDepthStencilState(clipMaskDepthMode, clipMaskStencilMode, renderable)) {
clipMaskDepthStencilState = std::move(depthStencilState);
}
}
if (clipMaskDepthStencilState) {
encoder->setDepthStencilState(clipMaskDepthStencilState.get());
} else {
assert(!"Failed to create depth-stencil state for clip masking");
return false;
}
if (!clipMaskPipelineState) {
// A vertex descriptor tells Metal what's in the vertex buffer
auto vertDesc = NS::RetainPtr(MTL::VertexDescriptor::vertexDescriptor());
auto attribDesc = NS::TransferPtr(MTL::VertexAttributeDescriptor::alloc()->init());
auto layoutDesc = NS::TransferPtr(MTL::VertexBufferLayoutDescriptor::alloc()->init());
if (!vertDesc || !attribDesc || !layoutDesc) {
return false;
}
attribDesc->setBufferIndex(ShaderClass::attributes[0].index);
attribDesc->setOffset(0);
attribDesc->setFormat(MTL::VertexFormatShort2);
layoutDesc->setStride(static_cast<NS::UInteger>(vertexSize));
layoutDesc->setStepFunction(MTL::VertexStepFunctionPerVertex);
layoutDesc->setStepRate(1);
vertDesc->attributes()->setObject(attribDesc.get(), 0);
vertDesc->layouts()->setObject(layoutDesc.get(), 0);
// Create a render pipeline state, telling Metal how to render the primitives
const auto& renderPassDescriptor = mtlRenderPass.getDescriptor();
if (auto state = mtlShader.getRenderPipelineState(renderable, vertDesc, colorMode)) {
clipMaskPipelineState = std::move(state);
}
}
if (clipMaskPipelineState) {
encoder->setRenderPipelineState(clipMaskPipelineState.get());
} else {
assert(!"Failed to create render pipeline state for clip masking");
return false;
}
// Create a buffer for the UBO data.
// This could potentially be reused, but `PaintParameters` is recreated for each frame.
constexpr auto uboSize = sizeof(shaders::ClipUBO);
const auto uboRes = createBuffer(tileUBOs.data(), tileUBOs.size() * uboSize, gfx::BufferUsageType::StaticDraw);
if (!uboRes) {
return false;
const auto bufferSize = tileUBOs.size() * uboSize;
if (!clipMaskUniformsBuffer || clipMaskUniformsBuffer.getSizeInBytes() < bufferSize) {
clipMaskUniformsBuffer = createBuffer(tileUBOs.data(), bufferSize, gfx::BufferUsageType::StaticDraw);
if (!clipMaskUniformsBuffer) {
return false;
}
} else {
clipMaskUniformsBuffer.update(tileUBOs.data(), bufferSize, /*offset=*/0);
}
encoder->setCullMode(MTL::CullModeNone);
encoder->setVertexBuffer(vertexRes.getMetalBuffer().get(), /*offset=*/0, ShaderClass::attributes[0].index);
encoder->setVertexBuffer(uboRes.getMetalBuffer().get(), /*offset=*/0, ShaderClass::uniforms[0].index);
encoder->setVertexBuffer(
clipMaskUniformsBuffer.getMetalBuffer().get(), /*offset=*/0, ShaderClass::uniforms[0].index);
encoder->drawIndexedPrimitives(MTL::PrimitiveType::PrimitiveTypeTriangle,
indexCount,
MTL::IndexType::IndexTypeUInt16,
@@ -453,15 +481,13 @@ void applyStencilMode(const gfx::StencilMode& stencilMode, MTL::StencilDescripto
MTLDepthStencilStatePtr Context::makeDepthStencilState(const gfx::DepthMode& depthMode,
const gfx::StencilMode& stencilMode,
const mtl::RenderPass& renderPass) const {
const gfx::Renderable& renderable) const {
auto depthStencilDescriptor = NS::TransferPtr(MTL::DepthStencilDescriptor::alloc()->init());
if (!depthStencilDescriptor) {
return {};
}
auto& device = backend.getDevice();
const auto& renderPassDescriptor = renderPass.getDescriptor();
const auto& renderable = renderPassDescriptor.renderable;
const auto& renderableResource = renderable.getResource<RenderableResource>();
if (const auto& rpd = renderableResource.getRenderPassDescriptor()) {
// Setting depth/stencil properties when the corresponding target textures aren't set causes, e.g.:

View File

@@ -172,6 +172,7 @@ void Drawable::draw(PaintParameters& parameters) const {
}
const auto& descriptor = renderPass.getDescriptor();
const auto& renderable = descriptor.renderable;
if (impl->renderPassDescriptor && descriptor != *impl->renderPassDescriptor) {
impl->pipelineState.reset();
}
@@ -188,8 +189,6 @@ void Drawable::draw(PaintParameters& parameters) const {
bindUniformBuffers(renderPass);
bindTextures(renderPass);
const auto& renderPassDescriptor = renderPass.getDescriptor();
if (!impl->indexes->getBuffer() || impl->indexes->getDirty()) {
assert(!"Index buffer not uploaded");
return;
@@ -210,7 +209,7 @@ void Drawable::draw(PaintParameters& parameters) const {
encoder->setFrontFacingWinding(mapWindingMode(cullMode.winding));
if (!impl->pipelineState) {
impl->pipelineState = shaderMTL.getRenderPipelineState(renderPassDescriptor, impl->vertexDesc, getColorMode());
impl->pipelineState = shaderMTL.getRenderPipelineState(renderable, impl->vertexDesc, getColorMode());
}
if (impl->pipelineState) {
encoder->setRenderPipelineState(impl->pipelineState.get());
@@ -239,7 +238,7 @@ void Drawable::draw(PaintParameters& parameters) const {
const auto depthMode = parameters.depthModeForSublayer(getSubLayerIndex(), getDepthType());
const auto stencilMode = enableStencil ? parameters.stencilModeForClipping(tileID->toUnwrapped())
: gfx::StencilMode::disabled();
impl->depthStencilState = context.makeDepthStencilState(depthMode, stencilMode, renderPass);
impl->depthStencilState = context.makeDepthStencilState(depthMode, stencilMode, renderable);
impl->previousStencilMode = *newStencilMode;
}
if (impl->depthStencilState) {

View File

@@ -38,13 +38,14 @@ void TileLayerGroup::upload(gfx::UploadPass& uploadPass) {
}
void TileLayerGroup::render(RenderOrchestrator&, PaintParameters& parameters) {
if (!enabled || !getDrawableCount()) {
if (!enabled || !getDrawableCount() || !parameters.renderPass) {
return;
}
auto& context = static_cast<Context&>(parameters.context);
const auto& renderPass = static_cast<const mtl::RenderPass&>(*parameters.renderPass);
const auto& encoder = renderPass.getMetalEncoder();
const auto& renderable = renderPass.getDescriptor().renderable;
// `stencilModeFor3D` uses a different stencil mask value each time its called, so if the
// drawables in this layer use 3D stencil mode, we need to set it up here so that all the
@@ -90,10 +91,10 @@ void TileLayerGroup::render(RenderOrchestrator&, PaintParameters& parameters) {
const auto depthMode = parameters.depthModeFor3D();
if (stencil3d) {
stencilMode3d = parameters.stencilModeFor3D();
stateWithStencil = context.makeDepthStencilState(depthMode, stencilMode3d, renderPass);
stateWithStencil = context.makeDepthStencilState(depthMode, stencilMode3d, renderable);
encoder->setStencilReferenceValue(stencilMode3d.ref);
}
stateWithoutStencil = context.makeDepthStencilState(depthMode, gfx::StencilMode::disabled(), renderPass);
stateWithoutStencil = context.makeDepthStencilState(depthMode, gfx::StencilMode::disabled(), renderable);
} else if (!tileIDs.empty()) {
parameters.renderTileClippingMasks(tileIDs);
}

View File

@@ -154,19 +154,9 @@ void Renderer::Impl::render(const RenderTree& renderTree,
// Tweakers are run in the upload pass so they can set up uniforms.
orchestrator.visitLayerGroups(
[&](LayerGroupBase& layerGroup) { layerGroup.runTweakers(renderTree, parameters); });
}
// Update the debug layer groups
orchestrator.updateDebugLayerGroups(renderTree, parameters);
// Give the layers a chance to do setup
// orchestrator.visitLayerGroups([&](LayerGroup& layerGroup) { layerGroup.preRender(orchestrator, parameters);
// });
// Upload layer groups
{
const auto uploadPass = parameters.encoder->createUploadPass("layerGroup-upload",
parameters.backend.getDefaultRenderable());
// Update the debug layer groups
orchestrator.updateDebugLayerGroups(renderTree, parameters);
// Give the layers a chance to upload
orchestrator.visitLayerGroups([&](LayerGroupBase& layerGroup) { layerGroup.upload(*uploadPass); });

View File

@@ -103,12 +103,11 @@ ShaderProgram::ShaderProgram(std::string name, RendererBackend& backend_, MTLFun
vertexFunction(std::move(vert)),
fragmentFunction(std::move(frag)) {}
MTLRenderPipelineStatePtr ShaderProgram::getRenderPipelineState(const gfx::RenderPassDescriptor& renderPassDescriptor,
MTLRenderPipelineStatePtr ShaderProgram::getRenderPipelineState(const gfx::Renderable& renderable,
const MTLVertexDescriptorPtr& vertexDescriptor,
const gfx::ColorMode& colorMode) const {
auto pool = NS::TransferPtr(NS::AutoreleasePool::alloc()->init());
const auto& renderable = renderPassDescriptor.renderable;
const auto& renderableResource = renderable.getResource<RenderableResource>();
auto colorFormat = MTL::PixelFormat::PixelFormatBGRA8Unorm;