// Each #kernel tells which function to compile; you can have many kernels
#pragma kernel CSMain

// Create a RenderTexture with enableRandomWrite flag and set it
// with cs.SetTexture
RWTexture2D<float4> Camera1;
RWTexture2D<float4> Camera2;
RWTexture2D<float4> Result;
RWStructuredBuffer<float> MonoChannelResult;
float ResX;
float Offset;
float FarClipPlane;

[numthreads(8,8,1)]
void CSMain (uint3 id : SV_DispatchThreadID)
{
    int2 idxy1 = int2(clamp(id.x + Offset, 0, ResX), id.y);
    int2 idxy2 = int2(clamp(id.x - Offset, 0, ResX), id.y);
    float3 col1 = Camera1[idxy1].xyz;
    float3 col2 = Camera1[idxy2].xyz;
    float diffFactorL = saturate(id.x - Offset);
    float diffFactorR = saturate(ResX - (id.x + Offset));
    float4 resVal = 0;
    //if(diffFactorL && diffFactorR > 0)
        resVal = float4(((col1 * diffFactorR + col2 * diffFactorL) / ((diffFactorL && diffFactorR) == false ? 1 : 2)).xyz, 1);
    //else
        //resVal = float4(max(col1, col2), 1);

    Result[id.xy] = resVal;
    MonoChannelResult[id.x + id.y * ResX] = resVal.r * FarClipPlane;
}
