Skip to content
Merged
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
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ jobs:
uses: actions/checkout@v4

- name: Setup Zig
uses: mlugg/setup-zig@v1
uses: mlugg/setup-zig@v2
with:
version: master

Expand Down
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
zig-out/
zig-pkg/
.zig-cache/
27 changes: 10 additions & 17 deletions build.zig
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
const std = @import("std");
const addBench = @import("zubench").addBench;

pub fn build(b: *std.Build) void {
const target = b.standardTargetOptions(.{});
const mode = b.standardOptimizeOption(.{});

const zubench = b.dependency("zubench", .{}).module("zubench");
const strided_arrays_pkg = b.dependency("strided-arrays", .{});

const strided_arrays = strided_arrays_pkg.module("strided-arrays");
Expand All @@ -21,9 +19,11 @@ pub fn build(b: *std.Build) void {

const exe = b.addExecutable(.{
.name = "zig-wfc",
.root_source_file = b.path("src/main.zig"),
.target = target,
.optimize = mode,
.root_module = b.createModule(.{
.root_source_file = b.path("src/main.zig"),
.target = target,
.optimize = mode,
}),
});
exe.root_module.addImport(strided_arrays_dep.name, strided_arrays_dep.module);
b.installArtifact(exe);
Expand All @@ -38,23 +38,16 @@ pub fn build(b: *std.Build) void {
run_step.dependOn(&run_cmd.step);

const wfc_tests = b.addTest(.{
.root_source_file = b.path("src/wfc.zig"),
.target = target,
.optimize = mode,
.root_module = b.createModule(.{
.root_source_file = b.path("src/wfc.zig"),
.target = target,
.optimize = mode,
}),
});
wfc_tests.root_module.addImport("zubench", zubench);
wfc_tests.root_module.addImport(strided_arrays_dep.name, strided_arrays_dep.module);

const wfc_tests_run = b.addRunArtifact(wfc_tests);

const test_step = b.step("test", "Run unit tests");
test_step.dependOn(&wfc_tests_run.step);

const bench_step = b.step("bench", "Run the benchmarks");

inline for (.{ .ReleaseSafe, .ReleaseFast, .ReleaseSmall }) |b_mode| {
const bench_exe = addBench(b, "src/core.zig", target, b_mode, &.{strided_arrays_dep});
const cmd = b.addRunArtifact(bench_exe);
bench_step.dependOn(&cmd.step);
}
}
17 changes: 10 additions & 7 deletions build.zig.zon
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,18 @@
.name = .zig_wfc,
.fingerprint = 0xf451383668953641,
.version = "0.0.0",
.paths = .{ "LICENSE", "build.zig", "build.zig.zon", "src", "README.md" },
.paths = .{
"LICENSE",
"build.zig",
"build.zig.zon",
"src",
"README.md",
},
.minimum_zig_version = "0.16.0",
.dependencies = .{
.@"strided-arrays" = .{
.url = "https://github.com/dweiller/zig-strided-arrays/archive/b32e31f767c294a218ff857ac9b6068629e775b7.tar.gz",
.hash = "strided_array-0.0.0-bRJh7EvCAADimJNom3qbzLC3dsiFvAiGRk50x-HoV4KR",
},
.zubench = .{
.url = "https://github.com/dweiller/zubench/archive/53140a07a6ee27e28549c46209d900ba92f61ceb.tar.gz",
.hash = "zubench-0.0.0-kIvtMziVAABBkFpJIF0hfbKvx04ZEE9P_G9Xu_xQvb7n",
.url = "git+https://github.com/dweiller/zig-strided-arrays.git#f0151f11c121099ee373ea3d485f835ae6abf74d",
.hash = "strided_array-0.0.0-bRJh7BXCAAAplUBK57yzs5eNRkqlan0Dw1ROcu348Sze",
},
},
}
84 changes: 7 additions & 77 deletions src/core.zig
Original file line number Diff line number Diff line change
Expand Up @@ -371,7 +371,7 @@ const Removal = struct {
tile_index: TileIndex,
coord: Coord,
};
const RemovalStack = std.ArrayList(Removal);
const RemovalStack = std.array_list.Managed(Removal);

const EntropyCoord = struct {
const Self = @This();
Expand Down Expand Up @@ -400,7 +400,7 @@ const EntropyHeap = struct {
std.debug.assert(heap_buffer.len <= heap_context.len);
self.fba = std.heap.FixedBufferAllocator.init(std.mem.sliceAsBytes(heap_buffer));
self.fba.end_index = self.fba.buffer.len;
self.heap = Heap.init(self.fba.allocator(), heap_context);
self.heap = Heap.initContext(heap_context);
self.heap.items = heap_buffer[0..0];
self.heap.cap = heap_buffer.len;
}
Expand All @@ -421,7 +421,7 @@ const EntropyHeap = struct {
.entropy = entropy,
.coord = item_ind.coord,
};
self.heap.add(item_ind.index) catch {
self.heap.push(self.fba.allocator(), item_ind.index) catch {
std.debug.panic(
"ran out of memory adding entropy coord {d}\nheap has size {d}",
.{ item_ind.index, self.heap.capacity() },
Expand Down Expand Up @@ -465,7 +465,7 @@ pub const CoreState = struct {
random: std.Random,

pub fn chooseCellToCollapse(self: *Self) ?Coord {
const entropy_coord_idx = self.entropy_heap.heap.removeOrNull() orelse return null;
const entropy_coord_idx = self.entropy_heap.heap.pop() orelse return null;
const entropy_coord = self.entropy_heap.heap.context[entropy_coord_idx];
return entropy_coord.coord;
}
Expand Down Expand Up @@ -538,9 +538,10 @@ pub const CoreState = struct {
index,
) orelse
std.debug.panic("could not find tile index {d} in entropy heap", .{index});
_ = self.entropy_heap.heap.removeIndex(remove_index);
_ = self.entropy_heap.heap.popIndex(remove_index);
self.entropy_heap.heap.context[index].entropy = new_entropy;
self.entropy_heap.heap.add(index) catch unreachable; //we just removed one so add() can't fail
// we just removed one so add() can't fail to push
self.entropy_heap.heap.push(self.entropy_heap.fba.allocator(), index) catch unreachable;
}
}

Expand Down Expand Up @@ -733,74 +734,3 @@ pub fn tile(
test {
std.testing.refAllDecls(@This());
}

const bench_ns = struct {
var bench_gpa = std.heap.GeneralPurposeAllocator(.{}){};
const bench_allocator = bench_gpa.allocator();

const bench_tile_count = 4;

var bench_edges = edges: {
var adj_0 = [1]TileSet{TileSet.initEmpty()} ** 4;
adj_0[0].set(0);
adj_0[0].set(2);
adj_0[1].set(0);
adj_0[1].set(1);
adj_0[2].set(0);
adj_0[2].set(2);
adj_0[3].set(0);
adj_0[3].set(1);
var adj_1 = [1]TileSet{TileSet.initEmpty()} ** 4;
adj_1[0].set(1);
adj_1[0].set(3);
adj_1[1].set(0);
adj_1[2].set(1);
adj_1[2].set(3);
adj_1[3].set(0);
var adj_2 = [1]TileSet{TileSet.initEmpty()} ** 4;
adj_2[0].set(0);
adj_2[1].set(2);
adj_2[1].set(3);
adj_2[2].set(0);
adj_2[3].set(2);
adj_2[3].set(3);
var adj_3 = [1]TileSet{TileSet.initEmpty()} ** 4;
adj_3[0].set(1);
adj_3[1].set(2);
adj_3[2].set(1);
adj_3[3].set(2);
break :edges [bench_tile_count][4]TileSet{
adj_0,
adj_1,
adj_2,
adj_3,
};
};

var weights = [_]Weight{ 1, 1, 1, 1 };

pub const benchmarks = benchmarks: {
const adjacencies: Adjacencies = .{ .allowed_edges = bench_edges[0..] };

const input = GenInput{
.seed = 0,
.tile_count = bench_tile_count,
.adjacency_rules = adjacencies,
.weights = &weights,
};

const output_shape = TileGrid.Indices{ 64, 64 };
const args = std.meta.ArgsTuple(@TypeOf(generateAlloc)){
bench_allocator,
bench_allocator,
input,
output_shape,
1,
};
break :benchmarks .{
.@"generateAlloc() 64x64" = @import("zubench").Spec(generateAlloc){ .args = args, .max_samples = 500 },
};
};
};

pub const benchmarks = bench_ns.benchmarks;
Loading
Loading