Skip to content

Commit 5d75bad

Browse files
committed
Make some fields optional
Fixes #4
1 parent 2eb792d commit 5d75bad

File tree

4 files changed

+65
-43
lines changed

4 files changed

+65
-43
lines changed

build.zig

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ pub fn build(b: *Build) void {
3232

3333
// Module
3434
const module = b.addModule("gltf", .{
35-
.root_source_file = b.path("src/main.zig"),
35+
.root_source_file = b.path("src/root.zig"),
3636
.target = target,
3737
.optimize = optimize,
3838
.link_libc = true,
@@ -55,7 +55,7 @@ pub fn build(b: *Build) void {
5555
.name = "benchmark",
5656
.root_source_file = b.path("src/benchmark.zig"),
5757
.target = target,
58-
.optimize = .ReleaseFast,
58+
.optimize = optimize,
5959
});
6060
benchmark.root_module.addImport("gltf", module);
6161
benchmark.root_module.addImport("build_options", build_options_module);

build.zig.zon

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
.dependencies = .{
66
.stb = .{
77
.url = "https://github.com/nothings/stb/archive/f75e8d1cad7d90d72ef7a4661f1b994ef78b4e31.tar.gz",
8-
.hash = "1220c4fe5a4c4ebec402f5cdef08bc264b56fb07f259107d2b01ba8d416d88624b50",
8+
.hash = "N-V-__8AAEcaTgDE_lpMTr7EAvXN7wi8JktW-wfyWRB9KwG6",
99
},
1010
},
1111
.paths = .{

src/benchmark.zig

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ pub fn main() !u8 {
6363

6464
const file = try std.fs.cwd().openFile(asset_path.?, .{});
6565
defer file.close();
66-
const data = try file.readToEndAllocOptions(allocator, 100 * 1024 * 1024, null, @alignOf(u8), 0);
66+
const data = try file.readToEndAllocOptions(allocator, 100 * 1024 * 1024, null, .of(u8), 0);
6767
defer allocator.free(data);
6868

6969
var timer = try std.time.Timer.start();
@@ -78,7 +78,7 @@ pub fn main() !u8 {
7878
timer.reset();
7979
var bm: GLTF.DefaultBufferManager = .empty;
8080
const dirname = std.fs.path.dirname(asset_path.?) orelse "";
81-
bm.cwd = try std.fs.cwd().openDir(dirname, .{});
81+
bm.root_dir = try std.fs.cwd().openDir(dirname, .{});
8282
const asset = try GLTF.parse(allocator, data, &bm, GLTF.DefaultBufferManager.loadUri, .{});
8383
defer asset.deinit();
8484
const took = timer.read();

src/main.zig renamed to src/root.zig

Lines changed: 60 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -159,13 +159,14 @@ const Metadata = struct {
159159
} = null,
160160
} = &.{},
161161
textures: []struct {
162-
sampler: u16,
162+
sampler: ?u16 = null,
163163
source: u16,
164164
} = &.{},
165165
images: []struct {
166166
name: ?[]u8 = null,
167-
mimeType: []u8,
168-
bufferView: u16,
167+
uri: ?[]u8 = null,
168+
mimeType: ?[]u8 = null,
169+
bufferView: ?u16 = null,
169170
} = &.{},
170171
animations: []struct {
171172
name: ?[]u8 = null,
@@ -298,37 +299,37 @@ pub const Options = struct {
298299
};
299300

300301
pub const DefaultBufferManager = struct {
301-
cwd: ?std.fs.Dir,
302+
root_dir: ?std.fs.Dir,
302303

303-
pub const empty: DefaultBufferManager = .{ .cwd = null };
304+
pub const empty: DefaultBufferManager = .{ .root_dir = null };
304305

305306
pub fn loadUri(
306307
bm: *DefaultBufferManager,
307308
allocator: mem.Allocator,
308309
uri: []const u8,
309-
len: u32,
310+
len: ?u32,
310311
) error{LoadingAsset}![]const u8 {
311312
if (std.Uri.parse(uri)) |puri| {
312313
const i = mem.indexOf(u8, puri.path.percent_encoded, "base64,") orelse return error.LoadingAsset;
313314
const base64_data = puri.path.percent_encoded[i + 7 ..];
314315
const base64_len = base64.Decoder.calcSizeForSlice(base64_data) catch return error.LoadingAsset;
315316
const data = allocator.alloc(u8, base64_len) catch return error.LoadingAsset;
316317
base64.Decoder.decode(data, base64_data) catch return error.LoadingAsset;
317-
if (data.len != len) return error.LoadingAsset;
318+
if (len != null and data.len != len.?) return error.LoadingAsset;
318319
return data;
319320
} else |_| {}
320321

321-
const cwd = if (bm.cwd) |cwd| cwd else std.fs.cwd();
322-
const data = cwd.readFileAlloc(allocator, uri, len) catch return error.LoadingAsset;
323-
return data[0..len];
322+
const cwd = if (bm.root_dir) |root_dir| root_dir else std.fs.cwd();
323+
const data = cwd.readFileAlloc(allocator, uri, len orelse std.math.maxInt(usize)) catch return error.LoadingAsset;
324+
return data[0 .. len orelse data.len];
324325
}
325326
};
326327

327328
pub fn parse(
328329
gpa: mem.Allocator,
329330
data: []const u8,
330331
ctx: anytype,
331-
loadUri: *const fn (ctx: @TypeOf(ctx), allocator: mem.Allocator, uri: []const u8, len: u32) error{LoadingAsset}![]const u8,
332+
loadUri: *const fn (ctx: @TypeOf(ctx), allocator: mem.Allocator, uri: []const u8, len: ?u32) error{LoadingAsset}![]const u8,
332333
options: Options,
333334
) Error!GLTF {
334335
var fbs = std.io.fixedBufferStream(data);
@@ -429,30 +430,44 @@ pub fn parse(
429430
if (pbr_metallic_roughness.baseColorTexture) |tex_index| {
430431
const tex = metadata.textures[tex_index.index];
431432
const img = metadata.images[tex.source];
432-
const buffer_view = metadata.bufferViews[img.bufferView];
433-
if (buffer_view.byteStride != null) return error.InvalidAsset;
434-
const buffer = metadata.buffers[buffer_view.buffer];
435-
const buffer_data = if (is_bin)
436-
data[fbs.pos..][0..buffer.byteLength]
437-
else
438-
try loadUri(ctx, allocator, buffer.uri.?, buffer.byteLength);
439-
const ptr = buffer_data[buffer_view.byteOffset..][0..buffer_view.byteLength];
440-
base_color = try loadTexture(ptr);
433+
434+
if (img.uri) |uri| {
435+
const buffer_data = try loadUri(ctx, allocator, uri, null);
436+
base_color = try loadTexture(buffer_data);
437+
} else {
438+
const buffer_view = metadata.bufferViews[img.bufferView.?];
439+
if (buffer_view.byteStride != null) return error.InvalidAsset;
440+
441+
const buffer = metadata.buffers[buffer_view.buffer];
442+
const buffer_data = if (is_bin)
443+
data[fbs.pos..][0..buffer.byteLength]
444+
else
445+
try loadUri(ctx, allocator, buffer.uri.?, buffer.byteLength);
446+
const ptr = buffer_data[buffer_view.byteOffset..][0..buffer_view.byteLength];
447+
base_color = try loadTexture(ptr);
448+
}
441449
}
442450

443451
var metallic_roughness: ?Texture = null;
444452
if (pbr_metallic_roughness.metallicRoughnessTexture) |tex_index| {
445453
const tex = metadata.textures[tex_index.index];
446454
const img = metadata.images[tex.source];
447-
const buffer_view = metadata.bufferViews[img.bufferView];
448-
if (buffer_view.byteStride != null) return error.InvalidAsset;
449-
const buffer = metadata.buffers[buffer_view.buffer];
450-
const buffer_data = if (is_bin)
451-
data[fbs.pos..][0..buffer.byteLength]
452-
else
453-
try loadUri(ctx, allocator, buffer.uri.?, buffer.byteLength);
454-
const ptr = buffer_data[buffer_view.byteOffset..][0..buffer_view.byteLength];
455-
metallic_roughness = try loadTexture(ptr);
455+
456+
if (img.uri) |uri| {
457+
const buffer_data = try loadUri(ctx, allocator, uri, null);
458+
metallic_roughness = try loadTexture(buffer_data);
459+
} else {
460+
const buffer_view = metadata.bufferViews[img.bufferView.?];
461+
if (buffer_view.byteStride != null) return error.InvalidAsset;
462+
463+
const buffer = metadata.buffers[buffer_view.buffer];
464+
const buffer_data = if (is_bin)
465+
data[fbs.pos..][0..buffer.byteLength]
466+
else
467+
try loadUri(ctx, allocator, buffer.uri.?, buffer.byteLength);
468+
const ptr = buffer_data[buffer_view.byteOffset..][0..buffer_view.byteLength];
469+
metallic_roughness = try loadTexture(ptr);
470+
}
456471
}
457472

458473
pbr = .{
@@ -465,15 +480,22 @@ pub fn parse(
465480
if (material.normalTexture) |tex_index| {
466481
const tex = metadata.textures[tex_index.index];
467482
const img = metadata.images[tex.source];
468-
const buffer_view = metadata.bufferViews[img.bufferView];
469-
if (buffer_view.byteStride != null) return error.InvalidAsset;
470-
const buffer = metadata.buffers[buffer_view.buffer];
471-
const buffer_data = if (is_bin)
472-
data[fbs.pos..][0..buffer.byteLength]
473-
else
474-
try loadUri(ctx, allocator, buffer.uri.?, buffer.byteLength);
475-
const ptr = buffer_data[buffer_view.byteOffset..][0..buffer_view.byteLength];
476-
normal = try loadTexture(ptr);
483+
484+
if (img.uri) |uri| {
485+
const buffer_data = try loadUri(ctx, allocator, uri, null);
486+
normal = try loadTexture(buffer_data);
487+
} else {
488+
const buffer_view = metadata.bufferViews[img.bufferView.?];
489+
if (buffer_view.byteStride != null) return error.InvalidAsset;
490+
491+
const buffer = metadata.buffers[buffer_view.buffer];
492+
const buffer_data = if (is_bin)
493+
data[fbs.pos..][0..buffer.byteLength]
494+
else
495+
try loadUri(ctx, allocator, buffer.uri.?, buffer.byteLength);
496+
const ptr = buffer_data[buffer_view.byteOffset..][0..buffer_view.byteLength];
497+
normal = try loadTexture(ptr);
498+
}
477499
}
478500

479501
out_material.* = .{

0 commit comments

Comments
 (0)