Some changes

main
noah metz 2024-10-14 19:42:38 -06:00
parent 5260a59db5
commit 38b7a2f954
5 changed files with 60 additions and 35 deletions

@ -6,6 +6,11 @@
#include "vk_mem_alloc.h"
#include "command.h"
typedef struct ComputePipelineStruct {
VkPipelineLayout layout;
VkPipeline pipeline;
} ComputePipeline;
typedef struct GraphicsPipelineStruct {
VkPipelineLayout layout;
VkPipeline pipeline;
@ -21,10 +26,18 @@ typedef struct UIUniformStruct {
mat4 screen;
} UIUniform;
typedef struct CharStruct {
typedef struct TextStruct {
vec3 pos;
vec2 size;
vec4 color;
uint32_t length;
uint32_t offset;
} Text;
typedef struct CharStruct {
vec3 pos;
vec4 color;
vec2 size;
uint32_t code;
} Char;
@ -93,6 +106,7 @@ typedef struct UIContextStruct {
struct RectBuffer ui_rect;
GraphicsPipeline ui_pipeline_rect;
GraphicsPipeline ui_pipeline_text;
ComputePipeline ui_compute_text;
} UIContext;
VkResult init_pipelines(VkDevice device, VmaAllocator allocator, VkExtent2D swapchain_extent, VkRenderPass render_pass, Queue transfer_queue, VkCommandPool transfer_pool, UIContext* context);

@ -176,24 +176,24 @@ VkResult render_thread(GLFWwindow* window, RenderContext* render_context) {
return result;
}
text[0].size[0] = 200.0f;
text[0].size[1] = 200.0f;
text[0].color[0] = 1.0f;
text[0].color[1] = 1.0f;
text[0].color[2] = 1.0f;
text[0].color[3] = 1.0f;
text[0].pos[0] = 200.0f;
text[0].pos[1] = 200.0f;
text[0].size[0] = 200.0f;
text[0].size[1] = 200.0f;
text[0].code = 1;
text[1].size[0] = 200.0f;
text[1].size[1] = 200.0f;
text[1].color[0] = 1.0f;
text[1].color[1] = 1.0f;
text[1].color[2] = 1.0f;
text[1].color[3] = 1.0f;
text[1].pos[0] = 400.0f;
text[1].pos[1] = 200.0f;
text[1].size[0] = 200.0f;
text[1].size[1] = 200.0f;
text[1].code = 14;
vmaUnmapMemory(render_context->allocator, text_memory);

@ -285,7 +285,7 @@ VkPipelineVertexInputStateCreateInfo input_info = {
return VK_SUCCESS;
}
VkResult create_ui_text_pipeline(VkDevice device, VkRenderPass render_pass, VkDescriptorSetLayout ui_descriptor_layout, VkDescriptorSetLayout text_layout, GraphicsPipeline* pipeline) {
VkResult create_ui_text_pipeline(VkDevice device, VkRenderPass render_pass, VkDescriptorSetLayout ui_descriptor_layout, VkDescriptorSetLayout font_layout, GraphicsPipeline* pipeline, ComputePipeline* compute) {
VkShaderModule vert_shader = load_shader_file("shader_src/ui_text.vert.spv", device);
if(vert_shader == VK_NULL_HANDLE) {
return VK_ERROR_UNKNOWN;
@ -366,7 +366,7 @@ VkResult create_ui_text_pipeline(VkDevice device, VkRenderPass render_pass, VkDe
.vertexAttributeDescriptionCount = sizeof(attributes)/sizeof(VkVertexInputAttributeDescription),
};
VkDescriptorSetLayout all_layouts[] = {ui_descriptor_layout, text_layout};
VkDescriptorSetLayout all_layouts[] = {ui_descriptor_layout, font_layout};
VkPipelineLayoutCreateInfo layout_info = {
.sType = VK_STRUCTURE_TYPE_PIPELINE_LAYOUT_CREATE_INFO,
.setLayoutCount = sizeof(all_layouts)/sizeof(VkDescriptorSetLayout),
@ -387,9 +387,9 @@ VkResult create_ui_text_pipeline(VkDevice device, VkRenderPass render_pass, VkDe
return VK_SUCCESS;
}
VkResult create_text_descriptor_pool(VkDevice device, uint32_t max_sets, VkDescriptorPool* pool, VkDescriptorSetLayout* text_descriptor_layout) {
VkResult create_text_descriptor_pools(VkDevice device, uint32_t max_sets, VkDescriptorPool* font_pool, VkDescriptorSetLayout* font_layout) {
VkResult result;
VkDescriptorPoolSize pool_sizes[] = {
VkDescriptorPoolSize font_pool_sizes[] = {
{
.type = VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER,
.descriptorCount = 1,
@ -397,22 +397,22 @@ VkResult create_text_descriptor_pool(VkDevice device, uint32_t max_sets, VkDescr
{
.type = VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER,
.descriptorCount = 1,
}
},
};
VkDescriptorPoolCreateInfo pool_info = {
VkDescriptorPoolCreateInfo font_pool_info = {
.sType = VK_STRUCTURE_TYPE_DESCRIPTOR_POOL_CREATE_INFO,
.pPoolSizes = pool_sizes,
.poolSizeCount = sizeof(pool_sizes)/sizeof(VkDescriptorPoolSize),
.pPoolSizes = font_pool_sizes,
.poolSizeCount = sizeof(font_pool_sizes)/sizeof(VkDescriptorPoolSize),
.maxSets = max_sets,
};
result = vkCreateDescriptorPool(device, &pool_info, NULL, pool);
result = vkCreateDescriptorPool(device, &font_pool_info, NULL, font_pool);
if(result != VK_SUCCESS) {
return result;
}
VkDescriptorSetLayoutBinding text_descriptor_bindings[] = {
VkDescriptorSetLayoutBinding font_descriptor_bindings[] = {
{
.binding = 0,
.descriptorType = VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER,
@ -424,21 +424,20 @@ VkResult create_text_descriptor_pool(VkDevice device, uint32_t max_sets, VkDescr
.descriptorType = VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER,
.descriptorCount = 1,
.stageFlags = VK_SHADER_STAGE_FRAGMENT_BIT,
}
},
};
VkDescriptorSetLayoutCreateInfo text_descriptor_info = {
VkDescriptorSetLayoutCreateInfo font_descriptor_info = {
.sType = VK_STRUCTURE_TYPE_DESCRIPTOR_SET_LAYOUT_CREATE_INFO,
.pBindings = text_descriptor_bindings,
.bindingCount = sizeof(text_descriptor_bindings)/sizeof(VkDescriptorSetLayoutBinding),
.pBindings = font_descriptor_bindings,
.bindingCount = sizeof(font_descriptor_bindings)/sizeof(VkDescriptorSetLayoutBinding),
};
result = vkCreateDescriptorSetLayout(device, &text_descriptor_info, NULL, text_descriptor_layout);
result = vkCreateDescriptorSetLayout(device, &font_descriptor_info, NULL, font_layout);
if(result != VK_SUCCESS) {
return result;
}
return VK_SUCCESS;
}
@ -472,7 +471,7 @@ VkResult create_text_descriptor(VkDevice device, VmaAllocator allocator, VkDescr
VkBufferCreateInfo uniform_buffer_info = {
.sType = VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO,
.usage = VK_BUFFER_USAGE_UNIFORM_BUFFER_BIT,
.usage = VK_BUFFER_USAGE_TRANSFER_DST_BIT | VK_BUFFER_USAGE_UNIFORM_BUFFER_BIT,
.size = sizeof(FontUniform),
.sharingMode = VK_SHARING_MODE_EXCLUSIVE,
};
@ -495,6 +494,7 @@ VkResult create_text_descriptor(VkDevice device, VmaAllocator allocator, VkDescr
.tiling = VK_IMAGE_TILING_OPTIMAL,
.initialLayout = VK_IMAGE_LAYOUT_UNDEFINED,
.samples = VK_SAMPLE_COUNT_1_BIT,
.imageType = VK_IMAGE_TYPE_2D,
};
VmaAllocationCreateInfo image_memory_info = {
@ -605,6 +605,7 @@ VkResult create_text_descriptor(VkDevice device, VmaAllocator allocator, VkDescr
.viewType = VK_IMAGE_VIEW_TYPE_2D,
.format = VK_FORMAT_R8G8B8A8_SRGB,
.subresourceRange = {
.aspectMask = VK_IMAGE_ASPECT_COLOR_BIT,
.layerCount = 1,
.baseMipLevel = 0,
.levelCount = 1,
@ -890,12 +891,12 @@ VkResult init_pipelines(VkDevice device, VmaAllocator allocator, VkExtent2D swap
return result;
}
result = create_text_descriptor_pool(device, 10, &context->font_pool, &context->font_layout);
result = create_text_descriptor_pools(device, 10, &context->font_pool, &context->font_layout);
if(result != VK_SUCCESS) {
return result;
}
result = create_ui_text_pipeline(device, render_pass, context->ui_descriptor_layout, context->font_layout, &context->ui_pipeline_text);
result = create_ui_text_pipeline(device, render_pass, context->ui_descriptor_layout, context->font_layout, &context->ui_pipeline_text, &context->ui_compute_text);
if(result != VK_SUCCESS) {
return result;
}

@ -16,6 +16,7 @@ uint32_t validation_layer_count = sizeof(validation_layers) / sizeof(const char
const char * instance_extensions[] = {
VK_KHR_PORTABILITY_ENUMERATION_EXTENSION_NAME,
VK_EXT_DEBUG_UTILS_EXTENSION_NAME,
VK_EXT_DEBUG_REPORT_EXTENSION_NAME,
"VK_EXT_metal_surface",
VK_KHR_SURFACE_EXTENSION_NAME,
};
@ -333,10 +334,6 @@ VkResult create_logical_device(VkPhysicalDevice physical_device, VkSurfaceKHR su
queue_create_info[1].pQueuePriorities = &default_queue_priority;
}
VkPhysicalDeviceVulkan13Features features_13 = {
.dynamicRendering = VK_TRUE,
};
VkPhysicalDeviceVulkan12Features features_12 = {
.sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_VULKAN_1_2_FEATURES,
.bufferDeviceAddress = VK_TRUE,
@ -346,7 +343,6 @@ VkResult create_logical_device(VkPhysicalDevice physical_device, VkSurfaceKHR su
.descriptorBindingUniformBufferUpdateAfterBind = VK_TRUE,
.descriptorBindingStorageBufferUpdateAfterBind = VK_TRUE,
.descriptorBindingSampledImageUpdateAfterBind = VK_TRUE,
.pNext = &features_13,
};
VkPhysicalDeviceFeatures device_features = {
@ -574,7 +570,7 @@ VkResult find_depth_format(VkPhysicalDevice physical_device, VkImageTiling tilin
return VK_ERROR_UNKNOWN;
}
VkResult create_render_pass(VkDevice device, VkSurfaceFormatKHR format, VkFormat depth_format, VkRenderPass* render_pass, VkAttachmentLoadOp color_load_op) {
VkResult create_render_pass(VkDevice device, VkSurfaceFormatKHR format, VkFormat depth_format, VkRenderPass* render_pass, VkImageLayout initial_layout, VkImageLayout final_layout, VkAttachmentLoadOp color_load_op) {
VkAttachmentDescription attachments[] = {
{
.format = format.format,
@ -583,8 +579,8 @@ VkResult create_render_pass(VkDevice device, VkSurfaceFormatKHR format, VkFormat
.storeOp = VK_ATTACHMENT_STORE_OP_STORE,
.stencilLoadOp = VK_ATTACHMENT_LOAD_OP_CLEAR,
.stencilStoreOp = VK_ATTACHMENT_STORE_OP_STORE,
.initialLayout = VK_IMAGE_LAYOUT_UNDEFINED,
.finalLayout = VK_IMAGE_LAYOUT_PRESENT_SRC_KHR,
.initialLayout = initial_layout,
.finalLayout = final_layout,
},
{
.format = depth_format,
@ -930,12 +926,12 @@ VkResult init_vulkan(GLFWwindow* window, RenderContext* context) {
return result;
}
result = create_render_pass(context->device, context->swapchain_format, context->depth_format, &context->world_render_pass, VK_ATTACHMENT_LOAD_OP_CLEAR);
result = create_render_pass(context->device, context->swapchain_format, context->depth_format, &context->world_render_pass, VK_IMAGE_LAYOUT_UNDEFINED, VK_IMAGE_LAYOUT_PRESENT_SRC_KHR, VK_ATTACHMENT_LOAD_OP_CLEAR);
if(result != VK_SUCCESS) {
return result;
}
result = create_render_pass(context->device, context->swapchain_format, context->depth_format, &context->ui_render_pass, VK_ATTACHMENT_LOAD_OP_LOAD);
result = create_render_pass(context->device, context->swapchain_format, context->depth_format, &context->ui_render_pass, VK_IMAGE_LAYOUT_PRESENT_SRC_KHR, VK_IMAGE_LAYOUT_PRESENT_SRC_KHR, VK_ATTACHMENT_LOAD_OP_LOAD);
if(result != VK_SUCCESS) {
return result;
}
@ -987,6 +983,19 @@ VkResult draw_frame(RenderContext* context, UIContext* ui_context, UILayer* ui_l
return result;
}
VkViewport viewport = {
.width = context->swapchain_extent.width,
.height = context->swapchain_extent.height,
.maxDepth = 1.0f,
.minDepth = 0.0f,
};
vkCmdSetViewport(context->swapchain_command_buffers[context->current_frame], 0, 1, &viewport);
VkRect2D scissor = {
.extent = context->swapchain_extent,
};
vkCmdSetScissor(context->swapchain_command_buffers[context->current_frame], 0, 1, &scissor);
VkClearValue clear_values[2] = {{.color={{0.0f, 0.0f, 0.0f, 1.0f}}}, {.depthStencil={1.0f, 0.0f}}};
VkDeviceSize offset = 0;

@ -54,6 +54,7 @@ keycode_list.sort()
for keycode in keycode_list:
draw.text((x, -tops[keycode]), chr(keycode), font=font)
x += widths[keycode]
print(f"{chr(keycode)} - {tops[keycode]}")
image.save(args.atlas)