// @include 'util.wgsl'
// ----------------------------------------------------------------------------
// Vertex shader
// ----------------------------------------------------------------------------

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

struct InstanceInput {
  @location(5) src_top_left: vec2<f32>,
  @location(6) src_dims: vec2<f32>,
  @location(7) dest_top_left: vec2<f32>,
  @location(8) dest_dims: vec2<f32>,
  @location(9) color: vec4<f32>,
};

struct VertexOutput {
  @builtin(position) clip_position : vec4<f32>,
  @location(0) tex_coords : vec2<f32>,
  @location(1) color: vec4<f32>,
};

@vertex fn vs_main(vertex : VertexInput, instance : InstanceInput)->VertexOutput {
  var out : VertexOutput;
  out.tex_coords = instance.src_top_left + (vertex.tex_coords * instance.src_dims);
  out.color = instance.color;

  let in_pos = instance.dest_top_left + (vec2f(vertex.position.x, vertex.position.y) * instance.dest_dims);

  let position = adjust_for_resolution(in_pos);
  out.clip_position = vec4f(position.x, position.y, vertex.position.z, 1.0);
  return out;
}

// ----------------------------------------------------------------------------
// Fragment shader
// ----------------------------------------------------------------------------

@group(1) @binding(0) var t_diffuse : texture_2d<f32>;
@group(1) @binding(1) var s_diffuse : sampler;

@fragment fn fs_main(in : VertexOutput)->@location(0) vec4<f32> {
  let tc = vec2(u32(in.tex_coords.x), u32(in.tex_coords.y));
  let c = textureLoad(t_diffuse, tc, 0);

  // If we were going to run this through the sampler it would look like
  // this... but I don't know if that'w what we should do? Right now the
  // direct load looks better?
  //
  // let tc = in.tex_coords / vec2<f32>(textureDimensions(t_diffuse));
  // let c = textureSample(t_diffuse, s_diffuse, tc);  

  return vec4<f32>(
      c.r * in.color.x,
      c.r * in.color.y,
      c.r * in.color.z,
      c.r * in.color.a
  );
  //return vec4<f32>(1.0,1.0,1.0,1.0);
}
