// ---------------------------------------------------------------------------- // Vertex shader // ---------------------------------------------------------------------------- struct VertexInput { @location(0) position : vec3, @location(1) tex_coords : vec2, }; struct InstanceInput { @location(5) src_top_left: vec2, @location(6) src_dims: vec2, @location(7) dest_top_left: vec2, @location(8) dest_dims: vec2, @location(9) color: vec4, }; struct VertexOutput { @builtin(position) clip_position : vec4, @location(0) tex_coords : vec2, @location(1) color: vec4, }; @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; @group(1) @binding(1) var s_diffuse : sampler; @fragment fn fs_main(in : VertexOutput)->@location(0) vec4 { 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(textureDimensions(t_diffuse)); // let c = textureSample(t_diffuse, s_diffuse, tc); return vec4( c.r * in.color.x, c.r * in.color.y, c.r * in.color.z, c.r * in.color.a ); //return vec4(1.0,1.0,1.0,1.0); } // ---------------------------------------------------------------------------- // Resolution Handling // ---------------------------------------------------------------------------- struct ScreenUniform { resolution : vec2f, }; @group(0) @binding(0) // 1. var screen : ScreenUniform; const RES = vec2f(320.0, 240.0); // The logical resolution of the screen. fn adjust_for_resolution(in_pos: vec2) -> vec2 { // Adjust in_pos for the "resolution" of the screen. let RES_AR = RES.x / RES.y; // The aspect ratio of the logical screen. // the actual resolution of the screen. let screen_ar = screen.resolution.x / screen.resolution.y; // Compute the difference in resolution ... correctly? // // nudge is the amount to add to the logical resolution so that the pixels // stay the same size but we respect the aspect ratio of the screen. (So // there's more of them in either the x or y direction.) var nudge = vec2f(0.0); if (screen_ar > RES_AR) { nudge.x = (RES.y * screen_ar) - RES.x; } else { nudge.y = (RES.x / screen_ar) - RES.y; } var new_logical_resolution = RES + nudge; // Now we can convert the incoming position to clip space, in the new screen. let centered = in_pos + (nudge / 2.0); var position = (2.0 * centered / new_logical_resolution) - 1.0; position.y = -position.y; return position; }