aboutsummaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authornsfisis <nsfisis@gmail.com>2023-08-19 14:56:33 +0900
committernsfisis <nsfisis@gmail.com>2023-08-19 14:56:33 +0900
commitb8d5f7cc926c19f87884f8506a925b86c53c99c1 (patch)
tree224381921c1d29221163287089d2b53efc78ce3a
parentaa61d3a19167c7ef21fd2c7dcceb759005e81ea3 (diff)
downloadRayTracingInOneWeekend.zig-b8d5f7cc926c19f87884f8506a925b86c53c99c1.tar.gz
RayTracingInOneWeekend.zig-b8d5f7cc926c19f87884f8506a925b86c53c99c1.tar.zst
RayTracingInOneWeekend.zig-b8d5f7cc926c19f87884f8506a925b86c53c99c1.zip
feat: update zig 0.11.0
-rw-r--r--Makefile2
-rw-r--r--build.zig63
m---------deps/zigimg0
-rw-r--r--src/main.zig22
-rw-r--r--src/rtw/perlin.zig18
-rw-r--r--src/rtw/texture.zig12
6 files changed, 76 insertions, 41 deletions
diff --git a/Makefile b/Makefile
index 58b5adb..9006a27 100644
--- a/Makefile
+++ b/Makefile
@@ -1,4 +1,4 @@
-BUILDFLAGS := -Drelease-fast=true
+BUILDFLAGS := -Doptimize=ReleaseFast
.PHONY: all
all: fmt
diff --git a/build.zig b/build.zig
index 6dbb3b1..08235d1 100644
--- a/build.zig
+++ b/build.zig
@@ -1,37 +1,72 @@
const std = @import("std");
-pub fn build(b: *std.build.Builder) void {
+// 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 {
// 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
// for restricting supported target set are available.
const target = b.standardTargetOptions(.{});
- // Standard release options allow the person running `zig build` to select
- // between Debug, ReleaseSafe, ReleaseFast, and ReleaseSmall.
- const mode = b.standardReleaseOptions();
+ // Standard optimization options allow the person running `zig build` to select
+ // between Debug, ReleaseSafe, ReleaseFast, and ReleaseSmall. Here we do not
+ // set a preferred release mode, allowing the user to decide how to optimize.
+ const optimize = b.standardOptimizeOption(.{});
- const exe = b.addExecutable("RayTracingInOneWeekend", "src/main.zig");
+ const exe = b.addExecutable(.{
+ .name = "RayTracingInOneWeekend",
+ // In this case the main source file is merely a path, however, in more
+ // complicated build scripts, this could be a generated file.
+ .root_source_file = .{ .path = "src/main.zig" },
+ .target = target,
+ .optimize = optimize,
+ });
- exe.addPackagePath("zigimg", "deps/zigimg/zigimg.zig");
+ exe.addAnonymousModule("zigimg", .{ .source_file = .{ .path = "deps/zigimg/zigimg.zig" } });
- exe.setTarget(target);
- exe.setBuildMode(mode);
- exe.install();
+ // 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`).
+ b.installArtifact(exe);
- const run_cmd = exe.run();
+ // This *creates* a Run step in the build graph, to be executed when another
+ // step is evaluated that depends on it. The next line below will establish
+ // such a dependency.
+ const run_cmd = b.addRunArtifact(exe);
+
+ // By making the run step depend on the install step, it will be run from the
+ // installation directory rather than directly from within the cache directory.
+ // This is not necessary, however, if the application depends on other installed
+ // files, this ensures they will be present and in the expected location.
run_cmd.step.dependOn(b.getInstallStep());
+
+ // This allows the user to pass arguments to the application in the build
+ // command itself, like this: `zig build run -- arg1 arg2 etc`
if (b.args) |args| {
run_cmd.addArgs(args);
}
+ // This creates a build step. It will be visible in the `zig build --help` menu,
+ // and can be selected like this: `zig build run`
+ // This will evaluate the `run` step rather than the default, which is "install".
const run_step = b.step("run", "Run the app");
run_step.dependOn(&run_cmd.step);
- const exe_tests = b.addTest("src/main.zig");
- exe_tests.setTarget(target);
- exe_tests.setBuildMode(mode);
+ // Creates a step for unit testing. This only builds the test executable
+ // but does not run it.
+ const unit_tests = b.addTest(.{
+ .root_source_file = .{ .path = "src/main.zig" },
+ .target = target,
+ .optimize = optimize,
+ });
+
+ const run_unit_tests = b.addRunArtifact(unit_tests);
+ // Similar to creating the run step earlier, this exposes a `test` step to
+ // the `zig build --help` menu, providing a way for the user to request
+ // running the unit tests.
const test_step = b.step("test", "Run unit tests");
- test_step.dependOn(&exe_tests.step);
+ test_step.dependOn(&run_unit_tests.step);
}
diff --git a/deps/zigimg b/deps/zigimg
-Subproject 64d04a63814b54301182194dadccd1f28c91906
+Subproject 40ddb16fd246174545b7327a12dce7f0889ada7
diff --git a/src/main.zig b/src/main.zig
index ad2273b..fa1f917 100644
--- a/src/main.zig
+++ b/src/main.zig
@@ -137,11 +137,11 @@ fn rayColor(r: Ray, background: Color, world: Hittable, rng: Random, depth: u32)
}
fn writeColor(out: anytype, c: Color, samples_per_pixel: u32) !void {
- const scale = 1.0 / @intToFloat(f64, samples_per_pixel);
+ const scale = 1.0 / @as(f64, @floatFromInt(samples_per_pixel));
try out.print("{} {} {}\n", .{
- @floatToInt(u8, 256.0 * math.clamp(@sqrt(c.x * scale), 0.0, 0.999)),
- @floatToInt(u8, 256.0 * math.clamp(@sqrt(c.y * scale), 0.0, 0.999)),
- @floatToInt(u8, 256.0 * math.clamp(@sqrt(c.z * scale), 0.0, 0.999)),
+ @as(u8, @intFromFloat(256.0 * math.clamp(@sqrt(c.x * scale), 0.0, 0.999))),
+ @as(u8, @intFromFloat(256.0 * math.clamp(@sqrt(c.y * scale), 0.0, 0.999))),
+ @as(u8, @intFromFloat(256.0 * math.clamp(@sqrt(c.z * scale), 0.0, 0.999))),
});
}
@@ -204,9 +204,9 @@ fn generateRandomScene(rng: Random, allocator: anytype) !Hittable {
while (b < 3) : (b += 1) {
const choose_mat = randomReal01(rng);
const center = Point3{
- .x = @intToFloat(f64, a) + 0.9 * randomReal01(rng),
+ .x = @as(f64, @floatFromInt(a)) + 0.9 * randomReal01(rng),
.y = 0.2,
- .z = @intToFloat(f64, b) + 0.9 * randomReal01(rng),
+ .z = @as(f64, @floatFromInt(b)) + 0.9 * randomReal01(rng),
};
if (center.sub(.{ .x = 4, .y = 0.2, .z = 0 }).norm() <= 0.9) {
@@ -309,7 +309,7 @@ fn generateCornellBox(allocator: anytype) !Hittable {
pub fn main() !void {
var gpa = std.heap.GeneralPurposeAllocator(.{}){};
const allocator = gpa.allocator();
- defer debug.assert(!gpa.deinit());
+ defer debug.assert(gpa.deinit() == .ok);
var rng_ = std.rand.DefaultPrng.init(42);
var rng = rng_.random();
@@ -317,7 +317,7 @@ pub fn main() !void {
// Image
var aspect_ratio: f64 = 3.0 / 2.0;
var image_width: u32 = 600;
- var image_height: u32 = @floatToInt(u32, @divTrunc(@intToFloat(f64, image_width), aspect_ratio));
+ var image_height: u32 = @as(u32, @intFromFloat(@divTrunc(@as(f64, @floatFromInt(image_width)), aspect_ratio)));
const max_depth = 50;
var samples_per_pixel: u32 = 50;
@@ -396,7 +396,7 @@ pub fn main() !void {
try stdout.print("P3\n{} {}\n255\n", .{ image_width, image_height });
- var j: i32 = @intCast(i32, image_height) - 1;
+ var j: i32 = @as(i32, @intCast(image_height)) - 1;
while (j >= 0) : (j -= 1) {
std.debug.print("\rScanlines remaining: {} ", .{j});
var i: i32 = 0;
@@ -404,8 +404,8 @@ pub fn main() !void {
var s: u32 = 0;
var pixelColor = rgb(0.0, 0.0, 0.0);
while (s < samples_per_pixel) : (s += 1) {
- const u = (@intToFloat(f64, i) + randomReal01(rng)) / (@intToFloat(f64, image_width) - 1.0);
- const v = (@intToFloat(f64, j) + randomReal01(rng)) / (@intToFloat(f64, image_height) - 1.0);
+ const u = (@as(f64, @floatFromInt(i)) + randomReal01(rng)) / (@as(f64, @floatFromInt(image_width)) - 1.0);
+ const v = (@as(f64, @floatFromInt(j)) + randomReal01(rng)) / (@as(f64, @floatFromInt(image_height)) - 1.0);
const r = camera.getRay(rng, u, v);
pixelColor = pixelColor.add(rayColor(r, background, world, rng, max_depth));
}
diff --git a/src/rtw/perlin.zig b/src/rtw/perlin.zig
index c60bb34..39173cd 100644
--- a/src/rtw/perlin.zig
+++ b/src/rtw/perlin.zig
@@ -53,9 +53,9 @@ pub const Perlin = struct {
const v_ = v * v * (3 - 2 * v);
const w_ = w * w * (3 - 2 * w);
- const i = @floatToInt(i32, @floor(p.x));
- const j = @floatToInt(i32, @floor(p.y));
- const k = @floatToInt(i32, @floor(p.z));
+ const i = @as(i32, @intFromFloat(@floor(p.x)));
+ const j = @as(i32, @intFromFloat(@floor(p.y)));
+ const k = @as(i32, @intFromFloat(@floor(p.z)));
var c: [2][2][2]Vec3 = undefined;
@@ -65,9 +65,9 @@ pub const Perlin = struct {
while (dj < 2) : (dj += 1) {
var dk: usize = 0;
while (dk < 2) : (dk += 1) {
- const ix = @intCast(usize, i + @intCast(i32, di)) & 255;
- const iy = @intCast(usize, j + @intCast(i32, dj)) & 255;
- const iz = @intCast(usize, k + @intCast(i32, dk)) & 255;
+ const ix = @as(usize, @intCast(i + @as(i32, @intCast(di)))) & 255;
+ const iy = @as(usize, @intCast(j + @as(i32, @intCast(dj)))) & 255;
+ const iz = @as(usize, @intCast(k + @as(i32, @intCast(dk)))) & 255;
c[di][dj][dk] = perlin.randomVec.items[
perlin.permX.items[ix] ^ perlin.permY.items[iy] ^ perlin.permZ.items[iz]
];
@@ -109,9 +109,9 @@ pub const Perlin = struct {
while (j < 2) : (j += 1) {
var k: usize = 0;
while (k < 2) : (k += 1) {
- const ti = @intToFloat(f64, i);
- const tj = @intToFloat(f64, j);
- const tk = @intToFloat(f64, k);
+ const ti = @as(f64, @floatFromInt(i));
+ const tj = @as(f64, @floatFromInt(j));
+ const tk = @as(f64, @floatFromInt(k));
const weight = Vec3{ .x = u - ti, .y = v - tj, .z = w - tk };
accum +=
(ti * u + (1.0 - ti) * (1.0 - u)) *
diff --git a/src/rtw/texture.zig b/src/rtw/texture.zig
index aabc18d..4095a9c 100644
--- a/src/rtw/texture.zig
+++ b/src/rtw/texture.zig
@@ -130,18 +130,18 @@ pub const ImageTexture = struct {
// Clamp input texture coordinates to [0, 1] x [1, 0]
const u_ = std.math.clamp(u, 0.0, 1.0);
const v_ = 1.0 - std.math.clamp(v, 0.0, 1.0); // Flip v to image coordinates
- const i = @floatToInt(usize, u_ * @intToFloat(f64, tx.image.width));
- const j = @floatToInt(usize, v_ * @intToFloat(f64, tx.image.height));
+ const i = @as(usize, @intFromFloat(u_ * @as(f64, @floatFromInt(tx.image.width))));
+ const j = @as(usize, @intFromFloat(v_ * @as(f64, @floatFromInt(tx.image.height))));
// Clamp integer mapping, since actual coordinates should be less than 1.0
const i_ = @min(i, tx.image.width - 1);
const j_ = @min(j, tx.image.width - 1);
const color_scale = 1.0 / 255.0;
const pixels = tx.image.pixels.asBytes();
const offset = j_ * tx.image.width * 4 + i_ * 4;
- const r = @intToFloat(f64, pixels[offset + 0]);
- const g = @intToFloat(f64, pixels[offset + 1]);
- const b = @intToFloat(f64, pixels[offset + 2]);
- const a = @intToFloat(f64, pixels[offset + 3]);
+ const r = @as(f64, @floatFromInt(pixels[offset + 0]));
+ const g = @as(f64, @floatFromInt(pixels[offset + 1]));
+ const b = @as(f64, @floatFromInt(pixels[offset + 2]));
+ const a = @as(f64, @floatFromInt(pixels[offset + 3]));
if (a == 0) {
// Ocean
return rgb(0, 0, 1.0);