GUI image implementation, player hotbar

This commit is contained in:
2026-09-20 22:31:46 +02:00
parent ff72311979
commit c3fa7115c8
6 changed files with 423 additions and 136 deletions

View File

@@ -33,11 +33,11 @@ pub const Draw = struct {
};
pub const Image = extern struct {
tint: vm.Vector4,
tint: vm.Vector4 = .initScalar(1),
position_sspx: vm.Vector2,
size_px: vm.Vector2,
uv_min: vm.Vector2,
uv_max: vm.Vector2,
uv_min: vm.Vector2 = .initScalar(0),
uv_max: vm.Vector2 = .initScalar(1),
texture_id: Textures.Id,
};
};
@@ -69,9 +69,14 @@ text_descriptor_set_layout: vk.DescriptorSetLayout,
text_pipeline_layout: vk.PipelineLayout,
text_pipeline: vk.Pipeline,
image_descriptor_set_layout: vk.DescriptorSetLayout,
image_pipeline_layout: vk.PipelineLayout,
image_pipeline: vk.Pipeline,
descriptor_pool: vk.DescriptorPool,
box_descriptor_set: vk.DescriptorSet,
text_descriptor_set: vk.DescriptorSet,
image_descriptor_set: vk.DescriptorSet,
pub const max_box_draws = 1024;
pub const max_image_draws = 1024;
@@ -113,7 +118,18 @@ pub fn init() !*Gui {
const box_cpu_buffer: std.ArrayList(Draw.Box) = try .initCapacity(allocator_persistent, max_box_draws);
const text_cpu_buffer: std.ArrayList(Draw.Text) = try .initCapacity(allocator_persistent, max_text_draws);
const image_cpu_buffer: std.ArrayList(Draw.Image) = try .initCapacity(allocator_persistent, max_image_draws);
const batches: std.ArrayList(Batch) = try .initCapacity(allocator_persistent, max_batches);
var index_buffer = try shaders.IndexBuffer.init(.{
.usage = .index,
.target_queue = .graphics,
.array_capacity = 6,
.name = "QuadIB",
});
errdefer index_buffer.deinit();
try index_buffer.write(.{
.elements = &.{ 0, 1, 2, 2, 1, 3 },
});
const sampler = try engine.createSampler(.{
.mag_filter = .linear,
@@ -165,17 +181,6 @@ pub fn init() !*Gui {
defer engine.destroyShaderModule(box_shader);
engine.setObjectName(box_shader, "SM gui_box", .{});
var index_buffer = try shaders.IndexBuffer.init(.{
.usage = .index,
.target_queue = .graphics,
.array_capacity = 6,
.name = "QuadIB",
});
errdefer index_buffer.deinit();
try index_buffer.write(.{
.elements = &.{ 0, 1, 2, 2, 1, 3 },
});
const box_pipeline = try engine.createGraphicsPipeline(.{
.stages = &.{
.{
@@ -429,18 +434,174 @@ pub fn init() !*Gui {
errdefer engine.destroyPipeline(text_pipeline);
engine.setObjectName(text_pipeline, "P GUI Box", .{});
const image_descriptor_set_layout = try engine.createDescriptorSetLayout(.{
.bindings = &.{
.{
.binding = 0,
.descriptor_type = .uniform_buffer,
.descriptor_count = 1,
.stage_flags = .{ .vertex_bit = true, .fragment_bit = true },
},
.{
.binding = 1,
.descriptor_type = .storage_buffer,
.descriptor_count = 1,
.stage_flags = .{ .vertex_bit = true, .fragment_bit = true },
},
.{
.binding = 2,
.descriptor_type = .sampler,
.descriptor_count = 1,
.stage_flags = .{ .vertex_bit = true, .fragment_bit = true },
.immutable_samplers = &.{sampler},
},
.{
.binding = 3,
.descriptor_type = .sampled_image,
.descriptor_count = Textures.max_textures,
.stage_flags = .{ .vertex_bit = true, .fragment_bit = true },
},
},
});
errdefer engine.destroyDescriptorSetLayout(image_descriptor_set_layout);
engine.setObjectName(box_descriptor_set_layout, "DSL GUI Image", .{});
const image_pipeline_layout = try engine.createPipelineLayout(.{
.set_layouts = &.{
image_descriptor_set_layout,
},
});
errdefer engine.destroyPipelineLayout(image_pipeline_layout);
engine.setObjectName(box_pipeline_layout, "PL GUI Image", .{});
const image_shader = try engine.createShaderModule(.{ .code = &shaders.gui_image_spv });
defer engine.destroyShaderModule(image_shader);
engine.setObjectName(text_shader, "SM gui_image", .{});
const image_pipeline = try engine.createGraphicsPipeline(.{
.stages = &.{
.{
.stage = .{ .vertex_bit = true },
.module = image_shader,
.name = "vertexMain",
},
.{
.stage = .{ .fragment_bit = true },
.module = image_shader,
.name = "fragmentMain",
},
},
.vertex_input_state = .{},
.input_assembly_state = .{
.topology = .triangle_list,
.primitive_restart_enable = .false,
},
.viewport_state = .{
.viewports = &.{undefined}, // dynamic
.scissors = &.{undefined}, // dynamic
},
.rasterization_state = .{
.depth_clamp_enable = .false,
.rasterizer_discard_enable = .false,
.polygon_mode = .fill,
.cull_mode = .{},
.front_face = .counter_clockwise,
.depth_bias_enable = .false,
.depth_bias_constant_factor = 0,
.depth_bias_clamp = 0,
.depth_bias_slope_factor = 0,
.line_width = 1,
},
.multisample_state = .{
.rasterization_samples = .{ .@"1_bit" = true },
.sample_shading_enable = .false,
.min_sample_shading = 1,
.alpha_to_coverage_enable = .false,
.alpha_to_one_enable = .false,
},
.depth_stencil_state = .{
.depth_test_enable = .false,
.depth_write_enable = .false,
.depth_compare_op = .always,
.depth_bounds_test_enable = .false,
.stencil_test_enable = .false,
.front = .{
.fail_op = .keep,
.pass_op = .keep,
.depth_fail_op = .keep,
.compare_op = .never,
.compare_mask = 0,
.write_mask = 0,
.reference = 0,
},
.back = .{
.fail_op = .keep,
.pass_op = .keep,
.depth_fail_op = .keep,
.compare_op = .never,
.compare_mask = 0,
.write_mask = 0,
.reference = 0,
},
.min_depth_bounds = 0,
.max_depth_bounds = 1,
},
.color_blend_state = .{
.logic_op_enable = .false,
.logic_op = .copy,
.attachments = &.{
.{
.blend_enable = .true,
.src_color_blend_factor = .one,
.dst_color_blend_factor = .one_minus_src_alpha,
.color_blend_op = .add,
.src_alpha_blend_factor = .one,
.dst_alpha_blend_factor = .one_minus_src_alpha,
.alpha_blend_op = .add,
.color_write_mask = .{
.r_bit = true,
.g_bit = true,
.b_bit = true,
.a_bit = true,
},
},
},
.blend_constants = .{ 0, 0, 0, 0 },
},
.dynamic_state = .{
.dynamic_states = &.{ .viewport, .scissor },
},
.layout = image_pipeline_layout,
.view_mask = 0,
.color_attachment_formats = &.{swapchain.params.surface_format.format},
.depth_attachment_format = Texture.Usage.depth.vkFormat(),
.stencil_attachment_format = .undefined,
});
errdefer engine.destroyPipeline(image_pipeline);
engine.setObjectName(image_pipeline, "P GUI Image", .{});
const descriptor_pool = try engine.createDescriptorPool(.{
.max_sets = 2,
.max_sets = 3,
.pool_sizes = &.{
.{
.type = .uniform_buffer,
.descriptor_count = 1 + // box
1, // text
1 + // text
1, // image
},
.{
.type = .storage_buffer,
.descriptor_count = 1 + // box
(1 + 3 * Fonts.max_fonts), // text
(1 + 3 * Fonts.max_fonts) + // text
1, // image
},
.{
.type = .sampled_image,
.descriptor_count = Textures.max_textures, // image
},
.{
.type = .sampler,
.descriptor_count = 1, // image
},
},
});
@@ -459,6 +620,12 @@ pub fn init() !*Gui {
});
engine.setObjectName(text_descriptor_set, "DS GUI Text", .{});
const image_descriptor_set = try engine.allocateDescriptorSet(.{
.descriptor_pool = descriptor_pool,
.set_layout = image_descriptor_set_layout,
});
engine.setObjectName(image_descriptor_set, "DS GUI Image", .{});
try engine.updateDescriptorSets(.{
.writes = &.{
.{
@@ -491,6 +658,21 @@ pub fn init() !*Gui {
},
},
},
.{
.dst_set = image_descriptor_set,
.dst_binding = 1,
.dst_array_element = 0,
.descriptor_type = .storage_buffer,
.descriptor_infos = .{
.buffer = &.{
.{
.buffer = image_gpu_buffer.buffer,
.offset = 0,
.range = vk.WHOLE_SIZE,
},
},
},
},
},
});
@@ -516,9 +698,14 @@ pub fn init() !*Gui {
.text_pipeline_layout = text_pipeline_layout,
.text_pipeline = text_pipeline,
.image_descriptor_set_layout = image_descriptor_set_layout,
.image_pipeline_layout = image_pipeline_layout,
.image_pipeline = image_pipeline,
.descriptor_pool = descriptor_pool,
.box_descriptor_set = box_descriptor_set,
.text_descriptor_set = text_descriptor_set,
.image_descriptor_set = image_descriptor_set,
};
return gui;
@@ -530,11 +717,14 @@ pub fn deinit(self: *Gui) void {
std.log.scoped(.deinit).debug("Deinitializing {*}", .{self});
engine.destroyDescriptorPool(self.descriptor_pool);
engine.destroyPipeline(self.image_pipeline);
engine.destroyPipeline(self.text_pipeline);
engine.destroyPipeline(self.box_pipeline);
self.index_buffer.deinit();
engine.destroyPipelineLayout(self.image_pipeline_layout);
engine.destroyPipelineLayout(self.text_pipeline_layout);
engine.destroyPipelineLayout(self.box_pipeline_layout);
engine.destroyDescriptorSetLayout(self.image_descriptor_set_layout);
engine.destroyDescriptorSetLayout(self.text_descriptor_set_layout);
engine.destroyDescriptorSetLayout(self.box_descriptor_set_layout);
engine.destroySampler(self.sampler);
@@ -667,8 +857,8 @@ pub fn draw(self: *const Gui, command_buffer: CommandBuffer) !void {
command_buffer.bindDescriptorSet(.graphics, self.text_pipeline_layout, 0, self.text_descriptor_set, null);
},
.Image => {
std.log.warn("GUI Image not implemented; skipping batch", .{});
continue;
command_buffer.bindPipeline(.graphics, self.image_pipeline);
command_buffer.bindDescriptorSet(.graphics, self.image_pipeline_layout, 0, self.image_descriptor_set, null);
},
}