2026-03-02 22:44:42 +08:00
|
|
|
|
#include <assets/loaders/shader_loader.h>
|
2026-03-03 05:53:40 +08:00
|
|
|
|
#include <module/module_registry.h>
|
|
|
|
|
|
#include <platform/file_module.h>
|
2026-03-02 22:44:42 +08:00
|
|
|
|
#include <renderer/shader.h>
|
|
|
|
|
|
#include <utils/logger.h>
|
|
|
|
|
|
|
|
|
|
|
|
namespace extra2d {
|
|
|
|
|
|
|
2026-03-03 05:53:40 +08:00
|
|
|
|
namespace {
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* @brief 获取 FileModule 实例
|
|
|
|
|
|
*/
|
|
|
|
|
|
FileModule *getFileModule() {
|
|
|
|
|
|
return getModule<FileModule>();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* @brief 检查文件是否存在(使用 FileModule)
|
|
|
|
|
|
*/
|
|
|
|
|
|
bool fileExists(const std::string &path) {
|
|
|
|
|
|
FileModule *fileModule = getFileModule();
|
|
|
|
|
|
if (fileModule) {
|
|
|
|
|
|
return fileModule->exists(path);
|
|
|
|
|
|
}
|
|
|
|
|
|
return false;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* @brief 读取文件内容为字符串(使用 FileModule,支持 RomFS)
|
|
|
|
|
|
*/
|
|
|
|
|
|
std::string readFileToString(const std::string &path) {
|
|
|
|
|
|
FileModule *fileModule = getFileModule();
|
|
|
|
|
|
if (fileModule) {
|
|
|
|
|
|
return fileModule->readString(path);
|
|
|
|
|
|
}
|
|
|
|
|
|
return "";
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
} // anonymous namespace
|
|
|
|
|
|
|
2026-03-02 22:44:42 +08:00
|
|
|
|
Ptr<Shader> ShaderLoader::load(const std::string &path) {
|
|
|
|
|
|
std::string basePath = path;
|
|
|
|
|
|
|
|
|
|
|
|
std::string::size_type dotPos = basePath.rfind('.');
|
|
|
|
|
|
if (dotPos != std::string::npos) {
|
|
|
|
|
|
basePath = basePath.substr(0, dotPos);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
std::string vertPath = basePath + ".vert";
|
|
|
|
|
|
std::string fragPath = basePath + ".frag";
|
|
|
|
|
|
|
2026-03-03 05:53:40 +08:00
|
|
|
|
if (!fileExists(vertPath)) {
|
2026-03-02 22:44:42 +08:00
|
|
|
|
vertPath = basePath + ".vs";
|
|
|
|
|
|
}
|
2026-03-03 05:53:40 +08:00
|
|
|
|
if (!fileExists(fragPath)) {
|
2026-03-02 22:44:42 +08:00
|
|
|
|
fragPath = basePath + ".fs";
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return load(vertPath, fragPath);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
Ptr<Shader> ShaderLoader::load(const std::string &vertPath,
|
|
|
|
|
|
const std::string &fragPath) {
|
2026-03-03 05:53:40 +08:00
|
|
|
|
// 读取顶点着色器文件
|
|
|
|
|
|
std::string vsSource = readFileToString(vertPath);
|
|
|
|
|
|
if (vsSource.empty()) {
|
|
|
|
|
|
E2D_LOG_ERROR("ShaderLoader: Failed to read vertex shader: {}", vertPath);
|
|
|
|
|
|
return Ptr<Shader>();
|
|
|
|
|
|
}
|
2026-03-02 22:44:42 +08:00
|
|
|
|
|
2026-03-03 05:53:40 +08:00
|
|
|
|
// 读取片段着色器文件
|
|
|
|
|
|
std::string fsSource = readFileToString(fragPath);
|
|
|
|
|
|
if (fsSource.empty()) {
|
|
|
|
|
|
E2D_LOG_ERROR("ShaderLoader: Failed to read fragment shader: {}", fragPath);
|
|
|
|
|
|
return Ptr<Shader>();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// 从源码加载着色器
|
|
|
|
|
|
Ptr<Shader> shader = makePtr<Shader>();
|
|
|
|
|
|
if (!shader->loadFromSource(vsSource, fsSource)) {
|
|
|
|
|
|
E2D_LOG_ERROR("ShaderLoader: Failed to compile shader {} + {}", vertPath,
|
2026-03-02 22:44:42 +08:00
|
|
|
|
fragPath);
|
|
|
|
|
|
return Ptr<Shader>();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
E2D_LOG_DEBUG("ShaderLoader: Loaded shader {} + {}", vertPath, fragPath);
|
|
|
|
|
|
return shader;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
Ptr<Shader> ShaderLoader::loadFromMemory(const uint8_t *data, size_t size) {
|
|
|
|
|
|
E2D_LOG_ERROR("ShaderLoader: loadFromMemory not supported for shaders");
|
|
|
|
|
|
return Ptr<Shader>();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
Ptr<Shader> ShaderLoader::loadFromSource(const std::string &vsSource,
|
|
|
|
|
|
const std::string &fsSource) {
|
|
|
|
|
|
Ptr<Shader> shader = makePtr<Shader>();
|
|
|
|
|
|
|
|
|
|
|
|
if (!shader->loadFromSource(vsSource, fsSource)) {
|
|
|
|
|
|
E2D_LOG_ERROR("ShaderLoader: Failed to compile shader from source");
|
|
|
|
|
|
return Ptr<Shader>();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
E2D_LOG_DEBUG("ShaderLoader: Compiled shader from source");
|
|
|
|
|
|
return shader;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
} // namespace extra2d
|