Skip to content

Commit 38b2ccc

Browse files
committed
Wrapping the QuakeMdl memory allocation in an Arena
1 parent 1bdab51 commit 38b2ccc

File tree

1 file changed

+15
-13
lines changed

1 file changed

+15
-13
lines changed

src/framework/utils/quakemdl.zig

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,14 @@ const graphics = @import("../platform/graphics.zig");
1111

1212
const assert = std.debug.assert;
1313
const Allocator = std.mem.Allocator;
14+
const ArenaAllocator = std.heap.ArenaAllocator;
1415
const File = std.fs.File;
1516

1617
pub const MDL = struct {
1718
frames: []MDLFrameType,
1819
skins: []MDLSkinType,
1920
material: graphics.Material,
20-
allocator: Allocator,
21+
arena_allocator: ArenaAllocator,
2122

2223
pub fn deinit(self: *MDL) void {
2324
for (self.skins) |skin| {
@@ -35,9 +36,7 @@ pub const MDL = struct {
3536
}
3637
}
3738

38-
self.allocator.free(self.frames);
39-
self.allocator.free(self.skins);
40-
39+
self.arena_allocator.deinit();
4140
self.material.deinit();
4241
}
4342
};
@@ -183,7 +182,7 @@ const MDLSkinGroup_ = struct {
183182
// Skin intervals
184183
const intervals_buff = try allocator.alloc(u8, count * @sizeOf(f32));
185184
_ = try file.read(intervals_buff);
186-
const intervals: []f32 = try bytesToStructArray(f32, intervals_buff);
185+
const intervals: []f32 = try bytesToStructArray(f32, allocator, intervals_buff);
187186

188187
// Skin pixels
189188
const size: u32 = width * height * count;
@@ -250,7 +249,7 @@ const MDLFrame_ = struct {
250249
const vertbuff = try allocator.alloc(u8, @sizeOf(TriVertex_) * vertex_count);
251250
defer allocator.free(vertbuff);
252251
_ = try file.read(vertbuff);
253-
const trivertexes = try bytesToStructArray(TriVertex_, vertbuff);
252+
const trivertexes = try bytesToStructArray(TriVertex_, allocator, vertbuff);
254253

255254
return .{
256255
.min = min,
@@ -283,7 +282,7 @@ const MDLFrameGroup_ = struct {
283282
// Frame intervals
284283
const intervals_buff = try allocator.alloc(u8, count * @sizeOf(f32));
285284
_ = try file.read(intervals_buff);
286-
const intervals: []f32 = try bytesToStructArray(f32, intervals_buff);
285+
const intervals: []f32 = try bytesToStructArray(f32, allocator, intervals_buff);
287286

288287
// Frames
289288
const frames: []MDLFrame_ = try allocator.alloc(MDLFrame_, count);
@@ -332,8 +331,7 @@ const TriVertex_ = struct {
332331
}
333332
};
334333

335-
fn bytesToStructArray(comptime T: type, bytes: []u8) std.mem.Allocator.Error![]T {
336-
const allocator = mem.getAllocator();
334+
fn bytesToStructArray(comptime T: type, allocator: Allocator, bytes: []u8) std.mem.Allocator.Error![]T {
337335
const size: u32 = @sizeOf(T);
338336
const length: u32 = @as(u32, @intCast(bytes.len)) / size;
339337
const result: []T = try allocator.alloc(T, length);
@@ -417,7 +415,10 @@ fn makeMesh(allocator: Allocator, frame: MDLFrame_, config: MDLMeshBuildConfig_)
417415
return builder.buildMesh(config.material);
418416
}
419417

420-
pub fn open(allocator: Allocator, path: []const u8) !MDL {
418+
pub fn open(in_allocator: Allocator, path: []const u8) !MDL {
419+
var arena = ArenaAllocator.init(in_allocator);
420+
var allocator = arena.allocator();
421+
421422
var file = try std.fs.cwd().openFile(
422423
path,
423424
std.fs.File.OpenFlags{
@@ -465,6 +466,7 @@ pub fn open(allocator: Allocator, path: []const u8) !MDL {
465466
// Material
466467
const default_material = try graphics.Material.init(.{
467468
.shader = graphics.Shader.initFromBuiltin(.{ .vertex_attributes = mesh.getShaderAttributes() }, default_mesh),
469+
.own_shader = true,
468470
.texture_0 = skins[0].single.texture,
469471
.samplers = &[_]graphics.FilterMode{.NEAREST},
470472
});
@@ -473,14 +475,14 @@ pub fn open(allocator: Allocator, path: []const u8) !MDL {
473475
const stvert_buff: []u8 = try allocator.alloc(u8, @sizeOf(STVertex_) * header.vertex_count);
474476
defer allocator.free(stvert_buff);
475477
_ = try file.read(stvert_buff);
476-
const stvertices = try bytesToStructArray(STVertex_, stvert_buff);
478+
const stvertices = try bytesToStructArray(STVertex_, allocator, stvert_buff);
477479
defer allocator.free(stvertices);
478480

479481
// Triangles
480482
const triangle_buff: []u8 = try allocator.alloc(u8, @sizeOf(Triangle_) * header.triangle_count);
481483
defer allocator.free(triangle_buff);
482484
_ = try file.read(triangle_buff);
483-
const triangles = try bytesToStructArray(Triangle_, triangle_buff);
485+
const triangles = try bytesToStructArray(Triangle_, allocator, triangle_buff);
484486
defer allocator.free(triangles);
485487

486488
// Transform
@@ -544,7 +546,7 @@ pub fn open(allocator: Allocator, path: []const u8) !MDL {
544546
.frames = frames,
545547
.skins = skins,
546548
.material = default_material,
547-
.allocator = allocator,
549+
.arena_allocator = arena,
548550
};
549551

550552
return mdl;

0 commit comments

Comments
 (0)