Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save robertleeplummerjr/0733b02a943908b25cf51d263d470c53 to your computer and use it in GitHub Desktop.
Save robertleeplummerjr/0733b02a943908b25cf51d263d470c53 to your computer and use it in GitHub Desktop.
const gl = canvas.getContext('webgl2', {
alpha: false,
depth: false,
antialias: false
});
const glVariable0 = gl.getParameter(gl.RENDERER);
const glVariables1 = gl.getExtension('EXT_color_buffer_float');
const glVariables2 = gl.getExtension('OES_texture_float_linear');
const glVariables3 = gl.getExtension('EXT_color_buffer_float');
gl.enable(gl.SCISSOR_TEST);
gl.viewport(0, 0, 2, 2);
const glVariable4 = gl.createShader(gl.VERTEX_SHADER);
gl.shaderSource(glVariable4, `#version 300 es
precision highp float;
precision highp int;
precision highp sampler2D;
in vec2 aPos;
in vec2 aTexCoord;
out vec2 vTexCoord;
uniform vec2 ratio;
void main(void) {
gl_Position = vec4((aPos + vec2(1)) * ratio + vec2(-1), 0, 1);
vTexCoord = aTexCoord;
}`);
gl.compileShader(glVariable4);
const glVariable5 = gl.createShader(gl.FRAGMENT_SHADER);
gl.shaderSource(glVariable5, `#version 300 es
precision highp float;
precision highp int;
precision highp sampler2D;
const int LOOP_MAX = 1000;
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 channel = integerMod(index, 4);
index = index / 4;
int w = texSize.x;
vec2 st = vec2(float(integerMod(index, w)), float(index / w)) + 0.5;
index = index / 4;
vec4 texel = texture(tex, st / vec2(texSize));
return texel[channel];}
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 channel = integerMod(index, 4);
index = index / 4;
int w = texSize.x;
vec2 st = vec2(float(integerMod(index, w)), float(index / w)) + 0.5;
index = index / 4;
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 channel = integerMod(index, 4);
index = index / 4;
int w = texSize.x;
vec2 st = vec2(float(integerMod(index, w)), float(index / w)) + 0.5;
index = index / 4;
return texture(tex, vec3(st / vec2(texSize), z));
}
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 user_a;
uniform highp ivec2 user_aSize;
uniform highp ivec3 user_aDim;
uniform highp int user_aBitRatio;
out vec4 data0;
float kernelResult = 0.0;
void kernel() {
kernelResult = get(user_a, user_aSize, user_aDim, user_aBitRatio, 0, 0, threadId.x);return;
}
void main(void) {
index = int(vTexCoord.s * float(uTexSize.x)) + int(vTexCoord.t * float(uTexSize.y)) * uTexSize.x;
index *= 4;
threadId = indexTo3D(index, uOutputDim);
kernel();
data0.r = kernelResult;
index += 1;
threadId = indexTo3D(index, uOutputDim);
kernel();
data0.g = kernelResult;
index += 1;
threadId = indexTo3D(index, uOutputDim);
kernel();
data0.b = kernelResult;
index += 1;
threadId = indexTo3D(index, uOutputDim);
kernel();
data0.a = kernelResult;
}`);
gl.compileShader(glVariable5);
const glVariable6 = gl.getShaderParameter(glVariable4, gl.COMPILE_STATUS);
const glVariable7 = gl.getShaderParameter(glVariable5, gl.COMPILE_STATUS);
const glVariable8 = gl.createProgram();
gl.attachShader(glVariable8, glVariable4);
gl.attachShader(glVariable8, glVariable5);
gl.linkProgram(glVariable8);
const glVariable9 = gl.createFramebuffer();
const glVariable10 = gl.createBuffer();
gl.bindBuffer(gl.ARRAY_BUFFER, glVariable10);
gl.bufferData(gl.ARRAY_BUFFER, 64, gl.STATIC_DRAW);
gl.bufferSubData(gl.ARRAY_BUFFER, 0, new Float32Array([-1,-1,1,-1,-1,1,1,1]));
gl.bufferSubData(gl.ARRAY_BUFFER, 32, new Float32Array([0,0,1,0,0,1,1,1]));
const glVariable11 = gl.getAttribLocation(glVariable8, 'aPos');
gl.enableVertexAttribArray(glVariable11);
gl.vertexAttribPointer(glVariable11, 2, gl.FLOAT, false, 0, 0);
const glVariable12 = gl.getAttribLocation(glVariable8, 'aTexCoord');
gl.enableVertexAttribArray(glVariable12);
gl.vertexAttribPointer(glVariable12, 2, gl.FLOAT, false, glVariable12, 32);
gl.bindFramebuffer(gl.FRAMEBUFFER, glVariable9);
const glVariable13 = gl.createTexture();
gl.activeTexture(33985);
gl.bindTexture(gl.TEXTURE_2D, glVariable13);
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_S, gl.CLAMP_TO_EDGE);
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_T, gl.CLAMP_TO_EDGE);
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.NEAREST);
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, gl.NEAREST);
gl.texImage2D(gl.TEXTURE_2D, glVariable12, gl.RGBA32F, 2, 2, glVariable12, gl.RGBA, gl.FLOAT, null);
gl.framebufferTexture2D(gl.FRAMEBUFFER, gl.COLOR_ATTACHMENT0, gl.TEXTURE_2D, glVariable13, glVariable12);
gl.useProgram(glVariable8);
gl.scissor(glVariable12, glVariable12, 2, 2);
const glVariable14 = gl.getUniformLocation(glVariable8, 'uOutputDim');
gl.uniform3iv(glVariable14, new Int32Array([4,1,1]));
const glVariable15 = gl.getUniformLocation(glVariable8, 'uTexSize');
gl.uniform2iv(glVariable15, [2,2]);
const glVariable16 = gl.getUniformLocation(glVariable8, 'ratio');
gl.uniform2f(glVariable16, glVariable11, glVariable11);
const glVariable17 = gl.createTexture();
gl.activeTexture(gl.TEXTURE0);
gl.bindTexture(gl.TEXTURE_2D, glVariable17);
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_S, gl.CLAMP_TO_EDGE);
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_T, gl.CLAMP_TO_EDGE);
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.NEAREST);
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, gl.NEAREST);
gl.texImage2D(gl.TEXTURE_2D, glVariable12, gl.RGBA32F, 2, 2, glVariable12, gl.RGBA, gl.FLOAT, new Float32Array([1,2,3,4,0,0,0,0,0,0,0,0,0,0,0,0]));
const glVariable18 = gl.getUniformLocation(glVariable8, 'user_aDim');
gl.uniform3iv(glVariable18, new Int32Array([4,1,1]));
const glVariable19 = gl.getUniformLocation(glVariable8, 'user_aSize');
gl.uniform2iv(glVariable19, [2,2]);
const glVariable20 = gl.getUniformLocation(glVariable8, 'user_aBitRatio');
gl.uniform1i(glVariable20, glVariable11);
const glVariable21 = gl.getUniformLocation(glVariable8, 'user_a');
gl.uniform1i(glVariable21, glVariable12);
gl.bindFramebuffer(gl.FRAMEBUFFER, glVariable9);
gl.drawArrays(gl.TRIANGLE_STRIP, glVariable12, 4);
const glVariables22 = gl.getExtension('EXT_color_buffer_float');
const glVariables23 = gl.getExtension('OES_texture_float_linear');
const glVariables24 = gl.getExtension('EXT_color_buffer_float');
gl.enable(gl.SCISSOR_TEST);
gl.viewport(glVariable12, glVariable12, 2, 2);
const glVariable25 = gl.createShader(gl.VERTEX_SHADER);
gl.shaderSource(glVariable25, `#version 300 es
precision highp float;
precision highp int;
precision highp sampler2D;
in vec2 aPos;
in vec2 aTexCoord;
out vec2 vTexCoord;
uniform vec2 ratio;
void main(void) {
gl_Position = vec4((aPos + vec2(1)) * ratio + vec2(-1), 0, 1);
vTexCoord = aTexCoord;
}`);
gl.compileShader(glVariable25);
const glVariable26 = gl.createShader(gl.FRAGMENT_SHADER);
gl.shaderSource(glVariable26, `#version 300 es
precision highp float;
precision highp int;
precision highp sampler2D;
const int LOOP_MAX = 1000;
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));
}
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 user_x;
uniform highp ivec2 user_xSize;
uniform highp ivec3 user_xDim;
uniform highp int user_xBitRatio;
out vec4 data0;
float kernelResult = 0.0;
void kernel() {
kernelResult = get(user_x, user_xSize, user_xDim, user_xBitRatio, threadId.z, threadId.y, threadId.x);return;
}
void main(void) {
index = int(vTexCoord.s * float(uTexSize.x)) + int(vTexCoord.t * float(uTexSize.y)) * uTexSize.x;
index *= 4;
threadId = indexTo3D(index, uOutputDim);
kernel();
data0.r = kernelResult;
index += 1;
threadId = indexTo3D(index, uOutputDim);
kernel();
data0.g = kernelResult;
index += 1;
threadId = indexTo3D(index, uOutputDim);
kernel();
data0.b = kernelResult;
index += 1;
threadId = indexTo3D(index, uOutputDim);
kernel();
data0.a = kernelResult;
}`);
gl.compileShader(glVariable26);
const glVariable27 = gl.getShaderParameter(glVariable25, gl.COMPILE_STATUS);
const glVariable28 = gl.getShaderParameter(glVariable26, gl.COMPILE_STATUS);
const glVariable29 = gl.createProgram();
gl.attachShader(glVariable29, glVariable25);
gl.attachShader(glVariable29, glVariable26);
gl.linkProgram(glVariable29);
const glVariable30 = gl.createFramebuffer();
const glVariable31 = gl.createBuffer();
gl.bindBuffer(gl.ARRAY_BUFFER, glVariable31);
gl.bufferData(gl.ARRAY_BUFFER, 64, gl.STATIC_DRAW);
gl.bufferSubData(gl.ARRAY_BUFFER, glVariable12, new Float32Array([-1,-1,1,-1,-1,1,1,1]));
gl.bufferSubData(gl.ARRAY_BUFFER, 32, new Float32Array([0,0,1,0,0,1,1,1]));
const glVariable32 = gl.getAttribLocation(glVariable29, 'aPos');
gl.enableVertexAttribArray(glVariable11);
gl.vertexAttribPointer(glVariable11, 2, gl.FLOAT, false, glVariable12, glVariable12);
const glVariable33 = gl.getAttribLocation(glVariable29, 'aTexCoord');
gl.enableVertexAttribArray(glVariable12);
gl.vertexAttribPointer(glVariable12, 2, gl.FLOAT, false, glVariable12, 32);
gl.bindFramebuffer(gl.FRAMEBUFFER, glVariable30);
const glVariable34 = gl.createTexture();
gl.activeTexture(33985);
gl.bindTexture(gl.TEXTURE_2D, glVariable34);
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_S, gl.CLAMP_TO_EDGE);
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_T, gl.CLAMP_TO_EDGE);
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.NEAREST);
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, gl.NEAREST);
gl.texImage2D(gl.TEXTURE_2D, glVariable12, gl.RGBA32F, 2, 2, glVariable12, gl.RGBA, gl.FLOAT, null);
gl.framebufferTexture2D(gl.FRAMEBUFFER, gl.COLOR_ATTACHMENT0, gl.TEXTURE_2D, glVariable34, glVariable12);
gl.useProgram(glVariable29);
gl.scissor(glVariable12, glVariable12, 2, 2);
const glVariable35 = gl.getUniformLocation(glVariable29, 'uOutputDim');
gl.uniform3iv(glVariable35, new Int32Array([4,1,1]));
const glVariable36 = gl.getUniformLocation(glVariable29, 'uTexSize');
gl.uniform2iv(glVariable36, [2,2]);
const glVariable37 = gl.getUniformLocation(glVariable29, 'ratio');
gl.uniform2f(glVariable37, glVariable11, glVariable11);
const glVariable38 = gl.createTexture();
gl.activeTexture(gl.TEXTURE0);
gl.bindTexture(gl.TEXTURE_2D, glVariable13);
const glVariable39 = gl.getUniformLocation(glVariable29, 'user_xDim');
gl.uniform3iv(glVariable39, [4,1,1]);
const glVariable40 = gl.getUniformLocation(glVariable29, 'user_xSize');
gl.uniform2iv(glVariable40, [2,2]);
const glVariable41 = gl.getUniformLocation(glVariable29, 'user_xBitRatio');
gl.uniform1i(glVariable41, glVariable11);
const glVariable42 = gl.getUniformLocation(glVariable29, 'user_x');
gl.uniform1i(glVariable42, glVariable12);
gl.bindFramebuffer(gl.FRAMEBUFFER, glVariable30);
gl.drawArrays(gl.TRIANGLE_STRIP, glVariable12, 4);
const glVariable43 = new Float32Array(8);
gl.readPixels(0, 0, 2, 1, gl.RGBA, gl.FLOAT, glVariable43);
gl.getError();
console.log(glVariable43);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment