Rendering hexagon regions

main
noah metz 2024-11-01 17:34:41 -06:00
parent 5b1275ea83
commit 74cccfe065
8 changed files with 112 additions and 63 deletions

@ -4,17 +4,34 @@
#include "gpu.h" #include "gpu.h"
#define REGION_SIZE 6 #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 { typedef struct GPUHexStruct {
VkDeviceAddress context; float height[6];
double time; uint32_t color[6];
} HexPushConstant; } 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 { typedef struct GPUHexContextStruct {
mat4 proj; mat4 proj;
mat4 view; mat4 view;
VkDeviceAddress regions; VkDeviceAddress regions[MAX_LOADED_REGIONS];
} GPUHexContext; } GPUHexContext;
typedef struct HexContextStruct { typedef struct HexContextStruct {
@ -25,37 +42,25 @@ typedef struct HexContextStruct {
GraphicsPipeline graphics; GraphicsPipeline graphics;
ComputePipeline compute; ComputePipeline compute;
HexRegion regions[MAX_LOADED_REGIONS];
GPUHexContext data; GPUHexContext data;
} HexContext; } HexContext;
typedef struct GPUHexStruct { typedef struct HexPushConstantStruct {
float height[6]; VkDeviceAddress context;
uint32_t color[6]; uint32_t region;
} GPUHex; double time;
} HexPushConstant;
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;
VkResult create_hex_context( VkResult create_hex_context(
RenderContext* gpu, RenderContext* gpu,
HexContext* context); HexContext* context);
VkResult create_hex_region( VkResult create_hex_region(
uint32_t x, uint32_t y, uint32_t q, uint32_t s,
HexRegion* region, HexRegion** region,
HexContext* hex,
RenderContext* gpu); RenderContext* gpu);
#endif #endif

@ -1,8 +1,9 @@
#version 450 #version 450
#extension GL_EXT_buffer_reference : require #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() { void main() {
outColor = vec4(1, 1, 1, 1); out_color = in_color;
} }

@ -45,7 +45,19 @@ const vec4 direction[] = {
vec4(-x, 0, z, 0), 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() { void main() {
Region region = pc.context.regions[pc.region];
uint radius = 0; uint radius = 0;
uint ring = 0; uint ring = 0;
uint side = 0; uint side = 0;
@ -57,8 +69,25 @@ void main() {
hex = ring - (radius*side); hex = ring - (radius*side);
} }
// Calculate position based on radius/ring/side/hex
vec4 position = vertices[indices[gl_VertexIndex]] + (starts[side]*radius) + (direction[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; gl_Position = pc.context.proj * pc.context.view * position;
} }

@ -3,27 +3,20 @@ struct Hex {
uint colors[6]; uint colors[6];
}; };
layout(std430, buffer_reference) readonly buffer HexList { layout(std430, buffer_reference) readonly buffer Region {
Hex h[]; uint q;
}; uint r;
Hex hexes[];
struct Region {
uint x;
uint y;
HexList hexes;
};
layout(std430, buffer_reference) readonly buffer RegionList {
Region r[];
}; };
layout(std430, buffer_reference) readonly buffer HexContext { layout(std430, buffer_reference) readonly buffer HexContext {
mat4 proj; mat4 proj;
mat4 view; mat4 view;
RegionList regions; Region regions[];
}; };
layout(std430, push_constant) uniform PushConstant { layout(std430, push_constant) uniform PushConstant {
HexContext context; HexContext context;
uint region;
float time; float time;
} pc; } pc;

@ -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) { void record_hex_draw(VkCommandBuffer command_buffer, HexContext* hex, double time, uint32_t frame) {
HexPushConstant push = { HexPushConstant push = {
.context = hex->address, .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); 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); vkCmdBindPipeline(command_buffer, VK_PIPELINE_BIND_POINT_GRAPHICS, hex->graphics.pipeline);

@ -206,25 +206,39 @@ VkResult create_hex_context(
&context->context, &context->context,
&context->context_memory)); &context->context_memory));
context->address = buffer_address(gpu->device, context->context); 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; 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; VkResult result;
region->x = x; uint32_t i = 0;
region->y = y; 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( VK_RESULT(create_storage_buffer(
gpu->allocator, gpu->allocator,
0, 0,
sizeof(GPUHexRegion), sizeof(GPUHexRegion),
&region->region, &(*region)->region,
&region->region_memory)); &(*region)->region_memory));
VK_RESULT(add_transfer(&region->x, region->region, offsetof(GPUHexRegion, x), sizeof(uint32_t)*2, 0, gpu)); 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);
(*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; return VK_SUCCESS;
} }

@ -122,8 +122,20 @@ VkResult main_thread(ClientContext* context) {
4*sizeof(uint32_t), 4*sizeof(uint32_t),
&context->render)); &context->render));
HexRegion region; HexRegion* region;
VK_RESULT(create_hex_region(0, 0, &region, &context->render)); VK_RESULT(create_hex_region(0, 0, &region, &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[0] = 0;
context->position[1] = 0; context->position[1] = 0;
context->position[2] = 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[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]); 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); 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, 2*sizeof(mat4), 0, &context->render);
add_transfer(&context->hex.data, context->hex.context, 0, sizeof(GPUHexContext), 1, &context->render);
} }
if(context->clicked_container != 0) { if(context->clicked_container != 0) {

@ -248,8 +248,6 @@ VkResult create_container(
context->containers[index].address[i] = buffer_address(gpu->device, context->containers[index].container[i]); 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[0] = container->offset[0];
context->containers[index].data.offset[1] = container->offset[1]; context->containers[index].data.offset[1] = container->offset[1];
context->containers[index].data.size[0] = container->size[0]; 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]); 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++) { for(uint32_t i = 0; i < MAX_FRAMES_IN_FLIGHT; i++) {
add_transfer( add_transfer(