Started hex drawing

main
noah metz 2024-10-30 21:24:03 -06:00
parent 62d17bee4a
commit 43c5ff71ad
10 changed files with 111 additions and 16 deletions

@ -7,6 +7,8 @@
VkResult draw_frame( VkResult draw_frame(
RenderContext* context, RenderContext* context,
UIContext* ui, UIContext* ui,
GraphicsPipeline* hex_graphics,
VkDeviceAddress world,
double time); double time);
#endif #endif

@ -32,7 +32,7 @@
#define VK_RESULT(x) {\ #define VK_RESULT(x) {\
result = x;\ result = x;\
if(result != VK_SUCCESS) {\ if(result != VK_SUCCESS) {\
fprintf(stderr, "%s failed with %s\n", #x, string_VkResult(result));\ fprintf(stderr, "%s:%d %s failed with %s\n", __FILE__, __LINE__, #x, string_VkResult(result));\
return x;\ return x;\
}\ }\
} }

@ -3,10 +3,15 @@
#include "gpu.h" #include "gpu.h"
typedef struct HexPushStruct { typedef struct GPUHexContextStruct {
mat4 proj;
mat4 view;
} GPUHexContext;
typedef struct HexPushConstantStruct {
VkDeviceAddress context; VkDeviceAddress context;
VkDeviceAddress world; double time;
} HexPush; } HexPushConstant;
VkResult create_hex_pipeline( VkResult create_hex_pipeline(
VkDevice device, VkDevice device,

@ -0,0 +1,6 @@
#version 450
#extension GL_EXT_buffer_reference : require
void main() {
}

@ -0,0 +1,8 @@
#version 450
#extension GL_EXT_buffer_reference : require
layout(location = 0) out vec4 outColor;
void main() {
outColor = vec4(1, 1, 1, 1);
}

@ -0,0 +1,30 @@
#version 450
#extension GL_EXT_buffer_reference : require
#include "hex_common.glsl"
#define PI 3.1415926535897932384626433832795
float w = 0.5;
vec4 vertices[] = {
vec4(0, 0, 0, 1),
vec4(w*cos(0*PI/3), w*sin(0*PI/3), 0, 1),
vec4(w*cos(1*PI/3), w*sin(1*PI/3), 0, 1),
vec4(w*cos(2*PI/3), w*sin(2*PI/3), 0, 1),
vec4(w*cos(3*PI/3), w*sin(3*PI/3), 0, 1),
vec4(w*cos(4*PI/3), w*sin(4*PI/3), 0, 1),
vec4(w*cos(5*PI/3), w*sin(5*PI/3), 0, 1),
};
uint indices[] = {
0, 1, 2,
0, 2, 3,
0, 3, 4,
0, 4, 5,
0, 5, 6,
0, 6, 1,
};
void main() {
gl_Position = pc.context.proj * pc.context.view * vertices[indices[gl_VertexIndex]];
}

@ -0,0 +1,9 @@
layout(std430, buffer_reference) readonly buffer HexContext {
mat4 proj;
mat4 view;
};
layout(std430, push_constant) uniform PushConstant {
HexContext context;
float time;
} pc;

@ -1,5 +1,6 @@
#include "draw.h" #include "draw.h"
#include "gpu.h" #include "gpu.h"
#include "hex.h"
#include "vulkan/vulkan_core.h" #include "vulkan/vulkan_core.h"
void record_ui_draw(VkCommandBuffer command_buffer, UIContext* ui_context, double time, uint32_t frame) { void record_ui_draw(VkCommandBuffer command_buffer, UIContext* ui_context, double time, uint32_t frame) {
@ -23,6 +24,16 @@ void record_ui_draw(VkCommandBuffer command_buffer, UIContext* ui_context, doubl
} }
} }
void record_hex_draw(VkCommandBuffer command_buffer, GraphicsPipeline* graphics, VkDeviceAddress context, double time) {
HexPushConstant push = {
.context = context,
.time = time,
};
vkCmdPushConstants(command_buffer, graphics->layout, VK_SHADER_STAGE_VERTEX_BIT, 0, sizeof(HexPushConstant), &push);
vkCmdBindPipeline(command_buffer, VK_PIPELINE_BIND_POINT_GRAPHICS, graphics->pipeline);
vkCmdDraw(command_buffer, 18, 1, 0, 0);
}
void record_ui_compute(VkCommandBuffer command_buffer, UIContext* ui, uint32_t frame) { void record_ui_compute(VkCommandBuffer command_buffer, UIContext* ui, uint32_t frame) {
UIPushConstant push = { UIPushConstant push = {
.time = 0.0, .time = 0.0,
@ -54,6 +65,8 @@ void record_ui_compute(VkCommandBuffer command_buffer, UIContext* ui, uint32_t f
VkResult draw_frame( VkResult draw_frame(
RenderContext* context, RenderContext* context,
UIContext* ui, UIContext* ui,
GraphicsPipeline* hex_graphics,
VkDeviceAddress hex_context,
double time) { double time) {
VkResult result; VkResult result;
@ -173,6 +186,7 @@ VkResult draw_frame(
}; };
vkCmdBeginRenderPass(command_buffer, &render_pass_begin, VK_SUBPASS_CONTENTS_INLINE); vkCmdBeginRenderPass(command_buffer, &render_pass_begin, VK_SUBPASS_CONTENTS_INLINE);
record_hex_draw(command_buffer, hex_graphics, hex_context, time);
vkCmdNextSubpass(command_buffer, VK_SUBPASS_CONTENTS_INLINE); vkCmdNextSubpass(command_buffer, VK_SUBPASS_CONTENTS_INLINE);
record_ui_draw(command_buffer, ui, time, context->current_frame); record_ui_draw(command_buffer, ui, time, context->current_frame);
vkCmdEndRenderPass(command_buffer); vkCmdEndRenderPass(command_buffer);

@ -23,15 +23,15 @@ VkResult create_hex_pipeline(
.module = compute_shader, .module = compute_shader,
}; };
VkPushConstantRange push_constant = { VkPushConstantRange compute_push = {
.size = sizeof(HexPush), .size = sizeof(HexPushConstant),
.offset = 0, .offset = 0,
.stageFlags = VK_SHADER_STAGE_COMPUTE_BIT | VK_SHADER_STAGE_VERTEX_BIT, .stageFlags = VK_SHADER_STAGE_COMPUTE_BIT,
}; };
VkPipelineLayoutCreateInfo compute_layout_info = { VkPipelineLayoutCreateInfo compute_layout_info = {
.sType = VK_STRUCTURE_TYPE_PIPELINE_LAYOUT_CREATE_INFO, .sType = VK_STRUCTURE_TYPE_PIPELINE_LAYOUT_CREATE_INFO,
.pPushConstantRanges = &push_constant, .pPushConstantRanges = &compute_push,
.pushConstantRangeCount = 1, .pushConstantRangeCount = 1,
}; };
@ -65,9 +65,15 @@ VkResult create_hex_pipeline(
}, },
}; };
VkPushConstantRange graphics_push = {
.size = sizeof(HexPushConstant),
.offset = 0,
.stageFlags = VK_SHADER_STAGE_VERTEX_BIT,
};
VkPipelineLayoutCreateInfo graphics_layout_info = { VkPipelineLayoutCreateInfo graphics_layout_info = {
.sType = VK_STRUCTURE_TYPE_PIPELINE_LAYOUT_CREATE_INFO, .sType = VK_STRUCTURE_TYPE_PIPELINE_LAYOUT_CREATE_INFO,
.pPushConstantRanges = &push_constant, .pPushConstantRanges = &graphics_push,
.pushConstantRangeCount = 1, .pushConstantRangeCount = 1,
}; };
@ -113,7 +119,7 @@ VkResult create_hex_pipeline(
VkPipelineRasterizationStateCreateInfo raster_info = { VkPipelineRasterizationStateCreateInfo raster_info = {
.sType = VK_STRUCTURE_TYPE_PIPELINE_RASTERIZATION_STATE_CREATE_INFO, .sType = VK_STRUCTURE_TYPE_PIPELINE_RASTERIZATION_STATE_CREATE_INFO,
.depthClampEnable = VK_TRUE, .depthClampEnable = VK_FALSE,
.rasterizerDiscardEnable = VK_FALSE, .rasterizerDiscardEnable = VK_FALSE,
.polygonMode = VK_POLYGON_MODE_FILL, .polygonMode = VK_POLYGON_MODE_FILL,
.lineWidth = 1.0f, .lineWidth = 1.0f,
@ -170,10 +176,6 @@ VkResult create_hex_pipeline(
.depthTestEnable = VK_TRUE, .depthTestEnable = VK_TRUE,
.depthWriteEnable = VK_TRUE, .depthWriteEnable = VK_TRUE,
.depthCompareOp = VK_COMPARE_OP_LESS, .depthCompareOp = VK_COMPARE_OP_LESS,
.depthBoundsTestEnable = VK_TRUE,
.minDepthBounds = 0.0f,
.maxDepthBounds = 1.0f,
.stencilTestEnable = VK_FALSE,
}; };
VkGraphicsPipelineCreateInfo graphics_pipeline_info = { VkGraphicsPipelineCreateInfo graphics_pipeline_info = {

@ -1,4 +1,5 @@
#include "GLFW/glfw3.h" #include "GLFW/glfw3.h"
#include "cglm/mat4.h"
#include "ui.h" #include "ui.h"
#include "gpu.h" #include "gpu.h"
#include "draw.h" #include "draw.h"
@ -17,6 +18,13 @@ typedef struct ClientContextStruct {
uint32_t clicked_container; uint32_t clicked_container;
uint32_t clicked_element; uint32_t clicked_element;
vec3 pos;
vec4 rot;
double delta_time;
VkBuffer hex_context;
VmaAllocation hex_context_memory;
GPUHexContext hex;
} ClientContext; } ClientContext;
void* network_thread(void* data) { void* network_thread(void* data) {
@ -102,18 +110,28 @@ VkResult main_thread(ClientContext* context) {
4*sizeof(uint32_t), 4*sizeof(uint32_t),
&context->render)); &context->render));
VK_RESULT(create_storage_buffer(context->render.allocator, 0, sizeof(GPUHexContext), &context->hex_context, &context->hex_context_memory));
VkDeviceAddress hex_context_address = buffer_address(context->render.device, context->hex_context);
glm_ortho_default((float)context->render.swapchain_extent.width/(float)context->render.swapchain_extent.height, context->hex.proj);
glm_translate_make(context->hex.view, context->pos);
add_transfer(&context->hex, context->hex_context, 0, sizeof(GPUHexContext), 0, &context->render);
add_transfer(&context->hex, context->hex_context, 0, sizeof(GPUHexContext), 1, &context->render);
// //
uint32_t* mapped_codes = context->ui.containers[0].layers[0].codes_buffer; uint32_t* mapped_codes = context->ui.containers[0].layers[0].codes_buffer;
GPUString* mapped_string = context->ui.containers[0].layers[0].strings_buffer; GPUString* mapped_string = context->ui.containers[0].layers[0].strings_buffer;
char str[21]; char str[21];
// //
double last_frame_time = glfwGetTime();
while(glfwWindowShouldClose(context->window) == 0) { while(glfwWindowShouldClose(context->window) == 0) {
double frame_time = glfwGetTime();
context->delta_time = (frame_time - last_frame_time);
context->clicked_element = 0x00000000; context->clicked_element = 0x00000000;
context->clicked_container = 0x00000000; context->clicked_container = 0x00000000;
glfwPollEvents(); glfwPollEvents();
double frame_time = glfwGetTime();
if(context->clicked_container != 0) { if(context->clicked_container != 0) {
snprintf(str, 21, "Clicked: %d.%d", context->clicked_container, context->clicked_element); snprintf(str, 21, "Clicked: %d.%d", context->clicked_container, context->clicked_element);
@ -135,12 +153,13 @@ VkResult main_thread(ClientContext* context) {
} }
// //
VkResult result = draw_frame(&context->render, &context->ui, frame_time); VkResult result = draw_frame(&context->render, &context->ui, &graphics, hex_context_address, frame_time);
if(result != VK_SUCCESS) { if(result != VK_SUCCESS) {
fprintf(stderr, "draw_frame error: %s\n", string_VkResult(result)); fprintf(stderr, "draw_frame error: %s\n", string_VkResult(result));
glfwDestroyWindow(context->window); glfwDestroyWindow(context->window);
} }
last_frame_time = frame_time;
} }
return 0; return 0;