#include <metal_stdlib>
using namespace metal;

template<typename T, size_t N>
struct tint_array {
  const constant T& operator[](size_t i) const constant { return elements[i]; }
  device T& operator[](size_t i) device { return elements[i]; }
  const device T& operator[](size_t i) const device { return elements[i]; }
  thread T& operator[](size_t i) thread { return elements[i]; }
  const thread T& operator[](size_t i) const thread { return elements[i]; }
  threadgroup T& operator[](size_t i) threadgroup { return elements[i]; }
  const threadgroup T& operator[](size_t i) const threadgroup { return elements[i]; }
  T elements[N];
};

struct vs_main_outputs {
  float4 tint_symbol [[position]];
};

float4 vs_main_inner(uint in_vertex_index) {
  return tint_array<float4, 3>{float4(0.0f, 0.0f, 0.0f, 1.0f), float4(0.0f, 1.0f, 0.0f, 1.0f), float4(1.0f, 1.0f, 0.0f, 1.0f)}[in_vertex_index];
}

vertex vs_main_outputs vs_main(uint in_vertex_index [[vertex_id]]) {
  vs_main_outputs tint_wrapper_result = {};
  tint_wrapper_result.tint_symbol = vs_main_inner(in_vertex_index);
  return tint_wrapper_result;
}
