|
|
@ -10,7 +10,7 @@ |
|
|
|
|
|
|
|
#include "shaders.h"
|
|
|
|
|
|
|
|
static std::string string_from_file(std::string path) { |
|
|
|
static std::string string_from_file(fs::path path) { |
|
|
|
std::ifstream in(path, std::ios::in); |
|
|
|
if (!in) { |
|
|
|
std::cerr << "Opening file " << path << " failed.\n"; |
|
|
@ -33,11 +33,11 @@ ShaderBundle::~ShaderBundle() { |
|
|
|
} |
|
|
|
|
|
|
|
ShaderBundle::ProgramHandle* ShaderBundle::add_program( |
|
|
|
const std::vector<std::pair<std::string, ShaderType>>& shaderPairs) { |
|
|
|
const std::vector<std::pair<fs::path, ShaderType>>& shaderPairs) { |
|
|
|
std::vector<ShaderId> shader_ids; |
|
|
|
for (auto shaderPair : shaderPairs) { |
|
|
|
ShaderId shader_id; |
|
|
|
std::tie(shader_id.name, shader_id.type) = shaderPair; |
|
|
|
std::tie(shader_id.path, shader_id.type) = shaderPair; |
|
|
|
|
|
|
|
auto upserted = this->shader_pool.emplace(std::move(shader_id), Shader()).first; |
|
|
|
// if the shader doesn't have a handle from OpenGL, get one
|
|
|
@ -66,8 +66,8 @@ ShaderBundle::ProgramHandle* ShaderBundle::add_program( |
|
|
|
|
|
|
|
ShaderBundle& ShaderBundle::recompile() { |
|
|
|
for (std::pair<const ShaderId, Shader> shader : this->shader_pool) { |
|
|
|
auto source_s = string_from_file(shader.first.name); |
|
|
|
source_s = ShaderBundle::preprocess(filesystem::path(shader.first.name), source_s); |
|
|
|
auto source_s = string_from_file(shader.first.path); |
|
|
|
source_s = ShaderBundle::preprocess(shader.first.path, source_s); |
|
|
|
const char *source_buf = source_s.c_str(); |
|
|
|
glShaderSource(shader.second.handle, 1, &source_buf, NULL); |
|
|
|
glCompileShader(shader.second.handle); |
|
|
@ -85,7 +85,7 @@ ShaderBundle& ShaderBundle::recompile() { |
|
|
|
|
|
|
|
std::cerr << "Error compiling: " << log_s << '\n'; |
|
|
|
} |
|
|
|
std::cout << "Successfully compiled " << shader.first.name << '\n'; |
|
|
|
std::cout << "Successfully compiled " << shader.first.path << '\n'; |
|
|
|
} |
|
|
|
|
|
|
|
return *this; |
|
|
@ -137,7 +137,7 @@ ShaderBundle& ShaderBundle::link() { |
|
|
|
return *this; |
|
|
|
} |
|
|
|
|
|
|
|
std::string ShaderBundle::preprocess(filesystem::path path, std::string shader) { |
|
|
|
std::string ShaderBundle::preprocess(fs::path path, std::string shader) { |
|
|
|
// TODO: this is probably not idiomatic C++; perhaps use a stringstream?
|
|
|
|
// TODO: proper erroring out.
|
|
|
|
|
|
|
@ -153,10 +153,10 @@ std::string ShaderBundle::preprocess(filesystem::path path, std::string shader) |
|
|
|
auto idx_nl = shader.find('\n', idx_end); |
|
|
|
|
|
|
|
auto file_str = shader.substr(idx_end + 1, idx_nl - idx_end - 2); |
|
|
|
auto file_path = path.parent_path().make_absolute() / file_str; |
|
|
|
auto file_path = fs::absolute(path.parent_path()) / file_str; |
|
|
|
|
|
|
|
auto included_glsl = preprocess(file_path, string_from_file(file_path.str())); |
|
|
|
auto included_glsl = preprocess(file_path, string_from_file(file_path)); |
|
|
|
shader.replace(directive_idx+1, idx_nl - directive_idx - 1, included_glsl); |
|
|
|
|
|
|
|
return preprocess(path, shader); |
|
|
|
} |
|
|
|
} |