Skip to content

Commit cfcabe1

Browse files
kcbannerhazeycode
andauthored
Remove usingnamespace usage, build script fixups, and support linking memory allocation functions from a shared library (#6)
* Use the new @extern field, .is_dll_import, to link the memory allocation functions from a shared library with the -msvc ABI * Alignment fixup * Remove usingnamespace usage * Fixup build script / callconv deprecations * Change `appendMeshPrimitive` to use unmanaged data structures * Update ArrayList usage to 0.15.1 * bump minimum_zig_version to 0.15.1 * Update README code examples for Zig 0.15.1 --------- Co-authored-by: Chris Heyes <[email protected]>
1 parent 06ca952 commit cfcabe1

File tree

7 files changed

+208
-205
lines changed

7 files changed

+208
-205
lines changed

README.md

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -74,11 +74,12 @@ pub fn main() !void {
7474
const data = try zmesh.io.zcgltf.parseAndLoadFile(content_dir ++ "cube.gltf");
7575
defer zmesh.io.zcgltf.freeData(data);
7676
77-
var mesh_indices = std.ArrayList(u32).init(allocator);
78-
var mesh_positions = std.ArrayList([3]f32).init(allocator);
79-
var mesh_normals = std.ArrayList([3]f32).init(allocator);
77+
var mesh_indices = std.ArrayListUnmanaged(u32){};
78+
var mesh_positions = std.ArrayListUnmanaged([3]f32){};
79+
var mesh_normals = std.ArrayListUnmanaged([3]f32){};
8080
81-
zmesh.io.zcgltf.appendMeshPrimitive(
81+
try zmesh.io.zcgltf.appendMeshPrimitive(
82+
allocator,
8283
data,
8384
0, // mesh index
8485
0, // gltf primitive index (submesh index)
@@ -98,8 +99,8 @@ pub fn main() !void {
9899
normal: [3]f32,
99100
};
100101
101-
var remap = std.ArrayList(u32).init(allocator);
102-
remap.resize(src_indices.items.len) catch unreachable;
102+
var remap = std.ArrayListUnmanaged(u32){};
103+
try remap.resize(allocator, src_indices.items.len);
103104
104105
const num_unique_vertices = zmesh.opt.generateVertexRemap(
105106
remap.items, // 'vertex remap' (destination)
@@ -108,8 +109,8 @@ pub fn main() !void {
108109
src_vertices.items, // non-optimized vertices
109110
);
110111
111-
var optimized_vertices = std.ArrayList(Vertex).init(allocator);
112-
optimized_vertices.resize(num_unique_vertices) catch unreachable;
112+
var optimized_vertices = std.ArrayListUnmanaged(Vertex){};
113+
try optimized_vertices.resize(allocator, num_unique_vertices);
113114
114115
zmesh.opt.remapVertexBuffer(
115116
Vertex, // Zig type describing your vertex

build.zig

Lines changed: 16 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -23,34 +23,31 @@ pub fn build(b: *std.Build) void {
2323
}
2424

2525
const options_module = options_step.createModule();
26-
27-
_ = b.addModule("root", .{
26+
const zmesh_module = b.addModule("root", .{
2827
.root_source_file = b.path("src/root.zig"),
28+
.target = target,
29+
.optimize = optimize,
2930
.imports = &.{
3031
.{ .name = "zmesh_options", .module = options_module },
3132
},
3233
});
3334

34-
const zmesh_lib = if (options.shared) blk: {
35-
const lib = b.addSharedLibrary(.{
36-
.name = "zmesh",
35+
const zmesh_lib = b.addLibrary(.{
36+
.name = "zmesh",
37+
.linkage = if (options.shared) .dynamic else .static,
38+
.root_module = b.createModule(.{
3739
.target = target,
3840
.optimize = optimize,
39-
});
41+
}),
42+
});
4043

41-
if (target.result.os.tag == .windows) {
42-
lib.root_module.addCMacro("PAR_SHAPES_API", "__declspec(dllexport)");
43-
lib.root_module.addCMacro("CGLTF_API", "__declspec(dllexport)");
44-
lib.root_module.addCMacro("MESHOPTIMIZER_API", "__declspec(dllexport)");
45-
lib.root_module.addCMacro("ZMESH_API", "__declspec(dllexport)");
46-
}
44+
if (options.shared and target.result.os.tag == .windows) {
45+
zmesh_lib.root_module.addCMacro("PAR_SHAPES_API", "__declspec(dllexport)");
46+
zmesh_lib.root_module.addCMacro("CGLTF_API", "__declspec(dllexport)");
47+
zmesh_lib.root_module.addCMacro("MESHOPTIMIZER_API", "__declspec(dllexport)");
48+
zmesh_lib.root_module.addCMacro("ZMESH_API", "__declspec(dllexport)");
49+
}
4750

48-
break :blk lib;
49-
} else b.addStaticLibrary(.{
50-
.name = "zmesh",
51-
.target = target,
52-
.optimize = optimize,
53-
});
5451
b.installArtifact(zmesh_lib);
5552

5653
zmesh_lib.linkLibC();
@@ -93,9 +90,7 @@ pub fn build(b: *std.Build) void {
9390

9491
const tests = b.addTest(.{
9592
.name = "zmesh-tests",
96-
.root_source_file = b.path("src/root.zig"),
97-
.target = target,
98-
.optimize = optimize,
93+
.root_module = zmesh_module,
9994
});
10095
b.installArtifact(tests);
10196

build.zig.zon

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
.name = .zmesh,
33
.fingerprint = 0xb56bea39e6c0eda0,
44
.version = "0.11.0-dev",
5-
.minimum_zig_version = "0.14.0",
5+
.minimum_zig_version = "0.15.1",
66
.paths = .{
77
"build.zig",
88
"build.zig.zon",

src/Shape.zig

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -458,20 +458,22 @@ test "zmesh.invert" {
458458
test "zmesh.custom" {
459459
const zmesh = @import("root.zig");
460460

461-
zmesh.init(std.testing.allocator);
461+
const allocator = std.testing.allocator;
462+
463+
zmesh.init(allocator);
462464
defer zmesh.deinit();
463465

464-
var positions = std.ArrayList([3]f32).init(std.testing.allocator);
465-
defer positions.deinit();
466-
try positions.append(.{ 0.0, 0.0, 0.0 });
467-
try positions.append(.{ 1.0, 0.0, 0.0 });
468-
try positions.append(.{ 1.0, 0.0, 1.0 });
469-
470-
var indices = std.ArrayList(IndexType).init(std.testing.allocator);
471-
defer indices.deinit();
472-
try indices.append(0);
473-
try indices.append(1);
474-
try indices.append(2);
466+
var positions: std.ArrayList([3]f32) = .{};
467+
defer positions.deinit(allocator);
468+
try positions.append(allocator, .{ 0.0, 0.0, 0.0 });
469+
try positions.append(allocator, .{ 1.0, 0.0, 0.0 });
470+
try positions.append(allocator, .{ 1.0, 0.0, 1.0 });
471+
472+
var indices: std.ArrayList(IndexType) = .{};
473+
defer indices.deinit(allocator);
474+
try indices.append(allocator, 0);
475+
try indices.append(allocator, 1);
476+
try indices.append(allocator, 2);
475477

476478
var shape = Shape.init(indices, positions, null, null);
477479
defer shape.deinit();

src/io.zig

Lines changed: 1 addition & 135 deletions
Original file line numberDiff line numberDiff line change
@@ -1,145 +1,11 @@
11
const std = @import("std");
22
const assert = std.debug.assert;
33

4-
const mem = @import("memory.zig");
5-
64
/// Deprecated. Use `zmesh.io.zcgltf.parseAndLoadFile` instead.
75
pub const parseAndLoadFile = zcgltf.parseAndLoadFile;
86
/// Deprecated. Use `zmesh.io.zcgltf.freeData` instead.
97
pub const freeData = zcgltf.freeData;
108
/// Deprecated. Use `zmesh.io.zcgltf.appendMeshPrimitive` instead.
119
pub const appendMeshPrimitive = zcgltf.appendMeshPrimitive;
1210

13-
pub const zcgltf = struct {
14-
const bindings = @import("zcgltf.zig");
15-
const Data = bindings.Data;
16-
17-
pub usingnamespace bindings;
18-
19-
pub fn parseAndLoadFile(pathname: [:0]const u8) bindings.Error!*Data {
20-
const options = bindings.Options{
21-
.memory = .{
22-
.alloc_func = mem.zmeshAllocUser,
23-
.free_func = mem.zmeshFreeUser,
24-
},
25-
};
26-
27-
const data = try bindings.parseFile(options, pathname);
28-
errdefer bindings.free(data);
29-
30-
try bindings.loadBuffers(options, data, pathname);
31-
32-
return data;
33-
}
34-
35-
pub fn freeData(data: *Data) void {
36-
bindings.free(data);
37-
}
38-
39-
pub fn appendMeshPrimitive(
40-
data: *Data,
41-
mesh_index: u32,
42-
prim_index: u32,
43-
indices: *std.ArrayList(u32),
44-
positions: *std.ArrayList([3]f32),
45-
normals: ?*std.ArrayList([3]f32),
46-
texcoords0: ?*std.ArrayList([2]f32),
47-
tangents: ?*std.ArrayList([4]f32),
48-
) !void {
49-
assert(mesh_index < data.meshes_count);
50-
assert(prim_index < data.meshes.?[mesh_index].primitives_count);
51-
52-
const mesh = &data.meshes.?[mesh_index];
53-
const prim = &mesh.primitives[prim_index];
54-
55-
const num_vertices: u32 = @as(u32, @intCast(prim.attributes[0].data.count));
56-
const num_indices: u32 = @as(u32, @intCast(prim.indices.?.count));
57-
58-
// Indices.
59-
{
60-
try indices.ensureTotalCapacity(indices.items.len + num_indices);
61-
62-
const accessor = prim.indices.?;
63-
const buffer_view = accessor.buffer_view.?;
64-
65-
assert(accessor.stride == buffer_view.stride or buffer_view.stride == 0);
66-
assert(buffer_view.buffer.data != null);
67-
68-
const data_addr = @as([*]const u8, @ptrCast(buffer_view.buffer.data)) +
69-
accessor.offset + buffer_view.offset;
70-
71-
if (accessor.stride == 1) {
72-
if (accessor.component_type != .r_8u) {
73-
return error.InvalidIndicesAccessorComponentType;
74-
}
75-
const src = @as([*]const u8, @ptrCast(data_addr));
76-
var i: u32 = 0;
77-
while (i < num_indices) : (i += 1) {
78-
indices.appendAssumeCapacity(src[i]);
79-
}
80-
} else if (accessor.stride == 2) {
81-
if (accessor.component_type != .r_16u) {
82-
return error.InvalidIndicesAccessorComponentType;
83-
}
84-
const src = @as([*]const u16, @ptrCast(@alignCast(data_addr)));
85-
var i: u32 = 0;
86-
while (i < num_indices) : (i += 1) {
87-
indices.appendAssumeCapacity(src[i]);
88-
}
89-
} else if (accessor.stride == 4) {
90-
if (accessor.component_type != .r_32u) {
91-
return error.InvalidIndicesAccessorComponentType;
92-
}
93-
const src = @as([*]const u32, @ptrCast(@alignCast(data_addr)));
94-
var i: u32 = 0;
95-
while (i < num_indices) : (i += 1) {
96-
indices.appendAssumeCapacity(src[i]);
97-
}
98-
} else {
99-
return error.InvalidIndicesAccessorStride;
100-
}
101-
}
102-
103-
// Attributes.
104-
{
105-
const attributes = prim.attributes[0..prim.attributes_count];
106-
for (attributes) |attrib| {
107-
const accessor = attrib.data;
108-
assert(accessor.component_type == .r_32f);
109-
110-
const buffer_view = accessor.buffer_view.?;
111-
assert(buffer_view.buffer.data != null);
112-
113-
assert(accessor.stride == buffer_view.stride or buffer_view.stride == 0);
114-
assert(accessor.stride * accessor.count == buffer_view.size);
115-
116-
const data_addr = @as([*]const u8, @ptrCast(buffer_view.buffer.data)) +
117-
accessor.offset + buffer_view.offset;
118-
119-
if (attrib.type == .position) {
120-
assert(accessor.type == .vec3);
121-
const slice = @as([*]const [3]f32, @ptrCast(@alignCast(data_addr)))[0..num_vertices];
122-
try positions.appendSlice(slice);
123-
} else if (attrib.type == .normal) {
124-
if (normals) |n| {
125-
assert(accessor.type == .vec3);
126-
const slice = @as([*]const [3]f32, @ptrCast(@alignCast(data_addr)))[0..num_vertices];
127-
try n.appendSlice(slice);
128-
}
129-
} else if (attrib.type == .texcoord) {
130-
if (texcoords0) |tc| {
131-
assert(accessor.type == .vec2);
132-
const slice = @as([*]const [2]f32, @ptrCast(@alignCast(data_addr)))[0..num_vertices];
133-
try tc.appendSlice(slice);
134-
}
135-
} else if (attrib.type == .tangent) {
136-
if (tangents) |tan| {
137-
assert(accessor.type == .vec4);
138-
const slice = @as([*]const [4]f32, @ptrCast(@alignCast(data_addr)))[0..num_vertices];
139-
try tan.appendSlice(slice);
140-
}
141-
}
142-
}
143-
}
144-
}
145-
};
11+
pub const zcgltf = @import("zcgltf.zig");

0 commit comments

Comments
 (0)