1
1
using Makie, Trace, ImageShow, Colors, FileIO, LinearAlgebra, GeometryBasics
2
+ using AMDGPU
3
+ ArrayType = ROCArray
2
4
3
5
function to_spectrum (data:: Colorant )
4
6
rgb = RGBf (data)
@@ -136,10 +138,10 @@ function render_scene(scene::Makie.Scene)
136
138
return render_scene (c -> Trace. WhittedIntegrator (c, Trace. UniformSampler (8 ), 5 ), scene)
137
139
end
138
140
139
- function render_scene (integrator_f, mscene :: Makie.Scene )
140
- # Only set background image if it isn't set by env light, since
141
+ function convert_scene (scene :: Makie.Scene )
142
+ # Only set background image if it isn't set by env light, since
141
143
# background image takes precedence
142
- resolution = Point2f (size (mscene ))
144
+ resolution = Point2f (size (scene ))
143
145
f = Trace. LanczosSincFilter (Point2f (1.0f0 ), 3.0f0 )
144
146
film = Trace. Film (
145
147
resolution,
@@ -148,25 +150,43 @@ function render_scene(integrator_f, mscene::Makie.Scene)
148
150
" trace-test.png" ,
149
151
)
150
152
primitives = []
151
- for plot in mscene . plots
153
+ for plot in scene . plots
152
154
prim = to_trace_primitive (plot)
153
155
! isnothing (prim) && push! (primitives, prim)
154
156
end
155
- camera = to_trace_camera (mscene , film)
157
+ camera = to_trace_camera (scene , film)
156
158
lights = []
157
- for light in mscene . lights
159
+ for light in scene . lights
158
160
l = to_trace_light (light)
159
161
isnothing (l) || push! (lights, l)
160
162
end
161
163
if isempty (lights)
162
164
error (" Must have at least one light" )
163
165
end
164
166
bvh = Trace. BVHAccel (primitives, 1 )
165
- integrator = integrator_f (camera)
166
167
scene = Trace. Scene ([lights... ], bvh)
168
+ return scene, camera, film
169
+ end
170
+
171
+ function render_whitted (mscene:: Makie.Scene ; samples_per_pixel= 8 , max_depth= 5 )
172
+ scene, camera, film = convert_scene (mscene)
173
+ integrator = Trace. WhittedIntegrator (camera, Trace. UniformSampler (samples_per_pixel), max_depth)
167
174
integrator (scene, film)
168
175
return reverse (film. framebuffer, dims= 1 )
169
176
end
177
+
178
+ function render_gpu (mscene:: Makie.Scene , ArrayType; samples_per_pixel= 8 , max_depth= 5 )
179
+ scene, camera, film = convert_scene (mscene)
180
+ preserve = []
181
+ gpu_scene = Trace. to_gpu (ArrayType, scene; preserve= preserve)
182
+ res = Int .((film. resolution... ,))
183
+ gpu_img = ArrayType (zeros (RGBf, res))
184
+ GC. @preserve preserve begin
185
+ Trace. launch_trace_image! (gpu_img, camera, gpu_scene, Int32 (samples_per_pixel), Int32 (max_depth))
186
+ end
187
+ return Array (gpu_img)
188
+ end
189
+
170
190
glass = Trace. GlassMaterial (
171
191
Trace. ConstantTexture (Trace. RGBSpectrum (1.0f0 )),
172
192
Trace. ConstantTexture (Trace. RGBSpectrum (1.0f0 )),
@@ -191,9 +211,12 @@ begin
191
211
cam3d! (scene)
192
212
mesh! (scene, catmesh, color= load (Makie. assetpath (" diffusemap.png" )))
193
213
center! (scene)
214
+ @time render_whitted (scene)
194
215
# 1.024328 seconds (16.94 M allocations: 5.108 GiB, 46.19% gc time, 81 lock conflicts)
195
216
# 0.913530 seconds (16.93 M allocations: 5.108 GiB, 42.52% gc time, 57 lock conflicts)
196
- @time render_scene (scene)
217
+ # 0.416158 seconds (75.58 k allocations: 88.646 MiB, 2.44% gc time, 16 lock conflicts)
218
+ @time render_gpu (scene, ArrayType)
219
+ # 0.135438 seconds (76.03 k allocations: 82.406 MiB, 8.57% gc time)
197
220
end
198
221
199
222
begin
@@ -206,7 +229,48 @@ begin
206
229
zs = [cos (x) * sin (y) for x in xs, y in ys]
207
230
surface! (scene, xs, ys, zs)
208
231
center! (scene)
209
- # 1.598740s
232
+
233
+ @time render_whitted (scene)
234
+ # 1.598740s
210
235
# 1.179450 seconds (17.30 M allocations: 5.126 GiB, 36.48% gc time, 94 lock conflicts)
211
- @time render_scene (scene)
236
+ # 0.976180 seconds (443.12 k allocations: 107.841 MiB, 6.60% gc time, 12 lock conflicts)
237
+ @time render_gpu (scene, ArrayType)
238
+ # 0.236231 seconds (443.48 k allocations: 101.598 MiB, 3.92% gc time)
239
+ end
240
+
241
+ model = load (joinpath (@__DIR__ , " .." , " src" , " assets" , " models" , " caustic-glass.ply" ))
242
+ begin
243
+ glass = Trace. GlassMaterial (
244
+ Trace. ConstantTexture (Trace. RGBSpectrum (1.0f0 )),
245
+ Trace. ConstantTexture (Trace. RGBSpectrum (1.0f0 )),
246
+ Trace. ConstantTexture (0.0f0 ),
247
+ Trace. ConstantTexture (0.0f0 ),
248
+ Trace. ConstantTexture (1.25f0 ),
249
+ true ,
250
+ )
251
+ plastic = Trace. PlasticMaterial (
252
+ Trace. ConstantTexture (Trace. RGBSpectrum (0.6399999857f0 , 0.6399999857f0 , 0.6399999857f0 )),
253
+ Trace. ConstantTexture (Trace. RGBSpectrum (0.1000000015f0 , 0.1000000015f0 , 0.1000000015f0 )),
254
+ Trace. ConstantTexture (0.010408001f0 ),
255
+ true ,
256
+ )
257
+ scene = Scene (size= (1024 , 1024 ); lights= [
258
+ AmbientLight (RGBf (1 , 1 , 1 )),
259
+ PointLight (Vec3f (4 , 4 , 10 ), RGBf (150 , 150 , 150 )),
260
+ PointLight (Vec3f (- 3 , 10 , 2.5 ), RGBf (60 , 60 , 60 )),
261
+ PointLight (Vec3f (0 , 3 , 0.5 ), RGBf (40 , 40 , 40 ))
262
+ ])
263
+ cam3d! (scene)
264
+ cm = scene. camera_controls
265
+ mesh! (scene, model, material= glass)
266
+ mini, maxi = extrema (Rect3f (decompose (Point, model)))
267
+ floorrect = Rect3f (Vec3f (- 10 , mini[2 ], - 10 ), Vec3f (20 , - 1 , 20 ))
268
+ mesh! (scene, floorrect, material= plastic)
269
+ center! (scene)
270
+ update_cam! (scene, Vec3f (- 1.6 , 6.2 , 0.2 ), Vec3f (- 3.6 , 2.5 , 2.4 ), Vec3f (0 , 1 , 0 ))
271
+
272
+ @time render_whitted (scene)
273
+ # 9.820304 seconds (1.69 M allocations: 235.165 MiB, 0.51% gc time, 3 lock conflicts)
274
+ @time render_gpu (scene, ArrayType)
275
+ # 6.128600 seconds (1.70 M allocations: 228.875 MiB, 1.09% gc time)
212
276
end
0 commit comments