Frostbite2D/Fostbite2D/include/fostbite2D/render/shader/shader_manager.h

130 lines
3.6 KiB
C++
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#pragma once
#include <fostbite2D/core/types.h>
#include <fostbite2D/render/shader/shader_interface.h>
#include <filesystem>
#include <functional>
#include <unordered_map>
namespace frostbite2D {
// ============================================================================
// Shader重载回调
// ============================================================================
using ShaderReloadCallback = std::function<void(Ptr<IShader> newShader)>;
// ============================================================================
// Shader管理器 - 统一入口
// ============================================================================
class ShaderManager {
public:
/**
* @brief 获取单例实例
* @return Shader管理器实例引用
*/
static ShaderManager &getInstance();
// ------------------------------------------------------------------------
// 初始化和关闭
// ------------------------------------------------------------------------
/**
* @brief 初始化Shader系统
* @param factory 渲染后端Shader工厂
* @return 初始化成功返回true失败返回false
*/
bool init(Ptr<IShaderFactory> factory);
/**
* @brief 关闭Shader系统
*/
void shutdown();
/**
* @brief 检查是否已初始化
* @return 已初始化返回true否则返回false
*/
bool isInitialized() const { return initialized_; }
// ------------------------------------------------------------------------
// Shader加载
// ------------------------------------------------------------------------
/**
* @brief 从源码加载Shader
* @param name Shader名称
* @param vertSource 顶点着色器源码
* @param fragSource 片段着色器源码
* @return 加载的Shader实例
*/
Ptr<IShader> loadFromSource(const std::string &name,
const std::string &vertSource,
const std::string &fragSource);
/**
* @brief 从文件加载Shader
* @param name Shader名称
* @param vertPath 顶点着色器文件路径
* @param fragPath 片段着色器文件路径
* @return 加载的Shader实例失败返回nullptr
*/
Ptr<IShader> loadFromFile(const std::string &name,
const std::filesystem::path &vertPath,
const std::filesystem::path &fragPath);
/**
* @brief 获取已加载的Shader
* @param name Shader名称
* @return Shader实例不存在返回nullptr
*/
Ptr<IShader> get(const std::string &name) const;
/**
* @brief 检查Shader是否存在
* @param name Shader名称
* @return 存在返回true否则返回false
*/
bool has(const std::string &name) const;
/**
* @brief 移除Shader
* @param name Shader名称
*/
void remove(const std::string &name);
/**
* @brief 清除所有Shader
*/
void clear();
/**
* @brief 注册重载回调
* @param name Shader名称
* @param callback 重载回调函数
*/
void setReloadCallback(const std::string &name,
ShaderReloadCallback callback);
private:
ShaderManager() = default;
~ShaderManager() = default;
ShaderManager(const ShaderManager &) = delete;
ShaderManager &operator=(const ShaderManager &) = delete;
Ptr<IShaderFactory> factory_;
struct ShaderInfo {
Ptr<IShader> shader;
ShaderReloadCallback reloadCallback;
std::string vertSource;
std::string fragSource;
std::filesystem::path vertPath;
std::filesystem::path fragPath;
};
std::unordered_map<std::string, ShaderInfo> shaders_;
bool initialized_ = false;
};
} // namespace frostbite2D