Created
September 3, 2018 13:58
-
-
Save jt0dd/cf6d6f1be251d04fa1c02183c20dec39 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #version 300 es | |
| precision highp float; | |
| precision highp int; | |
| precision highp sampler2D; | |
| const float LOOP_MAX = 1000.0; | |
| uniform ivec3 uOutputDim; | |
| uniform ivec2 uTexSize; | |
| in vec2 vTexCoord; | |
| vec2 integerMod(vec2 x, float y) { | |
| vec2 res = floor(mod(x, y)); | |
| return res * step(1.0 - floor(y), -res); | |
| } | |
| vec3 integerMod(vec3 x, float y) { | |
| vec3 res = floor(mod(x, y)); | |
| return res * step(1.0 - floor(y), -res); | |
| } | |
| vec4 integerMod(vec4 x, vec4 y) { | |
| vec4 res = floor(mod(x, y)); | |
| return res * step(1.0 - floor(y), -res); | |
| } | |
| float integerMod(float x, float y) { | |
| float res = floor(mod(x, y)); | |
| return res * (res > floor(y) - 1.0 ? 0.0 : 1.0); | |
| } | |
| int integerMod(int x, int y) { | |
| return x - (y * int(x/y)); | |
| } | |
| float div_with_int_check(float x, float y) { | |
| if (floor(x) == x && floor(y) == y && integerMod(x, y) == 0.0) { | |
| return float(int(x)/int(y)); | |
| } | |
| return x / y; | |
| } | |
| // Here be dragons! | |
| // DO NOT OPTIMIZE THIS CODE | |
| // YOU WILL BREAK SOMETHING ON SOMEBODY'S MACHINE | |
| // LEAVE IT AS IT IS, LEST YOU WASTE YOUR OWN TIME | |
| const vec2 MAGIC_VEC = vec2(1.0, -256.0); | |
| const vec4 SCALE_FACTOR = vec4(1.0, 256.0, 65536.0, 0.0); | |
| const vec4 SCALE_FACTOR_INV = vec4(1.0, 0.00390625, 0.0000152587890625, 0.0); // 1, 1/256, 1/65536 | |
| float decode32(vec4 rgba) { | |
| rgba *= 255.0; | |
| vec2 gte128; | |
| gte128.x = rgba.b >= 128.0 ? 1.0 : 0.0; | |
| gte128.y = rgba.a >= 128.0 ? 1.0 : 0.0; | |
| float exponent = 2.0 * rgba.a - 127.0 + dot(gte128, MAGIC_VEC); | |
| float res = exp2(round(exponent)); | |
| rgba.b = rgba.b - 128.0 * gte128.x; | |
| res = dot(rgba, SCALE_FACTOR) * exp2(round(exponent-23.0)) + res; | |
| res *= gte128.y * -2.0 + 1.0; | |
| return res; | |
| } | |
| vec4 encode32(float f) { | |
| float F = abs(f); | |
| float sign = f < 0.0 ? 1.0 : 0.0; | |
| float exponent = floor(log2(F)); | |
| float mantissa = (exp2(-exponent) * F); | |
| // exponent += floor(log2(mantissa)); | |
| vec4 rgba = vec4(F * exp2(23.0-exponent)) * SCALE_FACTOR_INV; | |
| rgba.rg = integerMod(rgba.rg, 256.0); | |
| rgba.b = integerMod(rgba.b, 128.0); | |
| rgba.a = exponent*0.5 + 63.5; | |
| rgba.ba += vec2(integerMod(exponent+127.0, 2.0), sign) * 128.0; | |
| rgba = floor(rgba); | |
| rgba *= 0.003921569; // 1/255 | |
| return rgba; | |
| } | |
| // Dragons end here | |
| float decode(vec4 rgba, int x, int bitRatio) { | |
| if (bitRatio == 1) { | |
| return decode32(rgba); | |
| } | |
| int channel = integerMod(x, bitRatio); | |
| if (bitRatio == 4) { | |
| return rgba[channel] * 255.0; | |
| } | |
| else { | |
| return rgba[channel*2] * 255.0 + rgba[channel*2 + 1] * 65280.0; | |
| } | |
| } | |
| int index; | |
| ivec3 threadId; | |
| ivec3 indexTo3D(int idx, ivec3 texDim) { | |
| int z = int(idx / (texDim.x * texDim.y)); | |
| idx -= z * int(texDim.x * texDim.y); | |
| int y = int(idx / texDim.x); | |
| int x = int(integerMod(idx, texDim.x)); | |
| return ivec3(x, y, z); | |
| } | |
| float get(sampler2D tex, ivec2 texSize, ivec3 texDim, int bitRatio, int z, int y, int x) { | |
| ivec3 xyz = ivec3(x, y, z); | |
| int index = xyz.x + texDim.x * (xyz.y + texDim.y * xyz.z); | |
| int w = texSize.x; | |
| vec2 st = vec2(float(integerMod(index, w)), float(index / w)) + 0.5; | |
| vec4 texel = texture(tex, st / vec2(texSize)); | |
| return decode(texel, x, bitRatio); | |
| } | |
| vec4 getImage2D(sampler2D tex, ivec2 texSize, ivec3 texDim, int z, int y, int x) { | |
| ivec3 xyz = ivec3(x, y, z); | |
| int index = xyz.x + texDim.x * (xyz.y + texDim.y * xyz.z); | |
| int w = texSize.x; | |
| vec2 st = vec2(float(integerMod(index, w)), float(index / w)) + 0.5; | |
| return texture(tex, st / vec2(texSize)); | |
| } | |
| vec4 getImage3D(sampler2DArray tex, ivec2 texSize, ivec3 texDim, int z, int y, int x) { | |
| ivec3 xyz = ivec3(x, y, z); | |
| int index = xyz.x + texDim.x * (xyz.y + texDim.y * xyz.z); | |
| int w = texSize.x; | |
| vec2 st = vec2(float(integerMod(index, w)), float(index / w)) + 0.5; | |
| return texture(tex, vec3(st / vec2(texSize), z)); | |
| } | |
| float get(sampler2D tex, ivec2 texSize, ivec3 texDim, int bitRatio, int y, int x) { | |
| return get(tex, texSize, texDim, bitRatio, 0, y, x); | |
| } | |
| float get(sampler2D tex, ivec2 texSize, ivec3 texDim, int bitRatio, int x) { | |
| return get(tex, texSize, texDim, bitRatio, 0, 0, x); | |
| } | |
| vec4 getImage2D(sampler2D tex, ivec2 texSize, ivec3 texDim, int y, int x) { | |
| return getImage2D(tex, texSize, texDim, 0, y, x); | |
| } | |
| vec4 getImage2D(sampler2D tex, ivec2 texSize, ivec3 texDim, int x) { | |
| return getImage2D(tex, texSize, texDim, 0, 0, x); | |
| } | |
| vec4 actualColor; | |
| void color(float r, float g, float b, float a) { | |
| actualColor = vec4(r,g,b,a); | |
| } | |
| void color(float r, float g, float b) { | |
| color(r,g,b,1.0); | |
| } | |
| uniform highp sampler2D constants_textures; | |
| uniform highp ivec2 constants_texturesSize; | |
| uniform highp ivec3 constants_texturesDim; | |
| uniform highp int constants_texturesBitRatio; | |
| out vec4 data0; | |
| float kernelResult = 0.0; | |
| void kernel() { | |
| float user_index=0.0; | |
| kernelResult = get(constants_textures, ivec2(constants_texturesSize[0],constants_texturesSize[1]), ivec3(constants_texturesDim[0],constants_texturesDim[1],constants_texturesDim[2]), constants_texturesBitRatio, int(user_index),0);return; | |
| } | |
| void main(void) { | |
| index = int(vTexCoord.s * float(uTexSize.x)) + int(vTexCoord.t * float(uTexSize.y)) * uTexSize.x; | |
| threadId = indexTo3D(index, uOutputDim); | |
| kernel(); | |
| data0 = encode32(kernelResult); | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment