From 45c0f3180efa4678bc415e3f09eddf527abb2419 Mon Sep 17 00:00:00 2001 From: adrien Date: Wed, 20 May 2026 09:55:34 +0200 Subject: [PATCH] Created a GpuTextureDef --- src/GpuRender.zig | 6 +++--- src/GpuTexture.zig | 22 +++++++++++++--------- src/circle.zig | 11 +++++------ 3 files changed, 21 insertions(+), 18 deletions(-) diff --git a/src/GpuRender.zig b/src/GpuRender.zig index e84fc5f..53b3300 100644 --- a/src/GpuRender.zig +++ b/src/GpuRender.zig @@ -10,7 +10,7 @@ pub const Binding = struct { element_size: u32 = 0, }; -pub const RenderDef = struct { +pub const GpuRenderDef = struct { bindings: []const Binding = &.{}, /// The surface texture format we are rendering to (e.g., BGRA8Unorm) texture_format: GpuTextureFormat, @@ -32,9 +32,9 @@ const GpuPrimitiveTopology = enum(c_uint) { }; pip: c.WGPURenderPipeline, -def: RenderDef, +def: GpuRenderDef, -pub fn init(device: GpuDevice, wgsl: []const u8, def: RenderDef) !@This() { +pub fn init(device: GpuDevice, wgsl: []const u8, def: GpuRenderDef) !@This() { var wgsl_src = c.WGPUShaderSourceWGSL{ .chain = .{ .sType = c.WGPUSType_ShaderSourceWGSL }, .code = sv(wgsl), diff --git a/src/GpuTexture.zig b/src/GpuTexture.zig index 35eb409..fe7f7ed 100644 --- a/src/GpuTexture.zig +++ b/src/GpuTexture.zig @@ -3,7 +3,7 @@ const c = @import("utils.zig").c; const GpuAllocator = @import("GpuAllocator.zig"); const GpuTextureFormat = @import("lib.zig").GpuTextureFormat; -const TextureUsage = enum(u64) { +const GpuTextureUsage = enum(u64) { None = 0x0000000000000000, CopySrc = 0x0000000000000001, CopyDst = 0x0000000000000002, @@ -13,29 +13,33 @@ const TextureUsage = enum(u64) { TransientAttachment = 0x0000000000000020, }; +pub const GpuTextureDef = struct { + size: c.WGPUExtent3D, + usage: std.EnumSet(GpuTextureUsage), + format: GpuTextureFormat, +}; + raw: c.WGPUTexture, -size: c.WGPUExtent3D, -usage: c.WGPUTextureUsage, -format: GpuTextureFormat, gloc: GpuAllocator, +def: GpuTextureDef, /// Allocates the underlying WebGPU handle and registers it to the parent GpuAllocator -pub fn init(gloc: GpuAllocator, format: GpuTextureFormat, size: c.WGPUExtent3D, usage: std.EnumSet(TextureUsage)) !@This() { +pub fn init(gloc: GpuAllocator, def: GpuTextureDef) !@This() { var use: u64 = 0; - var iter = usage.iterator(); + var iter = def.usage.iterator(); while (iter.next()) |flag| use |= @intFromEnum(flag); const desc = c.WGPUTextureDescriptor{ .usage = use, .dimension = c.WGPUTextureDimension_2D, - .size = size, - .format = @intFromEnum(format), + .size = def.size, + .format = @intFromEnum(def.format), .mipLevelCount = 1, .sampleCount = 1, }; const raw = try gloc.allocTexture(desc); - return .{ .gloc = gloc, .raw = raw, .size = size, .format = format, .usage = use }; + return .{ .gloc = gloc, .raw = raw, .def = def }; } /// Unregisters from the parent GpuAllocator and cleanly destroys GPU resources diff --git a/src/circle.zig b/src/circle.zig index 5805b9c..ee8e0ab 100644 --- a/src/circle.zig +++ b/src/circle.zig @@ -35,12 +35,11 @@ pub fn main(init: std.process.Init) !void { defer circle_rp.deinit(); // 3. Create the offscreen VRAM texture to render into - const texture = try GpuTexture.init( - gloc, - .RGBA8Unorm, - .{ .width = width, .height = height, .depthOrArrayLayers = 1 }, - .initMany(&.{ .RenderAttachment, .CopySrc }), - ); + const texture = try GpuTexture.init(gloc, .{ + .format = .RGBA8Unorm, + .size = .{ .width = width, .height = height, .depthOrArrayLayers = 1 }, + .usage = .initMany(&.{ .RenderAttachment, .CopySrc }), + }); defer texture.deinit(); const target_view = c.wgpuTextureCreateView(texture.raw, null) orelse return error.View;