Bindless textures

This commit is contained in:
David Gonzalez Martin 2024-11-30 20:07:44 -06:00 committed by David
parent fc9506f520
commit e499826c1f
4 changed files with 42 additions and 12 deletions
bootstrap

@ -20,6 +20,8 @@ STRUCT(Vertex)
f32 uv_x;
f32 uv_y;
Vec4 color;
u32 texture_index;
u32 reserved[3];
};
decl_vb(Vertex);
@ -30,7 +32,7 @@ STRUCT(GPUDrawPushConstants)
f32 height;
};
fn void draw_string(Renderer* renderer, VirtualBuffer(Vertex)* vertices, VirtualBuffer(u32)* indices, u32 width, u32 height, Vec4 color, String string, TextureAtlas texture_atlas)
fn void draw_string(Renderer* renderer, VirtualBuffer(Vertex)* vertices, VirtualBuffer(u32)* indices, u32 width, u32 height, Vec4 color, String string, TextureAtlas texture_atlas, u32 texture_index)
{
u32 index_offset = 0;
auto x_offset = width / 2;
@ -53,6 +55,7 @@ fn void draw_string(Renderer* renderer, VirtualBuffer(Vertex)* vertices, Virtual
.uv_x = (f32)uv_x / texture_atlas.width,
.uv_y = (f32)uv_y / texture_atlas.width,
.color = color,
.texture_index = texture_index,
};
*vb_add(vertices, 1) = (Vertex) {
.x = pos_x + character->width,
@ -60,6 +63,7 @@ fn void draw_string(Renderer* renderer, VirtualBuffer(Vertex)* vertices, Virtual
.uv_x = (f32)(uv_x + uv_width) / texture_atlas.width,
.uv_y = (f32)uv_y / texture_atlas.width,
.color = color,
.texture_index = texture_index,
};
*vb_add(vertices, 1) = (Vertex) {
.x = pos_x,
@ -67,6 +71,7 @@ fn void draw_string(Renderer* renderer, VirtualBuffer(Vertex)* vertices, Virtual
.uv_x = (f32)uv_x / texture_atlas.width,
.uv_y = (f32)(uv_y + uv_height) / texture_atlas.width,
.color = color,
.texture_index = texture_index,
};
*vb_add(vertices, 1) = (Vertex) {
.x = pos_x + character->width,
@ -74,6 +79,7 @@ fn void draw_string(Renderer* renderer, VirtualBuffer(Vertex)* vertices, Virtual
.uv_x = (f32)(uv_x + uv_width) / texture_atlas.width,
.uv_y = (f32)(uv_y + uv_height) / texture_atlas.width,
.color = color,
.texture_index = texture_index,
};
*vb_add(indices, 1) = index_offset + 0;
@ -131,7 +137,7 @@ void run_app(Arena* arena)
.binding = 0,
.type = DESCRIPTOR_TYPE_IMAGE_PLUS_SAMPLER,
.stage = SHADER_STAGE_FRAGMENT,
.count = 1,
.count = 2,
},
})),
},
@ -149,6 +155,20 @@ void run_app(Arena* arena)
.pipelines = array_to_slice(pipeline_create),
.shader_sources = array_to_slice(shader_source_paths),
});
u32 white_texture_width = 1024;
u32 white_texture_height = white_texture_width;
auto* white_texture_buffer = arena_allocate(arena, u32, white_texture_width * white_texture_height / sizeof(u32));
memset(white_texture_buffer, 0xff, white_texture_width * white_texture_height);
auto white_texture = renderer_texture_create(renderer, (TextureMemory) {
.pointer = white_texture_buffer,
.width = white_texture_width,
.height = white_texture_height,
.depth = 1,
.format = R8G8B8A8_SRGB,
});
auto font_path =
#ifdef _WIN32
strlit("C:/Users/David/Downloads/Fira_Sans/FiraSans-Regular.ttf")
@ -173,9 +193,10 @@ strlit("/usr/share/fonts/TTF/FiraSans-Regular.ttf")
.set = { .value = 0 },
.type = DESCRIPTOR_TYPE_IMAGE_PLUS_SAMPLER,
.binding = 0,
.descriptor_count = 1,
.descriptor_count = 2,
.textures = {
[0] = texture_atlas_image,
[0] = white_texture,
[1] = texture_atlas_image,
},
},
};
@ -188,7 +209,8 @@ strlit("/usr/share/fonts/TTF/FiraSans-Regular.ttf")
Vec4 color = {1, 1, 1, 1};
static_assert(sizeof(color) == 4 * sizeof(float));
draw_string(renderer, &vertices, &indices, initial_window_size.width, initial_window_size.height, color, strlit("Hello world!"), texture_atlas);
u32 texture_index = 1;
draw_string(renderer, &vertices, &indices, initial_window_size.width, initial_window_size.height, color, strlit("He"), texture_atlas, texture_index);
auto vertex_buffer_size = sizeof(*vertices.pointer) * vertices.length;
auto index_buffer_size = sizeof(*indices.pointer) * indices.length;

@ -1,16 +1,19 @@
#version 450
#extension GL_EXT_nonuniform_qualifier : require
//shader input
layout (location = 0) in vec2 in_uv;
layout (location = 1) in vec4 in_color;
layout (location = 0) in vec4 in_color;
layout (location = 1) in vec2 in_uv;
layout (location = 2) in flat uint texture_index;
//output write
layout (location = 0) out vec4 out_frag_color;
layout(set = 0, binding = 0) uniform sampler2D atlas_texture;
layout(set = 0, binding = 0) uniform sampler2D textures[];
void main()
{
vec4 sampled = texture(atlas_texture, in_uv);
vec4 sampled = texture(textures[texture_index], in_uv);
out_frag_color = in_color * sampled;
}

@ -2,8 +2,9 @@
#extension GL_EXT_buffer_reference : require
#extension GL_EXT_debug_printf : require
layout (location = 0) out vec2 out_uv;
layout (location = 1) out vec4 out_color;
layout (location = 0) out vec4 out_color;
layout (location = 1) out vec2 out_uv;
layout (location = 2) out uint out_texture_index;
struct Vertex {
float x;
@ -11,6 +12,8 @@ struct Vertex {
float uv_x;
float uv_y;
vec4 color;
uint texture_index;
uint r[3];
};
layout(buffer_reference, std430) readonly buffer VertexBuffer{
@ -33,9 +36,10 @@ void main()
float height = PushConstants.height;
//output data
gl_Position = vec4(2 * v.x / width - 1, 1 - (2 * v.y) / height, 0.0, 1.0);
gl_Position = vec4(2 * v.x / width - 1, 1 - (2 * v.y) / height, 0, 1);
out_uv = vec2(v.uv_x, v.uv_y);
out_color = v.color;
out_texture_index = v.texture_index;
//debugPrintfEXT("Position: (%f, %f, %f)\n", v.position.x, v.position.y, v.position.z);
//debugPrintfEXT("Color: (%f, %f, %f)\n", v.color.x, v.color.y, v.color.z);
}

@ -687,6 +687,7 @@ Renderer* renderer_initialize()
.sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_VULKAN_1_2_FEATURES,
.bufferDeviceAddress = 1,
.descriptorIndexing = 1,
.runtimeDescriptorArray = 1,
.pNext = &features13,
};