WebGL从浮点渲染目标中读取像素

use*_*435 10 webgl

例如,在WebGL中渲染浮点纹理的支持级别方面存在一些混淆.OES_texture_float扩展似乎并不强制要求它,因为可选支持FLOAT纹理作为FBO附件(不推荐使用),但看起来有些供应商继续实施它.因此,我的基本理解是渲染到浮点纹理实际上可以在非ES桌面环境中工作.我无法直接从浮点渲染目标中读取.

我的问题是,是否有一种方法可以使用WebGLContext :: readPixels()调用和Float32Array目标从浮点纹理中读取?提前致谢.

附件是一个成功读取字节纹理的脚本,但浮动纹理失败:

<html>
<head>
<script>
function run_test(use_float) {
    // Create canvas and context
    var canvas = document.createElement('canvas');
    document.body.appendChild(canvas);
    var gl = canvas.getContext("experimental-webgl");

    // Decide on types to user for texture
    var texType, bufferFmt;
    if (use_float) {
        texType = gl.FLOAT;
        bufferFmt = Float32Array;
    } else {
        texType = gl.UNSIGNED_BYTE;
        bufferFmt = Uint8Array;
    }

    // Query extension
    var OES_texture_float = gl.getExtension('OES_texture_float');
    if (!OES_texture_float) {
        throw new Error("No support for OES_texture_float");
    }

    // Clear
    gl.viewport(0, 0, canvas.width, canvas.height);
    gl.clearColor(1.0, 0.0, 0.0, 1.0);
    gl.clear(gl.COLOR_BUFFER_BIT);

    // Create texture
    var texture = gl.createTexture();
    gl.bindTexture(gl.TEXTURE_2D, texture);
    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.texImage2D(gl.TEXTURE_2D, 0, gl.RGBA, 512, 512, 0, gl.RGBA, texType, null);

    // Create and attach frame buffer
    var fbo = gl.createFramebuffer();
    gl.bindFramebuffer(gl.FRAMEBUFFER, fbo);
    gl.framebufferTexture2D(gl.FRAMEBUFFER, gl.COLOR_ATTACHMENT0, gl.TEXTURE_2D, texture, 0);
    gl.bindTexture(gl.TEXTURE_2D, null);
    if (gl.checkFramebufferStatus(gl.FRAMEBUFFER) != gl.FRAMEBUFFER_COMPLETE) {
        throw new Error("gl.checkFramebufferStatus(gl.FRAMEBUFFER) != gl.FRAMEBUFFER_COMPLETE");
    }

    // Clear
    gl.viewport(0, 0, 512, 512);
    gl.clear(gl.COLOR_BUFFER_BIT);
    var pixels = new bufferFmt(4 * 512 * 512);
    gl.readPixels(0, 0, 512, 512, gl.RGBA, texType, pixels);

    if (pixels[0] !== (use_float ? 1.0 : 255)) {
        throw new Error("pixels[0] === " + pixels[0].toString());
    }
}

function main() {
    run_test(false);
    console.log('Test passed using GL_UNSIGNED_BYTE');
    run_test(true);
    console.log('Test passed using GL_FLOAT');
}
</script>
</head>
<body onload='main()'>
</body>
</html>
Run Code Online (Sandbox Code Playgroud)

Adr*_*ley 11

不幸的是,将RGBA组件作为字节读出似乎仍然是WebGL的唯一方法.如果需要将浮点数编码为像素值,可以使用以下命令:

在你的分形着色器(GLSL/HLSL)中:

float shift_right (float v, float amt) { 
    v = floor(v) + 0.5; 
    return floor(v / exp2(amt)); 
}
float shift_left (float v, float amt) { 
    return floor(v * exp2(amt) + 0.5); 
}
float mask_last (float v, float bits) { 
    return mod(v, shift_left(1.0, bits)); 
}
float extract_bits (float num, float from, float to) { 
    from = floor(from + 0.5); to = floor(to + 0.5); 
    return mask_last(shift_right(num, from), to - from); 
}
vec4 encode_float (float val) { 
    if (val == 0.0) return vec4(0, 0, 0, 0); 
    float sign = val > 0.0 ? 0.0 : 1.0; 
    val = abs(val); 
    float exponent = floor(log2(val)); 
    float biased_exponent = exponent + 127.0; 
    float fraction = ((val / exp2(exponent)) - 1.0) * 8388608.0; 
    float t = biased_exponent / 2.0; 
    float last_bit_of_biased_exponent = fract(t) * 2.0; 
    float remaining_bits_of_biased_exponent = floor(t); 
    float byte4 = extract_bits(fraction, 0.0, 8.0) / 255.0; 
    float byte3 = extract_bits(fraction, 8.0, 16.0) / 255.0; 
    float byte2 = (last_bit_of_biased_exponent * 128.0 + extract_bits(fraction, 16.0, 23.0)) / 255.0; 
    float byte1 = (sign * 128.0 + remaining_bits_of_biased_exponent) / 255.0; 
    return vec4(byte4, byte3, byte2, byte1); 
}

 // (the following inside main(){}) return your float as the fragment color
 float myFloat = 420.420;
 gl_FragColor = encode_float(myFloat);
Run Code Online (Sandbox Code Playgroud)

然后回到JavaScript端,在进行绘制调用之后,您可以使用以下内容提取每个像素的编码浮点值:

var pixels = new Uint8Array(CANVAS.width * CANVAS.height * 4);
gl.readPixels(0, 0, CANVAS.width, CANVAS.height, gl.RGBA, gl.UNSIGNED_BYTE, pixels);
pixels = new Float32Array(pixels.buffer);

// pixels now contains an array of floats, 1 float for each pixel
Run Code Online (Sandbox Code Playgroud)


nra*_*aud 8

我正在添加我最近的发现:Chrome将让您阅读浮点数,作为实现定义格式的一部分(在规范中搜索"readPixels"),Firefox实现WEBGL_color_buffer_float扩展,因此您只需加载扩展并读取您的浮点数,我无法通过Safari阅读花车.

  • 非常感谢这个评论.我在chrome中看到我可以调用`gl.getContext("experimental-webgl")`然后调用`gl.readPixels(0,0,width,height,gl.RGB,gl.FLOAT,buf)`浮点FBO绑定,其中`buf`是一个`Float32Array`.但是如何在firefox中使用扩展?我可以调用`gl.getExtension("WEBGL_color_buffer_float")`,它返回一个`WEBGL_color_buffer_float {}`但是我怎么办呢? (2认同)
  • FWIW,规范的1.0.3版本[链接在上方](https://www.khronos.org/registry/webgl/specs/1.0.3/#5.14.12)并未提及“ FLOAT”,但[(WebGL1的最新版本)具有](https://www.khronos.org/registry/webgl/specs/latest/1.0/#5.14.12)。 (2认同)

Mor*_*bel 6

readPixels仅限于RGBA格式和UNSIGNED_BYTE类型(WebGL规范).但是有一些方法可以将浮动"打包"到RGBA/UNSIGNED_BYTE中:

http://concord-consortium.github.io/lab/experiments/webgl-gpgpu/webgl.html