#include "RayTypes.h"

RWStructuredBuffer<SphereRay> Output : register(u0, space1);

cbuffer UBO : register(b0, space2)
{
    int4 ubo_size : packoffset(c0);

    float4 camera_position : packoffset(c3);
};

[numthreads(64, 1, 1)]
void main(uint3 GlobalInvocationID : SV_DispatchThreadID)
{
    uint n = GlobalInvocationID.x;

    int2 r;
    r.y = n / ubo_size.x;
    r.x = n - r.y * ubo_size.x;

    float3 heading;

    float aspectRatio = (float) ubo_size.y / (float) ubo_size.x;
    heading.x = (r.x / (float) ubo_size.x) - 0.5;
    heading.y = ((r.y / (float) ubo_size.y) - 0.5) * aspectRatio;

    float d = clamp(smoothstep(0.0, 1.0, length(heading.xy) * 4.0 - 0.2) + 0.2, 0, 1.0);
    float scaling = d;
    //max(1, 2 - d);
    heading.xy *= lerp(1.0, scaling, 0.2);



    heading.z = -1;
    heading = normalize(heading);

    SphereRay sr = (SphereRay) 0;
    sr.Position = camera_position;
    sr.Heading.xyz = heading;

    Output[n] = sr;
}
