Blender V5.3
external_engine.cc
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2017 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
11
12#include "BKE_paint.hh"
13#include "DRW_engine.hh"
14#include "DRW_render.hh"
15
16#include "BLI_string.hh"
17
18#include "BLT_translation.hh"
19
20#include "DNA_particle_types.h"
21#include "DNA_screen_types.h"
22#include "DNA_view3d_types.h"
23
24#include "ED_image.hh"
25#include "ED_render.hh"
26#include "ED_screen.hh"
27#include "ED_view3d.hh"
28
29#include "GPU_debug.hh"
30#include "GPU_matrix.hh"
31#include "GPU_state.hh"
32
33#include "RE_engine.h"
34#include "RE_pipeline.h"
35
36#include "draw_cache.hh"
37#include "draw_cache_impl.hh"
38#include "draw_command.hh"
39#include "draw_common.hh"
40#include "draw_pass.hh"
41#include "draw_sculpt.hh"
42#include "draw_view.hh"
43#include "draw_view_data.hh"
44
45#include "external_engine.h" /* own include */
46
47namespace blender {
48
49/* Shaders */
50
51namespace draw::external {
52
58class Prepass {
59 private:
60 PassMain ps_ = {"prepass"};
61 PassMain::Sub *mesh_ps_ = nullptr;
62 PassMain::Sub *curves_ps_ = nullptr;
63 PassMain::Sub *pointcloud_ps_ = nullptr;
64 PassMain::Sub *gsplat_ps_ = nullptr;
65
66 /* Reuse overlay shaders. */
67 gpu::StaticShader depth_mesh = {"overlay_depth_mesh"};
68 gpu::StaticShader depth_curves = {"overlay_depth_curves"};
69 gpu::StaticShader depth_pointcloud = {"overlay_depth_pointcloud"};
70 gpu::StaticShader depth_gsplat = {"overlay_depth_gsplat"};
71
73
74 public:
76 {
77 dummy_buf.push_update();
78
79 ps_.init();
81 /* Dummy binds. They are unused in the variant we use.
82 * Just avoid validation layers complaining. */
83 ps_.bind_ubo(OVERLAY_GLOBALS_SLOT, &dummy_buf);
84 ps_.bind_ubo(DRW_CLIPPING_UBO_SLOT, &dummy_buf);
85 {
86 auto &sub = ps_.sub("Mesh");
87 sub.shader_set(depth_mesh.get());
88 mesh_ps_ = ⊂
89 }
90 {
91 auto &sub = ps_.sub("Curves");
92 sub.shader_set(depth_curves.get());
93 curves_ps_ = ⊂
94 }
95 {
96 auto &sub = ps_.sub("PointCloud");
97 sub.shader_set(depth_pointcloud.get());
98 pointcloud_ps_ = ⊂
99 }
100 {
101 auto &sub = ps_.sub("GSplat");
102 sub.shader_set(depth_gsplat.get());
103 gsplat_ps_ = ⊂
104 }
105 }
106
107 void particle_sync(Manager &manager, const ObjectRef &ob_ref)
108 {
109 Object *ob = ob_ref.object;
110
111 ResourceHandleRange handle = {};
112
113 for (ParticleSystem &psys : ob->particlesystem) {
115 continue;
116 }
117
118 const ParticleSettings *part = psys.part;
119 const int draw_as = (part->draw_as == PART_DRAW_REND) ? part->ren_as : part->draw_as;
120 if (draw_as == PART_DRAW_PATH && part->draw_as == PART_DRAW_REND) {
121 /* Case where the render engine should have rendered it, but we need to draw it for
122 * selection purpose. */
123 if (!handle.is_valid()) {
124 handle = manager.resource_handle_for_psys(ob_ref, ob_ref.particles_matrix());
125 }
126
127 gpu::Batch *geom = DRW_cache_particles_get_hair(ob, &psys, nullptr);
128 mesh_ps_->draw(geom, handle);
129 break;
130 }
131 }
132 }
133
134 void sculpt_sync(Manager &manager, const ObjectRef &ob_ref)
135 {
136 ResourceHandleRange handle = manager.unique_handle_for_sculpt(ob_ref);
137
139 mesh_ps_->draw(batch.batch, handle);
140 }
141 }
142
143 void object_sync(Manager &manager, const ObjectRef &ob_ref, const DRWContext &draw_ctx)
144 {
145 bool is_solid = ob_ref.object->dt >= OB_SOLID ||
147
148 if (!is_solid) {
149 return;
150 }
151
152 particle_sync(manager, ob_ref);
153
154 const bool use_sculpt_pbvh = BKE_sculptsession_use_pbvh_draw(ob_ref.object, draw_ctx.rv3d);
155
156 if (use_sculpt_pbvh) {
157 sculpt_sync(manager, ob_ref);
158 return;
159 }
160
161 gpu::Batch *geom_single = nullptr;
162 Span<gpu::Batch *> geom_list(&geom_single, 1);
163
164 ResourceHandleRange res_handle = manager.unique_handle(ob_ref);
165
166 PassMain::Sub *pass = nullptr;
167 switch (ob_ref.object->type) {
168 case OB_MESH:
169 geom_single = DRW_cache_mesh_surface_get(ob_ref.object);
170 pass = mesh_ps_;
171 break;
172 case OB_POINTCLOUD:
173 pass = pointcloud_is_gsplat(ob_ref.object) ? gsplat_ps_ : pointcloud_ps_;
174 geom_single = pointcloud_sub_pass_setup(*pass, ob_ref, res_handle);
175 break;
176 case OB_CURVES: {
177 const char *error = nullptr;
178 /* We choose to ignore the error here as the external engine can display them properly.
179 * The overlays can still be broken but it should be detected in solid mode. */
180 geom_single = curves_sub_pass_setup(*curves_ps_, draw_ctx.scene, ob_ref.object, error);
181 pass = curves_ps_;
182 break;
183 }
184 default:
185 break;
186 }
187
188 if (pass == nullptr) {
189 return;
190 }
191
192 for (int material_id : geom_list.index_range()) {
193 pass->draw(geom_list[material_id], res_handle);
194 }
195 }
196
197 void submit(Manager &manager, View &view)
198 {
199 manager.submit(ps_, view);
200 }
201};
202
203class Instance : public DrawEngine {
204 const DRWContext *draw_ctx = nullptr;
205
206 Prepass prepass;
207 /* Only do prepass if there is a need for it.
208 * This is only needed for GPencil integration. */
209 bool do_prepass = false;
210
211 StringRefNull name_get() final
212 {
213 return "External";
214 }
215
216 bool uses_render_border() const final
217 {
218 return true;
219 }
220
221 void init() final
222 {
223 draw_ctx = DRW_context_get();
224 const bool engine_provides_depth =
225 draw_ctx->v3d != nullptr &&
226 (ED_view3d_engine_type(draw_ctx->scene, draw_ctx->v3d->shading.type)->flag &
228 do_prepass = !engine_provides_depth &&
229 DRW_render_check_grease_pencil(draw_ctx->depsgraph, draw_ctx->v3d);
230 }
231
232 void begin_sync() final
233 {
234 if (do_prepass) {
235 prepass.begin_sync();
236 }
237 }
238
239 void object_sync(draw::ObjectRef &ob_ref, draw::Manager &manager) final
240 {
241 if (do_prepass) {
242 prepass.object_sync(manager, ob_ref, *draw_ctx);
243 }
244 }
245
246 void end_sync() final {}
247
248 void draw_scene_do_v3d(draw::Manager &manager, draw::View &view)
249 {
250 RegionView3D *rv3d = draw_ctx->rv3d;
251 ARegion *region = draw_ctx->region;
252 RenderEngineType *engine_type = ED_view3d_engine_type(draw_ctx->scene,
253 draw_ctx->v3d->shading.type);
254
255 if (engine_type->flag & RE_WRITE_VIEWPORT_DEPTH) {
258 }
259 else {
261 }
262
263 /* The external engine can use the OpenGL rendering API directly, so make sure the state is
264 * already applied.
265 * We should only need this before view_draw, but we do it here for compatibility
266 * (matching previous Blender versions) */
268
269 /* Create render engine. */
270 RenderEngine *render_engine = nullptr;
271 if (!rv3d->view_render) {
272 if (!(engine_type->view_update && engine_type->view_draw)) {
273 return;
274 }
275
276 rv3d->view_render = RE_NewViewRender(engine_type);
277 render_engine = RE_view_engine_get(rv3d->view_render);
278
279 if (ScrArea *area = CTX_wm_area(draw_ctx->evil_C)) {
281 }
282
283 engine_type->view_update(render_engine, draw_ctx->evil_C, draw_ctx->depsgraph);
284 }
285 else {
286 render_engine = RE_view_engine_get(rv3d->view_render);
287 }
288
289 /* Rendered draw. */
292 ED_region_pixelspace(region);
293
294 /* Render result draw. */
295 engine_type->view_draw(render_engine, draw_ctx->evil_C, draw_ctx->depsgraph);
296
299
300 if (do_prepass) {
301 prepass.submit(manager, view);
302 }
303
304 /* Set render info. */
305 if (render_engine->text[0] != '\0') {
306 STRNCPY(info, render_engine->text);
307 }
308 else {
309 info[0] = '\0';
310 }
311 }
312
313 /* Configure current matrix stack so that the external engine can use the same drawing code for
314 * both viewport and image editor drawing.
315 *
316 * The engine draws result in the pixel space, and is applying render offset. For image editor we
317 * need to switch from normalized space to pixel space, and "un-apply" offset. */
318 void external_image_space_matrix_set(const RenderEngine *engine)
319 {
320 BLI_assert(engine != nullptr);
321
322 SpaceImage *space_image = reinterpret_cast<SpaceImage *>(draw_ctx->space_data);
323
324 /* Apply current view as transformation matrix.
325 * This will configure drawing for normalized space with current zoom and pan applied. */
326
327 float4x4 view_matrix = draw::View::default_get().viewmat();
328 float4x4 projection_matrix = draw::View::default_get().winmat();
329
330 GPU_matrix_projection_set(projection_matrix.ptr());
331 GPU_matrix_set(view_matrix.ptr());
332
333 /* Switch from normalized space to pixel space. */
334 {
335 int width, height;
336 ED_space_image_get_size(space_image, &width, &height);
337
338 const float width_inv = width ? 1.0f / width : 0.0f;
339 const float height_inv = height ? 1.0f / height : 0.0f;
340 GPU_matrix_scale_2f(width_inv, height_inv);
341 }
342
343 /* Un-apply render offset. */
344 {
345 Render *render = engine->re;
346 rctf view_rect;
347 rcti render_rect;
348 RE_GetViewPlane(render, &view_rect, &render_rect);
349
350 GPU_matrix_translate_2f(-render_rect.xmin, -render_rect.ymin);
351 }
352 }
353
354 void draw_scene_do_image()
355 {
356 /* Get scene from the render job, to show progress for scenes render as part
357 * of compositor or sequencer. */
358 Scene *scene = ED_render_job_get_current_scene(draw_ctx->evil_C);
359 if (scene == nullptr) {
360 scene = draw_ctx->scene;
361 }
362
363 Render *re = RE_GetSceneRender(scene);
364 RenderEngine *engine = RE_engine_get(re);
365
366 /* Is tested before enabling the drawing engine. */
367 BLI_assert(re != nullptr);
368 BLI_assert(engine != nullptr);
369
371
372 /* The external engine can use the OpenGL rendering API directly, so make sure the state is
373 * already applied. */
375
376 const DefaultFramebufferList *dfbl = draw_ctx->viewport_framebuffer_list_get();
377
378 /* Clear the depth buffer to the value used by the background overlay so that the overlay is
379 * not happening outside of the drawn image.
380 *
381 * NOTE: The external engine only draws color. The depth is taken care of using the depth pass
382 * which initialized the depth to the values expected by the background overlay. */
384
387
388 external_image_space_matrix_set(engine);
389
390 GPU_debug_group_begin("External Engine");
391
392 const RenderEngineType *engine_type = engine->type;
393 BLI_assert(engine_type != nullptr);
394 BLI_assert(engine_type->draw != nullptr);
395
396 engine_type->draw(engine, draw_ctx->evil_C, draw_ctx->depsgraph);
397
399
402
404
406 }
407
408 void draw_scene_do(draw::Manager &manager, View &view)
409 {
410 if (draw_ctx->v3d != nullptr) {
411 draw_scene_do_v3d(manager, view);
412 return;
413 }
414
415 if (draw_ctx->space_data == nullptr) {
416 return;
417 }
418
419 const eSpace_Type space_type = eSpace_Type(draw_ctx->space_data->spacetype);
420 if (space_type == SPACE_IMAGE) {
421 draw_scene_do_image();
422 return;
423 }
424 }
425
426 void draw(draw::Manager &manager) final
427 {
428 /* TODO(fclem): Remove global access. */
429 View &view = View::default_get();
430
431 const DefaultFramebufferList *dfbl = draw_ctx->viewport_framebuffer_list_get();
432
433 /* Will be nullptr during OpenGL render.
434 * OpenGL render is used for quick preview (thumbnails or sequencer preview)
435 * where using the rendering engine to preview doesn't make so much sense. */
436 if (draw_ctx->evil_C) {
437 /* This is to keep compatibility with external engine. */
438 /* TODO(fclem): remove it eventually. */
440 GPU_framebuffer_clear_color(dfbl->default_fb, {0.0, 0.0, 0.0, 0.0});
441
443 draw_scene_do(manager, view);
445 }
446 }
447};
448
450{
451 return new Instance();
452}
453
454} // namespace draw::external
455
456/* Functions */
457
458/* NOTE: currently unused,
459 * we should not register unless we want to see this when debugging the view. */
460
462 /*next*/ nullptr,
463 /*prev*/ nullptr,
464 /*idname*/ "BLENDER_EXTERNAL",
465 /*name*/ N_("External"),
467 /*update*/ nullptr,
468 /*render*/ nullptr,
469 /*render_frame_finish*/ nullptr,
470 /*draw*/ nullptr,
471 /*bake*/ nullptr,
472 /*view_update*/ nullptr,
473 /*view_draw*/ nullptr,
474 /*view_pause*/ nullptr,
475 /*view_resume*/ nullptr,
476 /*update_script_node*/ nullptr,
477 /*update_render_passes*/ nullptr,
478 /*update_custom_camera*/ nullptr,
479 /*rna_ext*/
480 {
481 /*data*/ nullptr,
482 /*srna*/ nullptr,
483 /*call*/ nullptr,
484 },
485};
486
488{
489 /* Get scene from the render job, to show progress for scenes render as part
490 * of compositor or sequencer. */
491 Scene *scene = ED_render_job_get_current_scene(draw_ctx->evil_C);
492 if (scene == nullptr) {
493 scene = draw_ctx->scene;
494 }
495
496 const SpaceLink *space_data = draw_ctx->space_data;
497 if (space_data == nullptr) {
498 return false;
499 }
500
501 const eSpace_Type space_type = eSpace_Type(draw_ctx->space_data->spacetype);
502 if (space_type != SPACE_IMAGE) {
503 return false;
504 }
505
506 SpaceImage *space_image = reinterpret_cast<SpaceImage *>(const_cast<SpaceLink *>(space_data));
507 const Image *image = ED_space_image(space_image);
508 if (image == nullptr || image->type != IMA_TYPE_R_RESULT) {
509 return false;
510 }
511
512 if (image->render_slot != image->last_render_slot) {
513 return false;
514 }
515
516 /* Render is allocated on main thread, so it is safe to access it from here. */
517 Render *re = RE_GetSceneRender(scene);
518
519 if (re == nullptr) {
520 return false;
521 }
522
523 return RE_engine_draw_acquire(re);
524}
525
527{
528 if (rv3d->view_render) {
529 /* Free engine with DRW context enabled, as this may clean up per-context
530 * resources like VAOs. */
531 bool swap_context = !DRW_gpu_context_is_enabled();
532 if (swap_context) {
534 }
536 rv3d->view_render = nullptr;
537 if (swap_context) {
539 }
540 }
541}
542
543} // namespace blender
constexpr IndexRange index_range() const
Definition BLI_span.hh:405
ResourceHandle resource_handle_for_psys(const ObjectRef &ref, const float4x4 &model_matrix)
ResourceHandleRange unique_handle_for_sculpt(const ObjectRef &ref)
ResourceHandleRange unique_handle(const ObjectRef &ref)
void submit(PassSimple &pass, View &view)
float4x4 particles_matrix() const
const float4x4 & winmat(int view_id=0) const
Definition draw_view.hh:148
static View & default_get()
Definition draw_view.cc:317
const float4x4 & viewmat(int view_id=0) const
Definition draw_view.hh:136
void draw(gpu::Batch *batch, uint instance_len=-1, uint vertex_len=-1, uint vertex_first=-1, ResourceIDRange res_id={}, uint custom_id=0)
Definition draw_pass.hh:906
detail::PassBase< command::DrawMultiBuf > Sub
Definition draw_pass.hh:509
void sculpt_sync(Manager &manager, const ObjectRef &ob_ref)
void particle_sync(Manager &manager, const ObjectRef &ob_ref)
void object_sync(Manager &manager, const ObjectRef &ob_ref, const DRWContext &draw_ctx)
void submit(Manager &manager, View &view)
#define DRW_CLIPPING_UBO_SLOT
#define OVERLAY_GLOBALS_SLOT
detail::Pass< command::DrawMultiBuf > PassMain
gpu::Batch * curves_sub_pass_setup(PassMain::Sub &ps, const Scene *scene, Object *ob, const char *&r_error, GPUMaterial *gpu_material=nullptr)
Vector< SculptBatch > sculpt_batches_get(const Object *ob, SculptBatchFeature features)
bool pointcloud_is_gsplat(Object *ob)
gpu::Batch * pointcloud_sub_pass_setup(PassMain::Sub &sub_ps, const ObjectRef &ob_ref, const ResourceHandleRange &res_handle, GPUMaterial *gpu_material=nullptr)
gpu::Batch * DRW_cache_particles_get_hair(Object *object, ParticleSystem *psys, ModifierData *md)
gpu::Batch * DRW_cache_mesh_surface_get(Object *ob)
BMesh Iterators.
void GPU_framebuffer_bind(gpu::FrameBuffer *fb)
BLI_assert(slot->slot_subtype.map==BMO_OP_SLOT_SUBTYPE_MAP_FLT)
ScrArea * CTX_wm_area(const bContext *C)
char * STRNCPY(char(&dst)[N], const char *src)
RenderEngine * RE_engine_get(const Render *re)
void ED_space_image_get_size(SpaceImage *sima, int *r_width, int *r_height)
@ RE_WRITE_VIEWPORT_DEPTH
Definition RE_engine.h:58
@ RE_INTERNAL
Definition RE_engine.h:45
@ RE_USE_STEREO_VIEWPORT
Definition RE_engine.h:51
void GPU_framebuffer_clear_color(gpu::FrameBuffer *fb, const double4 clear_col)
RenderEngineType DRW_engine_viewport_external_type
void DRW_gpu_context_disable_ex(bool restore)
RenderEngineType * ED_view3d_engine_type(const Scene *scene, int drawtype)
MatBase< float, 4, 4 > float4x4
void DRW_gpu_context_enable_ex(bool restore)
bool DRW_gpu_context_is_enabled()
void ED_area_tag_redraw_regiontype(ScrArea *area, int regiontype)
Definition area.cc:802
void GPU_apply_state()
Definition gpu_state.cc:294
void GPU_matrix_scale_2f(float x, float y)
void DRW_engine_external_free(RegionView3D *rv3d)
Render * RE_GetSceneRender(const Scene *scene)
bool DRW_engine_external_acquire_for_image_editor(const DRWContext *draw_ctx)
bool DRW_object_is_visible_psys_in_active_context(const Object *object, const ParticleSystem *psys)
@ DRW_STATE_WRITE_COLOR
Definition draw_state.hh:32
@ DRW_STATE_WRITE_DEPTH
Definition draw_state.hh:31
@ DRW_STATE_DEPTH_LESS_EQUAL
Definition draw_state.hh:40
void DRW_submission_end()
const DRWContext * DRW_context_get()
void GPU_matrix_projection_set(const float m[4][4])
void GPU_matrix_translate_2f(float x, float y)
bool DRW_render_check_grease_pencil(Depsgraph *depsgraph, View3D *v3d=nullptr)
void RE_engine_draw_release(Render *re)
void GPU_framebuffer_clear_depth(gpu::FrameBuffer *fb, float clear_depth)
void GPU_debug_group_end()
Definition gpu_debug.cc:34
bool RE_engine_draw_acquire(Render *re)
void ED_region_pixelspace(const ARegion *region)
Definition area.cc:96
bool BKE_sculptsession_use_pbvh_draw(const Object *ob, const RegionView3D *rv3d)
void GPU_matrix_pop_projection()
ViewRender * RE_NewViewRender(RenderEngineType *engine_type)
void DRW_submission_start()
RenderEngine * RE_view_engine_get(const ViewRender *view_render)
void RE_GetViewPlane(Render *re, rctf *r_viewplane, rcti *r_disprect)
void GPU_matrix_pop()
void GPU_matrix_push_projection()
void GPU_matrix_set(const float m[4][4])
Image * ED_space_image(const SpaceImage *sima)
Definition image_edit.cc:43
void RE_FreeViewRender(ViewRender *view_render)
void GPU_matrix_push()
void GPU_debug_group_begin(const char *name)
Definition gpu_debug.cc:23
Scene * ED_render_job_get_current_scene(const bContext *C)
RegionView3D * rv3d
const bContext * evil_C
SpaceLink * space_data
char info[GPU_INFO_SIZE]
Definition DRW_render.hh:74
const c_style_mat & ptr() const
ListBaseT< struct ParticleSystem > particlesystem
eObject_VisibilityFlag visibility_flag
struct ViewRender * view_render
void(*) view_update(struct RenderEngine *engine, const struct bContext *context, struct Depsgraph *depsgraph)
Definition RE_engine.h:111
void(*) view_draw(struct RenderEngine *engine, const struct bContext *context, struct Depsgraph *depsgraph)
Definition RE_engine.h:114
void(*) draw(struct RenderEngine *engine, const struct bContext *context, struct Depsgraph *depsgraph)
Definition RE_engine.h:99
struct Depsgraph * depsgraph
Definition RE_engine.h:165
RenderEngineType * type
Definition RE_engine.h:141
struct Render * re
Definition RE_engine.h:148
static void set(DRWState state=DRW_STATE_DEFAULT)
DrawEngine * create_instance() final
#define N_(msgid)