62 lines
1.7 KiB
C++
62 lines
1.7 KiB
C++
|
|
#include <assets/loaders/shader_loader.h>
|
||
|
|
#include <filesystem>
|
||
|
|
#include <renderer/shader.h>
|
||
|
|
#include <utils/logger.h>
|
||
|
|
|
||
|
|
namespace extra2d {
|
||
|
|
|
||
|
|
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";
|
||
|
|
|
||
|
|
if (!std::filesystem::exists(vertPath)) {
|
||
|
|
vertPath = basePath + ".vs";
|
||
|
|
}
|
||
|
|
if (!std::filesystem::exists(fragPath)) {
|
||
|
|
fragPath = basePath + ".fs";
|
||
|
|
}
|
||
|
|
|
||
|
|
return load(vertPath, fragPath);
|
||
|
|
}
|
||
|
|
|
||
|
|
Ptr<Shader> ShaderLoader::load(const std::string &vertPath,
|
||
|
|
const std::string &fragPath) {
|
||
|
|
Ptr<Shader> shader = makePtr<Shader>();
|
||
|
|
|
||
|
|
if (!shader->loadFromFile(vertPath, fragPath)) {
|
||
|
|
E2D_LOG_ERROR("ShaderLoader: Failed to load shader {} + {}", vertPath,
|
||
|
|
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
|