Skip to content

Commit efa7f67

Browse files
committed
fix tests + tutorials
1 parent 0422827 commit efa7f67

7 files changed

Lines changed: 23 additions & 21 deletions

File tree

docs/src/gpu_raytracing_tutorial.md

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -277,4 +277,3 @@ DOM.img(src=Asset(data"gpu-benchmarks.png"), width="700px")
277277
* Add **adaptive sampling** (more samples only where needed)
278278
* Explore **shared memory** optimizations for BVH traversal
279279
* Implement **streaming multisampling** across frames
280-

docs/src/raytracing-core.jl

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,7 @@ end
130130
function reflective_kernel(bvh, ctx, tri, dist, bary, ray, sky_color, shadow_samples=8)
131131
hit_point = ray.o + ray.d * dist
132132
normal = compute_normal(tri, bary)
133-
mat = ctx.materials[tri.material_idx]
133+
mat = ctx.materials[tri.metadata]
134134

135135
# Direct lighting with soft shadows
136136
direct_color = compute_multi_light(bvh, ctx, hit_point, normal, mat, shadow_samples=shadow_samples)
@@ -153,7 +153,7 @@ function reflective_kernel(bvh, ctx, tri, dist, bary, ray, sky_color, shadow_sam
153153
reflection_color = if refl_hit
154154
refl_point = reflect_ray.o + reflect_ray.d * refl_dist
155155
refl_normal = compute_normal(refl_tri, refl_bary)
156-
refl_mat = ctx.materials[refl_tri.material_idx]
156+
refl_mat = ctx.materials[refl_tri.metadata]
157157
compute_multi_light(bvh, ctx, refl_point, refl_normal, refl_mat, shadow_samples=shadow_samples)
158158
else
159159
to_vec3f(sky_color)
@@ -207,7 +207,9 @@ function example_scene(; glass_cat=false)
207207
(sphere2, Material(RGB(0.3f0, 0.6f0, 0.9f0), 0.5f0, 0.3f0, 1.0f0, 0.0f0)),
208208
]
209209

210-
bvh, materials = MaterialScene(scene)
210+
geometries = [g for (g, _) in scene]
211+
materials = [m for (_, m) in scene]
212+
bvh = Raycore.BVH(geometries, (mesh_idx, tri_idx) -> UInt32(mesh_idx))
211213
lights = default_lights()
212214
ctx = RenderContext(lights, materials, 0.1f0)
213215
return bvh, ctx

docs/src/raytracing_tutorial_content.md

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -47,9 +47,9 @@ left_wall = normal_mesh(Rect3f(Vec3f(-5, -1.5, -2), Vec3f(0.01, 5, 10)))
4747
sphere1 = Tesselation(Sphere(Point3f(-2, -1.5 + 0.8, 2), 0.8f0), 64)
4848
sphere2 = Tesselation(Sphere(Point3f(2, -1.5 + 0.6, 1), 0.6f0), 64)
4949

50-
# Build our BVH acceleration structure
50+
# Build our BVH acceleration structure with material indices
5151
scene_geometry = [cat_mesh, floor, back_wall, left_wall, sphere1, sphere2]
52-
bvh = Raycore.BVH(scene_geometry)
52+
bvh = Raycore.BVH(scene_geometry, (mesh_idx, tri_idx) -> UInt32(mesh_idx))
5353
f, ax, pl = plot(bvh; axis=(; show_axis=false))
5454
```
5555
Set the camera to something better:
@@ -269,7 +269,7 @@ end
269269
function material_kernel(bvh, ctx, tri, dist, bary, ray)
270270
hit_point = ray.o + ray.d * dist
271271
normal = compute_normal(tri, bary)
272-
mat = ctx.materials[tri.material_idx]
272+
mat = ctx.materials[tri.metadata]
273273

274274
color = compute_multi_light(bvh, ctx, hit_point, normal, mat, shadow_samples=2)
275275
return to_rgb(color)
@@ -287,7 +287,7 @@ Add simple reflections for metallic surfaces:
287287
function reflective_kernel(bvh, ctx, tri, dist, bary, ray, sky_color)
288288
hit_point = ray.o + ray.d * dist
289289
normal = compute_normal(tri, bary)
290-
mat = ctx.materials[tri.material_idx]
290+
mat = ctx.materials[tri.metadata]
291291

292292
# Direct lighting with soft shadows
293293
direct_color = compute_multi_light(bvh, ctx, hit_point, normal, mat, shadow_samples=8)
@@ -310,7 +310,7 @@ function reflective_kernel(bvh, ctx, tri, dist, bary, ray, sky_color)
310310
reflection_color = if refl_hit
311311
refl_point = reflect_ray.o + reflect_ray.d * refl_dist
312312
refl_normal = compute_normal(refl_tri, refl_bary)
313-
refl_mat = ctx.materials[refl_tri.material_idx]
313+
refl_mat = ctx.materials[refl_tri.metadata]
314314
compute_multi_light(bvh, ctx, refl_point, refl_normal, refl_mat, shadow_samples=1)
315315
else
316316
to_vec3f(sky_color)

ext/RaycoreMakieExt.jl

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -184,12 +184,12 @@ end
184184
Helper function to draw BVH geometry
185185
"""
186186
function draw_bvh!(plot, bvh::Raycore.BVH, colors, alpha)
187-
# Group primitives by their material_idx
188-
primitive_groups = Dict{UInt32, Vector{Raycore.Triangle}}()
187+
# Group primitives by their metadata
188+
primitive_groups = Dict{Any, Vector{eltype(bvh.primitives)}}()
189189
for prim in bvh.primitives
190-
mat_idx = prim.material_idx
190+
mat_idx = prim.metadata
191191
if !haskey(primitive_groups, mat_idx)
192-
primitive_groups[mat_idx] = Raycore.Triangle[]
192+
primitive_groups[mat_idx] = eltype(bvh.primitives)[]
193193
end
194194
push!(primitive_groups[mat_idx], prim)
195195
end
@@ -240,7 +240,7 @@ function Makie.convert_arguments(::Type{Makie.Mesh}, bvh::Raycore.BVH)
240240
start_idx = length(vertices)
241241
for (v, n) in zip(prim.vertices, prim.normals)
242242
push!(vertices, v)
243-
push!(colors, prim.material_idx)
243+
push!(colors, Float32(prim.metadata))
244244
push!(normals, Vec3f(n))
245245
end
246246
push!(faces, GeometryBasics.TriangleFace(start_idx + 1, start_idx + 2, start_idx + 3))

src/kernels.jl

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -62,12 +62,14 @@ end
6262
function get_illumination(bvh, viewdir; grid_size=1000)
6363
# Calculate grid bounds
6464
hits = hits_from_grid(bvh, viewdir; grid_size=grid_size)
65-
result = Dict{UInt32, Float32}()
65+
# Use primitive metadata as keys - requires metadata to be the primitive index
66+
result = Dict{Int, Float32}()
6667
for hit in hits
6768
if hit.hit
68-
count = get!(result, hit.prim_idx, 0f0)
69-
result[hit.prim_idx] = count + 1f0
69+
idx = Int(hit.metadata)
70+
count = get!(result, idx, 0f0)
71+
result[idx] = count + 1f0
7072
end
7173
end
72-
return [get(result, UInt32(idx), 0.0f0) for idx in 1:length(bvh.primitives)]
74+
return [get(result, idx, 0.0f0) for idx in 1:length(bvh.primitives)]
7375
end

test/test_intersection.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -109,5 +109,5 @@ end
109109
intersects, triangle = Raycore.closest_hit(bvh, ray)
110110
@test intersects
111111
# BVH closest_hit returns Triangle object, not SurfaceInteraction
112-
@test typeof(triangle) == Raycore.Triangle
112+
@test triangle isa Raycore.Triangle
113113
end

test/test_type_stability.jl

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,7 @@ function gen_triangle()
3636
SVector(n1, n1, n1),
3737
SVector(Vec3f(NaN), Vec3f(NaN), Vec3f(NaN)),
3838
SVector(uv1, uv2, uv3),
39-
UInt32(1),
40-
UInt32(1)
39+
UInt32(1) # metadata (single field replaces mesh_idx and material_idx)
4140
)
4241
end
4342

0 commit comments

Comments
 (0)