#include <metal_stdlib>
#include <vertex_data.h>

using namespace metal;

struct Vertex {
    float2 pos;
    float2 uv;
};

constant Vertex vertices[6] = {
    {{-0.5,  0.5}, {0.0, 1.0}},
    {{-0.5, -0.5}, {0.0, 0.0}},
    {{ 0.5,  0.5}, {1.0, 1.0}},

    {{ 0.5,  0.5}, {1.0, 1.0}},
    {{-0.5, -0.5}, {0.0, 0.0}},
    {{ 0.5, -0.5}, {1.0, 0.0}},
};

struct VertexOut {
    float4 pos [[position]];
    float2 uv;
    float4 color;
};

vertex VertexOut vertex_main(
    uint vertexID [[vertex_id]],
    constant vertex_p2_s2_uv2_c4_a1* in,
    constant simd::float4x4 *ortho)
{
    Vertex v = vertices[vertexID % 6];

    VertexOut out;
    out.pos = (*ortho) * (float4((v.pos * in[vertexID].scale) + in[vertexID].pos, 0.0, 1.0));
    out.uv = v.uv;
    out.color = in[vertexID].color;

    return out;
}

fragment float4 fragment_main(VertexOut in [[stage_in]], texture2d<float> colorTexture [[texture(0)]]) {
    constexpr sampler textureSampler (mag_filter::linear, min_filter::linear);

    // Sample the texture to obtain a color
    const float4 colorSample = colorTexture.sample(textureSampler, in.uv);
    //float2 srgbOut = select(1.292 * in.uv,
    //                        1.055 * pow(in.uv, 1.0/2.4) - 0.055,
    //                        in.uv > 0.0031308);
    return colorSample;
    //return float4(srgbOut, 0.0, in.color.a);
}
