Skip to content

Commit b935524

Browse files
authored
Merge pull request #30 from pxl-th/la/pow_issue
La/pow issue
2 parents 2241e61 + a2d6332 commit b935524

File tree

9 files changed

+45
-23
lines changed

9 files changed

+45
-23
lines changed

docs/Project.toml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,15 @@
11
[deps]
2+
BenchmarkTools = "6e4b80f9-dd63-53aa-95a3-0cdb28fa8baf"
23
Documenter = "e30172f5-a6a5-5a46-863b-614d45cd2de4"
34
DocumenterVitepress = "4710194d-e776-4893-9690-8d956a29c365"
45
FileIO = "5789e2e9-d7fb-5bc7-8068-2c6fae9b9549"
56
GeometryBasics = "5c1252a2-5f33-56bf-86c9-59e7332b4326"
67
ImageCore = "a09fc81d-aa75-5fe9-8630-4744c3626534"
8+
ImageShow = "4e3cecfd-b093-5904-9786-8bbb286a6a31"
9+
KernelAbstractions = "63c18a36-062a-441e-b654-da1e3ab1ce7c"
710
Literate = "98b081ad-f1c9-55d3-8b20-4c87d4299306"
11+
Makie = "ee78f7c6-11fb-53f2-987a-cfe4a2b5a57a"
12+
Metal = "dde4c033-4e86-420c-a63e-0dd931031962"
813
Trace = "afc56b53-c9a9-482a-a956-d1d800e05558"
914

1015
[compat]

src/Trace.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ function concentric_sample_disk(u::Point2f)::Point2f
5252
# Map uniform random numbers to [-1, 1].
5353
offset = 2f0 * u - Vec2f(1f0)
5454
# Handle degeneracy at the origin.
55-
offset[1] 0 && offset[2] 0 && return Point2f(0)
55+
offset[1] 0f0 && offset[2] 0f0 && return Point2f(0)
5656
if abs(offset[1]) > abs(offset[2])
5757
r = offset[1]
5858
θ = (offset[2] / offset[1]) * π / 4f0

src/materials/material.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ Base.Base.@propagate_inbounds function glass_material(g::UberMaterial, si::Surfa
5959
t_black = is_black(t)
6060
r_black && t_black && return BSDF(si, η)
6161

62-
is_specular = u_roughness 0 && v_roughness 0
62+
is_specular = u_roughness 0f0 && v_roughness 0f0
6363
if is_specular && allow_multiple_lobes
6464
return BSDF(si, η, FresnelSpecular(true, r, t, 1.0f0, η, transport))
6565
end

src/materials/tonemap.jl

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,17 @@
11
using Colors, Statistics
22

3-
luminosity(c::RGB{T}) where {T} = (max(c.r, c.g, c.b) + min(c.r, c.g, c.b)) / 2.0
3+
luminosity(c::RGB{T}) where {T} = (max(c.r, c.g, c.b) + min(c.r, c.g, c.b)) / 2.0f0
44

55
function lum_max(rgb_m)
6-
lum_max = 0.0
6+
lum_max = 0.0f0
77
for pix in rgb_m
88
(lum_max > luminosity(pix)) || (lum_max = luminosity(pix))
99
end
1010
lum_max
1111
end
1212

13-
function avg_lum(rgb_m, δ::Number=1e-10)
14-
cumsum = 0.0
13+
function avg_lum(rgb_m, δ::Number=1f-10)
14+
cumsum = 0.0f0
1515
for pix in rgb_m
1616
cumsum += log10+ luminosity(pix))
1717
end
@@ -20,13 +20,13 @@ end
2020

2121
function normalize_image(
2222
rgb_m,
23-
a::Float64=0.18,
23+
a::Float32=0.18f0,
2424
lum::Union{Number,Nothing}=nothing,
25-
δ::Number=1e-10
25+
δ::Number=1f-10
2626
)
2727

28-
(isnothing(lum) || lum 0.0) && (lum = avg_lum(rgb_m, δ))
29-
return rgb_m .* a .* (1.0 / lum)
28+
(isnothing(lum) || lum 0.0f0) && (lum = avg_lum(rgb_m, δ))
29+
return rgb_m .* a .* (1.0f0 / lum)
3030
end
3131

3232
function clamp_image(img::AbstractMatrix{T}) where {T}
@@ -35,7 +35,7 @@ function clamp_image(img::AbstractMatrix{T}) where {T}
3535
end
3636
end
3737

38-
function γ_correction(img::AbstractMatrix{T}, γ::Float64=1.0, k::Float64=1.0) where T
38+
function γ_correction(img::AbstractMatrix{T}, γ::Float32=1.0f0, k::Float32=1.0f0) where T
3939
return map(img) do c
4040
return T(
4141
floor(255 * c.r^(1 / γ)),
@@ -46,8 +46,8 @@ function γ_correction(img::AbstractMatrix{T}, γ::Float64=1.0, k::Float64=1.0)
4646
end
4747

4848
function tone_mapping(img;
49-
a::Float64=0.18,
50-
γ::Float64=1.0,
49+
a::Float32=0.18f0,
50+
γ::Float32=1.0f0,
5151
lum::Union{Number,Nothing}=nothing
5252
)
5353
img = normalize_image(img, a, lum)

src/reflection/microfacet.jl

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -95,8 +95,11 @@ with the surface normal `w`.
9595
function D(trd::TrowbridgeReitzDistribution, w::Vec3f)::Float32
9696
tan_θ² = tan_θ(w)^2
9797
isinf(tan_θ²) && return 0f0
98-
99-
cos_θ⁴ = cos_θ(w)^4
98+
99+
# Calculate cos_θ⁴ without using ^4
100+
cos_θ² = cos_θ(w) * cos_θ(w)
101+
cos_θ⁴ = cos_θ² * cos_θ²
102+
100103
e = (cos_ϕ(w)^2 / (trd.α_x^2) + sin_ϕ(w)^2 / (trd.α_y^2)) * tan_θ²
101104
1f0 /* trd.α_x * trd.α_y * cos_θ⁴ * (1f0 + e)^2)
102105
end
@@ -210,7 +213,7 @@ function distribution_microfacet_reflection(m::UberBxDF{S}, wo::Vec3f, wi::Vec3f
210213
wh = wi + wo
211214
# Degenerate cases for microfacet reflection.
212215

213-
(cosθi 0 || cosθo 0) && return S(0f0)
216+
(cosθi 0f0 || cosθo 0f0) && return S(0f0)
214217
wh Vec3f(0) && return S(0f0)
215218
wh = normalize(wh)
216219
f = m.fresnel(wi face_forward(wh, Vec3f(0, 0, 1)))
@@ -222,12 +225,12 @@ end
222225
m::UberBxDF{S}, wo::Vec3f, u::Point2f,
223226
)::Tuple{Vec3f,Float32,RGBSpectrum,UInt8} where {S<:Spectrum}
224227

225-
wo[3] 0 && return Vec3f(0.0f0), 0.0f0, S(0.0f0), UInt8(0)
228+
wo[3] 0f0 && return Vec3f(0.0f0), 0.0f0, S(0.0f0), UInt8(0)
226229

227230
# Sample microfacet orientation `wh` and reflected direction `wi`.
228231

229232
wh = sample_wh(m.distribution, wo, u)
230-
(wo wh) < 0 && return Vec3f(0.0f0), 0.0f0, S(0.0f0), UInt8(0)
233+
(wo wh) < 0f0 && return Vec3f(0.0f0), 0.0f0, S(0.0f0), UInt8(0)
231234

232235
wi = reflect(wo, wh)
233236
!same_hemisphere(wo, wi) && return Vec3f(0.0f0), 0.0f0, S(0.0f0), UInt8(0)
@@ -252,7 +255,7 @@ function distribution_microfacet_transmission(m::UberBxDF{S}, wo::Vec3f, wi::Vec
252255
same_hemisphere(wo, wi) && return S(0f0)
253256

254257
cosθo, cosθi = cos_θ(wo), cos_θ(wi)
255-
(cosθo 0 || cosθi 0) && return S(0f0)
258+
(cosθo 0f0 || cosθi 0f0) && return S(0f0)
256259
# Compute `wh` from `wo` & `wi` for microfacet transmission.
257260
η = cos_θ(wo) > 0f0 ? (m.η_b / m.η_a) : (m.η_a / m.η_b)
258261
wh = normalize((wo + wi * η))
@@ -275,7 +278,7 @@ end
275278

276279
@inline function sample_microfacet_transmission(m::UberBxDF{S}, wo::Vec3f, u::Point2f) where {S<:Spectrum}
277280

278-
wo[3] 0 && return Vec3f(0f0), 0f0, S(0f0), UInt8(0)
281+
wo[3] 0f0 && return Vec3f(0f0), 0f0, S(0f0), UInt8(0)
279282
wh = sample_wh(m.distribution, wo, u)
280283
(wo wh) < 0 && return Vec3f(0f0), 0f0, S(0f0), UInt8(0)
281284

src/shapes/sphere.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ end
5555

5656
function refine_intersection(p::Point, s::Sphere)
5757
p *= s.radius ./ distance(Point3f(0), p)
58-
p[1] 0 && p[2] 0 && (p = Point3f(1f-6 * s.radius, p[2], p[3]))
58+
p[1] 0f0 && p[2] 0f0 && (p = Point3f(1f-6 * s.radius, p[2], p[3]))
5959
p
6060
end
6161

src/shapes/triangle_mesh.jl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -181,7 +181,7 @@ end
181181
δuv_13, δuv_23 = uv[1] - uv[3], uv[2] - uv[3]
182182
δp_13, δp_23 = Vec3f(vs[1] - vs[3]), Vec3f(vs[2] - vs[3])
183183
det = δuv_13[1] * δuv_23[2] - δuv_13[2] * δuv_23[1]
184-
if det 0
184+
if det 0f0
185185
v = normalize((vs[3] - vs[1]) × (vs[2] - vs[1]))
186186
_, ∂p∂u, ∂p∂v = coordinate_system(Vec3f(v))
187187
return ∂p∂u, ∂p∂v, δp_13, δp_23
@@ -201,7 +201,7 @@ end
201201
δuv_13, δuv_23 = uv[1] - uv[3], uv[2] - uv[3]
202202
δn_13, δn_23 = t_normals[1] - t_normals[3], t_normals[2] - t_normals[3]
203203
det = δuv_13[1] * δuv_23[2] - δuv_13[2] * δuv_23[1]
204-
det 0 && return Normal3f(0), Normal3f(0)
204+
det 0f0 && return Normal3f(0), Normal3f(0)
205205

206206
inv_det = 1f0 / det
207207
∂n∂u = (δuv_23[2] * δn_13 - δuv_13[2] * δn_23) * inv_det

test/Project.toml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,15 @@
11
[deps]
2+
BenchmarkTools = "6e4b80f9-dd63-53aa-95a3-0cdb28fa8baf"
3+
Cthulhu = "f68482b8-f384-11e8-15f7-abe071a5a75f"
24
FileIO = "5789e2e9-d7fb-5bc7-8068-2c6fae9b9549"
35
GeometryBasics = "5c1252a2-5f33-56bf-86c9-59e7332b4326"
46
ImageCore = "a09fc81d-aa75-5fe9-8630-4744c3626534"
57
ImageIO = "82e4d734-157c-48bb-816b-45c225c6df19"
68
ImageMagick = "6218d12a-5da1-5696-b52f-db25d2ecc6d1"
9+
ImageShow = "4e3cecfd-b093-5904-9786-8bbb286a6a31"
10+
KernelAbstractions = "63c18a36-062a-441e-b654-da1e3ab1ce7c"
711
LinearAlgebra = "37e2e46d-f89d-539d-b4ee-838fcccc9c8e"
12+
Makie = "ee78f7c6-11fb-53f2-987a-cfe4a2b5a57a"
13+
Metal = "dde4c033-4e86-420c-a63e-0dd931031962"
814
Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40"
15+
Trace = "afc56b53-c9a9-482a-a956-d1d800e05558"

test/gpu-threading-benchmarks.jl

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,13 @@ function launch_trace_image!(img, camera, scene)
100100
KA.synchronize(backend)
101101
return img
102102
end
103+
# using AMDGPU
104+
# ArrayType = ROCArray
105+
# using CUDA
106+
# ArrayType = CuArray
107+
108+
# using Metal
109+
# ArrayType = MtlArray
103110

104111
preserve = []
105112
gpu_scene = to_gpu(ArrayType, scene; preserve=preserve);

0 commit comments

Comments
 (0)