@@ -322,62 +322,37 @@ struct Reflect end
322
322
struct Transmit end
323
323
324
324
325
- function li_iterative (
326
- sampler, max_depth, initial_ray:: RayDifferentials , scene:: Scene
327
- ):: RGBSpectrum
328
-
329
- accumulated_l = RGBSpectrum (0.0f0 )
330
- stack = [(initial_ray, Int32 (0 ), accumulated_l)]
331
- while ! isempty (stack)
332
- (ray, depth, accumulated_l) = pop! (stack)
333
- if depth == sampler
334
- continue
335
- end
336
- hit, shape, si = intersect! (scene, ray)
337
- lights = scene. lights
338
-
339
- if ! hit
340
- accumulated_l += only_light (lights, ray)
341
- continue
342
- end
343
-
344
- core = si. core
345
- wo = core. wo
346
- si = compute_differentials (si, ray)
347
- m = get_material (scene, shape)
348
- if m. type === NO_MATERIAL
349
- new_ray = RayDifferentials (spawn_ray (si, ray. d))
350
- push! (stack, (new_ray, depth, accumulated_l))
351
- continue
352
- end
353
-
354
- bsdf = m (si, false , Radiance)
355
- accumulated_l += le (si, wo)
356
- accumulated_l = light_contribution (accumulated_l, lights, wo, scene, bsdf, sampler, si)
357
-
358
- if depth + 1 ≤ max_depth
359
- rd_reflect, reflect_l = specular (Reflect, bsdf, sampler, ray, si)
360
- if rd_reflect != = ray
361
- push! (stack, (rd_reflect, depth + Int32 (1 ), reflect_l * accumulated_l))
362
- end
363
- rd_transmit, transmit_l = specular (Transmit, bsdf, sampler, ray, si)
364
- if rd_transmit != = ray
365
- push! (stack, (rd_transmit, depth + Int32 (1 ), transmit_l * accumulated_l))
366
- end
367
- end
325
+ macro ntuple (N, value)
326
+ expr = :(())
327
+ for i in 1 : N
328
+ push! (expr. args, :($ (esc (value))))
368
329
end
369
- return accumulated_l
330
+ return expr
370
331
end
371
332
333
+ macro setindex (N, setindex_expr)
334
+ @assert Meta. isexpr (setindex_expr, :(= ))
335
+ index_expr = setindex_expr. args[1 ]
336
+ @assert Meta. isexpr (index_expr, :ref )
337
+ tuple = index_expr. args[1 ]
338
+ idx = index_expr. args[2 ]
339
+ value = setindex_expr. args[2 ]
340
+ expr = :(())
341
+ for i in 1 : N
342
+ push! (expr. args, :(ifelse ($ i != $ (esc (idx)), $ (esc (tuple))[$ i], $ (esc (value)))))
343
+ end
344
+ return :($ (esc (tuple)) = $ expr)
345
+ end
372
346
373
347
@inline function li_iterative (
374
348
sampler, max_depth, initial_ray:: RayDifferentials , scene:: Scene
375
349
):: RGBSpectrum
376
350
377
351
accumulated_l = RGBSpectrum (0.0f0 )
378
- stack = MVector {8,Tuple{Trace.RayDifferentials,Int32,Trace.RGBSpectrum}} (undef)
352
+ # stack = MVector{8,Tuple{Trace.RayDifferentials,Int32,Trace.RGBSpectrum}}(undef)
353
+ stack = @ntuple (8 , (initial_ray, Int32 (0 ), accumulated_l))
379
354
pos = Int32 (1 )
380
- stack[pos] = (initial_ray, Int32 (0 ), accumulated_l)
355
+ # stack[pos] = (initial_ray, Int32(0), accumulated_l)
381
356
@inbounds while pos > Int32 (0 )
382
357
(ray, depth, accumulated_l) = stack[pos]
383
358
pos -= Int32 (1 )
399
374
if m. type === NO_MATERIAL
400
375
new_ray = RayDifferentials (spawn_ray (si, ray. d))
401
376
pos += Int32 (1 )
402
- stack[pos] = (new_ray, depth, accumulated_l)
377
+ @setindex 8 stack[pos] = (new_ray, depth, accumulated_l)
378
+ # stack[pos] = (new_ray, depth, accumulated_l)
403
379
continue
404
380
end
405
381
@@ -411,12 +387,14 @@ end
411
387
rd_reflect, reflect_l = specular (Reflect, bsdf, sampler, ray, si)
412
388
if rd_reflect != = ray && pos < 8
413
389
pos += Int32 (1 )
414
- stack[pos] = (rd_reflect, depth + Int32 (1 ), reflect_l * accumulated_l)
390
+ @setindex 8 stack[pos] = (rd_reflect, depth + Int32 (1 ), reflect_l * accumulated_l)
391
+ # stack[pos] = (rd_reflect, depth + Int32(1), reflect_l * accumulated_l)
415
392
end
416
393
rd_transmit, transmit_l = specular (Transmit, bsdf, sampler, ray, si)
417
394
if rd_transmit != = ray && pos < 8
418
395
pos += Int32 (1 )
419
- stack[pos] = (rd_transmit, depth + Int32 (1 ), transmit_l * accumulated_l)
396
+ @setindex 8 stack[pos] = (rd_transmit, depth + Int32 (1 ), transmit_l * accumulated_l)
397
+ # stack[pos] = (rd_transmit, depth + Int32(1), transmit_l * accumulated_l)
420
398
end
421
399
end
422
400
end
0 commit comments