Unity: How to Convert Standard Custom Shader to HDRP Equivalent for GPU Occlusion Culling

84 Views Asked by At

I am working on expanding on a GPU Based Occlusion Culling system that was first created by Przemyslaw Zaworski on https://github.com/przemyslawzaworski/Unity-GPU-Based-Occlusion-Culling.

One of the items I would like to complete is adapting this to run on HDRP. However, I am not sure of how to modify the main shader to accomplish this.

Shader is below.

Shader "HardwareOcclusion"
{
    SubShader
    {
        Cull Off
        Tags { "RenderType" = "Transparent" "Queue" = "Transparent" }
        Pass
        {
            Blend SrcAlpha OneMinusSrcAlpha
            ZWrite Off
            CGPROGRAM
            #pragma vertex VSMain
            #pragma fragment PSMain
            #pragma target 5.0
 
            RWStructuredBuffer<float4> _Writer : register(u1);
            StructuredBuffer<float4> _Reader;
            int _Debug;
 
            float4 VSMain (float4 vertex : POSITION, out uint instance : TEXCOORD0, uint id : SV_VertexID) : SV_POSITION
            {
                instance = _Reader[id].w;
                return mul (UNITY_MATRIX_VP, float4(_Reader[id].xyz, 1.0));
            }
 
            [earlydepthstencil]
            float4 PSMain (float4 vertex : SV_POSITION, uint instance : TEXCOORD0) : SV_TARGET
            {
                _Writer[instance] = vertex;
                return float4(0.0, 0.0, 1.0, 0.2 * _Debug);
            }
            ENDCG
        }
    }
}

Basically, it appears that this is using the view depth to compare against the position of an object. These object positions are set via the _Reader buffer on initialization. From there it is using a compute shader to determine if the objects bounding box is visible. More info can be found in the repo.

This works fine in the standard render pipeline but obviously doesn't work in HDRP and I have no idea how this can be converted. I tried looking all over the internet for documentation on creating custom HDRP shaders but I could not derive a method for converting this standard shader. I need this so that I can perform occlusion culling on dynamically created meshes at runtime.

0

There are 0 best solutions below