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_COMMANDBUFFERSTATETRACKER_H 16 #define DAWNNATIVE_COMMANDBUFFERSTATETRACKER_H 17 18 #include "common/Constants.h" 19 #include "common/ityp_array.h" 20 #include "common/ityp_bitset.h" 21 #include "dawn_native/BindingInfo.h" 22 #include "dawn_native/Error.h" 23 #include "dawn_native/Forward.h" 24 25 namespace dawn_native { 26 27 class CommandBufferStateTracker { 28 public: 29 // Non-state-modifying validation functions 30 MaybeError ValidateCanDispatch(); 31 MaybeError ValidateCanDraw(); 32 MaybeError ValidateCanDrawIndexed(); 33 MaybeError ValidateBufferInRangeForVertexBuffer(uint32_t vertexCount, uint32_t firstVertex); 34 MaybeError ValidateBufferInRangeForInstanceBuffer(uint32_t instanceCount, 35 uint32_t firstInstance); 36 MaybeError ValidateIndexBufferInRange(uint32_t indexCount, uint32_t firstIndex); 37 38 // State-modifying methods 39 void SetComputePipeline(ComputePipelineBase* pipeline); 40 void SetRenderPipeline(RenderPipelineBase* pipeline); 41 void SetBindGroup(BindGroupIndex index, 42 BindGroupBase* bindgroup, 43 uint32_t dynamicOffsetCount, 44 const uint32_t* dynamicOffsets); 45 void SetIndexBuffer(wgpu::IndexFormat format, uint64_t size); 46 void SetVertexBuffer(VertexBufferSlot slot, uint64_t size); 47 48 static constexpr size_t kNumAspects = 4; 49 using ValidationAspects = std::bitset<kNumAspects>; 50 51 BindGroupBase* GetBindGroup(BindGroupIndex index) const; 52 const std::vector<uint32_t>& GetDynamicOffsets(BindGroupIndex index) const; 53 bool HasPipeline() const; 54 RenderPipelineBase* GetRenderPipeline() const; 55 ComputePipelineBase* GetComputePipeline() const; 56 PipelineLayoutBase* GetPipelineLayout() const; 57 wgpu::IndexFormat GetIndexFormat() const; 58 uint64_t GetIndexBufferSize() const; 59 60 private: 61 MaybeError ValidateOperation(ValidationAspects requiredAspects); 62 void RecomputeLazyAspects(ValidationAspects aspects); 63 MaybeError CheckMissingAspects(ValidationAspects aspects); 64 65 void SetPipelineCommon(PipelineBase* pipeline); 66 67 ValidationAspects mAspects; 68 69 ityp::array<BindGroupIndex, BindGroupBase*, kMaxBindGroups> mBindgroups = {}; 70 ityp::array<BindGroupIndex, std::vector<uint32_t>, kMaxBindGroups> mDynamicOffsets = {}; 71 ityp::bitset<VertexBufferSlot, kMaxVertexBuffers> mVertexBufferSlotsUsed; 72 bool mIndexBufferSet = false; 73 wgpu::IndexFormat mIndexFormat; 74 uint64_t mIndexBufferSize = 0; 75 76 ityp::array<VertexBufferSlot, uint64_t, kMaxVertexBuffers> mVertexBufferSizes = {}; 77 78 PipelineLayoutBase* mLastPipelineLayout = nullptr; 79 PipelineBase* mLastPipeline = nullptr; 80 81 const RequiredBufferSizes* mMinBufferSizes = nullptr; 82 }; 83 84 } // namespace dawn_native 85 86 #endif // DAWNNATIVE_COMMANDBUFFERSTATETRACKER_H 87