Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Q]Draw to screen works good but FBO not. #646

Open
budblack opened this issue Mar 20, 2022 · 3 comments
Open

[Q]Draw to screen works good but FBO not. #646

budblack opened this issue Mar 20, 2022 · 3 comments

Comments

@budblack
Copy link

Problem

When I use regl command with a simple shader code, and set a framebuffer property.The result pixel always filled with ZERO.

I think there's sth went wrong in my code.

  async testReglFbo() {
    const { ctx, service } = this;
    const { tile } = service;
    const width = 256, height = 256, channels = 4;
    const gl = tile.createGl(width, height)
    const regl = tile.createRegl(gl);

    /** ************************************************
     *  Do not care about the above codes.
     ***************************************************/
    const fbo = regl.framebuffer({ width, height });
    const cmd = regl({
      // ONLY IF I DON'T SET `framebuffer` PROPERTY, I CAN GET THE PIXELS.
      framebuffer: fbo,
      vert: `
        attribute vec2 position;
        void main() {
            gl_Position = vec4(position, 0, 1);
        }
      `,
      frag: `
        void main() {
          gl_FragColor = vec4(1, 0, 0, 1);
        }
      `,
      attributes: {
        position: [
          [-.9, -.9],
          [-.9, .9],
          [.9, -.9]
        ],
      },
      viewport: { x: 0, y: 0, width, height, },
      count: 3,
      primitive: 'triangles'
    });
    const pixels: Uint8Array = await new Promise(resolve => {
      cmd({}, () => {
        regl.draw();
        // ONLY IF I DON'T SET `framebuffer` PROPERTY, I CAN GET THE PIXELS.
        const pixels = regl.read();
        resolve(pixels);
      });
    });
    /** ************************************************
     *  Do not care about the following codes.
     ***************************************************/
    const sp = sharp(pixels, { raw: { width, height, channels } });
    const data = await sp.clone().png().toBuffer();
    ctx.set({ 'Content-Type': "image/png" });
    ctx.body = data;
  }

With FBO

testReglFbo

Without FBO

testReglFbo (1)

@budblack
Copy link
Author

I want to read the fbo data. Should I call function regl.read() in a command call situation?
Or there is some mistake in my code cause fbo not be filled?

@budblack budblack reopened this Mar 20, 2022
@budblack
Copy link
Author

OMG, works if I set depth: false of fbo.

const fbo = regl.framebuffer({ width, height, depth: false });

BUT WHY?

@ltzehan
Copy link

ltzehan commented May 17, 2024

Just as an FYI for anyone with the same issue: when depth: false, there is no depth attachment and all fragments will pass the depth test so this appears to work until more complex geometries come into the picture.

The issue here is that when the depth buffer is enabled, the default value of 0 is already representing an extremely close depth so no fragments are rendered

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants