Use Core GL context, allow OpenGL3/4 on macOS

This commit is contained in:
Struma 2020-02-24 22:29:36 -05:00 committed by Roza
parent 3ffad8c05f
commit 827e20db87
18 changed files with 166 additions and 176 deletions

View file

@ -8,26 +8,27 @@ uniform vec4 subRect;
uniform lowp float opacity; uniform lowp float opacity;
varying vec2 v_texCoord; in vec2 v_texCoord;
void main() out vec4 fragColor;
{
vec2 coor = v_texCoord;
vec2 dstCoor = (coor - subRect.xy) * subRect.zw;
vec4 srcFrag = texture2D(source, coor); void main() {
vec4 dstFrag = texture2D(destination, dstCoor); 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; vec4 resFrag;
float co2 = dstFrag.a * (1.0 - co1);
resFrag.a = co1 + co2;
if (resFrag.a == 0.0) float co1 = srcFrag.a * opacity;
resFrag.rgb = srcFrag.rgb; float co2 = dstFrag.a * (1.0 - co1);
else resFrag.a = co1 + co2;
resFrag.rgb = (co1*srcFrag.rgb + co2*dstFrag.rgb) / resFrag.a;
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;
} }

View file

@ -1,16 +1,17 @@
uniform sampler2D texture; uniform sampler2D v_texture;
varying vec2 v_texCoord; in vec2 v_texCoord;
varying vec2 v_blurCoord[2]; in vec2 v_blurCoord[2];
void main() out vec4 fragColor;
{
lowp vec4 frag = vec4(0, 0, 0, 0);
frag += texture2D(texture, v_texCoord); void main() {
frag += texture2D(texture, v_blurCoord[0]); lowp vec4 frag = vec4(0, 0, 0, 0);
frag += texture2D(texture, v_blurCoord[1]);
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;
} }

View file

@ -1,15 +1,8 @@
#ifdef GLSLES #version 330
#define attribute in
#ifdef FRAGMENT_SHADER #define varying out
/* Only the fragment shader has no default float precision */
precision mediump float;
#endif
#else
/* Desktop GLSL doesn't know about these */ /* Desktop GLSL doesn't know about these */
#define highp #define highp
#define mediump #define mediump
#define lowp #define lowp
#endif

View file

@ -1,9 +1,7 @@
uniform lowp float alpha; uniform lowp float alpha;
varying lowp vec4 v_color; in lowp vec4 v_color;
void main() out vec4 fragColor;
{ void main() { fragColor = vec4(v_color.rgb * alpha, 1); }
gl_FragColor = vec4(v_color.rgb * alpha, 1);
}

View file

@ -1,7 +1,4 @@
uniform lowp vec4 color; uniform lowp vec4 color;
out vec4 fragColor;
void main() void main() { fragColor = color; }
{
gl_FragColor = color;
}

View file

@ -1,19 +1,19 @@
uniform sampler2D texture; uniform sampler2D v_texture;
uniform lowp float gray; uniform lowp float gray;
varying vec2 v_texCoord; in vec2 v_texCoord;
const vec3 lumaF = vec3(.299, .587, .114); const vec3 lumaF = vec3(.299, .587, .114);
void main() out vec4 fragColor;
{ void main() {
/* Sample source color */ /* Sample source color */
vec4 frag = texture2D(texture, v_texCoord); vec4 frag = texture(v_texture, v_texCoord);
/* Apply gray */ /* Apply gray */
float luma = dot(frag.rgb, lumaF); float luma = dot(frag.rgb, lumaF);
frag.rgb = mix(frag.rgb, vec3(luma), gray); frag.rgb = mix(frag.rgb, vec3(luma), gray);
gl_FragColor = frag; fragColor = frag;
} }

View file

@ -1,40 +1,39 @@
uniform sampler2D texture; uniform sampler2D v_texture;
uniform mediump float hueAdjust; uniform mediump float hueAdjust;
varying vec2 v_texCoord; in vec2 v_texCoord;
out vec4 fragColor;
/* Source: gamedev.stackexchange.com/a/59808/24839 */ /* Source: gamedev.stackexchange.com/a/59808/24839 */
vec3 rgb2hsv(vec3 c) vec3 rgb2hsv(vec3 c) {
{ const vec4 K = vec4(0.0, -1.0 / 3.0, 2.0 / 3.0, -1.0);
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 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));
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. /* Avoid divide-by-zero situations by adding a very tiny delta.
* Since we always deal with underlying 8-Bit color values, this * Since we always deal with underlying 8-Bit color values, this
* should never mask a real value */ * should never mask a real value */
const float eps = 1.0e-10; 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) vec3 hsv2rgb(vec3 c) {
{ const vec4 K = vec4(1.0, 2.0 / 3.0, 1.0 / 3.0, 3.0);
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);
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);
return c.z * mix(K.xxx, clamp(p - K.xxx, 0.0, 1.0), c.y);
} }
void main () void main() {
{ vec4 color = texture(v_texture, v_texCoord.xy);
vec4 color = texture2D (texture, v_texCoord.xy); vec3 hsv = rgb2hsv(color.rgb);
vec3 hsv = rgb2hsv(color.rgb);
hsv.x += hueAdjust; hsv.x += hueAdjust;
color.rgb = hsv2rgb(hsv); color.rgb = hsv2rgb(hsv);
gl_FragColor = color; fragColor = color;
} }

View file

@ -2,7 +2,4 @@
uniform mat4 projMat; uniform mat4 projMat;
attribute vec2 position; attribute vec2 position;
void main() void main() { gl_Position = projMat * vec4(position, 0, 1); }
{
gl_Position = projMat * vec4(position, 0, 1);
}

View file

@ -1,5 +1,5 @@
uniform sampler2D texture; uniform sampler2D v_texture;
uniform lowp vec4 tone; uniform lowp vec4 tone;
@ -7,30 +7,31 @@ uniform lowp float opacity;
uniform lowp vec4 color; uniform lowp vec4 color;
uniform lowp vec4 flash; uniform lowp vec4 flash;
varying vec2 v_texCoord; in vec2 v_texCoord;
const vec3 lumaF = vec3(.299, .587, .114); const vec3 lumaF = vec3(.299, .587, .114);
void main() out vec4 fragColor;
{
/* 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;
/* Apply opacity */ void main() {
frag.a *= opacity; /* Sample source color */
vec4 frag = texture(v_texture, v_texCoord);
/* Apply color */
frag.rgb = mix(frag.rgb, color.rgb, color.a);
/* Apply flash */ /* Apply gray */
frag.rgb = mix(frag.rgb, flash.rgb, flash.a); float luma = dot(frag.rgb, lumaF);
frag.rgb = mix(frag.rgb, vec3(luma), tone.w);
gl_FragColor = frag;
/* 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;
} }

View file

@ -1,9 +1,7 @@
uniform sampler2D texture; uniform sampler2D v_texture;
varying vec2 v_texCoord; in vec2 v_texCoord;
void main() out vec4 fragColor;
{ void main() { fragColor = texture(v_texture, v_texCoord); }
gl_FragColor = texture2D(texture, v_texCoord);
}

View file

@ -1,11 +1,11 @@
uniform sampler2D texture; uniform sampler2D v_texture;
varying vec2 v_texCoord; in vec2 v_texCoord;
varying lowp vec4 v_color; in lowp vec4 v_color;
void main() out vec4 fragColor;
{ void main() {
gl_FragColor = texture2D(texture, v_texCoord); fragColor = texture(v_texture, v_texCoord);
gl_FragColor.a *= v_color.a; fragColor.a *= v_color.a;
} }

View file

@ -1,11 +1,11 @@
uniform sampler2D texture; uniform sampler2D v_texture;
uniform lowp float alpha; uniform lowp float alpha;
varying vec2 v_texCoord; in vec2 v_texCoord;
out vec4 fragColor;
void main() void main() {
{ fragColor = texture(v_texture, v_texCoord);
gl_FragColor = texture2D(texture, v_texCoord); fragColor.a *= alpha;
gl_FragColor.a *= alpha;
} }

View file

@ -1,7 +1,4 @@
varying lowp vec4 v_color; in lowp vec4 v_color;
out vec4 fragColor;
void main() void main() { fragColor = v_color; }
{
gl_FragColor = v_color;
}

View file

@ -1,5 +1,5 @@
uniform sampler2D texture; uniform sampler2D v_texture;
uniform lowp vec4 tone; uniform lowp vec4 tone;
@ -9,31 +9,32 @@ uniform lowp vec4 color;
uniform float bushDepth; uniform float bushDepth;
uniform lowp float bushOpacity; uniform lowp float bushOpacity;
varying vec2 v_texCoord; in vec2 v_texCoord;
const vec3 lumaF = vec3(.299, .587, .114); const vec3 lumaF = vec3(.299, .587, .114);
void main() out vec4 fragColor;
{
/* 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;
/* Apply opacity */ void main() {
frag.a *= opacity; /* Sample source color */
vec4 frag = texture(v_texture, v_texCoord);
/* Apply color */
frag.rgb = mix(frag.rgb, color.rgb, color.a);
/* Apply bush alpha by mathematical if */ /* Apply gray */
lowp float underBush = float(v_texCoord.y < bushDepth); float luma = dot(frag.rgb, lumaF);
frag.a *= clamp(bushOpacity + underBush, 0.0, 1.0); frag.rgb = mix(frag.rgb, vec3(luma), tone.w);
gl_FragColor = frag; /* 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;
} }

View file

@ -8,16 +8,17 @@ uniform float prog;
/* Vague [0, 512] normalized */ /* Vague [0, 512] normalized */
uniform float vague; uniform float vague;
varying vec2 v_texCoord; in vec2 v_texCoord;
void main() out vec4 fragColor;
{
float transV = texture2D(transMap, v_texCoord).r; void main() {
float cTransV = clamp(transV, prog, prog+vague); float transV = texture(transMap, v_texCoord).r;
lowp float alpha = (cTransV - prog) / vague; 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); vec4 newFrag = texture(currentScene, v_texCoord);
vec4 oldFrag = texture(frozenScene, v_texCoord);
gl_FragColor = mix(newFrag, oldFrag, alpha);
fragColor = mix(newFrag, oldFrag, alpha);
} }

View file

@ -5,12 +5,13 @@ uniform sampler2D frozenScene;
uniform sampler2D currentScene; uniform sampler2D currentScene;
uniform float prog; uniform float prog;
varying vec2 v_texCoord; in vec2 v_texCoord;
void main() out vec4 fragColor;
{
vec4 newPixel = texture2D(currentScene, v_texCoord);
vec4 oldPixel = texture2D(frozenScene, v_texCoord);
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);
} }

View file

@ -409,6 +409,11 @@ static SDL_GLContext initGL(SDL_Window *win, Config &conf,
if (conf.debugMode) if (conf.debugMode)
SDL_GL_SetAttribute(SDL_GL_CONTEXT_FLAGS, SDL_GL_CONTEXT_DEBUG_FLAG); 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); glCtx = SDL_GL_CreateContext(win);
if (!glCtx) { if (!glCtx) {

View file

@ -114,20 +114,20 @@ void Shader::unbind()
static void setupShaderSource(GLuint shader, GLenum type, static void setupShaderSource(GLuint shader, GLenum type,
const unsigned char *body, int bodySize) const unsigned char *body, int bodySize)
{ {
static const char glesDefine[] = "#define GLSLES\n"; //static const char glesDefine[] = "#define GLSLES\n";
static const char fragDefine[] = "#define FRAGMENT_SHADER\n"; static const char fragDefine[] = "";//#define FRAGMENT_SHADER\n";
const GLchar *shaderSrc[4]; const GLchar *shaderSrc[4];
GLint shaderSrcSize[4]; GLint shaderSrcSize[4];
size_t i = 0; size_t i = 0;
/*
if (gl.glsles) if (gl.glsles)
{ {
shaderSrc[i] = glesDefine; shaderSrc[i] = glesDefine;
shaderSrcSize[i] = sizeof(glesDefine)-1; shaderSrcSize[i] = sizeof(glesDefine)-1;
++i; ++i;
} }
*/
if (type == GL_FRAGMENT_SHADER) if (type == GL_FRAGMENT_SHADER)
{ {
shaderSrc[i] = fragDefine; shaderSrc[i] = fragDefine;