GPU-based font rendering (keming edition)
This commit is contained in:
@@ -8,6 +8,7 @@ const vm = @import("vecmath");
|
||||
|
||||
const CommandBuffer = @import("CommandBuffer.zig");
|
||||
const Engine = @import("Engine.zig");
|
||||
const Fonts = @import("Fonts.zig");
|
||||
const GenericBuffer = @import("GenericBuffer.zig").GenericBuffer;
|
||||
const Swapchain = @import("Swapchain.zig");
|
||||
const Texture = @import("Texture.zig");
|
||||
@@ -25,11 +26,10 @@ pub const Draw = struct {
|
||||
|
||||
pub const Text = extern struct {
|
||||
color: vm.Vector4,
|
||||
pos_min: vm.Vector2,
|
||||
pos_max: vm.Vector2,
|
||||
uv_min: vm.Vector2,
|
||||
uv_max: vm.Vector2,
|
||||
atlas_id: Textures.Id,
|
||||
bottom_left_sspx: vm.Vector2,
|
||||
font_size_px: f32,
|
||||
glyph_id: u16,
|
||||
font_id: Fonts.Id,
|
||||
};
|
||||
|
||||
pub const Image = extern struct {
|
||||
@@ -65,8 +65,13 @@ box_descriptor_set_layout: vk.DescriptorSetLayout,
|
||||
box_pipeline_layout: vk.PipelineLayout,
|
||||
box_pipeline: vk.Pipeline,
|
||||
|
||||
text_descriptor_set_layout: vk.DescriptorSetLayout,
|
||||
text_pipeline_layout: vk.PipelineLayout,
|
||||
text_pipeline: vk.Pipeline,
|
||||
|
||||
descriptor_pool: vk.DescriptorPool,
|
||||
box_descriptor_set: vk.DescriptorSet,
|
||||
text_descriptor_set: vk.DescriptorSet,
|
||||
|
||||
pub const max_box_draws = 1024;
|
||||
pub const max_image_draws = 1024;
|
||||
@@ -273,16 +278,169 @@ pub fn init() !*Gui {
|
||||
errdefer engine.destroyPipeline(box_pipeline);
|
||||
engine.setObjectName(box_pipeline, "P GUI Box", .{});
|
||||
|
||||
const text_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 = .storage_buffer,
|
||||
.descriptor_count = Fonts.max_fonts,
|
||||
.stage_flags = .{ .vertex_bit = true, .fragment_bit = true },
|
||||
},
|
||||
.{
|
||||
.binding = 3,
|
||||
.descriptor_type = .storage_buffer,
|
||||
.descriptor_count = Fonts.max_fonts,
|
||||
.stage_flags = .{ .vertex_bit = true, .fragment_bit = true },
|
||||
},
|
||||
.{
|
||||
.binding = 4,
|
||||
.descriptor_type = .storage_buffer,
|
||||
.descriptor_count = Fonts.max_fonts,
|
||||
.stage_flags = .{ .vertex_bit = true, .fragment_bit = true },
|
||||
},
|
||||
},
|
||||
});
|
||||
errdefer engine.destroyDescriptorSetLayout(text_descriptor_set_layout);
|
||||
engine.setObjectName(text_descriptor_set_layout, "DSL GUI Text", .{});
|
||||
|
||||
const text_pipeline_layout = try engine.createPipelineLayout(.{
|
||||
.set_layouts = &.{
|
||||
text_descriptor_set_layout,
|
||||
},
|
||||
});
|
||||
errdefer engine.destroyPipelineLayout(text_pipeline_layout);
|
||||
engine.setObjectName(text_pipeline_layout, "PL GUI Text", .{});
|
||||
|
||||
const text_shader = try engine.createShaderModule(.{ .code = &shaders.gui_text_spv });
|
||||
defer engine.destroyShaderModule(text_shader);
|
||||
engine.setObjectName(text_shader, "SM gui_text", .{});
|
||||
|
||||
const text_pipeline = try engine.createGraphicsPipeline(.{
|
||||
.stages = &.{
|
||||
.{
|
||||
.stage = .{ .vertex_bit = true },
|
||||
.module = text_shader,
|
||||
.name = "vertexMain",
|
||||
},
|
||||
.{
|
||||
.stage = .{ .fragment_bit = true },
|
||||
.module = text_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 = text_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(text_pipeline);
|
||||
engine.setObjectName(text_pipeline, "P GUI Box", .{});
|
||||
|
||||
const descriptor_pool = try engine.createDescriptorPool(.{
|
||||
.max_sets = 1,
|
||||
.max_sets = 2,
|
||||
.pool_sizes = &.{
|
||||
.{
|
||||
.type = .uniform_buffer,
|
||||
.descriptor_count = 1,
|
||||
.descriptor_count = 1 + // box
|
||||
1, // text
|
||||
},
|
||||
.{
|
||||
.type = .storage_buffer,
|
||||
.descriptor_count = 1,
|
||||
.descriptor_count = 1 + // box
|
||||
(1 + 3 * Fonts.max_fonts), // text
|
||||
},
|
||||
},
|
||||
});
|
||||
@@ -295,6 +453,12 @@ pub fn init() !*Gui {
|
||||
});
|
||||
engine.setObjectName(box_descriptor_set, "DS GUI Box", .{});
|
||||
|
||||
const text_descriptor_set = try engine.allocateDescriptorSet(.{
|
||||
.descriptor_pool = descriptor_pool,
|
||||
.set_layout = text_descriptor_set_layout,
|
||||
});
|
||||
engine.setObjectName(text_descriptor_set, "DS GUI Text", .{});
|
||||
|
||||
try engine.updateDescriptorSets(.{
|
||||
.writes = &.{
|
||||
.{
|
||||
@@ -312,6 +476,21 @@ pub fn init() !*Gui {
|
||||
},
|
||||
},
|
||||
},
|
||||
.{
|
||||
.dst_set = text_descriptor_set,
|
||||
.dst_binding = 1,
|
||||
.dst_array_element = 0,
|
||||
.descriptor_type = .storage_buffer,
|
||||
.descriptor_infos = .{
|
||||
.buffer = &.{
|
||||
.{
|
||||
.buffer = text_gpu_buffer.buffer,
|
||||
.offset = 0,
|
||||
.range = vk.WHOLE_SIZE,
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
});
|
||||
|
||||
@@ -333,8 +512,13 @@ pub fn init() !*Gui {
|
||||
.box_pipeline_layout = box_pipeline_layout,
|
||||
.box_pipeline = box_pipeline,
|
||||
|
||||
.text_descriptor_set_layout = text_descriptor_set_layout,
|
||||
.text_pipeline_layout = text_pipeline_layout,
|
||||
.text_pipeline = text_pipeline,
|
||||
|
||||
.descriptor_pool = descriptor_pool,
|
||||
.box_descriptor_set = box_descriptor_set,
|
||||
.text_descriptor_set = text_descriptor_set,
|
||||
};
|
||||
|
||||
return gui;
|
||||
@@ -346,9 +530,12 @@ pub fn deinit(self: *Gui) void {
|
||||
std.log.scoped(.deinit).debug("Deinitializing {*}", .{self});
|
||||
|
||||
engine.destroyDescriptorPool(self.descriptor_pool);
|
||||
engine.destroyPipeline(self.text_pipeline);
|
||||
engine.destroyPipeline(self.box_pipeline);
|
||||
self.index_buffer.deinit();
|
||||
engine.destroyPipelineLayout(self.text_pipeline_layout);
|
||||
engine.destroyPipelineLayout(self.box_pipeline_layout);
|
||||
engine.destroyDescriptorSetLayout(self.text_descriptor_set_layout);
|
||||
engine.destroyDescriptorSetLayout(self.box_descriptor_set_layout);
|
||||
engine.destroySampler(self.sampler);
|
||||
|
||||
@@ -379,6 +566,67 @@ pub fn pushText(self: *Gui, text: Draw.Text) void {
|
||||
self.extendOrAddBatch(.Text, instance);
|
||||
}
|
||||
|
||||
pub const PrintLineArgs = struct {
|
||||
position_sspx: vm.Vector2,
|
||||
font_id: Fonts.Id,
|
||||
cap_height_px: f32,
|
||||
color: vm.Vector4,
|
||||
};
|
||||
|
||||
pub fn printLine(self: *Gui, comptime fmt: []const u8, fmt_args: anytype, print_args: PrintLineArgs) void {
|
||||
const allocator_frame = ctx.allocator_frame;
|
||||
const fonts = ctx.fonts;
|
||||
|
||||
const text = std.fmt.allocPrint(allocator_frame, fmt, fmt_args) catch |err| {
|
||||
std.log.err("Failed formatting of string \"{s}\": {}", .{ fmt, err });
|
||||
return;
|
||||
};
|
||||
|
||||
const font = fonts.array.items[@intFromEnum(print_args.font_id)];
|
||||
const font_size_px = print_args.cap_height_px / font.cap_height_em;
|
||||
|
||||
const utf8_view = std.unicode.Utf8View.init(text) catch {
|
||||
std.log.err("Invalid UTF-8 sequence in GUI print line", .{});
|
||||
return;
|
||||
};
|
||||
|
||||
var cursor_sspx = print_args.position_sspx;
|
||||
var maybe_prev_glyph: ?u16 = null;
|
||||
var it = utf8_view.iterator();
|
||||
while (it.nextCodepoint()) |codepoint| {
|
||||
const glyph_id = font.codepoint_glyph_id.get(codepoint) orelse {
|
||||
std.log.warn("Missing codepoint 0x{X} '{u}' in font #{d}", .{ codepoint, codepoint, print_args.font_id });
|
||||
maybe_prev_glyph = null;
|
||||
continue;
|
||||
};
|
||||
defer maybe_prev_glyph = glyph_id;
|
||||
|
||||
const glyph_data = &font.cpu_glyph_data[glyph_id];
|
||||
|
||||
var cursor_offset_em = glyph_data.bottom_left_em;
|
||||
cursor_offset_em.y = -cursor_offset_em.y;
|
||||
cursor_offset_em.x += glyph_data.left_side_bearing_em;
|
||||
|
||||
if (maybe_prev_glyph) |prev_glyph| {
|
||||
const kerning_em = font.kerning_em.get(.{ prev_glyph, glyph_id }) orelse 0;
|
||||
cursor_offset_em.x += kerning_em;
|
||||
}
|
||||
|
||||
if (glyph_data.gpu_glyph_id) |gpu_glyph_id| {
|
||||
self.pushText(.{
|
||||
.color = print_args.color,
|
||||
.bottom_left_sspx = cursor_sspx.add(cursor_offset_em.mulScalar(font_size_px)),
|
||||
.font_size_px = font_size_px,
|
||||
.glyph_id = gpu_glyph_id,
|
||||
.font_id = print_args.font_id,
|
||||
});
|
||||
}
|
||||
|
||||
const advance_px = font_size_px * glyph_data.advance_em;
|
||||
cursor_sspx.x += advance_px;
|
||||
}
|
||||
}
|
||||
|
||||
pub fn pushImage(self: *Gui, image: Draw.Image) void {
|
||||
const instance: u32 = @intCast(self.image_cpu_buffer.items.len);
|
||||
self.image_cpu_buffer.appendBounded(image) catch return;
|
||||
@@ -413,8 +661,8 @@ pub fn draw(self: *const Gui, command_buffer: CommandBuffer) !void {
|
||||
command_buffer.bindDescriptorSet(.graphics, self.box_pipeline_layout, 0, self.box_descriptor_set, null);
|
||||
},
|
||||
.Text => {
|
||||
std.log.warn("GUI Text not implemented; skipping batch", .{});
|
||||
continue;
|
||||
command_buffer.bindPipeline(.graphics, self.text_pipeline);
|
||||
command_buffer.bindDescriptorSet(.graphics, self.text_pipeline_layout, 0, self.text_descriptor_set, null);
|
||||
},
|
||||
.Image => {
|
||||
std.log.warn("GUI Image not implemented; skipping batch", .{});
|
||||
|
||||
Reference in New Issue
Block a user