Skip to content

Commit 2241e61

Browse files
committed
small improvements
1 parent 5fd24cb commit 2241e61

File tree

4 files changed

+83
-96
lines changed

4 files changed

+83
-96
lines changed

docs/code/basic-scene.jl

Lines changed: 28 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -12,31 +12,32 @@ function tmesh(prim, material)
1212
return Trace.GeometricPrimitive(m, material)
1313
end
1414

15-
material_red = Trace.MatteMaterial(
16-
Trace.ConstantTexture(Trace.RGBSpectrum(0.796f0, 0.235f0, 0.2f0)),
17-
Trace.ConstantTexture(0.0f0),
18-
)
19-
material_blue = Trace.MatteMaterial(
20-
Trace.ConstantTexture(Trace.RGBSpectrum(0.251f0, 0.388f0, 0.847f0)),
21-
Trace.ConstantTexture(0.0f0),
22-
)
23-
material_white = Trace.MatteMaterial(
24-
Trace.ConstantTexture(Trace.RGBSpectrum(1.0f0)),
25-
Trace.ConstantTexture(0.0f0),
26-
)
27-
mirror = Trace.MirrorMaterial(Trace.ConstantTexture(Trace.RGBSpectrum(1.0f0)))
28-
glass = Trace.GlassMaterial(
29-
Trace.ConstantTexture(Trace.RGBSpectrum(1.0f0)),
30-
Trace.ConstantTexture(Trace.RGBSpectrum(1.0f0)),
31-
Trace.ConstantTexture(0.0f0),
32-
Trace.ConstantTexture(0.0f0),
33-
Trace.ConstantTexture(1.5f0),
34-
true,
35-
)
36-
3715
LowSphere(radius, contact=Point3f(0)) = Sphere(contact .+ Point3f(0, 0, radius), radius)
3816

3917
begin
18+
19+
material_red = Trace.MatteMaterial(
20+
Trace.ConstantTexture(Trace.RGBSpectrum(0.796f0, 0.235f0, 0.2f0)),
21+
Trace.ConstantTexture(0.0f0),
22+
)
23+
material_blue = Trace.MatteMaterial(
24+
Trace.ConstantTexture(Trace.RGBSpectrum(0.251f0, 0.388f0, 0.847f0)),
25+
Trace.ConstantTexture(0.0f0),
26+
)
27+
material_white = Trace.MatteMaterial(
28+
Trace.ConstantTexture(Trace.RGBSpectrum(1.0f0)),
29+
Trace.ConstantTexture(0.0f0),
30+
)
31+
mirror = Trace.MirrorMaterial(Trace.ConstantTexture(Trace.RGBSpectrum(1.0f0)))
32+
glass = Trace.GlassMaterial(
33+
Trace.ConstantTexture(Trace.RGBSpectrum(1.0f0)),
34+
Trace.ConstantTexture(Trace.RGBSpectrum(1.0f0)),
35+
Trace.ConstantTexture(0.0f0),
36+
Trace.ConstantTexture(0.0f0),
37+
Trace.ConstantTexture(1.5f0),
38+
true,
39+
)
40+
4041
s1 = tmesh(LowSphere(0.5f0), material_white)
4142
s2 = tmesh(LowSphere(0.3f0, Point3f(0.5, 0.5, 0)), material_blue)
4243
s3 = tmesh(LowSphere(0.3f0, Point3f(-0.5, 0.5, 0)), mirror)
@@ -49,12 +50,12 @@ begin
4950

5051
bvh = Trace.BVHAccel([s1, s2, s3, s4, ground, back, l, r], 1);
5152

52-
lights = [
53+
lights = (
5354
# Trace.PointLight(Vec3f(0, -1, 2), Trace.RGBSpectrum(22.0f0)),
5455
Trace.PointLight(Vec3f(0, 0, 2), Trace.RGBSpectrum(10.0f0)),
5556
Trace.PointLight(Vec3f(0, 3, 3), Trace.RGBSpectrum(25.0f0)),
56-
]
57-
scene = Trace.Scene(lights, bvh);
57+
)
58+
scene = Trace.Scene([lights...], bvh);
5859
resolution = Point2f(1024)
5960
f = Trace.LanczosSincFilter(Point2f(1.0f0), 3.0f0)
6061
film = Trace.Film(resolution,
@@ -77,6 +78,8 @@ end
7778
# 6.296157 seconds (17.64 k allocations: 19.796 MiB, 0.13% gc time, 45 lock conflicts)
7879
# After more GPU optimizations
7980
# 4.169616 seconds (17.37 k allocations: 19.777 MiB, 0.14% gc time, 20 lock conflicts)
81+
# After first shading running on GPU
82+
# 3.835527 seconds (17.36 k allocations: 19.779 MiB, 0.16% gc time, 41 lock conflicts)
8083

8184
camera_sample = Trace.get_camera_sample(integrator.sampler, Point2f(512))
8285
ray, ω = Trace.generate_ray_differential(integrator.camera, camera_sample)

src/integrators/sampler.jl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -129,8 +129,8 @@ function li(
129129
end
130130
if depth + 1 max_depth
131131
# Trace rays for specular reflection & refraction.
132-
l += specular_reflect(bsdf, sampler, max_depth, ray, si, scene, depth)
133-
l += specular_transmit(bsdf, sampler, max_depth, ray, si, scene, depth)
132+
# l += specular_reflect(bsdf, sampler, max_depth, ray, si, scene, depth)
133+
# l += specular_transmit(bsdf, sampler, max_depth, ray, si, scene, depth)
134134
end
135135
l
136136
end

test/gpu-sampler.jl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,3 +109,4 @@ gpu_img = ArrayType(zeros(RGBf, res, res));
109109
# @btime launch_trace_image!(gpu_img, cam, gpu_bvh, lights);
110110
launch_trace_image!(gpu_img, cam, gpu_scene);
111111
launch_trace_image!(img, cam, scene, lights)
112+
Array(gpu_img)

test/gpu-threading-benchmarks.jl

Lines changed: 52 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,15 @@
11
using GeometryBasics, LinearAlgebra, Trace, BenchmarkTools
22
using ImageShow
33
using Makie
4+
using KernelAbstractions
5+
import KernelAbstractions as KA
6+
using KernelAbstractions.Extras.LoopInfo: @unroll
7+
using AMDGPU
8+
9+
ArrayType = ROCArray
10+
# using CUDA
11+
# ArrayType = CuArray
12+
413
include("./../src/gpu-support.jl")
514

615
LowSphere(radius, contact=Point3f(0)) = Sphere(contact .+ Point3f(0, 0, radius), radius)
@@ -18,6 +27,7 @@ material_red = Trace.MatteMaterial(
1827
Trace.ConstantTexture(0.0f0),
1928
)
2029

30+
2131
begin
2232
s1 = tmesh(LowSphere(0.5f0), material_red)
2333
s2 = tmesh(LowSphere(0.3f0, Point3f(0.5, 0.5, 0)), material_red)
@@ -51,104 +61,56 @@ begin
5161
end
5262

5363
@inline function get_camera_sample(p_raster::Point2)
54-
5564
p_film = p_raster .+ rand(Point2f)
5665
p_lens = rand(Point2f)
5766
Trace.CameraSample(p_film, p_lens, rand(Float32))
5867
end
5968

60-
using KernelAbstractions.Extras.LoopInfo: @unroll
61-
62-
function simple_shading(bvh, shape, ray, si, l, depth, max_depth, lights)
63-
core = si.core
64-
n = si.shading.n
65-
wo = core.wo
66-
# Compute scattering functions for surface interaction.
67-
si = Trace.compute_differentials(si, ray)
68-
mat = Trace.get_material(bvh, shape)
69-
if mat.type === Trace.NO_MATERIAL
70-
return l
71-
end
72-
bsdf = mat(si, false, Trace.Radiance)
73-
# Compute emitted light if ray hit an area light source.
74-
l += Trace.le(si, wo)
75-
# Add contribution of each light source.
76-
@unroll for light in lights
77-
sampled_li, wi, pdf, vt = Trace.sample_li(
78-
light, core, rand(Point2f),
79-
)
80-
(Trace.is_black(sampled_li) || pdf 0.0f0) && continue
81-
f = bsdf(wo, wi)
82-
if !Trace.is_black(f) && !Trace.intersect_p(bvh, Trace.spawn_ray(vt.p0, vt.p1))
83-
l += f * sampled_li * abs(wi n) / pdf
84-
end
85-
end
86-
# if depth + 1 <= max_depth
87-
# # Trace rays for specular reflection & refraction.
88-
# l += specular_reflect(bsdf, i, ray, si, scene, depth)
89-
# l += specular_transmit(bsdf, i, ray, si, scene, depth)
90-
# end
91-
return l
92-
end
93-
69+
# ray = Trace.Ray(o=Point3f(0.5, 0.5, 1.0), d=Vec3f(0.0, 0.0, -1.0))
70+
# l = Trace.RGBSpectrum(0.0f0)
71+
# open("test3.llvm", "w") do io
72+
# code_llvm(io, simple_shading, typeof.((bvh, bvh.primitives[1], Trace.RayDifferentials(ray), Trace.SurfaceInteraction(), l, 1, 1, lights)))
73+
# end
9474

95-
@inline function trace_pixel(camera, bvh, xy, lights)
75+
@inline function trace_pixel(camera, scene, xy)
9676
pixel = Point2f(Tuple(xy))
9777
camera_sample = get_camera_sample(pixel)
9878
ray, ω = Trace.generate_ray_differential(camera, camera_sample)
99-
l = Trace.RGBSpectrum(0.0f0)
10079
if ω > 0.0f0
101-
hit, shape, si = Trace.intersect!(bvh, ray)
80+
hit, shape, si = Trace.intersect!(scene, ray)
10281
if hit
103-
l = simple_shading(bvh, shape, ray, si, l, 1, 8, lights)
82+
l = Trace.li(Trace.UniformSampler(8), 5, ray, scene, 1)
10483
end
10584
end
106-
return RGBf(l.c...)
85+
return l
10786
end
10887

109-
using KernelAbstractions
110-
import KernelAbstractions as KA
111-
112-
113-
@kernel function ka_trace_image!(img, camera, bvh, lights)
114-
idx = @index(Global, Linear)
115-
if checkbounds(Bool, img, idx)
116-
xy = Tuple(divrem(idx, size(img, 1)))
117-
@inbounds img[idx] = trace_pixel(camera, bvh, xy, lights)
88+
@kernel function ka_trace_image!(img, camera, scene)
89+
xy = @index(Global, Cartesian)
90+
if checkbounds(Bool, img, xy)
91+
l = trace_pixel(camera, scene, xy)
92+
@inbounds img[xy] = RGBf(l.c...)
11893
end
11994
end
12095

121-
function launch_trace_image_ir!(img, camera, bvh, lights)
122-
backend = KA.get_backend(img)
123-
kernel! = ka_trace_image!(backend)
124-
open("test2.ir", "w") do io
125-
@device_code_llvm io begin
126-
kernel!(img, camera, bvh, lights, ndrange = size(img), workgroupsize = (16, 16))
127-
end
128-
end
129-
AMDGPU.synchronize(; stop_hostcalls=false)
130-
return img
131-
end
132-
function launch_trace_image!(img, camera, bvh, lights)
96+
function launch_trace_image!(img, camera, scene)
13397
backend = KA.get_backend(img)
13498
kernel! = ka_trace_image!(backend)
135-
kernel!(img, camera, bvh, lights, ndrange=size(img), workgroupsize=(16, 16))
99+
kernel!(img, camera, scene, lights, ndrange=size(img), workgroupsize=(16, 16))
136100
KA.synchronize(backend)
137101
return img
138102
end
139-
using AMDGPU
140-
ArrayType = ROCArray
141-
# using CUDA
142-
# ArrayType = CuArray
103+
143104
preserve = []
144-
gpu_bvh = to_gpu(ArrayType, bvh; preserve=preserve);
105+
gpu_scene = to_gpu(ArrayType, scene; preserve=preserve);
145106
gpu_img = ArrayType(zeros(RGBf, res, res));
146107
# launch_trace_image!(img, cam, bvh, lights);
147108
# @btime launch_trace_image!(img, cam, bvh, lights);
148109
# @btime launch_trace_image!(gpu_img, cam, gpu_bvh, lights);
149-
launch_trace_image!(gpu_img, cam, gpu_bvh, lights);
150-
@btime launch_trace_image!(img, cam, bvh, lights)
110+
launch_trace_image!(gpu_img, cam, gpu_scene);
111+
launch_trace_image!(img, cam, scene, lights)
151112
# 76.420 ms (234 allocations: 86.05 KiB)
113+
# 75.973 ms (234 allocations: 86.05 KiB)
152114
Array(gpu_img)
153115

154116
function cu_trace_image!(img, camera, bvh, lights)
@@ -252,3 +214,24 @@ t = Trace.Triangle(m, 1)
252214
r = Trace.Ray(o=Point3f(ray_origin), d=ray_direction)
253215
Trace.intersect_p(t, r)
254216
Trace.intersect_triangle(r.o, r.d, t.vertices...)
217+
218+
# function launch_trace_image_ir!(img, camera, bvh, lights)
219+
# backend = KA.get_backend(img)
220+
# kernel! = ka_trace_image!(backend)
221+
# open("test2.ir", "w") do io
222+
# @device_code_llvm io begin
223+
# kernel!(img, camera, bvh, lights, ndrange = size(img), workgroupsize = (16, 16))
224+
# end
225+
# end
226+
# AMDGPU.synchronize(; stop_hostcalls=false)
227+
# return img
228+
# end
229+
230+
ray = Trace.RayDifferentials(Trace.Ray(o=Point3f(0.5, 0.5, 1.0), d=Vec3f(0.0, 0.0, -1.0)))
231+
open("li.llvm", "w") do io
232+
code_llvm(io, Trace.li, typeof.((Trace.UniformSampler(8), 5, ray, scene, 1)))
233+
end
234+
235+
open("li-wt.jl", "w") do io
236+
code_warntype(io, Trace.li, typeof.((Trace.UniformSampler(8), 5, ray, scene, 1)))
237+
end

0 commit comments

Comments
 (0)