• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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_PIPELINE_H_
16 #define DAWNNATIVE_PIPELINE_H_
17 
18 #include "dawn_native/CachedObject.h"
19 #include "dawn_native/Forward.h"
20 #include "dawn_native/ObjectBase.h"
21 #include "dawn_native/PerStage.h"
22 #include "dawn_native/PipelineLayout.h"
23 #include "dawn_native/ShaderModule.h"
24 
25 #include "dawn_native/dawn_platform.h"
26 
27 #include <array>
28 #include <bitset>
29 
30 namespace dawn_native {
31 
32     MaybeError ValidateProgrammableStage(DeviceBase* device,
33                                          const ShaderModuleBase* module,
34                                          const std::string& entryPoint,
35                                          uint32_t constantCount,
36                                          const ConstantEntry* constants,
37                                          const PipelineLayoutBase* layout,
38                                          SingleShaderStage stage);
39 
40     // Use map to make sure constant keys are sorted for creating shader cache keys
41     using PipelineConstantEntries = std::map<std::string, double>;
42 
43     struct ProgrammableStage {
44         Ref<ShaderModuleBase> module;
45         std::string entryPoint;
46 
47         // The metadata lives as long as module, that's ref-ed in the same structure.
48         const EntryPointMetadata* metadata = nullptr;
49 
50         PipelineConstantEntries constants;
51     };
52 
53     class PipelineBase : public ApiObjectBase, public CachedObject {
54       public:
55         ~PipelineBase() override;
56 
57         PipelineLayoutBase* GetLayout();
58         const PipelineLayoutBase* GetLayout() const;
59         const RequiredBufferSizes& GetMinBufferSizes() const;
60         const ProgrammableStage& GetStage(SingleShaderStage stage) const;
61         const PerStage<ProgrammableStage>& GetAllStages() const;
62         wgpu::ShaderStage GetStageMask() const;
63 
64         ResultOrError<Ref<BindGroupLayoutBase>> GetBindGroupLayout(uint32_t groupIndex);
65 
66         // Helper functions for std::unordered_map-based pipeline caches.
67         size_t ComputeContentHash() override;
68         static bool EqualForCache(const PipelineBase* a, const PipelineBase* b);
69 
70         // Implementation of the API entrypoint. Do not use in a reentrant manner.
71         BindGroupLayoutBase* APIGetBindGroupLayout(uint32_t groupIndex);
72 
73         // Initialize() should only be called once by the frontend.
74         virtual MaybeError Initialize() = 0;
75 
76       protected:
77         PipelineBase(DeviceBase* device,
78                      PipelineLayoutBase* layout,
79                      const char* label,
80                      std::vector<StageAndDescriptor> stages);
81         PipelineBase(DeviceBase* device, ObjectBase::ErrorTag tag);
82 
83         // Constructor used only for mocking and testing.
84         PipelineBase(DeviceBase* device);
85 
86       private:
87         MaybeError ValidateGetBindGroupLayout(uint32_t group);
88 
89         wgpu::ShaderStage mStageMask = wgpu::ShaderStage::None;
90         PerStage<ProgrammableStage> mStages;
91 
92         Ref<PipelineLayoutBase> mLayout;
93         RequiredBufferSizes mMinBufferSizes;
94     };
95 
96 }  // namespace dawn_native
97 
98 #endif  // DAWNNATIVE_PIPELINE_H_
99