Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
49 commits
Select commit Hold shift + click to select a range
44e250f
a few small improvements
SimonDanisch Aug 14, 2024
e2d9df8
use structarrays
SimonDanisch Aug 14, 2024
2edc348
switch to memory pool
SimonDanisch Aug 15, 2024
9583282
switch to ubershader
SimonDanisch Aug 15, 2024
107d30c
small performance improvements
SimonDanisch Aug 15, 2024
018ffe8
fix bugs + perf improvements
SimonDanisch Aug 17, 2024
2ea38b3
make BSDFVector immutable
SimonDanisch Aug 17, 2024
f9baae3
fix sppm
SimonDanisch Aug 17, 2024
e22a231
remove atomics and switch to StructArrays for SPPM
SimonDanisch Aug 17, 2024
c7cd082
get rid of offset allocation
SimonDanisch Aug 18, 2024
57f7818
almost 0 allocations
SimonDanisch Aug 18, 2024
2711fdb
use immutables for SurfaceInteraction
SimonDanisch Aug 18, 2024
a153620
remove pool
SimonDanisch Aug 18, 2024
8f7032c
fix bugs and reclaim performance
SimonDanisch Aug 19, 2024
334c03d
fix camera, break examples, add TraceMakie prototype, add light types
SimonDanisch Aug 20, 2024
16e6c3e
clean up
SimonDanisch Aug 21, 2024
a82630e
small corrections
SimonDanisch Aug 21, 2024
2db9419
add tonemapping and scene
SimonDanisch Aug 21, 2024
a175b15
cleanup
SimonDanisch Aug 21, 2024
d760f40
make intersect!(bvh, ray) work on the GPU
SimonDanisch Aug 22, 2024
afef8f2
fix TraceMakie
SimonDanisch Aug 22, 2024
4c5d2a8
performance improvements
SimonDanisch Aug 23, 2024
7ff9fe3
fix more problematic gpu code
SimonDanisch Aug 23, 2024
eafbffe
some more fixes
SimonDanisch Aug 23, 2024
5fd24cb
add new test
SimonDanisch Aug 23, 2024
17b19ab
some f0
lazarusA Aug 24, 2024
a7dbff6
more f0, but splitting cos4 solves the pow metal issue
lazarusA Aug 24, 2024
2241e61
small improvements
SimonDanisch Aug 24, 2024
a2d6332
Merge branch 'sd/more-improvements' into la/pow_issue
SimonDanisch Aug 24, 2024
b935524
Merge pull request #30 from pxl-th/la/pow_issue
SimonDanisch Aug 24, 2024
f41cf0e
merge stashed changes
SimonDanisch Aug 24, 2024
4cdcce2
more fixes
SimonDanisch Aug 24, 2024
60ef870
better mapreduce
SimonDanisch Aug 24, 2024
c113489
make li(...) iterative (normal raytracing broken)
SimonDanisch Aug 24, 2024
4d78f46
fix iterative version
SimonDanisch Aug 25, 2024
89d6db7
merge
SimonDanisch Aug 25, 2024
2e06678
fix missaligned pointer
SimonDanisch Aug 25, 2024
ecb3f2d
integrate GPU version into Trace
SimonDanisch Aug 25, 2024
a164916
get film tile to work with KernelAbstraction
SimonDanisch Aug 30, 2024
faf5ad1
small improvements
SimonDanisch Aug 30, 2024
aeaa63c
a few improvements
SimonDanisch Sep 1, 2024
ba362c6
integrate film refactor + kernelabstractions
SimonDanisch Sep 13, 2024
876d9c8
changes for new GeometryBasics
SimonDanisch Mar 24, 2025
41cb00a
switch to RayCaster.jl
SimonDanisch Oct 22, 2025
fa8f2b6
try to get back performance
SimonDanisch Oct 22, 2025
710be08
more perf
SimonDanisch Oct 22, 2025
d4660e2
is it working
SimonDanisch Oct 27, 2025
3c82a16
clean up
SimonDanisch Oct 27, 2025
fe67d5b
fix sppm
SimonDanisch Oct 27, 2025
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
21 changes: 19 additions & 2 deletions Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,35 @@ authors = ["Anton Smirnov <tonysmn97@gmail.com>"]
version = "0.1.0"

[deps]
Atomix = "a9b6321e-bd34-4604-b9c9-b65b8de01458"
FileIO = "5789e2e9-d7fb-5bc7-8068-2c6fae9b9549"
GeometryBasics = "5c1252a2-5f33-56bf-86c9-59e7332b4326"
ImageCore = "a09fc81d-aa75-5fe9-8630-4744c3626534"
ImageIO = "82e4d734-157c-48bb-816b-45c225c6df19"
KernelAbstractions = "63c18a36-062a-441e-b654-da1e3ab1ce7c"
LinearAlgebra = "37e2e46d-f89d-539d-b4ee-838fcccc9c8e"
ProgressMeter = "92933f4c-e287-5a05-a399-4b506db050ca"
Random = "9a3f8284-a2c9-5f02-9a11-845980a1fd5c"
RandomNumbers = "e6cf234a-135c-5ec9-84dd-332b85af5143"
RayCaster = "afc56b53-c9a9-482a-a956-d1d800e05559"
StaticArrays = "90137ffa-7385-5640-81b9-e52037218182"
StructArrays = "09ab397b-f2b6-538f-b94a-2f83cf4a842a"

[compat]
#Atomix = "0.1.0"
FileIO = "1.16"
GeometryBasics = "0.4"
GeometryBasics = "0.4, 0.5"
ImageCore = "0.10"
ImageIO = "0.6"
#KernelAbstractions = "0.9.24"
ProgressMeter = "1.10"
StaticArrays = "1.9"
RandomNumbers = "1.6.0"
StaticArrays = "1.9.7"
StructArrays = "0.6.18"

[extras]
JET = "c3a54625-cd67-489e-a8e7-0a5a0ff4e31b"
Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40"

[targets]
test = ["Test", "JET"]
5 changes: 5 additions & 0 deletions docs/Project.toml
Original file line number Diff line number Diff line change
@@ -1,10 +1,15 @@
[deps]
BenchmarkTools = "6e4b80f9-dd63-53aa-95a3-0cdb28fa8baf"
Documenter = "e30172f5-a6a5-5a46-863b-614d45cd2de4"
DocumenterVitepress = "4710194d-e776-4893-9690-8d956a29c365"
FileIO = "5789e2e9-d7fb-5bc7-8068-2c6fae9b9549"
GeometryBasics = "5c1252a2-5f33-56bf-86c9-59e7332b4326"
ImageCore = "a09fc81d-aa75-5fe9-8630-4744c3626534"
ImageShow = "4e3cecfd-b093-5904-9786-8bbb286a6a31"
KernelAbstractions = "63c18a36-062a-441e-b654-da1e3ab1ce7c"
Literate = "98b081ad-f1c9-55d3-8b20-4c87d4299306"
Makie = "ee78f7c6-11fb-53f2-987a-cfe4a2b5a57a"
Metal = "dde4c033-4e86-420c-a63e-0dd931031962"
Trace = "afc56b53-c9a9-482a-a956-d1d800e05558"

[compat]
Expand Down
211 changes: 211 additions & 0 deletions docs/code/basic-scene.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,211 @@
using GeometryBasics
using Trace
using FileIO
using ImageCore
# using BenchmarkTools
# using FileIO, ImageShow

function tmesh(prim, material)
prim = prim isa Sphere ? Tesselation(prim, 64) : prim
mesh = normal_mesh(prim)
m = Trace.create_triangle_mesh(mesh, Trace.ShapeCore())
return Trace.GeometricPrimitive(m, material)
end

LowSphere(radius, contact=Point3f(0)) = Sphere(contact .+ Point3f(0, 0, radius), radius)

begin

material_red = Trace.MatteMaterial(
Trace.ConstantTexture(Trace.RGBSpectrum(0.796f0, 0.235f0, 0.2f0)),
Trace.ConstantTexture(0.0f0),
)
material_blue = Trace.MatteMaterial(
Trace.ConstantTexture(Trace.RGBSpectrum(0.251f0, 0.388f0, 0.847f0)),
Trace.ConstantTexture(0.0f0),
)
material_white = Trace.MatteMaterial(
Trace.ConstantTexture(Trace.RGBSpectrum(1.0f0)),
Trace.ConstantTexture(0.0f0),
)
mirror = Trace.MirrorMaterial(Trace.ConstantTexture(Trace.RGBSpectrum(1.0f0)))
glass = Trace.GlassMaterial(
Trace.ConstantTexture(Trace.RGBSpectrum(1.0f0)),
Trace.ConstantTexture(Trace.RGBSpectrum(1.0f0)),
Trace.ConstantTexture(0.0f0),
Trace.ConstantTexture(0.0f0),
Trace.ConstantTexture(1.5f0),
true,
)

s1 = tmesh(LowSphere(0.5f0), material_white)
s2 = tmesh(LowSphere(0.3f0, Point3f(0.5, 0.5, 0)), material_white)
s3 = tmesh(LowSphere(0.3f0, Point3f(-0.5, 0.5, 0)), material_white)
s4 = tmesh(LowSphere(0.4f0, Point3f(0, 1.0, 0)), material_white)

ground = tmesh(Rect3f(Vec3f(-5, -5, 0), Vec3f(10, 10, 0.01)), material_white)
back = tmesh(Rect3f(Vec3f(-5, -3, 0), Vec3f(10, 0.01, 10)), material_white)
l = tmesh(Rect3f(Vec3f(-2, -5, 0), Vec3f(0.01, 10, 10)), material_white)
r = tmesh(Rect3f(Vec3f(2, -5, 0), Vec3f(0.01, 10, 10)), material_white)

bvh = Trace.no_material_bvh([s1, s2, s3, s4, ground, back, l, r]);

lights = (
# Trace.PointLight(Vec3f(0, -1, 2), Trace.RGBSpectrum(22.0f0)),
Trace.PointLight(Vec3f(0, 0, 2), Trace.RGBSpectrum(10.0f0)),
Trace.PointLight(Vec3f(0, 3, 3), Trace.RGBSpectrum(25.0f0)),
)
scene = Trace.Scene([lights...], bvh);
resolution = Point2f(1024)
f = Trace.LanczosSincFilter(Point2f(1.0f0), 3.0f0)

film = Trace.Film(resolution,
Trace.Bounds2(Point2f(0.0f0), Point2f(1.0f0)),
f, 1.0f0, 1.0f0,
)
screen_window = Trace.Bounds2(Point2f(-1), Point2f(1))
cam = Trace.PerspectiveCamera(
Trace.look_at(Point3f(0, 4, 2), Point3f(0, -4, -1), Vec3f(0, 0, 1)),
screen_window, 0.0f0, 1.0f0, 0.0f0, 1.0f6, 45.0f0, film,
)
end

using RayCaster
using PProf, Profile
using AMDGPU

# begin
# Trace.clear!(film)
# integrator = Trace.WhittedIntegrator(cam, Trace.UniformSampler(8), 5)
# @time integrator(scene, film, cam)
# end
pres = []
g_scene = Trace.to_gpu(ROCArray, scene; preserve=pres);
g_film = Trace.to_gpu(ROCArray, film; preserve=pres);
integrator = Trace.WhittedIntegrator(cam, Trace.UniformSampler(8), 5)
AMDGPU.@device_code_warntype interactive = true integrator(g_scene, g_film, cam);
@time integrator(scene, film, cam);
@time Trace.integrator_threaded(integrator, scene, film, cam);
using OpenCL

cl_scene = Trace.to_gpu(CLArray, scene)
cl_film = Trace.to_gpu(CLArray, film);

@time integrator(cl_scene, cl_film, cam);
OpenCL.@device_code_warntype interactive = true integrator(cl_scene, cl_film, cam)
# Setup for profiling sample_kernel_inner!

function profile_kernel(film, scene, cam)
tiles = film.tiles
tile_size = film.tile_size
filter_radius = film.filter_radius
filter_table = film.filter_table
pixels = film.pixels
tile_bounds = Trace.Bounds2(Point2f(1, 1), Point2f(16, 16))
tile_column = Int32(1)
max_depth = Int32(5)
sampler = Trace.UniformSampler(8)
camera = cam
spp_sqr = 1.0f0 / √Float32(sampler.samples_per_pixel)
@sync for i in 1:1024
for j in 1:1024
pixel = Point2f(i, j)
Threads.@spawn Trace.sample_kernel_inner!(
tiles, tile_bounds, tile_column, Point2f(size(pixels)),
max_depth, scene, sampler, camera,
pixel, spp_sqr, filter_table, filter_radius
)
end
end
return
end
@time Trace.integrator_threaded(integrator, scene,film, cam)

begin
tiles = film.tiles
tile_size = film.tile_size
filter_radius = film.filter_radius
filter_table = film.filter_table
pixels = film.pixels
tile_bounds = Trace.Bounds2(Point2f(1, 1), Point2f(16, 16))
tile_column = Int32(1)
max_depth = Int32(5)
sampler = Trace.UniformSampler(8)
camera = cam
pixel = Point2f(8, 8)
spp_sqr = 1.0f0 / √Float32(sampler.samples_per_pixel)
Trace.sample_kernel_inner!(
tiles, tile_bounds, tile_column, Point2f(size(pixels)),
max_depth, scene, sampler, camera,
pixel, spp_sqr, filter_table, filter_radius
)
end



begin
Profile.Allocs.clear()
Profile.Allocs.@profview_allocs integrator(scene, film, cam)
PProf.Allocs.pprof()
end

@btime Trace.NoMaterial()

bvh = scene.aggregate.bvh
ray = RayCaster.Ray(o=Point3f(0, 4, 2), d=Point3f(0, -4, -1))
args = Trace.intersect!(scene, ray)
camera_sample = @inline get_camera_sample(sampler, campix)
ray, ω = Trace.generate_ray_differential(cam, camera_sample)
ray = scale_differentials(ray, spp_sqr)
l = RGBSpectrum(0.0f0)


function testo(sampler, max_depth, ray, scene)
l = Trace.RGBSpectrum(0.0f0)
for depth in 1:1000
# For demonstration, we just accumulate some dummy value
l += Trace.li_iterative(sampler, max_depth, ray, scene)
end
return l
end

@allocated testo(sampler, max_depth, ray, scene)

@btime Trace.li_iterative(sampler, max_depth, ray, scene)
t1 = scene.aggregate.bvh.primitives[1]
si = Trace.triangle_to_surface_interaction(t1, ray, Point3f(0))
m = scene.aggregate.materials[1]

@edit m(si, false, Trace.UInt8(1))

bsdf = Trace.matte_material(m, si, false, Trace.UInt8(1))
m.type == Trace.MATTE_MATERIAL
Trace.BSDF()


@code_warntype Trace.specular(Trace.Transmit, bsdf, sampler, ray, si)
using JET

# begin
# resolution = Point2f(1024)
# Trace.clear!(film)
# @time render_scene(scene, film, cam)
# Trace.to_framebuffer!(film, 1.0f0)
# film.framebuffer
# end


# 6.296157 seconds (17.64 k allocations: 19.796 MiB, 0.13% gc time, 45 lock conflicts)
# After more GPU optimizations
# 4.169616 seconds (17.37 k allocations: 19.777 MiB, 0.14% gc time, 20 lock conflicts)
# After first shading running on GPU
# 3.835527 seconds (17.36 k allocations: 19.779 MiB, 0.16% gc time, 41 lock conflicts)
# 4.191 s (4710 allocations: 18.36 MiB)
# iterative_li: 5.2s -.-


# begin
# integrator = Trace.SPPMIntegrator(cam, 0.075f0, 5, 1)
# integrator(scene)
# img = reverse(film.framebuffer, dims=1)
# end
102 changes: 21 additions & 81 deletions docs/code/caustic_glass.jl
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
using GeometryBasics
using LinearAlgebra
using Trace
using GeometryBasics, ImageShow
using LinearAlgebra, Makie
using Trace, FileIO, MeshIO

function render()
model = load(joinpath(@__DIR__, "..", "src", "assets", "models", "caustic-glass.ply"))
begin
glass = Trace.GlassMaterial(
Trace.ConstantTexture(Trace.RGBSpectrum(1f0)),
Trace.ConstantTexture(Trace.RGBSpectrum(1f0)),
Expand All @@ -17,82 +18,21 @@ function render()
Trace.ConstantTexture(0.010408001f0),
true,
)

model = "./scenes/models/caustic-glass.ply"
triangle_meshes, triangles = Trace.load_triangle_mesh(
model, Trace.ShapeCore(Trace.translate(Vec3f(5, -1.49, -100)), false),
)
floor_triangles = Trace.create_triangle_mesh(
Trace.ShapeCore(Trace.translate(Vec3f(-10, 0, -87)), false),
2, UInt32[1, 2, 3, 1, 4, 3],
4,
[
Point3f(0, 0, 0), Point3f(0, 0, -30),
Point3f(30, 0, -30), Point3f(30, 0, 0),
],
[
Trace.Normal3f(0, 1, 0), Trace.Normal3f(0, 1, 0),
Trace.Normal3f(0, 1, 0), Trace.Normal3f(0, 1, 0),
],
)

primitives = Vector{Trace.GeometricPrimitive}(undef, 0)
for t in triangles
push!(primitives, Trace.GeometricPrimitive(t, glass))
end
for t in floor_triangles
push!(primitives, Trace.GeometricPrimitive(t, plastic))
scene = Scene(size=(1024, 1024); lights=[
AmbientLight(RGBf(1, 1, 1)),
PointLight(Vec3f(4, 4, 10), RGBf(150, 150, 150)),
PointLight(Vec3f(-3, 10, 2.5), RGBf(60, 60, 60)),
PointLight(Vec3f(0, 3, 0.5), RGBf(40, 40, 40))
])
cam3d!(scene)
cm = scene.camera_controls
mesh!(scene, model, material=glass)
mini, maxi = extrema(Rect3f(decompose(Point, model)))
floorrect = Rect3f(Vec3f(-10, mini[2], -10), Vec3f(20, -1, 20))
mesh!(scene, floorrect, material=plastic)
center!(scene)
update_cam!(scene, Vec3f(-1.6, 6.2, 0.2), Vec3f(-3.6, 2.5, 2.4), Vec3f(0, 1, 0))
render_scene(scene) do camera
Trace.SPPMIntegrator(camera, 0.075f0, 5, 100)
end

bvh = Trace.BVHAccel(primitives, 1)

from, to = Point3f(0, 2, 0), Point3f(-5, 0, 5)
cone_angle, cone_δ_angle = 30f0, 10f0
dir = normalize(Vec3f(to - from))
dir, du, dv = Trace.coordinate_system(dir)

dir_to_z = Trace.Transformation(transpose(Mat4f(
du[1], du[2], du[3], 0f0,
dv[1], dv[2], dv[3], 0f0,
dir[1], dir[2], dir[3], 0f0,
0f0, 0f0, 0f0, 1f0,
)))
light_to_world = (
Trace.translate(Vec3f(4.5, 0, -101))
* Trace.translate(Vec3f(from))
* inv(dir_to_z)
)

lights = [
Trace.SpotLight(
light_to_world, Trace.RGBSpectrum(60f0),
cone_angle, cone_angle - cone_δ_angle,
),
]

scene = Trace.Scene(lights, bvh)

resolution = Point2f(256)
n_samples = 8
ray_depth = 5

look_point = Point3f(-3, 0, -91)
screen = Trace.Bounds2(Point2f(-1f0), Point2f(1f0))
filter = Trace.LanczosSincFilter(Point2f(1f0), 3f0)

ir = Int64.(resolution)
film = Trace.Film(
resolution, Trace.Bounds2(Point2f(0), Point2f(1)),
filter, 1f0, 1f0,
"./scenes/caustics-sppm-$(ir[1])x$(ir[2]).png",
)
camera = Trace.PerspectiveCamera(
Trace.look_at(Point3f(0, 150, 150), look_point, Vec3f(0, 1, 0)),
screen, 0f0, 1f0, 0f0, 1f6, 90f0, film,
)

integrator = Trace.SPPMIntegrator(camera, 0.075f0, ray_depth, 100, -1)
integrator(scene)
end

render()
Loading
Loading