Shader "Confirmat/NewImageEffectShader"
{
    Properties
    {
        [HideInInspector] _MainTex ("Texture", 2D) = "white" {}
    }
    SubShader
    {
        // No culling or depth
        Cull Off ZWrite Off ZTest Always

        Pass
        {
            CGPROGRAM
            #pragma vertex vert
            #pragma fragment frag
            #pragma target 5.0   // EXTRA FOR STORING
            #include "UnityCG.cginc"

            struct appdata
            {
                float4 vertex : POSITION;
                float2 uv : TEXCOORD0;
            };

            struct v2f
            {
                float2 uv : TEXCOORD0;
                float4 vertex : SV_POSITION;
                float4 scrPos : TEXCOORD1;// EXTRA FOR STORING
            };

            v2f vert (appdata v)
            {
                v2f o;
                o.vertex = UnityObjectToClipPos(v.vertex);
                o.uv = v.uv;
                o.scrPos = ComputeScreenPos(o.vertex); // EXTRA FOR STORING
                return o;
            }

            sampler2D _MainTex;
            sampler2D _CameraDepthTexture;

            // EXTRA FOR STORING
            //Fragment Shader
            int _width;
            int _height;
            RWStructuredBuffer<float> _myBuffer : register(u1);

            fixed4 frag (v2f i) : SV_Target
            {
                //get depth from depth texture
                float depth = tex2D(_CameraDepthTexture, i.uv).r;
                depth = Linear01Depth(depth);
                depth = depth * _ProjectionParams.z; //depth as distance from camera in units 

                //EXTRA FOR STORING
                int x = int(i.scrPos.x * _width);
                int y = int(i.scrPos.y * _height);
                //_myBuffer[(y * _width) + x] = depth; // buffer should be written here, but nothing happens!
                _myBuffer[((_height - 1 - y) * _width) + x] = depth; // buffer should be written here, but nothing happens!

                return depth;
            }
            ENDCG
        }
    }
}
