From 74cccfe065740e1af2002db1c1d61d45e77423d5 Mon Sep 17 00:00:00 2001 From: Noah Metz Date: Fri, 1 Nov 2024 17:34:41 -0600 Subject: [PATCH] Rendering hexagon regions --- client/include/hex.h | 61 +++++++++++++++++++---------------- client/shader/hex.frag | 5 +-- client/shader/hex.vert | 31 +++++++++++++++++- client/shader/hex_common.glsl | 19 ++++------- client/src/draw.c | 3 +- client/src/hex.c | 32 ++++++++++++------ client/src/main.c | 19 ++++++++--- client/src/ui.c | 5 --- 8 files changed, 112 insertions(+), 63 deletions(-) diff --git a/client/include/hex.h b/client/include/hex.h index 554ea8d..a455854 100644 --- a/client/include/hex.h +++ b/client/include/hex.h @@ -4,17 +4,34 @@ #include "gpu.h" #define REGION_SIZE 6 -#define REGION_HEX_COUNT 3*REGION_SIZE*(REGION_SIZE-1)+1 +#define REGION_HEX_COUNT (3*REGION_SIZE*(REGION_SIZE-1)+1) +#define MAX_LOADED_REGIONS 100 -typedef struct HexPushConstantStruct { - VkDeviceAddress context; - double time; -} HexPushConstant; +typedef struct GPUHexStruct { + float height[6]; + uint32_t color[6]; +} GPUHex; + +typedef struct GPUHexRegionStruct { + uint32_t q; + uint32_t r; + + GPUHex hexes[REGION_HEX_COUNT]; +} GPUHexRegion; + +typedef struct HexRegionStruct { + uint32_t q; + uint32_t r; + + VkDeviceAddress address; + VkBuffer region; + VmaAllocation region_memory; +} HexRegion; typedef struct GPUHexContextStruct { mat4 proj; mat4 view; - VkDeviceAddress regions; + VkDeviceAddress regions[MAX_LOADED_REGIONS]; } GPUHexContext; typedef struct HexContextStruct { @@ -25,37 +42,25 @@ typedef struct HexContextStruct { GraphicsPipeline graphics; ComputePipeline compute; + HexRegion regions[MAX_LOADED_REGIONS]; + GPUHexContext data; } HexContext; -typedef struct GPUHexStruct { - float height[6]; - uint32_t color[6]; -} GPUHex; - -typedef struct GPUHexRegionStruct { - uint32_t x; - uint32_t y; - - GPUHex hexes[REGION_HEX_COUNT]; -} GPUHexRegion; - -typedef struct HexRegionStruct { - uint32_t x; - uint32_t y; - - VkDeviceAddress address; - VkBuffer region; - VmaAllocation region_memory; -} HexRegion; +typedef struct HexPushConstantStruct { + VkDeviceAddress context; + uint32_t region; + double time; +} HexPushConstant; VkResult create_hex_context( RenderContext* gpu, HexContext* context); VkResult create_hex_region( - uint32_t x, uint32_t y, - HexRegion* region, + uint32_t q, uint32_t s, + HexRegion** region, + HexContext* hex, RenderContext* gpu); #endif diff --git a/client/shader/hex.frag b/client/shader/hex.frag index 615f1b2..ec38044 100644 --- a/client/shader/hex.frag +++ b/client/shader/hex.frag @@ -1,8 +1,9 @@ #version 450 #extension GL_EXT_buffer_reference : require -layout(location = 0) out vec4 outColor; +layout(location = 0) out vec4 out_color; +layout(location = 0) in vec4 in_color; void main() { - outColor = vec4(1, 1, 1, 1); + out_color = in_color; } diff --git a/client/shader/hex.vert b/client/shader/hex.vert index e4d0b88..62d323a 100644 --- a/client/shader/hex.vert +++ b/client/shader/hex.vert @@ -45,7 +45,19 @@ const vec4 direction[] = { vec4(-x, 0, z, 0), }; +layout(location=0) out vec4 color; + +vec4 int2color(uint color_int) { + float r = float((color_int >> 24) & 0xFF)/255.0; + float g = float((color_int >> 16) & 0xFF)/255.0; + float b = float((color_int >> 8) & 0xFF)/255.0; + float a = float((color_int >> 0) & 0xFF)/255.0; + return vec4(r, g, b, a); +} + void main() { + Region region = pc.context.regions[pc.region]; + uint radius = 0; uint ring = 0; uint side = 0; @@ -57,8 +69,25 @@ void main() { hex = ring - (radius*side); } - // Calculate position based on radius/ring/side/hex vec4 position = vertices[indices[gl_VertexIndex]] + (starts[side]*radius) + (direction[side]*hex); + if(gl_VertexIndex % 3 == 0) { + position.y = (region.hexes[gl_InstanceIndex].heights[0] + + region.hexes[gl_InstanceIndex].heights[1] + + region.hexes[gl_InstanceIndex].heights[2] + + region.hexes[gl_InstanceIndex].heights[3] + + region.hexes[gl_InstanceIndex].heights[4] + + region.hexes[gl_InstanceIndex].heights[5])/6; + + color = (int2color(region.hexes[gl_InstanceIndex].colors[0]) + + int2color(region.hexes[gl_InstanceIndex].colors[1]) + + int2color(region.hexes[gl_InstanceIndex].colors[2]) + + int2color(region.hexes[gl_InstanceIndex].colors[3]) + + int2color(region.hexes[gl_InstanceIndex].colors[4]) + + int2color(region.hexes[gl_InstanceIndex].colors[5]))/6; + } else { + position.y = region.hexes[gl_InstanceIndex].heights[indices[gl_VertexIndex]-1]; + color = int2color(region.hexes[gl_InstanceIndex].colors[indices[gl_VertexIndex]-1]); + } gl_Position = pc.context.proj * pc.context.view * position; } diff --git a/client/shader/hex_common.glsl b/client/shader/hex_common.glsl index 383fe4d..7688fdc 100644 --- a/client/shader/hex_common.glsl +++ b/client/shader/hex_common.glsl @@ -3,27 +3,20 @@ struct Hex { uint colors[6]; }; -layout(std430, buffer_reference) readonly buffer HexList { - Hex h[]; -}; - -struct Region { - uint x; - uint y; - HexList hexes; -}; - -layout(std430, buffer_reference) readonly buffer RegionList { - Region r[]; +layout(std430, buffer_reference) readonly buffer Region { + uint q; + uint r; + Hex hexes[]; }; layout(std430, buffer_reference) readonly buffer HexContext { mat4 proj; mat4 view; - RegionList regions; + Region regions[]; }; layout(std430, push_constant) uniform PushConstant { HexContext context; + uint region; float time; } pc; diff --git a/client/src/draw.c b/client/src/draw.c index 55fff02..8eb99ce 100644 --- a/client/src/draw.c +++ b/client/src/draw.c @@ -25,7 +25,8 @@ void record_ui_draw(VkCommandBuffer command_buffer, UIContext* ui_context, doubl void record_hex_draw(VkCommandBuffer command_buffer, HexContext* hex, double time, uint32_t frame) { HexPushConstant push = { .context = hex->address, - .time = time, + .time = (float)time, + .region = 0, }; vkCmdPushConstants(command_buffer, hex->graphics.layout, VK_SHADER_STAGE_VERTEX_BIT, 0, sizeof(HexPushConstant), &push); vkCmdBindPipeline(command_buffer, VK_PIPELINE_BIND_POINT_GRAPHICS, hex->graphics.pipeline); diff --git a/client/src/hex.c b/client/src/hex.c index 2bf7ca3..e44a57c 100644 --- a/client/src/hex.c +++ b/client/src/hex.c @@ -206,25 +206,39 @@ VkResult create_hex_context( &context->context, &context->context_memory)); context->address = buffer_address(gpu->device, context->context); - VK_RESULT(add_transfer(&context->data, context->context, 0, sizeof(GPUHexContext), gpu->current_frame, gpu)); + VK_RESULT(add_transfer(&context->data, context->context, 0, sizeof(GPUHexContext) - sizeof(VkDeviceAddress)*MAX_LOADED_REGIONS, 0, gpu)); return VK_SUCCESS; } -VkResult create_hex_region(uint32_t x, uint32_t y, HexRegion* region, RenderContext* gpu) { +VkResult create_hex_region(uint32_t q, uint32_t r, HexRegion** region, HexContext* hex, RenderContext* gpu) { VkResult result; - region->x = x; - region->y = y; + uint32_t i = 0; + for(; i < MAX_LOADED_REGIONS; i++) { + if(hex->regions[i].address == 0) { + *region = &hex->regions[i]; + break; + } + } + + if(*region == NULL) { + return VK_ERROR_OUT_OF_HOST_MEMORY; + } + + (*region)->q = q; + (*region)->r = r; VK_RESULT(create_storage_buffer( gpu->allocator, 0, sizeof(GPUHexRegion), - ®ion->region, - ®ion->region_memory)); - VK_RESULT(add_transfer(®ion->x, region->region, offsetof(GPUHexRegion, x), sizeof(uint32_t)*2, 0, gpu)); - region->address = buffer_address(gpu->device, region->region); - + &(*region)->region, + &(*region)->region_memory)); + VK_RESULT(add_transfer(&(*region)->q, (*region)->region, offsetof(GPUHexRegion, q), sizeof(uint32_t)*2, 0, gpu)); + + (*region)->address = buffer_address(gpu->device, (*region)->region); + + VK_RESULT(add_transfer(&(*region)->address, hex->context, offsetof(GPUHexContext, regions) + sizeof(VkDeviceAddress)*i, sizeof(VkDeviceAddress), 0, gpu)); return VK_SUCCESS; } diff --git a/client/src/main.c b/client/src/main.c index 9316527..55ad519 100644 --- a/client/src/main.c +++ b/client/src/main.c @@ -122,8 +122,20 @@ VkResult main_thread(ClientContext* context) { 4*sizeof(uint32_t), &context->render)); - HexRegion region; - VK_RESULT(create_hex_region(0, 0, ®ion, &context->render)); + HexRegion* region; + VK_RESULT(create_hex_region(0, 0, ®ion, &context->hex, &context->render)); + GPUHex* temp_hexes = malloc(sizeof(GPUHex)*REGION_HEX_COUNT); + + uint32_t colors[] = {0xFF0000FF, 0x00FF00FF, 0x0000FFFF}; + for(uint32_t i = 0; i < REGION_HEX_COUNT; i++) { + for(uint32_t h = 0; h < 6; h++) { + temp_hexes[i].color[h] = colors[i%3]; + temp_hexes[i].height[h] = 0; + } + } + VK_RESULT(add_transfer(temp_hexes, region->region, offsetof(GPUHexRegion, hexes), sizeof(GPUHex)*REGION_HEX_COUNT, 0, &context->render)); + free(temp_hexes); + context->position[0] = 0; context->position[1] = 0; context->position[2] = 0; @@ -192,8 +204,7 @@ VkResult main_thread(ClientContext* context) { camera[1] = context->position[1] + context->distance*sin(context->rotation[1]); camera[2] = context->position[2] + context->distance*cos(context->rotation[1])*sin(context->rotation[0]); glm_lookat(camera, context->position, up, context->hex.data.view); - add_transfer(&context->hex.data, context->hex.context, 0, sizeof(GPUHexContext), 0, &context->render); - add_transfer(&context->hex.data, context->hex.context, 0, sizeof(GPUHexContext), 1, &context->render); + add_transfer(&context->hex.data, context->hex.context, 0, 2*sizeof(mat4), 0, &context->render); } if(context->clicked_container != 0) { diff --git a/client/src/ui.c b/client/src/ui.c index 63233e4..7623aee 100644 --- a/client/src/ui.c +++ b/client/src/ui.c @@ -248,8 +248,6 @@ VkResult create_container( context->containers[index].address[i] = buffer_address(gpu->device, context->containers[index].container[i]); } - fprintf(stderr, "Created container with storage buffers %p/%p\n", context->containers[index].container[0], context->containers[index].container[1]); - context->containers[index].data.offset[0] = container->offset[0]; context->containers[index].data.offset[1] = container->offset[1]; context->containers[index].data.size[0] = container->size[0]; @@ -331,9 +329,6 @@ VkResult create_layer( container->layers[index].address[i] = buffer_address(gpu->device, container->layers[index].layer[i]); } - fprintf(stderr, "Created layer with storage buffers %p/%p\n", container->layers[index].layer[0], container->layers[index].layer[1]); - fprintf(stderr, "String Buffers %p/%p\n", container->layers[index].strings[0], container->layers[index].strings[1]); - fprintf(stderr, "Code Buffers %p/%p\n", container->layers[index].codes[0], container->layers[index].codes[1]); for(uint32_t i = 0; i < MAX_FRAMES_IN_FLIGHT; i++) { add_transfer(