diff --git a/client/include/draw.h b/client/include/draw.h index bca234e..7faf535 100644 --- a/client/include/draw.h +++ b/client/include/draw.h @@ -7,6 +7,8 @@ VkResult draw_frame( RenderContext* context, UIContext* ui, + GraphicsPipeline* hex_graphics, + VkDeviceAddress world, double time); #endif diff --git a/client/include/gpu.h b/client/include/gpu.h index 06f8ea7..a49d408 100644 --- a/client/include/gpu.h +++ b/client/include/gpu.h @@ -32,7 +32,7 @@ #define VK_RESULT(x) {\ result = x;\ 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;\ }\ } diff --git a/client/include/hex.h b/client/include/hex.h index 4fa8e75..e5d85e7 100644 --- a/client/include/hex.h +++ b/client/include/hex.h @@ -3,10 +3,15 @@ #include "gpu.h" -typedef struct HexPushStruct { +typedef struct GPUHexContextStruct { + mat4 proj; + mat4 view; +} GPUHexContext; + +typedef struct HexPushConstantStruct { VkDeviceAddress context; - VkDeviceAddress world; -} HexPush; + double time; +} HexPushConstant; VkResult create_hex_pipeline( VkDevice device, diff --git a/client/shader/hex.comp b/client/shader/hex.comp new file mode 100644 index 0000000..96947ba --- /dev/null +++ b/client/shader/hex.comp @@ -0,0 +1,6 @@ +#version 450 +#extension GL_EXT_buffer_reference : require + +void main() { + +} diff --git a/client/shader/hex.frag b/client/shader/hex.frag new file mode 100644 index 0000000..615f1b2 --- /dev/null +++ b/client/shader/hex.frag @@ -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); +} diff --git a/client/shader/hex.vert b/client/shader/hex.vert new file mode 100644 index 0000000..3bef57a --- /dev/null +++ b/client/shader/hex.vert @@ -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]]; +} diff --git a/client/shader/hex_common.glsl b/client/shader/hex_common.glsl new file mode 100644 index 0000000..39abf2e --- /dev/null +++ b/client/shader/hex_common.glsl @@ -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; diff --git a/client/src/draw.c b/client/src/draw.c index d16b50d..bc19aa8 100644 --- a/client/src/draw.c +++ b/client/src/draw.c @@ -1,5 +1,6 @@ #include "draw.h" #include "gpu.h" +#include "hex.h" #include "vulkan/vulkan_core.h" 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) { UIPushConstant push = { .time = 0.0, @@ -54,6 +65,8 @@ void record_ui_compute(VkCommandBuffer command_buffer, UIContext* ui, uint32_t f VkResult draw_frame( RenderContext* context, UIContext* ui, + GraphicsPipeline* hex_graphics, + VkDeviceAddress hex_context, double time) { VkResult result; @@ -173,6 +186,7 @@ VkResult draw_frame( }; 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); record_ui_draw(command_buffer, ui, time, context->current_frame); vkCmdEndRenderPass(command_buffer); diff --git a/client/src/hex.c b/client/src/hex.c index 803088f..c752342 100644 --- a/client/src/hex.c +++ b/client/src/hex.c @@ -23,15 +23,15 @@ VkResult create_hex_pipeline( .module = compute_shader, }; - VkPushConstantRange push_constant = { - .size = sizeof(HexPush), + VkPushConstantRange compute_push = { + .size = sizeof(HexPushConstant), .offset = 0, - .stageFlags = VK_SHADER_STAGE_COMPUTE_BIT | VK_SHADER_STAGE_VERTEX_BIT, + .stageFlags = VK_SHADER_STAGE_COMPUTE_BIT, }; VkPipelineLayoutCreateInfo compute_layout_info = { .sType = VK_STRUCTURE_TYPE_PIPELINE_LAYOUT_CREATE_INFO, - .pPushConstantRanges = &push_constant, + .pPushConstantRanges = &compute_push, .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 = { .sType = VK_STRUCTURE_TYPE_PIPELINE_LAYOUT_CREATE_INFO, - .pPushConstantRanges = &push_constant, + .pPushConstantRanges = &graphics_push, .pushConstantRangeCount = 1, }; @@ -113,7 +119,7 @@ VkResult create_hex_pipeline( VkPipelineRasterizationStateCreateInfo raster_info = { .sType = VK_STRUCTURE_TYPE_PIPELINE_RASTERIZATION_STATE_CREATE_INFO, - .depthClampEnable = VK_TRUE, + .depthClampEnable = VK_FALSE, .rasterizerDiscardEnable = VK_FALSE, .polygonMode = VK_POLYGON_MODE_FILL, .lineWidth = 1.0f, @@ -170,10 +176,6 @@ VkResult create_hex_pipeline( .depthTestEnable = VK_TRUE, .depthWriteEnable = VK_TRUE, .depthCompareOp = VK_COMPARE_OP_LESS, - .depthBoundsTestEnable = VK_TRUE, - .minDepthBounds = 0.0f, - .maxDepthBounds = 1.0f, - .stencilTestEnable = VK_FALSE, }; VkGraphicsPipelineCreateInfo graphics_pipeline_info = { diff --git a/client/src/main.c b/client/src/main.c index 7ef74f9..3882c9c 100644 --- a/client/src/main.c +++ b/client/src/main.c @@ -1,4 +1,5 @@ #include "GLFW/glfw3.h" +#include "cglm/mat4.h" #include "ui.h" #include "gpu.h" #include "draw.h" @@ -17,6 +18,13 @@ typedef struct ClientContextStruct { uint32_t clicked_container; uint32_t clicked_element; + + vec3 pos; + vec4 rot; + double delta_time; + VkBuffer hex_context; + VmaAllocation hex_context_memory; + GPUHexContext hex; } ClientContext; void* network_thread(void* data) { @@ -102,18 +110,28 @@ VkResult main_thread(ClientContext* context) { 4*sizeof(uint32_t), &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; GPUString* mapped_string = context->ui.containers[0].layers[0].strings_buffer; char str[21]; // + double last_frame_time = glfwGetTime(); while(glfwWindowShouldClose(context->window) == 0) { + double frame_time = glfwGetTime(); + context->delta_time = (frame_time - last_frame_time); + context->clicked_element = 0x00000000; context->clicked_container = 0x00000000; glfwPollEvents(); - double frame_time = glfwGetTime(); if(context->clicked_container != 0) { 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) { fprintf(stderr, "draw_frame error: %s\n", string_VkResult(result)); glfwDestroyWindow(context->window); } + last_frame_time = frame_time; } return 0;