Skip to content

Commit 5fd24cb

Browse files
committed
add new test
1 parent eafbffe commit 5fd24cb

File tree

1 file changed

+111
-0
lines changed

1 file changed

+111
-0
lines changed

test/gpu-sampler.jl

Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
1+
using GeometryBasics, LinearAlgebra, Trace, BenchmarkTools
2+
using ImageShow
3+
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+
13+
include("./../src/gpu-support.jl")
14+
15+
LowSphere(radius, contact=Point3f(0)) = Sphere(contact .+ Point3f(0, 0, radius), radius)
16+
17+
function tmesh(prim, material)
18+
19+
prim = prim isa Sphere ? Tesselation(prim, 64) : prim
20+
mesh = normal_mesh(prim)
21+
m = Trace.create_triangle_mesh(mesh)
22+
return Trace.GeometricPrimitive(m, material)
23+
end
24+
25+
material_red = Trace.MatteMaterial(
26+
Trace.ConstantTexture(Trace.RGBSpectrum(0.796f0, 0.235f0, 0.2f0)),
27+
Trace.ConstantTexture(0.0f0),
28+
)
29+
30+
31+
begin
32+
s1 = tmesh(LowSphere(0.5f0), material_red)
33+
s2 = tmesh(LowSphere(0.3f0, Point3f(0.5, 0.5, 0)), material_red)
34+
s3 = tmesh(LowSphere(0.3f0, Point3f(-0.5, 0.5, 0)), material_red)
35+
s4 = tmesh(LowSphere(0.4f0, Point3f(0, 1.0, 0)), material_red)
36+
37+
ground = tmesh(Rect3f(Vec3f(-5, -5, 0), Vec3f(10, 10, 0.01)), material_red)
38+
back = tmesh(Rect3f(Vec3f(-5, -3, 0), Vec3f(10, 0.01, 10)), material_red)
39+
l = tmesh(Rect3f(Vec3f(-2, -5, 0), Vec3f(0.01, 10, 10)), material_red)
40+
r = tmesh(Rect3f(Vec3f(2, -5, 0), Vec3f(0.01, 10, 10)), material_red)
41+
bvh = Trace.BVHAccel([s1, s2, s3, s4, ground, back, l, r]);
42+
res = 512
43+
resolution = Point2f(res)
44+
f = Trace.LanczosSincFilter(Point2f(1.0f0), 3.0f0)
45+
film = Trace.Film(resolution,
46+
Trace.Bounds2(Point2f(0.0f0), Point2f(1.0f0)),
47+
f, 1.0f0, 1.0f0,
48+
"shadows_sppm_res.png",
49+
)
50+
screen_window = Trace.Bounds2(Point2f(-1), Point2f(1))
51+
cam = Trace.PerspectiveCamera(
52+
Trace.look_at(Point3f(0, 4, 2), Point3f(0, -4, -1), Vec3f(0, 0, 1)),
53+
screen_window, 0.0f0, 1.0f0, 0.0f0, 1.0f6, 45.0f0, film,
54+
)
55+
lights = (
56+
# Trace.PointLight(Vec3f(0, -1, 2), Trace.RGBSpectrum(22.0f0)),
57+
Trace.PointLight(Vec3f(0, 0, 2), Trace.RGBSpectrum(10.0f0)),
58+
Trace.PointLight(Vec3f(0, 3, 3), Trace.RGBSpectrum(25.0f0)),
59+
)
60+
img = zeros(RGBf, res, res)
61+
end
62+
63+
@inline function get_camera_sample(p_raster::Point2)
64+
p_film = p_raster .+ rand(Point2f)
65+
p_lens = rand(Point2f)
66+
Trace.CameraSample(p_film, p_lens, rand(Float32))
67+
end
68+
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
74+
75+
@inline function trace_pixel(camera, scene, xy)
76+
pixel = Point2f(Tuple(xy))
77+
camera_sample = get_camera_sample(pixel)
78+
ray, ω = Trace.generate_ray_differential(camera, camera_sample)
79+
if ω > 0.0f0
80+
hit, shape, si = Trace.intersect!(scene, ray)
81+
if hit
82+
l = Trace.li(Trace.UniformSampler(8), 5, ray, scene, 1)
83+
end
84+
end
85+
return l
86+
end
87+
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...)
93+
end
94+
end
95+
96+
function launch_trace_image!(img, camera, scene)
97+
backend = KA.get_backend(img)
98+
kernel! = ka_trace_image!(backend)
99+
kernel!(img, camera, scene, lights, ndrange=size(img), workgroupsize=(16, 16))
100+
KA.synchronize(backend)
101+
return img
102+
end
103+
104+
preserve = []
105+
gpu_scene = to_gpu(ArrayType, scene; preserve=preserve);
106+
gpu_img = ArrayType(zeros(RGBf, res, res));
107+
# launch_trace_image!(img, cam, bvh, lights);
108+
# @btime launch_trace_image!(img, cam, bvh, lights);
109+
# @btime launch_trace_image!(gpu_img, cam, gpu_bvh, lights);
110+
launch_trace_image!(gpu_img, cam, gpu_scene);
111+
launch_trace_image!(img, cam, scene, lights)

0 commit comments

Comments
 (0)