From dc7f8ed7501f19cd0a19044451fd7cf63a7ee4db Mon Sep 17 00:00:00 2001 From: Noah Metz Date: Fri, 18 Oct 2024 16:31:28 -0600 Subject: [PATCH] Added second layer --- client/include/ui.h | 2 +- client/shader_src/ui_text.frag | 8 +++--- client/shader_src/ui_text.vert | 2 ++ client/src/main.c | 45 ++++++++++++++++++++-------------- client/src/render.c | 6 +++-- client/src/ui.c | 6 ++--- 6 files changed, 41 insertions(+), 28 deletions(-) diff --git a/client/include/ui.h b/client/include/ui.h index e29ec04..e96883e 100644 --- a/client/include/ui.h +++ b/client/include/ui.h @@ -150,7 +150,7 @@ VkResult load_font( VkBuffer font_infos, VkDescriptorSet font_samplers, VkDescriptorSet font_textures, - uint32_t last_index, + uint32_t index, VkCommandPool transfer_pool, Queue transfer_queue, FT_Library library, diff --git a/client/shader_src/ui_text.frag b/client/shader_src/ui_text.frag index 66b4f28..75176fc 100644 --- a/client/shader_src/ui_text.frag +++ b/client/shader_src/ui_text.frag @@ -1,17 +1,17 @@ #version 450 #extension GL_EXT_nonuniform_qualifier : enable -//layout(set = 0, binding = 0) uniform sampler font_samplers[]; -//layout(set = 1, binding = 0) uniform texture2DArray font_textures[]; +layout(set = 0, binding = 0) uniform sampler font_samplers[]; +layout(set = 1, binding = 0) uniform texture2DArray font_textures[]; layout(location = 0) in vec4 fragColor; layout(location = 1) in vec2 fragUV; layout(location = 2) flat in uint code; +layout(location = 3) flat in uint font; layout(location = 0) out vec4 outColor; void main() { - //outColor = fragColor * texture(sampler2DArray(font_textures[0], font_samplers[0]), vec3(fragUV, code)); - outColor = vec4(1.0, 1.0, 1.0, 1.0); + outColor = fragColor * texture(sampler2DArray(font_textures[font], font_samplers[0]), vec3(fragUV, code)); } diff --git a/client/shader_src/ui_text.vert b/client/shader_src/ui_text.vert index 4bebc20..50a5b8e 100644 --- a/client/shader_src/ui_text.vert +++ b/client/shader_src/ui_text.vert @@ -54,6 +54,7 @@ layout(std430, push_constant) uniform Push { layout(location = 0) out vec4 fragColor; layout(location = 1) out vec2 fragUV; layout(location = 2) out uint code; +layout(location = 3) out uint font_index; const vec2 square[6] = { vec2(0.0, 0.0), @@ -78,5 +79,6 @@ void main() { fragColor = character.color; gl_Position = push.ui.screen * vec4(vec3(x, y, 0.0) + character.pos, 1.0); code = character.code; + font_index = 0; } diff --git a/client/src/main.c b/client/src/main.c index cbf78c5..a24e8e5 100644 --- a/client/src/main.c +++ b/client/src/main.c @@ -13,7 +13,7 @@ VkResult render_thread(GLFWwindow* window, RenderContext* render_context) { FT_Library library; UIContextStorage ui; - UILayerStorage layer; + UILayerStorage layers[2]; FontStorage font; @@ -40,36 +40,45 @@ VkResult render_thread(GLFWwindow* window, RenderContext* render_context) { VK_RESULT(load_font(render_context->device, render_context->allocator, ui.font_infos, ui.font_samplers, ui.font_textures, 0, render_context->transfer_pool, render_context->transfer_queue, library, "test.ttf", 16, VK_TRUE, &font)); - VK_RESULT(create_layer(10, 100, 10, render_context->device, render_context->allocator, render_context->transfer_pool, render_context->transfer_queue, &layer)); + VK_RESULT(create_layer(10, 100, 10, render_context->device, render_context->allocator, render_context->transfer_pool, render_context->transfer_queue, &layers[0])); + VK_RESULT(create_layer(10, 100, 10, render_context->device, render_context->allocator, render_context->transfer_pool, render_context->transfer_queue, &layers[1])); - VK_RESULT(create_transfer_buffer(render_context->allocator, sizeof(uint32_t) + sizeof(UIRect), &transfer, &transfer_memory, &mapped)); + VK_RESULT(create_transfer_buffer(render_context->allocator, sizeof(uint32_t) + 2*sizeof(UIRect), &transfer, &transfer_memory, &mapped)); uint32_t* mapped_count = (uint32_t*)(mapped); - *mapped_count = 1; + mapped_count[0] = 1; UIRect* mapped_rect = (UIRect*)(mapped + sizeof(uint32_t)); - mapped_rect->pos[0] = 0.0; - mapped_rect->pos[1] = 0.0; - mapped_rect->size[0] = 200.0; - mapped_rect->size[1] = 200.0; - mapped_rect->color[0] = 1.0; - mapped_rect->color[1] = 0.0; - mapped_rect->color[2] = 0.0; - mapped_rect->color[3] = 1.0; + mapped_rect[0].pos[0] = 0.0; + mapped_rect[0].pos[1] = 0.0; + mapped_rect[0].size[0] = 200.0; + mapped_rect[0].size[1] = 200.0; + mapped_rect[0].color[0] = 1.0; + mapped_rect[0].color[1] = 0.0; + mapped_rect[0].color[2] = 0.0; + mapped_rect[0].color[3] = 1.0; + + mapped_rect[1].pos[0] = 200.0; + mapped_rect[1].pos[1] = 0.0; + mapped_rect[1].size[0] = 200.0; + mapped_rect[1].size[1] = 200.0; + mapped_rect[1].color[0] = 0.0; + mapped_rect[1].color[1] = 1.0; + mapped_rect[1].color[2] = 0.0; + mapped_rect[1].color[3] = 1.0; VkCommandBuffer command_buffer = command_begin_single(render_context->device, render_context->transfer_pool); - command_copy_buffer(command_buffer, transfer, layer.layer, 0, offsetof(UILayer, draw_rects) + offsetof(DrawCommand, instance_count), sizeof(uint32_t)); - command_copy_buffer(command_buffer, transfer, layer.rects, sizeof(uint32_t), 0, sizeof(UIRect)); + command_copy_buffer(command_buffer, transfer, layers[0].layer, 0, offsetof(UILayer, draw_rects) + offsetof(DrawCommand, instance_count), sizeof(uint32_t)); + command_copy_buffer(command_buffer, transfer, layers[1].layer, 0, offsetof(UILayer, draw_rects) + offsetof(DrawCommand, instance_count), sizeof(uint32_t)); + command_copy_buffer(command_buffer, transfer, layers[0].rects, sizeof(uint32_t), 0, sizeof(UIRect)); + command_copy_buffer(command_buffer, transfer, layers[1].rects, sizeof(uint32_t) + sizeof(UIRect), 0, sizeof(UIRect)); VK_RESULT(command_end_single(render_context->device, command_buffer, render_context->transfer_pool, render_context->transfer_queue)); vkQueueWaitIdle(render_context->transfer_queue.handle); destroy_transfer_buffer(render_context->allocator, transfer, transfer_memory); - fprintf(stderr, "%ld\n", sizeof(UILayer)); - fprintf(stderr, "%ld\n", sizeof(UIRect)); - while(glfwWindowShouldClose(window) == 0) { glfwPollEvents(); - result = draw_frame(render_context, &ui, &layer, 1); + result = draw_frame(render_context, &ui, layers, 2); if(result != VK_SUCCESS) { fprintf(stderr, "draw_frame error: %s\n", string_VkResult(result)); return result; diff --git a/client/src/render.c b/client/src/render.c index 025aaeb..005da54 100644 --- a/client/src/render.c +++ b/client/src/render.c @@ -1051,14 +1051,16 @@ VkResult draw_frame(RenderContext* context, UIContextStorage* ui_context, UILaye // World subpass vkCmdNextSubpass(command_buffer, VK_SUBPASS_CONTENTS_INLINE); // UI subpass - vkCmdBindDescriptorSets(command_buffer, VK_PIPELINE_BIND_POINT_GRAPHICS, ui_context->char_pipeline.layout, 0, 1, &ui_context->font_samplers, 0, NULL); - vkCmdBindDescriptorSets(command_buffer, VK_PIPELINE_BIND_POINT_GRAPHICS, ui_context->char_pipeline.layout, 1, 1, &ui_context->font_textures, 0, NULL); for(uint32_t i = 0; i < ui_layer_count; i++) { push[1] = ui_layers[i].address; vkCmdPushConstants(command_buffer, ui_context->rect_pipeline.layout, VK_SHADER_STAGE_VERTEX_BIT | VK_SHADER_STAGE_FRAGMENT_BIT, 0, 16, push); vkCmdBindPipeline(command_buffer, VK_PIPELINE_BIND_POINT_GRAPHICS, ui_context->rect_pipeline.pipeline); vkCmdDrawIndirect(command_buffer, ui_layers[i].layer, offsetof(UILayer, draw_rects), 1, 0); vkCmdBindPipeline(command_buffer, VK_PIPELINE_BIND_POINT_GRAPHICS, ui_context->char_pipeline.pipeline); + if(i == 0) { + vkCmdBindDescriptorSets(command_buffer, VK_PIPELINE_BIND_POINT_GRAPHICS, ui_context->char_pipeline.layout, 0, 1, &ui_context->font_samplers, 0, NULL); + vkCmdBindDescriptorSets(command_buffer, VK_PIPELINE_BIND_POINT_GRAPHICS, ui_context->char_pipeline.layout, 1, 1, &ui_context->font_textures, 0, NULL); + } vkCmdDrawIndirect(command_buffer, ui_layers[i].layer, offsetof(UILayer, draw_glyphs), 1, 0); } diff --git a/client/src/ui.c b/client/src/ui.c index 2e07e54..a6cc416 100644 --- a/client/src/ui.c +++ b/client/src/ui.c @@ -474,9 +474,9 @@ VkResult load_font( FT_Load_Glyph(face, glyph_index, load_flags); for(uint32_t y = 0; y < face->glyph->bitmap.rows; y++) { for(uint32_t x = 0; x < face->glyph->bitmap.width; x++) { - uint32_t level = face->glyph->bitmap.buffer[y*face->glyph->bitmap.width+x]; - level = ((level * 0xFFFFFF) / 256) << 8; - images[max_width*max_height*i + max_width*y + x] = 0x000000FF + level; + uint64_t level = face->glyph->bitmap.buffer[y*face->glyph->bitmap.width+x]; + level = ((level * 0xFFFFFFFF) / 256); + images[max_width*max_height*i + max_width*y + x] = level; } } }