Weird behavior over multiple shaders, works inside single shader

I’m having a weird problem that I haven’t been able to figure out. I have a fairly complex post process routine and everything is working well except for a premultiply, unpremultiply section.

the logic is simple:

premult:
outputcolor.rgb *= outputcolor.a;

unpremult:
outputcolor.rgb /= outputcolor.a;

IF i put he logic in the same shader, the outputcolor returns with the same value it went in with. However, if i put the logic in two seperate shaders (do the premult, do the unpremult on the result) - parts of the image are completely blown out color wise - if the alpha is close to 0.

My rendertextures are argb32, 0 depth, etc.

If anyone has any idea why separating the same logic breaks instead of keeping it in one shader i would really appreciate it. The same code works outside of unity in native GL and DirectX having the steps in 2 separate shaders.

Are you using linear or gamma lighting?

Linear will affect the values of the sampled textures, unless you tell it not to.

That was it! Although it was on gamma, setting it to linear fixed it…When you say “tell it not too” what exactly does that mean? I would like for it to be functional on both linear and gamma.

Thanks again for your help

Select your texture and change the type to Advanced, then tick “Bypass sRGB”.

Or for a render texture you’ve generated in code;
myRenderTexture.sRGB = false;

RenderTexture.sRGB is read-only. To change it, you need to create a new RenderTexture and set its readwrite parameter.

Ah, my bad. I just checked through the script reference, not used RenderTextures much :stuck_out_tongue:

Don’t worry about it, this is called a community for a reason ;). There’s bound to be someone who does nothing but working with RenderTextures :slight_smile:

Actually, I think it would be better if it were mentioned in the documentation. I submitted a documentation bug report for it, so perhaps they’ll update the documentation if and when they have time for it.
[Edit] I got a positive reply back from Unity QA.