{gfx,examples}: add labels to gpu objects

Signed-off-by: Stephen Gutekanst <stephen@hexops.com>
This commit is contained in:
Stephen Gutekanst 2024-04-21 19:47:08 -07:00
parent 2b8bfcaa3e
commit bffc668005
10 changed files with 78 additions and 34 deletions

View file

@ -65,7 +65,8 @@ fn updatePipeline(
built: *gfx.SpritePipeline.BuiltPipeline,
) !void {
const device = core.state().device;
const encoder = device.createCommandEncoder(null);
const label = @tagName(name) ++ ".updatePipeline";
const encoder = device.createCommandEncoder(&.{ .label = label });
defer encoder.release();
var archetypes_iter = sprite.entities.query(.{ .all = &.{
@ -106,7 +107,7 @@ fn updatePipeline(
encoder.writeBuffer(built.transforms, 0, gfx.SpritePipeline.cp_transforms[0..i]);
encoder.writeBuffer(built.uv_transforms, 0, gfx.SpritePipeline.cp_uv_transforms[0..i]);
encoder.writeBuffer(built.sizes, 0, gfx.SpritePipeline.cp_sizes[0..i]);
var command = encoder.finish(null);
var command = encoder.finish(&.{ .label = label });
defer command.release();
core.state().queue.submit(&[_]*gpu.CommandBuffer{command});
}

View file

@ -186,35 +186,42 @@ fn buildPipeline(
const opt_layout = sprite_pipeline.get(pipeline_id, .layout);
const device = core.state().device;
const label = @tagName(name) ++ ".buildPipeline";
// Storage buffers
const transforms = device.createBuffer(&.{
.label = label ++ " transforms",
.usage = .{ .storage = true, .copy_dst = true },
.size = @sizeOf(math.Mat4x4) * sprite_buffer_cap,
.mapped_at_creation = .false,
});
const uv_transforms = device.createBuffer(&.{
.label = label ++ " uv_transforms",
.usage = .{ .storage = true, .copy_dst = true },
.size = @sizeOf(math.Mat3x3) * sprite_buffer_cap,
.mapped_at_creation = .false,
});
const sizes = device.createBuffer(&.{
.label = label ++ " sizes",
.usage = .{ .storage = true, .copy_dst = true },
.size = @sizeOf(math.Vec2) * sprite_buffer_cap,
.mapped_at_creation = .false,
});
const texture_sampler = opt_texture_sampler orelse device.createSampler(&.{
.label = label ++ " sampler",
.mag_filter = .nearest,
.min_filter = .nearest,
});
const uniforms = device.createBuffer(&.{
.label = label ++ " uniforms",
.usage = .{ .copy_dst = true, .uniform = true },
.size = @sizeOf(Uniforms),
.mapped_at_creation = .false,
});
const bind_group_layout = opt_bind_group_layout orelse device.createBindGroupLayout(
&gpu.BindGroupLayout.Descriptor.init(.{
.label = label,
.entries = &.{
gpu.BindGroupLayout.Entry.buffer(0, .{ .vertex = true }, .uniform, false, 0),
gpu.BindGroupLayout.Entry.buffer(1, .{ .vertex = true }, .read_only_storage, false, 0),
@ -230,10 +237,10 @@ fn buildPipeline(
);
defer bind_group_layout.release();
const texture_view = texture.createView(&gpu.TextureView.Descriptor{});
const texture2_view = if (opt_texture2) |tex| tex.createView(&gpu.TextureView.Descriptor{}) else texture_view;
const texture3_view = if (opt_texture3) |tex| tex.createView(&gpu.TextureView.Descriptor{}) else texture_view;
const texture4_view = if (opt_texture4) |tex| tex.createView(&gpu.TextureView.Descriptor{}) else texture_view;
const texture_view = texture.createView(&gpu.TextureView.Descriptor{ .label = label });
const texture2_view = if (opt_texture2) |tex| tex.createView(&gpu.TextureView.Descriptor{ .label = label }) else texture_view;
const texture3_view = if (opt_texture3) |tex| tex.createView(&gpu.TextureView.Descriptor{ .label = label }) else texture_view;
const texture4_view = if (opt_texture4) |tex| tex.createView(&gpu.TextureView.Descriptor{ .label = label }) else texture_view;
defer texture_view.release();
defer texture2_view.release();
defer texture3_view.release();
@ -241,6 +248,7 @@ fn buildPipeline(
const bind_group = opt_bind_group orelse device.createBindGroup(
&gpu.BindGroup.Descriptor.init(.{
.label = label,
.layout = bind_group_layout,
.entries = &.{
gpu.BindGroup.Entry.buffer(0, uniforms, 0, @sizeOf(Uniforms)),
@ -285,10 +293,12 @@ fn buildPipeline(
const bind_group_layouts = [_]*gpu.BindGroupLayout{bind_group_layout};
const pipeline_layout = opt_layout orelse device.createPipelineLayout(&gpu.PipelineLayout.Descriptor.init(.{
.label = label,
.bind_group_layouts = &bind_group_layouts,
}));
defer pipeline_layout.release();
const render_pipeline = device.createRenderPipeline(&gpu.RenderPipeline.Descriptor{
.label = label,
.fragment = &fragment,
.layout = pipeline_layout,
.vertex = gpu.VertexState{
@ -316,9 +326,8 @@ fn buildPipeline(
}
fn preRender(sprite_pipeline: *Mod) void {
const encoder = mach.core.device.createCommandEncoder(&gpu.CommandEncoder.Descriptor{
.label = "SpritePipeline.encoder",
});
const label = @tagName(name) ++ ".preRender";
const encoder = mach.core.device.createCommandEncoder(&.{ .label = label });
defer encoder.release();
var archetypes_iter = sprite_pipeline.entities.query(.{ .all = &.{
@ -353,7 +362,7 @@ fn preRender(sprite_pipeline: *Mod) void {
}
}
var command = encoder.finish(null);
var command = encoder.finish(&.{ .label = label });
defer command.release();
mach.core.queue.submit(&[_]*gpu.CommandBuffer{command});
}

View file

@ -81,7 +81,8 @@ fn updatePipeline(
built: *gfx.TextPipeline.BuiltPipeline,
) !void {
const device = core.state().device;
const encoder = device.createCommandEncoder(null);
const label = @tagName(name) ++ ".updatePipeline";
const encoder = device.createCommandEncoder(&.{ .label = label });
defer encoder.release();
const allocator = text_pipeline.state().allocator;
@ -272,7 +273,7 @@ fn updatePipeline(
}
if (num_texts > 0 or glyphs.items.len > 0) {
var command = encoder.finish(null);
var command = encoder.finish(&.{ .label = label });
defer command.release();
core.state().queue.submit(&[_]*gpu.CommandBuffer{command});
}

View file

@ -208,8 +208,10 @@ fn buildPipeline(
// Prepare texture for the font atlas.
// TODO(text): dynamic texture re-allocation when not large enough
// TODO(text): better default allocation size
const label = @tagName(name) ++ ".buildPipeline";
const img_size = gpu.Extent3D{ .width = 1024, .height = 1024 };
const texture = device.createTexture(&.{
.label = label,
.size = img_size,
.format = .rgba8_unorm,
.usage = .{
@ -226,32 +228,38 @@ fn buildPipeline(
// Storage buffers
const transforms = device.createBuffer(&.{
.label = label ++ " transforms",
.usage = .{ .storage = true, .copy_dst = true },
.size = @sizeOf(math.Mat4x4) * texts_buffer_cap,
.mapped_at_creation = .false,
});
const colors = device.createBuffer(&.{
.label = label ++ " colors",
.usage = .{ .storage = true, .copy_dst = true },
.size = @sizeOf(math.Vec4) * texts_buffer_cap,
.mapped_at_creation = .false,
});
const glyphs = device.createBuffer(&.{
.label = label ++ " glyphs",
.usage = .{ .storage = true, .copy_dst = true },
.size = @sizeOf(Glyph) * texts_buffer_cap,
.mapped_at_creation = .false,
});
const texture_sampler = opt_texture_sampler orelse device.createSampler(&.{
.label = label ++ " sampler",
.mag_filter = .nearest,
.min_filter = .nearest,
});
const uniforms = device.createBuffer(&.{
.label = label ++ " uniforms",
.usage = .{ .copy_dst = true, .uniform = true },
.size = @sizeOf(Uniforms),
.mapped_at_creation = .false,
});
const bind_group_layout = opt_bind_group_layout orelse device.createBindGroupLayout(
&gpu.BindGroupLayout.Descriptor.init(.{
.label = label,
.entries = &.{
gpu.BindGroupLayout.Entry.buffer(0, .{ .vertex = true }, .uniform, false, 0),
gpu.BindGroupLayout.Entry.buffer(1, .{ .vertex = true }, .read_only_storage, false, 0),
@ -264,11 +272,12 @@ fn buildPipeline(
);
defer bind_group_layout.release();
const texture_view = texture.createView(&gpu.TextureView.Descriptor{});
const texture_view = texture.createView(&gpu.TextureView.Descriptor{ .label = label });
defer texture_view.release();
const bind_group = opt_bind_group orelse device.createBindGroup(
&gpu.BindGroup.Descriptor.init(.{
.label = label,
.layout = bind_group_layout,
.entries = &.{
gpu.BindGroup.Entry.buffer(0, uniforms, 0, @sizeOf(Uniforms)),
@ -310,10 +319,12 @@ fn buildPipeline(
const bind_group_layouts = [_]*gpu.BindGroupLayout{bind_group_layout};
const pipeline_layout = opt_layout orelse device.createPipelineLayout(&gpu.PipelineLayout.Descriptor.init(.{
.label = label,
.bind_group_layouts = &bind_group_layouts,
}));
defer pipeline_layout.release();
const render_pipeline = device.createRenderPipeline(&gpu.RenderPipeline.Descriptor{
.label = label,
.fragment = &fragment,
.layout = pipeline_layout,
.vertex = gpu.VertexState{
@ -340,9 +351,8 @@ fn buildPipeline(
}
fn preRender(text_pipeline: *Mod) void {
const encoder = mach.core.device.createCommandEncoder(&gpu.CommandEncoder.Descriptor{
.label = "TextPipeline.encoder",
});
const label = @tagName(name) ++ ".preRender";
const encoder = mach.core.device.createCommandEncoder(&.{ .label = label });
defer encoder.release();
var archetypes_iter = text_pipeline.entities.query(.{ .all = &.{
@ -377,7 +387,7 @@ fn preRender(text_pipeline: *Mod) void {
}
}
var command = encoder.finish(null);
var command = encoder.finish(&.{ .label = label });
defer command.release();
mach.core.queue.submit(&[_]*gpu.CommandBuffer{command});
}