1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
|
//! Wrapper for handling textures.
const Self = @This();
const std = @import("std");
const Allocator = std.mem.Allocator;
const assert = std.debug.assert;
const builtin = @import("builtin");
const objc = @import("objc");
const mtl = @import("api.zig");
const Metal = @import("../Metal.zig");
const log = std.log.scoped(.metal);
/// Options for initializing a texture.
pub const Options = struct {
/// MTLDevice
device: objc.Object,
pixel_format: mtl.MTLPixelFormat,
resource_options: mtl.MTLResourceOptions,
usage: mtl.MTLTextureUsage,
};
/// The underlying MTLTexture Object.
texture: objc.Object,
/// The width of this texture.
width: usize,
/// The height of this texture.
height: usize,
/// Bytes per pixel for this texture.
bpp: usize,
pub const Error = error{
/// A Metal API call failed.
MetalFailed,
};
/// Initialize a texture
pub fn init(
opts: Options,
width: usize,
height: usize,
data: ?[]const u8,
) Error!Self {
// Create our descriptor
const desc = init: {
const Class = objc.getClass("MTLTextureDescriptor").?;
const id_alloc = Class.msgSend(objc.Object, objc.sel("alloc"), .{});
const id_init = id_alloc.msgSend(objc.Object, objc.sel("init"), .{});
break :init id_init;
};
defer desc.release();
// Set our properties
desc.setProperty("pixelFormat", @intFromEnum(opts.pixel_format));
desc.setProperty("width", @as(c_ulong, width));
desc.setProperty("height", @as(c_ulong, height));
desc.setProperty("resourceOptions", opts.resource_options);
desc.setProperty("usage", opts.usage);
// Initialize
const id = opts.device.msgSend(
?*anyopaque,
objc.sel("newTextureWithDescriptor:"),
.{desc},
) orelse return error.MetalFailed;
const self: Self = .{
.texture = objc.Object.fromId(id),
.width = width,
.height = height,
.bpp = bppOf(opts.pixel_format),
};
// If we have data, we set it here.
if (data) |d| {
assert(d.len == width * height * self.bpp);
try self.replaceRegion(0, 0, width, height, d);
}
return self;
}
pub fn deinit(self: Self) void {
self.texture.release();
}
/// Replace a region of the texture with the provided data.
///
/// Does NOT check the dimensions of the data to ensure correctness.
pub fn replaceRegion(
self: Self,
x: usize,
y: usize,
width: usize,
height: usize,
data: []const u8,
) error{}!void {
self.texture.msgSend(
void,
objc.sel("replaceRegion:mipmapLevel:withBytes:bytesPerRow:"),
.{
mtl.MTLRegion{
.origin = .{ .x = x, .y = y, .z = 0 },
.size = .{
.width = @intCast(width),
.height = @intCast(height),
.depth = 1,
},
},
@as(c_ulong, 0),
@as(*const anyopaque, data.ptr),
@as(c_ulong, self.bpp * width),
},
);
}
/// Returns the bytes per pixel for the provided pixel format
fn bppOf(pixel_format: mtl.MTLPixelFormat) usize {
return switch (pixel_format) {
// Invalid
.invalid => @panic("invalid pixel format"),
// Weird formats I was too lazy to get the sizes of
else => @panic("pixel format size unknown (unlikely that this format was actually used, could be memory corruption)"),
// 8-bit pixel formats
.a8unorm,
.r8unorm,
.r8unorm_srgb,
.r8snorm,
.r8uint,
.r8sint,
.rg8unorm,
.rg8unorm_srgb,
.rg8snorm,
.rg8uint,
.rg8sint,
.stencil8,
=> 1,
// 16-bit pixel formats
.r16unorm,
.r16snorm,
.r16uint,
.r16sint,
.r16float,
.rg16unorm,
.rg16snorm,
.rg16uint,
.rg16sint,
.rg16float,
.b5g6r5unorm,
.a1bgr5unorm,
.abgr4unorm,
.bgr5a1unorm,
.depth16unorm,
=> 2,
// 32-bit pixel formats
.rgba8unorm,
.rgba8unorm_srgb,
.rgba8snorm,
.rgba8uint,
.rgba8sint,
.bgra8unorm,
.bgra8unorm_srgb,
.rgb10a2unorm,
.rgb10a2uint,
.rg11b10float,
.rgb9e5float,
.bgr10a2unorm,
.bgr10_xr,
.bgr10_xr_srgb,
.r32uint,
.r32sint,
.r32float,
.depth32float,
.depth24unorm_stencil8,
=> 4,
// 64-bit pixel formats
.rg32uint,
.rg32sint,
.rg32float,
.rgba16unorm,
.rgba16snorm,
.rgba16uint,
.rgba16sint,
.rgba16float,
.bgra10_xr,
.bgra10_xr_srgb,
=> 8,
// 128-bit pixel formats,
.rgba32uint,
.rgba32sint,
.rgba32float,
=> 128,
};
}
|