tangled
alpha
login
or
join now
altagos.dev
/
rayray
this repo has no description
0
fork
atom
overview
issues
pulls
pipelines
added threaded image renderer + output as png
altagos.dev
2 years ago
99db8021
d38a4f73
+125
-9
5 changed files
expand all
collapse all
unified
split
.gitignore
build.zig
build.zig.zon
src
main.zig
rayray.zig
+1
.gitignore
···
1
/zig-cache
2
/zig-out
0
···
1
/zig-cache
2
/zig-out
3
+
/out
+8
-1
build.zig
···
4
const target = b.standardTargetOptions(.{});
5
const optimize = b.standardOptimizeOption(.{});
6
0
0
0
0
0
7
const rayray = b.addModule("rayray", .{
8
.root_source_file = .{ .path = "src/rayray.zig" },
9
.target = target,
10
.optimize = optimize,
11
});
0
0
12
addDeps(b, rayray);
13
14
const exe = b.addExecutable(.{
···
17
.target = target,
18
.optimize = optimize,
19
});
20
-
21
exe.root_module.addImport("rayray", rayray);
22
23
b.installArtifact(exe);
···
4
const target = b.standardTargetOptions(.{});
5
const optimize = b.standardOptimizeOption(.{});
6
7
+
const enable_spall = b.option(bool, "enable_spall", "Enable spall profiling") orelse false;
8
+
const spall = b.dependency("spall", .{
9
+
.enable = enable_spall,
10
+
});
11
+
12
const rayray = b.addModule("rayray", .{
13
.root_source_file = .{ .path = "src/rayray.zig" },
14
.target = target,
15
.optimize = optimize,
16
});
17
+
rayray.addImport("spall", spall.module("spall"));
18
+
19
addDeps(b, rayray);
20
21
const exe = b.addExecutable(.{
···
24
.target = target,
25
.optimize = optimize,
26
});
27
+
exe.root_module.addImport("spall", spall.module("spall"));
28
exe.root_module.addImport("rayray", rayray);
29
30
b.installArtifact(exe);
+6
-3
build.zig.zon
···
5
.dependencies = .{
6
// See `zig fetch --save <url>` for a command-line interface for adding dependencies.
7
.zigimg = .{
8
-
.url = "https://github.com/zigimg/zigimg/archive/2224f91ea4cf70f2254848657e308761b8cc8c5d.tar.gz",
9
-
// dumy hash, because zigimg does not contain a build.zig. zon
10
-
.hash = "122029f65edf3965d86f7d0741fe141bcc1d68b1f013fa7280bbfda4e87c6affc15f",
0
0
0
11
},
12
13
// libs folder
···
5
.dependencies = .{
6
// See `zig fetch --save <url>` for a command-line interface for adding dependencies.
7
.zigimg = .{
8
+
.url = "https://github.com/rockorager/zigimg/archive/19a49a7e44fb4b1c22341dfbd6566019de742055.tar.gz",
9
+
.hash = "1220ebfa8587cfd644995fc08e218dbb3ebd7344fb8e129ff02bc5a6d52a2325370d",
10
+
},
11
+
.spall = .{
12
+
.url = "https://git.sr.ht/~altagos/zig-spall/archive/7cae52aa2d1a519006e0c90fbd179cf7fcac0c83.tar.gz",
13
+
.hash = "1220753b2f9e7c4f3fb6cdc8fc275cc9073904ea7032a1f5596aa776a857de69f72c",
14
},
15
16
// libs folder
+22
-1
src/main.zig
···
0
0
0
0
1
const Raytracer = @import("rayray").Raytracer;
2
3
pub fn main() !void {
4
-
const raytracer = Raytracer.init();
0
0
0
0
0
0
0
0
0
0
0
0
5
defer raytracer.deinit();
0
0
0
0
0
6
}
···
1
+
const std = @import("std");
2
+
3
+
const spall = @import("spall");
4
+
5
const Raytracer = @import("rayray").Raytracer;
6
7
pub fn main() !void {
8
+
var arena = std.heap.ArenaAllocator.init(std.heap.page_allocator);
9
+
defer arena.deinit();
10
+
const allocator = arena.allocator();
11
+
12
+
try spall.init("./out/trace.spall");
13
+
defer spall.deinit();
14
+
15
+
spall.init_thread();
16
+
defer spall.deinit_thread();
17
+
18
+
const s = spall.trace(@src(), "Main", .{});
19
+
20
+
var raytracer = try Raytracer.init(allocator);
21
defer raytracer.deinit();
22
+
23
+
const img = try raytracer.render();
24
+
25
+
s.end();
26
+
try img.writeToFilePath("./out/out.png", .{ .png = .{} });
27
}
+88
-4
src/rayray.zig
···
1
const std = @import("std");
2
0
0
0
0
3
pub const Raytracer = struct {
4
const Self = @This();
5
6
allocator: std.mem.Allocator,
7
8
-
pub fn init(allocator: std.mem.Allocator) Self {
0
0
9
return .{
10
.allocator = allocator,
0
11
};
12
}
13
···
15
_ = self;
16
}
17
18
-
pub fn render(self: *Self) void {
19
-
_ = self;
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
20
}
21
};
22
23
-
pub const Camera = struct {};
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
···
1
const std = @import("std");
2
3
+
const spall = @import("spall");
4
+
const zigimg = @import("zigimg");
5
+
const color = zigimg.color;
6
+
7
pub const Raytracer = struct {
8
const Self = @This();
9
10
allocator: std.mem.Allocator,
11
12
+
camera: Camera,
13
+
14
+
pub fn init(allocator: std.mem.Allocator) !Self {
15
return .{
16
.allocator = allocator,
17
+
.camera = try Camera.init(allocator, 256 * 10, 256 * 10),
18
};
19
}
20
···
22
_ = self;
23
}
24
25
+
pub fn render(self: *Self) !zigimg.Image {
26
+
const s = spall.trace(@src(), "render", .{});
27
+
defer s.end();
28
+
29
+
const rows: usize = try std.Thread.getCpuCount();
30
+
const row_height = @divTrunc(self.camera.height, rows);
31
+
const num_threads = blk: {
32
+
if (self.camera.height % rows == 0) {
33
+
break :blk rows;
34
+
}
35
+
break :blk rows + 1;
36
+
};
37
+
std.debug.print("rows: {}, row_height: {}, num_threads: {}", .{ rows, row_height, num_threads });
38
+
39
+
const threads = try self.allocator.alloc(std.Thread, num_threads);
40
+
defer self.allocator.free(threads);
41
+
42
+
for (0..num_threads) |row| {
43
+
const t = try std.Thread.spawn(.{}, r, .{ &self.camera, row, row_height });
44
+
threads[row] = t;
45
+
}
46
+
47
+
for (threads) |t| {
48
+
t.join();
49
+
}
50
+
51
+
return self.camera.image;
52
+
}
53
+
54
+
fn r(camera: *Camera, row: usize, height: usize) void {
55
+
spall.init_thread();
56
+
defer spall.deinit_thread();
57
+
58
+
const s = spall.trace(@src(), "thread {}", .{row});
59
+
defer s.end();
60
+
61
+
for (0..height) |iy| {
62
+
const y = iy + height * row;
63
+
if (y >= camera.height) break;
64
+
65
+
for (0..camera.width) |x| {
66
+
@setRuntimeSafety(false);
67
+
if (iy <= height - 5) {
68
+
camera.setPixel(x, y, color.Rgba32.initRgba(
69
+
@intCast(x),
70
+
@intCast(y),
71
+
0,
72
+
255,
73
+
)) catch break;
74
+
} else {
75
+
camera.setPixel(x, y, color.Rgba32.initRgba(
76
+
0,
77
+
0,
78
+
255,
79
+
255,
80
+
)) catch break;
81
+
}
82
+
}
83
+
}
84
}
85
};
86
87
+
pub const Camera = struct {
88
+
width: usize,
89
+
height: usize,
90
+
image: zigimg.Image,
91
+
92
+
pub fn init(allocator: std.mem.Allocator, width: usize, height: usize) !Camera {
93
+
const img = try zigimg.Image.create(allocator, width, height, zigimg.PixelFormat.rgba32);
94
+
95
+
return Camera{
96
+
.width = width,
97
+
.height = height,
98
+
.image = img,
99
+
};
100
+
}
101
+
102
+
pub fn setPixel(self: *Camera, x: usize, y: usize, c: color.Rgba32) !void {
103
+
if (x >= self.width or y >= self.height) return error.OutOfBounds;
104
+
const i = x + self.width * y;
105
+
self.image.pixels.rgba32[i] = c;
106
+
}
107
+
};