Blender V5.3
draw_gsplat.bsl.hh
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2026 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
18
19#pragma once
20
22#include "draw_model.bsl.hh"
23#include "draw_shader_shared.hh"
24#include "draw_view.bsl.hh"
25
29
30namespace draw::gsplat {
31
32namespace detail {
33
35{
36 float3 c1 = rgb_color * (1.0f / 12.92f);
37 float3 c2 = pow((rgb_color + 0.055f) * (1.0f / 1.055f), float3(2.4f));
38 return mix(c1, c2, step(float3(0.04045f), rgb_color));
39}
40
43{
44 return range.scale_from_unit(float3(data) / float3(255.0f));
45}
46
51bool is_inside_viewport(AxisTuple axes, float3 hP, float2 viewport_size)
52{
53 float2 corner = 2.0f * max(abs(axes.axis_0), abs(axes.axis_1)) * 2.0f / viewport_size;
54 float2 bbox_bl = hP.xy + corner;
55 float2 bbox_ur = hP.xy - corner;
56
57 constexpr float ndc_bounds = 1.0f;
58 bool clip_bl = any(equal(max(bbox_bl, float2(-ndc_bounds)), float2(-ndc_bounds)));
59 bool clip_ur = any(equal(min(bbox_ur, float2(ndc_bounds)), float2(ndc_bounds)));
60
61 return !(clip_bl || clip_ur);
62}
63
64} // namespace detail
65
66/* Shader can skip ellipses (e.g. for point primitives) or radiance parts (e.g. for overlay). */
71
72struct Resources {
74
75 [[storage(0, read)]] const uint4 (&shape_data)[];
76 [[storage(1, read)]] const uint4 (&radiance_data)[];
77 [[storage(2, write)]] uint2 (&ellipses_comp)[];
78 [[storage(3, write)]] uint2 (&radiance_comp)[];
79
82 /* [[push_constant]] const uint inst_id; */
83 /* [[push_constant]] const uint base_inst; */
86
87 Gaussian get(uint gs_id) const
88 {
89 const uint4 data = shape_data[gs_id];
91 }
92};
93
94/* Return value for `eval_splat()` below.
95 * Can indicate that a splat should not be drawn. */
96struct SplatData {
99 float opacity;
100
102 {
103 return {.opacity = 0.0f};
104 }
105
106 bool is_culled() const
107 {
108 return opacity == 0.0f;
109 }
110
111 uint2 pack() const
112 {
114 }
115};
116
122 ObjectMatrices obj,
123 ViewMatrices view,
124 [[resource_table]] const Resources &srt)
125{
126 /* Is the splat behind the viewer? */
127 float3 wP = obj.point_object_to_world(gs.mean);
128 float3 hP = view.point_world_to_ndc(wP);
129 if (view.is_perspective() && hP.z < 0.0f) {
130 return SplatData::culled();
131 }
132
133 /* Discard splat if alpha undercuts 1/255. */
134 if (gs.opacity < (1.0f / 255.0f)) {
135 return SplatData::culled();
136 }
137
138 /* Discard splat if scale is invalid along any axis. */
139 if (any(equal(gs.scale, float3(0.0f))) || any(isnan(gs.scale))) {
140 return SplatData::culled();
141 }
142
143 /* Project Gaussian to screen-space, recovering 2D axes for a splat billboard. */
144 float3 covmat_2d_v = project_gaussian(obj, view, gs);
145 AxisTuple axes = covmat_to_axes(covmat_2d_v);
146
147 /* Discard splat if it is not partially/entirely inside/enclosing the viewport. */
148 if (!detail::is_inside_viewport(axes, hP, srt.viewport_size)) {
149 return SplatData::culled();
150 }
151
152 /* Compute and pack normal. Normal is oriented towards view, never back-facing. */
154 float3 wN = obj.normal_object_to_world(lN);
155 float3 wV = view.world_incident_vector(wP);
156 if (dot(wN, wV) < 0.0f) {
157 wN = -wN;
158 }
159
160 return SplatData{.axes = axes, .N = wN, .opacity = gs.opacity};
161}
162
169template<uint Degrees>
171 ObjectMatrices obj,
172 ViewMatrices view,
173 [[resource_table]] const Resources &srt)
174{
175 /* Object-space incident view vector. */
176 float3 wP = obj.point_object_to_world(gs.mean);
177 float3 wV = view.world_incident_vector(wP);
179
180 /* Unpack spherical harmonic degrees 0, 1, 2, 3.
181 *
182 * The fun part! These require 3*16 coefficients, which are packed over
183 * uint4s (i.e. 48by) using 8b per coefficient. Layout is as follows:
184 *
185 * pack_0 : [xyzx][yzxy][zxyz][xyzx]
186 * pack_1 : [yzxy][zxyz][xyzx][yzxy]
187 * pack_2 : [zxyz][xyzx][yzxy][zxyz]
188 *
189 * The functions `detail::slice_tuple_*()` read disjointed color sets
190 * across the uint4, unpacking the 6 sets of 3 bytes left to right. */
191 const ScalingRange radiance_base_range = srt.infos.radiance_base_range;
192 const ScalingRange radiance_sh_range = srt.infos.radiance_sh_range;
193 const uint4 sh0 = srt.radiance_data[3 * gs.id];
194 const uint4 sh1 = srt.radiance_data[3 * gs.id + 1];
195 const uint4 sh2 = srt.radiance_data[3 * gs.id + 2];
196
197 float3 radiance = float3(0.0f);
198 if (Degrees > 0) {
200 .M0 = detail::unpack_sh(detail::slice_tuple_xyzn(sh0.x), radiance_base_range)};
201 radiance += L0.evaluate(lV);
202 }
203 if (Degrees > 1) {
205 .Mn1 = detail::unpack_sh(detail::slice_tuple_xnyz(sh0.x, sh0.y), radiance_sh_range),
206 .M0 = detail::unpack_sh(detail::slice_tuple_xynz(sh0.y, sh0.z), radiance_sh_range),
207 .Mp1 = detail::unpack_sh(detail::slice_tuple_nxyz(sh0.z), radiance_sh_range)};
208 radiance += L1.evaluate(lV);
209 }
210 if (Degrees > 2) {
212 .Mn2 = detail::unpack_sh(detail::slice_tuple_xyzn(sh0.w), radiance_sh_range),
213 .Mn1 = detail::unpack_sh(detail::slice_tuple_xnyz(sh0.w, sh1.x), radiance_sh_range),
214 .M0 = detail::unpack_sh(detail::slice_tuple_xynz(sh1.x, sh1.y), radiance_sh_range),
215 .Mp1 = detail::unpack_sh(detail::slice_tuple_nxyz(sh1.y), radiance_sh_range),
216 .Mp2 = detail::unpack_sh(detail::slice_tuple_xyzn(sh1.z), radiance_sh_range)};
217 radiance += L2.evaluate(lV);
218 }
219 if (Degrees > 3) {
221 .Mn3 = detail::unpack_sh(detail::slice_tuple_xnyz(sh1.z, sh1.w), radiance_sh_range),
222 .Mn2 = detail::unpack_sh(detail::slice_tuple_xynz(sh1.w, sh2.x), radiance_sh_range),
223 .Mn1 = detail::unpack_sh(detail::slice_tuple_nxyz(sh2.x), radiance_sh_range),
224 .M0 = detail::unpack_sh(detail::slice_tuple_xyzn(sh2.y), radiance_sh_range),
225 .Mp1 = detail::unpack_sh(detail::slice_tuple_xnyz(sh2.y, sh2.z), radiance_sh_range),
226 .Mp2 = detail::unpack_sh(detail::slice_tuple_xynz(sh2.z, sh2.w), radiance_sh_range),
227 .Mp3 = detail::unpack_sh(detail::slice_tuple_nxyz(sh2.w), radiance_sh_range)};
228 radiance += L3.evaluate(lV);
229 }
230
231 /* Resolve radiance as sh + 0.5, matching the implementation of [3dgs2023]. Note that our
232 * conversion to linear rgb is erroneous; splat data is fitted for sRGB data. */
233 float3 resolved_radiance = detail::scene_linear_from_bt709(radiance + 0.5f);
234 return max(resolved_radiance, 0.0f);
235}
239 [[resource_table]] const Resources &);
243 [[resource_table]] const Resources &);
244
246 [[resource_table]] Resources &srt,
247 [[resource_table]] const Pipeline &pipeline,
248 [[resource_table]] const draw::View &views,
249 [[resource_table]] const draw::Model &models,
250 [[global_invocation_id]] const uint3 global_id)
251{
252 int gs_index = int(global_id.x) + srt.invocs_start;
253 if (gs_index >= uint(srt.invocs_count)) {
254 return;
255 }
256
257 /* TODO(not_mark): This does not account for instancing. */
258 const ObjectMatrices obj = models.get(srt.resource_id);
259 const ViewMatrices view = views.get(0);
260 const Gaussian gs = srt.get(gs_index);
261
262 /* Splat computation and output. */
264 if (pipeline.do_ellipses) [[static_branch]] {
265 splat = eval_splat(gs, obj, view, srt);
266 srt.ellipses_comp[gs_index] = splat.is_culled() ? uint2(0) : splat.pack();
267 }
268
269 /* Base/full radiance computation and output. */
270 if (pipeline.do_radiance) [[static_branch]] {
271 float4 radiance = float4(0.0f, 0.0f, 0.0f, 1.0f);
272
273 /* Only compute radiance if necessary. */
274 if (pipeline.do_ellipses) [[static_branch]] {
275 if (!splat.is_culled()) {
276 radiance.rgb = eval_radiance<4>(gs, obj, view, srt);
277 }
278 }
279 else {
280 radiance.rgb = eval_radiance<4>(gs, obj, view, srt);
281 }
282
283 /* Radiance is packed to fp16. */
284 srt.radiance_comp[gs_index] = uint2(packHalf2x16(radiance.xy), packHalf2x16(radiance.zw));
285 }
286}
287
288/* clang-format off */
289
290PipelineCompute compute_ellipses(compute_entry, Pipeline{.do_ellipses = true, .do_radiance = false});
291PipelineCompute compute_radiance(compute_entry, Pipeline{.do_ellipses = false, .do_radiance = true});
292PipelineCompute compute_ellipses_radiance(compute_entry, Pipeline{.do_ellipses = true, .do_radiance = true});
293
294/* clang-format on */
295
296} // namespace draw::gsplat
btScalar max
#define DRW_GSPLAT_GROUP_SIZE
#define DRW_OBJ_DATA_INFO_UBO_SLOT
#define uniform(slot)
#define storage(slot, qualifiers)
#define push_constant
#define compilation_constant
#define resource_table
#define static_branch
#define global_invocation_id
#define compute
#define local_size(...)
#define isnan
bool any(VecOp< bool, D >) RET
#define pow
VecBase< float, D > normalize(VecOp< float, D >) RET
float dot(VecOp< float, D >, VecOp< float, D >) RET
#define abs
VecBase< float, D > step(VecOp< float, D >, VecOp< float, D >) RET
uint packHalf2x16(float2) RET
VecBase< bool, D > equal(VecOp< T, D >, VecOp< T, D >) RET
uint packSnorm2x16(float2) RET
float3 transform_direction(float3x3 mat, float3 direction)
uint3 slice_tuple_xynz(uint t1, uint t2)
float3 scene_linear_from_bt709(float3 rgb_color)
float3 unpack_sh(uint3 data, ScalingRange range)
uint3 slice_tuple_xnyz(uint t1, uint t2)
bool is_inside_viewport(AxisTuple axes, float3 hP, float2 viewport_size)
float2 pack_normal_octahedral(float3 N)
float3 project_gaussian(ObjectMatrices object, ViewMatrices view, Gaussian gs)
PipelineCompute compute_radiance(compute_entry, Pipeline{.do_ellipses=false,.do_radiance=true})
AxisTuple covmat_to_axes(float3 covmat_2d_v)
float3 get_planar_normal(Quaternion rotation, float3 scale)
void compute_entry(Resources &srt, const Pipeline &pipeline, const draw::View &views, const draw::Model &models, const uint3 global_id)
PipelineCompute compute_ellipses(compute_entry, Pipeline{.do_ellipses=true,.do_radiance=false})
SplatData eval_splat(Gaussian gs, ObjectMatrices obj, ViewMatrices view, const Resources &srt)
float3 eval_radiance(Gaussian gs, ObjectMatrices obj, ViewMatrices view, const Resources &srt)
PipelineCompute compute_ellipses_radiance(compute_entry, Pipeline{.do_ellipses=true,.do_radiance=true})
#define mix
#define min(a, b)
Definition sort.cc:38
struct ScalingRange splat_mean_range
struct ScalingRange splat_scale_range
float3 point_object_to_world(float3 lP) const
float3 scale_from_unit(float3 value) const
bool is_perspective() const
float3 point_world_to_ndc(float3 P) const
float3 world_incident_vector(float3 P) const
static Gaussian unpack(uint gs_id, uint4 data, ScalingRange mean_range, ScalingRange scale_range)
const uint4(& radiance_data)[]
const GSplatInfos & infos
const uint4(& shape_data)[]
Gaussian get(uint gs_id) const
static SplatData culled()
float z
Definition sky_math.h:136
uint x
Definition types_uint4.h:13
uint y
Definition types_uint4.h:13
uint z
Definition types_uint4.h:13
uint w
Definition types_uint4.h:13