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_SHADERMODULE_H_ 16 #define DAWNNATIVE_SHADERMODULE_H_ 17 18 #include "common/Constants.h" 19 #include "dawn_native/Error.h" 20 #include "dawn_native/Forward.h" 21 #include "dawn_native/ObjectBase.h" 22 #include "dawn_native/PerStage.h" 23 24 #include "dawn_native/dawn_platform.h" 25 26 #include <array> 27 #include <bitset> 28 #include <vector> 29 30 namespace spirv_cross { 31 class Compiler; 32 } 33 34 namespace dawn_native { 35 36 MaybeError ValidateShaderModuleDescriptor(DeviceBase* device, 37 const ShaderModuleDescriptor* descriptor); 38 39 class ShaderModuleBase : public ObjectBase { 40 public: 41 ShaderModuleBase(DeviceBase* device, 42 const ShaderModuleDescriptor* descriptor, 43 bool blueprint = false); 44 ~ShaderModuleBase() override; 45 46 static ShaderModuleBase* MakeError(DeviceBase* device); 47 48 void ExtractSpirvInfo(const spirv_cross::Compiler& compiler); 49 50 struct BindingInfo { 51 // The SPIRV ID of the resource. 52 uint32_t id; 53 uint32_t base_type_id; 54 dawn::BindingType type; 55 bool used = false; 56 }; 57 using ModuleBindingInfo = 58 std::array<std::array<BindingInfo, kMaxBindingsPerGroup>, kMaxBindGroups>; 59 60 const ModuleBindingInfo& GetBindingInfo() const; 61 const std::bitset<kMaxVertexAttributes>& GetUsedVertexAttributes() const; 62 ShaderStage GetExecutionModel() const; 63 64 bool IsCompatibleWithPipelineLayout(const PipelineLayoutBase* layout); 65 66 // Functors necessary for the unordered_set<ShaderModuleBase*>-based cache. 67 struct HashFunc { 68 size_t operator()(const ShaderModuleBase* module) const; 69 }; 70 struct EqualityFunc { 71 bool operator()(const ShaderModuleBase* a, const ShaderModuleBase* b) const; 72 }; 73 74 private: 75 ShaderModuleBase(DeviceBase* device, ObjectBase::ErrorTag tag); 76 77 bool IsCompatibleWithBindGroupLayout(size_t group, const BindGroupLayoutBase* layout); 78 79 // TODO(cwallez@chromium.org): The code is only stored for deduplication. We could maybe 80 // store a cryptographic hash of the code instead? 81 std::vector<uint32_t> mCode; 82 bool mIsBlueprint = false; 83 84 ModuleBindingInfo mBindingInfo; 85 std::bitset<kMaxVertexAttributes> mUsedVertexAttributes; 86 ShaderStage mExecutionModel; 87 }; 88 89 } // namespace dawn_native 90 91 #endif // DAWNNATIVE_SHADERMODULE_H_ 92