diff --git a/gpu/README.md b/gpu/README.md index 6ca7dd27..b76741eb 100644 --- a/gpu/README.md +++ b/gpu/README.md @@ -238,4 +238,6 @@ const descriptor = gpu.Surface.Descriptor{ ### Others +* `Device.createShaderModuleWGSL` (helper to create WGSL shader modules more nicely) + There may be other opportunities for helpers, to improve the existing APIs, or add utility APIs on top of the existing APIs. If you find one, please open an issue we'd love to consider it. diff --git a/gpu/examples/main.zig b/gpu/examples/main.zig index 7412e17f..9067aa10 100644 --- a/gpu/examples/main.zig +++ b/gpu/examples/main.zig @@ -49,24 +49,14 @@ pub fn main() !void { \\ return vec4(pos[VertexIndex], 0.0, 1.0); \\ } ; - const vs_module = setup.device.createShaderModule(&.{ - .next_in_chain = .{ .wgsl_descriptor = &.{ - .source = vs, - } }, - .label = "my vertex shader", - }); + const vs_module = setup.device.createShaderModuleWGSL("my vertex shader", vs); const fs = \\ @fragment fn main() -> @location(0) vec4 { \\ return vec4(1.0, 0.0, 0.0, 1.0); \\ } ; - const fs_module = setup.device.createShaderModule(&.{ - .next_in_chain = .{ .wgsl_descriptor = &.{ - .source = fs, - } }, - .label = "my fragment shader", - }); + const fs_module = setup.device.createShaderModuleWGSL("my fragment shader", fs); // Fragment state const blend = gpu.BlendState{ diff --git a/gpu/src/device.zig b/gpu/src/device.zig index 33106cfb..7049c3d0 100644 --- a/gpu/src/device.zig +++ b/gpu/src/device.zig @@ -194,6 +194,20 @@ pub const Device = opaque { return Impl.deviceCreateShaderModule(device, descriptor); } + /// Helper to make createShaderModule invocations slightly nicer. + pub inline fn createShaderModuleWGSL( + device: *Device, + label: ?[*:0]const u8, + wgsl_source: [*:0]const u8, + ) *ShaderModule { + return device.createShaderModule(&ShaderModule.Descriptor{ + .next_in_chain = .{ .wgsl_descriptor = &.{ + .source = wgsl_source, + } }, + .label = label, + }); + } + pub inline fn createSwapChain(device: *Device, surface: ?*Surface, descriptor: *const SwapChain.Descriptor) *SwapChain { return Impl.deviceCreateSwapChain(device, surface, descriptor); }