Skip to content

Truly independent web browser

3.8 KB
Raw Blame
<!DOCTYPE html>
<script src="../include.js"></script>
<script>
    // Uploads a 3x2 canvas, whose rows aren't a multiple of 4 bytes long in these formats, and reads back what sampling
    // the texture gives for its first and second rows.
    function sampledTexels(gl, format, source) {
        const texture = gl.createTexture();
        gl.bindTexture(gl.TEXTURE_2D, texture);
        gl.texImage2D(gl.TEXTURE_2D, 0, format, format, gl.UNSIGNED_BYTE, source);
        const error = gl.getError();
        gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.NEAREST);
        gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, gl.NEAREST);
        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.clearColor(0, 0, 0, 0);
        gl.clear(gl.COLOR_BUFFER_BIT);
        gl.drawArrays(gl.TRIANGLE_STRIP, 0, 4);

        const pixels = new Uint8Array(3 * 2 * 4);
        gl.readPixels(0, 0, 3, 2, gl.RGBA, gl.UNSIGNED_BYTE, pixels);
        const texel = index => Array.from(pixels.slice(index * 4, index * 4 + 4)).join(",");
        // The texture's first row, the canvas's top row, is the framebuffer's bottom row, which readPixels() reads first.
        return `error ${error} | first row ${texel(0)} | second row ${texel(3)}`;
    }

    test(() => {
        const source = document.createElement("canvas");
        source.width = 3;
        source.height = 2;
        const context = source.getContext("2d");
        context.fillStyle = "rgb(200, 100, 50)";
        context.fillRect(0, 0, 3, 1);
        context.fillStyle = "rgb(10, 20, 30)";
        context.fillRect(0, 1, 3, 1);

        const canvas = document.createElement("canvas");
        canvas.width = 3;
        canvas.height = 2;
        const gl = canvas.getContext("webgl");

        const target = gl.createTexture();
        gl.bindTexture(gl.TEXTURE_2D, target);
        gl.texImage2D(gl.TEXTURE_2D, 0, gl.RGBA, 3, 2, 0, gl.RGBA, gl.UNSIGNED_BYTE, null);
        gl.bindFramebuffer(gl.FRAMEBUFFER, gl.createFramebuffer());
        gl.framebufferTexture2D(gl.FRAMEBUFFER, gl.COLOR_ATTACHMENT0, gl.TEXTURE_2D, target, 0);
        gl.viewport(0, 0, 3, 2);

        const vertexShader = gl.createShader(gl.VERTEX_SHADER);
        gl.shaderSource(vertexShader, "attribute vec2 position; varying vec2 coordinate; void main() { coordinate = (position + 1.0) / 2.0; gl_Position = vec4(position, 0, 1); }");
        gl.compileShader(vertexShader);
        const fragmentShader = gl.createShader(gl.FRAGMENT_SHADER);
        gl.shaderSource(fragmentShader, "precision mediump float; varying vec2 coordinate; uniform sampler2D source; void main() { gl_FragColor = texture2D(source, coordinate); }");
        gl.compileShader(fragmentShader);
        const program = gl.createProgram();
        gl.attachShader(program, vertexShader);
        gl.attachShader(program, fragmentShader);
        gl.linkProgram(program);
        gl.useProgram(program);

        gl.bindBuffer(gl.ARRAY_BUFFER, gl.createBuffer());
        gl.bufferData(gl.ARRAY_BUFFER, new Float32Array([-1, -1, 1, -1, -1, 1, 1, 1]), gl.STATIC_DRAW);
        const position = gl.getAttribLocation(program, "position");
        gl.enableVertexAttribArray(position);
        gl.vertexAttribPointer(position, 2, gl.FLOAT, false, 0, 0);

        for (const alignment of [4, 8]) {
            gl.pixelStorei(gl.UNPACK_ALIGNMENT, alignment);
            println(`UNPACK_ALIGNMENT ${alignment}:`);
            for (const [name, format] of [["RGB", gl.RGB], ["LUMINANCE", gl.LUMINANCE], ["LUMINANCE_ALPHA", gl.LUMINANCE_ALPHA], ["ALPHA", gl.ALPHA]])
                println(`    ${name}: ${sampledTexels(gl, format, source)}`);
            println(`    UNPACK_ALIGNMENT afterward: ${gl.getParameter(gl.UNPACK_ALIGNMENT)}`);
        }
    });
</script>