#include <metal_stdlib>
#include <simd/simd.h>

using namespace metal;

struct VertexOut
{
    float2 uv [[user(locn0)]];
    float eyedist [[user(locn1)]];
    float4 gl_Position [[position]];
};

struct VertexIn
{
    float3 pos_attr [[attribute(0)]];
};

vertex VertexOut vertexShader(VertexIn in [[stage_in]], constant float4x4& modelview_unif [[buffer(1)]], constant float4x4& proj_unif [[buffer(2)]], constant float2& tex_scale_unif [[buffer(3)]], constant float2& tex_offset_unif [[buffer(4)]])
{
    VertexOut out = {};
    float2 texture_uv = float2(in.pos_attr.x, in.pos_attr.y);
    float4 pos = float4(in.pos_attr.x, in.pos_attr.y, in.pos_attr.z, 1.0);
    float4 world_pos = modelview_unif * pos;
    out.gl_Position = proj_unif * world_pos;
    out.uv = (texture_uv * tex_scale_unif) + tex_offset_unif;
    out.eyedist = -world_pos.z;
    return out;
}

