Added rect draw api

Added some new maths types
Updated shaders to use new D_Rect structure
Added rect buffers to frames
Misc cleanup
This commit is contained in:
2025-10-05 14:27:05 +01:00
parent 3b8c50a361
commit 1757fc4b96
14 changed files with 432 additions and 121 deletions

View File

@@ -372,6 +372,16 @@ bool Vk_Setup(SDL_Window *window) {
vk.CreateDescriptorPool(vk.device, &descriptor_pool, 0, &frame->descriptors);
// rect buffer
//
Vk_Buffer *rbo = &frame->rbo;
rbo->usage = VK_BUFFER_USAGE_STORAGE_BUFFER_BIT;
rbo->size = D_MAX_RECTS * sizeof(D_Rect);
rbo->host_visible = true;
Vk_BufferCreate(rbo);
VkSemaphoreCreateInfo semaphore = { 0 };
semaphore.sType = VK_STRUCTURE_TYPE_SEMAPHORE_CREATE_INFO;
@@ -547,7 +557,7 @@ internal VkDeviceMemory Vk_Allocate(VkMemoryRequirements *mreq, VkMemoryProperty
}
}
if (type_index != -1) {
if (type_index != U32_MAX) {
VkMemoryAllocateInfo alloc_info = { 0 };
alloc_info.sType = VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO;
alloc_info.allocationSize = mreq->size;
@@ -556,6 +566,7 @@ internal VkDeviceMemory Vk_Allocate(VkMemoryRequirements *mreq, VkMemoryProperty
vk.AllocateMemory(vk.device, &alloc_info, 0, &result);
}
Assert(result != VK_NULL_HANDLE);
return result;
}
@@ -566,6 +577,7 @@ void Vk_BufferCreate(Vk_Buffer *buffer) {
create_info.size = buffer->size;
vk.CreateBuffer(vk.device, &create_info, 0, &buffer->handle);
Assert(buffer->handle != VK_NULL_HANDLE);
VkMemoryRequirements req;
vk.GetBufferMemoryRequirements(vk.device, buffer->handle, &req);
@@ -580,6 +592,7 @@ void Vk_BufferCreate(Vk_Buffer *buffer) {
if (buffer->host_visible) {
vk.MapMemory(vk.device, buffer->memory, 0, buffer->size, 0, &buffer->data);
}
}
void Vk_ImageCreate(Vk_Image *image) {

View File

@@ -96,6 +96,8 @@ struct Vk_Frame {
U32 next_scratch;
Vk_CommandBuffer scratch[VK_NUM_SCRATCH];
Vk_Buffer rbo;
U32 image; // swapchain image index
};

View File

@@ -62,6 +62,8 @@
VK_FUNC(CmdDraw);
VK_FUNC(CmdSetViewport);
VK_FUNC(CmdSetScissor);
VK_FUNC(CmdDrawIndexed);
VK_FUNC(CmdBindIndexBuffer);
VK_FUNC(CmdPushConstants);
VK_FUNC(CmdCopyBufferToImage);

View File

@@ -4,12 +4,12 @@
layout(location = 0) in vec2 frag_uv;
layout(location = 1) in vec4 frag_c;
layout(location = 2) in flat uint idx;
layout(location = 2) in flat uint texid;
layout(location = 0) out vec4 framebuffer;
layout(binding = 1) uniform sampler2D u_image[];
layout(binding = 1) uniform sampler2D u_images[];
void main() {
framebuffer = frag_c * texture(u_image[idx], frag_uv);
framebuffer = frag_c * texture(u_images[texid], frag_uv);
}

View File

@@ -2,6 +2,28 @@
#extension GL_EXT_scalar_block_layout : enable
uint indices[6] = { 0, 1, 3, 0, 3, 2 };
vec2 verticies[4] = vec2[](
vec2(-0.5, -0.5),
vec2( 0.5, -0.5),
vec2(-0.5, 0.5),
vec2( 0.5, 0.5)
);
struct G_Rect {
uint id;
uint c[4];
float uv[4];
float angle;
float x, y;
float w, h;
float _pad0, _pad1;
};
struct Vertex {
vec4 p;
vec2 uv;
@@ -15,22 +37,28 @@ uniform Global {
};
layout(binding = 0, scalar)
readonly buffer Vertices {
Vertex vtx[];
readonly buffer Rect {
G_Rect rects[];
};
layout(location = 0) out vec2 frag_uv;
layout(location = 1) out vec4 frag_c;
layout(location = 2) out flat uint idx;
layout(location = 2) out flat uint texid;
vec4 unorm_colour(uint c) {
vec4 result = vec4((c >> 0) & 0xFF, (c >> 8) & 0xFF, (c >> 16) & 0xFF, (c >> 24) & 0xFF) / 255.0f;
return result;
}
void main() {
Vertex v = vtx[gl_VertexIndex];
G_Rect rect = rects[gl_InstanceIndex];
uint idx = indices[gl_VertexIndex];
gl_Position = proj * v.p;
vec2 p = (verticies[idx] * vec2(rect.w, rect.h)) + vec2(rect.x, rect.y);
frag_uv = v.uv;
frag_c = vec4((v.c >> 24) & 0xFF, (v.c >> 16) & 0xFF, (v.c >> 8) & 0xFF, (v.c >> 0) & 0xFF) / 255.0f;
frag_c = frag_c.abgr;
gl_Position = proj * vec4(p, 1.0f, 1.0f);
idx = v.pad;
frag_uv = vec2(rect.uv[(idx & 1) << 1], rect.uv[1 + (uint(idx / 2) << 1)]);
frag_c = unorm_colour(rect.c[idx]);
texid = rect.id;
}