| // Copyright 2018 The Fuchsia Authors. All rights reserved. |
| // Use of this source code is governed by a BSD-style license that can be |
| // found in the LICENSE file. |
| |
| #ifndef LIB_ESCHER_DEFAULTS_DEFAULT_SHADER_PROGRAM_FACTORY_H_ |
| #define LIB_ESCHER_DEFAULTS_DEFAULT_SHADER_PROGRAM_FACTORY_H_ |
| |
| #include "lib/escher/util/hash_map.h" |
| #include "lib/escher/util/hasher.h" |
| #include "lib/escher/vk/shader_module_template.h" |
| #include "lib/escher/vk/shader_program.h" |
| #include "lib/escher/vk/shader_program_factory.h" |
| #include "src/lib/fxl/memory/ref_counted.h" |
| |
| namespace escher { |
| |
| // Provides the default implementation of ShaderProgramFactory; Escher creates |
| // an instance of this class and implements ShaderProgramFactory by delegating |
| // to it. NOTE: programs generated by DefaultShaderProgramFactory are cached. |
| class DefaultShaderProgramFactory final : public ShaderProgramFactory { |
| public: |
| DefaultShaderProgramFactory(EscherWeakPtr escher, |
| HackFilesystemPtr filesystem); |
| ~DefaultShaderProgramFactory() override; |
| |
| // Exposed so that the client can call InitializeWithRealFiles() upon the |
| // filesystem. This is hacky and will change in the future. |
| const HackFilesystemPtr& filesystem() const { return filesystem_; } |
| |
| // Clear all cached programs and other data. |
| void Clear(); |
| |
| // |ShaderProgramFactory|. Public so that Escher::GetProgram() can delegate |
| // to it. |
| ShaderProgramPtr GetProgram( |
| const std::string shader_paths[EnumCount<ShaderStage>()], |
| ShaderVariantArgs args) override; |
| |
| private: |
| // Obtain a lazily-generated and cached ShaderModuleTemplate corresponding to |
| // the specified shader stage and source code. |
| ShaderModuleTemplatePtr ObtainShaderModuleTemplate( |
| ShaderStage stage, const std::string& source_path); |
| |
| // This is used for paranoid debug-mode verification that we don't have hash |
| // collisions. Otherwise, we'd simply stash the ShaderProgramPtr. |
| struct Record { |
| ShaderProgramPtr ptr; |
| #ifndef NDEBUG |
| std::string paths[6]; |
| ShaderVariantArgs args; |
| #endif |
| }; |
| |
| const EscherWeakPtr escher_; |
| HackFilesystemPtr filesystem_; |
| HashMap<Hash, Record> programs_; |
| std::unordered_map<std::string, ShaderModuleTemplatePtr> templates_; |
| }; |
| |
| } // namespace escher |
| |
| #endif // LIB_ESCHER_DEFAULTS_DEFAULT_SHADER_PROGRAM_FACTORY_H_ |