Skip to content

Commit 759fb63

Browse files
authored
wavefront + bvh cleanup (#8)
* glass and cleanup * remove indices from compact triangle and run test_wavefront with same parameters as gpu benchmark
1 parent 2dcc3dd commit 759fb63

File tree

4 files changed

+88
-135
lines changed

4 files changed

+88
-135
lines changed

docs/src/raytracing-core.jl

Lines changed: 28 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
using Raycore, GeometryBasics, LinearAlgebra
22
using Colors, ImageShow
33
import KernelAbstractions as KA
4+
using KernelAbstractions: @kernel, @index
45
function compute_normal(triangle, bary_coords)
56
v0, v1, v2 = Raycore.normals(triangle)
67
u, v, w = bary_coords[1], bary_coords[2], bary_coords[3]
@@ -29,8 +30,13 @@ struct Material
2930
base_color::RGB{Float32}
3031
metallic::Float32
3132
roughness::Float32
33+
ior::Float32 # Index of refraction (1.0 = opaque, 1.5 = glass)
34+
transmission::Float32 # 0 = opaque, 1 = fully transparent
3235
end
3336

37+
# Convenience constructor for backward compatibility
38+
Material(base_color, metallic, roughness) = Material(base_color, metallic, roughness, 1.0f0, 0.0f0)
39+
3440
struct RenderContext{L<:AbstractVector{PointLight},M<:AbstractVector{Material}}
3541
lights::L
3642
materials::M
@@ -41,21 +47,30 @@ function Raycore.to_gpu(Arr, ctx::RenderContext)
4147
return RenderContext(to_gpu(Arr, ctx.lights), to_gpu(Arr, ctx.materials), ctx.ambient)
4248
end
4349

44-
function render_context()
50+
function render_context(; glass_cat=false)
4551
# Create lights and materials
4652
lights = [
4753
PointLight(Point3f(3, 4, -2), 50.0f0, RGB(1.0f0, 0.9f0, 0.8f0)),
4854
PointLight(Point3f(-3, 2, 0), 20.0f0, RGB(0.7f0, 0.8f0, 1.0f0)),
4955
PointLight(Point3f(0, 5, 5), 15.0f0, RGB(1.0f0, 1.0f0, 1.0f0))
5056
]
5157

58+
# Material: base_color, metallic, roughness, ior, transmission
59+
cat_material = if glass_cat
60+
# Glass cat: clear glass with slight green tint, ior=1.5
61+
Material(RGB(0.95f0, 1.0f0, 0.95f0), 0.0f0, 0.0f0, 1.5f0, 1.0f0)
62+
else
63+
# Original diffuse cat
64+
Material(RGB(0.8f0, 0.6f0, 0.4f0), 0.0f0, 0.8f0, 1.0f0, 0.0f0)
65+
end
66+
5267
materials = [
53-
Material(RGB(0.8f0, 0.6f0, 0.4f0), 0.0f0, 0.8f0), # cat
54-
Material(RGB(0.3f0, 0.5f0, 0.3f0), 0.0f0, 0.9f0), # floor
55-
Material(RGB(0.8f0, 0.6f0, 0.5f0), 0.8f0, 0.05f0), # back wall
56-
Material(RGB(0.7f0, 0.7f0, 0.8f0), 0.0f0, 0.8f0), # left wall
57-
Material(RGB(0.9f0, 0.9f0, 0.9f0), 0.8f0, 0.02f0), # sphere1 - metallic
58-
Material(RGB(0.3f0, 0.6f0, 0.9f0), 0.5f0, 0.3f0), # sphere2 - semi-metallic
68+
cat_material, # cat (index 1)
69+
Material(RGB(0.3f0, 0.5f0, 0.3f0), 0.0f0, 0.9f0, 1.0f0, 0.0f0), # floor - diffuse
70+
Material(RGB(0.8f0, 0.6f0, 0.5f0), 0.8f0, 0.05f0, 1.0f0, 0.0f0), # back wall - metallic
71+
Material(RGB(0.7f0, 0.7f0, 0.8f0), 0.0f0, 0.8f0, 1.0f0, 0.0f0), # left wall - diffuse
72+
Material(RGB(0.9f0, 0.9f0, 0.9f0), 0.8f0, 0.02f0, 1.0f0, 0.0f0), # sphere1 - metallic
73+
Material(RGB(0.3f0, 0.6f0, 0.9f0), 0.5f0, 0.3f0, 1.0f0, 0.0f0), # sphere2 - semi-metallic
5974
]
6075

6176
return RenderContext(lights, materials, 0.1f0)
@@ -171,7 +186,7 @@ function reflective_kernel(bvh, ctx, tri, dist, bary, ray, sky_color, shadow_sam
171186
return to_rgb(direct_color)
172187
end
173188

174-
function example_scene()
189+
function example_scene(; glass_cat=false)
175190
cat_mesh = Makie.loadasset("cat.obj")
176191
angle = deg2rad(150f0)
177192
rotation = Makie.Quaternionf(0, sin(angle/2), 0, cos(angle/2))
@@ -198,8 +213,11 @@ function example_scene()
198213

199214
# Build our BVH acceleration structure
200215
scene_geometry = [cat_mesh, floor, back_wall, left_wall, sphere1, sphere2]
201-
bvh = Raycore.BVH(scene_geometry)
202-
return bvh, render_context()
216+
return scene_geometry, render_context(glass_cat=glass_cat)
217+
end
218+
219+
function example_scene_glass_cat()
220+
example_scene(glass_cat=true)
203221
end
204222

205223
function sample_light(bvh, ctx, width, height, camera_pos, focal_length, aspect, x, y, sky_color)

docs/src/test_wavefront.jl

Lines changed: 28 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,31 +8,53 @@ import Makie
88
using Makie: RGBf
99
import KernelAbstractions as KA
1010
using ImageShow
11+
using BenchmarkTools
1112

1213
# Load helper functions
1314
include("raytracing-core.jl")
1415
include("wavefront-renderer.jl")
1516

17+
geom, ctx = example_scene()
18+
bvh = BVH(geom)
19+
# ibvh = Raycore.InstancedBVH(geom)
1620
begin
17-
bvh, ctx = example_scene()
18-
img = fill(RGBf(0, 0, 0), 1024, 2048)
19-
renderer = WavefrontRenderer(
20-
img, bvh, ctx;
21+
img = fill(RGBf(0, 0, 0), 400, 720)
22+
renderer = WavefrontRenderer(img, bvh, ctx)
23+
@btime render!(renderer)
24+
nothing
25+
end
26+
renderer.framebuffer
27+
renderer_instanced.framebuffer
28+
begin
29+
img = fill(RGBf(0, 0, 0), 400, 720)
30+
renderer_instanced = WavefrontRenderer(
31+
img, ibvh, ctx;
2132
camera_pos=Point3f(0, -0.9, -2.5),
2233
fov=45.0f0,
2334
sky_color=RGB{Float32}(0.5f0, 0.7f0, 1.0f0),
2435
samples_per_pixel=4
2536
)
26-
Array(@time render!(renderer))
37+
@btime render!(renderer_instanced)
38+
nothing
2739
end
40+
using ImageShow
2841

2942
using FileIO
3043
save("wavefront.png", map(col -> mapc(c -> clamp(c, 0f0, 1f0), col), renderer.framebuffer))
3144

3245
using AMDGPU
3346
amd_renderer = to_gpu(ROCArray, renderer);
34-
Array(@time render!(amd_renderer))
47+
Array(@btime render!(amd_renderer))
3548

3649
using pocl_jll, OpenCL
3750
amd_renderer = to_gpu(CLArray, renderer);
3851
Array(@time render!(amd_renderer))
52+
r = Raycore.Ray(o=Point3f(0, 0, 0), d=Vec3f(0, 0, 1), t_max=0.0f0)
53+
54+
@code_warntype any_hit(ibvh, r)
55+
56+
function test(bvh)
57+
meshes = getfield(bvh, :meshes)
58+
end
59+
60+
typeof(test(ibvh))

0 commit comments

Comments
 (0)