From c64a69ee6dbb54adc8de299946af856121cce4e3 Mon Sep 17 00:00:00 2001 From: Roza Date: Mon, 24 Feb 2020 22:29:36 -0500 Subject: [PATCH] Use Core GL context, allow OpenGL3/4 on macOS --- shader/bitmapBlit.frag | 33 ++++++++++++------------- shader/blur.frag | 21 ++++++++-------- shader/common.h | 13 +++------- shader/flashMap.frag | 8 +++---- shader/flatColor.frag | 7 ++---- shader/gray.frag | 20 ++++++++-------- shader/hue.frag | 49 +++++++++++++++++++------------------- shader/minimal.vert | 5 +--- shader/plane.frag | 45 +++++++++++++++++----------------- shader/simple.frag | 10 ++++---- shader/simpleAlpha.frag | 14 +++++------ shader/simpleAlphaUni.frag | 12 +++++----- shader/simpleColor.frag | 9 +++---- shader/sprite.frag | 47 ++++++++++++++++++------------------ shader/trans.frag | 23 +++++++++--------- shader/transSimple.frag | 13 +++++----- src/main.mm | 5 ++++ src/shader.cpp | 8 +++---- 18 files changed, 166 insertions(+), 176 deletions(-) diff --git a/shader/bitmapBlit.frag b/shader/bitmapBlit.frag index 850b919f..35ffba8f 100644 --- a/shader/bitmapBlit.frag +++ b/shader/bitmapBlit.frag @@ -8,26 +8,27 @@ uniform vec4 subRect; uniform lowp float opacity; -varying vec2 v_texCoord; +in vec2 v_texCoord; -void main() -{ - vec2 coor = v_texCoord; - vec2 dstCoor = (coor - subRect.xy) * subRect.zw; +out vec4 fragColor; - vec4 srcFrag = texture2D(source, coor); - vec4 dstFrag = texture2D(destination, dstCoor); +void main() { + vec2 coor = v_texCoord; + vec2 dstCoor = (coor - subRect.xy) * subRect.zw; - vec4 resFrag; + vec4 srcFrag = texture(source, coor); + vec4 dstFrag = texture(destination, dstCoor); - float co1 = srcFrag.a * opacity; - float co2 = dstFrag.a * (1.0 - co1); - resFrag.a = co1 + co2; + vec4 resFrag; - if (resFrag.a == 0.0) - resFrag.rgb = srcFrag.rgb; - else - resFrag.rgb = (co1*srcFrag.rgb + co2*dstFrag.rgb) / resFrag.a; + float co1 = srcFrag.a * opacity; + float co2 = dstFrag.a * (1.0 - co1); + resFrag.a = co1 + co2; - gl_FragColor = resFrag; + if (resFrag.a == 0.0) + resFrag.rgb = srcFrag.rgb; + else + resFrag.rgb = (co1 * srcFrag.rgb + co2 * dstFrag.rgb) / resFrag.a; + + fragColor = resFrag; } diff --git a/shader/blur.frag b/shader/blur.frag index 3b8fe48c..0aee5e3d 100644 --- a/shader/blur.frag +++ b/shader/blur.frag @@ -1,16 +1,17 @@ -uniform sampler2D texture; +uniform sampler2D v_texture; -varying vec2 v_texCoord; -varying vec2 v_blurCoord[2]; +in vec2 v_texCoord; +in vec2 v_blurCoord[2]; -void main() -{ - lowp vec4 frag = vec4(0, 0, 0, 0); +out vec4 fragColor; - frag += texture2D(texture, v_texCoord); - frag += texture2D(texture, v_blurCoord[0]); - frag += texture2D(texture, v_blurCoord[1]); +void main() { + lowp vec4 frag = vec4(0, 0, 0, 0); - gl_FragColor = frag / 3.0; + frag += texture(v_texture, v_texCoord); + frag += texture(v_texture, v_blurCoord[0]); + frag += texture(v_texture, v_blurCoord[1]); + + fragColor = frag / 3.0; } diff --git a/shader/common.h b/shader/common.h index a34ef548..64068da9 100644 --- a/shader/common.h +++ b/shader/common.h @@ -1,15 +1,8 @@ -#ifdef GLSLES - -#ifdef FRAGMENT_SHADER -/* Only the fragment shader has no default float precision */ -precision mediump float; -#endif - -#else +#version 330 +#define attribute in +#define varying out /* Desktop GLSL doesn't know about these */ #define highp #define mediump #define lowp - -#endif diff --git a/shader/flashMap.frag b/shader/flashMap.frag index 34ed06b8..e5d463a8 100644 --- a/shader/flashMap.frag +++ b/shader/flashMap.frag @@ -1,9 +1,7 @@ uniform lowp float alpha; -varying lowp vec4 v_color; +in lowp vec4 v_color; -void main() -{ - gl_FragColor = vec4(v_color.rgb * alpha, 1); -} +out vec4 fragColor; +void main() { fragColor = vec4(v_color.rgb * alpha, 1); } diff --git a/shader/flatColor.frag b/shader/flatColor.frag index 985f5dc0..1b51e2cd 100644 --- a/shader/flatColor.frag +++ b/shader/flatColor.frag @@ -1,7 +1,4 @@ uniform lowp vec4 color; - -void main() -{ - gl_FragColor = color; -} +out vec4 fragColor; +void main() { fragColor = color; } diff --git a/shader/gray.frag b/shader/gray.frag index c4cb9827..22278a44 100644 --- a/shader/gray.frag +++ b/shader/gray.frag @@ -1,19 +1,19 @@ -uniform sampler2D texture; +uniform sampler2D v_texture; uniform lowp float gray; -varying vec2 v_texCoord; +in vec2 v_texCoord; const vec3 lumaF = vec3(.299, .587, .114); -void main() -{ - /* Sample source color */ - vec4 frag = texture2D(texture, v_texCoord); +out vec4 fragColor; +void main() { + /* Sample source color */ + vec4 frag = texture(v_texture, v_texCoord); - /* Apply gray */ - float luma = dot(frag.rgb, lumaF); - frag.rgb = mix(frag.rgb, vec3(luma), gray); + /* Apply gray */ + float luma = dot(frag.rgb, lumaF); + frag.rgb = mix(frag.rgb, vec3(luma), gray); - gl_FragColor = frag; + fragColor = frag; } diff --git a/shader/hue.frag b/shader/hue.frag index 61143ac3..707b80b1 100644 --- a/shader/hue.frag +++ b/shader/hue.frag @@ -1,40 +1,39 @@ -uniform sampler2D texture; +uniform sampler2D v_texture; uniform mediump float hueAdjust; -varying vec2 v_texCoord; +in vec2 v_texCoord; + +out vec4 fragColor; /* Source: gamedev.stackexchange.com/a/59808/24839 */ -vec3 rgb2hsv(vec3 c) -{ - const vec4 K = vec4(0.0, -1.0 / 3.0, 2.0 / 3.0, -1.0); - vec4 p = mix(vec4(c.bg, K.wz), vec4(c.gb, K.xy), step(c.b, c.g)); - vec4 q = mix(vec4(p.xyw, c.r), vec4(c.r, p.yzx), step(p.x, c.r)); +vec3 rgb2hsv(vec3 c) { + const vec4 K = vec4(0.0, -1.0 / 3.0, 2.0 / 3.0, -1.0); + vec4 p = mix(vec4(c.bg, K.wz), vec4(c.gb, K.xy), step(c.b, c.g)); + vec4 q = mix(vec4(p.xyw, c.r), vec4(c.r, p.yzx), step(p.x, c.r)); - float d = q.x - min(q.w, q.y); + float d = q.x - min(q.w, q.y); - /* Avoid divide-by-zero situations by adding a very tiny delta. - * Since we always deal with underlying 8-Bit color values, this - * should never mask a real value */ - const float eps = 1.0e-10; + /* Avoid divide-by-zero situations by adding a very tiny delta. + * Since we always deal with underlying 8-Bit color values, this + * should never mask a real value */ + const float eps = 1.0e-10; - return vec3(abs(q.z + (q.w - q.y) / (6.0 * d + eps)), d / (q.x + eps), q.x); + return vec3(abs(q.z + (q.w - q.y) / (6.0 * d + eps)), d / (q.x + eps), q.x); } -vec3 hsv2rgb(vec3 c) -{ - const vec4 K = vec4(1.0, 2.0 / 3.0, 1.0 / 3.0, 3.0); - vec3 p = abs(fract(c.xxx + K.xyz) * 6.0 - K.www); - return c.z * mix(K.xxx, clamp(p - K.xxx, 0.0, 1.0), c.y); +vec3 hsv2rgb(vec3 c) { + const vec4 K = vec4(1.0, 2.0 / 3.0, 1.0 / 3.0, 3.0); + vec3 p = abs(fract(c.xxx + K.xyz) * 6.0 - K.www); + return c.z * mix(K.xxx, clamp(p - K.xxx, 0.0, 1.0), c.y); } -void main () -{ - vec4 color = texture2D (texture, v_texCoord.xy); - vec3 hsv = rgb2hsv(color.rgb); +void main() { + vec4 color = texture(v_texture, v_texCoord.xy); + vec3 hsv = rgb2hsv(color.rgb); - hsv.x += hueAdjust; - color.rgb = hsv2rgb(hsv); + hsv.x += hueAdjust; + color.rgb = hsv2rgb(hsv); - gl_FragColor = color; + fragColor = color; } diff --git a/shader/minimal.vert b/shader/minimal.vert index cccadc1b..33635e47 100644 --- a/shader/minimal.vert +++ b/shader/minimal.vert @@ -2,7 +2,4 @@ uniform mat4 projMat; attribute vec2 position; -void main() -{ - gl_Position = projMat * vec4(position, 0, 1); -} +void main() { gl_Position = projMat * vec4(position, 0, 1); } diff --git a/shader/plane.frag b/shader/plane.frag index 14b52c56..f39e355d 100644 --- a/shader/plane.frag +++ b/shader/plane.frag @@ -1,5 +1,5 @@ -uniform sampler2D texture; +uniform sampler2D v_texture; uniform lowp vec4 tone; @@ -7,30 +7,31 @@ uniform lowp float opacity; uniform lowp vec4 color; uniform lowp vec4 flash; -varying vec2 v_texCoord; +in vec2 v_texCoord; const vec3 lumaF = vec3(.299, .587, .114); -void main() -{ - /* Sample source color */ - vec4 frag = texture2D(texture, v_texCoord); - - /* Apply gray */ - float luma = dot(frag.rgb, lumaF); - frag.rgb = mix(frag.rgb, vec3(luma), tone.w); - - /* Apply tone */ - frag.rgb += tone.rgb; +out vec4 fragColor; - /* Apply opacity */ - frag.a *= opacity; - - /* Apply color */ - frag.rgb = mix(frag.rgb, color.rgb, color.a); +void main() { + /* Sample source color */ + vec4 frag = texture(v_texture, v_texCoord); - /* Apply flash */ - frag.rgb = mix(frag.rgb, flash.rgb, flash.a); - - gl_FragColor = frag; + /* Apply gray */ + float luma = dot(frag.rgb, lumaF); + frag.rgb = mix(frag.rgb, vec3(luma), tone.w); + + /* Apply tone */ + frag.rgb += tone.rgb; + + /* Apply opacity */ + frag.a *= opacity; + + /* Apply color */ + frag.rgb = mix(frag.rgb, color.rgb, color.a); + + /* Apply flash */ + frag.rgb = mix(frag.rgb, flash.rgb, flash.a); + + fragColor = frag; } diff --git a/shader/simple.frag b/shader/simple.frag index e00b11cb..aede6961 100644 --- a/shader/simple.frag +++ b/shader/simple.frag @@ -1,9 +1,7 @@ -uniform sampler2D texture; +uniform sampler2D v_texture; -varying vec2 v_texCoord; +in vec2 v_texCoord; -void main() -{ - gl_FragColor = texture2D(texture, v_texCoord); -} +out vec4 fragColor; +void main() { fragColor = texture(v_texture, v_texCoord); } diff --git a/shader/simpleAlpha.frag b/shader/simpleAlpha.frag index 163bca90..9c181f06 100644 --- a/shader/simpleAlpha.frag +++ b/shader/simpleAlpha.frag @@ -1,11 +1,11 @@ -uniform sampler2D texture; +uniform sampler2D v_texture; -varying vec2 v_texCoord; -varying lowp vec4 v_color; +in vec2 v_texCoord; +in lowp vec4 v_color; -void main() -{ - gl_FragColor = texture2D(texture, v_texCoord); - gl_FragColor.a *= v_color.a; +out vec4 fragColor; +void main() { + fragColor = texture(v_texture, v_texCoord); + fragColor.a *= v_color.a; } diff --git a/shader/simpleAlphaUni.frag b/shader/simpleAlphaUni.frag index cc0cac3c..cef53800 100644 --- a/shader/simpleAlphaUni.frag +++ b/shader/simpleAlphaUni.frag @@ -1,11 +1,11 @@ -uniform sampler2D texture; +uniform sampler2D v_texture; uniform lowp float alpha; -varying vec2 v_texCoord; +in vec2 v_texCoord; +out vec4 fragColor; -void main() -{ - gl_FragColor = texture2D(texture, v_texCoord); - gl_FragColor.a *= alpha; +void main() { + fragColor = texture(v_texture, v_texCoord); + fragColor.a *= alpha; } diff --git a/shader/simpleColor.frag b/shader/simpleColor.frag index da3a77fc..c4a1fa76 100644 --- a/shader/simpleColor.frag +++ b/shader/simpleColor.frag @@ -1,7 +1,4 @@ -varying lowp vec4 v_color; - -void main() -{ - gl_FragColor = v_color; -} +in lowp vec4 v_color; +out vec4 fragColor; +void main() { fragColor = v_color; } diff --git a/shader/sprite.frag b/shader/sprite.frag index 93c35f38..383d2912 100644 --- a/shader/sprite.frag +++ b/shader/sprite.frag @@ -1,5 +1,5 @@ -uniform sampler2D texture; +uniform sampler2D v_texture; uniform lowp vec4 tone; @@ -9,31 +9,32 @@ uniform lowp vec4 color; uniform float bushDepth; uniform lowp float bushOpacity; -varying vec2 v_texCoord; +in vec2 v_texCoord; const vec3 lumaF = vec3(.299, .587, .114); -void main() -{ - /* Sample source color */ - vec4 frag = texture2D(texture, v_texCoord); - - /* Apply gray */ - float luma = dot(frag.rgb, lumaF); - frag.rgb = mix(frag.rgb, vec3(luma), tone.w); - - /* Apply tone */ - frag.rgb += tone.rgb; +out vec4 fragColor; - /* Apply opacity */ - frag.a *= opacity; - - /* Apply color */ - frag.rgb = mix(frag.rgb, color.rgb, color.a); +void main() { + /* Sample source color */ + vec4 frag = texture(v_texture, v_texCoord); - /* Apply bush alpha by mathematical if */ - lowp float underBush = float(v_texCoord.y < bushDepth); - frag.a *= clamp(bushOpacity + underBush, 0.0, 1.0); - - gl_FragColor = frag; + /* Apply gray */ + float luma = dot(frag.rgb, lumaF); + frag.rgb = mix(frag.rgb, vec3(luma), tone.w); + + /* Apply tone */ + frag.rgb += tone.rgb; + + /* Apply opacity */ + frag.a *= opacity; + + /* Apply color */ + frag.rgb = mix(frag.rgb, color.rgb, color.a); + + /* Apply bush alpha by mathematical if */ + lowp float underBush = float(v_texCoord.y < bushDepth); + frag.a *= clamp(bushOpacity + underBush, 0.0, 1.0); + + fragColor = frag; } diff --git a/shader/trans.frag b/shader/trans.frag index 9023aed0..9710e52e 100644 --- a/shader/trans.frag +++ b/shader/trans.frag @@ -8,16 +8,17 @@ uniform float prog; /* Vague [0, 512] normalized */ uniform float vague; -varying vec2 v_texCoord; +in vec2 v_texCoord; -void main() -{ - float transV = texture2D(transMap, v_texCoord).r; - float cTransV = clamp(transV, prog, prog+vague); - lowp float alpha = (cTransV - prog) / vague; - - vec4 newFrag = texture2D(currentScene, v_texCoord); - vec4 oldFrag = texture2D(frozenScene, v_texCoord); - - gl_FragColor = mix(newFrag, oldFrag, alpha); +out vec4 fragColor; + +void main() { + float transV = texture(transMap, v_texCoord).r; + float cTransV = clamp(transV, prog, prog + vague); + lowp float alpha = (cTransV - prog) / vague; + + vec4 newFrag = texture(currentScene, v_texCoord); + vec4 oldFrag = texture(frozenScene, v_texCoord); + + fragColor = mix(newFrag, oldFrag, alpha); } diff --git a/shader/transSimple.frag b/shader/transSimple.frag index a5aac7d9..69c5c7e7 100644 --- a/shader/transSimple.frag +++ b/shader/transSimple.frag @@ -5,12 +5,13 @@ uniform sampler2D frozenScene; uniform sampler2D currentScene; uniform float prog; -varying vec2 v_texCoord; +in vec2 v_texCoord; -void main() -{ - vec4 newPixel = texture2D(currentScene, v_texCoord); - vec4 oldPixel = texture2D(frozenScene, v_texCoord); +out vec4 fragColor; - gl_FragColor = mix(oldPixel, newPixel, prog); +void main() { + vec4 newPixel = texture(currentScene, v_texCoord); + vec4 oldPixel = texture(frozenScene, v_texCoord); + + fragColor = mix(oldPixel, newPixel, prog); } diff --git a/src/main.mm b/src/main.mm index 9677bf5f..ff75c255 100644 --- a/src/main.mm +++ b/src/main.mm @@ -409,6 +409,11 @@ static SDL_GLContext initGL(SDL_Window *win, Config &conf, if (conf.debugMode) SDL_GL_SetAttribute(SDL_GL_CONTEXT_FLAGS, SDL_GL_CONTEXT_DEBUG_FLAG); + // Enables OpenGL 3 and 4 on macOS + SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 3); + SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 3); + SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, SDL_GL_CONTEXT_PROFILE_CORE); + glCtx = SDL_GL_CreateContext(win); if (!glCtx) { diff --git a/src/shader.cpp b/src/shader.cpp index e4aee7eb..f423969c 100644 --- a/src/shader.cpp +++ b/src/shader.cpp @@ -114,20 +114,20 @@ void Shader::unbind() static void setupShaderSource(GLuint shader, GLenum type, const unsigned char *body, int bodySize) { - static const char glesDefine[] = "#define GLSLES\n"; - static const char fragDefine[] = "#define FRAGMENT_SHADER\n"; + //static const char glesDefine[] = "#define GLSLES\n"; + static const char fragDefine[] = "";//#define FRAGMENT_SHADER\n"; const GLchar *shaderSrc[4]; GLint shaderSrcSize[4]; size_t i = 0; - +/* if (gl.glsles) { shaderSrc[i] = glesDefine; shaderSrcSize[i] = sizeof(glesDefine)-1; ++i; } - +*/ if (type == GL_FRAGMENT_SHADER) { shaderSrc[i] = fragDefine;