diff --git a/src/dawn_native/Device.cpp b/src/dawn_native/Device.cpp index b0ef00db13..adc75fce9b 100644 --- a/src/dawn_native/Device.cpp +++ b/src/dawn_native/Device.cpp @@ -112,6 +112,8 @@ namespace dawn_native { // alive. mState = State::Alive; + DAWN_TRY_ASSIGN(mEmptyBindGroupLayout, CreateEmptyBindGroupLayout()); + return {}; } @@ -390,21 +392,22 @@ namespace dawn_native { return mFormatTable[index]; } - ResultOrError DeviceBase::GetOrCreateBindGroupLayout( + ResultOrError> DeviceBase::GetOrCreateBindGroupLayout( const BindGroupLayoutDescriptor* descriptor) { BindGroupLayoutBase blueprint(this, descriptor); + Ref result = nullptr; auto iter = mCaches->bindGroupLayouts.find(&blueprint); if (iter != mCaches->bindGroupLayouts.end()) { - (*iter)->Reference(); - return *iter; + result = *iter; + } else { + BindGroupLayoutBase* backendObj; + DAWN_TRY_ASSIGN(backendObj, CreateBindGroupLayoutImpl(descriptor)); + backendObj->SetIsCachedReference(); + mCaches->bindGroupLayouts.insert(backendObj); + result = AcquireRef(backendObj); } - - BindGroupLayoutBase* backendObj; - DAWN_TRY_ASSIGN(backendObj, CreateBindGroupLayoutImpl(descriptor)); - backendObj->SetIsCachedReference(); - mCaches->bindGroupLayouts.insert(backendObj); - return backendObj; + return std::move(result); } void DeviceBase::UncacheBindGroupLayout(BindGroupLayoutBase* obj) { @@ -413,22 +416,18 @@ namespace dawn_native { ASSERT(removedCount == 1); } - ResultOrError DeviceBase::GetOrCreateEmptyBindGroupLayout() { - if (!mEmptyBindGroupLayout) { - BindGroupLayoutDescriptor desc = {}; - desc.entryCount = 0; - desc.entries = nullptr; + // Private function used at initialization + ResultOrError> DeviceBase::CreateEmptyBindGroupLayout() { + BindGroupLayoutDescriptor desc = {}; + desc.entryCount = 0; + desc.entries = nullptr; - BindGroupLayoutBase* bgl = nullptr; - if (ConsumedError(GetOrCreateBindGroupLayout(&desc), &bgl)) { - return BindGroupLayoutBase::MakeError(this); - } - mEmptyBindGroupLayout = bgl; - return bgl; - } else { - mEmptyBindGroupLayout->Reference(); - return mEmptyBindGroupLayout.Get(); - } + return GetOrCreateBindGroupLayout(&desc); + } + + BindGroupLayoutBase* DeviceBase::GetEmptyBindGroupLayout() { + ASSERT(mEmptyBindGroupLayout); + return mEmptyBindGroupLayout.Get(); } ResultOrError DeviceBase::GetOrCreateComputePipeline( @@ -871,7 +870,9 @@ namespace dawn_native { if (IsValidationEnabled()) { DAWN_TRY(ValidateBindGroupLayoutDescriptor(this, descriptor)); } - DAWN_TRY_ASSIGN(*result, GetOrCreateBindGroupLayout(descriptor)); + Ref bgl; + DAWN_TRY_ASSIGN(bgl, GetOrCreateBindGroupLayout(descriptor)); + *result = bgl.Detach(); return {}; } @@ -1107,4 +1108,4 @@ namespace dawn_native { } } -} // namespace dawn_native +} // namespace dawn_native \ No newline at end of file diff --git a/src/dawn_native/Device.h b/src/dawn_native/Device.h index f9a3b43aba..a513796e14 100644 --- a/src/dawn_native/Device.h +++ b/src/dawn_native/Device.h @@ -108,11 +108,11 @@ namespace dawn_native { // the created object will be, the "blueprint". The blueprint is just a FooBase object // instead of a backend Foo object. If the blueprint doesn't match an object in the // cache, then the descriptor is used to make a new object. - ResultOrError GetOrCreateBindGroupLayout( + ResultOrError> GetOrCreateBindGroupLayout( const BindGroupLayoutDescriptor* descriptor); void UncacheBindGroupLayout(BindGroupLayoutBase* obj); - ResultOrError GetOrCreateEmptyBindGroupLayout(); + BindGroupLayoutBase* GetEmptyBindGroupLayout(); ResultOrError GetOrCreateComputePipeline( const ComputePipelineDescriptor* descriptor); @@ -266,6 +266,8 @@ namespace dawn_native { TextureBase* texture, const TextureViewDescriptor* descriptor) = 0; + ResultOrError> CreateEmptyBindGroupLayout(); + MaybeError CreateBindGroupInternal(BindGroupBase** result, const BindGroupDescriptor* descriptor); MaybeError CreateBindGroupLayoutInternal(BindGroupLayoutBase** result, diff --git a/src/dawn_native/Pipeline.cpp b/src/dawn_native/Pipeline.cpp index 15dd06838d..074f647382 100644 --- a/src/dawn_native/Pipeline.cpp +++ b/src/dawn_native/Pipeline.cpp @@ -92,17 +92,14 @@ namespace dawn_native { BindGroupIndex groupIndex(groupIndexIn); + BindGroupLayoutBase* bgl = nullptr; if (!mLayout->GetBindGroupLayoutsMask()[groupIndex]) { - BindGroupLayoutBase* bgl = nullptr; - if (GetDevice()->ConsumedError(GetDevice()->GetOrCreateEmptyBindGroupLayout(), &bgl)) { - return BindGroupLayoutBase::MakeError(GetDevice()); - } - return bgl; + bgl = GetDevice()->GetEmptyBindGroupLayout(); + } else { + bgl = mLayout->GetBindGroupLayout(groupIndex); } - - BindGroupLayoutBase* bgl = mLayout->GetBindGroupLayout(groupIndex); bgl->Reference(); return bgl; } -} // namespace dawn_native +} // namespace dawn_native \ No newline at end of file diff --git a/src/dawn_native/PipelineLayout.cpp b/src/dawn_native/PipelineLayout.cpp index def4875c44..995a3ee806 100644 --- a/src/dawn_native/PipelineLayout.cpp +++ b/src/dawn_native/PipelineLayout.cpp @@ -222,7 +222,10 @@ namespace dawn_native { // We should never produce a bad descriptor. ASSERT(!ValidateBindGroupLayoutDescriptor(device, &desc).IsError()); - DAWN_TRY_ASSIGN(bindGroupLayouts[group], device->GetOrCreateBindGroupLayout(&desc)); + + Ref bgl; + DAWN_TRY_ASSIGN(bgl, device->GetOrCreateBindGroupLayout(&desc)); + bindGroupLayouts[group] = bgl.Detach(); } PipelineLayoutDescriptor desc = {}; @@ -313,4 +316,4 @@ namespace dawn_native { return true; } -} // namespace dawn_native +} // namespace dawn_native \ No newline at end of file