gpujs / gpu.js

GPU Accelerated JavaScript
https://gpu.rocks
MIT License
15.1k stars 652 forks source link

Graphical alpha not working #643

Closed harshkhandeparkar closed 3 years ago

harshkhandeparkar commented 3 years ago

A GIF or MEME to give some spice of the internet

What is wrong?

image

This kernel should ideally display nothing since the alpha is 0, but it displays this: image

Where does it happen?

I was trying to change something in gpujs-real-renderer

How do we replicate the issue?

  1. You can see the kernel above

How important is this (1-5)?

5.5555555555.......

Expected behavior (i.e. solution)

Other Comments

midnight-dev commented 3 years ago

So the buffer gets the correct RGB (1, 0, 1) but it shouldn't be the final color due to the 0 alpha. I wonder if frame buffers actually hold data like this in WebGL. I haven't gotten that close to the metal of the spec to be honest. Is it possible that it held that as the clear color and the alpha produced what WebGL thought was "clear?"

Also, does this have any effect on something you're developing? Just curious. I'm not likely to be the one who fixes this, but it's good to know limitations of necessary evils when running calculations.

harshkhandeparkar commented 3 years ago

Well, the gpu.js API supports alpha and WebGL probably does, too. If it isn't supported, why is it even there? It doesn't really concern what I am developing, I was just experimenting with alpha values to see if I can make interpolation smoother. Now I have realized that changing the alpha will do nothing :joy:

midnight-dev commented 3 years ago

I'm not saying that it doesn't support it. I'm just thinking out loud that when gpu.js gets a frame that finalizes to be 100% clear, it may use the previously computed color as the "clear" value so it doesn't have to write new data to the buffer. I'm not sure if that's how it's operating or if that's part of the spec. Does 100% opaque color still get written out if a partial alpha is used, like 0.1 or 0.5?

@robertleeplummerjr Can you offer any insight here?

harshkhandeparkar commented 3 years ago

Yes, partial alpha also gives opaque colors.

midnight-dev commented 3 years ago

Does this behavior change at all when you force premultiplied alpha for the GPU's context? Something like this:

const canvas = DOM.canvas(500, 500);
const gl = canvas.getContext('webgl2', { premultipliedAlpha: false });

const gpu = new GPU({
  canvas,
  context: gl
});

... where you'd want to specify premultipliedAlpha. This snippet was hiding in the readme, by the way, I didn't write it. If that doesn't do it, be prepared to crack open the fragment shader code. It'll have to be something in there.

harshkhandeparkar commented 3 years ago

Hmm. GLSL is headache...

harshkhandeparkar commented 3 years ago

My local dev env is filled with many other changes so I can't even test this rn.

harshkhandeparkar commented 3 years ago

Yes, premultiplied alpha is working. 1) That needs better documentation 2) It is still quite difficult to use

But I don't really need alpha anymore ¯\_(ツ)_/¯

midnight-dev commented 3 years ago

Yeah, I agree with both points. Seems fine if you already know it, I'm sure, but sometimes you won't know unless you try it... The more of a pain it is to try something new, the fewer people will attempt it. It feels like this could be abstracted to a cleaner solution.