Skip to content

Commit a153620

Browse files
committed
remove pool
1 parent 2711fdb commit a153620

File tree

19 files changed

+194
-199
lines changed

19 files changed

+194
-199
lines changed

src/Trace.jl

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -189,30 +189,30 @@ struct Scene{P<:Primitive, L<:NTuple{N, Light} where N}
189189
end
190190
end
191191

192-
@inline function intersect!(pool, scene::Scene, ray::Union{Ray,RayDifferentials})
193-
intersect!(pool, scene.aggregate, ray)
192+
@inline function intersect!(scene::Scene, ray::AbstractRay)
193+
intersect!(scene.aggregate, ray)
194194
end
195-
@inline function intersect_p(pool, scene::Scene, ray::Union{Ray,RayDifferentials})
196-
intersect_p(pool, scene.aggregate, ray)
195+
@inline function intersect_p(scene::Scene, ray::AbstractRay)
196+
intersect_p(scene.aggregate, ray)
197197
end
198198

199199
@inline function spawn_ray(
200-
pool, p0::Interaction, p1::Interaction, δ::Float32 = 1f-6,
201-
)::Ray
200+
p0::Interaction, p1::Interaction, δ::Float32 = 1f-6,
201+
)::Ray
202202
direction = p1.p - p0.p
203203
origin = p0.p .+ δ .* direction
204-
return allocate(pool, Ray, (origin, direction, Inf32, p0.time))
204+
return Ray(origin, direction, Inf32, p0.time)
205205
end
206206

207-
@inline function spawn_ray(pool, p0::SurfaceInteraction, p1::Interaction)::Ray
208-
spawn_ray(pool, p0.core, p1)
207+
@inline function spawn_ray(p0::SurfaceInteraction, p1::Interaction)::Ray
208+
spawn_ray(p0.core, p1)
209209
end
210210

211211
@inline function spawn_ray(
212-
pool, si::SurfaceInteraction, direction::Vec3f, δ::Float32 = 1f-6,
212+
si::SurfaceInteraction, direction::Vec3f, δ::Float32 = 1f-6,
213213
)::Ray
214214
origin = si.core.p .+ δ .* direction
215-
return default(pool, Ray; o=origin, d=direction, time=si.core.time)
215+
return Ray(o=origin, d=direction, time=si.core.time)
216216
end
217217

218218
include("shapes/Shape.jl")

src/accel/bvh.jl

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -210,12 +210,12 @@ end
210210
length(bvh.nodes) > 0 ? bvh.nodes[1].bounds : Bounds3()
211211
end
212212

213-
function intersect!(pool, bvh::BVHAccel{P}, ray::MutableRef{<:AbstractRay})::Tuple{Bool, P, SurfaceInteraction} where P
213+
function intersect!(bvh::BVHAccel{P}, ray::AbstractRay)::Tuple{Bool,P,SurfaceInteraction} where {P}
214214
hit = false
215215
interaction = SurfaceInteraction()
216216
isempty(bvh.nodes) && return hit, nothing, interaction
217217

218-
check_direction!(ray)
218+
ray = check_direction(ray)
219219
inv_dir = 1f0 ./ ray.d
220220
dir_is_neg = is_dir_negative(ray.d)
221221

@@ -227,13 +227,13 @@ function intersect!(pool, bvh::BVHAccel{P}, ray::MutableRef{<:AbstractRay})::Tup
227227
primitive::P = first(bvh.primitives)
228228
@inbounds while true
229229
ln = bvh.nodes[current_node_i]
230-
if intersect_p(pool, ln.bounds, ray, inv_dir, dir_is_neg)
230+
if intersect_p(ln.bounds, ray, inv_dir, dir_is_neg)
231231
if ln isa LinearBVHLeaf && ln.n_primitives > 0
232232
# Intersect ray with primitives in node.
233233
for i in 0:ln.n_primitives-1
234234
tmp_primitive = bvh.primitives[ln.primitives_offset+i]
235-
tmp_hit, tmp_interaction = intersect_p!(
236-
pool, tmp_primitive, ray,
235+
tmp_hit, ray, tmp_interaction = intersect_p!(
236+
tmp_primitive, ray,
237237
)
238238
if tmp_hit && !(tmp_interaction isa Vec3)
239239
hit = tmp_hit
@@ -263,10 +263,10 @@ function intersect!(pool, bvh::BVHAccel{P}, ray::MutableRef{<:AbstractRay})::Tup
263263
return hit, primitive, interaction
264264
end
265265

266-
function intersect_p(pool, bvh::BVHAccel, ray::MutableRef{<:AbstractRay})
266+
function intersect_p(bvh::BVHAccel, ray::AbstractRay)
267267
length(bvh.nodes) == 0 && return false
268268

269-
check_direction!(ray)
269+
ray = check_direction(ray)
270270
inv_dir = 1f0 ./ ray.d
271271
dir_is_neg = is_dir_negative(ray.d)
272272

@@ -276,11 +276,11 @@ function intersect_p(pool, bvh::BVHAccel, ray::MutableRef{<:AbstractRay})
276276

277277
while true
278278
ln = bvh.nodes[current_node_i]
279-
if intersect_p(pool, ln.bounds, ray, inv_dir, dir_is_neg)
279+
if intersect_p(ln.bounds, ray, inv_dir, dir_is_neg)
280280
if ln isa LinearBVHLeaf && ln.n_primitives > 0
281281
for i in 0:ln.n_primitives-1
282282
intersect_p(
283-
pool, bvh.primitives[ln.primitives_offset+i], ray,
283+
bvh.primitives[ln.primitives_offset+i], ray,
284284
) && return true
285285
end
286286
to_visit_offset == 1 && break

src/bounds.jl

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,7 @@ function bounding_sphere(b::Bounds3)::Tuple{Point3f,Float32}
152152
center, radius
153153
end
154154

155-
function intersect(b::Bounds3, ray::MutableRef{<:AbstractRay})::Tuple{Bool,Float32,Float32}
155+
function intersect(b::Bounds3, ray::AbstractRay)::Tuple{Bool,Float32,Float32}
156156
t0, t1 = 0f0, ray.t_max
157157
@inbounds for i in 1:3
158158
# Update interval for i-th bbox slab.
@@ -182,9 +182,9 @@ end
182182
dir_is_negative: 1 -- false, 2 -- true
183183
"""
184184
function intersect_p(
185-
pool, b::Bounds3, ray::MutableRef{<:AbstractRay},
186-
inv_dir::Vec3f, dir_is_negative::Point3{UInt8},
187-
)::Bool
185+
b::Bounds3, ray::AbstractRay,
186+
inv_dir::Vec3f, dir_is_negative::Point3{UInt8},
187+
)::Bool
188188
tx_min = (b[dir_is_negative[1]][1] - ray.o[1]) * inv_dir[1]
189189
tx_max = (b[3-dir_is_negative[1]][1] - ray.o[1]) * inv_dir[1]
190190
ty_min = (b[dir_is_negative[2]][2] - ray.o[2]) * inv_dir[2]

src/camera/camera.jl

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -37,30 +37,30 @@ set this value to indicate how much light carries through the lenses,
3737
based on their optical properties.
3838
"""
3939
function generate_ray(
40-
camera::C, sample::CameraSample,
41-
)::Tuple{Ray,Float32} where C<:Camera
40+
camera::C, sample::CameraSample,
41+
)::Tuple{Ray,Float32} where C<:Camera
4242
end
4343
"""
4444
Same as `generate_ray`, but also computes rays for pixels shifted one pixel
4545
in x & y directions on the film plane.
4646
Useful for anti-aliasing textures.
4747
"""
4848
function generate_ray_differential(
49-
pool::MemoryPool,
50-
camera::C, sample::CameraSample,
51-
)::Tuple{RayDifferentials,Float32} where C<:Camera
52-
ray, wt = generate_ray(pool, camera, sample)
49+
camera::C, sample::CameraSample,
50+
)::Tuple{RayDifferentials,Float32} where C<:Camera
51+
52+
ray, wt = generate_ray(camera, sample)
5353
shifted_x = CameraSample(
5454
sample.film + Point2f(1f0, 0f0), sample.lens, sample.time,
5555
)
5656
shifted_y = CameraSample(
5757
sample.film + Point2f(0f0, 1f0), sample.lens, sample.time,
5858
)
59-
ray_x, wt_x = generate_ray(pool, camera, shifted_x)
60-
ray_y, wt_y = generate_ray(pool, camera, shifted_y)
61-
rayd = allocate(pool, RayDifferentials,
62-
(ray.o, ray.d, ray.t_max, ray.time,
63-
true, ray_x.o, ray_y.o, ray_x.d, ray_y.d)
59+
ray_x, wt_x = generate_ray(camera, shifted_x)
60+
ray_y, wt_y = generate_ray(camera, shifted_y)
61+
rayd = RayDifferentials(
62+
ray.o, ray.d, ray.t_max, ray.time,
63+
true, ray_x.o, ray_y.o, ray_x.d, ray_y.d
6464
)
6565
rayd, wt
6666
end

src/camera/perspective.jl

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -82,15 +82,13 @@ end
8282

8383
@inline get_film(c::PerspectiveCamera)::Film = c.core.core.film
8484

85-
function generate_ray(
86-
pool::MemoryPool,
87-
camera::PerspectiveCamera, sample::CameraSample,
88-
)::Tuple{Ray,Float32}
85+
@inline function generate_ray(
86+
camera::PerspectiveCamera, sample::CameraSample,
87+
)::Tuple{Ray,Float32}
8988
# Compute raster & camera sample positions.
9089
p_film = Point3f(sample.film[1], sample.film[2], 0f0)
9190
p_camera = camera.core.raster_to_camera(p_film)
92-
93-
ray = default(pool, Ray; o=Point3f(0), d=normalize(Vec3f(p_camera)))
91+
ray = Ray(o=Point3f(0), d=normalize(Vec3f(p_camera)))
9492
# Modify ray for depth of field.
9593
if camera.core.lens_radius > 0
9694
# Sample points on lens.
@@ -103,13 +101,13 @@ function generate_ray(
103101
ray.d = normalize(Vec3f(p_focus - ray.o))
104102
end
105103

106-
ray.time = lerp(
104+
time = lerp(
107105
camera.core.core.shutter_open,
108106
camera.core.core.shutter_close,
109107
sample.time,
110108
)
111109
# TODO add medium
112-
apply!(camera.core.core.camera_to_world, ray)
113-
ray.d = normalize(ray.d)
110+
ray = apply(camera.core.core.camera_to_world, ray)
111+
Ray(ray, d=normalize(ray.d))
114112
ray, 1f0
115113
end

src/integrators/sampler.jl

Lines changed: 30 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,14 @@ struct WhittedIntegrator{C<: Camera, S <: AbstractSampler} <: SamplerIntegrator
66
max_depth::Int64
77
end
88

9-
@noinline function sample_kernel_inner(pool, i, scene, t_sampler, film, film_tile, camera, pixel, spp_sqr)
9+
@noinline function sample_kernel_inner(i, scene, t_sampler, film, film_tile, camera, pixel, spp_sqr)
1010
while has_next_sample(t_sampler)
11-
free_all(pool) # clear memory pool
1211
camera_sample = get_camera_sample(t_sampler, pixel)
13-
ray, ω = generate_ray_differential(pool, camera, camera_sample)
14-
scale_differentials!(ray, spp_sqr)
12+
ray, ω = generate_ray_differential(camera, camera_sample)
13+
ray = scale_differentials(ray, spp_sqr)
1514
l = RGBSpectrum(0f0)
1615
if ω > 0.0f0
17-
l = li(pool, i, ray, scene, 1)
16+
l = li(i, ray, scene, 1)
1817
end
1918
# TODO check l for invalid values
2019
if isnan(l)
@@ -25,14 +24,12 @@ end
2524
end
2625
end
2726

28-
@noinline function sample_kernel(mempools, i, camera, scene, film, film_tile, tile_bounds)
29-
30-
pool = mempools[Threads.threadid()]
27+
@noinline function sample_kernel(i, camera, scene, film, film_tile, tile_bounds)
3128
t_sampler = deepcopy(i.sampler)
3229
spp_sqr = 1f0 / Float32(t_sampler.samples_per_pixel)
3330
for pixel in tile_bounds
3431
start_pixel!(t_sampler, pixel)
35-
sample_kernel_inner(pool, i, scene, t_sampler, film, film_tile, camera, pixel, spp_sqr)
32+
sample_kernel_inner(i, scene, t_sampler, film, film_tile, camera, pixel, spp_sqr)
3633
end
3734
merge_film_tile!(film, film_tile)
3835
end
@@ -51,7 +48,6 @@ function (i::SamplerIntegrator)(scene::Scene)
5148
total_tiles = width * height - 1
5249
bar = Progress(total_tiles, 1)
5350
@info "Utilizing $(Threads.nthreads()) threads"
54-
mempools = [MemoryPool(round(Int, 3*16384)) for _ in 1:Threads.maxthreadid()]
5551
film = get_film(i.camera)
5652
camera = i.camera
5753
filter_radius = film.filter.radius
@@ -70,20 +66,20 @@ function (i::SamplerIntegrator)(scene::Scene)
7066
tile_bounds = Bounds2(tb_min, tb_max)
7167
film_tile = filmtiles[Threads.threadid()]
7268
film_tile = update_bounds!(film, film_tile, tile_bounds)
73-
sample_kernel(mempools, i, camera, scene, film, film_tile, tile_bounds)
69+
sample_kernel(i, camera, scene, film, film_tile, tile_bounds)
7470
end
7571
next!(bar)
7672
end
7773
save(film)
7874
end
7975

8076
function li(
81-
pool, i::WhittedIntegrator, ray::RayDifferentials, scene::Scene, depth::Int64,
77+
i::WhittedIntegrator, ray::RayDifferentials, scene::Scene, depth::Int64,
8278
)::RGBSpectrum
8379

8480
l = RGBSpectrum(0f0)
8581
# Find closest ray intersection or return background radiance.
86-
hit, primitive, si = intersect!(pool, scene, ray)
82+
hit, primitive, si = intersect!(scene, ray)
8783
if !hit
8884
for light in scene.lights
8985
l += le(light, ray)
@@ -96,10 +92,10 @@ function li(
9692
n = si.shading.n
9793
wo = core.wo
9894
# Compute scattering functions for surface interaction.
99-
si, bsdf = compute_scattering!(pool, primitive, si, ray)
95+
si, bsdf = compute_scattering!(primitive, si, ray)
10096
if bsdf.bxdfs.last == 0
10197
return li(
102-
pool, spawn_ray(pool, si, ray.d),
98+
spawn_ray(si, ray.d),
10399
scene, i.sampler, depth,
104100
)
105101
end
@@ -108,7 +104,7 @@ function li(
108104
# Add contribution of each light source.
109105
for light in scene.lights
110106
sampled_li, wi, pdf, visibility_tester = sample_li(
111-
pool, light, core, get_2d(i.sampler),
107+
light, core, get_2d(i.sampler),
112108
)
113109
(is_black(sampled_li) || pdf 0f0) && continue
114110
f = bsdf(wo, wi)
@@ -118,14 +114,14 @@ function li(
118114
end
119115
if depth + 1 i.max_depth
120116
# Trace rays for specular reflection & refraction.
121-
l += specular_reflect(pool, bsdf, i, ray, si, scene, depth)
122-
l += specular_transmit(pool, bsdf, i, ray, si, scene, depth)
117+
l += specular_reflect(bsdf, i, ray, si, scene, depth)
118+
l += specular_transmit(bsdf, i, ray, si, scene, depth)
123119
end
124120
l
125121
end
126122

127123
function specular_reflect(
128-
pool, bsdf, i::I, ray::RayDifferentials,
124+
bsdf, i::I, ray::RayDifferentials,
129125
surface_intersect::SurfaceInteraction, scene::Scene, depth::Int64,
130126
) where I<:SamplerIntegrator
131127

@@ -142,11 +138,10 @@ function specular_reflect(
142138
return RGBSpectrum(0f0)
143139
end
144140
# Compute ray differential for specular reflection.
145-
rd = allocate(pool, RayDifferentials, spawn_ray(pool, surface_intersect, wi))
141+
rd = RayDifferentials(spawn_ray(surface_intersect, wi))
146142
if ray.has_differentials
147-
rd.has_differentials = true
148-
rd.rx_origin = surface_intersect.core.p + surface_intersect.∂p∂x
149-
rd.ry_origin = surface_intersect.core.p + surface_intersect.∂p∂y
143+
rx_origin = surface_intersect.core.p + surface_intersect.∂p∂x
144+
ry_origin = surface_intersect.core.p + surface_intersect.∂p∂y
150145
# Compute differential reflected directions.
151146
∂n∂x = (
152147
surface_intersect.shading.∂n∂u * surface_intersect.∂u∂x
@@ -162,14 +157,15 @@ function specular_reflect(
162157
∂wo∂y = -ray.ry_direction - wo
163158
∂dn∂x = ∂wo∂x ns + wo ∂n∂x
164159
∂dn∂y = ∂wo∂y ns + wo ∂n∂y
165-
rd.rx_direction = wi - ∂wo∂x + 2f0 * (wo ns) * ∂n∂x + ∂dn∂x * ns
166-
rd.ry_direction = wi - ∂wo∂y + 2f0 * (wo ns) * ∂n∂y + ∂dn∂y * ns
160+
rx_direction = wi - ∂wo∂x + 2f0 * (wo ns) * ∂n∂x + ∂dn∂x * ns
161+
ry_direction = wi - ∂wo∂y + 2f0 * (wo ns) * ∂n∂y + ∂dn∂y * ns
162+
rd = RayDifferentials(rd, rx_origin=rx_origin, ry_origin=ry_origin, rx_direction=rx_direction, ry_direction=ry_direction)
167163
end
168-
return f * li(pool, i, rd, scene, depth + 1) * abs(wi ns) / pdf
164+
return f * li(i, rd, scene, depth + 1) * abs(wi ns) / pdf
169165
end
170166

171167
function specular_transmit(
172-
pool, bsdf, i::S, ray::RayDifferentials,
168+
bsdf, i::S, ray::RayDifferentials,
173169
surface_intersect::SurfaceInteraction, scene::Scene, depth::Int64,
174170
) where S<:SamplerIntegrator
175171

@@ -185,11 +181,10 @@ function specular_transmit(
185181
return RGBSpectrum(0f0)
186182
end
187183
# TODO shift in ray direction instead of normal?
188-
rd = allocate(pool, RayDifferentials, spawn_ray(pool, surface_intersect, wi))
184+
rd = RayDifferentials(spawn_ray(surface_intersect, wi))
189185
if ray.has_differentials
190-
rd.has_differentials = true
191-
rd.rx_origin = surface_intersect.core.p + surface_intersect.∂p∂x
192-
rd.ry_origin = surface_intersect.core.p + surface_intersect.∂p∂y
186+
rx_origin = surface_intersect.core.p + surface_intersect.∂p∂x
187+
ry_origin = surface_intersect.core.p + surface_intersect.∂p∂y
193188
# Compute differential transmitted directions.
194189
∂n∂x = (
195190
surface_intersect.shading.∂n∂u * surface_intersect.∂u∂x
@@ -219,8 +214,9 @@ function specular_transmit(
219214
ν = η -^2 * (wo ns)) / abs(wi ns)
220215
∂μ∂x = ν * ∂dn∂x
221216
∂μ∂y = ν * ∂dn∂y
222-
rd.rx_direction = wi - η * ∂wo∂x + μ * ∂n∂x + ∂μ∂x * ns
223-
rd.ry_direction = wi - η * ∂wo∂y + μ * ∂n∂y + ∂μ∂y * ns
217+
rx_direction = wi - η * ∂wo∂x + μ * ∂n∂x + ∂μ∂x * ns
218+
ry_direction = wi - η * ∂wo∂y + μ * ∂n∂y + ∂μ∂y * ns
219+
rd = RayDifferentials(rd, rx_origin=rx_origin, ry_origin=ry_origin, rx_direction=rx_direction, ry_direction=ry_direction)
224220
end
225-
f * li(pool, i, rd, scene, depth + 1) * abs(wi ns) / pdf
221+
f * li(i, rd, scene, depth + 1) * abs(wi ns) / pdf
226222
end

0 commit comments

Comments
 (0)