zgpu

git clone git://git.electrosoup.com/zgpu
Log | Files | Refs | Submodules | README

commit 67797425147031b412ecbe1653008b8ff9f1e235
parent 36abab12b15fcd5709558a0b2d23d28a8aab26d7
Author: Christian Ermann <christianermann@gmail.com>
Date:   Sun, 20 Apr 2025 19:53:13 -0700

Request and release Adapter

Diffstat:
Asrc/adapter.zig | 15+++++++++++++++
Msrc/instance.zig | 94++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++-
Msrc/main.zig | 8++++++++
3 files changed, 116 insertions(+), 1 deletion(-)

diff --git a/src/adapter.zig b/src/adapter.zig @@ -0,0 +1,15 @@ +const std = @import("std"); + +const c = @import("c.zig").c; + +pub const Adapter = opaque { + pub fn release(adapter: *Adapter) void { + c.wgpuAdapterRelease(@ptrCast(adapter)); + } + + // enumerateFeatures(...) + // getInfo(...) + // getLimits(...) + // hasFeature(...) + // reference(...) +}; diff --git a/src/instance.zig b/src/instance.zig @@ -1,7 +1,48 @@ +const std = @import("std"); const c = @import("c.zig").c; const ChainedStruct = @import("common.zig").ChainedStruct; const Surface = @import("surface.zig").Surface; +const Adapter = @import("adapter.zig").Adapter; + +const RequestAdapterStatus = enum(u32) { + success = 0x00000000, + unavailable = 0x00000001, + err = 0x00000002, + unknown = 0x00000003, +}; + +const RequestAdapterError = error{ + Unavailable, + Error, + Unknown, +}; + +pub const RequestAdapterOptions = extern struct { + next: ?*const ChainedStruct = null, + compatible_surface: ?*Surface, + power_preference: PowerPreference, + backend_type: BackendType, + force_fallback_adapter: bool, + + const PowerPreference = enum(u32) { + undefined = 0x00000000, + low_power = 0x00000001, + high_performance = 0x00000002, + }; + + const BackendType = enum(u32) { + undefined = 0x00000000, + null = 0x00000001, + webgpu = 0x00000002, + d3d11 = 0x00000003, + d3d12 = 0x00000004, + metal = 0x00000005, + vulkan = 0x00000006, + opengl = 0x00000007, + opengles = 0x00000008, + }; +}; pub const Instance = opaque { pub const Descriptor = extern struct { @@ -72,7 +113,58 @@ pub const Instance = opaque { )); } + pub fn requestAdapter( + instance: *Instance, + options: *const RequestAdapterOptions, + ) !*Adapter { + const RequestAdapterResponse = struct { + status: RequestAdapterStatus, + adapter: ?*Adapter, + + pub fn callback( + status: RequestAdapterStatus, + adapter: ?*Adapter, + message: ?[*:0]const u8, + userdata: ?*anyopaque, + ) callconv(.C) void { + const self: *@This() = @alignCast(@ptrCast(userdata)); + switch (status) { + .success => {}, + else => { + std.log.err( + "failed to request adapter - {s}", + .{message.?}, + ); + }, + } + self.* = .{ + .status = status, + .adapter = adapter, + }; + } + }; + + var response: RequestAdapterResponse = undefined; + c.wgpuInstanceRequestAdapter( + @ptrCast(instance), + @ptrCast(options), + @ptrCast(&RequestAdapterResponse.callback), + @ptrCast(&response), + ); + switch (response.status) { + .success => return response.adapter.?, + .unavailable => { + return RequestAdapterError.Unavailable; + }, + .err => { + return RequestAdapterError.Error; + }, + .unknown => { + return RequestAdapterError.Unknown; + }, + } + } + // hasWGSLLanguageFeature(...) - // requestAdapter(...) // reference(...) }; diff --git a/src/main.zig b/src/main.zig @@ -64,4 +64,12 @@ pub fn main() !void { std.process.exit(1); }; defer surface.release(); + + const adapter = try instance.requestAdapter(&.{ + .compatible_surface = surface, + .power_preference = .undefined, + .backend_type = .undefined, + .force_fallback_adapter = false, + }); + defer adapter.release(); }