Skip to content
Snippets Groups Projects
Commit 3a83cba5 authored by Philip Trettner's avatar Philip Trettner
Browse files

createFromFiles now traverses the dot hierarchy to find shaders

parent ba396817
No related branches found
No related tags found
No related merge requests found
#pragma once
#include <string>
#include <algorithm>
#include <string>
namespace glow
{
......@@ -68,5 +68,25 @@ inline std::string pathOf(std::string const &filename)
return "";
return filename.substr(0, minPos);
}
/// strips everything after (and including) the last dot of the filename
/// returns "" otherwise
/// e.g. "a.b.c" -> "a.b" but "a.b/c" -> ""
inline std::string stripFileDot(std::string const &filename)
{
auto dotPos = filename.rfind('.');
if (dotPos == std::string::npos)
return "";
auto slashPosA = filename.rfind('/');
if (slashPosA != std::string::npos && slashPosA > dotPos)
return "";
auto slashPosB = filename.rfind('\\');
if (slashPosB != std::string::npos && slashPosB > dotPos)
return "";
return filename.substr(0, dotPos);
}
}
}
......@@ -473,6 +473,7 @@ SharedProgram Program::createFromFile(const std::string &fileOrBaseName)
std::string content;
std::string realFileName;
// try to resolve file directly
if (Shader::resolveFile(fileOrBaseName, type, content, realFileName))
{
if (!realFileName.empty())
......@@ -480,14 +481,27 @@ SharedProgram Program::createFromFile(const std::string &fileOrBaseName)
else
program->attachShader(Shader::createFromSource(type, content));
}
else
else // otherwise check endings
{
for (auto const &kvp : glow::shaderEndingToType)
if (Shader::resolveFile(fileOrBaseName + kvp.first, type, content, realFileName))
{
auto fname = fileOrBaseName;
while (fname != "")
{
if (Shader::resolveFile(fname + kvp.first, type, content, realFileName))
{
if (!realFileName.empty())
program->attachShader(Shader::createFromFile(type, realFileName));
else
program->attachShader(Shader::createFromSource(type, content));
break; // found file
}
// try to find "base" versions
fname = util::stripFileDot(fname);
}
}
}
if (program->mShader.empty())
......
......@@ -406,6 +406,9 @@ public: // static construction
/// E.g. createFromFile("mesh");
/// will use mesh.vsh as vertex shader and mesh.fsh as fragment shader if available
/// See common/shader_endings.cc for a list of available endings
/// Will also traverse "dots" if file not found to check for base versions
/// E.g. createFromFile("mesh.opaque");
/// might find mesh.opaque.fsh and mesh.vsh
static SharedProgram createFromFile(std::string const& fileOrBaseName);
/// Creates a program from a list of explicitly named files
/// Shader type is determined by common/shader_endings.cc
......
......@@ -170,6 +170,7 @@ bool Shader::resolveFile(const std::string &name, GLenum &shaderType, std::strin
if (util::endswith(name, kvp.first))
{
shaderType = kvp.second;
found = true;
break;
}
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment