Automatic texture batching + sprite rendering

This commit is contained in:
2025-06-23 00:15:01 +02:00
parent b02b2b02d5
commit e8516827e1
15 changed files with 758 additions and 4704 deletions

View File

@@ -1,47 +1,56 @@
struct Cell {
position: vec2f,
struct Sprite {
matrix: mat4x4f,
};
struct VsUniform {
mvp: mat4x4f,
};
struct VsI { //Vertex shader input
@builtin(instance_index) instance: u32,
@location(0) position: vec2f,
@location(1) uv: vec2f,
};
struct Vs2Fs { //Vertex shader to Fragment shader
@builtin(position) pos: vec4f,
@location(0) @interpolate(flat) instance: u32,
@location(0) @interpolate(linear) uv: vec2f,
};
struct FsO { //Fragment shader output
@builtin(frag_depth) depth: f32,
@location(0) color: vec4f,
};
fn color(i: u32) -> vec3f
{
let r: f32 = f32(((i >> 0) & 0xff));
let g: f32 = f32(((i >> 8) & 0xff));
let b: f32 = f32(((i >> 16) & 0xff));
return vec3f(r / 255, g / 255, b / 255);
}
@binding(0) @group(0) var<uniform> vs_uniforms: VsUniform;
@binding(0) @group(1) var tex: texture_2d<f32>;
@binding(1) @group(1) var samp: sampler;
@binding(2) @group(1) var<storage> sprites: array<Sprite>;
//@binding(0) @group(0) var<uniform> mvp: mat4x4f;
@binding(0) @group(1) var<storage> cells: array<Cell>;
@vertex
fn vs_main(input: VsI) -> Vs2Fs {
@vertex fn vs_main(input: VsI) -> Vs2Fs {
var output: Vs2Fs;
output.pos = vec4f(cells[input.instance].position.xy, 0.0, 0.0)/* * mvp*/;
output.instance = input.instance;
output.pos = vec4f(input.position.x, input.position.y, 0, 0) * sprites[input.instance].matrix * vs_uniforms.mvp;
output.uv = input.uv;
return output;
}
@fragment
fn fs_main(input: Vs2Fs) -> FsO {
// Convert a 32bit uint color (hex representation) into a normalized vec4f
/*fn convertColor(input: u32) -> vec4f {
let r: f32 = f32(((input >> 0) & 0xff));
let g: f32 = f32(((input >> 8) & 0xff));
let b: f32 = f32(((input >> 16) & 0xff));
let a: f32 = f32(((input >> 24) & 0xff));
return vec4f(r / 255, g / 255, b / 255, a / 255);
}*/
// Get the texture array index from the UV
/*fn indexFromCoord(uv: vec2f, width: u32, height: u32) -> u32 {
let x: u32 = clamp(floor(uv.x * width), 0, width);
let y: u32 = clamp(floor(uv.y * height), 0, height);
return y * width + x;
}*/
@fragment fn fs_main(input: Vs2Fs) -> FsO {
var output: FsO;
output.depth = 1.0;
output.color = vec4f(1.0, 0.0, 1.0, 1.0);
//output.color = vec4f(color(input.instance), 1.0);
output.color = textureSample(tex, samp, input.uv);
return output;
}