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 #include "dawn_native/ChainUtils_autogen.h" 16 17 #include <unordered_set> 18 19 namespace dawn_native { 20 21 {% for value in types["s type"].values %} 22 {% if value.valid %} 23 void FindInChain(const ChainedStruct* chain, const {{as_cppEnum(value.name)}}** out) { 24 for (; chain; chain = chain->nextInChain) { 25 if (chain->sType == wgpu::SType::{{as_cppEnum(value.name)}}) { 26 *out = static_cast<const {{as_cppEnum(value.name)}}*>(chain); 27 break; 28 } 29 } 30 } 31 {% endif %} 32 {% endfor %} 33 34 MaybeError ValidateSTypes(const ChainedStruct* chain, 35 std::vector<std::vector<wgpu::SType>> oneOfConstraints) { 36 std::unordered_set<wgpu::SType> allSTypes; 37 for (; chain; chain = chain->nextInChain) { 38 if (allSTypes.find(chain->sType) != allSTypes.end()) { 39 return DAWN_VALIDATION_ERROR("Chain cannot have duplicate sTypes"); 40 } 41 allSTypes.insert(chain->sType); 42 } 43 for (const auto& oneOfConstraint : oneOfConstraints) { 44 bool satisfied = false; 45 for (wgpu::SType oneOfSType : oneOfConstraint) { 46 if (allSTypes.find(oneOfSType) != allSTypes.end()) { 47 if (satisfied) { 48 return DAWN_VALIDATION_ERROR("Unsupported sType combination"); 49 } 50 satisfied = true; 51 allSTypes.erase(oneOfSType); 52 } 53 } 54 } 55 if (!allSTypes.empty()) { 56 return DAWN_VALIDATION_ERROR("Unsupported sType"); 57 } 58 return {}; 59 } 60 61 } // namespace dawn_native 62