Skip to content

add tracy #2

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 26 additions & 0 deletions build.zig
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,14 @@ pub fn build(b: *std.Build) void {
.root_module = lib_mod,
});

// Get the Tracy dependency
const tracy = b.dependency("tracy", .{
.target = target,
.optimize = optimize,
// ...
});
lib.root_module.addImport("tracy", tracy.module("tracy"));

// This declares intent for the library to be installed into the standard
// location when the user invokes the "install" step (the default step when
// running `zig build`).
Expand All @@ -65,6 +73,24 @@ pub fn build(b: *std.Build) void {
.root_module = exe_mod,
});

const tracy_enabled = b.option(
bool,
"tracy",
"Build with Tracy support.",
) orelse false;

// Make Tracy available as an import
exe.root_module.addImport("tracy", tracy.module("tracy"));

// Pick an implementation based on the build flags.
// Don't build both, we don't want to link with Tracy at all unless we intend to enable it.
if (tracy_enabled) {
// The user asked to enable Tracy, use the real implementation
exe.root_module.addImport("tracy_impl", tracy.module("tracy_impl_enabled"));
} else {
// The user asked to disable Tracy, use the dummy implementation
exe.root_module.addImport("tracy_impl", tracy.module("tracy_impl_disabled"));
}
// This declares intent for the executable to be installed into the
// standard location when the user invokes the "install" step (the default
// step when running `zig build`).
Expand Down
42 changes: 4 additions & 38 deletions build.zig.zon
Original file line number Diff line number Diff line change
Expand Up @@ -36,45 +36,11 @@
// Once all dependencies are fetched, `zig build` no longer requires
// internet connectivity.
.dependencies = .{
// See `zig fetch --save <url>` for a command-line interface for adding dependencies.
//.example = .{
// // When updating this field to a new URL, be sure to delete the corresponding
// // `hash`, otherwise you are communicating that you expect to find the old hash at
// // the new URL. If the contents of a URL change this will result in a hash mismatch
// // which will prevent zig from using it.
// .url = "https://example.com/foo.tar.gz",
//
// // This is computed from the file contents of the directory of files that is
// // obtained after fetching `url` and applying the inclusion rules given by
// // `paths`.
// //
// // This field is the source of truth; packages do not come from a `url`; they
// // come from a `hash`. `url` is just one of many possible mirrors for how to
// // obtain a package matching this `hash`.
// //
// // Uses the [multihash](https://multiformats.io/multihash/) format.
// .hash = "...",
//
// // When this is provided, the package is found in a directory relative to the
// // build root. In this case the package's hash is irrelevant and therefore not
// // computed. This field and `url` are mutually exclusive.
// .path = "foo",
//
// // When this is set to `true`, a package is declared to be lazily
// // fetched. This makes the dependency only get fetched if it is
// // actually used.
// .lazy = false,
//},
.tracy = .{
.url = "git+https://github.com/Games-by-Mason/tracy_zig#0cad4646be2791982f692a7a47716ec92b62eb2e",
.hash = "tracy-0.0.0-R7l8BdlCAQBFp6uysnan3Alaxap65HBF1mzGdabdKe8z",
},
},

// Specifies the set of files and directories that are included in this package.
// Only files and directories listed here are included in the `hash` that
// is computed for this package. Only files listed here will remain on disk
// when using the zig package manager. As a rule of thumb, one should list
// files required for compilation plus any license(s).
// Paths are relative to the build root. Use the empty string (`""`) to refer to
// the build root itself.
// A directory listed here means that all files within, recursively, are included.
.paths = .{
"build.zig",
"build.zig.zon",
Expand Down
49 changes: 47 additions & 2 deletions src/frame/frame.zig
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ const std = @import("std");
const rice = @import("../rice.zig");
const util = @import("../util.zig");
const StreamInfo = @import("../metadata/block.zig").StreamInfo;
const tracy = @import("tracy");

const crc8 = std.hash.crc.Crc(u8, .{
.polynomial = 0x07,
.initial = 0x00,
Expand Down Expand Up @@ -129,6 +131,12 @@ pub const Frame = struct {
footer: u16,

pub fn parseFrame(reader: std.io.AnyReader, alloc: std.mem.Allocator, stream_info: ?StreamInfo) !Frame {
// const zone = tracy.Zone.begin(.{
// .name = "Parse Frame",
// .src = @src(),
// .color = .blue,
// });
// defer zone.end();
var frame: Frame = undefined;

var hasher = crc16.init();
Expand Down Expand Up @@ -289,6 +297,13 @@ pub fn ReaderToCRCWriter(comptime T: type) type {

// fn readBitsNoEof
pub fn readBitsNoEof(self: *ReaderToCRCWriter(T), comptime I: type, num: u16) !I {
const zone = tracy.Zone.begin(.{
.name = std.fmt.comptimePrint("readBits->{s}", .{@typeName(I)}),
.src = @src(),
.color = .blue,
});
defer zone.end();

const readed = try self.br.readBitsNoEof(I, num);
try self.bw.writeBits(readed, num);

Expand All @@ -314,6 +329,12 @@ pub const FrameHeader = struct {
crc: u8,

pub fn parseFrameHeader(reader: std.io.AnyReader) !FrameHeader {
// const zone = tracy.Zone.begin(.{
// .name = "Parse Frame Header",
// .src = @src(),
// .color = .blue,
// });
// defer zone.end();
var hasher = crc8.init();
const crc_writer = CrcWriter(crc8){ .crc_obj = &hasher };

Expand Down Expand Up @@ -408,6 +429,13 @@ pub const SubFrame = struct {
subblock: []i64,

pub fn parseSubframe(br: anytype, alloc: std.mem.Allocator, frame: FrameHeader, stream_info: ?StreamInfo, channel_num: u3) !SubFrame {
// const zone = tracy.Zone.begin(.{
// .name = "Parse SUB Frame",
// .src = @src(),
// .color = .green,
// });

// defer zone.end();
var subframe: SubFrame = undefined;
// std.debug.print("BR start: {}\n", .{br});

Expand Down Expand Up @@ -457,6 +485,12 @@ pub const SubFrame = struct {

// const verbatim_int_type = std.meta.Int(.signed, real_bit_depth);
const verbatim_int_type = i64;

const subblock_zone = tracy.Zone.begin(.{
.name = "Subblock Parse",
.src = @src(),
.color = .pink,
});
subframe.subblock = switch (subframe.header) {
.constant => blk: {
const buf = try alloc.alloc(verbatim_int_type, frame.block_size);
Expand Down Expand Up @@ -494,6 +528,11 @@ pub const SubFrame = struct {

// std.debug.print("first partition: {}\n", .{current_partition});

// const partition_zone = tracy.Zone.begin(.{
// .name = "Parse partition (fixed)",
// .src = @src(),
// .color = .white,
// });
switch (order) {
0 => {
// read remaining samples
Expand Down Expand Up @@ -545,7 +584,7 @@ pub const SubFrame = struct {
return error.forbidden_fixed_predictor_order;
},
}

// partition_zone.end();
// std.debug.print("buf: {d}\n", .{buf});

break :blk buf;
Expand Down Expand Up @@ -577,6 +616,11 @@ pub const SubFrame = struct {

var current_partition = try rice.Partition.readPartition(br, coded_residual);

const partition_zone = tracy.Zone.begin(.{
.name = "Parse partition",
.src = @src(),
.color = .white,
});
for (order..buf.len) |i| {
if ((i != 0) and i % number_of_samples_in_each_partition == 0) {
current_partition = try rice.Partition.readPartition(br, coded_residual);
Expand All @@ -588,11 +632,12 @@ pub const SubFrame = struct {

buf[i] = ((predicted >> @intCast(prediction_right_shift)) + try current_partition.readNextResidual(br)) << wasted;
}
partition_zone.end();

break :blk buf;
},
};

subblock_zone.end();
// std.debug.print("Created: {d}\n", .{subframe.subblock});

return subframe;
Expand Down
54 changes: 41 additions & 13 deletions src/main.zig
Original file line number Diff line number Diff line change
@@ -1,14 +1,41 @@
const std = @import("std");
const lib = @import("flac_decoder_lib");
const builtin = @import("builtin");

// If `tracy_impl` is not set in your root file, Tracy integration will be disabled.
pub const tracy_impl = @import("tracy_impl");

// You can optionally configure Tracy by setting `tracy_options` in your root file.
pub const tracy = @import("tracy");
pub const tracy_options: tracy.Options = .{
.on_demand = false,
.no_broadcast = false,
.only_localhost = false,
.only_ipv4 = false,
.delayed_init = false,
.manual_lifetime = false,
.verbose = false,
.data_port = null,
.broadcast_port = null,
.default_callstack_depth = 20,
};

pub const Signature = extern struct {
sig: [4]u8,
};

const allocator = std.heap.smp_allocator;
var tracy_allocator = tracy.Allocator{ .parent = std.heap.smp_allocator };

pub fn main() !void {
const file = try std.fs.cwd().openFile("test/test.flac", .{});
var allocator = tracy_allocator.allocator();

const zone = tracy.Zone.begin(.{
.name = "Main",
.src = @src(),
.color = .tomato,
});
defer zone.end();
const file = try std.fs.cwd().openFile("test/06 Honey Bee.flac", .{});
var breader = std.io.bufferedReader(file.reader());
const file_reader = breader.reader();

Expand Down Expand Up @@ -152,19 +179,20 @@ pub fn main() !void {
error.EndOfStream => null,
else => |er| return er,
}) |x| {
_ = x;
// std.debug.print("Channel: {}\n", .{x.header.channel});
// std.debug.print("SUBFRAMES: {any}\n", .{x.sub_frames});
for (0..x.header.block_size) |sample| {
for (x.sub_frames) |subframe| {
try stdout.writeInt(
i16,
@truncate(subframe.subblock[sample]),

std.builtin.Endian.little,
);
}
}
_ = frame_arena.reset(.retain_capacity);
// for (0..x.header.block_size) |sample| {
// for (x.sub_frames) |subframe| {
// try stdout.writeInt(
// i16,
// @truncate(subframe.subblock[sample]),

// std.builtin.Endian.little,
// );
// }
// }
// _ = frame_arena.reset(.retain_capacity);
}
frame_arena.deinit();

Expand Down
17 changes: 16 additions & 1 deletion src/rice.zig
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
const std = @import("std");
const tracy = @import("tracy");
const frame = @import("frame/frame.zig");
const util = @import("util.zig");

pub const ParameterSize = enum {
@"4-bits",
@"5-bits",
Expand Down Expand Up @@ -37,6 +39,11 @@ pub const Partition = struct {
parameter: u5,

pub fn readPartition(br: anytype, residual: CodedResidual) !Partition {
const partition_zone = tracy.Zone.begin(.{
.name = "READ partition",
.src = @src(),
.color = .white,
});
const param: u5 = switch (residual.parameter_size) {
.@"4-bits" => try br.readBitsNoEof(u5, 4),
.@"5-bits" => try br.readBitsNoEof(u5, 5),
Expand All @@ -47,13 +54,21 @@ pub const Partition = struct {
.@"5-bits" => param == 0b11111,
};

return .{
const ret = Partition{
.parameter = if (escape) try br.readBitsNoEof(u5, 5) else param,
.escaped = escape,
};
partition_zone.end();
return ret;
}

pub fn readNextResidual(partition: Partition, br: anytype) !i32 {
const zone = tracy.Zone.begin(.{
.name = "readNextResidual",
.src = @src(),
.color = .green,
});
defer zone.end();
if (partition.escaped) {
if (partition.parameter == 0) {
return 0;
Expand Down
2 changes: 0 additions & 2 deletions src/root.zig
Original file line number Diff line number Diff line change
@@ -1,4 +1,2 @@
const std = @import("std");

pub const metadata = @import("metadata/metadata.zig");
pub const frame = @import("frame/frame.zig");