Split MakeVertexDesc so it is const

Fixed: dawn:1384
Change-Id: I6071097e8d78b8462459623443b66f3afa42d9d8
Reviewed-on: https://dawn-review.googlesource.com/c/dawn/+/130480
Kokoro: Kokoro <noreply+kokoro@google.com>
Reviewed-by: Brandon Jones <toji@google.com>
Reviewed-by: Corentin Wallez <cwallez@chromium.org>
Commit-Queue: Austin Eng <enga@chromium.org>
This commit is contained in:
Austin Eng 2023-05-01 19:57:35 +00:00 committed by Dawn LUCI CQ
parent 4e9ebeff02
commit b15d853239
4 changed files with 21 additions and 18 deletions

View File

@ -47,7 +47,7 @@ class PipelineLayout final : public PipelineLayoutBase {
const BindingIndexInfo& GetBindingIndexInfo(SingleShaderStage stage) const; const BindingIndexInfo& GetBindingIndexInfo(SingleShaderStage stage) const;
// The number of Metal vertex stage buffers used for the whole pipeline layout. // 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: private:
PipelineLayout(Device* device, const PipelineLayoutDescriptor* descriptor); PipelineLayout(Device* device, const PipelineLayoutDescriptor* descriptor);

View File

@ -77,7 +77,7 @@ const PipelineLayout::BindingIndexInfo& PipelineLayout::GetBindingIndexInfo(
return mIndexInfo[stage]; return mIndexInfo[stage];
} }
uint32_t PipelineLayout::GetBufferBindingCount(SingleShaderStage stage) { uint32_t PipelineLayout::GetBufferBindingCount(SingleShaderStage stage) const {
return mBufferBindingCount[stage]; return mBufferBindingCount[stage];
} }

View File

@ -55,7 +55,7 @@ class RenderPipeline final : public RenderPipelineBase {
private: private:
using RenderPipelineBase::RenderPipelineBase; using RenderPipelineBase::RenderPipelineBase;
NSRef<MTLVertexDescriptor> MakeVertexDesc(); NSRef<MTLVertexDescriptor> MakeVertexDesc() const;
MTLPrimitiveType mMtlPrimitiveTopology; MTLPrimitiveType mMtlPrimitiveTopology;
MTLWinding mMtlFrontFace; MTLWinding mMtlFrontFace;

View File

@ -323,19 +323,29 @@ MaybeError RenderPipeline::Initialize() {
mMtlPrimitiveTopology = MTLPrimitiveTopology(GetPrimitiveTopology()); mMtlPrimitiveTopology = MTLPrimitiveTopology(GetPrimitiveTopology());
mMtlFrontFace = MTLFrontFace(GetFrontFace()); mMtlFrontFace = MTLFrontFace(GetFrontFace());
mMtlCullMode = ToMTLCullMode(GetCullMode()); 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(); auto mtlDevice = ToBackend(GetDevice())->GetMTLDevice();
NSRef<MTLRenderPipelineDescriptor> descriptorMTLRef = NSRef<MTLRenderPipelineDescriptor> descriptorMTLRef =
AcquireNSRef([MTLRenderPipelineDescriptor new]); AcquireNSRef([MTLRenderPipelineDescriptor new]);
MTLRenderPipelineDescriptor* descriptorMTL = descriptorMTLRef.Get(); MTLRenderPipelineDescriptor* descriptorMTL = descriptorMTLRef.Get();
// TODO(dawn:1384): MakeVertexDesc should be const in the future, so we don't need to call NSRef<MTLVertexDescriptor> vertexDesc;
// it here when vertex pulling is enabled
NSRef<MTLVertexDescriptor> vertexDesc = MakeVertexDesc();
// Calling MakeVertexDesc first is important since it sets indices for packed bindings
if (GetDevice()->IsToggleEnabled(Toggle::MetalEnableVertexPulling)) { if (GetDevice()->IsToggleEnabled(Toggle::MetalEnableVertexPulling)) {
vertexDesc = AcquireNSRef([MTLVertexDescriptor new]); vertexDesc = AcquireNSRef([MTLVertexDescriptor new]);
} else {
vertexDesc = MakeVertexDesc();
} }
descriptorMTL.vertexDescriptor = vertexDesc.Get(); descriptorMTL.vertexDescriptor = vertexDesc.Get();
@ -449,13 +459,9 @@ wgpu::ShaderStage RenderPipeline::GetStagesRequiringStorageBufferLength() const
return mStagesRequiringStorageBufferLength; return mStagesRequiringStorageBufferLength;
} }
NSRef<MTLVertexDescriptor> RenderPipeline::MakeVertexDesc() { NSRef<MTLVertexDescriptor> RenderPipeline::MakeVertexDesc() const {
MTLVertexDescriptor* mtlVertexDescriptor = [MTLVertexDescriptor new]; 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())) { for (VertexBufferSlot slot : IterateBitSet(GetVertexBufferSlotsUsed())) {
const VertexBufferInfo& info = GetVertexBuffer(slot); const VertexBufferInfo& info = GetVertexBuffer(slot);
@ -486,11 +492,8 @@ NSRef<MTLVertexDescriptor> RenderPipeline::MakeVertexDesc() {
layoutDesc.stride = info.arrayStride; layoutDesc.stride = info.arrayStride;
} }
mtlVertexDescriptor.layouts[mtlVertexBufferIndex] = layoutDesc; mtlVertexDescriptor.layouts[GetMtlVertexBufferIndex(slot)] = layoutDesc;
[layoutDesc release]; [layoutDesc release];
mMtlVertexBufferIndices[slot] = mtlVertexBufferIndex;
mtlVertexBufferIndex++;
} }
for (VertexAttributeLocation loc : IterateBitSet(GetAttributeLocationsUsed())) { for (VertexAttributeLocation loc : IterateBitSet(GetAttributeLocationsUsed())) {
@ -499,7 +502,7 @@ NSRef<MTLVertexDescriptor> RenderPipeline::MakeVertexDesc() {
auto attribDesc = [MTLVertexAttributeDescriptor new]; auto attribDesc = [MTLVertexAttributeDescriptor new];
attribDesc.format = VertexFormatType(info.format); attribDesc.format = VertexFormatType(info.format);
attribDesc.offset = info.offset; attribDesc.offset = info.offset;
attribDesc.bufferIndex = mMtlVertexBufferIndices[info.vertexBufferSlot]; attribDesc.bufferIndex = GetMtlVertexBufferIndex(info.vertexBufferSlot);
mtlVertexDescriptor.attributes[static_cast<uint8_t>(loc)] = attribDesc; mtlVertexDescriptor.attributes[static_cast<uint8_t>(loc)] = attribDesc;
[attribDesc release]; [attribDesc release];
} }