// Stylized Water 3 by Staggart Creations (http://staggart.xyz)
// COPYRIGHT PROTECTED UNDER THE UNITY ASSET STORE EULA (https://unity.com/legal/as-terms)
//    • Copying or referencing source code for the production of new asset store, or public, content is strictly prohibited!
//    • Uploading this file to a public repository will subject it to an automated DMCA takedown request.

#define THREAD_GROUPS 64
#pragma kernel SampleOffsets

#include "..\Libraries\Height.hlsl"

//Input
StructuredBuffer<float3> positions;
uint sampleCount; //Length of 'positions' array

//Output
RWStructuredBuffer<float> offsets;

[numthreads(THREAD_GROUPS,1,1)]
void SampleOffsets(uint id : SV_DispatchThreadID)
{
    //Early out when no more positions are left to process
    //Important for the Metal API, as it avoids accessing unbound array elements!
    if(id > sampleCount) return;
    
    const uint index = (uint)(id);

    //Input positions to sample at
    const float3 positionWS = positions[index];

    //Position, relative to rendering bounds (normalized 0-1)
    const float2 uv = WorldToHeightUV(positionWS);
    
    //Texel value at coordinates
    float2 heights = SampleHeightBuffer(uv).rg;

    //Output
    offsets[index] = heights.r + heights.g; 
}