From 7374a023eff964817c9e5fbe75a661540516f798 Mon Sep 17 00:00:00 2001 From: UjinT34 <41110182+UjinT34@users.noreply.github.com> Date: Sat, 29 Mar 2025 03:19:35 +0300 Subject: renderer/opengl: Extract shaders from source (#9600) --------- Co-authored-by: Mihai Fufezan --- src/render/shaders/Border.hpp | 187 -------- src/render/shaders/CM.frag | 440 ------------------- src/render/shaders/Shadow.hpp | 90 ---- src/render/shaders/SharedValues.hpp | 3 - src/render/shaders/Textures.hpp | 541 ------------------------ src/render/shaders/glsl/CM.frag | 55 +++ src/render/shaders/glsl/CM.glsl | 364 ++++++++++++++++ src/render/shaders/glsl/blur1.frag | 141 ++++++ src/render/shaders/glsl/blur2.frag | 23 + src/render/shaders/glsl/blurfinish.frag | 32 ++ src/render/shaders/glsl/blurfinish_legacy.frag | 28 ++ src/render/shaders/glsl/blurprepare.frag | 58 +++ src/render/shaders/glsl/blurprepare_legacy.frag | 29 ++ src/render/shaders/glsl/border.frag | 183 ++++++++ src/render/shaders/glsl/border_legacy.frag | 174 ++++++++ src/render/shaders/glsl/ext.frag | 35 ++ src/render/shaders/glsl/glitch.frag | 64 +++ src/render/shaders/glsl/passthru.frag | 7 + src/render/shaders/glsl/quad.frag | 14 + src/render/shaders/glsl/rgba.frag | 36 ++ src/render/shaders/glsl/rgbamatte.frag | 8 + src/render/shaders/glsl/rgbx.frag | 33 ++ src/render/shaders/glsl/rounding.glsl | 29 ++ src/render/shaders/glsl/shadow.frag | 100 +++++ src/render/shaders/glsl/shadow_legacy.frag | 86 ++++ src/render/shaders/glsl/tex.vert | 15 + src/render/shaders/glsl/tex300.vert | 17 + src/render/shaders/glsl/tex320.vert | 17 + 28 files changed, 1548 insertions(+), 1261 deletions(-) delete mode 100644 src/render/shaders/Border.hpp delete mode 100644 src/render/shaders/CM.frag delete mode 100644 src/render/shaders/Shadow.hpp delete mode 100644 src/render/shaders/SharedValues.hpp delete mode 100644 src/render/shaders/Textures.hpp create mode 100644 src/render/shaders/glsl/CM.frag create mode 100644 src/render/shaders/glsl/CM.glsl create mode 100644 src/render/shaders/glsl/blur1.frag create mode 100644 src/render/shaders/glsl/blur2.frag create mode 100644 src/render/shaders/glsl/blurfinish.frag create mode 100644 src/render/shaders/glsl/blurfinish_legacy.frag create mode 100644 src/render/shaders/glsl/blurprepare.frag create mode 100644 src/render/shaders/glsl/blurprepare_legacy.frag create mode 100644 src/render/shaders/glsl/border.frag create mode 100644 src/render/shaders/glsl/border_legacy.frag create mode 100644 src/render/shaders/glsl/ext.frag create mode 100644 src/render/shaders/glsl/glitch.frag create mode 100644 src/render/shaders/glsl/passthru.frag create mode 100644 src/render/shaders/glsl/quad.frag create mode 100644 src/render/shaders/glsl/rgba.frag create mode 100644 src/render/shaders/glsl/rgbamatte.frag create mode 100644 src/render/shaders/glsl/rgbx.frag create mode 100644 src/render/shaders/glsl/rounding.glsl create mode 100644 src/render/shaders/glsl/shadow.frag create mode 100644 src/render/shaders/glsl/shadow_legacy.frag create mode 100644 src/render/shaders/glsl/tex.vert create mode 100644 src/render/shaders/glsl/tex300.vert create mode 100644 src/render/shaders/glsl/tex320.vert (limited to 'src/render/shaders') diff --git a/src/render/shaders/Border.hpp b/src/render/shaders/Border.hpp deleted file mode 100644 index 3b9a91f0..00000000 --- a/src/render/shaders/Border.hpp +++ /dev/null @@ -1,187 +0,0 @@ -#pragma once - -#include -#include -#include "SharedValues.hpp" - -// makes a stencil without corners -inline const std::string FRAGBORDER1 = R"#( -precision highp float; -varying vec4 v_color; -varying vec2 v_texcoord; - -uniform vec2 topLeft; -uniform vec2 fullSize; -uniform vec2 fullSizeUntransformed; -uniform float radius; -uniform float radiusOuter; -uniform float roundingPower; -uniform float thick; - -// Gradients are in OkLabA!!!! {l, a, b, alpha} -uniform vec4 gradient[10]; -uniform vec4 gradient2[10]; -uniform int gradientLength; -uniform int gradient2Length; -uniform float angle; -uniform float angle2; -uniform float gradientLerp; -uniform float alpha; - -float linearToGamma(float x) { - return x >= 0.0031308 ? 1.055 * pow(x, 0.416666666) - 0.055 : 12.92 * x; -} - -vec4 okLabAToSrgb(vec4 lab) { - float l = pow(lab[0] + lab[1] * 0.3963377774 + lab[2] * 0.2158037573, 3.0); - float m = pow(lab[0] + lab[1] * (-0.1055613458) + lab[2] * (-0.0638541728), 3.0); - float s = pow(lab[0] + lab[1] * (-0.0894841775) + lab[2] * (-1.2914855480), 3.0); - - return vec4(linearToGamma(l * 4.0767416621 + m * -3.3077115913 + s * 0.2309699292), - linearToGamma(l * (-1.2684380046) + m * 2.6097574011 + s * (-0.3413193965)), - linearToGamma(l * (-0.0041960863) + m * (-0.7034186147) + s * 1.7076147010), - lab[3]); -} - -vec4 getOkColorForCoordArray1(vec2 normalizedCoord) { - if (gradientLength < 2) - return gradient[0]; - - float finalAng = 0.0; - - if (angle > 4.71 /* 270 deg */) { - normalizedCoord[1] = 1.0 - normalizedCoord[1]; - finalAng = 6.28 - angle; - } else if (angle > 3.14 /* 180 deg */) { - normalizedCoord[0] = 1.0 - normalizedCoord[0]; - normalizedCoord[1] = 1.0 - normalizedCoord[1]; - finalAng = angle - 3.14; - } else if (angle > 1.57 /* 90 deg */) { - normalizedCoord[0] = 1.0 - normalizedCoord[0]; - finalAng = 3.14 - angle; - } else { - finalAng = angle; - } - - float sine = sin(finalAng); - - float progress = (normalizedCoord[1] * sine + normalizedCoord[0] * (1.0 - sine)) * float(gradientLength - 1); - int bottom = int(floor(progress)); - int top = bottom + 1; - - return gradient[top] * (progress - float(bottom)) + gradient[bottom] * (float(top) - progress); -} - -vec4 getOkColorForCoordArray2(vec2 normalizedCoord) { - if (gradient2Length < 2) - return gradient2[0]; - - float finalAng = 0.0; - - if (angle2 > 4.71 /* 270 deg */) { - normalizedCoord[1] = 1.0 - normalizedCoord[1]; - finalAng = 6.28 - angle; - } else if (angle2 > 3.14 /* 180 deg */) { - normalizedCoord[0] = 1.0 - normalizedCoord[0]; - normalizedCoord[1] = 1.0 - normalizedCoord[1]; - finalAng = angle - 3.14; - } else if (angle2 > 1.57 /* 90 deg */) { - normalizedCoord[0] = 1.0 - normalizedCoord[0]; - finalAng = 3.14 - angle2; - } else { - finalAng = angle2; - } - - float sine = sin(finalAng); - - float progress = (normalizedCoord[1] * sine + normalizedCoord[0] * (1.0 - sine)) * float(gradient2Length - 1); - int bottom = int(floor(progress)); - int top = bottom + 1; - - return gradient2[top] * (progress - float(bottom)) + gradient2[bottom] * (float(top) - progress); -} - -vec4 getColorForCoord(vec2 normalizedCoord) { - vec4 result1 = getOkColorForCoordArray1(normalizedCoord); - - if (gradient2Length <= 0) - return okLabAToSrgb(result1); - - vec4 result2 = getOkColorForCoordArray2(normalizedCoord); - - return okLabAToSrgb(mix(result1, result2, gradientLerp)); -} - -void main() { - - highp vec2 pixCoord = vec2(gl_FragCoord); - highp vec2 pixCoordOuter = pixCoord; - highp vec2 originalPixCoord = v_texcoord; - originalPixCoord *= fullSizeUntransformed; - float additionalAlpha = 1.0; - - vec4 pixColor = vec4(1.0, 1.0, 1.0, 1.0); - - bool done = false; - - pixCoord -= topLeft + fullSize * 0.5; - pixCoord *= vec2(lessThan(pixCoord, vec2(0.0))) * -2.0 + 1.0; - pixCoordOuter = pixCoord; - pixCoord -= fullSize * 0.5 - radius; - pixCoordOuter -= fullSize * 0.5 - radiusOuter; - - // center the pixes dont make it top-left - pixCoord += vec2(1.0, 1.0) / fullSize; - pixCoordOuter += vec2(1.0, 1.0) / fullSize; - - if (min(pixCoord.x, pixCoord.y) > 0.0 && radius > 0.0) { - // smoothing constant for the edge: more = blurrier, but smoother - const float SMOOTHING_CONSTANT = )#" + - std::format("{:.7f}", SHADER_ROUNDED_SMOOTHING_FACTOR) + R"#(; - - float dist = pow(pow(pixCoord.x,roundingPower)+pow(pixCoord.y,roundingPower),1.0/roundingPower); - float distOuter = pow(pow(pixCoordOuter.x,roundingPower)+pow(pixCoordOuter.y,roundingPower),1.0/roundingPower); - float h = (thick / 2.0); - - if (dist < radius - h) { - // lower - float normalized = smoothstep(0.0, 1.0, (dist - radius + thick + SMOOTHING_CONSTANT) / (SMOOTHING_CONSTANT * 2.0)); - additionalAlpha *= normalized; - done = true; - } else if (min(pixCoordOuter.x, pixCoordOuter.y) > 0.0) { - // higher - float normalized = 1.0 - smoothstep(0.0, 1.0, (distOuter - radiusOuter + SMOOTHING_CONSTANT) / (SMOOTHING_CONSTANT * 2.0)); - additionalAlpha *= normalized; - done = true; - } else if (distOuter < radiusOuter - h) { - additionalAlpha = 1.0; - done = true; - } - } - - // now check for other shit - if (!done) { - // distance to all straight bb borders - float distanceT = originalPixCoord[1]; - float distanceB = fullSizeUntransformed[1] - originalPixCoord[1]; - float distanceL = originalPixCoord[0]; - float distanceR = fullSizeUntransformed[0] - originalPixCoord[0]; - - // get the smallest - float smallest = min(min(distanceT, distanceB), min(distanceL, distanceR)); - - if (smallest > thick) - discard; - } - - if (additionalAlpha == 0.0) - discard; - - pixColor = getColorForCoord(v_texcoord); - pixColor.rgb *= pixColor[3]; - - pixColor *= alpha * additionalAlpha; - - gl_FragColor = pixColor; -} -)#"; diff --git a/src/render/shaders/CM.frag b/src/render/shaders/CM.frag deleted file mode 100644 index aa26f97d..00000000 --- a/src/render/shaders/CM.frag +++ /dev/null @@ -1,440 +0,0 @@ -R"#( -#version 320 es -//#extension GL_OES_EGL_image_external : require - -precision highp float; -in vec2 v_texcoord; -uniform sampler2D tex; -//uniform samplerExternalOES texture0; - -uniform int texType; // eTextureType: 0 - rgba, 1 - rgbx, 2 - ext -uniform int sourceTF; // eTransferFunction -uniform int targetTF; // eTransferFunction -uniform mat4x2 sourcePrimaries; -uniform mat4x2 targetPrimaries; -uniform float maxLuminance; -uniform float dstMaxLuminance; -uniform float dstRefLuminance; -uniform float sdrSaturation; -uniform float sdrBrightnessMultiplier; - -uniform float alpha; - -uniform vec2 topLeft; -uniform vec2 fullSize; -uniform float radius; -uniform float roundingPower; - -uniform int discardOpaque; -uniform int discardAlpha; -uniform float discardAlphaValue; - -uniform int applyTint; -uniform vec3 tint; - -//enum eTransferFunction -#define CM_TRANSFER_FUNCTION_BT1886 1 -#define CM_TRANSFER_FUNCTION_GAMMA22 2 -#define CM_TRANSFER_FUNCTION_GAMMA28 3 -#define CM_TRANSFER_FUNCTION_ST240 4 -#define CM_TRANSFER_FUNCTION_EXT_LINEAR 5 -#define CM_TRANSFER_FUNCTION_LOG_100 6 -#define CM_TRANSFER_FUNCTION_LOG_316 7 -#define CM_TRANSFER_FUNCTION_XVYCC 8 -#define CM_TRANSFER_FUNCTION_SRGB 9 -#define CM_TRANSFER_FUNCTION_EXT_SRGB 10 -#define CM_TRANSFER_FUNCTION_ST2084_PQ 11 -#define CM_TRANSFER_FUNCTION_ST428 12 -#define CM_TRANSFER_FUNCTION_HLG 13 - -// sRGB constants -#define SRGB_POW 2.4 -#define SRGB_INV_POW (1.0 / SRGB_POW) -#define SRGB_D_CUT 0.04045 -#define SRGB_E_CUT 0.0031308 -#define SRGB_LO 12.92 -#define SRGB_HI 1.055 -#define SRGB_HI_ADD 0.055 - -// PQ constants -#define PQ_M1 0.1593017578125 -#define PQ_M2 78.84375 -#define PQ_INV_M1 (1.0 / PQ_M1) -#define PQ_INV_M2 (1.0 / PQ_M2) -#define PQ_C1 0.8359375 -#define PQ_C2 18.8515625 -#define PQ_C3 18.6875 - -// HLG constants -#define HLG_D_CUT (1.0 / 12.0) -#define HLG_E_CUT (sqrt(3.0) * pow(HLG_D_CUT, 0.5)) -#define HLG_A 0.17883277 -#define HLG_B 0.28466892 -#define HLG_C 0.55991073 - -#define SDR_MIN_LUMINANCE 0.2 -#define SDR_MAX_LUMINANCE 80.0 -#define HDR_MIN_LUMINANCE 0.005 -#define HDR_MAX_LUMINANCE 10000.0 -#define HLG_MAX_LUMINANCE 1000.0 - -// smoothing constant for the edge: more = blurrier, but smoother -#define M_PI 3.1415926535897932384626433832795 -#define M_E 2.718281828459045 -#define SMOOTHING_CONSTANT (M_PI / 5.34665792551) - -vec4 rounding(vec4 color) { - highp vec2 pixCoord = vec2(gl_FragCoord); - pixCoord -= topLeft + fullSize * 0.5; - pixCoord *= vec2(lessThan(pixCoord, vec2(0.0))) * -2.0 + 1.0; - pixCoord -= fullSize * 0.5 - radius; - pixCoord += vec2(1.0, 1.0) / fullSize; // center the pix dont make it top-left - - if (pixCoord.x + pixCoord.y > radius) { - float dist = pow(pow(pixCoord.x, roundingPower) + pow(pixCoord.y, roundingPower), 1.0/roundingPower); - - if (dist > radius + SMOOTHING_CONSTANT) - discard; - - float normalized = 1.0 - smoothstep(0.0, 1.0, (dist - radius + SMOOTHING_CONSTANT) / (SMOOTHING_CONSTANT * 2.0)); - - color *= normalized; - } - - return color; -} - -vec3 xy2xyz(vec2 xy) { - if (xy.y == 0.0) - return vec3(0.0, 0.0, 0.0); - - return vec3(xy.x / xy.y, 1.0, (1.0 - xy.x - xy.y) / xy.y); -} - -vec4 saturate(vec4 color, mat3 primaries, float saturation) { - if (saturation == 1.0) - return color; - vec3 brightness = vec3(primaries[1][0], primaries[1][1], primaries[1][2]); - float Y = dot(color.rgb, brightness); - return vec4(mix(vec3(Y), color.rgb, saturation), color[3]); -} - -vec3 toLinearRGB(vec3 color, int tf) { - if (tf == CM_TRANSFER_FUNCTION_EXT_LINEAR) - return color; - - bvec3 isLow; - vec3 lo; - vec3 hi; - switch (tf) { - case CM_TRANSFER_FUNCTION_ST2084_PQ: - vec3 E = pow(clamp(color.rgb, vec3(0.0), vec3(1.0)), vec3(PQ_INV_M2)); - return pow( - (max(E - PQ_C1, vec3(0.0))) / (PQ_C2 - PQ_C3 * E), - vec3(PQ_INV_M1) - ); - case CM_TRANSFER_FUNCTION_GAMMA22: - return pow(max(color.rgb, vec3(0.0)), vec3(2.2)); - case CM_TRANSFER_FUNCTION_GAMMA28: - return pow(max(color.rgb, vec3(0.0)), vec3(2.8)); - case CM_TRANSFER_FUNCTION_HLG: - isLow = lessThanEqual(color.rgb, vec3(HLG_D_CUT)); - lo = sqrt(3.0) * pow(color.rgb, vec3(0.5)); - hi = HLG_A * log(12.0 * color.rgb - HLG_B) + HLG_C; - return mix(hi, lo, isLow); - case CM_TRANSFER_FUNCTION_BT1886: - case CM_TRANSFER_FUNCTION_ST240: - case CM_TRANSFER_FUNCTION_LOG_100: - case CM_TRANSFER_FUNCTION_LOG_316: - case CM_TRANSFER_FUNCTION_XVYCC: - case CM_TRANSFER_FUNCTION_EXT_SRGB: - case CM_TRANSFER_FUNCTION_ST428: - - case CM_TRANSFER_FUNCTION_SRGB: - default: - isLow = lessThanEqual(color.rgb, vec3(SRGB_D_CUT)); - lo = color.rgb / SRGB_LO; - hi = pow((color.rgb + SRGB_HI_ADD) / SRGB_HI, vec3(SRGB_POW)); - return mix(hi, lo, isLow); - } -} - -vec4 toLinear(vec4 color, int tf) { - if (tf == CM_TRANSFER_FUNCTION_EXT_LINEAR) - return color; - - color.rgb /= max(color.a, 0.001); - color.rgb = toLinearRGB(color.rgb, tf); - color.rgb *= color.a; - return color; -} - -vec4 toNit(vec4 color, int tf) { - if (tf == CM_TRANSFER_FUNCTION_EXT_LINEAR) - color.rgb = color.rgb * SDR_MAX_LUMINANCE; - else { - - switch (tf) { - case CM_TRANSFER_FUNCTION_ST2084_PQ: - color.rgb = color.rgb * (HDR_MAX_LUMINANCE - HDR_MIN_LUMINANCE) + HDR_MIN_LUMINANCE; break; - case CM_TRANSFER_FUNCTION_HLG: - color.rgb = color.rgb * (HLG_MAX_LUMINANCE - HDR_MIN_LUMINANCE) + HDR_MIN_LUMINANCE; break; - case CM_TRANSFER_FUNCTION_GAMMA22: - case CM_TRANSFER_FUNCTION_GAMMA28: - case CM_TRANSFER_FUNCTION_BT1886: - case CM_TRANSFER_FUNCTION_ST240: - case CM_TRANSFER_FUNCTION_LOG_100: - case CM_TRANSFER_FUNCTION_LOG_316: - case CM_TRANSFER_FUNCTION_XVYCC: - case CM_TRANSFER_FUNCTION_EXT_SRGB: - case CM_TRANSFER_FUNCTION_ST428: - case CM_TRANSFER_FUNCTION_SRGB: - default: - color.rgb = color.rgb * (SDR_MAX_LUMINANCE - SDR_MIN_LUMINANCE) + SDR_MIN_LUMINANCE; break; - } - } - return color; -} - -vec3 fromLinearRGB(vec3 color, int tf) { - bvec3 isLow; - vec3 lo; - vec3 hi; - - switch (tf) { - case CM_TRANSFER_FUNCTION_EXT_LINEAR: - return color; - case CM_TRANSFER_FUNCTION_ST2084_PQ: - vec3 E = pow(clamp(color.rgb, vec3(0.0), vec3(1.0)), vec3(PQ_M1)); - return pow( - (vec3(PQ_C1) + PQ_C2 * E) / (vec3(1.0) + PQ_C3 * E), - vec3(PQ_M2) - ); - break; - case CM_TRANSFER_FUNCTION_GAMMA22: - return pow(max(color.rgb, vec3(0.0)), vec3(1.0 / 2.2)); - case CM_TRANSFER_FUNCTION_GAMMA28: - return pow(max(color.rgb, vec3(0.0)), vec3(1.0 / 2.8)); - case CM_TRANSFER_FUNCTION_HLG: - isLow = lessThanEqual(color.rgb, vec3(HLG_E_CUT)); - lo = pow(color.rgb / sqrt(3.0), vec3(2.0)); - hi = (pow(vec3(M_E), (color.rgb - HLG_C) / HLG_A) + HLG_B) / 12.0; - return mix(hi, lo, isLow); - case CM_TRANSFER_FUNCTION_BT1886: - case CM_TRANSFER_FUNCTION_ST240: - case CM_TRANSFER_FUNCTION_LOG_100: - case CM_TRANSFER_FUNCTION_LOG_316: - case CM_TRANSFER_FUNCTION_XVYCC: - case CM_TRANSFER_FUNCTION_EXT_SRGB: - case CM_TRANSFER_FUNCTION_ST428: - - case CM_TRANSFER_FUNCTION_SRGB: - default: - isLow = lessThanEqual(color.rgb, vec3(SRGB_E_CUT)); - lo = color.rgb * SRGB_LO; - hi = pow(color.rgb, vec3(SRGB_INV_POW)) * SRGB_HI - SRGB_HI_ADD; - return mix(hi, lo, isLow); - } -} - -vec4 fromLinear(vec4 color, int tf) { - if (tf == CM_TRANSFER_FUNCTION_EXT_LINEAR) - return color; - - color.rgb /= max(color.a, 0.001); - color.rgb = fromLinearRGB(color.rgb, tf); - color.rgb *= color.a; - return color; -} - -vec4 fromLinearNit(vec4 color, int tf) { - if (tf == CM_TRANSFER_FUNCTION_EXT_LINEAR) - color.rgb = color.rgb / SDR_MAX_LUMINANCE; - else { - color.rgb /= max(color.a, 0.001); - - switch (tf) { - case CM_TRANSFER_FUNCTION_ST2084_PQ: - color.rgb = (color.rgb - HDR_MIN_LUMINANCE) / (HDR_MAX_LUMINANCE - HDR_MIN_LUMINANCE); break; - case CM_TRANSFER_FUNCTION_HLG: - color.rgb = (color.rgb - HDR_MIN_LUMINANCE) / (HLG_MAX_LUMINANCE - HDR_MIN_LUMINANCE); break; - case CM_TRANSFER_FUNCTION_GAMMA22: - case CM_TRANSFER_FUNCTION_GAMMA28: - case CM_TRANSFER_FUNCTION_BT1886: - case CM_TRANSFER_FUNCTION_ST240: - case CM_TRANSFER_FUNCTION_LOG_100: - case CM_TRANSFER_FUNCTION_LOG_316: - case CM_TRANSFER_FUNCTION_XVYCC: - case CM_TRANSFER_FUNCTION_EXT_SRGB: - case CM_TRANSFER_FUNCTION_ST428: - case CM_TRANSFER_FUNCTION_SRGB: - default: - color.rgb = (color.rgb - SDR_MIN_LUMINANCE) / (SDR_MAX_LUMINANCE - SDR_MIN_LUMINANCE); break; - } - - color.rgb = fromLinearRGB(color.rgb, tf); - - color.rgb *= color.a; - } - return color; -} - -mat3 primaries2xyz(mat4x2 primaries) { - vec3 r = xy2xyz(primaries[0]); - vec3 g = xy2xyz(primaries[1]); - vec3 b = xy2xyz(primaries[2]); - vec3 w = xy2xyz(primaries[3]); - - mat3 invMat = inverse( - mat3( - r.x, r.y, r.z, - g.x, g.y, g.z, - b.x, b.y, b.z - ) - ); - - vec3 s = invMat * w; - - return mat3( - s.r * r.x, s.r * r.y, s.r * r.z, - s.g * g.x, s.g * g.y, s.g * g.z, - s.b * b.x, s.b * b.y, s.b * b.z - ); -} - -// const vec2 D65 = vec2(0.3127, 0.3290); -const mat3 Bradford = mat3( - 0.8951, 0.2664, -0.1614, - -0.7502, 1.7135, 0.0367, - 0.0389, -0.0685, 1.0296 -); -const mat3 BradfordInv = inverse(Bradford); - -mat3 adaptWhite(vec2 src, vec2 dst) { - if (src == dst) - return mat3( - 1.0, 0.0, 0.0, - 0.0, 1.0, 0.0, - 0.0, 0.0, 1.0 - ); - - vec3 srcXYZ = xy2xyz(src); - vec3 dstXYZ = xy2xyz(dst); - vec3 factors = (Bradford * dstXYZ) / (Bradford * srcXYZ); - - return BradfordInv * mat3( - factors.x, 0.0, 0.0, - 0.0, factors.y, 0.0, - 0.0, 0.0, factors.z - ) * Bradford; -} - -vec4 convertPrimaries(vec4 color, mat3 src, vec2 srcWhite, mat3 dst, vec2 dstWhite) { - mat3 convMat = inverse(dst) * adaptWhite(srcWhite, dstWhite) * src; - return vec4(convMat * color.rgb, color[3]); -} - -const mat3 BT2020toLMS = mat3( - 0.3592, 0.6976, -0.0358, - -0.1922, 1.1004, 0.0755, - 0.0070, 0.0749, 0.8434 -); -const mat3 LMStoBT2020 = inverse(BT2020toLMS); - -const mat3 ICtCpPQ = transpose(mat3( - 2048.0, 2048.0, 0.0, - 6610.0, -13613.0, 7003.0, - 17933.0, -17390.0, -543.0 -) / 4096.0); -const mat3 ICtCpPQInv = inverse(ICtCpPQ); - -const mat3 ICtCpHLG = transpose(mat3( - 2048.0, 2048.0, 0.0, - 3625.0, -7465.0, 3840.0, - 9500.0, -9212.0, -288.0 -) / 4096.0); -const mat3 ICtCpHLGInv = inverse(ICtCpHLG); - -vec4 tonemap(vec4 color, mat3 dstXYZ) { - if (maxLuminance < dstMaxLuminance * 1.01) - return vec4(clamp(color.rgb, vec3(0.0), vec3(dstMaxLuminance)), color[3]); - - mat3 toLMS = BT2020toLMS * dstXYZ; - mat3 fromLMS = inverse(dstXYZ) * LMStoBT2020; - - vec3 lms = fromLinear(vec4((toLMS * color.rgb) / HDR_MAX_LUMINANCE, 1.0), CM_TRANSFER_FUNCTION_ST2084_PQ).rgb; - vec3 ICtCp = ICtCpPQ * lms; - - float E = pow(clamp(ICtCp[0], 0.0, 1.0), PQ_INV_M2); - float luminance = pow( - (max(E - PQ_C1, 0.0)) / (PQ_C2 - PQ_C3 * E), - PQ_INV_M1 - ) * HDR_MAX_LUMINANCE; - - float srcScale = maxLuminance / dstRefLuminance; - float dstScale = dstMaxLuminance / dstRefLuminance; - - float minScale = min(srcScale, 1.5); - float dimming = 1.0 / clamp(minScale / dstScale, 1.0, minScale); - float refLuminance = dstRefLuminance * dimming; - - float low = min(luminance * dimming, refLuminance); - float highlight = clamp((luminance / dstRefLuminance - 1.0) / (srcScale - 1.0), 0.0, 1.0); - float high = log(highlight * (M_E - 1.0) + 1.0) * (dstMaxLuminance - refLuminance); - luminance = low + high; - - E = pow(clamp(ICtCp[0], 0.0, 1.0), PQ_M1); - ICtCp[0] = pow( - (PQ_C1 + PQ_C2 * E) / (1.0 + PQ_C3 * E), - PQ_M2 - ) / HDR_MAX_LUMINANCE; - return vec4(fromLMS * toLinear(vec4(ICtCpPQInv * ICtCp, 1.0), CM_TRANSFER_FUNCTION_ST2084_PQ).rgb * HDR_MAX_LUMINANCE, color[3]); -} - -layout(location = 0) out vec4 fragColor; -void main() { - vec4 pixColor; - if (texType == 1) - pixColor = vec4(texture(tex, v_texcoord).rgb, 1.0); -// else if (texType == 2) -// pixColor = texture(texture0, v_texcoord); - else // assume rgba - pixColor = texture(tex, v_texcoord); - - if (discardOpaque == 1 && pixColor[3] * alpha == 1.0) - discard; - - if (discardAlpha == 1 && pixColor[3] <= discardAlphaValue) - discard; - - pixColor.rgb /= max(pixColor.a, 0.001); - pixColor.rgb = toLinearRGB(pixColor.rgb, sourceTF); - mat3 srcxyz = primaries2xyz(sourcePrimaries); - mat3 dstxyz; - - if (sourcePrimaries == targetPrimaries) - dstxyz = srcxyz; - else { - dstxyz = primaries2xyz(targetPrimaries); - pixColor = convertPrimaries(pixColor, srcxyz, sourcePrimaries[3], dstxyz, targetPrimaries[3]); - } - - pixColor = toNit(pixColor, sourceTF); - pixColor.rgb *= pixColor.a; - pixColor = tonemap(pixColor, dstxyz); - - if (sourceTF == CM_TRANSFER_FUNCTION_SRGB && targetTF == CM_TRANSFER_FUNCTION_ST2084_PQ) - pixColor = saturate(pixColor, srcxyz, sdrSaturation); - - pixColor *= sdrBrightnessMultiplier; - pixColor = fromLinearNit(pixColor, targetTF); - - if (applyTint == 1) - pixColor = vec4(pixColor.rgb * tint.rgb, pixColor[3]); - - if (radius > 0.0) - pixColor = rounding(pixColor); - - fragColor = pixColor * alpha; -} -)#" diff --git a/src/render/shaders/Shadow.hpp b/src/render/shaders/Shadow.hpp deleted file mode 100644 index c04d4bf3..00000000 --- a/src/render/shaders/Shadow.hpp +++ /dev/null @@ -1,90 +0,0 @@ -#pragma once - -#include - -inline const std::string FRAGSHADOW = R"#( -precision highp float; -varying vec4 v_color; -varying vec2 v_texcoord; - -uniform vec2 topLeft; -uniform vec2 bottomRight; -uniform vec2 fullSize; -uniform float radius; -uniform float roundingPower; -uniform float range; -uniform float shadowPower; - -float pixAlphaRoundedDistance(float distanceToCorner) { - if (distanceToCorner > radius) { - return 0.0; - } - - if (distanceToCorner > radius - range) { - return pow((range - (distanceToCorner - radius + range)) / range, shadowPower); // i think? - } - - return 1.0; -} - -float modifiedLength(vec2 a) { - return pow(pow(abs(a.x),roundingPower)+pow(abs(a.y),roundingPower),1.0/roundingPower); -} - -void main() { - - vec4 pixColor = v_color; - float originalAlpha = pixColor[3]; - - bool done = false; - - vec2 pixCoord = fullSize * v_texcoord; - - // ok, now we check the distance to a border. - - if (pixCoord[0] < topLeft[0]) { - if (pixCoord[1] < topLeft[1]) { - // top left - pixColor[3] = pixColor[3] * pixAlphaRoundedDistance(modifiedLength(pixCoord - topLeft)); - done = true; - } else if (pixCoord[1] > bottomRight[1]) { - // bottom left - pixColor[3] = pixColor[3] * pixAlphaRoundedDistance(modifiedLength(pixCoord - vec2(topLeft[0], bottomRight[1]))); - done = true; - } - } else if (pixCoord[0] > bottomRight[0]) { - if (pixCoord[1] < topLeft[1]) { - // top right - pixColor[3] = pixColor[3] * pixAlphaRoundedDistance(modifiedLength(pixCoord - vec2(bottomRight[0], topLeft[1]))); - done = true; - } else if (pixCoord[1] > bottomRight[1]) { - // bottom right - pixColor[3] = pixColor[3] * pixAlphaRoundedDistance(modifiedLength(pixCoord - bottomRight)); - done = true; - } - } - - if (!done) { - // distance to all straight bb borders - float distanceT = pixCoord[1]; - float distanceB = fullSize[1] - pixCoord[1]; - float distanceL = pixCoord[0]; - float distanceR = fullSize[0] - pixCoord[0]; - - // get the smallest - float smallest = min(min(distanceT, distanceB), min(distanceL, distanceR)); - - if (smallest < range) { - pixColor[3] = pixColor[3] * pow((smallest / range), shadowPower); - } - } - - if (pixColor[3] == 0.0) { - discard; return; - } - - // premultiply - pixColor.rgb *= pixColor[3]; - - gl_FragColor = pixColor; -})#"; diff --git a/src/render/shaders/SharedValues.hpp b/src/render/shaders/SharedValues.hpp deleted file mode 100644 index 9a74c892..00000000 --- a/src/render/shaders/SharedValues.hpp +++ /dev/null @@ -1,3 +0,0 @@ -#pragma once - -constexpr float SHADER_ROUNDED_SMOOTHING_FACTOR = M_PI / 5.34665792551; \ No newline at end of file diff --git a/src/render/shaders/Textures.hpp b/src/render/shaders/Textures.hpp deleted file mode 100644 index 002c3459..00000000 --- a/src/render/shaders/Textures.hpp +++ /dev/null @@ -1,541 +0,0 @@ -#pragma once - -#include -#include -#include "SharedValues.hpp" - -inline static constexpr auto ROUNDED_SHADER_FUNC = [](const std::string colorVarName) -> std::string { - return R"#( - - // shoutout me: I fixed this shader being a bit pixelated while watching hentai - - highp vec2 pixCoord = vec2(gl_FragCoord); - pixCoord -= topLeft + fullSize * 0.5; - pixCoord *= vec2(lessThan(pixCoord, vec2(0.0))) * -2.0 + 1.0; - pixCoord -= fullSize * 0.5 - radius; - pixCoord += vec2(1.0, 1.0) / fullSize; // center the pix dont make it top-left - - // smoothing constant for the edge: more = blurrier, but smoother - const float SMOOTHING_CONSTANT = )#" + - std::format("{:.7f}", SHADER_ROUNDED_SMOOTHING_FACTOR) + R"#(; - - if (pixCoord.x + pixCoord.y > radius) { - - float dist = pow(pow(pixCoord.x, roundingPower) + pow(pixCoord.y, roundingPower), 1.0/roundingPower); - - if (dist > radius + SMOOTHING_CONSTANT) - discard; - - float normalized = 1.0 - smoothstep(0.0, 1.0, (dist - radius + SMOOTHING_CONSTANT) / (SMOOTHING_CONSTANT * 2.0)); - - )#" + - colorVarName + R"#( = )#" + colorVarName + R"#( * normalized; - } -)#"; -}; - -inline const std::string QUADVERTSRC = R"#( -uniform mat3 proj; -uniform vec4 color; -attribute vec2 pos; -attribute vec2 texcoord; -attribute vec2 texcoordMatte; -varying vec4 v_color; -varying vec2 v_texcoord; -varying vec2 v_texcoordMatte; - -void main() { - gl_Position = vec4(proj * vec3(pos, 1.0), 1.0); - v_color = color; - v_texcoord = texcoord; - v_texcoordMatte = texcoordMatte; -})#"; - -inline const std::string QUADFRAGSRC = R"#( -precision highp float; -varying vec4 v_color; - -uniform vec2 topLeft; -uniform vec2 fullSize; -uniform float radius; -uniform float roundingPower; - -void main() { - - vec4 pixColor = v_color; - - if (radius > 0.0) { - )#" + - ROUNDED_SHADER_FUNC("pixColor") + R"#( - } - - gl_FragColor = pixColor; -})#"; - -inline const std::string TEXVERTSRC = R"#( -uniform mat3 proj; -attribute vec2 pos; -attribute vec2 texcoord; -varying vec2 v_texcoord; - -void main() { - gl_Position = vec4(proj * vec3(pos, 1.0), 1.0); - v_texcoord = texcoord; -})#"; - -inline const std::string TEXVERTSRC320 = R"#(#version 320 es -uniform mat3 proj; -in vec2 pos; -in vec2 texcoord; -out vec2 v_texcoord; - -void main() { - gl_Position = vec4(proj * vec3(pos, 1.0), 1.0); - v_texcoord = texcoord; -})#"; - -inline const std::string TEXFRAGSRCCM = -#include "CM.frag" - ; - -inline const std::string TEXFRAGSRCRGBA = R"#( -precision highp float; -varying vec2 v_texcoord; // is in 0-1 -uniform sampler2D tex; -uniform float alpha; - -uniform vec2 topLeft; -uniform vec2 fullSize; -uniform float radius; -uniform float roundingPower; - -uniform int discardOpaque; -uniform int discardAlpha; -uniform float discardAlphaValue; - -uniform int applyTint; -uniform vec3 tint; - -void main() { - - vec4 pixColor = texture2D(tex, v_texcoord); - - if (discardOpaque == 1 && pixColor[3] * alpha == 1.0) - discard; - - if (discardAlpha == 1 && pixColor[3] <= discardAlphaValue) - discard; - - if (applyTint == 1) { - pixColor[0] = pixColor[0] * tint[0]; - pixColor[1] = pixColor[1] * tint[1]; - pixColor[2] = pixColor[2] * tint[2]; - } - - if (radius > 0.0) { - )#" + - ROUNDED_SHADER_FUNC("pixColor") + R"#( - } - - gl_FragColor = pixColor * alpha; -})#"; - -inline const std::string TEXFRAGSRCRGBAPASSTHRU = R"#( -precision highp float; -varying vec2 v_texcoord; // is in 0-1 -uniform sampler2D tex; - -void main() { - gl_FragColor = texture2D(tex, v_texcoord); -})#"; - -inline const std::string TEXFRAGSRCRGBAMATTE = R"#( -precision highp float; -varying vec2 v_texcoord; // is in 0-1 -uniform sampler2D tex; -uniform sampler2D texMatte; - -void main() { - gl_FragColor = texture2D(tex, v_texcoord) * texture2D(texMatte, v_texcoord)[0]; // I know it only uses R, but matte should be black/white anyways. -})#"; - -inline const std::string TEXFRAGSRCRGBX = R"#( -precision highp float; -varying vec2 v_texcoord; -uniform sampler2D tex; -uniform float alpha; - -uniform vec2 topLeft; -uniform vec2 fullSize; -uniform float radius; -uniform float roundingPower; - -uniform int discardOpaque; -uniform int discardAlpha; -uniform int discardAlphaValue; - -uniform int applyTint; -uniform vec3 tint; - -void main() { - - if (discardOpaque == 1 && alpha == 1.0) - discard; - - vec4 pixColor = vec4(texture2D(tex, v_texcoord).rgb, 1.0); - - if (applyTint == 1) { - pixColor[0] = pixColor[0] * tint[0]; - pixColor[1] = pixColor[1] * tint[1]; - pixColor[2] = pixColor[2] * tint[2]; - } - - if (radius > 0.0) { - )#" + - ROUNDED_SHADER_FUNC("pixColor") + R"#( - } - - gl_FragColor = pixColor * alpha; -})#"; - -inline const std::string FRAGBLUR1 = R"#( -#version 100 -precision highp float; -varying highp vec2 v_texcoord; // is in 0-1 -uniform sampler2D tex; - -uniform float radius; -uniform vec2 halfpixel; -uniform int passes; -uniform float vibrancy; -uniform float vibrancy_darkness; - -// see http://alienryderflex.com/hsp.html -const float Pr = 0.299; -const float Pg = 0.587; -const float Pb = 0.114; - -// Y is "v" ( brightness ). X is "s" ( saturation ) -// see https://www.desmos.com/3d/a88652b9a4 -// Determines if high brightness or high saturation is more important -const float a = 0.93; -const float b = 0.11; -const float c = 0.66; // Determines the smoothness of the transition of unboosted to boosted colors -// - -// http://www.flong.com/archive/texts/code/shapers_circ/ -float doubleCircleSigmoid(float x, float a) { - a = clamp(a, 0.0, 1.0); - - float y = .0; - if (x <= a) { - y = a - sqrt(a * a - x * x); - } else { - y = a + sqrt(pow(1. - a, 2.) - pow(x - 1., 2.)); - } - return y; -} - -vec3 rgb2hsl(vec3 col) { - float red = col.r; - float green = col.g; - float blue = col.b; - - float minc = min(col.r, min(col.g, col.b)); - float maxc = max(col.r, max(col.g, col.b)); - float delta = maxc - minc; - - float lum = (minc + maxc) * 0.5; - float sat = 0.0; - float hue = 0.0; - - if (lum > 0.0 && lum < 1.0) { - float mul = (lum < 0.5) ? (lum) : (1.0 - lum); - sat = delta / (mul * 2.0); - } - - if (delta > 0.0) { - vec3 maxcVec = vec3(maxc); - vec3 masks = vec3(equal(maxcVec, col)) * vec3(notEqual(maxcVec, vec3(green, blue, red))); - vec3 adds = vec3(0.0, 2.0, 4.0) + vec3(green - blue, blue - red, red - green) / delta; - - hue += dot(adds, masks); - hue /= 6.0; - - if (hue < 0.0) - hue += 1.0; - } - - return vec3(hue, sat, lum); -} - -vec3 hsl2rgb(vec3 col) { - const float onethird = 1.0 / 3.0; - const float twothird = 2.0 / 3.0; - const float rcpsixth = 6.0; - - float hue = col.x; - float sat = col.y; - float lum = col.z; - - vec3 xt = vec3(0.0); - - if (hue < onethird) { - xt.r = rcpsixth * (onethird - hue); - xt.g = rcpsixth * hue; - xt.b = 0.0; - } else if (hue < twothird) { - xt.r = 0.0; - xt.g = rcpsixth * (twothird - hue); - xt.b = rcpsixth * (hue - onethird); - } else - xt = vec3(rcpsixth * (hue - twothird), 0.0, rcpsixth * (1.0 - hue)); - - xt = min(xt, 1.0); - - float sat2 = 2.0 * sat; - float satinv = 1.0 - sat; - float luminv = 1.0 - lum; - float lum2m1 = (2.0 * lum) - 1.0; - vec3 ct = (sat2 * xt) + satinv; - - vec3 rgb; - if (lum >= 0.5) - rgb = (luminv * ct) + lum2m1; - else - rgb = lum * ct; - - return rgb; -} - -void main() { - vec2 uv = v_texcoord * 2.0; - - vec4 sum = texture2D(tex, uv) * 4.0; - sum += texture2D(tex, uv - halfpixel.xy * radius); - sum += texture2D(tex, uv + halfpixel.xy * radius); - sum += texture2D(tex, uv + vec2(halfpixel.x, -halfpixel.y) * radius); - sum += texture2D(tex, uv - vec2(halfpixel.x, -halfpixel.y) * radius); - - vec4 color = sum / 8.0; - - if (vibrancy == 0.0) { - gl_FragColor = color; - } else { - // Invert it so that it correctly maps to the config setting - float vibrancy_darkness1 = 1.0 - vibrancy_darkness; - - // Decrease the RGB components based on their perceived brightness, to prevent visually dark colors from overblowing the rest. - vec3 hsl = rgb2hsl(color.rgb); - // Calculate perceived brightness, as not boost visually dark colors like deep blue as much as equally saturated yellow - float perceivedBrightness = doubleCircleSigmoid(sqrt(color.r * color.r * Pr + color.g * color.g * Pg + color.b * color.b * Pb), 0.8 * vibrancy_darkness1); - - float b1 = b * vibrancy_darkness1; - float boostBase = hsl[1] > 0.0 ? smoothstep(b1 - c * 0.5, b1 + c * 0.5, 1.0 - (pow(1.0 - hsl[1] * cos(a), 2.0) + pow(1.0 - perceivedBrightness * sin(a), 2.0))) : 0.0; - - float saturation = clamp(hsl[1] + (boostBase * vibrancy) / float(passes), 0.0, 1.0); - - vec3 newColor = hsl2rgb(vec3(hsl[0], saturation, hsl[2])); - - gl_FragColor = vec4(newColor, color[3]); - } -} -)#"; - -inline const std::string FRAGBLUR2 = R"#( -#version 100 -precision highp float; -varying highp vec2 v_texcoord; // is in 0-1 -uniform sampler2D tex; - -uniform float radius; -uniform vec2 halfpixel; - -void main() { - vec2 uv = v_texcoord / 2.0; - - vec4 sum = texture2D(tex, uv + vec2(-halfpixel.x * 2.0, 0.0) * radius); - - sum += texture2D(tex, uv + vec2(-halfpixel.x, halfpixel.y) * radius) * 2.0; - sum += texture2D(tex, uv + vec2(0.0, halfpixel.y * 2.0) * radius); - sum += texture2D(tex, uv + vec2(halfpixel.x, halfpixel.y) * radius) * 2.0; - sum += texture2D(tex, uv + vec2(halfpixel.x * 2.0, 0.0) * radius); - sum += texture2D(tex, uv + vec2(halfpixel.x, -halfpixel.y) * radius) * 2.0; - sum += texture2D(tex, uv + vec2(0.0, -halfpixel.y * 2.0) * radius); - sum += texture2D(tex, uv + vec2(-halfpixel.x, -halfpixel.y) * radius) * 2.0; - - gl_FragColor = sum / 12.0; -} -)#"; - -inline const std::string FRAGBLURPREPARE = R"#( -precision highp float; -varying vec2 v_texcoord; // is in 0-1 -uniform sampler2D tex; - -uniform float contrast; -uniform float brightness; - -float gain(float x, float k) { - float a = 0.5 * pow(2.0 * ((x < 0.5) ? x : 1.0 - x), k); - return (x < 0.5) ? a : 1.0 - a; -} - -void main() { - vec4 pixColor = texture2D(tex, v_texcoord); - - // contrast - if (contrast != 1.0) { - pixColor.r = gain(pixColor.r, contrast); - pixColor.g = gain(pixColor.g, contrast); - pixColor.b = gain(pixColor.b, contrast); - } - - // brightness - if (brightness > 1.0) { - pixColor.rgb *= brightness; - } - - gl_FragColor = pixColor; -} -)#"; - -inline const std::string FRAGBLURFINISH = R"#( -precision highp float; -varying vec2 v_texcoord; // is in 0-1 -uniform sampler2D tex; - -uniform float noise; -uniform float brightness; - -float hash(vec2 p) { - vec3 p3 = fract(vec3(p.xyx) * 1689.1984); - p3 += dot(p3, p3.yzx + 33.33); - return fract((p3.x + p3.y) * p3.z); -} - -void main() { - vec4 pixColor = texture2D(tex, v_texcoord); - - // noise - float noiseHash = hash(v_texcoord); - float noiseAmount = (mod(noiseHash, 1.0) - 0.5); - pixColor.rgb += noiseAmount * noise; - - // brightness - if (brightness < 1.0) { - pixColor.rgb *= brightness; - } - - gl_FragColor = pixColor; -} -)#"; - -inline const std::string TEXFRAGSRCEXT = R"#( -#extension GL_OES_EGL_image_external : require - -precision highp float; -varying vec2 v_texcoord; -uniform samplerExternalOES texture0; -uniform float alpha; - -uniform vec2 topLeft; -uniform vec2 fullSize; -uniform float radius; -uniform float roundingPower; - -uniform int discardOpaque; -uniform int discardAlpha; -uniform int discardAlphaValue; - -uniform int applyTint; -uniform vec3 tint; - -void main() { - - vec4 pixColor = texture2D(texture0, v_texcoord); - - if (discardOpaque == 1 && pixColor[3] * alpha == 1.0) - discard; - - if (applyTint == 1) { - pixColor[0] = pixColor[0] * tint[0]; - pixColor[1] = pixColor[1] * tint[1]; - pixColor[2] = pixColor[2] * tint[2]; - } - - if (radius > 0.0) { - )#" + - ROUNDED_SHADER_FUNC("pixColor") + R"#( - } - - gl_FragColor = pixColor * alpha; -} -)#"; - -static const std::string FRAGGLITCH = R"#( -precision highp float; -varying vec2 v_texcoord; -uniform sampler2D tex; -uniform float time; // quirk: time is set to 0 at the beginning, should be around 10 when crash. -uniform float distort; -uniform vec2 screenSize; - -float rand(float co) { - return fract(sin(dot(vec2(co, co), vec2(12.9898, 78.233))) * 43758.5453); -} - -float rand(vec2 co) { - return fract(sin(dot(co, vec2(12.9898, 78.233))) * 43758.5453); -} - -float noise(vec2 point) { - vec2 floored = floor(point); - vec2 fractal = fract(point); - fractal = fractal * fractal * (3.0 - 2.0 * fractal); - - float mixed = mix( - mix(rand(floored), rand(floored + vec2(1.0, 0.0)), fractal.x), - mix(rand(floored + vec2(0.0,1.0)), rand(floored + vec2(1.0,1.0)), fractal.x), fractal.y); - return mixed * mixed; -} - -void main() { - float ABERR_OFFSET = 4.0 * (distort / 5.5) * time; - float TEAR_AMOUNT = 9000.0 * (1.0 - (distort / 5.5)); - float TEAR_BANDS = 108.0 / 2.0 * (distort / 5.5) * 2.0; - float MELT_AMOUNT = (distort * 8.0) / screenSize.y; - - float NOISE = abs(mod(noise(v_texcoord) * distort * time * 2.771, 1.0)) * time / 10.0; - if (time < 2.0) - NOISE = 0.0; - - float offset = (mod(rand(floor(v_texcoord.y * TEAR_BANDS)) * 318.772 * time, 20.0) - 10.0) / TEAR_AMOUNT; - - vec2 blockOffset = vec2(((abs(mod(rand(floor(v_texcoord.x * 37.162)) * 721.43, 100.0))) - 50.0) / 200000.0 * pow(time, 3.0), - ((abs(mod(rand(floor(v_texcoord.y * 45.882)) * 733.923, 100.0))) - 50.0) / 200000.0 * pow(time, 3.0)); - if (time < 3.0) - blockOffset = vec2(0,0); - - float meltSeed = abs(mod(rand(floor(v_texcoord.x * screenSize.x * 17.719)) * 281.882, 1.0)); - if (meltSeed < 0.8) { - meltSeed = 0.0; - } else { - meltSeed *= 25.0 * NOISE; - } - float meltAmount = MELT_AMOUNT * meltSeed; - - vec2 pixCoord = vec2(v_texcoord.x + offset + NOISE * 3.0 / screenSize.x + blockOffset.x, v_texcoord.y - meltAmount + 0.02 * NOISE / screenSize.x + NOISE * 3.0 / screenSize.y + blockOffset.y); - - vec4 pixColor = texture2D(tex, pixCoord); - vec4 pixColorLeft = texture2D(tex, pixCoord + vec2(ABERR_OFFSET / screenSize.x, 0)); - vec4 pixColorRight = texture2D(tex, pixCoord + vec2(-ABERR_OFFSET / screenSize.x, 0)); - - pixColor[0] = pixColorLeft[0]; - pixColor[2] = pixColorRight[2]; - - pixColor[0] += distort / 90.0; - - gl_FragColor = pixColor; -} -)#"; diff --git a/src/render/shaders/glsl/CM.frag b/src/render/shaders/glsl/CM.frag new file mode 100644 index 00000000..369e9cf0 --- /dev/null +++ b/src/render/shaders/glsl/CM.frag @@ -0,0 +1,55 @@ +#version 300 es +//#extension GL_OES_EGL_image_external : require +#extension GL_ARB_shading_language_include : enable + +precision highp float; +in vec2 v_texcoord; +uniform sampler2D tex; +//uniform samplerExternalOES texture0; + +uniform int texType; // eTextureType: 0 - rgba, 1 - rgbx, 2 - ext +// uniform int skipCM; +uniform int sourceTF; // eTransferFunction +uniform int targetTF; // eTransferFunction +uniform mat4x2 sourcePrimaries; +uniform mat4x2 targetPrimaries; + +uniform float alpha; + +uniform int discardOpaque; +uniform int discardAlpha; +uniform float discardAlphaValue; + +uniform int applyTint; +uniform vec3 tint; + +#include "rounding.glsl" +#include "CM.glsl" + +layout(location = 0) out vec4 fragColor; +void main() { + vec4 pixColor; + if (texType == 1) + pixColor = vec4(texture(tex, v_texcoord).rgb, 1.0); +// else if (texType == 2) +// pixColor = texture(texture0, v_texcoord); + else // assume rgba + pixColor = texture(tex, v_texcoord); + + if (discardOpaque == 1 && pixColor[3] * alpha == 1.0) + discard; + + if (discardAlpha == 1 && pixColor[3] <= discardAlphaValue) + discard; + + // this shader shouldn't be used when skipCM == 1 + pixColor = doColorManagement(pixColor, sourceTF, sourcePrimaries, targetTF, targetPrimaries); + + if (applyTint == 1) + pixColor = vec4(pixColor.rgb * tint.rgb, pixColor[3]); + + if (radius > 0.0) + pixColor = rounding(pixColor); + + fragColor = pixColor * alpha; +} diff --git a/src/render/shaders/glsl/CM.glsl b/src/render/shaders/glsl/CM.glsl new file mode 100644 index 00000000..4cf2745c --- /dev/null +++ b/src/render/shaders/glsl/CM.glsl @@ -0,0 +1,364 @@ +uniform float maxLuminance; +uniform float dstMaxLuminance; +uniform float dstRefLuminance; +uniform float sdrSaturation; +uniform float sdrBrightnessMultiplier; + +//enum eTransferFunction +#define CM_TRANSFER_FUNCTION_BT1886 1 +#define CM_TRANSFER_FUNCTION_GAMMA22 2 +#define CM_TRANSFER_FUNCTION_GAMMA28 3 +#define CM_TRANSFER_FUNCTION_ST240 4 +#define CM_TRANSFER_FUNCTION_EXT_LINEAR 5 +#define CM_TRANSFER_FUNCTION_LOG_100 6 +#define CM_TRANSFER_FUNCTION_LOG_316 7 +#define CM_TRANSFER_FUNCTION_XVYCC 8 +#define CM_TRANSFER_FUNCTION_SRGB 9 +#define CM_TRANSFER_FUNCTION_EXT_SRGB 10 +#define CM_TRANSFER_FUNCTION_ST2084_PQ 11 +#define CM_TRANSFER_FUNCTION_ST428 12 +#define CM_TRANSFER_FUNCTION_HLG 13 + +// sRGB constants +#define SRGB_POW 2.4 +#define SRGB_INV_POW (1.0 / SRGB_POW) +#define SRGB_D_CUT 0.04045 +#define SRGB_E_CUT 0.0031308 +#define SRGB_LO 12.92 +#define SRGB_HI 1.055 +#define SRGB_HI_ADD 0.055 + +// PQ constants +#define PQ_M1 0.1593017578125 +#define PQ_M2 78.84375 +#define PQ_INV_M1 (1.0 / PQ_M1) +#define PQ_INV_M2 (1.0 / PQ_M2) +#define PQ_C1 0.8359375 +#define PQ_C2 18.8515625 +#define PQ_C3 18.6875 + +// HLG constants +#define HLG_D_CUT (1.0 / 12.0) +#define HLG_E_CUT (sqrt(3.0) * pow(HLG_D_CUT, 0.5)) +#define HLG_A 0.17883277 +#define HLG_B 0.28466892 +#define HLG_C 0.55991073 + +#define SDR_MIN_LUMINANCE 0.2 +#define SDR_MAX_LUMINANCE 80.0 +#define HDR_MIN_LUMINANCE 0.005 +#define HDR_MAX_LUMINANCE 10000.0 +#define HLG_MAX_LUMINANCE 1000.0 + +#define M_E 2.718281828459045 + +vec3 xy2xyz(vec2 xy) { + if (xy.y == 0.0) + return vec3(0.0, 0.0, 0.0); + + return vec3(xy.x / xy.y, 1.0, (1.0 - xy.x - xy.y) / xy.y); +} + +vec4 saturate(vec4 color, mat3 primaries, float saturation) { + if (saturation == 1.0) + return color; + vec3 brightness = vec3(primaries[1][0], primaries[1][1], primaries[1][2]); + float Y = dot(color.rgb, brightness); + return vec4(mix(vec3(Y), color.rgb, saturation), color[3]); +} + +vec3 toLinearRGB(vec3 color, int tf) { + if (tf == CM_TRANSFER_FUNCTION_EXT_LINEAR) + return color; + + bvec3 isLow; + vec3 lo; + vec3 hi; + switch (tf) { + case CM_TRANSFER_FUNCTION_ST2084_PQ: + vec3 E = pow(clamp(color.rgb, vec3(0.0), vec3(1.0)), vec3(PQ_INV_M2)); + return pow( + (max(E - PQ_C1, vec3(0.0))) / (PQ_C2 - PQ_C3 * E), + vec3(PQ_INV_M1) + ); + case CM_TRANSFER_FUNCTION_GAMMA22: + return pow(max(color.rgb, vec3(0.0)), vec3(2.2)); + case CM_TRANSFER_FUNCTION_GAMMA28: + return pow(max(color.rgb, vec3(0.0)), vec3(2.8)); + case CM_TRANSFER_FUNCTION_HLG: + isLow = lessThanEqual(color.rgb, vec3(HLG_D_CUT)); + lo = sqrt(3.0) * pow(color.rgb, vec3(0.5)); + hi = HLG_A * log(12.0 * color.rgb - HLG_B) + HLG_C; + return mix(hi, lo, isLow); + case CM_TRANSFER_FUNCTION_BT1886: + case CM_TRANSFER_FUNCTION_ST240: + case CM_TRANSFER_FUNCTION_LOG_100: + case CM_TRANSFER_FUNCTION_LOG_316: + case CM_TRANSFER_FUNCTION_XVYCC: + case CM_TRANSFER_FUNCTION_EXT_SRGB: + case CM_TRANSFER_FUNCTION_ST428: + + case CM_TRANSFER_FUNCTION_SRGB: + default: + isLow = lessThanEqual(color.rgb, vec3(SRGB_D_CUT)); + lo = color.rgb / SRGB_LO; + hi = pow((color.rgb + SRGB_HI_ADD) / SRGB_HI, vec3(SRGB_POW)); + return mix(hi, lo, isLow); + } +} + +vec4 toLinear(vec4 color, int tf) { + if (tf == CM_TRANSFER_FUNCTION_EXT_LINEAR) + return color; + + color.rgb /= max(color.a, 0.001); + color.rgb = toLinearRGB(color.rgb, tf); + color.rgb *= color.a; + return color; +} + +vec4 toNit(vec4 color, int tf) { + if (tf == CM_TRANSFER_FUNCTION_EXT_LINEAR) + color.rgb = color.rgb * SDR_MAX_LUMINANCE; + else { + + switch (tf) { + case CM_TRANSFER_FUNCTION_ST2084_PQ: + color.rgb = color.rgb * (HDR_MAX_LUMINANCE - HDR_MIN_LUMINANCE) + HDR_MIN_LUMINANCE; break; + case CM_TRANSFER_FUNCTION_HLG: + color.rgb = color.rgb * (HLG_MAX_LUMINANCE - HDR_MIN_LUMINANCE) + HDR_MIN_LUMINANCE; break; + case CM_TRANSFER_FUNCTION_GAMMA22: + case CM_TRANSFER_FUNCTION_GAMMA28: + case CM_TRANSFER_FUNCTION_BT1886: + case CM_TRANSFER_FUNCTION_ST240: + case CM_TRANSFER_FUNCTION_LOG_100: + case CM_TRANSFER_FUNCTION_LOG_316: + case CM_TRANSFER_FUNCTION_XVYCC: + case CM_TRANSFER_FUNCTION_EXT_SRGB: + case CM_TRANSFER_FUNCTION_ST428: + case CM_TRANSFER_FUNCTION_SRGB: + default: + color.rgb = color.rgb * (SDR_MAX_LUMINANCE - SDR_MIN_LUMINANCE) + SDR_MIN_LUMINANCE; break; + } + } + return color; +} + +vec3 fromLinearRGB(vec3 color, int tf) { + bvec3 isLow; + vec3 lo; + vec3 hi; + + switch (tf) { + case CM_TRANSFER_FUNCTION_EXT_LINEAR: + return color; + case CM_TRANSFER_FUNCTION_ST2084_PQ: + vec3 E = pow(clamp(color.rgb, vec3(0.0), vec3(1.0)), vec3(PQ_M1)); + return pow( + (vec3(PQ_C1) + PQ_C2 * E) / (vec3(1.0) + PQ_C3 * E), + vec3(PQ_M2) + ); + break; + case CM_TRANSFER_FUNCTION_GAMMA22: + return pow(max(color.rgb, vec3(0.0)), vec3(1.0 / 2.2)); + case CM_TRANSFER_FUNCTION_GAMMA28: + return pow(max(color.rgb, vec3(0.0)), vec3(1.0 / 2.8)); + case CM_TRANSFER_FUNCTION_HLG: + isLow = lessThanEqual(color.rgb, vec3(HLG_E_CUT)); + lo = pow(color.rgb / sqrt(3.0), vec3(2.0)); + hi = (pow(vec3(M_E), (color.rgb - HLG_C) / HLG_A) + HLG_B) / 12.0; + return mix(hi, lo, isLow); + case CM_TRANSFER_FUNCTION_BT1886: + case CM_TRANSFER_FUNCTION_ST240: + case CM_TRANSFER_FUNCTION_LOG_100: + case CM_TRANSFER_FUNCTION_LOG_316: + case CM_TRANSFER_FUNCTION_XVYCC: + case CM_TRANSFER_FUNCTION_EXT_SRGB: + case CM_TRANSFER_FUNCTION_ST428: + + case CM_TRANSFER_FUNCTION_SRGB: + default: + isLow = lessThanEqual(color.rgb, vec3(SRGB_E_CUT)); + lo = color.rgb * SRGB_LO; + hi = pow(color.rgb, vec3(SRGB_INV_POW)) * SRGB_HI - SRGB_HI_ADD; + return mix(hi, lo, isLow); + } +} + +vec4 fromLinear(vec4 color, int tf) { + if (tf == CM_TRANSFER_FUNCTION_EXT_LINEAR) + return color; + + color.rgb /= max(color.a, 0.001); + color.rgb = fromLinearRGB(color.rgb, tf); + color.rgb *= color.a; + return color; +} + +vec4 fromLinearNit(vec4 color, int tf) { + if (tf == CM_TRANSFER_FUNCTION_EXT_LINEAR) + color.rgb = color.rgb / SDR_MAX_LUMINANCE; + else { + color.rgb /= max(color.a, 0.001); + + switch (tf) { + case CM_TRANSFER_FUNCTION_ST2084_PQ: + color.rgb = (color.rgb - HDR_MIN_LUMINANCE) / (HDR_MAX_LUMINANCE - HDR_MIN_LUMINANCE); break; + case CM_TRANSFER_FUNCTION_HLG: + color.rgb = (color.rgb - HDR_MIN_LUMINANCE) / (HLG_MAX_LUMINANCE - HDR_MIN_LUMINANCE); break; + case CM_TRANSFER_FUNCTION_GAMMA22: + case CM_TRANSFER_FUNCTION_GAMMA28: + case CM_TRANSFER_FUNCTION_BT1886: + case CM_TRANSFER_FUNCTION_ST240: + case CM_TRANSFER_FUNCTION_LOG_100: + case CM_TRANSFER_FUNCTION_LOG_316: + case CM_TRANSFER_FUNCTION_XVYCC: + case CM_TRANSFER_FUNCTION_EXT_SRGB: + case CM_TRANSFER_FUNCTION_ST428: + case CM_TRANSFER_FUNCTION_SRGB: + default: + color.rgb = (color.rgb - SDR_MIN_LUMINANCE) / (SDR_MAX_LUMINANCE - SDR_MIN_LUMINANCE); break; + } + + color.rgb = fromLinearRGB(color.rgb, tf); + + color.rgb *= color.a; + } + return color; +} + +mat3 primaries2xyz(mat4x2 primaries) { + vec3 r = xy2xyz(primaries[0]); + vec3 g = xy2xyz(primaries[1]); + vec3 b = xy2xyz(primaries[2]); + vec3 w = xy2xyz(primaries[3]); + + mat3 invMat = inverse( + mat3( + r.x, r.y, r.z, + g.x, g.y, g.z, + b.x, b.y, b.z + ) + ); + + vec3 s = invMat * w; + + return mat3( + s.r * r.x, s.r * r.y, s.r * r.z, + s.g * g.x, s.g * g.y, s.g * g.z, + s.b * b.x, s.b * b.y, s.b * b.z + ); +} + +// const vec2 D65 = vec2(0.3127, 0.3290); +const mat3 Bradford = mat3( + 0.8951, 0.2664, -0.1614, + -0.7502, 1.7135, 0.0367, + 0.0389, -0.0685, 1.0296 +); +const mat3 BradfordInv = inverse(Bradford); + +mat3 adaptWhite(vec2 src, vec2 dst) { + if (src == dst) + return mat3( + 1.0, 0.0, 0.0, + 0.0, 1.0, 0.0, + 0.0, 0.0, 1.0 + ); + + vec3 srcXYZ = xy2xyz(src); + vec3 dstXYZ = xy2xyz(dst); + vec3 factors = (Bradford * dstXYZ) / (Bradford * srcXYZ); + + return BradfordInv * mat3( + factors.x, 0.0, 0.0, + 0.0, factors.y, 0.0, + 0.0, 0.0, factors.z + ) * Bradford; +} + +vec4 convertPrimaries(vec4 color, mat3 src, vec2 srcWhite, mat3 dst, vec2 dstWhite) { + mat3 convMat = inverse(dst) * adaptWhite(srcWhite, dstWhite) * src; + return vec4(convMat * color.rgb, color[3]); +} + +const mat3 BT2020toLMS = mat3( + 0.3592, 0.6976, -0.0358, + -0.1922, 1.1004, 0.0755, + 0.0070, 0.0749, 0.8434 +); +const mat3 LMStoBT2020 = inverse(BT2020toLMS); + +const mat3 ICtCpPQ = transpose(mat3( + 2048.0, 2048.0, 0.0, + 6610.0, -13613.0, 7003.0, + 17933.0, -17390.0, -543.0 +) / 4096.0); +const mat3 ICtCpPQInv = inverse(ICtCpPQ); + +const mat3 ICtCpHLG = transpose(mat3( + 2048.0, 2048.0, 0.0, + 3625.0, -7465.0, 3840.0, + 9500.0, -9212.0, -288.0 +) / 4096.0); +const mat3 ICtCpHLGInv = inverse(ICtCpHLG); + +vec4 tonemap(vec4 color, mat3 dstXYZ) { + if (maxLuminance < dstMaxLuminance * 1.01) + return vec4(clamp(color.rgb, vec3(0.0), vec3(dstMaxLuminance)), color[3]); + + mat3 toLMS = BT2020toLMS * dstXYZ; + mat3 fromLMS = inverse(dstXYZ) * LMStoBT2020; + + vec3 lms = fromLinear(vec4((toLMS * color.rgb) / HDR_MAX_LUMINANCE, 1.0), CM_TRANSFER_FUNCTION_ST2084_PQ).rgb; + vec3 ICtCp = ICtCpPQ * lms; + + float E = pow(clamp(ICtCp[0], 0.0, 1.0), PQ_INV_M2); + float luminance = pow( + (max(E - PQ_C1, 0.0)) / (PQ_C2 - PQ_C3 * E), + PQ_INV_M1 + ) * HDR_MAX_LUMINANCE; + + float srcScale = maxLuminance / dstRefLuminance; + float dstScale = dstMaxLuminance / dstRefLuminance; + + float minScale = min(srcScale, 1.5); + float dimming = 1.0 / clamp(minScale / dstScale, 1.0, minScale); + float refLuminance = dstRefLuminance * dimming; + + float low = min(luminance * dimming, refLuminance); + float highlight = clamp((luminance / dstRefLuminance - 1.0) / (srcScale - 1.0), 0.0, 1.0); + float high = log(highlight * (M_E - 1.0) + 1.0) * (dstMaxLuminance - refLuminance); + luminance = low + high; + + E = pow(clamp(ICtCp[0], 0.0, 1.0), PQ_M1); + ICtCp[0] = pow( + (PQ_C1 + PQ_C2 * E) / (1.0 + PQ_C3 * E), + PQ_M2 + ) / HDR_MAX_LUMINANCE; + return vec4(fromLMS * toLinear(vec4(ICtCpPQInv * ICtCp, 1.0), CM_TRANSFER_FUNCTION_ST2084_PQ).rgb * HDR_MAX_LUMINANCE, color[3]); +} + +vec4 doColorManagement(vec4 pixColor, int srcTF, mat4x2 srcPrimaries, int dstTF, mat4x2 dstPrimaries) { + pixColor.rgb /= max(pixColor.a, 0.001); + pixColor.rgb = toLinearRGB(pixColor.rgb, srcTF); + mat3 srcxyz = primaries2xyz(srcPrimaries); + mat3 dstxyz; + if (srcPrimaries == dstPrimaries) + dstxyz = srcxyz; + else { + dstxyz = primaries2xyz(dstPrimaries); + pixColor = convertPrimaries(pixColor, srcxyz, srcPrimaries[3], dstxyz, dstPrimaries[3]); + } + pixColor = toNit(pixColor, srcTF); + pixColor.rgb *= pixColor.a; + pixColor = tonemap(pixColor, dstxyz); + pixColor = fromLinearNit(pixColor, dstTF); + if (srcTF == CM_TRANSFER_FUNCTION_SRGB && dstTF == CM_TRANSFER_FUNCTION_ST2084_PQ) { + pixColor = saturate(pixColor, srcxyz, sdrSaturation); + pixColor.rgb /= pixColor.a; + pixColor.rgb *= sdrBrightnessMultiplier; + pixColor.rgb *= pixColor.a; + } + return pixColor; +} diff --git a/src/render/shaders/glsl/blur1.frag b/src/render/shaders/glsl/blur1.frag new file mode 100644 index 00000000..dbabd610 --- /dev/null +++ b/src/render/shaders/glsl/blur1.frag @@ -0,0 +1,141 @@ +#version 100 +precision highp float; +varying highp vec2 v_texcoord; // is in 0-1 +uniform sampler2D tex; + +uniform float radius; +uniform vec2 halfpixel; +uniform int passes; +uniform float vibrancy; +uniform float vibrancy_darkness; + +// see http://alienryderflex.com/hsp.html +const float Pr = 0.299; +const float Pg = 0.587; +const float Pb = 0.114; + +// Y is "v" ( brightness ). X is "s" ( saturation ) +// see https://www.desmos.com/3d/a88652b9a4 +// Determines if high brightness or high saturation is more important +const float a = 0.93; +const float b = 0.11; +const float c = 0.66; // Determines the smoothness of the transition of unboosted to boosted colors +// + +// http://www.flong.com/archive/texts/code/shapers_circ/ +float doubleCircleSigmoid(float x, float a) { + a = clamp(a, 0.0, 1.0); + + float y = .0; + if (x <= a) { + y = a - sqrt(a * a - x * x); + } else { + y = a + sqrt(pow(1. - a, 2.) - pow(x - 1., 2.)); + } + return y; +} + +vec3 rgb2hsl(vec3 col) { + float red = col.r; + float green = col.g; + float blue = col.b; + + float minc = min(col.r, min(col.g, col.b)); + float maxc = max(col.r, max(col.g, col.b)); + float delta = maxc - minc; + + float lum = (minc + maxc) * 0.5; + float sat = 0.0; + float hue = 0.0; + + if (lum > 0.0 && lum < 1.0) { + float mul = (lum < 0.5) ? (lum) : (1.0 - lum); + sat = delta / (mul * 2.0); + } + + if (delta > 0.0) { + vec3 maxcVec = vec3(maxc); + vec3 masks = vec3(equal(maxcVec, col)) * vec3(notEqual(maxcVec, vec3(green, blue, red))); + vec3 adds = vec3(0.0, 2.0, 4.0) + vec3(green - blue, blue - red, red - green) / delta; + + hue += dot(adds, masks); + hue /= 6.0; + + if (hue < 0.0) + hue += 1.0; + } + + return vec3(hue, sat, lum); +} + +vec3 hsl2rgb(vec3 col) { + const float onethird = 1.0 / 3.0; + const float twothird = 2.0 / 3.0; + const float rcpsixth = 6.0; + + float hue = col.x; + float sat = col.y; + float lum = col.z; + + vec3 xt = vec3(0.0); + + if (hue < onethird) { + xt.r = rcpsixth * (onethird - hue); + xt.g = rcpsixth * hue; + xt.b = 0.0; + } else if (hue < twothird) { + xt.r = 0.0; + xt.g = rcpsixth * (twothird - hue); + xt.b = rcpsixth * (hue - onethird); + } else + xt = vec3(rcpsixth * (hue - twothird), 0.0, rcpsixth * (1.0 - hue)); + + xt = min(xt, 1.0); + + float sat2 = 2.0 * sat; + float satinv = 1.0 - sat; + float luminv = 1.0 - lum; + float lum2m1 = (2.0 * lum) - 1.0; + vec3 ct = (sat2 * xt) + satinv; + + vec3 rgb; + if (lum >= 0.5) + rgb = (luminv * ct) + lum2m1; + else + rgb = lum * ct; + + return rgb; +} + +void main() { + vec2 uv = v_texcoord * 2.0; + + vec4 sum = texture2D(tex, uv) * 4.0; + sum += texture2D(tex, uv - halfpixel.xy * radius); + sum += texture2D(tex, uv + halfpixel.xy * radius); + sum += texture2D(tex, uv + vec2(halfpixel.x, -halfpixel.y) * radius); + sum += texture2D(tex, uv - vec2(halfpixel.x, -halfpixel.y) * radius); + + vec4 color = sum / 8.0; + + if (vibrancy == 0.0) { + gl_FragColor = color; + } else { + // Invert it so that it correctly maps to the config setting + float vibrancy_darkness1 = 1.0 - vibrancy_darkness; + + // Decrease the RGB components based on their perceived brightness, to prevent visually dark colors from overblowing the rest. + vec3 hsl = rgb2hsl(color.rgb); + // Calculate perceived brightness, as not boost visually dark colors like deep blue as much as equally saturated yellow + float perceivedBrightness = doubleCircleSigmoid(sqrt(color.r * color.r * Pr + color.g * color.g * Pg + color.b * color.b * Pb), 0.8 * vibrancy_darkness1); + + float b1 = b * vibrancy_darkness1; + float boostBase = hsl[1] > 0.0 ? smoothstep(b1 - c * 0.5, b1 + c * 0.5, 1.0 - (pow(1.0 - hsl[1] * cos(a), 2.0) + pow(1.0 - perceivedBrightness * sin(a), 2.0))) : 0.0; + + float saturation = clamp(hsl[1] + (boostBase * vibrancy) / float(passes), 0.0, 1.0); + + vec3 newColor = hsl2rgb(vec3(hsl[0], saturation, hsl[2])); + + gl_FragColor = vec4(newColor, color[3]); + } +} diff --git a/src/render/shaders/glsl/blur2.frag b/src/render/shaders/glsl/blur2.frag new file mode 100644 index 00000000..dfd8eeab --- /dev/null +++ b/src/render/shaders/glsl/blur2.frag @@ -0,0 +1,23 @@ +#version 100 +precision highp float; +varying highp vec2 v_texcoord; // is in 0-1 +uniform sampler2D tex; + +uniform float radius; +uniform vec2 halfpixel; + +void main() { + vec2 uv = v_texcoord / 2.0; + + vec4 sum = texture2D(tex, uv + vec2(-halfpixel.x * 2.0, 0.0) * radius); + + sum += texture2D(tex, uv + vec2(-halfpixel.x, halfpixel.y) * radius) * 2.0; + sum += texture2D(tex, uv + vec2(0.0, halfpixel.y * 2.0) * radius); + sum += texture2D(tex, uv + vec2(halfpixel.x, halfpixel.y) * radius) * 2.0; + sum += texture2D(tex, uv + vec2(halfpixel.x * 2.0, 0.0) * radius); + sum += texture2D(tex, uv + vec2(halfpixel.x, -halfpixel.y) * radius) * 2.0; + sum += texture2D(tex, uv + vec2(0.0, -halfpixel.y * 2.0) * radius); + sum += texture2D(tex, uv + vec2(-halfpixel.x, -halfpixel.y) * radius) * 2.0; + + gl_FragColor = sum / 12.0; +} diff --git a/src/render/shaders/glsl/blurfinish.frag b/src/render/shaders/glsl/blurfinish.frag new file mode 100644 index 00000000..6ab48337 --- /dev/null +++ b/src/render/shaders/glsl/blurfinish.frag @@ -0,0 +1,32 @@ +#version 300 es +#extension GL_ARB_shading_language_include : enable + +precision highp float; +in vec2 v_texcoord; // is in 0-1 +uniform sampler2D tex; + +uniform float noise; +uniform float brightness; + +float hash(vec2 p) { + vec3 p3 = fract(vec3(p.xyx) * 1689.1984); + p3 += dot(p3, p3.yzx + 33.33); + return fract((p3.x + p3.y) * p3.z); +} + +layout(location = 0) out vec4 fragColor; +void main() { + vec4 pixColor = texture(tex, v_texcoord); + + // noise + float noiseHash = hash(v_texcoord); + float noiseAmount = (mod(noiseHash, 1.0) - 0.5); + pixColor.rgb += noiseAmount * noise; + + // brightness + if (brightness < 1.0) { + pixColor.rgb *= brightness; + } + + fragColor = pixColor; +} diff --git a/src/render/shaders/glsl/blurfinish_legacy.frag b/src/render/shaders/glsl/blurfinish_legacy.frag new file mode 100644 index 00000000..2a7ebca1 --- /dev/null +++ b/src/render/shaders/glsl/blurfinish_legacy.frag @@ -0,0 +1,28 @@ +precision highp float; +varying vec2 v_texcoord; // is in 0-1 +uniform sampler2D tex; + +uniform float noise; +uniform float brightness; + +float hash(vec2 p) { + vec3 p3 = fract(vec3(p.xyx) * 1689.1984); + p3 += dot(p3, p3.yzx + 33.33); + return fract((p3.x + p3.y) * p3.z); +} + +void main() { + vec4 pixColor = texture2D(tex, v_texcoord); + + // noise + float noiseHash = hash(v_texcoord); + float noiseAmount = (mod(noiseHash, 1.0) - 0.5); + pixColor.rgb += noiseAmount * noise; + + // brightness + if (brightness < 1.0) { + pixColor.rgb *= brightness; + } + + gl_FragColor = pixColor; +} diff --git a/src/render/shaders/glsl/blurprepare.frag b/src/render/shaders/glsl/blurprepare.frag new file mode 100644 index 00000000..0be2d53a --- /dev/null +++ b/src/render/shaders/glsl/blurprepare.frag @@ -0,0 +1,58 @@ +#version 300 es +#extension GL_ARB_shading_language_include : enable + +precision highp float; +in vec2 v_texcoord; // is in 0-1 +uniform sampler2D tex; + +uniform float contrast; +uniform float brightness; + +uniform int skipCM; +uniform int sourceTF; // eTransferFunction +uniform int targetTF; // eTransferFunction +uniform mat4x2 sourcePrimaries; +uniform mat4x2 targetPrimaries; + +#include "CM.glsl" + +float gain(float x, float k) { + float a = 0.5 * pow(2.0 * ((x < 0.5) ? x : 1.0 - x), k); + return (x < 0.5) ? a : 1.0 - a; +} + +layout(location = 0) out vec4 fragColor; +void main() { + vec4 pixColor = texture(tex, v_texcoord); + + if (skipCM == 0) { + if (sourceTF == CM_TRANSFER_FUNCTION_ST2084_PQ) { + pixColor.rgb /= sdrBrightnessMultiplier; + } + pixColor.rgb = toLinearRGB(pixColor.rgb, sourceTF); + mat3 srcxyz = primaries2xyz(sourcePrimaries); + mat3 dstxyz; + if (sourcePrimaries == targetPrimaries) + dstxyz = srcxyz; + else { + dstxyz = primaries2xyz(targetPrimaries); + pixColor = convertPrimaries(pixColor, srcxyz, sourcePrimaries[3], dstxyz, targetPrimaries[3]); + } + pixColor = toNit(pixColor, sourceTF); + pixColor = fromLinearNit(pixColor, targetTF); + } + + // contrast + if (contrast != 1.0) { + pixColor.r = gain(pixColor.r, contrast); + pixColor.g = gain(pixColor.g, contrast); + pixColor.b = gain(pixColor.b, contrast); + } + + // brightness + if (brightness > 1.0) { + pixColor.rgb *= brightness; + } + + fragColor = pixColor; +} diff --git a/src/render/shaders/glsl/blurprepare_legacy.frag b/src/render/shaders/glsl/blurprepare_legacy.frag new file mode 100644 index 00000000..2df933d5 --- /dev/null +++ b/src/render/shaders/glsl/blurprepare_legacy.frag @@ -0,0 +1,29 @@ +precision highp float; +varying vec2 v_texcoord; // is in 0-1 +uniform sampler2D tex; + +uniform float contrast; +uniform float brightness; + +float gain(float x, float k) { + float a = 0.5 * pow(2.0 * ((x < 0.5) ? x : 1.0 - x), k); + return (x < 0.5) ? a : 1.0 - a; +} + +void main() { + vec4 pixColor = texture2D(tex, v_texcoord); + + // contrast + if (contrast != 1.0) { + pixColor.r = gain(pixColor.r, contrast); + pixColor.g = gain(pixColor.g, contrast); + pixColor.b = gain(pixColor.b, contrast); + } + + // brightness + if (brightness > 1.0) { + pixColor.rgb *= brightness; + } + + gl_FragColor = pixColor; +} diff --git a/src/render/shaders/glsl/border.frag b/src/render/shaders/glsl/border.frag new file mode 100644 index 00000000..68541e14 --- /dev/null +++ b/src/render/shaders/glsl/border.frag @@ -0,0 +1,183 @@ +#version 300 es +#extension GL_ARB_shading_language_include : enable + +precision highp float; +in vec2 v_texcoord; + +uniform int skipCM; +uniform int sourceTF; // eTransferFunction +uniform int targetTF; // eTransferFunction +uniform mat4x2 sourcePrimaries; +uniform mat4x2 targetPrimaries; + +uniform vec2 fullSizeUntransformed; +uniform float radiusOuter; +uniform float thick; + +// Gradients are in OkLabA!!!! {l, a, b, alpha} +uniform vec4 gradient[10]; +uniform vec4 gradient2[10]; +uniform int gradientLength; +uniform int gradient2Length; +uniform float angle; +uniform float angle2; +uniform float gradientLerp; +uniform float alpha; + +#include "rounding.glsl" +#include "CM.glsl" + +vec4 okLabAToSrgb(vec4 lab) { + float l = pow(lab[0] + lab[1] * 0.3963377774 + lab[2] * 0.2158037573, 3.0); + float m = pow(lab[0] + lab[1] * (-0.1055613458) + lab[2] * (-0.0638541728), 3.0); + float s = pow(lab[0] + lab[1] * (-0.0894841775) + lab[2] * (-1.2914855480), 3.0); + + return vec4(fromLinearRGB( + vec3( + l * 4.0767416621 + m * -3.3077115913 + s * 0.2309699292, + l * (-1.2684380046) + m * 2.6097574011 + s * (-0.3413193965), + l * (-0.0041960863) + m * (-0.7034186147) + s * 1.7076147010 + ), CM_TRANSFER_FUNCTION_SRGB + ), lab[3]); +} + +vec4 getOkColorForCoordArray1(vec2 normalizedCoord) { + if (gradientLength < 2) + return gradient[0]; + + float finalAng = 0.0; + + if (angle > 4.71 /* 270 deg */) { + normalizedCoord[1] = 1.0 - normalizedCoord[1]; + finalAng = 6.28 - angle; + } else if (angle > 3.14 /* 180 deg */) { + normalizedCoord[0] = 1.0 - normalizedCoord[0]; + normalizedCoord[1] = 1.0 - normalizedCoord[1]; + finalAng = angle - 3.14; + } else if (angle > 1.57 /* 90 deg */) { + normalizedCoord[0] = 1.0 - normalizedCoord[0]; + finalAng = 3.14 - angle; + } else { + finalAng = angle; + } + + float sine = sin(finalAng); + + float progress = (normalizedCoord[1] * sine + normalizedCoord[0] * (1.0 - sine)) * float(gradientLength - 1); + int bottom = int(floor(progress)); + int top = bottom + 1; + + return gradient[top] * (progress - float(bottom)) + gradient[bottom] * (float(top) - progress); +} + +vec4 getOkColorForCoordArray2(vec2 normalizedCoord) { + if (gradient2Length < 2) + return gradient2[0]; + + float finalAng = 0.0; + + if (angle2 > 4.71 /* 270 deg */) { + normalizedCoord[1] = 1.0 - normalizedCoord[1]; + finalAng = 6.28 - angle; + } else if (angle2 > 3.14 /* 180 deg */) { + normalizedCoord[0] = 1.0 - normalizedCoord[0]; + normalizedCoord[1] = 1.0 - normalizedCoord[1]; + finalAng = angle - 3.14; + } else if (angle2 > 1.57 /* 90 deg */) { + normalizedCoord[0] = 1.0 - normalizedCoord[0]; + finalAng = 3.14 - angle2; + } else { + finalAng = angle2; + } + + float sine = sin(finalAng); + + float progress = (normalizedCoord[1] * sine + normalizedCoord[0] * (1.0 - sine)) * float(gradient2Length - 1); + int bottom = int(floor(progress)); + int top = bottom + 1; + + return gradient2[top] * (progress - float(bottom)) + gradient2[bottom] * (float(top) - progress); +} + +vec4 getColorForCoord(vec2 normalizedCoord) { + vec4 result1 = getOkColorForCoordArray1(normalizedCoord); + + if (gradient2Length <= 0) + return okLabAToSrgb(result1); + + vec4 result2 = getOkColorForCoordArray2(normalizedCoord); + + return okLabAToSrgb(mix(result1, result2, gradientLerp)); +} + +layout(location = 0) out vec4 fragColor; +void main() { + highp vec2 pixCoord = vec2(gl_FragCoord); + highp vec2 pixCoordOuter = pixCoord; + highp vec2 originalPixCoord = v_texcoord; + originalPixCoord *= fullSizeUntransformed; + float additionalAlpha = 1.0; + + vec4 pixColor = vec4(1.0, 1.0, 1.0, 1.0); + + bool done = false; + + pixCoord -= topLeft + fullSize * 0.5; + pixCoord *= vec2(lessThan(pixCoord, vec2(0.0))) * -2.0 + 1.0; + pixCoordOuter = pixCoord; + pixCoord -= fullSize * 0.5 - radius; + pixCoordOuter -= fullSize * 0.5 - radiusOuter; + + // center the pixes dont make it top-left + pixCoord += vec2(1.0, 1.0) / fullSize; + pixCoordOuter += vec2(1.0, 1.0) / fullSize; + + if (min(pixCoord.x, pixCoord.y) > 0.0 && radius > 0.0) { + float dist = pow(pow(pixCoord.x,roundingPower)+pow(pixCoord.y,roundingPower),1.0/roundingPower); + float distOuter = pow(pow(pixCoordOuter.x,roundingPower)+pow(pixCoordOuter.y,roundingPower),1.0/roundingPower); + float h = (thick / 2.0); + + if (dist < radius - h) { + // lower + float normalized = smoothstep(0.0, 1.0, (dist - radius + thick + SMOOTHING_CONSTANT) / (SMOOTHING_CONSTANT * 2.0)); + additionalAlpha *= normalized; + done = true; + } else if (min(pixCoordOuter.x, pixCoordOuter.y) > 0.0) { + // higher + float normalized = 1.0 - smoothstep(0.0, 1.0, (distOuter - radiusOuter + SMOOTHING_CONSTANT) / (SMOOTHING_CONSTANT * 2.0)); + additionalAlpha *= normalized; + done = true; + } else if (distOuter < radiusOuter - h) { + additionalAlpha = 1.0; + done = true; + } + } + + // now check for other shit + if (!done) { + // distance to all straight bb borders + float distanceT = originalPixCoord[1]; + float distanceB = fullSizeUntransformed[1] - originalPixCoord[1]; + float distanceL = originalPixCoord[0]; + float distanceR = fullSizeUntransformed[0] - originalPixCoord[0]; + + // get the smallest + float smallest = min(min(distanceT, distanceB), min(distanceL, distanceR)); + + if (smallest > thick) + discard; + } + + if (additionalAlpha == 0.0) + discard; + + pixColor = getColorForCoord(v_texcoord); + pixColor.rgb *= pixColor[3]; + + if (skipCM == 0) + pixColor = doColorManagement(pixColor, sourceTF, sourcePrimaries, targetTF, targetPrimaries); + + pixColor *= alpha * additionalAlpha; + + fragColor = pixColor; +} diff --git a/src/render/shaders/glsl/border_legacy.frag b/src/render/shaders/glsl/border_legacy.frag new file mode 100644 index 00000000..39842be5 --- /dev/null +++ b/src/render/shaders/glsl/border_legacy.frag @@ -0,0 +1,174 @@ +#extension GL_ARB_shading_language_include : enable + +precision highp float; +varying vec4 v_color; +varying vec2 v_texcoord; + +uniform vec2 fullSizeUntransformed; +uniform float radiusOuter; +uniform float thick; + +// Gradients are in OkLabA!!!! {l, a, b, alpha} +uniform vec4 gradient[10]; +uniform vec4 gradient2[10]; +uniform int gradientLength; +uniform int gradient2Length; +uniform float angle; +uniform float angle2; +uniform float gradientLerp; +uniform float alpha; + +#include "rounding.glsl" + +float linearToGamma(float x) { + return x >= 0.0031308 ? 1.055 * pow(x, 0.416666666) - 0.055 : 12.92 * x; +} + +vec4 okLabAToSrgb(vec4 lab) { + float l = pow(lab[0] + lab[1] * 0.3963377774 + lab[2] * 0.2158037573, 3.0); + float m = pow(lab[0] + lab[1] * (-0.1055613458) + lab[2] * (-0.0638541728), 3.0); + float s = pow(lab[0] + lab[1] * (-0.0894841775) + lab[2] * (-1.2914855480), 3.0); + + return vec4(linearToGamma(l * 4.0767416621 + m * -3.3077115913 + s * 0.2309699292), + linearToGamma(l * (-1.2684380046) + m * 2.6097574011 + s * (-0.3413193965)), + linearToGamma(l * (-0.0041960863) + m * (-0.7034186147) + s * 1.7076147010), + lab[3]); +} + +vec4 getOkColorForCoordArray1(vec2 normalizedCoord) { + if (gradientLength < 2) + return gradient[0]; + + float finalAng = 0.0; + + if (angle > 4.71 /* 270 deg */) { + normalizedCoord[1] = 1.0 - normalizedCoord[1]; + finalAng = 6.28 - angle; + } else if (angle > 3.14 /* 180 deg */) { + normalizedCoord[0] = 1.0 - normalizedCoord[0]; + normalizedCoord[1] = 1.0 - normalizedCoord[1]; + finalAng = angle - 3.14; + } else if (angle > 1.57 /* 90 deg */) { + normalizedCoord[0] = 1.0 - normalizedCoord[0]; + finalAng = 3.14 - angle; + } else { + finalAng = angle; + } + + float sine = sin(finalAng); + + float progress = (normalizedCoord[1] * sine + normalizedCoord[0] * (1.0 - sine)) * float(gradientLength - 1); + int bottom = int(floor(progress)); + int top = bottom + 1; + + return gradient[top] * (progress - float(bottom)) + gradient[bottom] * (float(top) - progress); +} + +vec4 getOkColorForCoordArray2(vec2 normalizedCoord) { + if (gradient2Length < 2) + return gradient2[0]; + + float finalAng = 0.0; + + if (angle2 > 4.71 /* 270 deg */) { + normalizedCoord[1] = 1.0 - normalizedCoord[1]; + finalAng = 6.28 - angle; + } else if (angle2 > 3.14 /* 180 deg */) { + normalizedCoord[0] = 1.0 - normalizedCoord[0]; + normalizedCoord[1] = 1.0 - normalizedCoord[1]; + finalAng = angle - 3.14; + } else if (angle2 > 1.57 /* 90 deg */) { + normalizedCoord[0] = 1.0 - normalizedCoord[0]; + finalAng = 3.14 - angle2; + } else { + finalAng = angle2; + } + + float sine = sin(finalAng); + + float progress = (normalizedCoord[1] * sine + normalizedCoord[0] * (1.0 - sine)) * float(gradient2Length - 1); + int bottom = int(floor(progress)); + int top = bottom + 1; + + return gradient2[top] * (progress - float(bottom)) + gradient2[bottom] * (float(top) - progress); +} + +vec4 getColorForCoord(vec2 normalizedCoord) { + vec4 result1 = getOkColorForCoordArray1(normalizedCoord); + + if (gradient2Length <= 0) + return okLabAToSrgb(result1); + + vec4 result2 = getOkColorForCoordArray2(normalizedCoord); + + return okLabAToSrgb(mix(result1, result2, gradientLerp)); +} + +void main() { + + highp vec2 pixCoord = vec2(gl_FragCoord); + highp vec2 pixCoordOuter = pixCoord; + highp vec2 originalPixCoord = v_texcoord; + originalPixCoord *= fullSizeUntransformed; + float additionalAlpha = 1.0; + + vec4 pixColor = vec4(1.0, 1.0, 1.0, 1.0); + + bool done = false; + + pixCoord -= topLeft + fullSize * 0.5; + pixCoord *= vec2(lessThan(pixCoord, vec2(0.0))) * -2.0 + 1.0; + pixCoordOuter = pixCoord; + pixCoord -= fullSize * 0.5 - radius; + pixCoordOuter -= fullSize * 0.5 - radiusOuter; + + // center the pixes dont make it top-left + pixCoord += vec2(1.0, 1.0) / fullSize; + pixCoordOuter += vec2(1.0, 1.0) / fullSize; + + if (min(pixCoord.x, pixCoord.y) > 0.0 && radius > 0.0) { + float dist = pow(pow(pixCoord.x,roundingPower)+pow(pixCoord.y,roundingPower),1.0/roundingPower); + float distOuter = pow(pow(pixCoordOuter.x,roundingPower)+pow(pixCoordOuter.y,roundingPower),1.0/roundingPower); + float h = (thick / 2.0); + + if (dist < radius - h) { + // lower + float normalized = smoothstep(0.0, 1.0, (dist - radius + thick + SMOOTHING_CONSTANT) / (SMOOTHING_CONSTANT * 2.0)); + additionalAlpha *= normalized; + done = true; + } else if (min(pixCoordOuter.x, pixCoordOuter.y) > 0.0) { + // higher + float normalized = 1.0 - smoothstep(0.0, 1.0, (distOuter - radiusOuter + SMOOTHING_CONSTANT) / (SMOOTHING_CONSTANT * 2.0)); + additionalAlpha *= normalized; + done = true; + } else if (distOuter < radiusOuter - h) { + additionalAlpha = 1.0; + done = true; + } + } + + // now check for other shit + if (!done) { + // distance to all straight bb borders + float distanceT = originalPixCoord[1]; + float distanceB = fullSizeUntransformed[1] - originalPixCoord[1]; + float distanceL = originalPixCoord[0]; + float distanceR = fullSizeUntransformed[0] - originalPixCoord[0]; + + // get the smallest + float smallest = min(min(distanceT, distanceB), min(distanceL, distanceR)); + + if (smallest > thick) + discard; + } + + if (additionalAlpha == 0.0) + discard; + + pixColor = getColorForCoord(v_texcoord); + pixColor.rgb *= pixColor[3]; + + pixColor *= alpha * additionalAlpha; + + gl_FragColor = pixColor; +} diff --git a/src/render/shaders/glsl/ext.frag b/src/render/shaders/glsl/ext.frag new file mode 100644 index 00000000..3b71f59c --- /dev/null +++ b/src/render/shaders/glsl/ext.frag @@ -0,0 +1,35 @@ +#extension GL_ARB_shading_language_include : enable +#extension GL_OES_EGL_image_external : require + +precision highp float; +varying vec2 v_texcoord; +uniform samplerExternalOES texture0; +uniform float alpha; + +#include "rounding.glsl" + +uniform int discardOpaque; +uniform int discardAlpha; +uniform int discardAlphaValue; + +uniform int applyTint; +uniform vec3 tint; + +void main() { + + vec4 pixColor = texture2D(texture0, v_texcoord); + + if (discardOpaque == 1 && pixColor[3] * alpha == 1.0) + discard; + + if (applyTint == 1) { + pixColor[0] = pixColor[0] * tint[0]; + pixColor[1] = pixColor[1] * tint[1]; + pixColor[2] = pixColor[2] * tint[2]; + } + + if (radius > 0.0) + pixColor = rounding(pixColor); + + gl_FragColor = pixColor * alpha; +} diff --git a/src/render/shaders/glsl/glitch.frag b/src/render/shaders/glsl/glitch.frag new file mode 100644 index 00000000..f1dc121e --- /dev/null +++ b/src/render/shaders/glsl/glitch.frag @@ -0,0 +1,64 @@ +precision highp float; +varying vec2 v_texcoord; +uniform sampler2D tex; +uniform float time; // quirk: time is set to 0 at the beginning, should be around 10 when crash. +uniform float distort; +uniform vec2 screenSize; + +float rand(float co) { + return fract(sin(dot(vec2(co, co), vec2(12.9898, 78.233))) * 43758.5453); +} + +float rand(vec2 co) { + return fract(sin(dot(co, vec2(12.9898, 78.233))) * 43758.5453); +} + +float noise(vec2 point) { + vec2 floored = floor(point); + vec2 fractal = fract(point); + fractal = fractal * fractal * (3.0 - 2.0 * fractal); + + float mixed = mix( + mix(rand(floored), rand(floored + vec2(1.0, 0.0)), fractal.x), + mix(rand(floored + vec2(0.0,1.0)), rand(floored + vec2(1.0,1.0)), fractal.x), fractal.y); + return mixed * mixed; +} + +void main() { + float ABERR_OFFSET = 4.0 * (distort / 5.5) * time; + float TEAR_AMOUNT = 9000.0 * (1.0 - (distort / 5.5)); + float TEAR_BANDS = 108.0 / 2.0 * (distort / 5.5) * 2.0; + float MELT_AMOUNT = (distort * 8.0) / screenSize.y; + + float NOISE = abs(mod(noise(v_texcoord) * distort * time * 2.771, 1.0)) * time / 10.0; + if (time < 2.0) + NOISE = 0.0; + + float offset = (mod(rand(floor(v_texcoord.y * TEAR_BANDS)) * 318.772 * time, 20.0) - 10.0) / TEAR_AMOUNT; + + vec2 blockOffset = vec2(((abs(mod(rand(floor(v_texcoord.x * 37.162)) * 721.43, 100.0))) - 50.0) / 200000.0 * pow(time, 3.0), + ((abs(mod(rand(floor(v_texcoord.y * 45.882)) * 733.923, 100.0))) - 50.0) / 200000.0 * pow(time, 3.0)); + if (time < 3.0) + blockOffset = vec2(0,0); + + float meltSeed = abs(mod(rand(floor(v_texcoord.x * screenSize.x * 17.719)) * 281.882, 1.0)); + if (meltSeed < 0.8) { + meltSeed = 0.0; + } else { + meltSeed *= 25.0 * NOISE; + } + float meltAmount = MELT_AMOUNT * meltSeed; + + vec2 pixCoord = vec2(v_texcoord.x + offset + NOISE * 3.0 / screenSize.x + blockOffset.x, v_texcoord.y - meltAmount + 0.02 * NOISE / screenSize.x + NOISE * 3.0 / screenSize.y + blockOffset.y); + + vec4 pixColor = texture2D(tex, pixCoord); + vec4 pixColorLeft = texture2D(tex, pixCoord + vec2(ABERR_OFFSET / screenSize.x, 0)); + vec4 pixColorRight = texture2D(tex, pixCoord + vec2(-ABERR_OFFSET / screenSize.x, 0)); + + pixColor[0] = pixColorLeft[0]; + pixColor[2] = pixColorRight[2]; + + pixColor[0] += distort / 90.0; + + gl_FragColor = pixColor; +} diff --git a/src/render/shaders/glsl/passthru.frag b/src/render/shaders/glsl/passthru.frag new file mode 100644 index 00000000..532b5525 --- /dev/null +++ b/src/render/shaders/glsl/passthru.frag @@ -0,0 +1,7 @@ +precision highp float; +varying vec2 v_texcoord; // is in 0-1 +uniform sampler2D tex; + +void main() { + gl_FragColor = texture2D(tex, v_texcoord); +} diff --git a/src/render/shaders/glsl/quad.frag b/src/render/shaders/glsl/quad.frag new file mode 100644 index 00000000..303d1a81 --- /dev/null +++ b/src/render/shaders/glsl/quad.frag @@ -0,0 +1,14 @@ +#extension GL_ARB_shading_language_include : enable +precision highp float; +varying vec4 v_color; + +#include "rounding.glsl" + +void main() { + vec4 pixColor = v_color; + + if (radius > 0.0) + pixColor = rounding(pixColor); + + gl_FragColor = pixColor; +} diff --git a/src/render/shaders/glsl/rgba.frag b/src/render/shaders/glsl/rgba.frag new file mode 100644 index 00000000..ac2d237d --- /dev/null +++ b/src/render/shaders/glsl/rgba.frag @@ -0,0 +1,36 @@ +#extension GL_ARB_shading_language_include : enable +precision highp float; +varying vec2 v_texcoord; // is in 0-1 +uniform sampler2D tex; +uniform float alpha; + +#include "rounding.glsl" + +uniform int discardOpaque; +uniform int discardAlpha; +uniform float discardAlphaValue; + +uniform int applyTint; +uniform vec3 tint; + +void main() { + + vec4 pixColor = texture2D(tex, v_texcoord); + + if (discardOpaque == 1 && pixColor[3] * alpha == 1.0) + discard; + + if (discardAlpha == 1 && pixColor[3] <= discardAlphaValue) + discard; + + if (applyTint == 1) { + pixColor[0] = pixColor[0] * tint[0]; + pixColor[1] = pixColor[1] * tint[1]; + pixColor[2] = pixColor[2] * tint[2]; + } + + if (radius > 0.0) + pixColor = rounding(pixColor); + + gl_FragColor = pixColor * alpha; +} diff --git a/src/render/shaders/glsl/rgbamatte.frag b/src/render/shaders/glsl/rgbamatte.frag new file mode 100644 index 00000000..618e495f --- /dev/null +++ b/src/render/shaders/glsl/rgbamatte.frag @@ -0,0 +1,8 @@ +precision highp float; +varying vec2 v_texcoord; // is in 0-1 +uniform sampler2D tex; +uniform sampler2D texMatte; + +void main() { + gl_FragColor = texture2D(tex, v_texcoord) * texture2D(texMatte, v_texcoord)[0]; // I know it only uses R, but matte should be black/white anyways. +} diff --git a/src/render/shaders/glsl/rgbx.frag b/src/render/shaders/glsl/rgbx.frag new file mode 100644 index 00000000..2a2aad63 --- /dev/null +++ b/src/render/shaders/glsl/rgbx.frag @@ -0,0 +1,33 @@ +#extension GL_ARB_shading_language_include : enable +precision highp float; +varying vec2 v_texcoord; +uniform sampler2D tex; +uniform float alpha; + +#include "rounding.glsl" + +uniform int discardOpaque; +uniform int discardAlpha; +uniform int discardAlphaValue; + +uniform int applyTint; +uniform vec3 tint; + +void main() { + + if (discardOpaque == 1 && alpha == 1.0) + discard; + + vec4 pixColor = vec4(texture2D(tex, v_texcoord).rgb, 1.0); + + if (applyTint == 1) { + pixColor[0] = pixColor[0] * tint[0]; + pixColor[1] = pixColor[1] * tint[1]; + pixColor[2] = pixColor[2] * tint[2]; + } + + if (radius > 0.0) + pixColor = rounding(pixColor); + + gl_FragColor = pixColor * alpha; +} diff --git a/src/render/shaders/glsl/rounding.glsl b/src/render/shaders/glsl/rounding.glsl new file mode 100644 index 00000000..cae34465 --- /dev/null +++ b/src/render/shaders/glsl/rounding.glsl @@ -0,0 +1,29 @@ +// smoothing constant for the edge: more = blurrier, but smoother +#define M_PI 3.1415926535897932384626433832795 +#define SMOOTHING_CONSTANT (M_PI / 5.34665792551) + +uniform float radius; +uniform float roundingPower; +uniform vec2 topLeft; +uniform vec2 fullSize; + +vec4 rounding(vec4 color) { + vec2 pixCoord = vec2(gl_FragCoord); + pixCoord -= topLeft + fullSize * 0.5; + pixCoord *= vec2(lessThan(pixCoord, vec2(0.0))) * -2.0 + 1.0; + pixCoord -= fullSize * 0.5 - radius; + pixCoord += vec2(1.0, 1.0) / fullSize; // center the pix dont make it top-left + + if (pixCoord.x + pixCoord.y > radius) { + float dist = pow(pow(pixCoord.x, roundingPower) + pow(pixCoord.y, roundingPower), 1.0/roundingPower); + + if (dist > radius + SMOOTHING_CONSTANT) + discard; + + float normalized = 1.0 - smoothstep(0.0, 1.0, (dist - radius + SMOOTHING_CONSTANT) / (SMOOTHING_CONSTANT * 2.0)); + + color *= normalized; + } + + return color; +} diff --git a/src/render/shaders/glsl/shadow.frag b/src/render/shaders/glsl/shadow.frag new file mode 100644 index 00000000..7a930f96 --- /dev/null +++ b/src/render/shaders/glsl/shadow.frag @@ -0,0 +1,100 @@ +#version 300 es +#extension GL_ARB_shading_language_include : enable + +precision highp float; +in vec4 v_color; +in vec2 v_texcoord; + +uniform int skipCM; +uniform int sourceTF; // eTransferFunction +uniform int targetTF; // eTransferFunction +uniform mat4x2 sourcePrimaries; +uniform mat4x2 targetPrimaries; + +uniform vec2 topLeft; +uniform vec2 bottomRight; +uniform vec2 fullSize; +uniform float radius; +uniform float roundingPower; +uniform float range; +uniform float shadowPower; + +#include "CM.glsl" + +float pixAlphaRoundedDistance(float distanceToCorner) { + if (distanceToCorner > radius) { + return 0.0; + } + + if (distanceToCorner > radius - range) { + return pow((range - (distanceToCorner - radius + range)) / range, shadowPower); // i think? + } + + return 1.0; +} + +float modifiedLength(vec2 a) { + return pow(pow(abs(a.x),roundingPower)+pow(abs(a.y),roundingPower),1.0/roundingPower); +} + +layout(location = 0) out vec4 fragColor; +void main() { + + vec4 pixColor = v_color; + float originalAlpha = pixColor[3]; + + bool done = false; + + vec2 pixCoord = fullSize * v_texcoord; + + // ok, now we check the distance to a border. + + if (pixCoord[0] < topLeft[0]) { + if (pixCoord[1] < topLeft[1]) { + // top left + pixColor[3] = pixColor[3] * pixAlphaRoundedDistance(modifiedLength(pixCoord - topLeft)); + done = true; + } else if (pixCoord[1] > bottomRight[1]) { + // bottom left + pixColor[3] = pixColor[3] * pixAlphaRoundedDistance(modifiedLength(pixCoord - vec2(topLeft[0], bottomRight[1]))); + done = true; + } + } else if (pixCoord[0] > bottomRight[0]) { + if (pixCoord[1] < topLeft[1]) { + // top right + pixColor[3] = pixColor[3] * pixAlphaRoundedDistance(modifiedLength(pixCoord - vec2(bottomRight[0], topLeft[1]))); + done = true; + } else if (pixCoord[1] > bottomRight[1]) { + // bottom right + pixColor[3] = pixColor[3] * pixAlphaRoundedDistance(modifiedLength(pixCoord - bottomRight)); + done = true; + } + } + + if (!done) { + // distance to all straight bb borders + float distanceT = pixCoord[1]; + float distanceB = fullSize[1] - pixCoord[1]; + float distanceL = pixCoord[0]; + float distanceR = fullSize[0] - pixCoord[0]; + + // get the smallest + float smallest = min(min(distanceT, distanceB), min(distanceL, distanceR)); + + if (smallest < range) { + pixColor[3] = pixColor[3] * pow((smallest / range), shadowPower); + } + } + + if (pixColor[3] == 0.0) { + discard; return; + } + + // premultiply + pixColor.rgb *= pixColor[3]; + + if (skipCM == 0) + pixColor = doColorManagement(pixColor, sourceTF, sourcePrimaries, targetTF, targetPrimaries); + + fragColor = pixColor; +} \ No newline at end of file diff --git a/src/render/shaders/glsl/shadow_legacy.frag b/src/render/shaders/glsl/shadow_legacy.frag new file mode 100644 index 00000000..bf929e63 --- /dev/null +++ b/src/render/shaders/glsl/shadow_legacy.frag @@ -0,0 +1,86 @@ +#extension GL_ARB_shading_language_include : enable +precision highp float; +varying vec4 v_color; +varying vec2 v_texcoord; + +uniform vec2 topLeft; +uniform vec2 bottomRight; +uniform vec2 fullSize; +uniform float radius; +uniform float roundingPower; +uniform float range; +uniform float shadowPower; + +float pixAlphaRoundedDistance(float distanceToCorner) { + if (distanceToCorner > radius) { + return 0.0; + } + + if (distanceToCorner > radius - range) { + return pow((range - (distanceToCorner - radius + range)) / range, shadowPower); // i think? + } + + return 1.0; +} + +float modifiedLength(vec2 a) { + return pow(pow(abs(a.x),roundingPower)+pow(abs(a.y),roundingPower),1.0/roundingPower); +} + +void main() { + + vec4 pixColor = v_color; + float originalAlpha = pixColor[3]; + + bool done = false; + + vec2 pixCoord = fullSize * v_texcoord; + + // ok, now we check the distance to a border. + + if (pixCoord[0] < topLeft[0]) { + if (pixCoord[1] < topLeft[1]) { + // top left + pixColor[3] = pixColor[3] * pixAlphaRoundedDistance(modifiedLength(pixCoord - topLeft)); + done = true; + } else if (pixCoord[1] > bottomRight[1]) { + // bottom left + pixColor[3] = pixColor[3] * pixAlphaRoundedDistance(modifiedLength(pixCoord - vec2(topLeft[0], bottomRight[1]))); + done = true; + } + } else if (pixCoord[0] > bottomRight[0]) { + if (pixCoord[1] < topLeft[1]) { + // top right + pixColor[3] = pixColor[3] * pixAlphaRoundedDistance(modifiedLength(pixCoord - vec2(bottomRight[0], topLeft[1]))); + done = true; + } else if (pixCoord[1] > bottomRight[1]) { + // bottom right + pixColor[3] = pixColor[3] * pixAlphaRoundedDistance(modifiedLength(pixCoord - bottomRight)); + done = true; + } + } + + if (!done) { + // distance to all straight bb borders + float distanceT = pixCoord[1]; + float distanceB = fullSize[1] - pixCoord[1]; + float distanceL = pixCoord[0]; + float distanceR = fullSize[0] - pixCoord[0]; + + // get the smallest + float smallest = min(min(distanceT, distanceB), min(distanceL, distanceR)); + + if (smallest < range) { + pixColor[3] = pixColor[3] * pow((smallest / range), shadowPower); + } + } + + if (pixColor[3] == 0.0) { + discard; return; + } + + // premultiply + pixColor.rgb *= pixColor[3]; + + gl_FragColor = pixColor; +} diff --git a/src/render/shaders/glsl/tex.vert b/src/render/shaders/glsl/tex.vert new file mode 100644 index 00000000..6ede6a8e --- /dev/null +++ b/src/render/shaders/glsl/tex.vert @@ -0,0 +1,15 @@ +uniform mat3 proj; +uniform vec4 color; +attribute vec2 pos; +attribute vec2 texcoord; +attribute vec2 texcoordMatte; +varying vec4 v_color; +varying vec2 v_texcoord; +varying vec2 v_texcoordMatte; + +void main() { + gl_Position = vec4(proj * vec3(pos, 1.0), 1.0); + v_color = color; + v_texcoord = texcoord; + v_texcoordMatte = texcoordMatte; +} \ No newline at end of file diff --git a/src/render/shaders/glsl/tex300.vert b/src/render/shaders/glsl/tex300.vert new file mode 100644 index 00000000..f9f12998 --- /dev/null +++ b/src/render/shaders/glsl/tex300.vert @@ -0,0 +1,17 @@ +#version 300 es + +uniform mat3 proj; +uniform vec4 color; +in vec2 pos; +in vec2 texcoord; +in vec2 texcoordMatte; +out vec4 v_color; +out vec2 v_texcoord; +out vec2 v_texcoordMatte; + +void main() { + gl_Position = vec4(proj * vec3(pos, 1.0), 1.0); + v_color = color; + v_texcoord = texcoord; + v_texcoordMatte = texcoordMatte; +} diff --git a/src/render/shaders/glsl/tex320.vert b/src/render/shaders/glsl/tex320.vert new file mode 100644 index 00000000..309bc02b --- /dev/null +++ b/src/render/shaders/glsl/tex320.vert @@ -0,0 +1,17 @@ +#version 320 es + +uniform mat3 proj; +uniform vec4 color; +in vec2 pos; +in vec2 texcoord; +in vec2 texcoordMatte; +out vec4 v_color; +out vec2 v_texcoord; +out vec2 v_texcoordMatte; + +void main() { + gl_Position = vec4(proj * vec3(pos, 1.0), 1.0); + v_color = color; + v_texcoord = texcoord; + v_texcoordMatte = texcoordMatte; +} -- cgit v1.2.3