Skip to content

Commit

Permalink
libgui|GLShaderBank: Implemented a custom GLSL include directive
Browse files Browse the repository at this point in the history
Alternative to ‘include.vertex’ and ‘include.fragment’ in .dei file.
  • Loading branch information
skyjake committed May 2, 2017
1 parent 9b808c7 commit 6bb6e36
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 2 deletions.
@@ -1,7 +1,5 @@
# Shader for the DGL drawing routines that emulate OpenGL 1.x behavior.
shader dgl.draw {
path.vertex = "dgl_draw.vsh"

include.fragment <../include/fog.glsl>
path.fragment = "dgl_draw.fsh"
}
Expand Up @@ -20,6 +20,8 @@

#version 330

#include "../include/fog.glsl"

uniform int uTexEnabled;
uniform int uTexMode;
uniform vec4 uTexModeColor;
Expand Down
31 changes: 31 additions & 0 deletions doomsday/sdk/libgui/src/graphics/glshaderbank.cpp
Expand Up @@ -29,10 +29,31 @@
#include <de/ScriptedInfo>
#include <de/math.h>

#include <QRegularExpression>
#include <QMap>

namespace de {

static String processIncludes(String source, String const &sourceFolderPath)
{
QRegularExpression const re("#include\\s+\"([^\"]+)\"");
forever
{
auto found = re.match(source);
if (!found.hasMatch()) break; // No more includes.

String incFilePath = sourceFolderPath / found.captured(1);
String incSource = String::fromUtf8(Block(App::rootFolder().locate<File const>(incFilePath)));
incSource = processIncludes(incSource, incFilePath.fileNamePath());

Rangei const capRange(found.capturedStart(), found.capturedEnd());
source = source.substr(0, capRange.start)
+ incSource
+ source.substr(capRange.end);
}
return source;
}

DENG2_PIMPL(GLShaderBank)
{
struct Source : public ISource
Expand Down Expand Up @@ -76,6 +97,12 @@ DENG2_PIMPL(GLShaderBank)
Block(String("#define %1 %2\n").arg(macroName).arg(content).toLatin1()));
source = String::fromLatin1(combo);
}

void insertIncludes(GLShaderBank const &bank, Record const &def)
{
convertToSourceText();
source = processIncludes(source, bank.absolutePathInContext(def, ".").fileNamePath());
}
};

GLShaderBank &bank;
Expand Down Expand Up @@ -276,6 +303,10 @@ Bank::ISource *GLShaderBank::newSourceFromInfo(String const &id)
}
}

// Handle #include directives in the source.
vtx .insertIncludes(*this, def);
frag.insertIncludes(*this, def);

if (def.has("defines"))
{
DictionaryValue const &dict = def.getdt("defines");
Expand Down

0 comments on commit 6bb6e36

Please sign in to comment.