Blender V5.3
draw_gsplat.cc
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2026 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
8
10
11#include "GPU_batch.hh"
12#include "GPU_capabilities.hh"
13#include "GPU_material.hh"
14#include "GPU_shader.hh"
15#include "GPU_texture.hh"
16#include "GPU_vertex_buffer.hh"
17
18#include "DRW_render.hh"
19
20#include "draw_cache_impl.hh"
21#include "draw_common.hh"
22#include "draw_common_c.hh"
25#include "draw_shader.hh"
26/* For drw_curves_get_attribute_sampler_name. */
28
29namespace blender::draw {
30
31void DRW_gsplat_init(DRWData *drw_data)
32{
33 if (drw_data == nullptr) {
34 drw_data = drw_get().data;
35 }
36 if (drw_data->gsplat_module == nullptr) {
37 drw_data->gsplat_module = MEM_new<GSplatModule>("GSplatModule");
38 }
39}
40
42{
43 MEM_delete(gsplat_module);
44}
45
46namespace detail {
47
49static void batched_dispatch(const int invocs_count, PassSimple::Sub &pass)
50{
51 const int64_t max_invocs_per_dispatch = int64_t(GPU_max_work_group_count(0)) *
53 int invocs_start = 0;
54 while (invocs_start < invocs_count) {
55 int num_invocs = std::min(int64_t(invocs_count - invocs_start), max_invocs_per_dispatch);
56 pass.push_constant("invocs_start", invocs_start);
57 pass.push_constant("invocs_count", invocs_count);
59 invocs_start += num_invocs;
60 }
61}
62
63template<typename PassT>
64void compute_pass_setup(PassT &ps, const ObjectRef &ob_ref, const ResourceHandleRange &res_handle)
65{
67 GSplatEvalCache &cache = GSplatEvalCache::get(pointcloud);
68
69 /* TODO(not_mark): account for instancing in compute pass, otherwise int(res_handle.index()). */
70 const int resource_id = res_handle.id_range().first();
71 const float2 viewport_size = DRW_context_get()->viewport_size_get();
72
73 auto &sub_ps = ps.sub("GSplatSubPass");
74
75 sub_ps.bind_ubo("infos", cache.infos_buf_get());
76
77 sub_ps.bind_ssbo("shape_data", cache.shape_data_buf_get());
78 sub_ps.bind_ssbo("radiance_data", cache.radiance_data_buf_get());
79 sub_ps.bind_ssbo("ellipses_comp", cache.ellipses_comp_buf_get());
80 sub_ps.bind_ssbo("radiance_comp", cache.radiance_comp_buf_get());
81
82 sub_ps.push_constant("viewport_size", viewport_size);
83 sub_ps.push_constant("resource_id", resource_id);
84
85 batched_dispatch(pointcloud.totpoint, sub_ps);
86}
87
88template<typename PassT>
89gpu::Batch *draw_subpass_setup(PassT &sub_ps,
90 const ObjectRef &ob_ref,
91 const ResourceHandleRange &res_handle,
92 GPUMaterial *gpu_material)
93{
94 const Object *object = ob_ref.object;
96
98 if (pointcloud.type != PointCloudType::GSplat) {
99 return nullptr;
100 }
101
102 /* An empty pointcloud should never result in a draw-call. However, the buffer binding commands
103 * will still be executed. In this case, in order to avoid assertion, we bind dummy VBOs. */
104 const bool is_empty = pointcloud.totpoint == 0;
105
106 /* GSplats currently rely on per-object, per-view compute prepasses, and geometry subpasses then
107 * mostly resolve their outputs into the view. */
109 if (!is_empty) {
110 module.sync_object(ob_ref, res_handle);
111 }
112
113 /* Ensure we have no unbound resources, required for Vulkan.
114 * Certain GL drivers also refuse to draw anything otherwise. */
115 sub_ps.bind_texture("u", module.dummy_vbo);
116 sub_ps.bind_texture("au", module.dummy_vbo);
117 sub_ps.bind_texture("a", module.dummy_vbo);
118 sub_ps.bind_texture("c", module.dummy_vbo);
119 sub_ps.bind_texture("ac", module.dummy_vbo);
120
121 GSplatEvalCache &cache = GSplatEvalCache::get(pointcloud);
122 sub_ps.bind_ubo("infos", is_empty ? module.dummy_ubo : cache.infos_buf_get());
123 sub_ps.bind_texture("shape_data_tx", is_empty ? module.dummy_vbo : cache.shape_data_buf_get());
124 sub_ps.bind_texture("ellipses_comp_tx",
125 is_empty ? &module.dummy_vbo : cache.ellipses_comp_buf_get());
126 sub_ps.bind_texture("radiance_comp_tx",
127 is_empty ? &module.dummy_vbo : cache.radiance_comp_buf_get());
128
129 if (gpu_material != nullptr) {
130 ListBaseT<GPUMaterialAttribute> gpu_attrs = GPU_material_attributes(gpu_material);
131 for (GPUMaterialAttribute &gpu_attr : gpu_attrs) {
133 char sampler_name[32];
134 drw_curves_get_attribute_sampler_name(gpu_attr.name, sampler_name);
135
136 gpu::VertBuf **attribute_buf = DRW_gsplat_evaluated_attribute(&pointcloud, gpu_attr.name);
137 if (STREQ(gpu_attr.name, "radiance")) {
138 /* If the "radiance" attribute is provided, we do not use spherical harmonics output,
139 * but instead use the provided attribute as a color directly. */
140 sub_ps.bind_texture(sampler_name,
141 is_empty ?
142 &module.dummy_vbo :
143 (attribute_buf ? attribute_buf : cache.radiance_comp_buf_get()));
144 }
145 else {
146 sub_ps.bind_texture(sampler_name,
147 (attribute_buf && !is_empty) ? attribute_buf : &module.dummy_vbo);
148 }
149 }
150 }
151
152 return cache.surface_get(pointcloud);
153}
154
155} // namespace detail
156
158{
159 compute_objects_.clear();
160
161 compute_ellipses_ps_.init();
162 compute_ellipses_ps_.state_set(DRW_STATE_NO_DRAW);
163 compute_radiance_ps_.init();
164 compute_radiance_ps_.state_set(DRW_STATE_NO_DRAW);
165 compute_ellipses_radiance_ps_.init();
166 compute_ellipses_radiance_ps_.state_set(DRW_STATE_NO_DRAW);
167}
168
169void GSplatModule::sync_object(const ObjectRef &ob_ref, const ResourceHandleRange &res_handle)
170{
171 /* Do not create subpasses if these are already created for this object
172 * in an earlier object_sync. Passes can be reused across submissions. */
173 /* TODO(not_mark): account for instancing in sync. For now, take first object. */
174 ObjectKey ob_key = ob_ref.is_range() ? ObjectKey(ob_ref, 0) : ObjectKey(ob_ref);
175 if (compute_objects_.contains(ob_key)) {
176 return;
177 }
178 if (compute_objects_.is_empty()) {
179 /* First `sync_object` that adds a gsplat. Get shaders now, to avoid
180 * unnecessarily compiling in scenes without splats. */
181 compute_ellipses_ps_.shader_set(DRW_shader_gsplat_compute_get(GSplatEvalShader::Ellipses));
182 compute_radiance_ps_.shader_set(DRW_shader_gsplat_compute_get(GSplatEvalShader::Radiance));
183 compute_ellipses_radiance_ps_.shader_set(
185 }
186 compute_objects_.add(ob_key);
187
188 /* Synchronize compute passes. */
189 detail::compute_pass_setup(compute_ellipses_ps_, ob_ref, res_handle);
190 detail::compute_pass_setup(compute_radiance_ps_, ob_ref, res_handle);
191 detail::compute_pass_setup(compute_ellipses_radiance_ps_, ob_ref, res_handle);
192}
193
195{
196 if (compute_objects_.is_empty()) {
197 /* No objects synced, skip submission. */
198 return;
199 }
200 manager.submit(get_ps(type), view);
201}
202
204{
206 module.begin_sync();
207}
208
210{
212 module.update(manager, view, GSplatEvalShader::Ellipses);
213}
214
216{
218 module.update(manager, view, GSplatEvalShader::Radiance);
219}
220
222{
224 module.update(manager, view, GSplatEvalShader::EllipsesRadiance);
225}
226
228{
229 /* GSplats and point-clouds share the base DNA type. We treat them separately
230 * in draw engines as the implementations are quite different. This check is
231 * done often, so it is kept as a helper function for now. */
232 if (object->type == OB_POINTCLOUD) {
234 return pointcloud.type == PointCloudType::GSplat;
235 }
236 return false;
237}
238
240 const ObjectRef &ob_ref,
241 const ResourceHandleRange &res_handle,
242 GPUMaterial *gpu_material)
243{
244 return detail::draw_subpass_setup(sub_ps, ob_ref, res_handle, gpu_material);
245}
246
247gpu::Batch *gsplat_sub_pass_setup(PassSimple::Sub &sub_ps,
248 const ObjectRef &ob_ref,
249 const ResourceHandleRange &res_handle,
250 GPUMaterial *gpu_material)
251{
252 return detail::draw_subpass_setup(sub_ps, ob_ref, res_handle, gpu_material);
253}
254
255} // namespace blender::draw
#define STREQ(a, b)
constexpr int64_t first() const
void submit(PassSimple &pass, View &view)
void dispatch(int group_len)
void push_constant(const char *name, const float &data)
detail::PassBase< command::DrawCommandBuf > Sub
Definition draw_pass.hh:509
#define DRW_GSPLAT_GROUP_SIZE
gpu::Batch * draw_subpass_setup(PassT &sub_ps, const ObjectRef &ob_ref, const ResourceHandleRange &res_handle, GPUMaterial *gpu_material)
void compute_pass_setup(PassT &ps, const ObjectRef &ob_ref, const ResourceHandleRange &res_handle)
static void batched_dispatch(const int invocs_count, PassSimple::Sub &pass)
void DRW_gsplat_module_free(draw::GSplatModule *module)
void DRW_gsplat_ensure_radiance(draw::Manager &manager, draw::View &view)
void DRW_gsplat_ensure_ellipses_radiance(draw::Manager &manager, draw::View &view)
bool pointcloud_is_gsplat(Object *ob)
void DRW_gsplat_init(DRWData *drw_data=nullptr)
void drw_curves_get_attribute_sampler_name(const StringRef layer_name, char r_sampler_name[32])
gpu::VertBuf ** DRW_gsplat_evaluated_attribute(PointCloud *pointcloud, StringRef name)
void DRW_gsplat_ensure_ellipses(draw::Manager &manager, draw::View &view)
void DRW_gsplat_begin_sync()
gpu::Batch * gsplat_sub_pass_setup(PassMain::Sub &sub_ps, const ObjectRef &ob_ref, const ResourceHandleRange &res_handle, GPUMaterial *gpu_material=nullptr)
BLI_assert(slot->slot_subtype.map==BMO_OP_SLOT_SUBTYPE_MAP_FLT)
__int64 int64_t
Definition BKE_text.h:90
gpu::Shader * DRW_shader_gsplat_compute_get(draw::GSplatEvalShader type)
DRWContext & drw_get()
int GPU_max_work_group_count(int index)
VecBase< float, 2 > float2
T & DRW_object_get_data_for_drawing(const Object &object)
ListBaseT< GPUMaterialAttribute > GPU_material_attributes(const GPUMaterial *material)
@ DRW_STATE_NO_DRAW
Definition draw_state.hh:29
const DRWContext * DRW_context_get()
MINLINE uint divide_ceil_u(uint a, uint b)
float2 viewport_size_get() const
draw::GSplatModule * gsplat_module
gpu::Batch * surface_get(const PointCloud &pointcloud)
static GSplatEvalCache & get(PointCloud &pointcloud)
void update(draw::Manager &manager, draw::View &view, GSplatEvalShader type)
void sync_object(const ObjectRef &ob_ref, const ResourceHandleRange &res_handle)