get rid of tile_infos buffer for world shader and instead just use the vertex uvs

This commit is contained in:
Sven Balzer 2025-09-26 07:22:08 +02:00
parent dcd40b6394
commit 434abdc9a7
2 changed files with 12 additions and 67 deletions

View File

@ -249,8 +249,6 @@ static TileInfo tile_infos[] = {
{ 0x0423, "tiles/grass_ground_two_corner.png", TILE_CORNER_INFO(TILEKIND_GRASS, TILEKIND_GROUND, TILEKIND_GRASS, TILEKIND_GROUND ) },
};
static V4 cpu_tile_infos_buffer[SDL_arraysize(tile_infos)];
static Sint32 selected_tile_kind = -1;
static Sint32 selected_tile = -1;
@ -851,16 +849,6 @@ static bool recreate_graphics_pipelines() {
.minBindingSize = 0,
},
},
{
.binding = 3,
.visibility = WGPUShaderStage_Vertex,
.buffer = {
.type = WGPUBufferBindingType_ReadOnlyStorage,
.hasDynamicOffset = false,
.minBindingSize = 0,
},
},
};
WGPUBindGroupLayoutDescriptor world_bind_group_layout_descriptor = {
@ -902,13 +890,18 @@ static bool recreate_graphics_pipelines() {
.offset = offsetof(Vertex, pos),
.shaderLocation = 0,
},
{
.format = WGPUVertexFormat_Float32x2,
.offset = offsetof(Vertex, uv),
.shaderLocation = 1,
},
};
WGPUVertexAttribute instance_buffer_attributes[] = {
{
.format = WGPUVertexFormat_Uint32,
.offset = 0,
.shaderLocation = 1,
.shaderLocation = 2,
},
};
@ -1121,8 +1114,6 @@ static bool recreate_tile_textures() {
SDL_assert_always(width == TILE_SIZE);
SDL_assert_always(height == TILE_SIZE);
cpu_tile_infos_buffer[i] = V4_(0, 0, 1, 1);
WGPUTexelCopyTextureInfo texel_copy_texture_info = {
.texture = tile_textures_array,
.mipLevel = 0,
@ -1516,12 +1507,6 @@ int main(int argc, char **argv) {
return 1;
}
tile_infos_buffer = create_buffer(WGPUBufferUsage_Storage, SDL_arraysize(cpu_tile_infos_buffer) * sizeof(*cpu_tile_infos_buffer), cpu_tile_infos_buffer, "tile_infos_buffer");
if (!tile_infos_buffer) {
log_error("Failed to create buffer. Exiting.");
return 1;
}
WGPUBindGroupEntry per_frame_bind_group_entries[] = {
{ .binding = 0, .buffer = view_projection_matrix_buffer, .offset = 0, .size = WGPU_WHOLE_SIZE },
{ .binding = 1, .buffer = per_frame_buffer, .offset = 0, .size = WGPU_WHOLE_SIZE },
@ -1539,7 +1524,6 @@ int main(int argc, char **argv) {
{ .binding = 0, .textureView = tile_textures_array_view },
{ .binding = 1, .sampler = pixel_sampler },
{ .binding = 2, .buffer = tint_color_buffer, .offset = 0, .size = WGPU_WHOLE_SIZE },
{ .binding = 3, .buffer = tile_infos_buffer, .offset = 0, .size = WGPU_WHOLE_SIZE },
};
WGPUBindGroupDescriptor world_bind_group_descriptor = {

View File

@ -3,11 +3,12 @@ struct VertexShaderInput {
@builtin(vertex_index) vertex_index: u32,
@location(0) pos: vec3<f32>,
@location(1) uv: vec2<f32>,
// Per Instance
@builtin(instance_index) instance_index: u32,
@location(1) tile_info: u32,
@location(2) tile_info: u32,
};
struct VertexShaderOutput {
@ -32,8 +33,6 @@ struct Per_Frame_Data {
@group(0) @binding(0) var<uniform> view_projection_matrix: mat4x4<f32>;
@group(0) @binding(1) var<uniform> per_frame: Per_Frame_Data;
@group(1) @binding(3) var<storage, read> tile_infos: array<vec4<f32>>;
@vertex fn main_vertex(input: VertexShaderInput) -> VertexShaderOutput {
var output: VertexShaderOutput;
@ -45,49 +44,11 @@ struct Per_Frame_Data {
output.tile = tile_type;
output.pos = vec4<f32>(tile_pos + input.pos.xy, 0, 1) * view_projection_matrix;
let uv_min_max = tile_infos[tile_type];
switch (rotation) {
case 0: {
switch (input.vertex_index) {
case 0: { output.uv = uv_min_max.xy; }
case 1: { output.uv = uv_min_max.xw; }
case 2: { output.uv = uv_min_max.zw; }
case 3: { output.uv = uv_min_max.zy; }
default: {}
}
}
case 1: {
switch (input.vertex_index) {
case 0: { output.uv = uv_min_max.zy; }
case 1: { output.uv = uv_min_max.xy; }
case 2: { output.uv = uv_min_max.xw; }
case 3: { output.uv = uv_min_max.zw; }
default: {}
}
}
case 2: {
switch (input.vertex_index) {
case 0: { output.uv = uv_min_max.zw; }
case 1: { output.uv = uv_min_max.zy; }
case 2: { output.uv = uv_min_max.xy; }
case 3: { output.uv = uv_min_max.xw; }
default: {}
}
}
case 3: {
switch (input.vertex_index) {
case 0: { output.uv = uv_min_max.xw; }
case 1: { output.uv = uv_min_max.zw; }
case 2: { output.uv = uv_min_max.zy; }
case 3: { output.uv = uv_min_max.xy; }
default: {}
}
}
case 0: { output.uv = vec2<f32>( input.uv.x, input.uv.y); }
case 1: { output.uv = vec2<f32>(1.0 - input.uv.y, input.uv.x); }
case 2: { output.uv = vec2<f32>(1.0 - input.uv.x, 1.0 - input.uv.y); }
case 3: { output.uv = vec2<f32>( input.uv.y, 1.0 - input.uv.x); }
default: {}
}