• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2021 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_CHAIN_UTILS_H_
16 #define DAWNNATIVE_CHAIN_UTILS_H_
17 
18 #include "dawn_native/dawn_platform.h"
19 #include "dawn_native/Error.h"
20 
21 namespace dawn_native {
22     {% for value in types["s type"].values %}
23         {% if value.valid %}
24             void FindInChain(const ChainedStruct* chain, const {{as_cppEnum(value.name)}}** out);
25         {% endif %}
26     {% endfor %}
27 
28     // Verifies that |chain| only contains ChainedStructs of types enumerated in
29     // |oneOfConstraints| and contains no duplicate sTypes. Each vector in
30     // |oneOfConstraints| defines a set of sTypes that cannot coexist in the same chain.
31     // For example:
32     //   ValidateSTypes(chain, { { ShaderModuleSPIRVDescriptor, ShaderModuleWGSLDescriptor } }))
33     //   ValidateSTypes(chain, { { Extension1 }, { Extension2 } })
34     MaybeError ValidateSTypes(const ChainedStruct* chain,
35                               std::vector<std::vector<wgpu::SType>> oneOfConstraints);
36 
37     template <typename T>
38     MaybeError ValidateSingleSTypeInner(const ChainedStruct* chain, T sType) {
39         DAWN_INVALID_IF(chain->sType != sType,
40             "Unsupported sType (%s). Expected (%s)", chain->sType, sType);
41         return {};
42     }
43 
44     template <typename T, typename... Args>
45     MaybeError ValidateSingleSTypeInner(const ChainedStruct* chain, T sType, Args... sTypes) {
46         if (chain->sType == sType) {
47             return {};
48         }
49         return ValidateSingleSTypeInner(chain, sTypes...);
50     }
51 
52     // Verifies that |chain| contains a single ChainedStruct of type |sType| or no ChainedStructs
53     // at all.
54     template <typename T>
55     MaybeError ValidateSingleSType(const ChainedStruct* chain, T sType) {
56         if (chain == nullptr) {
57             return {};
58         }
59         DAWN_INVALID_IF(chain->nextInChain != nullptr,
60             "Chain can only contain a single chained struct.");
61         return ValidateSingleSTypeInner(chain, sType);
62     }
63 
64     // Verifies that |chain| contains a single ChainedStruct with a type enumerated in the
65     // parameter pack or no ChainedStructs at all.
66     template <typename T, typename... Args>
67     MaybeError ValidateSingleSType(const ChainedStruct* chain, T sType, Args... sTypes) {
68         if (chain == nullptr) {
69             return {};
70         }
71         DAWN_INVALID_IF(chain->nextInChain != nullptr,
72             "Chain can only contain a single chained struct.");
73         return ValidateSingleSTypeInner(chain, sType, sTypes...);
74     }
75 
76 }  // namespace dawn_native
77 
78 #endif  // DAWNNATIVE_CHAIN_UTILS_H_
79