Skip to content

Commit eafbffe

Browse files
committed
some more fixes
1 parent 7ff9fe3 commit eafbffe

File tree

8 files changed

+74
-60
lines changed

8 files changed

+74
-60
lines changed

src/Trace.jl

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -180,14 +180,14 @@ struct Scene{P<:Primitive, L<:NTuple{N, Light} where N}
180180
lights::L
181181
aggregate::P
182182
bound::Bounds3
183+
end
183184

184-
function Scene(
185-
lights::Vector{L}, aggregate::P,
186-
) where L<:Light where P<:Primitive
187-
# TODO preprocess for lights
188-
ltuple = Tuple(lights)
189-
new{P,typeof(ltuple)}(ltuple, aggregate, world_bound(aggregate))
190-
end
185+
function Scene(
186+
lights::Union{Tuple,AbstractVector}, aggregate::P,
187+
) where {P<:Primitive}
188+
# TODO preprocess for lights
189+
ltuple = Tuple(lights)
190+
Scene{P,typeof(ltuple)}(ltuple, aggregate, world_bound(aggregate))
191191
end
192192

193193
@inline function intersect!(scene::Scene, ray::AbstractRay)

src/gpu-support.jl

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,15 @@ function to_gpu(ArrayType, m::Trace.Texture; preserve=[])
2424
end
2525

2626
function to_gpu(ArrayType, m::Trace.UberMaterial; preserve=[])
27-
@assert !Trace.no_texture(m.Kd)
28-
Kd = to_gpu(ArrayType, m.Kd; preserve=preserve)
29-
no_tex_s = typeof(Kd)()
27+
if !Trace.no_texture(m.Kd)
28+
Kd = to_gpu(ArrayType, m.Kd; preserve=preserve)
29+
no_tex_s = typeof(Kd)()
30+
Kr = Trace.no_texture(m.Kr) ? no_tex_s : to_gpu(ArrayType, m.Kr; preserve=preserve)
31+
else
32+
Kr = to_gpu(ArrayType, m.Kr; preserve=preserve)
33+
no_tex_s = typeof(Kr)()
34+
Kd = Trace.no_texture(m.Kd) ? no_tex_s : to_gpu(ArrayType, m.Kd; preserve=preserve)
35+
end
3036
f_tex = to_gpu(ArrayType, Trace.Texture(ArrayType(zeros(Float32, 1, 1))); preserve=preserve)
3137
no_tex_f = typeof(f_tex)()
3238
return Trace.UberMaterial(
@@ -52,3 +58,8 @@ function to_gpu(ArrayType, bvh::Trace.BVHAccel; preserve=[])
5258
materials = to_gpu(ArrayType, to_gpu.((ArrayType,), bvh.materials; preserve=preserve); preserve=preserve)
5359
return Trace.BVHAccel(primitives, materials, bvh.max_node_primitives, nodes)
5460
end
61+
62+
function to_gpu(ArrayType, scene::Trace.Scene; preserve=[])
63+
bvh = to_gpu(ArrayType, scene.aggregate; preserve=preserve)
64+
return Trace.Scene(scene.lights, bvh, scene.bound)
65+
end

src/integrators/sampler.jl

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

9-
109
function sample_kernel_inner(i::A, scene::B, t_sampler::C, film::D, film_tile::E, camera::F, pixel::G, spp_sqr::H) where {A, B, C, D, E, F, G, H}
11-
while has_next_sample(t_sampler)
10+
for _ in 1:t_sampler.samples_per_pixel
1211
camera_sample = get_camera_sample(t_sampler, pixel)
1312
ray, ω = generate_ray_differential(camera, camera_sample)
1413
ray = scale_differentials(ray, spp_sqr)
1514
l = RGBSpectrum(0f0)
1615
if ω > 0.0f0
17-
l = li(i, ray, scene, 1)
16+
l = li(t_sampler, i.max_depth, ray, scene, 1)
1817
end
1918
# TODO check l for invalid values
2019
if isnan(l)
2120
l = RGBSpectrum(0f0)
2221
end
2322
add_sample!(film, film_tile, camera_sample.film, l, ω)
24-
start_next_sample!(t_sampler)
2523
end
2624
end
2725

2826
@noinline function sample_kernel(i, camera, scene, film, film_tile, tile_bounds)
2927
t_sampler = deepcopy(i.sampler)
3028
spp_sqr = 1f0 / Float32(t_sampler.samples_per_pixel)
3129
for pixel in tile_bounds
32-
start_pixel!(t_sampler, pixel)
3330
sample_kernel_inner(i, scene, t_sampler, film, film_tile, camera, pixel, spp_sqr)
3431
end
3532
merge_film_tile!(film, film_tile)
@@ -73,18 +70,19 @@ function (i::SamplerIntegrator)(scene::Scene, film)
7370
end
7471

7572
function get_material(bvh::BVHAccel, shape::Triangle)
73+
materials = bvh.materials
7674
@inbounds if shape.material_idx == 0
77-
return bvh.materials[1]
75+
return materials[1]
7876
else
79-
return bvh.materials[shape.material_idx]
77+
return materials[shape.material_idx]
8078
end
8179
end
8280
function get_material(scene::Scene, shape::Triangle)
8381
get_material(scene.aggregate, shape)
8482
end
8583

8684
function li(
87-
i::WhittedIntegrator, ray::RayDifferentials, scene::Scene, depth::Int64,
85+
sampler, max_depth, ray::RayDifferentials, scene::Scene, depth::Int64,
8886
)::RGBSpectrum
8987

9088
l = RGBSpectrum(0f0)
@@ -106,64 +104,69 @@ function li(
106104
m = get_material(scene, shape)
107105
if m.type === NO_MATERIAL
108106
return li(
109-
i, RayDifferentials(spawn_ray(si, ray.d)),
107+
sampler, max_depth, RayDifferentials(spawn_ray(si, ray.d)),
110108
scene, depth,
111109
)
112110
end
113111
bsdf = m(si, false, Radiance)
114112
# Compute emitted light if ray hit an area light source.
115113
l += le(si, wo)
116114
# Add contribution of each light source.
117-
for light in scene.lights
118-
sampled_li, wi, pdf, visibility_tester = sample_li(
119-
light, core, get_2d(i.sampler),
120-
)
121-
(is_black(sampled_li) || pdf 0f0) && continue
122-
f = bsdf(wo, wi)
123-
if !is_black(f) && unoccluded(visibility_tester, scene)
124-
l += f * sampled_li * abs(wi n) / pdf
115+
lights = scene.lights
116+
Base.Cartesian.@nexprs 8 i -> begin
117+
if i <= length(lights)
118+
light = lights[i]
119+
sampled_li, wi, pdf, visibility_tester = sample_li(
120+
light, core, get_2d(sampler),
121+
)
122+
if !(is_black(sampled_li) || pdf 0f0)
123+
f = bsdf(wo, wi)
124+
if !is_black(f) && unoccluded(visibility_tester, scene)
125+
l += f * sampled_li * abs(wi n) / pdf
126+
end
127+
end
125128
end
126129
end
127-
if depth + 1 i.max_depth
130+
if depth + 1 max_depth
128131
# Trace rays for specular reflection & refraction.
129-
l += specular_reflect(bsdf, i, ray, si, scene, depth)
130-
l += specular_transmit(bsdf, i, ray, si, scene, depth)
132+
l += specular_reflect(bsdf, sampler, max_depth, ray, si, scene, depth)
133+
l += specular_transmit(bsdf, sampler, max_depth, ray, si, scene, depth)
131134
end
132135
l
133136
end
134137

135-
function specular_reflect(
136-
bsdf, i::I, ray::RayDifferentials,
137-
surface_intersect::SurfaceInteraction, scene::Scene, depth::Int64,
138-
) where I<:SamplerIntegrator
138+
@inline function specular_reflect(
139+
bsdf, sampler, max_depth, ray::RayDifferentials,
140+
si::SurfaceInteraction, scene::Scene, depth::Int64,
141+
)
139142

140143
# Compute specular reflection direction `wi` and BSDF value.
141144

142-
wo = surface_intersect.core.wo
145+
wo = si.core.wo
143146
type = BSDF_REFLECTION | BSDF_SPECULAR
144147
wi, f, pdf, sampled_type = sample_f(
145-
bsdf, wo, get_2d(i.sampler), type,
148+
bsdf, wo, get_2d(sampler), type,
146149
)
147150
# Return contribution of specular reflection.
148-
ns = surface_intersect.shading.n
151+
ns = si.shading.n
149152
if !(pdf > 0f0 && !is_black(f) && abs(wi ns) != 0f0)
150153
return RGBSpectrum(0f0)
151154
end
152155
# Compute ray differential for specular reflection.
153-
rd = RayDifferentials(spawn_ray(surface_intersect, wi))
156+
rd = RayDifferentials(spawn_ray(si, wi))
154157
if ray.has_differentials
155-
rx_origin = surface_intersect.core.p + surface_intersect.∂p∂x
156-
ry_origin = surface_intersect.core.p + surface_intersect.∂p∂y
158+
rx_origin = si.core.p + si.∂p∂x
159+
ry_origin = si.core.p + si.∂p∂y
157160
# Compute differential reflected directions.
158161
∂n∂x = (
159-
surface_intersect.shading.∂n∂u * surface_intersect.∂u∂x
162+
si.shading.∂n∂u * si.∂u∂x
160163
+
161-
surface_intersect.shading.∂n∂v * surface_intersect.∂v∂x
164+
si.shading.∂n∂v * si.∂v∂x
162165
)
163166
∂n∂y = (
164-
surface_intersect.shading.∂n∂u * surface_intersect.∂u∂y
167+
si.shading.∂n∂u * si.∂u∂y
165168
+
166-
surface_intersect.shading.∂n∂v * surface_intersect.∂v∂y
169+
si.shading.∂n∂v * si.∂v∂y
167170
)
168171
∂wo∂x = -ray.rx_direction - wo
169172
∂wo∂y = -ray.ry_direction - wo
@@ -173,19 +176,19 @@ function specular_reflect(
173176
ry_direction = wi - ∂wo∂y + 2f0 * (wo ns) * ∂n∂y + ∂dn∂y * ns
174177
rd = RayDifferentials(rd, rx_origin=rx_origin, ry_origin=ry_origin, rx_direction=rx_direction, ry_direction=ry_direction)
175178
end
176-
return f * li(i, rd, scene, depth + 1) * abs(wi ns) / pdf
179+
return f * li(sampler, max_depth, rd, scene, depth + 1) * abs(wi ns) / pdf
177180
end
178181

179-
function specular_transmit(
180-
bsdf, i::S, ray::RayDifferentials,
182+
@inline function specular_transmit(
183+
bsdf, sampler, max_depth, ray::RayDifferentials,
181184
surface_intersect::SurfaceInteraction, scene::Scene, depth::Int64,
182-
) where S<:SamplerIntegrator
185+
)
183186

184187
# Compute specular reflection direction `wi` and BSDF value.
185188
wo = surface_intersect.core.wo
186189
type = BSDF_TRANSMISSION | BSDF_SPECULAR
187190
wi, f, pdf, sampled_type = sample_f(
188-
bsdf, wo, get_2d(i.sampler), type,
191+
bsdf, wo, get_2d(sampler), type,
189192
)
190193

191194
ns = surface_intersect.shading.n
@@ -223,12 +226,12 @@ function specular_transmit(
223226
∂dn∂x = ∂wo∂x ns + wo ∂n∂x
224227
∂dn∂y = ∂wo∂y ns + wo ∂n∂y
225228
μ = η * (wo ns) - abs(wi ns)
226-
ν = η -^2 * (wo ns)) / abs(wi ns)
229+
ν = η - * η * (wo ns)) / abs(wi ns)
227230
∂μ∂x = ν * ∂dn∂x
228231
∂μ∂y = ν * ∂dn∂y
229232
rx_direction = wi - η * ∂wo∂x + μ * ∂n∂x + ∂μ∂x * ns
230233
ry_direction = wi - η * ∂wo∂y + μ * ∂n∂y + ∂μ∂y * ns
231234
rd = RayDifferentials(rd, rx_origin=rx_origin, ry_origin=ry_origin, rx_direction=rx_direction, ry_direction=ry_direction)
232235
end
233-
f * li(i, rd, scene, depth + 1) * abs(wi ns) / pdf
236+
f * li(sampler, max_depth, rd, scene, depth + 1) * abs(wi ns) / pdf
234237
end

src/materials/bsdf.jl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,7 @@ function sample_f(
142142
# Get BxDF for chosen component.
143143
count = component
144144
component -= 1
145-
bxdf = nothing
145+
bxdf = UberBxDF{RGBSpectrum}()
146146
bxdfs = b.bxdfs
147147
Base.Cartesian.@nexprs 8 i -> begin
148148
if i <= bxdfs.last
@@ -155,7 +155,7 @@ function sample_f(
155155
end
156156
end
157157
end
158-
@real_assert !isnothing(bxdf) "n bxdfs $(b.n_bxdfs), component $component, count $count"
158+
@real_assert bxdf.active "n bxdfs $(b.n_bxdfs), component $component, count $count"
159159
# Remap BxDF sample u to [0, 1)^2.
160160
u_remapped = Point2f(
161161
min(u[1] * matching_components - component, 1f0), u[2],

src/materials/material.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
MATTE_MATERIAL = UInt8(1)
1+
const MATTE_MATERIAL = UInt8(1)
22

33

44
"""

src/reflection/microfacet.jl

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -76,8 +76,8 @@ correspond to near-perfect specular reflection, rather than by specifying
7676
@inline function roughness_to_α(roughness::Float32)::Float32
7777
roughness = max(1f-3, roughness)
7878
x = log(roughness)
79-
1.62142f0 + 0.819955f0 * x + 0.1734f0 * x^2 +
80-
0.0171201f0 * x^3 + 0.000640711f0 * x^4
79+
1.62142f0 + 0.819955f0 * x + 0.1734f0 * x*x +
80+
0.0171201f0 * x*x*x + 0.000640711f0 * x*x*x*x
8181
end
8282

8383
@inline function G1(m::MicrofacetDistribution, w::Vec3f)::Float32
@@ -273,7 +273,7 @@ function distribution_microfacet_transmission(m::UberBxDF{S}, wo::Vec3f, wi::Vec
273273
)
274274
end
275275

276-
@inline function sample_microfacet_transmission(m::UberBxDF{S}, wo::Vec3f, u::Point2f)::S where {S<:Spectrum}
276+
@inline function sample_microfacet_transmission(m::UberBxDF{S}, wo::Vec3f, u::Point2f) where {S<:Spectrum}
277277

278278
wo[3] 0 && return Vec3f(0f0), 0f0, S(0f0), UInt8(0)
279279
wh = sample_wh(m.distribution, wo, u)

src/sampler/sampler.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -155,7 +155,7 @@ function get_2d(ps::PixelSampler)::Point2f
155155
end
156156

157157

158-
mutable struct UniformSampler <: AbstractSampler
158+
struct UniformSampler <: AbstractSampler
159159
current_sample::Int64
160160
samples_per_pixel::Int64
161161
UniformSampler(samples_per_pixel::Integer) = new(1, samples_per_pixel)

src/textures/basic.jl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,13 +26,13 @@ function Base.convert(::Type{Texture{ElType,N,T}}, ::NoTexture) where {ElType,N,
2626
end
2727

2828
function (c::Texture{T})(si::SurfaceInteraction)::T where {T<:TextureType}
29-
@inbounds if c.isconst
29+
if c.isconst
3030
return c.const_value
3131
else
3232
uv = Vec2f(1f0 - si.uv[2], si.uv[1])
3333
s = unsafe_trunc.(Int32, size(c.data))
3434
idx = map(x -> unsafe_trunc(Int32, x), Int32(1) .+ ((s .- Int32(1)) .* uv))
3535
idx = clamp.(idx, Int32(1), s)
36-
return c.data[idx...]
36+
@inbounds return c.data[idx...]
3737
end
3838
end

0 commit comments

Comments
 (0)