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,61 +0,0 @@
@block common
@ctype vec2 position_t;
struct cell
{
vec2 pos;
};
vec3 color(int i)
{
float r = ((i >> 0) & 0xff)/255.0f;
float g = ((i >> 8) & 0xff)/255.0f;
float b = ((i >> 16) & 0xff)/255.0f;
return vec3(r, g, b);
}
@end
@vs vs
@include_block common
layout(binding = 0) uniform vs_uniform {
float radius;
};
layout(location = 0) in vec2 in_quad;
layout(location = 1) out vec2 _quad;
layout(location = 2) out vec2 _centroid;
layout(location = 3) flat out vec3 _color;
readonly: layout(binding = 0) readonly buffer vs_ssbo {
cell cells[];
};
void main()
{
_centroid = cells[gl_InstanceIndex].pos;
_color = color(gl_InstanceIndex);
_quad = radius * in_quad + _centroid;
gl_Position = vec4(_quad, 0.0, 1.0);
}
@end
@fs fs
@include_block common
layout(binding = 1) uniform fs_uniform {
float radius;
};
layout(location = 0) out vec4 frag_color;
layout(location = 1) in vec2 _quad;
layout(location = 2) in vec2 _centroid;
layout(location = 3) flat in vec3 _color;
void main()
{
gl_FragDepth = length(_quad - _centroid);
if(gl_FragDepth > radius) discard;
frag_color = vec4(_color, 1.0);
}
@end
@program base vs fs

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;
}