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 #ifndef DAWNNATIVE_BINDGROUP_H_ 16 #define DAWNNATIVE_BINDGROUP_H_ 17 18 #include "common/Constants.h" 19 #include "common/Math.h" 20 #include "dawn_native/BindGroupLayout.h" 21 #include "dawn_native/Error.h" 22 #include "dawn_native/Forward.h" 23 #include "dawn_native/ObjectBase.h" 24 25 #include "dawn_native/dawn_platform.h" 26 27 #include <array> 28 29 namespace dawn_native { 30 31 class DeviceBase; 32 33 MaybeError ValidateBindGroupDescriptor(DeviceBase* device, 34 const BindGroupDescriptor* descriptor); 35 36 struct BufferBinding { 37 BufferBase* buffer; 38 uint64_t offset; 39 uint64_t size; 40 }; 41 42 class BindGroupBase : public ApiObjectBase { 43 public: 44 static BindGroupBase* MakeError(DeviceBase* device); 45 46 ObjectType GetType() const override; 47 48 BindGroupLayoutBase* GetLayout(); 49 const BindGroupLayoutBase* GetLayout() const; 50 BufferBinding GetBindingAsBufferBinding(BindingIndex bindingIndex); 51 SamplerBase* GetBindingAsSampler(BindingIndex bindingIndex) const; 52 TextureViewBase* GetBindingAsTextureView(BindingIndex bindingIndex); 53 const ityp::span<uint32_t, uint64_t>& GetUnverifiedBufferSizes() const; 54 ExternalTextureBase* GetBindingAsExternalTexture(BindingIndex bindingIndex); 55 56 protected: 57 // To save memory, the size of a bind group is dynamically determined and the bind group is 58 // placement-allocated into memory big enough to hold the bind group with its 59 // dynamically-sized bindings after it. The pointer of the memory of the beginning of the 60 // binding data should be passed as |bindingDataStart|. 61 BindGroupBase(DeviceBase* device, 62 const BindGroupDescriptor* descriptor, 63 void* bindingDataStart); 64 65 // Helper to instantiate BindGroupBase. We pass in |derived| because BindGroupBase may not 66 // be first in the allocation. The binding data is stored after the Derived class. 67 template <typename Derived> BindGroupBase(Derived * derived,DeviceBase * device,const BindGroupDescriptor * descriptor)68 BindGroupBase(Derived* derived, DeviceBase* device, const BindGroupDescriptor* descriptor) 69 : BindGroupBase(device, 70 descriptor, 71 AlignPtr(reinterpret_cast<char*>(derived) + sizeof(Derived), 72 descriptor->layout->GetBindingDataAlignment())) { 73 static_assert(std::is_base_of<BindGroupBase, Derived>::value, ""); 74 } 75 76 // Constructor used only for mocking and testing. 77 BindGroupBase(DeviceBase* device); 78 void DestroyImpl() override; 79 80 ~BindGroupBase() override; 81 82 private: 83 BindGroupBase(DeviceBase* device, ObjectBase::ErrorTag tag); 84 void DeleteThis() override; 85 86 Ref<BindGroupLayoutBase> mLayout; 87 BindGroupLayoutBase::BindingDataPointers mBindingData; 88 }; 89 90 } // namespace dawn_native 91 92 #endif // DAWNNATIVE_BINDGROUP_H_ 93