Raycore:GPU 加速和模块化光线求交
Raycore: GPU accelerated and modular ray intersections

原始链接: https://makie.org/website/blogposts/raycore/

## Raycore.jl:高性能光线-三角形求交引擎 Raycore.jl 是一个新推出的 Julia 库,专为快速的光线-三角形求交计算而设计,利用边界体积层次结构 (BVH) 在 CPU 和 GPU 上实现加速。它旨在为 Makie 可视化生态系统提供一个逼真的光线追踪后端,同时也设计成一个通用的工具,可应用于光传输、热传递和声学模拟等领域。 Raycore 利用 Julia 的优势——与 C/C++ 相当的性能、通过 KernelAbstractions.jl 提供的优秀 GPU 支持以及灵活的多分派系统——提供了一种高级且可扩展的光线追踪方法。尽管存在初始编译时间和 GPU 代码复杂性等挑战,但持续的开发旨在缓解这些问题。 主要特性包括快速的 BVH 构建/遍历、CPU/GPU 兼容性、分析工具以及随场景尺寸增加的强大性能扩展性。交互式教程引导用户从基本概念到高级 GPU 优化。未来的开发重点是完整的 Makie 后端、探索替代加速结构以及进一步的性能改进。 Raycore.jl 欢迎社区贡献,为那些寻求推进高性能光线追踪的人士提供了一个易于访问的代码库。

Hacker News 新闻 | 过去 | 评论 | 提问 | 展示 | 招聘 | 提交 登录 Raycore:GPU 加速和模块化的光线与物体相交引擎 (makie.org) 6 分,由 simondanisch 发布 1 小时前 | 隐藏 | 过去 | 收藏 | 1 条评论 simondanisch 1 小时前 [–] 我很高兴地宣布 Raycore.jl,这是一个高性能的光线-三角形相交引擎,具有 BVH 加速,可在 Julia 中进行 CPU 和 GPU 执行。Raycore 将为 Makie 提供新的光线追踪后端,为生态系统带来逼真的渲染。 我们分离出了光线相交引擎,因为它可以在许多不同的领域中使用,因此我们非常好奇社区将用它来创造什么,以及我们可以在未来几年内将性能推到什么程度。 该软件包包含交互式教程,涵盖从基础知识到高级 GPU 优化的所有内容。回复 指南 | 常见问题 | 列表 | API | 安全 | 法律 | 申请 YC | 联系 搜索:
相关文章

原文

I'm excited to announce Raycore.jl, a high-performance ray-triangle intersection engine with Bounding Volume Hierarchy (BVH) acceleration, designed for both CPU and GPU execution in Julia. Raycore will power a new raytracing backend for Makie, bringing photorealistic rendering to the Makie ecosystem with the familiar Makie API. We factored out the ray intersection engine, since it can be used in many other fields like simulating light transport, heat transfer, or acoustig propagation and many other.

Why Write a New Ray Intersection Engine?

You might wonder: why build yet another ray tracer? The answer lies in Julia's unique strengths, the opportunities they create, and the flexibility we gain from having complete control over the rendering implementation.

Advantages of Julia

  • High-level language with performance close to C/C++ - Write readable code that runs fast

  • Great GPU support - Single codebase runs on CUDA, AMD, Metal, oneAPI, and OpenCL via KernelAbstractions.jl

  • Multiple dispatch for different geometries, algorithms, and materials - Extend the system cleanly without modifying core code

  • Pluggable architecture for new features - Add custom materials, sampling strategies, or acceleration structures

  • One of the best languages to write out math - The code looks like the equations you'd write on paper

Honest Assessment: The Tradeoffs

Julia isn't perfect, and there are certainly challenges:

  • Long compile times for first use - The first run of a function triggers JIT compilation

  • GPU code still has some rough edges - Complex kernels require careful attention to avoid allocations and GPU-unfriendly constructs

  • Not all backends work yet - I've only tested AMDGPU and OpenCL.jl. Metal.jl and OpenCL on macOS don't work yet, though I think it's just a matter of time to support all backends.

In practice, compile times aren't as bad as they might sound. You keep a Julia session running and only pay the compilation cost once. There's also ongoing work on precompilation that could reduce these times to near-zero in the future and compile most kernels ahead of time. For GPU code, better tooling for detecting and fixing issues is on the horizon, along with improved error messages when problematic LLVM code is generated.

The flexibility to write a high-performance ray tracer in a high-level language opens up exciting possibilities:

  • Use automatic differentiation to get gradients for training ML models

  • Plug in new optimizations seamlessly without fighting a type system or rewriting core algorithms

  • Democratize working on high-performance ray tracing - contributions don't require C++ expertise and the code base is fairly small

  • Rapid experimentation - test new ideas without lengthy compile cycles

What is Raycore.jl?

Raycore is a focused library that does one thing well: fast ray-triangle intersections with BVH acceleration. It provides the building blocks for ray tracing applications without imposing a particular rendering architecture.

Core Features

  • Fast BVH construction and traversal

  • CPU and GPU support via KernelAbstractions.jl

  • Analysis tools: centroid calculation, illumination analysis, view factors for radiosity

  • Makie integration for visualization

Performance

Thanks to the bounding volume hierarchy, the intersection performance scales relatively well with the scene size.

using Raycore, GeometryBasics, BenchmarkTools, Markdown, Bonito
ray = Raycore.Ray(o=Point3f(0, 0, -5), d=Vec3f(0, 0, 1))
results = map([1, 1000, 10000]) do n
    spheres = [Tesselation(Sphere(randn(Point3f) .* 1000f0, 0.5f0), 32) for _ in 1:n]
    bvh = Raycore.BVH(spheres)
    tclosest = BenchmarkTools.@belapsed Raycore.closest_hit($bvh, $ray)
    ntriangles = length(bvh.primitives)
    # use closest hit for time per triangle, since it needs to traverse more triangles
    tpt = string(round((tclosest/ntriangles)*1e9, digits=5), "ns")
    tcloseststr = string(round(tclosest * 1e6, digits=2), "μs")
    (triangles=ntriangles, closest=tcloseststr, time_per_triangle_ns=tpt)
end
DOM.div(Bonito.Table(results))
19220.01μs0.0064ns
19220000.58μs0.0003ns
192200004.01μs0.00021ns

Since you can run thousands of ray intersections in parallel on the GPU, we get pretty great performance per ray intersection. The below benchmark is from the GPU tutorial, which generates roughly 400x700px * 4 samples x 8 shadow rays per 3 lights + 1 reflection ray which can spawn up to 8 shadow rays, so around 40mio rays, which we can do in an optimized GPU kernel in around 43ms: GPU Benchmarks

Interactive Tutorials

The documentation includes several hands-on tutorials that build from basics to advanced GPU optimization:

1. BVH Hit Tests & Basics

Learn the fundamentals of ray-triangle intersection, BVH construction, and visualization.

BVH Basics

Try the tutorial →

2. Ray Tracing in one Hour

Build a complete ray tracer from scratch with shadows, materials, reflections, and tone mapping analogous to the famous Ray Tracing in one Weekend.

Ray Tracing

Try the tutorial →

3. Ray Tracing on the GPU

Port the ray tracer to GPU and learn optimization techniques: loop unrolling, tiling, and wavefront rendering. Includes comprehensive benchmarks comparing different approaches.

Try the tutorial →

4. View Factors & Analysis

Calculate view factors, illumination, and centroids for radiosity and thermal analysis applications.

View Factors

Try the tutorial →

Getting Started

Install Raycore.jl from the Julia package manager:


using Pkg
Pkg.add("Raycore")

Then check out the interactive tutorials to start building your first ray tracer!

Future Work

We have some plans for further improving the Package in the future:

  • Makie raytracing backend - Bringing photorealistic rendering to the Makie ecosystem

  • Advanced acceleration structures - Explore alternatives to BVH like kd-trees or octrees for specific use cases

  • GPU memory optimizations - Reduce memory footprint for larger scenes

  • Improved precompilation - Further reduce first-run latency

  • Further optimize performance - It would be great to have as many people as possible work on optimizing this to the core :)

Contributions are welcome! The codebase is designed to be approachable, and the Julia community is friendly and helpful.

Acknowledgments

Raycore.jl was split out from Trace.jl, originally created by Anton Smirnov. Trace.jl will soon be renamed to Hikari and released as the main ray tracing implementation built on top of Raycore, providing a complete path tracing framework. This project builds on the excellent work of the Julia GPU ecosystem, particularly KernelAbstractions.jl for portable GPU programming and of course all the GPU backend packages. Special thanks to everyone who helped shape Raycore. Parts of this work was made possible by an investment of the Sovereign Tech Agency and most of the optimization and GPU work has been funded by Muon Space.

I'm excited to see what can be build with Raycore.jl and how far we can push the performance as a community!

联系我们 contact @ memedata.com