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

Question Problem when trying to sample a texture twice in the same pass (Bug?)

Discussion in 'High Definition Render Pipeline' started by ERedXIII, Mar 5, 2020.

  1. ERedXIII

    ERedXIII

    Joined:
    Sep 30, 2015
    Posts:
    4
    Hello,

    I am fairly new to Unity and HDRP so maybe this is not a bug but a missunderstanding of something from my side.
    The thing is, as stated in the title, that when I try to sample a texture twice I get different results. The first time I just get the same image in the screen, but the second time some of the objects are transparent or missing.
    The idea I had in mind is to do a manual bloom kawase effect so I'm using Custom Passes and I need to sample the texture 4 times at different locations for each pass to get the wide blur.
    I have checked the outline customPass example where the texture is being sampled twice for neighbourhood checking and the outline seems to work OK.
    I have slightly changed the code to have the outline effect happening only in the top half of the screen and for the bottom hal (in order to reproduce the weird behaviour) I am sampling the texture again at the regular coordinates (same as first time) and I get weird results as well.
    I have changed from the original outline shader the code from if (Luminance...) onwards.
    Here is the original(under Scripting example: Outline Pass):
    https://docs.unity3d.com/Packages/c...s.high-definition@7.2/manual/Custom-Pass.html

    And here is the snippet I have changed to be able to reproduce the error:
    Code (csharp):
    1.  
    2. [hlsl] // don't know how to embed shader code
    3. if (Luminance(outline.rgb) < luminanceThreshold & posInput.positionNDC.y<0.5) //& posInput.positionNDC.y>0.5
    4.         {
    5.             for (int i = 0; i < MAXSAMPLES; i++)
    6.             {
    7.                 float2 uvN = uv + _ScreenSize.zw * _RTHandleScale.xy * samplingPositions[i];
    8.                 float4 neighbour = SAMPLE_TEXTURE2D_X_LOD(_OutlineBuffer, s_linear_clamp_sampler, uvN, 0);
    9.  
    10.                 if (Luminance(neighbour) > luminanceThreshold)
    11.                 {
    12.                     outline.rgb = _OutlineColor.rgb;
    13.                     outline.a = 1;
    14.                     //break;
    15.                     return outline;
    16.                 }
    17.             }
    18.         }
    19.         float4 new_sample = SAMPLE_TEXTURE2D_X_LOD(_OutlineBuffer, s_linear_clamp_sampler, uv, 0);
    20.         return new_sample;
    21.  
    Here you can see the original outline custom pass effect: outline_ok.PNG
    And here the effect happening only half screen with sampling only once working ok:
    outline_half_ok.PNG
    And then when I sample twice as in the previous snippet:
    outline_half_weird.PNG
    Here as you can see, the sky is black. In my game the results are even more extreme with (as I already said) some objects not whowing up and some faces being transparent.

    Hope someone can help me figure out what I am doing wrong or if it's a bug, how should I report it?

    Thanks in advanced