Skip to content

Commit

Permalink
- JUCE: added support for #version directive in GLSL to allow compati…
Browse files Browse the repository at this point in the history
…bility with older models
  • Loading branch information
christoph-hart committed Nov 12, 2021
1 parent 211e214 commit d503b36
Show file tree
Hide file tree
Showing 3 changed files with 99 additions and 10 deletions.
86 changes: 83 additions & 3 deletions JUCE/modules/juce_opengl/opengl/juce_OpenGLGraphicsContext.cpp
Expand Up @@ -1877,10 +1877,90 @@ struct CustomProgram : public ReferenceCountedObject,
JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (CustomProgram)
};

struct VersionHelpers
{
static bool hasVersionString(const String& fragmentShaderCode)
{
return fragmentShaderCode.contains("#version");
}

static double getVersionNumber(const String& fragmentShaderCode)
{
if (!hasVersionString(fragmentShaderCode))
return OpenGLShaderProgram::getLanguageVersion();

return (double)getVersionLine(fragmentShaderCode).getTrailingIntValue() / 100.0;
}

static String getVersionLine(const String& fragmentShaderCode)
{
if (!hasVersionString(fragmentShaderCode))
return {};

auto vIndex = fragmentShaderCode.indexOf("#version");

auto s = fragmentShaderCode.getCharPointer() + vIndex;
auto end = s;

while (end)
{
if (*end == '\n')
return String(s, end);

end++;
}

jassertfalse;
return {};
}

static String getCodeWithoutVersion(const String& fragmentShaderCode)
{
auto s = StringArray::fromLines(fragmentShaderCode);

for (auto& l : s)
{
if (l.startsWith("#version"))
l = " ";
}

return s.joinIntoString("\n");
}

static String addPrefixAfterVersion(const String& fragmentShaderCode, const String& prefix)
{
String s;

if (hasVersionString(fragmentShaderCode))
{
s << getVersionLine(fragmentShaderCode) << "\n";
s << prefix;
s << getCodeWithoutVersion(fragmentShaderCode);
}
else
{
s << prefix;
s << fragmentShaderCode;
}

return s;
}

static String appendCustomGraphicsHeader(const String& fragmentShaderCode)
{
String prefix;
prefix << JUCE_DECLARE_VARYING_COLOUR;
prefix << JUCE_DECLARE_VARYING_PIXELPOS;
prefix << "\n#define pixelAlpha frontColour.a\n";

return addPrefixAfterVersion(fragmentShaderCode, prefix);
}
};



OpenGLGraphicsContextCustomShader::OpenGLGraphicsContextCustomShader (const String& fragmentShaderCode)
: code (String (JUCE_DECLARE_VARYING_COLOUR
JUCE_DECLARE_VARYING_PIXELPOS
"\n#define pixelAlpha frontColour.a\n") + fragmentShaderCode),
: code (VersionHelpers::appendCustomGraphicsHeader(fragmentShaderCode)),
hashName (String::toHexString (fragmentShaderCode.hashCode64()) + "_shader")
{
}
Expand Down
21 changes: 15 additions & 6 deletions JUCE/modules/juce_opengl/opengl/juce_OpenGLHelpers.cpp
Expand Up @@ -120,12 +120,21 @@ String OpenGLHelpers::translateVertexShaderToV3 (const String& code)
String OpenGLHelpers::translateFragmentShaderToV3 (const String& code)
{
#if JUCE_OPENGL3
if (OpenGLShaderProgram::getLanguageVersion() > 1.2)
return JUCE_GLSL_VERSION "\n"
"out " JUCE_MEDIUMP " vec4 fragColor;\n"
+ code.replace ("varying", "in")
.replace ("texture2D", "texture")
.replace ("gl_FragColor", "fragColor");
if (VersionHelpers::getVersionNumber(code) > 1.2)
{
String prefix;

auto replacedCode = code.replace("varying", "in")
.replace("texture2D", "texture")
.replace("gl_FragColor", "fragColor");

if (!VersionHelpers::hasVersionString(code))
prefix << JUCE_GLSL_VERSION << "\n";

prefix << "out " JUCE_MEDIUMP " vec4 fragColor;\n";

return VersionHelpers::addPrefixAfterVersion(replacedCode, prefix);
}
#endif

return code;
Expand Down
2 changes: 1 addition & 1 deletion hi_scripting/scripting/api/ScriptingGraphics.cpp
Expand Up @@ -501,7 +501,7 @@ String ScriptingObjects::ScriptShader::getHeader()
s << "\n#define fragCoord _gl_fc()\n";
s << "#define fragColor gl_FragColor\n";

#if JUCE_WINDOWS
#if JUCE_WINDOWS && USE_BACKEND
// The #line directive does not work on macOS apparently...
s << "#line 0 \"" << shaderName << "\" \n";
#endif
Expand Down

0 comments on commit d503b36

Please sign in to comment.