Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

HDRP Postprocessing not respecting set textures

Discussion in 'High Definition Render Pipeline' started by GoGoGadget, Jun 10, 2020.

  1. GoGoGadget

    GoGoGadget

    Joined:
    Sep 23, 2013
    Posts:
    864
    Hi all,

    Struggling quite a bit with a custom postprocessing effect, trying to get my HDRP shader to ever actually receive the texture that I'm setting in it.

    HDRP 7.3/Unity 2019.3.

    Currently my pseudocode looks like this:
    1. Blit from source into a temp RT handle (this works)
    2. Repeat (1) a few times for a mip pyramid, using each previous temp RT handle as the source (this works)
    3. Then, I am trying to use all 3 (or even just 1 to get it working) of the temporary RTs from above in a final pass. This does not work. I have tried setting them via the MaterialPropertyBlock, tried setting them directly on the material, weirdly, the shader does get the texture parameters (ie. the size of the texture - not just Unity default texture2D) but not any colour info
    Any help would be greatly appreciated!

    Frame debugger:


    Closest example effect I'm trying to emulate: https://github.com/keijiro/Kino/blo...eijiro.kino.post-processing/Runtime/Streak.cs

    Relevant code - note that even source does not appear in the frame debugger as a real texture - even though this exact code a few lines and a few passes above does 'work' in setting the source texture.
    Code (CSharp):
    1.         _prop.SetTexture(ShaderIDs.BloomTex1, mipOne);
    2.  
    3.         m_Material.SetTexture("_SourceRT", source);
    4.         _prop.SetTexture("_SourceRT", source);
    5.  
    6.         var mipFinal = m_Pool.Get(new Vector2(scaleW * 2f, scaleH * 2f), m_ColorFormat);
    7.         HDUtils.DrawFullScreen(cmd, m_Material, mipFinal, _prop, 4);
     
    Last edited: Jun 11, 2020
  2. GoGoGadget

    GoGoGadget

    Joined:
    Sep 23, 2013
    Posts:
    864
    Alternately, has anyone come across any non-keijiro examples of HDRP postprocessing scripts/shaders outside of the main stack? Still struggling to get this to work as designed here.
     
  3. Lad-Ty

    Lad-Ty

    Joined:
    May 7, 2013
    Posts:
    60
    Hopefully you have it working now? But anyway I'll try responding since it might help someone, and this thread popped on Google during many of my searches for very similar issues in postprocess writing for HDRP... Though I must say I got my stuff working only thanks to Keijiro's repo actually. And to be honest, I'm not sure why and what since it doesn't sound very intuitive, just that it works, I'll very happily read about how other people here do it. Talking about the sampling part mainly.

    What works for me is:

    The .cs side:
    Code (CSharp):
    1. public override void Render(CommandBuffer cmd, HDCamera camera, RTHandle source, RTHandle destination) {
    2.     ...
    3.     propertyBlock.Clear();
    4.     propertyBlock.SetTexture("_SourceTexture", source);            // passing source
    5.     propertyBlock.SetTexture("_InputTexture", rth0);        // passing custom created RTHandle
    6.     ...
    7.     HDUtils.DrawFullScreen(cmd, material, destination, propertyBlock, 0);        // render using my material and my properties
    8. }
    // I believe even material.SetTexture("...", ...); worked for me, and the issue was the shader part/sampling the textures. Though I'm going with the propertyBlock approach here

    the .shader side:
    Code (CSharp):
    1. TEXTURE2D_X(_SourceTexture);
    2. TEXTURE2D(_InputTexture);
    3. ...
    4. /* getting the source color -> from the pre post process texture */
    5. float4 GetSourceColor(float2 positionSS) {
    6.     return LOAD_TEXTURE2D_X(_SourceTexture, positionSS).rgba;
    7. }
    8. /* getting the color in my "input texture" */
    9. float4 GetInputColor(float2 uv) {
    10.     return SAMPLE_TEXTURE2D(_InputTexture, s_linear_clamp_sampler, uv).rgba;
    11. }
    Key for me was that I can't use "TEXTURE2D_X" for custom input texture (that I could somewhat understand), but also that I can't use "LOAD_TEXTURE2D" but have to use "SAMPLE_TEXTURE2D" for some reason.

    Also be careful with the SAMPLE_TEXTURE2D, in case of using RTHandle of non-native resolution, when converting from positionSS, you have to multiply by "_RTHandleScale.xy" or something like that.
     
    Last edited: Jul 15, 2020
    jjbish likes this.
  4. GoGoGadget

    GoGoGadget

    Joined:
    Sep 23, 2013
    Posts:
    864
    Thanks for the reply! I did end up landing on something similar, but using tex2D, which is a bit of a throwback but works as reliably as it did in previous PostPro versions. I also found that any of the TEXTURE2D_X's and LOAD_TEXTUREs didn't work.