Skip to content

Commit 2e06678

Browse files
committed
fix missaligned pointer
1 parent 89d6db7 commit 2e06678

File tree

1 file changed

+27
-49
lines changed

1 file changed

+27
-49
lines changed

src/integrators/sampler.jl

Lines changed: 27 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -322,62 +322,37 @@ struct Reflect end
322322
struct Transmit end
323323

324324

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))))
368329
end
369-
return accumulated_l
330+
return expr
370331
end
371332

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
372346

373347
@inline function li_iterative(
374348
sampler, max_depth, initial_ray::RayDifferentials, scene::Scene
375349
)::RGBSpectrum
376350

377351
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))
379354
pos = Int32(1)
380-
stack[pos] = (initial_ray, Int32(0), accumulated_l)
355+
# stack[pos] = (initial_ray, Int32(0), accumulated_l)
381356
@inbounds while pos > Int32(0)
382357
(ray, depth, accumulated_l) = stack[pos]
383358
pos -= Int32(1)
@@ -399,7 +374,8 @@ end
399374
if m.type === NO_MATERIAL
400375
new_ray = RayDifferentials(spawn_ray(si, ray.d))
401376
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)
403379
continue
404380
end
405381

@@ -411,12 +387,14 @@ end
411387
rd_reflect, reflect_l = specular(Reflect, bsdf, sampler, ray, si)
412388
if rd_reflect !== ray && pos < 8
413389
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)
415392
end
416393
rd_transmit, transmit_l = specular(Transmit, bsdf, sampler, ray, si)
417394
if rd_transmit !== ray && pos < 8
418395
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)
420398
end
421399
end
422400
end

0 commit comments

Comments
 (0)