Blender V5.3
gsplat_intersect.h
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2026 Blender Foundation
2 *
3 * SPDX-License-Identifier: Apache-2.0 */
4
5#pragma once
6
7#include "kernel/globals.h"
8#include "kernel/types.h"
9
11#include "kernel/geom/object.h"
13
15
16/* Gaussian splats intersection functions.
17 *
18 * References:
19 *
20 * [3dgrt2024] Nicolas Moenne-Loccoz et. al.
21 * "3D Gaussian Ray Tracing: Fast Tracing of Particle Scenes" (2024)
22 * ACM Transactions on Graphics and SIGGRAPH Asia
23 * https://gaussiantracer.github.io/
24 *
25 * [sun2025] Xin Sun and Iliyan Georgiev and Yun Fei and Miloš Hašan
26 * "Stochastic ray tracing of transparent 3D Gaussians" (2025)
27 * EGSR conference proceedings
28 * https://iliyan.com/publications/GaussianRayTracing/
29 *
30 * [Rijsdijk2026] Joris Rijsdijk et. al.
31 * "Gaussian Point Splatting" (2026)
32 * ACM Trans. Graph.
33 * https://jorisar.nl/gaussian_point_splatting/
34 *
35 * [chen2024pgsr] Nicolas Moenne-Loccoz et. al.
36 * "PGSR: Planar-based Gaussian Splatting for Efficient and High-Fidelity Surface
37 * Reconstruction" (2024)
38 * https://zju3dv.github.io/pgsr/
39 *
40 * [ptir-gs] Junke Zhu et. al.
41 * "Path-Traced Inverse Rendering with Global Illumination in 3D Gaussian Fields"
42 * (2026)
43 * https://junkzhu.github.io/project_pages/PTIR/ */
44
45#ifdef __POINTCLOUD__
46
47# if defined(__GSPLATS__)
48
49/* Parameters of the Gaussian splat that are needed for intersection. */
50struct GSplatIsectParams {
53 float opacity;
54};
55
56/* Optimized access to Gaussian splat parameters for the intersection purposes.
57 * Reuses steps calculation to access multiple motion attributes. */
58ccl_device_forceinline GSplatIsectParams gsplat_intersect_get_params(
59 KernelGlobals kg, const int object, const int prim, const float time, const int primitive_type)
60{
61 GSplatIsectParams params;
62
63 if ((primitive_type & PRIMITIVE_MOTION) == 0) {
64 const int scale_offset = gsplat_attr_scale_offset(kg, object);
65 const int rotation_offset = gsplat_attr_rotation_offset(kg, object);
66 const int radiance_base_offset = gsplat_attr_radiance_base_offset(kg, object);
67 params.scale = kernel_data_fetch(attributes_float3, scale_offset + prim);
68 params.rotation = kernel_data_fetch(attributes_quaternion, rotation_offset + prim);
69 params.opacity = kernel_data_fetch(attributes_float4, radiance_base_offset + prim).w;
70 return params;
71 }
72
73 const GSplatMotionInfo motion_info = motion_gsplat_compute_motion_info(kg, object, time);
74 params.scale = motion_gsplat_scale(kg, motion_info, object, prim);
75 params.rotation = motion_gsplat_rotation(kg, motion_info, object, prim);
76 params.opacity = motion_gsplat_opacity(kg, motion_info, object, prim);
77 return params;
78}
79
80/* Intersect ray against Gaussian splat.
81 * Performs stochastic alpha test described in [sun2025]. */
82ccl_device_forceinline bool gsplat_intersect_test(KernelGlobals kg,
83 const float3 ray_P,
84 const float3 ray_D,
85 const float ray_tmin,
86 const float ray_tmax,
87 const int object,
88 const int prim,
89 const float time,
90 const int primitive_type,
91 ccl_private float &r_t)
92{
93 const int position_offset = kernel_data_fetch(objects, object).position_offset;
94 const float4 point = (primitive_type & PRIMITIVE_MOTION) ?
95 motion_gsplat_point(kg, object, prim, time) :
96 kernel_data_fetch(points, position_offset + prim);
97
98 /* Intersect with the bounding sphere.
99 * Equivalent to IsNegligible() check in Algorithm 1 of [sun2025].
100 *
101 * Disable backface culling so that we can intersect splats with the ray origin which is inside
102 * of the bounding sphere. */
103 float sphere_t;
104 if (!point_intersect_test<false>(point, ray_P, ray_D, ray_tmin, ray_tmax, &sphere_t)) {
105 return false;
106 }
107
108 const GSplatIsectParams params = gsplat_intersect_get_params(
109 kg, object, prim, time, primitive_type);
110
111 const float3 mu = make_float3(point);
112
113 /* [3dgrt2024] Eq. (8).
114 * The division by scale could return NaN. Account for this in the distance check. */
115 const float3x3 S_inv = float3x3_scale(1.0f / params.scale);
116 const float3x3 R = quaternion_to_rotation(params.rotation);
117 const float3x3 R_T = transposed(R);
118 const float3x3 S_inv_x_R_T = S_inv * R_T;
119 const float3 o_g = S_inv_x_R_T * (ray_P - mu);
120 const float3 d_g = S_inv_x_R_T * ray_D;
121 const float t = -dot(o_g, d_g) / dot(d_g, d_g); /* tau_max in [3dgrt2024] */
122
123 /* Check the intersection with the Gaussian splat is within the valid ray range.
124 * The previous checks are based on the bounding sphere distance.
125 *
126 * The t might be lower than the ray_tmin when the ray origin is within the bounding sphere but
127 * is "past" the density extrema.
128 *
129 * Gracefully reject NaN distance (which could happen if the scale is 0 along any axis). */
130 if (!(t >= ray_tmin && t <= ray_tmax)) {
131 return false;
132 }
133
134 /* Calculate intersection point. */
135 const float3 x = ray_P + t * ray_D;
136 const float3 x_minus_mu = x - mu;
137
138# if 0
139 /* Naive implementation calculating covariance matrix. [3dgrt2024] Eq. (2).
140 * Note that for the particle response evaluation we actually need an inverse of the covariance
141 * matrix.
142 *
143 * Start with the original equation \Sigma = RSS^TR^T
144 * It could be simplified since S^T \equiv S due to the matrix being diagonal scale matrix:
145 * \Sigma = RS^2R^T.
146 *
147 * \Sigma^{-1} = (RS^2R^T)^{-1} = (R^T)^{-1}(S^2)^{-1}R^{-1} = (R^{-1})^T(S^2)^{-1}R^{-1}
148 *
149 * Since R is an orthonormal rotation matrix: R^{-1} \equiv R^T
150 *
151 * There seems to be precision issues with the naive implementation resulting in the floaters in
152 * bicycle and garden scenes from Inria models set are rendered as large almost opaque spheres
153 * (instead of being almost transparent disks). A possible workaround for it is to clamp the
154 * scale, but it results in other issues (choosing clamping size, difference in the render
155 * results from the ground truth, etc.). */
156 const float3x3 Sigma_inv = R * float3x3_scale(1.0f / (params.scale * params.scale)) * R_T;
157 const float e = dot(x_minus_mu, Sigma_inv * x_minus_mu);
158# else
159 /* Combination of [3dgrt2024] Eq. (8). and [ptir-gs] Eq. (1), applied to the (x-mu) instead
160 * of the (0 - mu). it might be obvious, but it was [ptir-gs] that inspired to look into this
161 * equation. */
162 const float3 e_o_g = S_inv_x_R_T * x_minus_mu;
163 const float e = dot(e_o_g, e_o_g);
164# endif
165
166 /* Particle response.
167 * [sun2025] Algorithm 1 (line 12). */
168 const float particle_opacity = params.opacity * fast_expf(-1.0f / 2.0f * e);
169
170 /* Approach similar to [sun2025] who uses stateless trigonometric hash function as a source of
171 * RNG: hash the intersection point to a [0 .. 1] floating point. */
172 /* TODO(sergey): Check on using potentially cheaper PCG hash?
173 * Internally hash_float3_to_float() uses Jenkins Lookup3 hash function. */
174 if (hash_float3_to_float(x) > particle_opacity) {
175 return false;
176 }
177
178 r_t = t;
179
180 return true;
181}
182
183ccl_device_forceinline bool gsplat_intersect(KernelGlobals kg,
185 const float3 ray_P,
186 const float3 ray_D,
187 const float ray_tmin,
188 const float ray_tmax,
189 const int object,
190 const int prim,
191 const float time,
192 const int primitive_type)
193{
194 kernel_assert(primitive_type & PRIMITIVE_GSPLAT);
195
196 if (!gsplat_intersect_test(
197 kg, ray_P, ray_D, ray_tmin, ray_tmax, object, prim, time, primitive_type, isect->t))
198 {
199 return false;
200 }
201
202 isect->prim = prim;
203 isect->object = object;
204 isect->type = primitive_type;
205 isect->u = 0.0f;
206 isect->v = 0.0f;
207 return true;
208}
209# endif /* __GSPLATS__ */
210
211ccl_device_forceinline bool point_or_gsplat_intersect(KernelGlobals kg,
213 const float3 ray_P,
214 const float3 ray_D,
215 const float ray_tmin,
216 const float ray_tmax,
217 const int object,
218 const int prim,
219 const float time,
220 const int primitive_type)
221{
222# if defined(__GSPLATS__)
223 if (primitive_type & PRIMITIVE_POINT)
224# endif
225 {
226 return point_intersect(
227 kg, isect, ray_P, ray_D, ray_tmin, ray_tmax, object, prim, time, primitive_type);
228 }
229
230# if defined(__GSPLATS__)
231 kernel_assert(primitive_type & PRIMITIVE_GSPLAT);
232 return gsplat_intersect(
233 kg, isect, ray_P, ray_D, ray_tmin, ray_tmax, object, prim, time, primitive_type);
234# endif
235}
236
237#endif
238
#define kernel_assert(cond)
#define ccl_device_forceinline
#define kernel_data_fetch(name, index)
#define ccl_private
#define CCL_NAMESPACE_END
ccl_device_forceinline float3 make_float3(const float x, const float y, const float z)
float dot(VecOp< float, D >, VecOp< float, D >) RET
VecBase< float, 3 > float3
void quaternion_to_rotation(float w, float x, float y, float z, float4 &rotation)
float3x3 scale(float3x3 mat, float2 scale)
ccl_device_inline float hash_float3_to_float(const float3 k)
Definition hash.h:227
@ PRIMITIVE_MOTION
@ PRIMITIVE_GSPLAT
@ PRIMITIVE_POINT
ccl_device_inline float fast_expf(const float x)
Definition math_fast.h:431
ccl_device_inline float3x3 transposed(const float3x3 m)
ccl_device_inline float3x3 float3x3_scale(const float3 s)
#define R
CCL_NAMESPACE_BEGIN ccl_device_forceinline int gsplat_attr_radiance_base_offset(KernelGlobals kg, const int object)
ccl_device_forceinline int gsplat_attr_scale_offset(KernelGlobals kg, const int object)
ccl_device_forceinline int gsplat_attr_rotation_offset(KernelGlobals kg, const int object)
QuaternionBase< float > Quaternion
ATTR_WARN_UNUSED_RESULT const BMVert const BMEdge * e