• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2020 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_BINDINGINFO_H_
16 #define DAWNNATIVE_BINDINGINFO_H_
17 
18 #include "common/Constants.h"
19 #include "common/ityp_array.h"
20 #include "dawn_native/Error.h"
21 #include "dawn_native/Format.h"
22 #include "dawn_native/IntegerTypes.h"
23 #include "dawn_native/PerStage.h"
24 
25 #include "dawn_native/dawn_platform.h"
26 
27 #include <cstdint>
28 
29 namespace dawn_native {
30 
31     // Not a real WebGPU limit, but the sum of the two limits is useful for internal optimizations.
32     static constexpr uint32_t kMaxDynamicBuffersPerPipelineLayout =
33         kMaxDynamicUniformBuffersPerPipelineLayout + kMaxDynamicStorageBuffersPerPipelineLayout;
34 
35     static constexpr BindingIndex kMaxDynamicBuffersPerPipelineLayoutTyped =
36         BindingIndex(kMaxDynamicBuffersPerPipelineLayout);
37 
38     // Not a real WebGPU limit, but used to optimize parts of Dawn which expect valid usage of the
39     // API. There should never be more bindings than the max per stage, for each stage.
40     static constexpr uint32_t kMaxBindingsPerPipelineLayout =
41         3 * (kMaxSampledTexturesPerShaderStage + kMaxSamplersPerShaderStage +
42              kMaxStorageBuffersPerShaderStage + kMaxStorageTexturesPerShaderStage +
43              kMaxUniformBuffersPerShaderStage);
44 
45     static constexpr BindingIndex kMaxBindingsPerPipelineLayoutTyped =
46         BindingIndex(kMaxBindingsPerPipelineLayout);
47 
48     // TODO(enga): Figure out a good number for this.
49     static constexpr uint32_t kMaxOptimalBindingsPerGroup = 32;
50 
51     enum class BindingInfoType { Buffer, Sampler, Texture, StorageTexture, ExternalTexture };
52 
53     absl::FormatConvertResult<absl::FormatConversionCharSet::kString> AbslFormatConvert(
54         BindingInfoType value,
55         const absl::FormatConversionSpec& spec,
56         absl::FormatSink* s);
57 
58     struct BindingInfo {
59         BindingNumber binding;
60         wgpu::ShaderStage visibility;
61 
62         BindingInfoType bindingType;
63 
64         // TODO(dawn:527): These four values could be made into a union.
65         BufferBindingLayout buffer;
66         SamplerBindingLayout sampler;
67         TextureBindingLayout texture;
68         StorageTextureBindingLayout storageTexture;
69     };
70 
71     struct BindingSlot {
72         BindGroupIndex group;
73         BindingNumber binding;
74     };
75 
76     struct PerStageBindingCounts {
77         uint32_t sampledTextureCount;
78         uint32_t samplerCount;
79         uint32_t storageBufferCount;
80         uint32_t storageTextureCount;
81         uint32_t uniformBufferCount;
82         uint32_t externalTextureCount;
83     };
84 
85     struct BindingCounts {
86         uint32_t totalCount;
87         uint32_t bufferCount;
88         uint32_t unverifiedBufferCount;  // Buffers with minimum buffer size unspecified
89         uint32_t dynamicUniformBufferCount;
90         uint32_t dynamicStorageBufferCount;
91         PerStage<PerStageBindingCounts> perStage;
92     };
93 
94     void IncrementBindingCounts(BindingCounts* bindingCounts, const BindGroupLayoutEntry& entry);
95     void AccumulateBindingCounts(BindingCounts* bindingCounts, const BindingCounts& rhs);
96     MaybeError ValidateBindingCounts(const BindingCounts& bindingCounts);
97 
98     // For buffer size validation
99     using RequiredBufferSizes = ityp::array<BindGroupIndex, std::vector<uint64_t>, kMaxBindGroups>;
100 
101 }  // namespace dawn_native
102 
103 #endif  // DAWNNATIVE_BINDINGINFO_H_
104