Skip to content

Commit

Permalink
Merge pull request #30993 from Chaosus/gles2_funcs
Browse files Browse the repository at this point in the history
Expose several GLES3 built-ins to GLES2
  • Loading branch information
akien-mga authored Aug 1, 2019
2 parents 3256fbf + 8b4c538 commit 8743da9
Show file tree
Hide file tree
Showing 4 changed files with 344 additions and 28 deletions.
35 changes: 34 additions & 1 deletion drivers/gles2/shader_compiler_gles2.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -928,9 +928,25 @@ ShaderCompilerGLES2::ShaderCompilerGLES2() {
actions[VS::SHADER_CANVAS_ITEM].usage_defines["NORMAL"] = "#define NORMAL_USED\n";
actions[VS::SHADER_CANVAS_ITEM].usage_defines["NORMALMAP"] = "#define NORMALMAP_USED\n";
actions[VS::SHADER_CANVAS_ITEM].usage_defines["LIGHT"] = "#define USE_LIGHT_SHADER_CODE\n";
actions[VS::SHADER_CANVAS_ITEM].usage_defines["round"] = "#define ROUND_USED\n";
actions[VS::SHADER_CANVAS_ITEM].render_mode_defines["skip_vertex_transform"] = "#define SKIP_TRANSFORM_USED\n";

// Ported from GLES3

actions[VS::SHADER_CANVAS_ITEM].usage_defines["sinh"] = "#define SINH_USED\n";
actions[VS::SHADER_CANVAS_ITEM].usage_defines["cosh"] = "#define COSH_USED\n";
actions[VS::SHADER_CANVAS_ITEM].usage_defines["tanh"] = "#define TANH_USED\n";
actions[VS::SHADER_CANVAS_ITEM].usage_defines["asinh"] = "#define ASINH_USED\n";
actions[VS::SHADER_CANVAS_ITEM].usage_defines["acosh"] = "#define ACOSH_USED\n";
actions[VS::SHADER_CANVAS_ITEM].usage_defines["atanh"] = "#define ATANH_USED\n";
actions[VS::SHADER_CANVAS_ITEM].usage_defines["determinant"] = "#define DETERMINANT_USED\n";
actions[VS::SHADER_CANVAS_ITEM].usage_defines["transpose"] = "#define TRANSPOSE_USED\n";
actions[VS::SHADER_CANVAS_ITEM].usage_defines["outerProduct"] = "#define OUTER_PRODUCT_USED\n";
actions[VS::SHADER_CANVAS_ITEM].usage_defines["round"] = "#define ROUND_USED\n";
actions[VS::SHADER_CANVAS_ITEM].usage_defines["roundEven"] = "#define ROUND_EVEN_USED\n";
actions[VS::SHADER_CANVAS_ITEM].usage_defines["isinf"] = "#define IS_INF_USED\n";
actions[VS::SHADER_CANVAS_ITEM].usage_defines["isnan"] = "#define IS_NAN_USED\n";
actions[VS::SHADER_CANVAS_ITEM].usage_defines["trunc"] = "#define TRUNC_USED\n";

/** SPATIAL SHADER **/

actions[VS::SHADER_SPATIAL].renames["WORLD_MATRIX"] = "world_transform";
Expand Down Expand Up @@ -1023,6 +1039,23 @@ ShaderCompilerGLES2::ShaderCompilerGLES2() {
actions[VS::SHADER_SPATIAL].usage_defines["DIFFUSE_LIGHT"] = "#define USE_LIGHT_SHADER_CODE\n";
actions[VS::SHADER_SPATIAL].usage_defines["SPECULAR_LIGHT"] = "#define USE_LIGHT_SHADER_CODE\n";

// Ported from GLES3

actions[VS::SHADER_SPATIAL].usage_defines["sinh"] = "#define SINH_USED\n";
actions[VS::SHADER_SPATIAL].usage_defines["cosh"] = "#define COSH_USED\n";
actions[VS::SHADER_SPATIAL].usage_defines["tanh"] = "#define TANH_USED\n";
actions[VS::SHADER_SPATIAL].usage_defines["asinh"] = "#define ASINH_USED\n";
actions[VS::SHADER_SPATIAL].usage_defines["acosh"] = "#define ACOSH_USED\n";
actions[VS::SHADER_SPATIAL].usage_defines["atanh"] = "#define ATANH_USED\n";
actions[VS::SHADER_SPATIAL].usage_defines["determinant"] = "#define DETERMINANT_USED\n";
actions[VS::SHADER_SPATIAL].usage_defines["transpose"] = "#define TRANSPOSE_USED\n";
actions[VS::SHADER_SPATIAL].usage_defines["outerProduct"] = "#define OUTER_PRODUCT_USED\n";
actions[VS::SHADER_SPATIAL].usage_defines["round"] = "#define ROUND_USED\n";
actions[VS::SHADER_SPATIAL].usage_defines["roundEven"] = "#define ROUND_EVEN_USED\n";
actions[VS::SHADER_SPATIAL].usage_defines["isinf"] = "#define IS_INF_USED\n";
actions[VS::SHADER_SPATIAL].usage_defines["isnan"] = "#define IS_NAN_USED\n";
actions[VS::SHADER_SPATIAL].usage_defines["trunc"] = "#define TRUNC_USED\n";

actions[VS::SHADER_SPATIAL].render_mode_defines["skip_vertex_transform"] = "#define SKIP_TRANSFORM_USED\n";
actions[VS::SHADER_SPATIAL].render_mode_defines["world_vertex_coords"] = "#define VERTEX_WORLD_COORDS_USED\n";

Expand Down
2 changes: 2 additions & 0 deletions drivers/gles2/shaders/scene.glsl
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@ precision highp float;
precision highp int;
#endif

/* clang-format on */
#include "stdlib.glsl"
/* clang-format off */

#define SHADER_IS_SRGB true

Expand Down
291 changes: 286 additions & 5 deletions drivers/gles2/shaders/stdlib.glsl
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,139 @@ highp vec4 texel2DFetch(highp sampler2D tex, ivec2 size, ivec2 coord) {

return texture2DLod(tex, vec2(x_coord, y_coord), 0.0);
}

#ifdef SINH_USED

highp float sinh(highp float x) {
return 0.5 * (exp(x) - exp(-x));
}

highp vec2 sinh(highp vec2 x) {
return 0.5 * vec2(exp(x.x) - exp(-x.x), exp(x.y) - exp(-x.y));
}

highp vec3 sinh(highp vec3 x) {
return 0.5 * vec3(exp(x.x) - exp(-x.x), exp(x.y) - exp(-x.y), exp(x.z) - exp(-x.z));
}

highp vec4 sinh(highp vec4 x) {
return 0.5 * vec4(exp(x.x) - exp(-x.x), exp(x.y) - exp(-x.y), exp(x.z) - exp(-x.z), exp(x.w) - exp(-x.w));
}

#endif

#ifdef COSH_USED

highp float cosh(highp float x) {
return 0.5 * (exp(x) + exp(-x));
}

highp vec2 cosh(highp vec2 x) {
return 0.5 * vec2(exp(x.x) + exp(-x.x), exp(x.y) + exp(-x.y));
}

highp vec3 cosh(highp vec3 x) {
return 0.5 * vec3(exp(x.x) + exp(-x.x), exp(x.y) + exp(-x.y), exp(x.z) + exp(-x.z));
}

highp vec4 cosh(highp vec4 x) {
return 0.5 * vec4(exp(x.x) + exp(-x.x), exp(x.y) + exp(-x.y), exp(x.z) + exp(-x.z), exp(x.w) + exp(-x.w));
}

#endif

#ifdef TANH_USED

highp float tanh(highp float x) {
highp float exp2x = exp(2.0 * x);
return (exp2x - 1.0) / (exp2x + 1.0);
}

highp vec2 tanh(highp vec2 x) {
highp float exp2x = exp(2.0 * x.x);
highp float exp2y = exp(2.0 * x.y);
return vec2((exp2x - 1.0) / (exp2x + 1.0), (exp2y - 1.0) / (exp2y + 1.0));
}

highp vec3 tanh(highp vec3 x) {
highp float exp2x = exp(2.0 * x.x);
highp float exp2y = exp(2.0 * x.y);
highp float exp2z = exp(2.0 * x.z);
return vec3((exp2x - 1.0) / (exp2x + 1.0), (exp2y - 1.0) / (exp2y + 1.0), (exp2z - 1.0) / (exp2z + 1.0));
}

highp vec4 tanh(highp vec4 x) {
highp float exp2x = exp(2.0 * x.x);
highp float exp2y = exp(2.0 * x.y);
highp float exp2z = exp(2.0 * x.z);
highp float exp2w = exp(2.0 * x.w);
return vec4((exp2x - 1.0) / (exp2x + 1.0), (exp2y - 1.0) / (exp2y + 1.0), (exp2z - 1.0) / (exp2z + 1.0), (exp2w - 1.0) / (exp2w + 1.0));
}

#endif

#ifdef ASINH_USED

highp float asinh(highp float x) {
return sign(x) * log(abs(x) + sqrt(1.0 + x * x));
}

highp vec2 asinh(highp vec2 x) {
return vec2(sign(x.x) * log(abs(x.x) + sqrt(1.0 + x.x * x.x)), sign(x.y) * log(abs(x.y) + sqrt(1.0 + x.y * x.y)));
}

highp vec3 asinh(highp vec3 x) {
return vec3(sign(x.x) * log(abs(x.x) + sqrt(1.0 + x.x * x.x)), sign(x.y) * log(abs(x.y) + sqrt(1.0 + x.y * x.y)), sign(x.z) * log(abs(x.z) + sqrt(1.0 + x.z * x.z)));
}

highp vec4 asinh(highp vec4 x) {
return vec4(sign(x.x) * log(abs(x.x) + sqrt(1.0 + x.x * x.x)), sign(x.y) * log(abs(x.y) + sqrt(1.0 + x.y * x.y)), sign(x.z) * log(abs(x.z) + sqrt(1.0 + x.z * x.z)), sign(x.w) * log(abs(x.w) + sqrt(1.0 + x.w * x.w)));
}

#endif

#ifdef ACOSH_USED

highp float acosh(highp float x) {
return log(x + sqrt(x * x - 1.0));
}

highp vec2 acosh(highp vec2 x) {
return vec2(log(x.x + sqrt(x.x * x.x - 1.0)), log(x.y + sqrt(x.y * x.y - 1.0)));
}

highp vec3 acosh(highp vec3 x) {
return vec3(log(x.x + sqrt(x.x * x.x - 1.0)), log(x.y + sqrt(x.y * x.y - 1.0)), log(x.z + sqrt(x.z * x.z - 1.0)));
}

highp vec4 acosh(highp vec4 x) {
return vec4(log(x.x + sqrt(x.x * x.x - 1.0)), log(x.y + sqrt(x.y * x.y - 1.0)), log(x.z + sqrt(x.z * x.z - 1.0)), log(x.w + sqrt(x.w * x.w - 1.0)));
}

#endif

#ifdef ATANH_USED

highp float atanh(highp float x) {
return 0.5 * log((1.0 + x) / (1.0 - x));
}

highp vec2 atanh(highp vec2 x) {
return 0.5 * vec2(log((1.0 + x.x) / (1.0 - x.x)), log((1.0 + x.y) / (1.0 - x.y)));
}

highp vec3 atanh(highp vec3 x) {
return 0.5 * vec3(log((1.0 + x.x) / (1.0 - x.x)), log((1.0 + x.y) / (1.0 - x.y)), log((1.0 + x.z) / (1.0 - x.z)));
}

highp vec4 atanh(highp vec4 x) {
return 0.5 * vec4(log((1.0 + x.x) / (1.0 - x.x)), log((1.0 + x.y) / (1.0 - x.y)), log((1.0 + x.z) / (1.0 - x.z)), log((1.0 + x.w) / (1.0 - x.w)));
}

#endif

#ifdef ROUND_USED

highp float round(highp float x) {
return floor(x + 0.5);
}
Expand All @@ -51,14 +183,163 @@ highp vec3 round(highp vec3 x) {
highp vec4 round(highp vec4 x) {
return floor(x + vec4(0.5));
}

#endif

#ifdef ROUND_EVEN_USED

highp float roundEven(highp float x) {
highp float t = x + 0.5;
highp float f = floor(t);
highp float r;
if (t == f) {
if (x > 0)
r = f - mod(f, 2);
else
r = f + mod(f, 2);
} else
r = f;
return r;
}

highp vec2 roundEven(highp vec2 x) {
return vec2(roundEven(x.x), roundEven(x.y));
}

highp vec3 roundEven(highp vec3 x) {
return vec3(roundEven(x.x), roundEven(x.y), roundEven(x.z));
}

highp vec4 roundEven(highp vec4 x) {
return vec4(roundEven(x.x), roundEven(x.y), roundEven(x.z), roundEven(x.w));
}

#endif

#ifdef IS_INF_USED

bool isinf(highp float x) {
return (2 * x == x) && (x != 0);
}

bvec2 isinf(highp vec2 x) {
return bvec2((2 * x.x == x.x) && (x.x != 0), (2 * x.y == x.y) && (x.y != 0));
}

bvec3 isinf(highp vec3 x) {
return bvec3((2 * x.x == x.x) && (x.x != 0), (2 * x.y == x.y) && (x.y != 0), (2 * x.z == x.z) && (x.z != 0));
}

bvec4 isinf(highp vec4 x) {
return bvec4((2 * x.x == x.x) && (x.x != 0), (2 * x.y == x.y) && (x.y != 0), (2 * x.z == x.z) && (x.z != 0), (2 * x.w == x.w) && (x.w != 0));
}

#endif

#ifdef IS_NAN_USED

bool isnan(highp float x) {
return x != x;
}

bvec2 isnan(highp vec2 x) {
return bvec2(x.x != x.x, x.y != x.y);
}

bvec3 isnan(highp vec3 x) {
return bvec3(x.x != x.x, x.y != x.y, x.z != x.z);
}

bvec4 isnan(highp vec4 x) {
return bvec4(x.x != x.x, x.y != x.y, x.z != x.z, x.w != x.w);
}

#endif

#ifdef TRUNC_USED

highp float trunc(highp float x) {
return x < 0 ? -floor(-x) : floor(x);
}

highp vec2 trunc(highp vec2 x) {
return vec2(x.x < 0 ? -floor(-x.x) : floor(x.x), x.y < 0 ? -floor(-x.y) : floor(x.y));
}

highp vec3 trunc(highp vec3 x) {
return vec3(x.x < 0 ? -floor(-x.x) : floor(x.x), x.y < 0 ? -floor(-x.y) : floor(x.y), x.z < 0 ? -floor(-x.z) : floor(x.z));
}

highp vec4 trunc(highp vec4 x) {
return vec4(x.x < 0 ? -floor(-x.x) : floor(x.x), x.y < 0 ? -floor(-x.y) : floor(x.y), x.z < 0 ? -floor(-x.z) : floor(x.z), x.w < 0 ? -floor(-x.w) : floor(x.w));
}

#endif

#ifdef DETERMINANT_USED

highp float determinant(highp mat2 m) {
return m[0].x * m[1].y - m[1].x * m[0].y;
}

highp float determinant(highp mat3 m) {
return m[0].x * (m[1].y * m[2].z - m[2].y * m[1].z) - m[1].x * (m[0].y * m[2].z - m[2].y * m[0].z) + m[2].x * (m[0].y * m[1].z - m[1].y * m[0].z);
}

highp float determinant(highp mat4 m) {
highp float s00 = m[2].z * m[3].w - m[3].z * m[2].w;
highp float s01 = m[2].y * m[3].w - m[3].y * m[2].w;
highp float s02 = m[2].y * m[3].z - m[3].y * m[2].z;
highp float s03 = m[2].x * m[3].w - m[3].x * m[2].w;
highp float s04 = m[2].x * m[3].z - m[3].x * m[2].z;
highp float s05 = m[2].x * m[3].y - m[3].x * m[2].y;
highp vec4 c = vec4((m[1].y * s00 - m[1].z * s01 + m[1].w * s02), -(m[1].x * s00 - m[1].z * s03 + m[1].w * s04), (m[1].x * s01 - m[1].y * s03 + m[1].w * s05), -(m[1].x * s02 - m[1].y * s04 + m[1].z * s05));
return m[0].x * c.x + m[0].y * c.y + m[0].z * c.z + m[0].w * c.w;
}

#endif

#ifndef USE_GLES_OVER_GL
highp mat4 transpose(highp mat4 src) {

#ifdef TRANSPOSE_USED

highp mat2 transpose(highp mat2 m) {
return mat2(
vec2(m[0].x, m[1].x),
vec2(m[0].y, m[1].y));
}

highp mat3 transpose(highp mat3 m) {
return mat3(
vec3(m[0].x, m[1].x, m[2].x),
vec3(m[0].y, m[1].y, m[2].y),
vec3(m[0].z, m[1].z, m[2].z));
}

#endif

highp mat4 transpose(highp mat4 m) {
return mat4(
vec4(src[0].x, src[1].x, src[2].x, src[3].x),
vec4(src[0].y, src[1].y, src[2].y, src[3].y),
vec4(src[0].z, src[1].z, src[2].z, src[3].z),
vec4(src[0].w, src[1].w, src[2].w, src[3].w));
vec4(m[0].x, m[1].x, m[2].x, m[3].x),
vec4(m[0].y, m[1].y, m[2].y, m[3].y),
vec4(m[0].z, m[1].z, m[2].z, m[3].z),
vec4(m[0].w, m[1].w, m[2].w, m[3].w));
}

#ifdef OUTER_PRODUCT_USED

highp mat2 outerProduct(highp vec2 c, highp vec2 r) {
return mat2(c * r.x, c * r.y);
}

highp mat3 outerProduct(highp vec3 c, highp vec3 r) {
return mat3(c * r.x, c * r.y, c * r.z);
}

highp mat4 outerProduct(highp vec4 c, highp vec4 r) {
return mat4(c * r.x, c * r.y, c * r.z, c * r.w);
}

#endif

#endif
Loading

0 comments on commit 8743da9

Please sign in to comment.