<script id="hist-vs" type="not-js">
#version 300 es
uniform highp usampler2D u_texture;
uniform uvec4 u_colorMult;
void main() {
const int mipLevel = 0;
ivec2 size = textureSize(u_texture, mipLevel);
// based on an id (0, 1, 2, 3 ...) compute the pixel x, y for the source image
ivec2 pixel = ivec2(
gl_VertexID % size.x,
gl_VertexID / size.x);
// get the pixels but 0 out channels we don't want
uvec4 color = texelFetch(u_texture, pixel, mipLevel) * u_colorMult;
// add up all the channels. Since 3 are zeroed out we'll get just one channel
uint colorSum = color.r + color.g + color.b + color.a;
// set the position to be over a single pixel in the 256x256 destination texture
uvec2 pos = uvec2(
colorSum % 256u,
colorSum / 256u);
gl_Position = vec4(((vec2(pos) + 0.5) / 256.0) * 2.0 - 1.0, 0, 1);
gl_PointSize = 1.0;
}
</script>
<script id="hist-fs" type="not-js">
#version 300 es
precision highp float;
out vec4 color;
void main() {
color = vec4(1);
}
</script>
<script id="max-fs" type="not-js">
#version 300 es
precision mediump float;
uniform sampler2D u_texture;
out vec4 outColor;
void main() {
vec4 maxColor = vec4(0);
// we know the texture is 256x256 so just go over the whole thing
for (int y = 0; y < 256; ++y) {
for (int x = 0; x < 256; ++x) {
ivec2 uv = ivec2(x, y);
// get max value of pixel
maxColor = max(maxColor, texelFetch(u_texture, uv, 0));
}
}
outColor = maxColor;
}
</script>
<script id="show-vs" type="not-js">
#version 300 es
in vec4 position;
void main() {
gl_Position = position;
}
</script>
<script id="show-fs" type="not-js">
#version 300 es
precision mediump float;
uniform sampler2D u_histTexture;
uniform vec2 u_resolution;
uniform sampler2D u_maxTexture;
out vec4 outColor;
void main() {
// get the max color constants
vec4 maxColor = texture(u_maxTexture, vec2(0));
// compute a UV 0 to 1 (only)
vec2 uv = floor(gl_FragCoord.xy) /...