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

Suggestion for using non-linear sRGB in Shader #11

Open TerryYoung518 opened 1 year ago

TerryYoung518 commented 1 year ago

This pertains to the BlendingMethod option SrgbOff for an OBS source.

When SrgbOff is enabled for an OBS source, it skips pre-multiplication, which involves multiplying the RGB values with the alpha channel (default background is black). However, after applying the shader filter, the pre-multiplication is reintroduced. To address this, I used a "pre-division function" to offset the pre-multiplication, which worked as expected.

To implement this, include the following function outside the image.Sample():

float4 fix_alpha(float4 rgba)
{
  float r = min(rgba.r / rgba.a, 1);
  float g = min(rgba.g / rgba.a, 1);
  float b = min(rgba.b / rgba.a, 1);
  return float4(r, g, b, rgba.a);
}

The code should be written as follows: fix_alpha(image.Sample(...)). Regardless of whether BlendingMethod is set to SrgbOff or Normal, the filter will work as expected.