exeldro / obs-shaderfilter

OBS Studio filter for applying an arbitrary shader to a source.
GNU General Public License v2.0
377 stars 39 forks source link

Bugfix/fix premultiplied alpha #36

Closed FiniteSingularity closed 8 months ago

FiniteSingularity commented 8 months ago

In the rendering pipeline, OBS passes color values with pre-multiplied alpha to filters. This can cause issues with custom shaders, if the input source has intermediate (translucent) alpha values, and causes the colors of the source to darken in the shader.

This PR changes the rendering process of shaderfilter to first grab the input texture using OBS's DrawAlphaDivide pass-through shader to undo the alpha pre-multiplication. This new input texture is then passed through the custom shader, and finally is rendered out to the source chain with a new draw_output function.

Since there are existing custom shaders where users have correct for pre-multiplied alpha in their shader, there is also a new pre-processor macro: #define USE_PM_ALPHA 1 that turns off this correction. For those shaders that have internally corrected, this will be a breaking change, as they will need to either remove the code that corrects for pre-multiplied alpha, or they will need to add #define USE_PM_ALPHA 1 near the top of their shader file.

Finally, this PR updates the readme file to give some documentation on the basic pre-processor macros available: #include, #define, and #define USE_PM_ALPHA 1.

FiniteSingularity commented 8 months ago

This PR should fix #11 and #30

FiniteSingularity commented 8 months ago

Also, you can test the difference in color. Grab a source, apply a color correction to it and set the alpha to 0.5. Then after the color correction filter, add a user defined shader (with just the default passthrough shader). The colors should remain unchanged (this is the new behavior).

Now, at the top of your user defined shader text, add #define USE_PM_ALPHA 1. You should notice the colors darken a bit. This is the old behavior.