Skip to content

Instantly share code, notes, and snippets.

@mlarouche
Last active March 13, 2022 12:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mlarouche/faf55d663a25c94830b9c89031d50da4 to your computer and use it in GitHub Desktop.
Save mlarouche/faf55d663a25c94830b9c89031d50da4 to your computer and use it in GitHub Desktop.
free function to Self wrapper
const std = @import("std");
const platform = @import("../platforms/platform.zig").platform;
const GpuResource = @import("gpu_resource.zig").GpuResource;
pub const CommandListKind = enum {
Graphics,
Compute,
Copy,
};
pub const CommandListHandle = struct {
index: u32 = 0,
};
pub const GraphicsCommandList = struct {
handle: CommandListHandle,
const Self = @This();
pub const close = commandListForward(Self, doClose);
pub const copyBufferRegion = commandListForward(Self, doCopyBufferRegion);
};
pub const ComputeCommandList = struct {
handle: CommandListHandle,
const Self = @This();
pub const close = commandListForward(Self, doClose);
pub const copyBufferRegion = commandListForward(Self, doCopyBufferRegion);
};
pub const CopyCommandList = struct {
handle: CommandListHandle,
const Self = @This();
pub const close = commandListForward(Self, doClose);
pub const copyBufferRegion = commandListForward(Self, doCopyBufferRegion);
};
fn doClose(handle: CommandListHandle) !void {
return platform.gpu.commandListClose(handle);
}
fn doCopyBufferRegion(
handle: CommandListHandle,
destination_resource: GpuResource,
destination_offset: u64,
source_resource: GpuResource,
source_offset: u64,
num_bytes: u64,
) !void {
return platform.gpu.commandListCopyBufferRegion(
handle,
destination_resource,
destination_offset,
source_resource,
source_offset,
num_bytes,
);
}
fn functionArgCount(function: anytype) usize {
const function_type = @TypeOf(function);
const info = @typeInfo(function_type);
if (info != .Fn) {
@compileError("ArgsTuple expects a function type");
}
return info.Fn.args.len;
}
fn getFunctionReturnType(function: anytype) type {
const function_type = @TypeOf(function);
const info = @typeInfo(function_type);
if (info != .Fn) {
@compileError("ArgsTuple expects a function type");
}
return info.Fn.return_type.?;
}
fn getForwardReturnType(comptime T: type, function: anytype) type {
return switch (functionArgCount(function) - 1) {
0 => return fn (self: T) getFunctionReturnType(function),
1 => return fn (self: T, arg1: anytype) getFunctionReturnType(function),
2 => return fn (self: T, arg1: anytype, arg2: anytype) getFunctionReturnType(function),
3 => return fn (self: T, arg1: anytype, arg2: anytype, arg3: anytype) getFunctionReturnType(function),
4 => return fn (self: T, arg1: anytype, arg2: anytype, arg3: anytype, arg4: anytype) getFunctionReturnType(function),
5 => return fn (self: T, arg1: anytype, arg2: anytype, arg3: anytype, arg4: anytype, arg5: anytype) getFunctionReturnType(function),
6 => return fn (self: T, arg1: anytype, arg2: anytype, arg3: anytype, arg4: anytype, arg5: anytype, arg6: anytype) getFunctionReturnType(function),
7 => return fn (self: T, arg1: anytype, arg2: anytype, arg3: anytype, arg4: anytype, arg5: anytype, arg6: anytype, arg7: anytype) getFunctionReturnType(function),
8 => return fn (self: T, arg1: anytype, arg2: anytype, arg3: anytype, arg4: anytype, arg5: anytype, arg6: anytype, arg7: anytype, arg8: anytype) getFunctionReturnType(function),
9 => return fn (self: T, arg1: anytype, arg2: anytype, arg3: anytype, arg4: anytype, arg5: anytype, arg6: anytype, arg7: anytype, arg8: anytype, arg9: anytype) getFunctionReturnType(function),
else => @compileError("add thunk for more parameters"),
};
}
fn commandListForward(comptime T: type, function: anytype) getForwardReturnType(T, function) {
const thunk_struct = struct {
fn thunk0(self: T) getFunctionReturnType(function) {
return function(self.handle);
}
fn thunk1(
self: T,
arg1: anytype,
) getFunctionReturnType(function) {
return function(
self.handle,
arg1,
);
}
fn thunk2(
self: T,
arg1: anytype,
arg2: anytype,
) getFunctionReturnType(function) {
return function(
self.handle,
arg1,
arg2,
);
}
fn thunk3(
self: T,
arg1: anytype,
arg2: anytype,
arg3: anytype,
) getFunctionReturnType(function) {
return function(
self.handle,
arg1,
arg2,
arg3,
);
}
fn thunk4(
self: T,
arg1: anytype,
arg2: anytype,
arg3: anytype,
arg4: anytype,
) getFunctionReturnType(function) {
return function(
self.handle,
arg1,
arg2,
arg3,
arg4,
);
}
fn thunk5(
self: T,
arg1: anytype,
arg2: anytype,
arg3: anytype,
arg4: anytype,
arg5: anytype,
) getFunctionReturnType(function) {
return function(
self.handle,
arg1,
arg2,
arg3,
arg4,
arg5,
);
}
fn thunk6(
self: T,
arg1: anytype,
arg2: anytype,
arg3: anytype,
arg4: anytype,
arg5: anytype,
arg6: anytype,
) getFunctionReturnType(function) {
return function(
self.handle,
arg1,
arg2,
arg3,
arg4,
arg5,
arg6,
);
}
fn thunk7(
self: T,
arg1: anytype,
arg2: anytype,
arg3: anytype,
arg4: anytype,
arg5: anytype,
arg6: anytype,
arg7: anytype,
) getFunctionReturnType(function) {
return function(
self.handle,
arg1,
arg2,
arg3,
arg4,
arg5,
arg6,
arg7,
);
}
fn thunk8(
self: T,
arg1: anytype,
arg2: anytype,
arg3: anytype,
arg4: anytype,
arg5: anytype,
arg6: anytype,
arg7: anytype,
arg8: anytype,
) getFunctionReturnType(function) {
return function(
self.handle,
arg1,
arg2,
arg3,
arg4,
arg5,
arg6,
arg7,
arg8,
);
}
fn thunk9(
self: T,
arg1: anytype,
arg2: anytype,
arg3: anytype,
arg4: anytype,
arg5: anytype,
arg6: anytype,
arg7: anytype,
arg8: anytype,
arg9: anytype,
) getFunctionReturnType(function) {
return function(
self.handle,
arg1,
arg2,
arg3,
arg4,
arg5,
arg6,
arg7,
arg8,
arg9,
);
}
};
return switch (functionArgCount(function) - 1) {
0 => return thunk_struct.thunk0,
1 => return thunk_struct.thunk1,
2 => return thunk_struct.thunk2,
3 => return thunk_struct.thunk3,
4 => return thunk_struct.thunk4,
5 => return thunk_struct.thunk5,
6 => return thunk_struct.thunk6,
7 => return thunk_struct.thunk7,
8 => return thunk_struct.thunk8,
9 => return thunk_struct.thunk9,
else => @compileError("add thunk for more parameters"),
};
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment