Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Shader: add glslVersion to support GLSL3 shaders #2623

Open
wants to merge 1 commit into
base: develop
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
32 changes: 21 additions & 11 deletions src/openfl/display/Shader.hx
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,17 @@ class Shader
**/
public var precisionHint:ShaderPrecision;

/**
The version to compile the shader to, as specified after the #version
command in GLSL. Also see:

* https://en.wikipedia.org/wiki/OpenGL_Shading_Language
* https://www.khronos.org/opengl/wiki/Core_Language_(GLSL)

Common values might be "100", "300 es" or "400".
**/
public var glslVersion:String;

/**
The compiled Program3D if available.

Expand Down Expand Up @@ -261,6 +272,7 @@ class Shader
{
byteCode = code;
precisionHint = FULL;
glslVersion = null;

__glSourceDirty = true;
__numPasses = 1;
Expand Down Expand Up @@ -473,7 +485,10 @@ class Shader
__paramFloat = new Array();
__paramInt = new Array();

__processGLData(glVertexSource, "attribute");
var glsl3 = glslVersion != null ?
Std.parseInt(glslVersion.split(" ")[0]) >= 300 : false;

__processGLData(glVertexSource, glsl3 ? "in" : "attribute");
__processGLData(glVertexSource, "uniform");
__processGLData(glFragmentSource, "uniform");
}
Expand All @@ -482,6 +497,8 @@ class Shader
{
var gl = __context.gl;

var versionPrefix = glslVersion != null ? '#version ${glslVersion}\n' : "";

#if (js && html5)
var prefix = (precisionHint == FULL ? "precision mediump float;\n" : "precision lowp float;\n");
#else
Expand All @@ -494,8 +511,8 @@ class Shader
+ "#endif\n\n";
#end

var vertex = prefix + glVertexSource;
var fragment = prefix + glFragmentSource;
var vertex = versionPrefix + prefix + glVertexSource;
var fragment = versionPrefix + prefix + glFragmentSource;

var id = vertex + fragment;

Expand Down Expand Up @@ -573,14 +590,7 @@ class Shader
{
var lastMatch = 0, position, regex, name, type;

if (storageType == "uniform")
{
regex = ~/uniform ([A-Za-z0-9]+) ([A-Za-z0-9_]+)/;
}
else
{
regex = ~/attribute ([A-Za-z0-9]+) ([A-Za-z0-9_]+)/;
}
regex = new EReg('${storageType} ([A-Za-z0-9]+) ([A-Za-z0-9_]+)', "");

while (regex.matchSub(source, lastMatch))
{
Expand Down