From b15d853239e34cc3bf30ac5e8c64bb97f7a17964 Mon Sep 17 00:00:00 2001 From: Austin Eng Date: Mon, 1 May 2023 19:57:35 +0000 Subject: [PATCH] Split MakeVertexDesc so it is const Fixed: dawn:1384 Change-Id: I6071097e8d78b8462459623443b66f3afa42d9d8 Reviewed-on: https://dawn-review.googlesource.com/c/dawn/+/130480 Kokoro: Kokoro Reviewed-by: Brandon Jones Reviewed-by: Corentin Wallez Commit-Queue: Austin Eng --- src/dawn/native/metal/PipelineLayoutMTL.h | 2 +- src/dawn/native/metal/PipelineLayoutMTL.mm | 2 +- src/dawn/native/metal/RenderPipelineMTL.h | 2 +- src/dawn/native/metal/RenderPipelineMTL.mm | 33 ++++++++++++---------- 4 files changed, 21 insertions(+), 18 deletions(-) diff --git a/src/dawn/native/metal/PipelineLayoutMTL.h b/src/dawn/native/metal/PipelineLayoutMTL.h index 48711e7693..9e61faad86 100644 --- a/src/dawn/native/metal/PipelineLayoutMTL.h +++ b/src/dawn/native/metal/PipelineLayoutMTL.h @@ -47,7 +47,7 @@ class PipelineLayout final : public PipelineLayoutBase { const BindingIndexInfo& GetBindingIndexInfo(SingleShaderStage stage) const; // The number of Metal vertex stage buffers used for the whole pipeline layout. - uint32_t GetBufferBindingCount(SingleShaderStage stage); + uint32_t GetBufferBindingCount(SingleShaderStage stage) const; private: PipelineLayout(Device* device, const PipelineLayoutDescriptor* descriptor); diff --git a/src/dawn/native/metal/PipelineLayoutMTL.mm b/src/dawn/native/metal/PipelineLayoutMTL.mm index 800db51a7c..a14f82ed5d 100644 --- a/src/dawn/native/metal/PipelineLayoutMTL.mm +++ b/src/dawn/native/metal/PipelineLayoutMTL.mm @@ -77,7 +77,7 @@ const PipelineLayout::BindingIndexInfo& PipelineLayout::GetBindingIndexInfo( return mIndexInfo[stage]; } -uint32_t PipelineLayout::GetBufferBindingCount(SingleShaderStage stage) { +uint32_t PipelineLayout::GetBufferBindingCount(SingleShaderStage stage) const { return mBufferBindingCount[stage]; } diff --git a/src/dawn/native/metal/RenderPipelineMTL.h b/src/dawn/native/metal/RenderPipelineMTL.h index 23268aea03..079e341beb 100644 --- a/src/dawn/native/metal/RenderPipelineMTL.h +++ b/src/dawn/native/metal/RenderPipelineMTL.h @@ -55,7 +55,7 @@ class RenderPipeline final : public RenderPipelineBase { private: using RenderPipelineBase::RenderPipelineBase; - NSRef MakeVertexDesc(); + NSRef MakeVertexDesc() const; MTLPrimitiveType mMtlPrimitiveTopology; MTLWinding mMtlFrontFace; diff --git a/src/dawn/native/metal/RenderPipelineMTL.mm b/src/dawn/native/metal/RenderPipelineMTL.mm index 57ef5c5108..62d3735ff3 100644 --- a/src/dawn/native/metal/RenderPipelineMTL.mm +++ b/src/dawn/native/metal/RenderPipelineMTL.mm @@ -323,19 +323,29 @@ MaybeError RenderPipeline::Initialize() { mMtlPrimitiveTopology = MTLPrimitiveTopology(GetPrimitiveTopology()); mMtlFrontFace = MTLFrontFace(GetFrontFace()); mMtlCullMode = ToMTLCullMode(GetCullMode()); + // Build a mapping of vertex buffer slots to packed indices + { + // Vertex buffers are placed after all the buffers for the bind groups. + uint32_t mtlVertexBufferIndex = + ToBackend(GetLayout())->GetBufferBindingCount(SingleShaderStage::Vertex); + + for (VertexBufferSlot slot : IterateBitSet(GetVertexBufferSlotsUsed())) { + mMtlVertexBufferIndices[slot] = mtlVertexBufferIndex; + mtlVertexBufferIndex++; + } + } + auto mtlDevice = ToBackend(GetDevice())->GetMTLDevice(); NSRef descriptorMTLRef = AcquireNSRef([MTLRenderPipelineDescriptor new]); MTLRenderPipelineDescriptor* descriptorMTL = descriptorMTLRef.Get(); - // TODO(dawn:1384): MakeVertexDesc should be const in the future, so we don't need to call - // it here when vertex pulling is enabled - NSRef vertexDesc = MakeVertexDesc(); - - // Calling MakeVertexDesc first is important since it sets indices for packed bindings + NSRef vertexDesc; if (GetDevice()->IsToggleEnabled(Toggle::MetalEnableVertexPulling)) { vertexDesc = AcquireNSRef([MTLVertexDescriptor new]); + } else { + vertexDesc = MakeVertexDesc(); } descriptorMTL.vertexDescriptor = vertexDesc.Get(); @@ -449,13 +459,9 @@ wgpu::ShaderStage RenderPipeline::GetStagesRequiringStorageBufferLength() const return mStagesRequiringStorageBufferLength; } -NSRef RenderPipeline::MakeVertexDesc() { +NSRef RenderPipeline::MakeVertexDesc() const { MTLVertexDescriptor* mtlVertexDescriptor = [MTLVertexDescriptor new]; - // Vertex buffers are packed after all the buffers for the bind groups. - uint32_t mtlVertexBufferIndex = - ToBackend(GetLayout())->GetBufferBindingCount(SingleShaderStage::Vertex); - for (VertexBufferSlot slot : IterateBitSet(GetVertexBufferSlotsUsed())) { const VertexBufferInfo& info = GetVertexBuffer(slot); @@ -486,11 +492,8 @@ NSRef RenderPipeline::MakeVertexDesc() { layoutDesc.stride = info.arrayStride; } - mtlVertexDescriptor.layouts[mtlVertexBufferIndex] = layoutDesc; + mtlVertexDescriptor.layouts[GetMtlVertexBufferIndex(slot)] = layoutDesc; [layoutDesc release]; - - mMtlVertexBufferIndices[slot] = mtlVertexBufferIndex; - mtlVertexBufferIndex++; } for (VertexAttributeLocation loc : IterateBitSet(GetAttributeLocationsUsed())) { @@ -499,7 +502,7 @@ NSRef RenderPipeline::MakeVertexDesc() { auto attribDesc = [MTLVertexAttributeDescriptor new]; attribDesc.format = VertexFormatType(info.format); attribDesc.offset = info.offset; - attribDesc.bufferIndex = mMtlVertexBufferIndices[info.vertexBufferSlot]; + attribDesc.bufferIndex = GetMtlVertexBufferIndex(info.vertexBufferSlot); mtlVertexDescriptor.attributes[static_cast(loc)] = attribDesc; [attribDesc release]; }