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
+ using AMDGPU
8
+
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)
@@ -51,88 +61,42 @@ begin
51
61
end
52
62
53
63
@inline function get_camera_sample (p_raster:: Point2 )
54
-
55
64
p_film = p_raster .+ rand (Point2f)
56
65
p_lens = rand (Point2f)
57
66
Trace. CameraSample (p_film, p_lens, rand (Float32))
58
67
end
59
68
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
94
74
95
- @inline function trace_pixel (camera, bvh , xy, lights )
75
+ @inline function trace_pixel (camera, scene , xy)
96
76
pixel = Point2f (Tuple (xy))
97
77
camera_sample = get_camera_sample (pixel)
98
78
ray, ω = Trace. generate_ray_differential (camera, camera_sample)
99
- l = Trace. RGBSpectrum (0.0f0 )
100
79
if ω > 0.0f0
101
- hit, shape, si = Trace. intersect! (bvh , ray)
80
+ hit, shape, si = Trace. intersect! (scene , ray)
102
81
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 )
104
83
end
105
84
end
106
- return RGBf (l . c ... )
85
+ return l
107
86
end
108
87
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... )
118
93
end
119
94
end
120
95
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)
133
97
backend = KA. get_backend (img)
134
98
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 ))
136
100
KA. synchronize (backend)
137
101
return img
138
102
end
@@ -141,17 +105,19 @@ end
141
105
# using CUDA
142
106
# ArrayType = CuArray
143
107
144
- using Metal
145
- ArrayType = MtlArray
108
+ # using Metal
109
+ # ArrayType = MtlArray
110
+
146
111
preserve = []
147
- gpu_bvh = to_gpu (ArrayType, bvh ; preserve= preserve);
112
+ gpu_scene = to_gpu (ArrayType, scene ; preserve= preserve);
148
113
gpu_img = ArrayType (zeros (RGBf, res, res));
149
114
# launch_trace_image!(img, cam, bvh, lights);
150
115
# @btime launch_trace_image!(img, cam, bvh, lights);
151
116
# @btime launch_trace_image!(gpu_img, cam, gpu_bvh, lights);
152
- launch_trace_image! (gpu_img, cam, gpu_bvh, lights );
153
- @btime launch_trace_image! (img, cam, bvh , lights)
117
+ launch_trace_image! (gpu_img, cam, gpu_scene );
118
+ launch_trace_image! (img, cam, scene , lights)
154
119
# 76.420 ms (234 allocations: 86.05 KiB)
120
+ # 75.973 ms (234 allocations: 86.05 KiB)
155
121
Array (gpu_img)
156
122
157
123
function cu_trace_image! (img, camera, bvh, lights)
@@ -255,3 +221,24 @@ t = Trace.Triangle(m, 1)
255
221
r = Trace. Ray (o= Point3f (ray_origin), d= ray_direction)
256
222
Trace. intersect_p (t, r)
257
223
Trace. intersect_triangle (r. o, r. d, t. vertices... )
224
+
225
+ # function launch_trace_image_ir!(img, camera, bvh, lights)
226
+ # backend = KA.get_backend(img)
227
+ # kernel! = ka_trace_image!(backend)
228
+ # open("test2.ir", "w") do io
229
+ # @device_code_llvm io begin
230
+ # kernel!(img, camera, bvh, lights, ndrange = size(img), workgroupsize = (16, 16))
231
+ # end
232
+ # end
233
+ # AMDGPU.synchronize(; stop_hostcalls=false)
234
+ # return img
235
+ # end
236
+
237
+ ray = Trace. RayDifferentials (Trace. Ray (o= Point3f (0.5 , 0.5 , 1.0 ), d= Vec3f (0.0 , 0.0 , - 1.0 )))
238
+ open (" li.llvm" , " w" ) do io
239
+ code_llvm (io, Trace. li, typeof .((Trace. UniformSampler (8 ), 5 , ray, scene, 1 )))
240
+ end
241
+
242
+ open (" li-wt.jl" , " w" ) do io
243
+ code_warntype (io, Trace. li, typeof .((Trace. UniformSampler (8 ), 5 , ray, scene, 1 )))
244
+ end
0 commit comments