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

using namespace metal;

struct VertexOut
{
    float2 uv [[user(locn0)]];
    float eyedist [[user(locn1)]];
    float3 lightVector [[user(locn2)]];
    float3 normal [[user(locn3)]];
    float4 gl_Position [[position]];
};

struct VertexIn
{
    float3 pos_attr [[attribute(0)]];
    float2 uv_attr [[attribute(1)]];
    float3 normal_attr [[attribute(2)]];
};

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 = {};
    out.normal = in.normal_attr;
    float4 pos = modelview_unif * float4(in.pos_attr, 1.0);
    out.gl_Position = proj_unif * pos;
    out.uv = (in.uv_attr * tex_scale_unif) + tex_offset_unif;
    out.eyedist = -pos.z;
    return out;
}

