From 36baa6a10e69a70a8054ca74c7ed81e00dc981d4 Mon Sep 17 00:00:00 2001 From: zzoro Date: Mon, 16 Nov 2020 21:56:19 -0500 Subject: [PATCH] Revert to using GL compatibility contexts (and GLES2) --- 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/tilemap.frag | 13 ++++------ shader/tilemap.vert | 23 +++++++++--------- shader/trans.frag | 23 +++++++++--------- shader/transSimple.frag | 13 +++++----- src/config.h | 5 ---- src/config.mm | 8 ------- src/main.mm | 7 ------ src/shader.cpp | 8 +++---- 22 files changed, 192 insertions(+), 201 deletions(-) diff --git a/shader/bitmapBlit.frag b/shader/bitmapBlit.frag index 35ffba8f..850b919f 100644 --- a/shader/bitmapBlit.frag +++ b/shader/bitmapBlit.frag @@ -8,27 +8,26 @@ uniform vec4 subRect; uniform lowp float opacity; -in vec2 v_texCoord; +varying vec2 v_texCoord; -out vec4 fragColor; +void main() +{ + vec2 coor = v_texCoord; + vec2 dstCoor = (coor - subRect.xy) * subRect.zw; -void main() { - vec2 coor = v_texCoord; - vec2 dstCoor = (coor - subRect.xy) * subRect.zw; + vec4 srcFrag = texture2D(source, coor); + vec4 dstFrag = texture2D(destination, dstCoor); - vec4 srcFrag = texture(source, coor); - vec4 dstFrag = texture(destination, dstCoor); + vec4 resFrag; - vec4 resFrag; + float co1 = srcFrag.a * opacity; + float co2 = dstFrag.a * (1.0 - co1); + resFrag.a = co1 + co2; - float co1 = srcFrag.a * opacity; - float co2 = dstFrag.a * (1.0 - co1); - resFrag.a = co1 + co2; + if (resFrag.a == 0.0) + resFrag.rgb = srcFrag.rgb; + else + resFrag.rgb = (co1*srcFrag.rgb + co2*dstFrag.rgb) / resFrag.a; - if (resFrag.a == 0.0) - resFrag.rgb = srcFrag.rgb; - else - resFrag.rgb = (co1 * srcFrag.rgb + co2 * dstFrag.rgb) / resFrag.a; - - fragColor = resFrag; + gl_FragColor = resFrag; } diff --git a/shader/blur.frag b/shader/blur.frag index 0aee5e3d..3b8fe48c 100644 --- a/shader/blur.frag +++ b/shader/blur.frag @@ -1,17 +1,16 @@ -uniform sampler2D v_texture; +uniform sampler2D texture; -in vec2 v_texCoord; -in vec2 v_blurCoord[2]; +varying vec2 v_texCoord; +varying vec2 v_blurCoord[2]; -out vec4 fragColor; +void main() +{ + lowp vec4 frag = vec4(0, 0, 0, 0); -void main() { - lowp vec4 frag = vec4(0, 0, 0, 0); + frag += texture2D(texture, v_texCoord); + frag += texture2D(texture, v_blurCoord[0]); + frag += texture2D(texture, v_blurCoord[1]); - frag += texture(v_texture, v_texCoord); - frag += texture(v_texture, v_blurCoord[0]); - frag += texture(v_texture, v_blurCoord[1]); - - fragColor = frag / 3.0; + gl_FragColor = frag / 3.0; } diff --git a/shader/common.h b/shader/common.h index 64068da9..a34ef548 100644 --- a/shader/common.h +++ b/shader/common.h @@ -1,8 +1,15 @@ -#version 330 -#define attribute in -#define varying out +#ifdef GLSLES + +#ifdef FRAGMENT_SHADER +/* Only the fragment shader has no default float precision */ +precision mediump float; +#endif + +#else /* 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 e5d463a8..34ed06b8 100644 --- a/shader/flashMap.frag +++ b/shader/flashMap.frag @@ -1,7 +1,9 @@ uniform lowp float alpha; -in lowp vec4 v_color; +varying lowp vec4 v_color; -out vec4 fragColor; -void main() { fragColor = vec4(v_color.rgb * alpha, 1); } +void main() +{ + gl_FragColor = vec4(v_color.rgb * alpha, 1); +} diff --git a/shader/flatColor.frag b/shader/flatColor.frag index 1b51e2cd..985f5dc0 100644 --- a/shader/flatColor.frag +++ b/shader/flatColor.frag @@ -1,4 +1,7 @@ uniform lowp vec4 color; -out vec4 fragColor; -void main() { fragColor = color; } + +void main() +{ + gl_FragColor = color; +} diff --git a/shader/gray.frag b/shader/gray.frag index 22278a44..c4cb9827 100644 --- a/shader/gray.frag +++ b/shader/gray.frag @@ -1,19 +1,19 @@ -uniform sampler2D v_texture; +uniform sampler2D texture; uniform lowp float gray; -in vec2 v_texCoord; +varying vec2 v_texCoord; const vec3 lumaF = vec3(.299, .587, .114); -out vec4 fragColor; -void main() { - /* Sample source color */ - vec4 frag = texture(v_texture, v_texCoord); +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), gray); + /* Apply gray */ + float luma = dot(frag.rgb, lumaF); + frag.rgb = mix(frag.rgb, vec3(luma), gray); - fragColor = frag; + gl_FragColor = frag; } diff --git a/shader/hue.frag b/shader/hue.frag index 707b80b1..61143ac3 100644 --- a/shader/hue.frag +++ b/shader/hue.frag @@ -1,39 +1,40 @@ -uniform sampler2D v_texture; +uniform sampler2D texture; uniform mediump float hueAdjust; -in vec2 v_texCoord; - -out vec4 fragColor; +varying vec2 v_texCoord; /* 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 = texture(v_texture, v_texCoord.xy); - vec3 hsv = rgb2hsv(color.rgb); +void main () +{ + vec4 color = texture2D (texture, v_texCoord.xy); + vec3 hsv = rgb2hsv(color.rgb); - hsv.x += hueAdjust; - color.rgb = hsv2rgb(hsv); + hsv.x += hueAdjust; + color.rgb = hsv2rgb(hsv); - fragColor = color; + gl_FragColor = color; } diff --git a/shader/minimal.vert b/shader/minimal.vert index 33635e47..cccadc1b 100644 --- a/shader/minimal.vert +++ b/shader/minimal.vert @@ -2,4 +2,7 @@ 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 f39e355d..14b52c56 100644 --- a/shader/plane.frag +++ b/shader/plane.frag @@ -1,5 +1,5 @@ -uniform sampler2D v_texture; +uniform sampler2D texture; uniform lowp vec4 tone; @@ -7,31 +7,30 @@ uniform lowp float opacity; uniform lowp vec4 color; uniform lowp vec4 flash; -in vec2 v_texCoord; +varying vec2 v_texCoord; const vec3 lumaF = vec3(.299, .587, .114); -out vec4 fragColor; +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; -void main() { - /* Sample source color */ - vec4 frag = texture(v_texture, v_texCoord); + /* Apply opacity */ + frag.a *= opacity; + + /* Apply color */ + frag.rgb = mix(frag.rgb, color.rgb, color.a); - /* 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; + /* Apply flash */ + frag.rgb = mix(frag.rgb, flash.rgb, flash.a); + + gl_FragColor = frag; } diff --git a/shader/simple.frag b/shader/simple.frag index aede6961..e00b11cb 100644 --- a/shader/simple.frag +++ b/shader/simple.frag @@ -1,7 +1,9 @@ -uniform sampler2D v_texture; +uniform sampler2D texture; -in vec2 v_texCoord; +varying vec2 v_texCoord; -out vec4 fragColor; -void main() { fragColor = texture(v_texture, v_texCoord); } +void main() +{ + gl_FragColor = texture2D(texture, v_texCoord); +} diff --git a/shader/simpleAlpha.frag b/shader/simpleAlpha.frag index 9c181f06..163bca90 100644 --- a/shader/simpleAlpha.frag +++ b/shader/simpleAlpha.frag @@ -1,11 +1,11 @@ -uniform sampler2D v_texture; +uniform sampler2D texture; -in vec2 v_texCoord; -in lowp vec4 v_color; +varying vec2 v_texCoord; +varying lowp vec4 v_color; -out vec4 fragColor; -void main() { - fragColor = texture(v_texture, v_texCoord); - fragColor.a *= v_color.a; +void main() +{ + gl_FragColor = texture2D(texture, v_texCoord); + gl_FragColor.a *= v_color.a; } diff --git a/shader/simpleAlphaUni.frag b/shader/simpleAlphaUni.frag index cef53800..cc0cac3c 100644 --- a/shader/simpleAlphaUni.frag +++ b/shader/simpleAlphaUni.frag @@ -1,11 +1,11 @@ -uniform sampler2D v_texture; +uniform sampler2D texture; uniform lowp float alpha; -in vec2 v_texCoord; -out vec4 fragColor; +varying vec2 v_texCoord; -void main() { - fragColor = texture(v_texture, v_texCoord); - fragColor.a *= alpha; +void main() +{ + gl_FragColor = texture2D(texture, v_texCoord); + gl_FragColor.a *= alpha; } diff --git a/shader/simpleColor.frag b/shader/simpleColor.frag index c4a1fa76..da3a77fc 100644 --- a/shader/simpleColor.frag +++ b/shader/simpleColor.frag @@ -1,4 +1,7 @@ -in lowp vec4 v_color; -out vec4 fragColor; -void main() { fragColor = v_color; } +varying lowp vec4 v_color; + +void main() +{ + gl_FragColor = v_color; +} diff --git a/shader/sprite.frag b/shader/sprite.frag index 383d2912..93c35f38 100644 --- a/shader/sprite.frag +++ b/shader/sprite.frag @@ -1,5 +1,5 @@ -uniform sampler2D v_texture; +uniform sampler2D texture; uniform lowp vec4 tone; @@ -9,32 +9,31 @@ uniform lowp vec4 color; uniform float bushDepth; uniform lowp float bushOpacity; -in vec2 v_texCoord; +varying vec2 v_texCoord; const vec3 lumaF = vec3(.299, .587, .114); -out vec4 fragColor; +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; -void main() { - /* Sample source color */ - vec4 frag = texture(v_texture, v_texCoord); + /* Apply opacity */ + frag.a *= opacity; + + /* Apply color */ + frag.rgb = mix(frag.rgb, color.rgb, color.a); - /* 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; + /* 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; } diff --git a/shader/tilemap.frag b/shader/tilemap.frag index 7f988839..9c20b652 100644 --- a/shader/tilemap.frag +++ b/shader/tilemap.frag @@ -1,20 +1,17 @@ - -uniform sampler2D v_texture; +uniform sampler2D texture; uniform lowp vec4 tone; uniform lowp float opacity; uniform lowp vec4 color; -in vec2 v_texCoord; +varying vec2 v_texCoord; const vec3 lumaF = vec3(.299, .587, .114); -out vec4 fragColor; - void main() { /* Sample source color */ - vec4 frag = texture(v_texture, v_texCoord); + vec4 frag = texture2D(texture, v_texCoord); /* Apply gray */ float luma = dot(frag.rgb, lumaF); @@ -29,5 +26,5 @@ void main() { /* Apply color */ frag.rgb = mix(frag.rgb, color.rgb, color.a); - fragColor = frag; -} + gl_FragColor = frag; +} \ No newline at end of file diff --git a/shader/tilemap.vert b/shader/tilemap.vert index 69f725d5..1a88a5e2 100644 --- a/shader/tilemap.vert +++ b/shader/tilemap.vert @@ -1,4 +1,3 @@ - uniform mat4 projMat; uniform vec2 texSizeInv; @@ -17,7 +16,7 @@ const float tileH = 32.0; const float autotileW = 3.0*tileW; const float autotileH = 4.0*tileW; const float atAreaW = autotileW; -const float atAreaH = autotileH*nAutotiles; +const float atAreaH = autotileH*float(nAutotiles); const float atAniOffsetX = 3.0*tileW; const float atAniOffsetY = tileH; @@ -25,17 +24,17 @@ uniform lowp int atFrames[nAutotiles]; void main() { - vec2 tex = texCoord; - lowp uint atIndex = uint(tex.y / autotileH); + vec2 tex = texCoord; + lowp int atIndex = int(tex.y / autotileH); - lowp uint pred = uint(tex.x <= atAreaW && tex.y <= atAreaH); - lowp uint frame = uint(aniIndex % atFrames[atIndex]); - lowp uint col = frame % 8u; - lowp uint row = frame / 8u; - tex.x += atAniOffsetX * (col * pred); - tex.y += atAniOffsetY * (row * pred); + lowp int pred = int(tex.x <= atAreaW && tex.y <= atAreaH); + lowp int frame = int(aniIndex - atFrames[atIndex] * (aniIndex / atFrames[atIndex])); + lowp int row = frame / 8; + lowp int col = frame - 8 * row; + tex.x += atAniOffsetX * float(col * pred); + tex.y += atAniOffsetY * float(row * pred); - gl_Position = projMat * vec4(position + translation, 0, 1); + gl_Position = projMat * vec4(position + translation, 0, 1); - v_texCoord = tex * texSizeInv; + v_texCoord = tex * texSizeInv; } diff --git a/shader/trans.frag b/shader/trans.frag index 9710e52e..9023aed0 100644 --- a/shader/trans.frag +++ b/shader/trans.frag @@ -8,17 +8,16 @@ uniform float prog; /* Vague [0, 512] normalized */ uniform float vague; -in vec2 v_texCoord; +varying vec2 v_texCoord; -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); +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); } diff --git a/shader/transSimple.frag b/shader/transSimple.frag index 69c5c7e7..a5aac7d9 100644 --- a/shader/transSimple.frag +++ b/shader/transSimple.frag @@ -5,13 +5,12 @@ uniform sampler2D frozenScene; uniform sampler2D currentScene; uniform float prog; -in vec2 v_texCoord; +varying vec2 v_texCoord; -out vec4 fragColor; +void main() +{ + vec4 newPixel = texture2D(currentScene, v_texCoord); + vec4 oldPixel = texture2D(frozenScene, v_texCoord); -void main() { - vec4 newPixel = texture(currentScene, v_texCoord); - vec4 oldPixel = texture(frozenScene, v_texCoord); - - fragColor = mix(oldPixel, newPixel, prog); + gl_FragColor = mix(oldPixel, newPixel, prog); } diff --git a/src/config.h b/src/config.h index 51c53d64..9b1156d7 100644 --- a/src/config.h +++ b/src/config.h @@ -29,11 +29,6 @@ struct Config { int rgssVersion; - struct { - char major; - char minor; - } glVersion; - bool debugMode; bool printFPS; diff --git a/src/config.mm b/src/config.mm index 9905f2ea..63dd6d92 100644 --- a/src/config.mm +++ b/src/config.mm @@ -170,14 +170,6 @@ void Config::read(int argc, char *argv[]) { fillStringVec(opts[@"rubyLoadpath"], rubyLoadpaths); rgssVersion = clamp(rgssVersion, 0, 3); SE.sourceCount = clamp(SE.sourceCount, 1, 64); - - if ([opts[@"openGL4"] boolValue]) { - glVersion.major = 4; - glVersion.minor = 1; - } else { - glVersion.major = 3; - glVersion.minor = 3; - } } static void setupScreenSize(Config &conf) { diff --git a/src/main.mm b/src/main.mm index ce39d625..28f0bced 100644 --- a/src/main.mm +++ b/src/main.mm @@ -448,13 +448,6 @@ static SDL_GLContext initGL(SDL_Window *win, Config &conf, if (conf.debugMode) SDL_GL_SetAttribute(SDL_GL_CONTEXT_FLAGS, SDL_GL_CONTEXT_DEBUG_FLAG); - // Core profile enables OpenGL4 on macOS - // Using OpenGL 4.1 requires a GPU with max texture size of 16384 or better - - SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, conf.glVersion.major); - SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, conf.glVersion.minor); - 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 260e250a..b050e8f0 100644 --- a/src/shader.cpp +++ b/src/shader.cpp @@ -115,20 +115,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;