Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
67 changes: 48 additions & 19 deletions src/adapter.zig
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,42 @@ pub const RequestAdapterOptions = extern struct {

power_preference: PowerPreference = PowerPreference.@"undefined",

// If true, requires the adapter to be a "fallback" adapter as defined by the JS spec.
// If this is not possible, the request returns null.
force_fallback_adapter: bool = false,

// If set, requires the adapter to have a particular backend type.
// If this is not possible, the request returns null.
backend_type: BackendType = BackendType.@"undefined",

// If set, requires the adapter to be able to output to a particular surface.
// If this is not possible, the request returns null.
compatible_surface: ?*Surface = null,

pub fn toWGPU(self: RequestAdapterOptions) WGPURequestAdapterOptions {
return WGPURequestAdapterOptions{
.next_in_chain = self.next_in_chain,
.feature_level = self.feature_level,
.power_preference = self.power_preference,
.force_fallback_adapter = @intFromBool(self.force_fallback_adapter),
.backend_type = self.backend_type,
.compatible_surface = self.compatible_surface,
};
}
};

pub const WGPURequestAdapterOptions = extern struct {

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we want to make all of these WGPU structs public? I've been changing them to private, but I suppose it wouldn't be a bad idea to expose them in some way, as long as it doesn't lead to confusion. I think in root.zig we should probably export them under their own namespace so we don't have people mistakenly mixing them with our wrapper code. There has been talk about Zig getting rid of usingnamespace so I need to do some work on root.zig soon anyway.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's up to you. I personally like to have things exposed so I can use them everywhere I'd need (especially types), but that's just me.

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Alright, I think let's make them public, but before we merge in the draft PR we put the WGPU structs into their own namespace.

next_in_chain: ?*const ChainedStruct = null,

// "Feature level" for the adapter request. If an adapter is returned,
// it must support the features and limits in the requested feature level.
//
// Implementations may ignore FeatureLevel.compatibility and provide FeatureLevel.core instead.
// FeatureLevel.core is the default in the JS API, but in C, this field is **required** (must not be undefined).
feature_level: FeatureLevel = FeatureLevel.core,

power_preference: PowerPreference = PowerPreference.@"undefined",

// If true, requires the adapter to be a "fallback" adapter as defined by the JS spec.
// If this is not possible, the request returns null.
force_fallback_adapter: WGPUBool = @intFromBool(false),
Expand All @@ -83,6 +119,17 @@ pub const RequestAdapterOptions = extern struct {
// If set, requires the adapter to be able to output to a particular surface.
// If this is not possible, the request returns null.
compatible_surface: ?*Surface = null,

pub fn toRequestAdapterOptions(self: WGPURequestAdapterOptions) RequestAdapterOptions {
return RequestAdapterOptions{
.next_in_chain = self.next_in_chain,
.feature_level = self.feature_level,
.power_preference = self.power_preference,
.force_fallback_adapter = self.force_fallback_adapter != 0,
.backend_type = self.backend_type,
.compatible_surface = self.compatible_surface,
};
}
};

const RequestAdapterStatus = enum(u32) {
Expand Down Expand Up @@ -284,25 +331,7 @@ pub const Adapter = opaque{
};

if(descriptor) |d| {
var device_extras: ?*const ChainedStruct = undefined;
if(d.native_extras) |native_extras| {
device_extras = @ptrCast(&WGPUDeviceExtras {
.trace_path = .fromSlice(native_extras.trace_path),
});
} else {
device_extras = null;
}

return wgpuAdapterRequestDevice(self, &WGPUDeviceDescriptor{
.next_in_chain = device_extras,
.label = .fromSlice(d.label),
.required_feature_count = d.required_features.len,
.required_features = d.required_features.ptr,
.required_limits = if(d.required_limits) |l| &l else null,
.default_queue = d.default_queue,
.device_lost_callback_info = d.device_lost_callback_info,
.uncaptured_error_callback_info = d.uncaptured_error_callback_info,
}, callback_info);
return wgpuAdapterRequestDevice(self, &d.toWGPU(), callback_info);
} else {
return wgpuAdapterRequestDevice(self, null, callback_info);
}
Expand Down
40 changes: 40 additions & 0 deletions src/device.zig
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,28 @@ pub const DeviceDescriptor = struct {
device_lost_callback_info: DeviceLostCallbackInfo = DeviceLostCallbackInfo {},
uncaptured_error_callback_info: UncapturedErrorCallbackInfo = UncapturedErrorCallbackInfo{},
native_extras: ?DeviceExtras = null,

pub fn toWGPU(self: DeviceDescriptor) WGPUDeviceDescriptor {
var device_extras: ?*const ChainedStruct = undefined;
if(self.native_extras) |native_extras| {
device_extras = @ptrCast(&WGPUDeviceExtras {
.trace_path = .fromSlice(native_extras.trace_path),
});
} else {
device_extras = null;
}

return WGPUDeviceDescriptor {
.next_in_chain = device_extras,
.label = .fromSlice(self.label),
.required_feature_count = self.required_features.len,
.required_features = self.required_features.ptr,
.required_limits = if(self.required_limits) |l| &l else null,
.default_queue = self.default_queue,
.device_lost_callback_info = self.device_lost_callback_info,
.uncaptured_error_callback_info = self.uncaptured_error_callback_info,
};
}
};

pub const WGPUDeviceDescriptor = extern struct {
Expand All @@ -158,6 +180,24 @@ pub const WGPUDeviceDescriptor = extern struct {
default_queue: QueueDescriptor = QueueDescriptor{},
device_lost_callback_info: DeviceLostCallbackInfo = DeviceLostCallbackInfo {},
uncaptured_error_callback_info: UncapturedErrorCallbackInfo = UncapturedErrorCallbackInfo{},

pub fn toDeviceDescriptor(self: WGPUDeviceDescriptor) DeviceDescriptor {
var device_extras: ?*const WGPUDeviceExtras = null;
if(self.next_in_chain) |n| {
device_extras = @ptrCast(n);
}

return DeviceDescriptor{
.label = self.label,
.required_feature_count = self.required_feature_count,
.required_features = self.required_features,
.required_limits = self.required_limits,
.default_queue = self.default_queue,
.device_lost_callback_info = self.device_lost_callback_info,
.uncaptured_error_callback_info = self.uncaptured_error_callback_info,
.native_extras = if(device_extras) |d| d.trace_path.toSlice() else null,
};
}
};

const RequestDeviceStatus = enum(u32) {
Expand Down
5 changes: 3 additions & 2 deletions src/instance.zig
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ const SType = _chained_struct.SType;
const _adapter = @import("adapter.zig");
const Adapter = _adapter.Adapter;
const RequestAdapterOptions = _adapter.RequestAdapterOptions;
const WGPURequestAdapterOptions = _adapter.WGPURequestAdapterOptions;
const RequestAdapterCallbackInfo = _adapter.RequestAdapterCallbackInfo;
const RequestAdapterCallback = _adapter.RequestAdapterCallback;
const RequestAdapterError = _adapter.RequestAdapterError;
Expand Down Expand Up @@ -213,7 +214,7 @@ extern fn wgpuInstanceCreateSurface(instance: *Instance, descriptor: *const Surf
extern fn wgpuInstanceGetWGSLLanguageFeatures(instance: *Instance, features: *WGPUSupportedWGSLLanguageFeatures) Status;
extern fn wgpuInstanceHasWGSLLanguageFeature(instance: *Instance, feature: WGSLLanguageFeatureName) WGPUBool;
extern fn wgpuInstanceProcessEvents(instance: *Instance) void;
extern fn wgpuInstanceRequestAdapter(instance: *Instance, options: ?*const RequestAdapterOptions, callback_info: RequestAdapterCallbackInfo) Future;
extern fn wgpuInstanceRequestAdapter(instance: *Instance, options: ?*const WGPURequestAdapterOptions, callback_info: RequestAdapterCallbackInfo) Future;
extern fn wgpuInstanceWaitAny(instance: *Instance, future_count: usize, futures: ?[*] FutureWaitInfo, timeout_ns: u64) WaitStatus;
extern fn wgpuInstanceAddRef(instance: *Instance) void;
extern fn wgpuInstanceRelease(instance: *Instance) void;
Expand Down Expand Up @@ -363,7 +364,7 @@ pub const Instance = opaque {
.userdata2 = @constCast(@ptrCast(callback)),
};
if (options) |o| {
return wgpuInstanceRequestAdapter(self, &o, callback_info);
return wgpuInstanceRequestAdapter(self, &o.toWGPU(), callback_info);
} else {
return wgpuInstanceRequestAdapter(self, null, callback_info);
}
Expand Down