blob: 90a84e258283ba0512aa4da24474cae572c674ae [file] [log] [blame]
// 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 SRC_UI_LIB_ESCHER_DEFAULTS_DEFAULT_SHADER_PROGRAM_FACTORY_H_
#define SRC_UI_LIB_ESCHER_DEFAULTS_DEFAULT_SHADER_PROGRAM_FACTORY_H_
#include "src/lib/fxl/memory/ref_counted.h"
#include "src/ui/lib/escher/util/hash_map.h"
#include "src/ui/lib/escher/util/hasher.h"
#include "src/ui/lib/escher/vk/shader_module_template.h"
#include "src/ui/lib/escher/vk/shader_program.h"
#include "src/ui/lib/escher/vk/shader_program_factory.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();
// 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);
private:
friend class Escher;
// |ShaderProgramFactory|. Public so that Escher::GetProgram() can delegate
// to it.
ShaderProgramPtr GetProgramImpl(const std::string shader_paths[EnumCount<ShaderStage>()],
ShaderVariantArgs args) override;
// 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 // SRC_UI_LIB_ESCHER_DEFAULTS_DEFAULT_SHADER_PROGRAM_FACTORY_H_