Skip to content

Commit

Permalink
Merge pull request #3 from SinclaM/zig-0.12.0
Browse files Browse the repository at this point in the history
  • Loading branch information
SinclaM committed Jan 1, 2024
2 parents 5bc18da + 9a9aeda commit 82fc927
Show file tree
Hide file tree
Showing 55 changed files with 486 additions and 356,098 deletions.
12 changes: 10 additions & 2 deletions .github/workflows/deploy.yml
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,21 @@ jobs:
- name: Install Zig 🔧
uses: korandoru/setup-zig@v1
with:
zig-version: 0.11.0
zig-version: master

- name: Install Emscripten 🔧
run: |
curl -O https://codeload.github.com/emscripten-core/emsdk/tar.gz/refs/tags/3.1.51
tar xzf 3.1.51
cd emsdk-3.1.51
./emsdk install latest
./emsdk activate latest
- name: Run tests 📊
run: zig build test

- name: Build 🏗️
run: zig build -Dtarget=wasm32-freestanding -Doptimize=ReleaseFast -Dcpu=generic+bulk_memory+atomics+mutable_globals
run: zig build --sysroot emsdk-3.1.51/upstream/emscripten -Dtarget=wasm32-emscripten -Doptimize=ReleaseFast && sed -i'' -e 's/_emscripten_return_address,/() => {},/g' www/ray-tracer-challenge.js

- name: Deploy to GH Pages 🚀
uses: peaceiris/actions-gh-pages@v3
Expand Down
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,4 @@
/www/*.wasm
/www/scenes
/www/data
/www/ray-tracer-challenge.*
99 changes: 76 additions & 23 deletions build.zig
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
const std = @import("std");
const builtin = @import("builtin");

// Although this function looks imperative, note that its job is to
// declaratively construct a build graph that will be executed by an external
// runner.
pub fn build(b: *std.Build) void {
pub fn build(b: *std.Build) !void {
// Standard target options allows the person running `zig build` to choose
// what target to build for. Here we do not override the defaults, which
// means any target is allowed, and the default is native. Other options
Expand All @@ -15,22 +16,74 @@ pub fn build(b: *std.Build) void {
// set a preferred release mode, allowing the user to decide how to optimize.
const optimize = b.standardOptimizeOption(.{});

const zigimg = b.dependency("zigimg", .{
.target = target,
.optimize = optimize,
});

if (target.cpu_arch) |arch| {
if (arch == std.Target.Cpu.Arch.wasm32) {
const lib = b.addSharedLibrary(.{
.name = "ray-tracer-challenge",
const lib = b.addStaticLibrary(.{
.name = "lib",
.root_source_file = .{ .path = "src/lib.zig" },
.target = target,
.target = .{
.cpu_arch = .wasm32,
.cpu_model = .{ .explicit = &std.Target.wasm.cpu.mvp },
.cpu_features_add = std.Target.wasm.featureSet(&.{ .atomics, .bulk_memory, .simd128 }),
.os_tag = .emscripten,
},
.optimize = optimize,
.link_libc = true
});
lib.rdynamic = true;
lib.shared_memory = true;
lib.single_threaded = false;
lib.bundle_compiler_rt = true;

lib.addModule("zigimg", zigimg.module("zigimg"));

if (b.sysroot == null) {
@panic("pass '--sysroot \"[path to emsdk]/upstream/emscripten\"'");
}

const install_lib = b.addInstallArtifact(
lib, .{ .dest_dir = .{ .override = .{ .custom = "../www/" } } }
const emccExe = switch (builtin.os.tag) {
.windows => "emcc.bat",
else => "emcc",
};
var emcc_run_arg = try b.allocator.alloc(
u8,
b.sysroot.?.len + emccExe.len + 1
);
defer b.allocator.free(emcc_run_arg);

emcc_run_arg = try std.fmt.bufPrint(
emcc_run_arg,
"{s}" ++ std.fs.path.sep_str ++ "{s}",
.{ b.sysroot.?, emccExe }
);
b.getInstallStep().dependOn(&install_lib.step);

var www = std.fs.cwd().openDir("www", .{}) catch @panic("Can't access www!");
const emcc_command = b.addSystemCommand(&[_][]const u8{emcc_run_arg});
emcc_command.addFileArg(lib.getEmittedBin());
emcc_command.step.dependOn(&lib.step);
emcc_command.addArgs(&[_][]const u8{
"-o",
"www" ++ std.fs.path.sep_str ++ "ray-tracer-challenge.js",
"--embed-file",
"data@/",
"--no-entry",
"-pthread",
"-sPTHREAD_POOL_SIZE=navigator.hardwareConcurrency",
"-sINITIAL_MEMORY=167772160",
"-sALLOW_MEMORY_GROWTH",
"-sEXPORTED_FUNCTIONS=_startInitRenderer,_tryFinishInitRenderer,_initRendererIsOk,_initRendererGetPixels,_initRendererGetWidth,_initRendererGetHeight,_initRendererGetErr,_deinitRenderer,_startRender,_tryFinishRender,_rotateCamera,_moveCamera",
"-sEXPORTED_RUNTIME_METHODS=ccall,cwrap",
"-sSTACK_SIZE=10485760", // Increase stack size to 10MB
"-sALLOW_BLOCKING_ON_MAIN_THREAD=1",
"-O3",
//"-sUSE_OFFSET_CONVERTER",
});
b.getInstallStep().dependOn(&emcc_command.step);

var www = try std.fs.cwd().openDir("www", .{});
defer www.close();

www.makeDir("scenes") catch |err| switch (err) {
Expand All @@ -45,25 +98,23 @@ pub fn build(b: *std.Build) void {

{
// Copy all the scene descriptions into www
var scenes_src = std.fs.cwd().openDir("scenes", .{}) catch @panic("Can't access scenes!");
var scenes_src = try std.fs.cwd().openDir("scenes", .{});
defer scenes_src.close();

var scenes_dest = www.openDir("scenes", .{}) catch @panic("Can't access www/scenes!");
var scenes_dest = try www.openDir("scenes", .{});
defer scenes_dest.close();

var iter_scenes = std.fs.cwd().openIterableDir("scenes", .{})
catch @panic("Can't access scenes for iteration!");
var iter_scenes = try std.fs.cwd().openDir("scenes", .{ .iterate = true});
defer iter_scenes.close();

var iter = iter_scenes.iterate();
while (true) {
const entry = iter.next() catch @panic("Can't iterate through scenes!");
const entry = try iter.next();
if (entry == null) {
break;
} else {
switch (entry.?.kind) {
.file => scenes_src.copyFile(entry.?.name, scenes_dest, entry.?.name, .{})
catch @panic("Can't copy scene!"),
.file => try scenes_src.copyFile(entry.?.name, scenes_dest, entry.?.name, .{}),
else => {},
}
}
Expand All @@ -72,25 +123,23 @@ pub fn build(b: *std.Build) void {

{
// Copy all the data files into www
var data_src = std.fs.cwd().openDir("data", .{}) catch @panic("Can't access data!");
var data_src = try std.fs.cwd().openDir("data", .{});
defer data_src.close();

var data_dest = www.openDir("data", .{}) catch @panic("Can't access www/data!");
var data_dest = try www.openDir("data", .{});
defer data_dest.close();

var iter_data = std.fs.cwd().openIterableDir("data", .{})
catch @panic("Can't access data for iteration!");
var iter_data = try std.fs.cwd().openDir("data", .{ .iterate = true });
defer iter_data.close();

var iter = iter_data.iterate();
while (true) {
const entry = iter.next() catch @panic("Can't iterate through data!");
const entry = try iter.next();
if (entry == null) {
break;
} else {
switch (entry.?.kind) {
.file => data_src.copyFile(entry.?.name, data_dest, entry.?.name, .{})
catch @panic("Can't copy data!"),
.file => try data_src.copyFile(entry.?.name, data_dest, entry.?.name, .{}),
else => {},
}
}
Expand All @@ -107,6 +156,8 @@ pub fn build(b: *std.Build) void {
.optimize = optimize,
});

exe.addModule("zigimg", zigimg.module("zigimg"));

// 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 Expand Up @@ -145,6 +196,8 @@ pub fn build(b: *std.Build) void {
.optimize = optimize,
});

unit_tests.addModule("zigimg", zigimg.module("zigimg"));

const run_unit_tests = b.addRunArtifact(unit_tests);

// Similar to creating the run step earlier, this exposes a `test` step to
Expand Down
16 changes: 16 additions & 0 deletions build.zig.zon
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
.{
.name = "ray-tracer-challenge",
.version = "0.0.0",
.dependencies = .{
.zigimg = .{
.url = "https://github.com/zigimg/zigimg/archive/b510787f9908ef704c9292768988a3d4c864d41c.tar.gz",
.hash = "1220e4f559ea449f13383e34b1102e6e1befd6f59a5a9538f823613546b4876e8706",
},
},
.paths = .{
"build.zig",
"build.zig.zon",
"README.md",
"src",
},
}
Binary file added data/earthmap1k.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading

0 comments on commit 82fc927

Please sign in to comment.