1
1
using GeometryBasics, LinearAlgebra, Trace, BenchmarkTools
2
2
using ImageShow
3
3
using Makie
4
+ using KernelAbstractions
5
+ import KernelAbstractions as KA
6
+ using KernelAbstractions. Extras. LoopInfo: @unroll
7
+
8
+ # using AMDGPU
9
+ # ArrayType = ROCArray
10
+ using CUDA
11
+ ArrayType = CuArray
12
+
4
13
include (" ./../src/gpu-support.jl" )
5
14
6
15
LowSphere (radius, contact= Point3f (0 )) = Sphere (contact .+ Point3f (0 , 0 , radius), radius)
@@ -18,6 +27,7 @@ material_red = Trace.MatteMaterial(
18
27
Trace. ConstantTexture (0.0f0 ),
19
28
)
20
29
30
+
21
31
begin
22
32
s1 = tmesh (LowSphere (0.5f0 ), material_red)
23
33
s2 = tmesh (LowSphere (0.3f0 , Point3f (0.5 , 0.5 , 0 )), material_red)
@@ -47,103 +57,59 @@ begin
47
57
Trace. PointLight (Vec3f (0 , 0 , 2 ), Trace. RGBSpectrum (10.0f0 )),
48
58
Trace. PointLight (Vec3f (0 , 3 , 3 ), Trace. RGBSpectrum (25.0f0 )),
49
59
)
60
+ scene = Trace. Scene ([lights... ], bvh)
50
61
img = zeros (RGBf, res, res)
51
62
end
52
63
53
64
@inline function get_camera_sample (p_raster:: Point2 )
54
-
55
65
p_film = p_raster .+ rand (Point2f)
56
66
p_lens = rand (Point2f)
57
67
Trace. CameraSample (p_film, p_lens, rand (Float32))
58
68
end
59
69
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
70
+ # ray = Trace.Ray(o=Point3f(0.5, 0.5, 1.0), d=Vec3f(0.0, 0.0, -1.0))
71
+ # l = Trace.RGBSpectrum(0.0f0)
72
+ # open("test3.llvm", "w") do io
73
+ # code_llvm(io, simple_shading, typeof.((bvh, bvh.primitives[1], Trace.RayDifferentials(ray), Trace.SurfaceInteraction(), l, 1, 1, lights)))
74
+ # end
93
75
94
-
95
- @inline function trace_pixel (camera, bvh, xy, lights)
76
+ @inline function trace_pixel (camera, scene, xy)
96
77
pixel = Point2f (Tuple (xy))
97
78
camera_sample = get_camera_sample (pixel)
98
79
ray, ω = Trace. generate_ray_differential (camera, camera_sample)
99
- l = Trace. RGBSpectrum (0.0f0 )
100
80
if ω > 0.0f0
101
- hit, shape, si = Trace. intersect! (bvh , ray)
81
+ hit, shape, si = Trace. intersect! (scene , ray)
102
82
if hit
103
- l = simple_shading (bvh, shape , ray, si, l, 1 , 8 , lights )
83
+ l = Trace . li (Trace . UniformSampler ( 8 ), 5 , ray, scene, 1 )
104
84
end
105
85
end
106
- return RGBf (l . c ... )
86
+ return l
107
87
end
108
88
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)
89
+ @kernel function ka_trace_image! (img, camera, scene)
90
+ linear_idx = @index (Global, Linear)
91
+ if checkbounds (Bool, img, linear_idx)
92
+ x = ((linear_idx - 1 ) % size (img, 1 )) + 1
93
+ y = ((linear_idx - 1 ) ÷ size (img, 1 )) + 1
94
+ l = trace_pixel (camera, scene, (x, y))
95
+ @inbounds img[linear_idx] = RGBf (l. c... )
118
96
end
119
97
end
120
98
121
- function launch_trace_image_ir ! (img, camera, bvh, lights )
99
+ function launch_trace_image ! (img, camera, scene )
122
100
backend = KA. get_backend (img)
123
101
kernel! = ka_trace_image! (backend)
124
- open (" test2.ir" , " w" ) do io
125
- CUDA. @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)
133
- backend = KA. get_backend (img)
134
- kernel! = ka_trace_image! (backend)
135
- kernel! (img, camera, bvh, lights, ndrange= size (img), workgroupsize= (16 , 16 ))
102
+ kernel! (img, camera, scene, ndrange= size (img), workgroupsize= (16 , 16 ))
136
103
KA. synchronize (backend)
137
104
return img
138
105
end
139
- # using AMDGPU
140
- # ArrayType = ROCArray
141
- using CUDA
142
- ArrayType = CuArray
106
+
143
107
preserve = []
144
- gpu_bvh = to_gpu (ArrayType, bvh ; preserve= preserve);
108
+ gpu_scene = to_gpu (ArrayType, scene ; preserve= preserve);
145
109
gpu_img = ArrayType (zeros (RGBf, res, res));
146
110
# launch_trace_image!(img, cam, bvh, lights);
147
111
# @btime launch_trace_image!(img, cam, bvh, lights);
148
112
# @btime launch_trace_image!(gpu_img, cam, gpu_bvh, lights);
149
- launch_trace_image! (gpu_img, cam, gpu_bvh, lights);
113
+ launch_trace_image! (gpu_img, cam, gpu_scene);
114
+ # @btime (launch_trace_image!(img, cam, scene));
115
+ # 234.530 ms (456 allocations: 154.26 KiB)
0 commit comments