Created a GpuTextureDef

This commit is contained in:
adrien 2026-05-20 09:55:34 +02:00
parent fc57bee3af
commit 45c0f3180e
3 changed files with 21 additions and 18 deletions

View File

@ -10,7 +10,7 @@ pub const Binding = struct {
element_size: u32 = 0, element_size: u32 = 0,
}; };
pub const RenderDef = struct { pub const GpuRenderDef = struct {
bindings: []const Binding = &.{}, bindings: []const Binding = &.{},
/// The surface texture format we are rendering to (e.g., BGRA8Unorm) /// The surface texture format we are rendering to (e.g., BGRA8Unorm)
texture_format: GpuTextureFormat, texture_format: GpuTextureFormat,
@ -32,9 +32,9 @@ const GpuPrimitiveTopology = enum(c_uint) {
}; };
pip: c.WGPURenderPipeline, 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{ var wgsl_src = c.WGPUShaderSourceWGSL{
.chain = .{ .sType = c.WGPUSType_ShaderSourceWGSL }, .chain = .{ .sType = c.WGPUSType_ShaderSourceWGSL },
.code = sv(wgsl), .code = sv(wgsl),

View File

@ -3,7 +3,7 @@ const c = @import("utils.zig").c;
const GpuAllocator = @import("GpuAllocator.zig"); const GpuAllocator = @import("GpuAllocator.zig");
const GpuTextureFormat = @import("lib.zig").GpuTextureFormat; const GpuTextureFormat = @import("lib.zig").GpuTextureFormat;
const TextureUsage = enum(u64) { const GpuTextureUsage = enum(u64) {
None = 0x0000000000000000, None = 0x0000000000000000,
CopySrc = 0x0000000000000001, CopySrc = 0x0000000000000001,
CopyDst = 0x0000000000000002, CopyDst = 0x0000000000000002,
@ -13,29 +13,33 @@ const TextureUsage = enum(u64) {
TransientAttachment = 0x0000000000000020, TransientAttachment = 0x0000000000000020,
}; };
pub const GpuTextureDef = struct {
size: c.WGPUExtent3D,
usage: std.EnumSet(GpuTextureUsage),
format: GpuTextureFormat,
};
raw: c.WGPUTexture, raw: c.WGPUTexture,
size: c.WGPUExtent3D,
usage: c.WGPUTextureUsage,
format: GpuTextureFormat,
gloc: GpuAllocator, gloc: GpuAllocator,
def: GpuTextureDef,
/// Allocates the underlying WebGPU handle and registers it to the parent GpuAllocator /// 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 use: u64 = 0;
var iter = usage.iterator(); var iter = def.usage.iterator();
while (iter.next()) |flag| use |= @intFromEnum(flag); while (iter.next()) |flag| use |= @intFromEnum(flag);
const desc = c.WGPUTextureDescriptor{ const desc = c.WGPUTextureDescriptor{
.usage = use, .usage = use,
.dimension = c.WGPUTextureDimension_2D, .dimension = c.WGPUTextureDimension_2D,
.size = size, .size = def.size,
.format = @intFromEnum(format), .format = @intFromEnum(def.format),
.mipLevelCount = 1, .mipLevelCount = 1,
.sampleCount = 1, .sampleCount = 1,
}; };
const raw = try gloc.allocTexture(desc); 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 /// Unregisters from the parent GpuAllocator and cleanly destroys GPU resources

View File

@ -35,12 +35,11 @@ pub fn main(init: std.process.Init) !void {
defer circle_rp.deinit(); defer circle_rp.deinit();
// 3. Create the offscreen VRAM texture to render into // 3. Create the offscreen VRAM texture to render into
const texture = try GpuTexture.init( const texture = try GpuTexture.init(gloc, .{
gloc, .format = .RGBA8Unorm,
.RGBA8Unorm, .size = .{ .width = width, .height = height, .depthOrArrayLayers = 1 },
.{ .width = width, .height = height, .depthOrArrayLayers = 1 }, .usage = .initMany(&.{ .RenderAttachment, .CopySrc }),
.initMany(&.{ .RenderAttachment, .CopySrc }), });
);
defer texture.deinit(); defer texture.deinit();
const target_view = c.wgpuTextureCreateView(texture.raw, null) orelse return error.View; const target_view = c.wgpuTextureCreateView(texture.raw, null) orelse return error.View;