#include #include #include namespace extra2d { ShaderLoader::ShaderLoader(AssetFileSystem fileSystem) : fileSystem_(std::move(fileSystem)) {} Ptr 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"; if (!fileSystem_.exists(vertPath)) { vertPath = basePath + ".vs"; } if (!fileSystem_.exists(fragPath)) { fragPath = basePath + ".fs"; } return load(vertPath, fragPath); } Ptr ShaderLoader::load(const std::string &vertPath, const std::string &fragPath) { // 读取顶点着色器文件 std::string vsSource = fileSystem_.readString(vertPath); if (vsSource.empty()) { E2D_ERROR("ShaderLoader: 读取顶点着色器失败: {}", vertPath); return Ptr(); } // 读取片段着色器文件 std::string fsSource = fileSystem_.readString(fragPath); if (fsSource.empty()) { E2D_ERROR("ShaderLoader: 读取片段着色器失败: {}", fragPath); return Ptr(); } // 从源码加载着色器 Ptr shader = makePtr(); if (!shader->loadFromSource(vsSource, fsSource)) { E2D_ERROR("ShaderLoader: 编译着色器失败 {} + {}", vertPath, fragPath); return Ptr(); } E2D_DEBUG("ShaderLoader: 已加载着色器 {} + {}", vertPath, fragPath); return shader; } Ptr ShaderLoader::loadFromMemory(const uint8_t *data, size_t size) { E2D_ERROR("ShaderLoader: loadFromMemory 不支持着色器"); return Ptr(); } Ptr ShaderLoader::loadFromSource(const std::string &vsSource, const std::string &fsSource) { Ptr shader = makePtr(); if (!shader->loadFromSource(vsSource, fsSource)) { E2D_ERROR("ShaderLoader: 从源码编译着色器失败"); return Ptr(); } E2D_DEBUG("ShaderLoader: 已从源码编译着色器"); return shader; } } // namespace extra2d