#include <metal_stdlib>
using namespace metal;

// Test shader (Metal Shading Language port of test.vk.glsl).
// Author: JailDesigner

struct ShadertoyUBO {
    float iTime;
    float2 iResolution;
};

struct PassthroughVOut {
    float4 pos [[position]];
    float2 uv;
};

static float3 palette(float t) {
    float3 a = float3(1.0, 0.5, 0.5);
    float3 b = float3(1.0, 0.5, 0.5);
    float3 c = float3(1.0, 1.0, 1.0);
    float3 d = float3(0.263, 0.416, 0.557);
    return a + b * cos(6.28318 * (c * t * d));
}

fragment float4 test_fs(PassthroughVOut in [[stage_in]],
                        constant ShadertoyUBO& u [[buffer(0)]]) {
    float2 fragCoord = in.uv * u.iResolution;
    float2 uv = (fragCoord * 2.0 - u.iResolution) / u.iResolution.y;
    float d = length(uv);
    float3 col = palette(d);
    d = sin(d * 8.0 + u.iTime) / 8.0;
    d = abs(d);
    d = 0.02 / d;
    col *= d;
    return float4(col, 1.0);
}
