1
1
using Makie, Trace, ImageShow, Colors, FileIO, LinearAlgebra, GeometryBasics
2
- using AMDGPU
2
+ using AMDGPU, GLMakie
3
+
3
4
ArrayType = ROCArray
4
5
5
6
function to_spectrum (data:: Colorant )
@@ -123,19 +124,17 @@ end
123
124
124
125
function to_trace_camera (scene:: Makie.Scene , film)
125
126
cc = scene. camera_controls
126
- fov = cc. fov[]
127
- view = Trace. look_at (
128
- Point3f (cc. eyeposition[]), Point3f (cc. lookat[]), Vec3f (cc. upvector[]),
129
- )
130
- return Trace. PerspectiveCamera (
131
- view, Trace. Bounds2 (Point2f (- 1.0f0 ), Point2f (1.0f0 )),
132
- 0.0f0 , 1.0f0 , 0.0f0 , 1.0f6 , Float32 (fov),
133
- film
134
- )
135
- end
136
-
137
- function render_scene (scene:: Makie.Scene )
138
- return render_scene (c -> Trace. WhittedIntegrator (c, Trace. UniformSampler (8 ), 5 ), scene)
127
+ return lift (scene, cc. eyeposition, cc. lookat, cc. upvector, cc. fov) do eyeposition, lookat, upvector, fov
128
+ view = Trace. look_at (
129
+ Point3f (eyeposition), Point3f (lookat), Vec3f (upvector),
130
+ )
131
+ return Trace. PerspectiveCamera (
132
+ view, Trace. Bounds2 (Point2f (- 1.0f0 ), Point2f (1.0f0 )),
133
+ 0.0f0 , 1.0f0 , 0.0f0 , 1.0f6 , Float32 (fov),
134
+ film
135
+ )
136
+ end
137
+ return
139
138
end
140
139
141
140
function convert_scene (scene:: Makie.Scene )
170
169
171
170
function render_whitted (mscene:: Makie.Scene ; samples_per_pixel= 8 , max_depth= 5 )
172
171
scene, camera, film = convert_scene (mscene)
173
- integrator = Trace. WhittedIntegrator (camera, Trace. UniformSampler (samples_per_pixel), max_depth)
172
+ integrator = Trace. WhittedIntegrator (camera[], Trace. UniformSampler (samples_per_pixel), max_depth)
173
+ integrator (scene, film)
174
+ return reverse (film. framebuffer, dims= 1 )
175
+ end
176
+
177
+ function render_sppm (mscene:: Makie.Scene ; search_radius= 0.075f0 , max_depth= 5 , iterations= 100 )
178
+ scene, camera, film = convert_scene (mscene)
179
+ integrator = Trace. SPPMIntegrator (camera[], search_radius, max_depth, iterations, film)
174
180
integrator (scene, film)
175
181
return reverse (film. framebuffer, dims= 1 )
176
182
end
@@ -182,41 +188,57 @@ function render_gpu(mscene::Makie.Scene, ArrayType; samples_per_pixel=8, max_dep
182
188
res = Int .((film. resolution... ,))
183
189
gpu_img = ArrayType (zeros (RGBf, res))
184
190
GC. @preserve preserve begin
185
- Trace. launch_trace_image! (gpu_img, camera, gpu_scene, Int32 (samples_per_pixel), Int32 (max_depth))
191
+ Trace. launch_trace_image! (gpu_img, camera[] , gpu_scene, Int32 (samples_per_pixel), Int32 (max_depth), Int32 ( 0 ))
186
192
end
187
193
return Array (gpu_img)
188
194
end
189
195
190
- glass = Trace. GlassMaterial (
191
- Trace. ConstantTexture (Trace. RGBSpectrum (1.0f0 )),
192
- Trace. ConstantTexture (Trace. RGBSpectrum (1.0f0 )),
193
- Trace. ConstantTexture (0.0f0 ),
194
- Trace. ConstantTexture (0.0f0 ),
195
- Trace. ConstantTexture (1.25f0 ),
196
- true ,
197
- )
198
- mirror = Trace. MirrorMaterial (Trace. ConstantTexture (Trace. RGBSpectrum (1.0f0 )))
199
- plastic = Trace. PlasticMaterial (
200
- Trace. ConstantTexture (Trace. RGBSpectrum (0.6399999857f0 , 0.6399999857f0 , 0.6399999857f0 )),
201
- Trace. ConstantTexture (Trace. RGBSpectrum (0.1000000015f0 , 0.1000000015f0 , 0.1000000015f0 )),
202
- Trace. ConstantTexture (0.010408001f0 ),
203
- true ,
204
- )
205
- catmesh = load (Makie. assetpath (" cat.obj" ))
196
+ function render (w:: Whitten5 , scene)
197
+
198
+ end
199
+
200
+ function render_interactive (mscene:: Makie.Scene , ArrayType; max_depth= 5 )
201
+ scene, camera, film = convert_scene (mscene)
202
+ preserve = []
203
+ gpu_scene = Trace. to_gpu (ArrayType, scene; preserve= preserve)
204
+ res = Int .((film. resolution... ,))
205
+ gpu_img = ArrayType (zeros (RGBf, res))
206
+ s = Scene (size= res)
207
+ imgp = image! (s, - 1 .. 1 , - 1 .. 1 , Array (gpu_img))
208
+ display (GLMakie. Screen (), mscene)
209
+ display (GLMakie. Screen (), s)
210
+ cam_start = camera[]
211
+ n_iter = Int32 (1 )
212
+ Base. errormonitor (@async while isopen (s)
213
+ GC. @preserve preserve begin
214
+ if cam_start != camera[]
215
+ cam_start = camera[]
216
+ gpu_img .= RGBf (0 , 0 , 0 )
217
+ end
218
+ Trace. launch_trace_image! (gpu_img, camera[], gpu_scene, Int32 (1 ), Int32 (max_depth), n_iter)
219
+ n_iter += Int32 (1 )
220
+ end
221
+ imgp[3 ] = Array (gpu_img)
222
+ sleep (1 / 10 )
223
+ end )
224
+ return Array (gpu_img)
225
+ end
206
226
207
227
begin
228
+ catmesh = load (Makie. assetpath (" cat.obj" ))
208
229
scene = Scene (size= (1024 , 1024 );
209
230
lights= [AmbientLight (RGBf (0.7 , 0.6 , 0.6 )), PointLight (Vec3f (0 , 1 , 0.5 ), RGBf (1.3 , 1.3 , 1.3 ))]
210
231
)
211
232
cam3d! (scene)
212
233
mesh! (scene, catmesh, color= load (Makie. assetpath (" diffusemap.png" )))
213
234
center! (scene)
214
- @time render_whitted (scene)
235
+ # @time render_whitted(scene)
215
236
# 1.024328 seconds (16.94 M allocations: 5.108 GiB, 46.19% gc time, 81 lock conflicts)
216
237
# 0.913530 seconds (16.93 M allocations: 5.108 GiB, 42.52% gc time, 57 lock conflicts)
217
238
# 0.416158 seconds (75.58 k allocations: 88.646 MiB, 2.44% gc time, 16 lock conflicts)
218
239
@time render_gpu (scene, ArrayType)
219
240
# 0.135438 seconds (76.03 k allocations: 82.406 MiB, 8.57% gc time)
241
+ # render_interactive(scene, ArrayType; max_depth=5)
220
242
end
221
243
222
244
begin
@@ -236,24 +258,31 @@ begin
236
258
# 0.976180 seconds (443.12 k allocations: 107.841 MiB, 6.60% gc time, 12 lock conflicts)
237
259
@time render_gpu (scene, ArrayType)
238
260
# 0.236231 seconds (443.48 k allocations: 101.598 MiB, 3.92% gc time)
261
+ # render_interactive(scene, ArrayType; max_depth=5)
239
262
end
240
263
241
- model = load (joinpath (@__DIR__ , " .." , " src" , " assets" , " models" , " caustic-glass.ply" ))
242
264
begin
265
+ model = load (joinpath (@__DIR__ , " .." , " src" , " assets" , " models" , " caustic-glass.ply" ))
243
266
glass = Trace. GlassMaterial (
244
- Trace. ConstantTexture (Trace. RGBSpectrum (1.0f0 )),
245
- Trace. ConstantTexture (Trace. RGBSpectrum (1.0f0 )),
267
+ Trace. ConstantTexture (Trace. RGBSpectrum (0.9f0 )),
268
+ Trace. ConstantTexture (Trace. RGBSpectrum (0.88f0 )),
246
269
Trace. ConstantTexture (0.0f0 ),
247
270
Trace. ConstantTexture (0.0f0 ),
248
- Trace. ConstantTexture (1.25f0 ),
271
+ Trace. ConstantTexture (1.4f0 ),
249
272
true ,
250
273
)
251
274
plastic = Trace. PlasticMaterial (
252
- Trace. ConstantTexture (Trace. RGBSpectrum (0.6399999857f0 , 0.6399999857f0 , 0.6399999857f0 )),
275
+ Trace. ConstantTexture (Trace. RGBSpectrum (0.6399999857f0 , 0.6399999857f0 , 0.5399999857f0 )),
253
276
Trace. ConstantTexture (Trace. RGBSpectrum (0.1000000015f0 , 0.1000000015f0 , 0.1000000015f0 )),
254
277
Trace. ConstantTexture (0.010408001f0 ),
255
278
true ,
256
279
)
280
+ plastic_ceil = Trace. PlasticMaterial (
281
+ Trace. ConstantTexture (Trace. RGBSpectrum (0.3399999857f0 , 0.6399999857f0 , 0.8399999857f0 )),
282
+ Trace. ConstantTexture (Trace. RGBSpectrum (1.4f0 )),
283
+ Trace. ConstantTexture (0.000408001f0 ),
284
+ true ,
285
+ )
257
286
scene = Scene (size= (1024 , 1024 ); lights= [
258
287
AmbientLight (RGBf (1 , 1 , 1 )),
259
288
PointLight (Vec3f (4 , 4 , 10 ), RGBf (150 , 150 , 150 )),
@@ -265,12 +294,30 @@ begin
265
294
mesh! (scene, model, material= glass)
266
295
mini, maxi = extrema (Rect3f (decompose (Point, model)))
267
296
floorrect = Rect3f (Vec3f (- 10 , mini[2 ], - 10 ), Vec3f (20 , - 1 , 20 ))
268
- mesh! (scene, floorrect, material= plastic)
297
+ mesh! (scene, floorrect, material= plastic_ceil)
298
+ ceiling = Rect3f (Vec3f (- 25 , 11 , - 25 ), Vec3f (50 , - 1 , 50 ))
299
+ mesh! (scene, ceiling, material= plastic)
269
300
center! (scene)
270
301
update_cam! (scene, Vec3f (- 1.6 , 6.2 , 0.2 ), Vec3f (- 3.6 , 2.5 , 2.4 ), Vec3f (0 , 1 , 0 ))
271
302
272
303
@time render_whitted (scene)
273
304
# 9.820304 seconds (1.69 M allocations: 235.165 MiB, 0.51% gc time, 3 lock conflicts)
274
- @time render_gpu (scene, ArrayType)
305
+ # @time render_gpu(scene, ArrayType)
275
306
# 6.128600 seconds (1.70 M allocations: 228.875 MiB, 1.09% gc time)
307
+ # @time render_sppm(scene; iterations=500)
308
+ # @time colorbuffer(scene; backend=RPRMakie)
309
+ # 6.321123 seconds (10.09 k allocations: 66.559 MiB, 0.15% gc time)
310
+ # render_interactive(scene, ArrayType; max_depth=5)
276
311
end
312
+
313
+ @time begin
314
+ tscene, tcamera, tfilm = convert_scene (scene)
315
+ w = Whitten5 (tfilm; samples_per_pixel= 8 )
316
+ end ;
317
+ begin
318
+ @time launch_trace_image! (w, tcamera[], tscene)
319
+ Trace. to_framebuffer! (tfilm, 1.0f0 )
320
+ tfilm. framebuffer
321
+ end
322
+
323
+ # img = render_sppm(scene; iterations=1)
0 commit comments