struct CameraUniform {
  vp: mat4x4<f32>;
};

struct MeshInstance {
  transform: mat4x4<f32>;
};

struct MeshData {
  instances: array<MeshInstance>;
};

struct VertexInput {
  [[location(0)]] position: vec3<f32>;
  [[location(1)]] tex_coords: vec2<f32>;
};

struct VertexOutput {
  [[builtin(position)]] clip_position: vec4<f32>;
  [[location(0)]] tex_coords: vec2<f32>;
};

[[group(0), binding(0)]]
var<uniform> camera: CameraUniform;

[[group(1), binding(0)]]
var<storage,read> meshes: MeshData;

[[group(2), binding(0)]] var t_albedo: texture_2d<f32>;
[[group(2), binding(1)]] var s_albedo: sampler;

[[stage(vertex)]]
fn vs_main(
  [[builtin(instance_index)]] mesh_idx: u32,
  vertex: VertexInput,
) -> VertexOutput {
  let transform = meshes.instances[mesh_idx].transform;
  var out: VertexOutput;
  out.clip_position = camera.vp * transform * vec4<f32>(vertex.position, 1.0);
  out.tex_coords = vertex.tex_coords;
  return out;
}

[[stage(fragment)]]
fn fs_main(
  frag: VertexOutput,
) -> [[location(0)]] vec4<f32> {
  return textureSample(t_albedo, s_albedo, frag.tex_coords);
}
