#pragma kernel Preprocess

// Input
sampler2D Input;
uint2 ImageSize;

// Output
RWStructuredBuffer<float> Output;

[numthreads(8, 8, 1)]
void Preprocess(uint2 id : SV_DispatchThreadID)
{
    // UV (vertically flipped)
    float2 uv = float2(0.5 + id.x, ImageSize.y - 0.5 - id.y) / ImageSize;

    // UV gradients
    float2 duv_dx = float2(1.0 / ImageSize.x, 0);
    float2 duv_dy = float2(0, -1.0 / ImageSize.y);

    // Texture sample
    float3 rgb = tex2Dgrad(Input, uv, duv_dx, duv_dy).rgb * 2 - 1;

    // Tensor element output
    uint offs = (id.y * ImageSize.x + id.x) * 3;
    Output[offs + 0] = rgb.r;
    Output[offs + 1] = rgb.g;
    Output[offs + 2] = rgb.b;
}
