1 // Copyright 2017 The Dawn Authors 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 #include "dawn_native/PipelineLayout.h" 16 17 #include "common/Assert.h" 18 #include "common/BitSetIterator.h" 19 #include "common/HashUtils.h" 20 #include "dawn_native/BindGroupLayout.h" 21 #include "dawn_native/Device.h" 22 23 namespace dawn_native { 24 ValidatePipelineLayoutDescriptor(DeviceBase * device,const PipelineLayoutDescriptor * descriptor)25 MaybeError ValidatePipelineLayoutDescriptor(DeviceBase* device, 26 const PipelineLayoutDescriptor* descriptor) { 27 if (descriptor->nextInChain != nullptr) { 28 return DAWN_VALIDATION_ERROR("nextInChain must be nullptr"); 29 } 30 31 if (descriptor->bindGroupLayoutCount > kMaxBindGroups) { 32 return DAWN_VALIDATION_ERROR("too many bind group layouts"); 33 } 34 35 for (uint32_t i = 0; i < descriptor->bindGroupLayoutCount; ++i) { 36 DAWN_TRY(device->ValidateObject(descriptor->bindGroupLayouts[i])); 37 } 38 return {}; 39 } 40 41 // PipelineLayoutBase 42 PipelineLayoutBase(DeviceBase * device,const PipelineLayoutDescriptor * descriptor,bool blueprint)43 PipelineLayoutBase::PipelineLayoutBase(DeviceBase* device, 44 const PipelineLayoutDescriptor* descriptor, 45 bool blueprint) 46 : ObjectBase(device), mIsBlueprint(blueprint) { 47 ASSERT(descriptor->bindGroupLayoutCount <= kMaxBindGroups); 48 for (uint32_t group = 0; group < descriptor->bindGroupLayoutCount; ++group) { 49 mBindGroupLayouts[group] = descriptor->bindGroupLayouts[group]; 50 mMask.set(group); 51 } 52 } 53 PipelineLayoutBase(DeviceBase * device,ObjectBase::ErrorTag tag)54 PipelineLayoutBase::PipelineLayoutBase(DeviceBase* device, ObjectBase::ErrorTag tag) 55 : ObjectBase(device, tag) { 56 } 57 ~PipelineLayoutBase()58 PipelineLayoutBase::~PipelineLayoutBase() { 59 // Do not uncache the actual cached object if we are a blueprint 60 if (!mIsBlueprint && !IsError()) { 61 GetDevice()->UncachePipelineLayout(this); 62 } 63 } 64 65 // static MakeError(DeviceBase * device)66 PipelineLayoutBase* PipelineLayoutBase::MakeError(DeviceBase* device) { 67 return new PipelineLayoutBase(device, ObjectBase::kError); 68 } 69 GetBindGroupLayout(size_t group) const70 const BindGroupLayoutBase* PipelineLayoutBase::GetBindGroupLayout(size_t group) const { 71 ASSERT(!IsError()); 72 ASSERT(group < kMaxBindGroups); 73 ASSERT(mMask[group]); 74 return mBindGroupLayouts[group].Get(); 75 } 76 GetBindGroupLayoutsMask() const77 const std::bitset<kMaxBindGroups> PipelineLayoutBase::GetBindGroupLayoutsMask() const { 78 ASSERT(!IsError()); 79 return mMask; 80 } 81 InheritedGroupsMask(const PipelineLayoutBase * other) const82 std::bitset<kMaxBindGroups> PipelineLayoutBase::InheritedGroupsMask( 83 const PipelineLayoutBase* other) const { 84 ASSERT(!IsError()); 85 return {(1 << GroupsInheritUpTo(other)) - 1u}; 86 } 87 GroupsInheritUpTo(const PipelineLayoutBase * other) const88 uint32_t PipelineLayoutBase::GroupsInheritUpTo(const PipelineLayoutBase* other) const { 89 ASSERT(!IsError()); 90 91 for (uint32_t i = 0; i < kMaxBindGroups; ++i) { 92 if (!mMask[i] || mBindGroupLayouts[i].Get() != other->mBindGroupLayouts[i].Get()) { 93 return i; 94 } 95 } 96 return kMaxBindGroups; 97 } 98 operator ()(const PipelineLayoutBase * pl) const99 size_t PipelineLayoutBase::HashFunc::operator()(const PipelineLayoutBase* pl) const { 100 size_t hash = Hash(pl->mMask); 101 102 for (uint32_t group : IterateBitSet(pl->mMask)) { 103 HashCombine(&hash, pl->GetBindGroupLayout(group)); 104 } 105 106 return hash; 107 } 108 operator ()(const PipelineLayoutBase * a,const PipelineLayoutBase * b) const109 bool PipelineLayoutBase::EqualityFunc::operator()(const PipelineLayoutBase* a, 110 const PipelineLayoutBase* b) const { 111 if (a->mMask != b->mMask) { 112 return false; 113 } 114 115 for (uint32_t group : IterateBitSet(a->mMask)) { 116 if (a->GetBindGroupLayout(group) != b->GetBindGroupLayout(group)) { 117 return false; 118 } 119 } 120 121 return true; 122 } 123 124 } // namespace dawn_native 125