[oden] Let's get started on text

This has already been a journey and it will keep being a journey I think.
This commit is contained in:
John Doty 2023-08-31 08:22:59 -07:00
parent 44130cf22a
commit 71aa3c39f7
6 changed files with 503 additions and 4 deletions

85
src/text_shader.wgsl Normal file
View file

@ -0,0 +1,85 @@
// ----------------------------------------------------------------------------
// 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>,
};
struct VertexOutput {
@builtin(position) clip_position : vec4<f32>,
@location(0) tex_coords : vec2<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);
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_multisampled_2d<f32>;
@group(1) @binding(1) var s_diffuse : sampler;
@fragment fn fs_main(in : VertexOutput)->@location(0) vec4<f32> {
// TODO: Should we be sampling here for the shader?
let tc = vec2(u32(in.tex_coords.x), u32(in.tex_coords.y));
return textureLoad(t_diffuse, tc, 0);
}
// ----------------------------------------------------------------------------
// Resolution Handling
// ----------------------------------------------------------------------------
struct ScreenUniform {
resolution : vec2f,
};
@group(0) @binding(0) // 1.
var<uniform> screen : ScreenUniform;
const RES = vec2f(320.0, 240.0); // The logical resolution of the screen.
fn adjust_for_resolution(in_pos: vec2<f32>) -> vec2<f32> {
// 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;
}