Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

HDRP Post Processing shaders - accessing GBuffers

Discussion in 'Graphics Experimental Previews' started by nehvaleem, Aug 11, 2019.

  1. nehvaleem

    nehvaleem

    Joined:
    Dec 13, 2012
    Posts:
    382
    Hi,
    It will probably be a long post so for starters here is some sort tl;dr:

    So far I was able to fork HDRP and plug it back to unity, add some code to embedded post processing system to do some sort of shader work as in FinalPass.

    I've created new material using
    Code (CSharp):
    1. CoreUtils.CreateEngineMaterial(m_Resources.shaders.outlineShader);
    And then, after all of the built-in post processing (but before FinalPass call) I am using it with:
    Code (CSharp):
    1. HDUtils.DrawFullScreen(cmd, m_OutlineMaterial, destination);
    Shader seems to work just. I am able to access screen depth using LoadCameraDepth(input.vertex.xy);

    Here comes the part that I don't understand at all. In my shader, that is working fine in 2018.3 (hdrp 4.X) I've got something like this:

    Code (CSharp):
    1.  
    2. TEXTURE2D_SAMPLER2D(_MainTex, sampler_MainTex);
    3. TEXTURE2D_SAMPLER2D(_GBufferTexture0, sampler_GBufferTexture0);
    4. TEXTURE2D_SAMPLER2D(_GBufferTexture1, sampler_GBufferTexture1);
    5. TEXTURE2D_SAMPLER2D(_GBufferTexture2, sampler_GBufferTexture2);
    6. TEXTURE2D_SAMPLER2D(_GBufferTexture3, sampler_GBufferTexture3);
    7. TEXTURE2D_SAMPLER2D(_CameraDepthTexture, sampler_CameraDepthTexture);
    8. TEXTURE2D_SAMPLER2D(_NormalBufferTexture, sampler_NormalBufferTexture);
    9.  
    And all of those were accessible. By reading some source code of current hdrp I've noticed that some of macros has changed and now I have to declare textures and samplers separately with TEXTURE2D_X and SAMPLER. Despite that no matter how I would try to access gbuffer I am getting unexpected results - as grey output for example.

    In Lit.hlsl there is something about accessing textures in gbuffer but when I am trying to do the same in my shader I've got compilation errors with missing functions/structs no matter what sources I include.

    I've also searched answers in Debug sources, especially in DebugFullScreen.shader. There was something like this:

    Code (CSharp):
    1.  
    2. float depth = LoadCameraDepth(input.positionCS.xy);
    3. PositionInputs posInput = GetPositionInput(input.positionCS.xy, _ScreenSize.zw, depth, UNITY_MATRIX_I_VP, UNITY_MATRIX_V);
    4.  
    5. BSDFData bsdfData;
    6. BuiltinData builtinData;
    7. DECODE_FROM_GBUFFER(posInput.positionSS, UINT_MAX, bsdfData, builtinData);
    8.  
    But no matter what sources I include (even the same as in shader source in correct order) I am getting error that BSDFData cannot be resolved.

    Questions:
    1. Am I even on the correct path to implement my custom post processing effects by using HDUtils.DrawFullScreen() method?
    2. Where can I find info about macros in hdrp? For example I would like to know what are the differences between TEXTURE2D and TEXTURE2D_X or what does LOAD_TEXTURE2D_X do and so on? Currently I am just making some not to good assumptions and work mainly by doing mistakes and learning from them.
    3. Why can't I use BSDFData and what it contains?
    4. What should I do to be able to access all of the gbuffer textures? Is it something wrong with the way that I am attempting to do my post process effect?


    It would be awesome if someone could shed some light on my issue. I am very new to shaders at all and how event hdrp works.
     
    Delphic_ likes this.
  2. rz_0lento

    rz_0lento

    Joined:
    Oct 8, 2013
    Posts:
    2,361
  3. SebLagarde

    SebLagarde

    Unity Technologies

    Joined:
    Dec 30, 2015
    Posts:
    928
    Hi,

    it is not DebugFullScreen.sahder that you should look at but DebugViewMaterialGBuffer.shader. This is a self content shader that access to the GBuffer. When accessing the GBuffer you need to use the Lit.hlsl file, as GBuffer is only define with our lit shader (the Lit.hlsl contain surface and bsdf data). See the list of include in this shader, you should include the same list.
    you can look at how DebugViewMaterialGBuffer is call in C#

    The TEXTURE2D_X is use to be compatible with single instancing VR path. All our render target texture are texture array to deal with multiple eye and the _X is a macro to expand to an array if the platform support VR. In practice, if you want to have your postprocess compatible with VR you must use these macro.

    Note that we don't recommend to access to GBuffer, as people can switch to forward renderer and so don't have GBuffer in this case (all depends what feature you want to support) and this doesn't support our forward only material (hair, fabric, stacklit...). However you can access to what we call NormalData: Depth, normal and smoothness. This also work in forward and with forward material. You have an example in ScreenSpaceReflections.compute file

    hope this help.
     
  4. nehvaleem

    nehvaleem

    Joined:
    Dec 13, 2012
    Posts:
    382
    This seems like an awesome feature, but won't it be available only in HDRP 7.X making it only compatible with unity 2019.3 cycle?

    It sure helps, but with your advice I've encountered the problem that i've faced some time before. When I include the same list as in DebugViewMaterialGBuffer.shader I am getting an error that I don't quite understand:

    Code (CSharp):
    1. Shader error in 'Hidden/ShadowOfTheRoad/Outline': undeclared identifier 'FastLog2' at /Users/xxx/Projects/Unity/_packages/ExtendedScriptableRenderPipeline/com.unity.render-pipelines.high-definition/Runtime/Material/SubsurfaceScattering/SubsurfaceScattering.hlsl(171) (on d3d11)
    2.  
    3. Compiling Vertex program
    4. Platform defines: UNITY_ENABLE_REFLECTION_BUFFERS UNITY_USE_DITHER_MASK_FOR_ALPHABLENDED_SHADOWS UNITY_PBS_USE_BRDF1 UNITY_SPECCUBE_BOX_PROJECTION UNITY_SPECCUBE_BLENDING UNITY_ENABLE_DETAIL_NORMALMAP SHADER_API_DESKTOP UNITY_LIGHT_PROBE_PROXY_VOLUME UNITY_LIGHTMAP_FULL_HDR
    The include list in my shader is exactly as in DebugViewMaterialGBuffer.shader, order of includes is exactly the same also.

    My whole shader code is as follows:

    Outline.shader


    Code (CSharp):
    1. Shader "Hidden/ShadowOfTheRoad/Outline"
    2. {
    3.     SubShader
    4.     {
    5.         Tags{ "RenderPipeline" = "HDRenderPipeline" }
    6.  
    7.         Pass
    8.         {
    9.             ZWrite On ZTest Always Blend Off Cull Off
    10.  
    11.             HLSLPROGRAM
    12.                 #pragma vertex Vert
    13.                 #pragma fragment Frag
    14.  
    15.                 #include "Outline.hlsl"
    16.             ENDHLSL
    17.         }
    18.     }
    19.     Fallback Off
    20. }
    21.  
    Outline.hlsl

    Code (CSharp):
    1. #pragma target 4.5
    2. #pragma only_renderers d3d11 ps4 xboxone vulkan metal switch
    3.  
    4. #include "Packages/com.unity.render-pipelines.core/ShaderLibrary/Common.hlsl"
    5.  
    6. #include "Packages/com.unity.render-pipelines.high-definition/Runtime/ShaderLibrary/ShaderVariables.hlsl"
    7. #include "Packages/com.unity.render-pipelines.high-definition/Runtime/Material/Material.hlsl"
    8.  
    9. #include "Packages/com.unity.render-pipelines.high-definition/Runtime/Debug/DebugDisplay.hlsl"
    10. #include "Packages/com.unity.render-pipelines.high-definition/Runtime/Material/Lit/Lit.hlsl"
    11.  
    12. float4 _outlineColor;
    13.  
    14. struct Attributes
    15. {
    16.     uint vertexID : SV_VertexID;
    17.     UNITY_VERTEX_INPUT_INSTANCE_ID
    18. };
    19.  
    20. struct Varyings
    21. {
    22.     float4 positionCS : SV_POSITION;
    23.     float2 texcoord : TEXCOORD0;
    24.     UNITY_VERTEX_OUTPUT_STEREO
    25. };
    26.  
    27. Varyings Vert(Attributes input)
    28. {
    29.     Varyings output;
    30.     UNITY_SETUP_INSTANCE_ID(input);
    31.     UNITY_INITIALIZE_VERTEX_OUTPUT_STEREO(output);
    32.     output.positionCS = GetFullScreenTriangleVertexPosition(input.vertexID);
    33.     output.texcoord = GetFullScreenTriangleTexCoord(input.vertexID);
    34.     return output;
    35. }
    36.  
    37. float3 Frag(Varyings input) : SV_Target
    38. {
    39.     float depth = LoadCameraDepth(input.positionCS.xy);
    40.     PositionInputs posInput = GetPositionInput(input.positionCS.xy, _ScreenSize.zw, depth, UNITY_MATRIX_I_VP, UNITY_MATRIX_V);
    41.  
    42.     BSDFData bsdfData;
    43.     BuiltinData builtinData;
    44.     DECODE_FROM_GBUFFER(posInput.positionSS, UINT_MAX, bsdfData, builtinData);
    45.  
    46.     float depth = LoadCameraDepth(input.positionCS.xy);
    47.     float3 color = _outlineColor * depth;
    48.  
    49.     return color;
    50. }
    51.  
    Am I missing something obvious?
     
    Last edited: Aug 12, 2019
    CTW_AyumiYasui likes this.
  5. SebLagarde

    SebLagarde

    Unity Technologies

    Joined:
    Dec 30, 2015
    Posts:
    928
    hmm...

    just random guess but IIRC the #pragma directive don't work in include, you need to move this part in .shader


    1. #pragma target 4.5
    2. #pragma only_renderers d3d11 ps4 xboxone vulkan metal switch
    I guess it is a godd candidate to your failure as the code that fail is:


    #if (SHADER_TARGET >= 45)
    uint FastLog2(uint x)
    {
    return firstbithigh(x);
    }
    #endif

    So I guess the shader compiler don't find any directive #pragma target 4.5 because it is in an include.
     
    nehvaleem likes this.
  6. nehvaleem

    nehvaleem

    Joined:
    Dec 13, 2012
    Posts:
    382

    Awesome, you are exactly right. I would struggle much more if it wasn't for you :) Thanks - now I will try to access the data I need and hopefully recreate my effects.

    But I won't hesitate to ask if I encounter another problems :)
     
  7. Lynxed

    Lynxed

    Joined:
    Dec 9, 2012
    Posts:
    121
    What if i make a custom post process for a non-VR game. Do i have to make 2DArrays for single textures?
     
  8. Lad-Ty

    Lad-Ty

    Joined:
    May 7, 2013
    Posts:
    50
    I'll try asking here since it's probably the closest thread I found to what I'm looking for. I really need to be able to read diffuse buffer for the post-process effect I'm working on - screen space GI (+AO). I need to be able to bounce light even at surfaces that are fully shadowed/black. And to properly light them up, I need to know their (diffuse) color. Sadly I have no luck accessing it, the buffer is fully black and I have no idea why, what else should I include or how else to sample it.
    I've had some luck with this: "TEXTURE2D_X(_GBufferTexture0); + LOAD_TEXTURE2D_X(_GBufferTexture0, positionSS);" , but only when reading _GBufferTexture1.

    I have also tried using macros I found in the DebugViewMaterialGBuffer.shader and/or Lit.hlsl you talked about here
    Code (CSharp):
    1. float4 GetDiffuseColor(float2 positionSS) {
    2.     BSDFData bsdfData;
    3.     BuiltinData builtinData;
    4.     DECODE_FROM_GBUFFER(positionSS, UINT_MAX, bsdfData, builtinData);
    5.     return float4(GetDiffuseOrDefaultColor(bsdfData, 1).rgb, 1);
    6. }
    But same result -> all black. So I'd like to know, how can I actually sample diffuse buffer in HDRP PostProcess when using Deferred shading? Currently I can only work with the colors from source RT, which is fine for well lit scenes but not when trying to light up dark areas, there you lose a lot of color info, making the effect very dull and ineffective.

    Test scene (dark areas are severely lacking):


    Currently I can only achieve the middle image, which does not really make it look like the light is actually bouncing and lighting other surfaces. The 3rd image is "artifically boosted" to resemble what I think it should approximately look like, but for now I'm just adding a bit of white to any sampled color so even black surfaces show up. Wouldn't make sense for example to fully shadowed green surface, that would look like the original color is dark grey :/

    I'd like the middle variant to be a fallback in case of forward rendering possibly, why not, just sample the source. But when available (= deferred), I really need to know the diffuse color that should be affected. Any help would be really appreciated, I am stuck for 3 days just on getting this to work really.
     
    Last edited: Jul 12, 2020