Added Plugins [Platform, Window, Device]
This commit is contained in:
parent
d00fdbdbcc
commit
0d72225229
7 changed files with 320 additions and 0 deletions
4
src/plugins/device/resource/wgpu_device.zig
Normal file
4
src/plugins/device/resource/wgpu_device.zig
Normal file
|
|
@ -0,0 +1,4 @@
|
|||
const wgpu = @import("wgpu");
|
||||
|
||||
raw: *wgpu.WGPUDeviceImpl,
|
||||
queue: *wgpu.WGPUQueueImpl,
|
||||
32
src/plugins/device/root.zig
Normal file
32
src/plugins/device/root.zig
Normal file
|
|
@ -0,0 +1,32 @@
|
|||
//! ----------------------------------------------------
|
||||
//! ----------------------------------------------------
|
||||
|
||||
//
|
||||
// PRIVATE
|
||||
//
|
||||
|
||||
const App = @import("app");
|
||||
const lifetime = @import("system/lifetime.zig");
|
||||
|
||||
//
|
||||
// PUBLIC
|
||||
//
|
||||
|
||||
pub const WGPUDevice = @import("resource/wgpu_device.zig");
|
||||
|
||||
/// ----------------------------------------------------
|
||||
/// ----------------------------------------------------
|
||||
pub const plugin: App.plugin.Plugin = .{
|
||||
.name = "Device",
|
||||
.build = build,
|
||||
};
|
||||
|
||||
/// ----------------------------------------------------
|
||||
/// ----------------------------------------------------
|
||||
fn build(
|
||||
_: *App.plugin.Plugin,
|
||||
app: *App,
|
||||
) !void {
|
||||
try app.schedules.add(App.stage.init, lifetime.init);
|
||||
try app.schedules.add(App.stage.deinit, lifetime.deinit);
|
||||
}
|
||||
161
src/plugins/device/system/lifetime.zig
Normal file
161
src/plugins/device/system/lifetime.zig
Normal file
|
|
@ -0,0 +1,161 @@
|
|||
//! ----------------------------------------------------
|
||||
//! ----------------------------------------------------
|
||||
|
||||
const std = @import("std");
|
||||
const wgpu = @import("wgpu");
|
||||
const App = @import("app");
|
||||
|
||||
const WGPUInstance = @import("../../platform/resource/wgpu_instance.zig");
|
||||
const WGPUDevice = @import("../resource/wgpu_device.zig");
|
||||
|
||||
/// ----------------------------------------------------
|
||||
/// ----------------------------------------------------
|
||||
const AdapterUD = struct {
|
||||
adapter: ?*wgpu.WGPUAdapterImpl = null,
|
||||
ended: bool = false,
|
||||
};
|
||||
|
||||
/// ----------------------------------------------------
|
||||
/// ----------------------------------------------------
|
||||
const DeviceUD = struct {
|
||||
device: ?*wgpu.WGPUDeviceImpl = null,
|
||||
ended: bool = false,
|
||||
};
|
||||
|
||||
/// ----------------------------------------------------
|
||||
/// ----------------------------------------------------
|
||||
pub fn init(app: *App) !void {
|
||||
// --- GET WGPU INSTANCE ---
|
||||
const instance = app.resources.getPtr(
|
||||
WGPUInstance,
|
||||
) orelse return error.WGPUInstanceNotSet;
|
||||
|
||||
//
|
||||
// ADAPTER
|
||||
//
|
||||
|
||||
// TODO: Mabe Mabe add other backends n stuff
|
||||
var adapter_userdata: AdapterUD = .{};
|
||||
_ = wgpu.wgpuInstanceRequestAdapter(
|
||||
instance.raw,
|
||||
&wgpu.WGPURequestAdapterOptions{
|
||||
.backendType = wgpu.WGPUBackendType_Vulkan,
|
||||
},
|
||||
wgpu.WGPURequestAdapterCallbackInfo{
|
||||
.callback = adapterCallback,
|
||||
.userdata1 = &adapter_userdata,
|
||||
},
|
||||
);
|
||||
|
||||
// --- ADAPTER ---
|
||||
const adapter = if (adapter_userdata.adapter) |v| v else return error.NoAdapter;
|
||||
defer wgpu.wgpuAdapterRelease(adapter);
|
||||
|
||||
//
|
||||
// DEVICE
|
||||
//
|
||||
|
||||
// --- REQUEST DEVICE ---
|
||||
var device_userdata: DeviceUD = .{};
|
||||
_ = wgpu.wgpuAdapterRequestDevice(
|
||||
adapter,
|
||||
&wgpu.WGPUDeviceDescriptor{
|
||||
.label = .{ .data = "MainDevice", .length = wgpu.WGPU_STRLEN },
|
||||
.uncapturedErrorCallbackInfo = .{
|
||||
.callback = uncapturedErrorCallback,
|
||||
},
|
||||
},
|
||||
.{
|
||||
.callback = deviceCallback,
|
||||
.userdata1 = &device_userdata,
|
||||
},
|
||||
);
|
||||
|
||||
// --- NEW WGPU DEVICE ---
|
||||
try app.resources.set(WGPUDevice{
|
||||
.raw = device_userdata.device orelse return error.DeviceIsBeginRetarded,
|
||||
.queue = wgpu.wgpuDeviceGetQueue(
|
||||
device_userdata.device,
|
||||
) orelse return error.FailedToGetDeviceQueue,
|
||||
});
|
||||
}
|
||||
|
||||
/// ----------------------------------------------------
|
||||
/// ----------------------------------------------------
|
||||
pub fn deinit(app: *App) !void {
|
||||
// --- # ---
|
||||
const device = app.resources.getPtr(
|
||||
WGPUDevice,
|
||||
) orelse return error.WGPUDeviceNotSet;
|
||||
|
||||
// --- # ---
|
||||
wgpu.wgpuQueueRelease(device.queue);
|
||||
wgpu.wgpuDeviceRelease(device.raw);
|
||||
}
|
||||
|
||||
/// ----------------------------------------------------
|
||||
/// ----------------------------------------------------
|
||||
fn uncapturedErrorCallback(
|
||||
_: [*c]const wgpu.WGPUDevice,
|
||||
err_type: wgpu.WGPUErrorType,
|
||||
message: wgpu.WGPUStringView,
|
||||
_: ?*anyopaque,
|
||||
_: ?*anyopaque,
|
||||
) callconv(.c) void {
|
||||
std.debug.print("[{s}] {s}\n", .{
|
||||
switch (err_type) {
|
||||
wgpu.WGPUErrorType_Validation => "\x1b[33mVALIDATION\x1b[0m",
|
||||
wgpu.WGPUErrorType_OutOfMemory => "\x1b[31mOUT_OF_MEMORY\x1b[0m",
|
||||
else => "IDK",
|
||||
},
|
||||
message.data,
|
||||
});
|
||||
}
|
||||
|
||||
/// ----------------------------------------------------
|
||||
/// ----------------------------------------------------
|
||||
fn adapterCallback(
|
||||
status: wgpu.WGPURequestAdapterStatus,
|
||||
adapter: wgpu.WGPUAdapter,
|
||||
message: wgpu.WGPUStringView,
|
||||
raw_userdata: ?*anyopaque,
|
||||
_: ?*anyopaque,
|
||||
) callconv(.c) void {
|
||||
// --- USERDATA ---
|
||||
const userdata: *AdapterUD = @ptrCast(@alignCast(raw_userdata));
|
||||
defer userdata.ended = true;
|
||||
|
||||
// --- STATUS ---
|
||||
switch (status) {
|
||||
wgpu.WGPURequestAdapterStatus_Success => userdata.adapter = adapter,
|
||||
else => {
|
||||
std.debug.print("Failed to get WebGPU adapter: {s}\n", .{
|
||||
message.data,
|
||||
});
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
/// ----------------------------------------------------
|
||||
/// ----------------------------------------------------
|
||||
fn deviceCallback(
|
||||
status: wgpu.WGPURequestDeviceStatus,
|
||||
device: wgpu.WGPUDevice,
|
||||
message: wgpu.WGPUStringView,
|
||||
raw_userdata: ?*anyopaque,
|
||||
_: ?*anyopaque,
|
||||
) callconv(.c) void {
|
||||
// --- USERDATA ---
|
||||
const userdata: *DeviceUD = @ptrCast(@alignCast(raw_userdata));
|
||||
defer userdata.ended = true;
|
||||
|
||||
// --- STATUS ---
|
||||
switch (status) {
|
||||
wgpu.WGPURequestDeviceStatus_Success => userdata.device = device,
|
||||
else => {
|
||||
std.debug.print("Failed to get WebGPU adapter: {s}\n", .{
|
||||
message.data,
|
||||
});
|
||||
},
|
||||
}
|
||||
}
|
||||
3
src/plugins/platform/resource/wgpu_instance.zig
Normal file
3
src/plugins/platform/resource/wgpu_instance.zig
Normal file
|
|
@ -0,0 +1,3 @@
|
|||
const wgpu = @import("wgpu");
|
||||
|
||||
raw: *wgpu.WGPUInstanceImpl,
|
||||
64
src/plugins/platform/root.zig
Normal file
64
src/plugins/platform/root.zig
Normal file
|
|
@ -0,0 +1,64 @@
|
|||
//! ----------------------------------------------------
|
||||
//! ----------------------------------------------------
|
||||
|
||||
//
|
||||
// PRIVATE
|
||||
//
|
||||
|
||||
const std = @import("std");
|
||||
const sdl = @import("sdl");
|
||||
const wgpu = @import("wgpu");
|
||||
const App = @import("app");
|
||||
|
||||
//
|
||||
// PUBLIC
|
||||
//
|
||||
|
||||
pub const WGPUInstance = @import("resource/wgpu_instance.zig");
|
||||
|
||||
/// ----------------------------------------------------
|
||||
/// ----------------------------------------------------
|
||||
pub const plugin: App.plugin.Plugin = .{
|
||||
.name = "Platform",
|
||||
.build = build,
|
||||
};
|
||||
|
||||
/// ----------------------------------------------------
|
||||
/// ----------------------------------------------------
|
||||
fn build(
|
||||
_: *App.plugin.Plugin,
|
||||
app: *App,
|
||||
) !void {
|
||||
try app.schedules.add(App.stage.init, init);
|
||||
try app.schedules.add(App.stage.deinit, deinit);
|
||||
}
|
||||
|
||||
/// ----------------------------------------------------
|
||||
/// ----------------------------------------------------
|
||||
pub fn init(app: *App) !void {
|
||||
// --- SDL3 INIT ---
|
||||
if (!sdl.SDL_Init(sdl.SDL_INIT_VIDEO)) {
|
||||
std.debug.print("[SDL_ERROR] SDL_Init: {s}\n", .{sdl.SDL_GetError()});
|
||||
return error.SDL_INIT;
|
||||
}
|
||||
|
||||
// --- WGPU INIT ---
|
||||
try app.resources.set(WGPUInstance{
|
||||
.raw = wgpu.wgpuCreateInstance(&wgpu.WGPUInstanceDescriptor{}) orelse return {
|
||||
return error.WGPU_Init;
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
/// ----------------------------------------------------
|
||||
/// ----------------------------------------------------
|
||||
pub fn deinit(app: *App) !void {
|
||||
// --- # ---
|
||||
const res = try app.resources.getMany(struct {
|
||||
instance: *WGPUInstance,
|
||||
});
|
||||
|
||||
// --- # ---
|
||||
wgpu.wgpuInstanceRelease(res.instance.raw);
|
||||
sdl.SDL_Quit();
|
||||
}
|
||||
28
src/plugins/template/root.zig
Normal file
28
src/plugins/template/root.zig
Normal file
|
|
@ -0,0 +1,28 @@
|
|||
//! ----------------------------------------------------
|
||||
//! ----------------------------------------------------
|
||||
|
||||
//
|
||||
// PRIVATE
|
||||
//
|
||||
|
||||
const App = @import("app");
|
||||
|
||||
//
|
||||
// PUBLIC
|
||||
//
|
||||
|
||||
/// ----------------------------------------------------
|
||||
/// ----------------------------------------------------
|
||||
pub const plugin: App.plugin.Plugin = .{
|
||||
.name = "Template",
|
||||
.build = build,
|
||||
};
|
||||
|
||||
/// ----------------------------------------------------
|
||||
/// ----------------------------------------------------
|
||||
fn build(
|
||||
_: *App.plugin.Plugin,
|
||||
app: *App,
|
||||
) !void {
|
||||
_ = app;
|
||||
}
|
||||
28
src/plugins/window/root.zig
Normal file
28
src/plugins/window/root.zig
Normal file
|
|
@ -0,0 +1,28 @@
|
|||
//! ----------------------------------------------------
|
||||
//! ----------------------------------------------------
|
||||
|
||||
//
|
||||
// PRIVATE
|
||||
//
|
||||
|
||||
const App = @import("app");
|
||||
|
||||
//
|
||||
// PUBLIC
|
||||
//
|
||||
|
||||
/// ----------------------------------------------------
|
||||
/// ----------------------------------------------------
|
||||
pub const plugin: App.plugin.Plugin = .{
|
||||
.name = "Window",
|
||||
.build = build,
|
||||
};
|
||||
|
||||
/// ----------------------------------------------------
|
||||
/// ----------------------------------------------------
|
||||
fn build(
|
||||
_: *App.plugin.Plugin,
|
||||
app: *App,
|
||||
) !void {
|
||||
_ = app;
|
||||
}
|
||||
Loading…
Add table
Reference in a new issue