gpu: add Dawn initialization safety check
Signed-off-by: Stephen Gutekanst <stephen@hexops.com>
This commit is contained in:
parent
ada4f4134c
commit
8b8d16bbc8
1 changed files with 4 additions and 0 deletions
|
|
@ -1,10 +1,12 @@
|
||||||
const gpu = @import("main.zig");
|
const gpu = @import("main.zig");
|
||||||
|
const builtin = @import("builtin");
|
||||||
|
|
||||||
const c = @cImport({
|
const c = @cImport({
|
||||||
@cInclude("dawn/webgpu.h");
|
@cInclude("dawn/webgpu.h");
|
||||||
@cInclude("mach_dawn.h");
|
@cInclude("mach_dawn.h");
|
||||||
});
|
});
|
||||||
|
|
||||||
|
var didInit = false;
|
||||||
var procs: c.DawnProcTable = undefined;
|
var procs: c.DawnProcTable = undefined;
|
||||||
|
|
||||||
/// A Dawn implementation of the gpu.Interface, which merely directs calls to the Dawn proc table.
|
/// A Dawn implementation of the gpu.Interface, which merely directs calls to the Dawn proc table.
|
||||||
|
|
@ -12,10 +14,12 @@ var procs: c.DawnProcTable = undefined;
|
||||||
/// Before use, it must be `.init()`ialized in order to set the global proc table.
|
/// Before use, it must be `.init()`ialized in order to set the global proc table.
|
||||||
pub const Interface = struct {
|
pub const Interface = struct {
|
||||||
pub fn init() void {
|
pub fn init() void {
|
||||||
|
didInit = true;
|
||||||
procs = c.machDawnGetProcTable();
|
procs = c.machDawnGetProcTable();
|
||||||
}
|
}
|
||||||
|
|
||||||
pub inline fn createInstance(descriptor: ?*const gpu.Instance.Descriptor) ?*gpu.Instance {
|
pub inline fn createInstance(descriptor: ?*const gpu.Instance.Descriptor) ?*gpu.Instance {
|
||||||
|
if (builtin.mode == .Debug and !didInit) @panic("dawn: not initialized; did you forget to call gpu.Impl.init()?");
|
||||||
return @ptrCast(?*gpu.Instance, procs.createInstance.?(
|
return @ptrCast(?*gpu.Instance, procs.createInstance.?(
|
||||||
@ptrCast(?*const c.WGPUInstanceDescriptor, descriptor),
|
@ptrCast(?*const c.WGPUInstanceDescriptor, descriptor),
|
||||||
));
|
));
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue