Blender V5.3
workbench_engine.cc
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2023 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
8
9#include "BLI_rect.hh"
10#include "BLI_string.hh"
11
12#include "DNA_fluid_types.h"
13
14#include "BKE_editmesh.hh"
15#include "BKE_material.hh"
16#include "BKE_modifier.hh"
17#include "BKE_object.hh"
18#include "BKE_paint.hh"
19#include "BKE_paint_bvh.hh"
20#include "BKE_particle.h"
21#include "BKE_report.hh"
22
24
26#include "ED_paint.hh"
27#include "ED_view3d.hh"
28
29#include "BLT_translation.hh"
30
31#include "GPU_context.hh"
32#include "IMB_imbuf_types.hh"
33
34#include "RE_engine.h"
35#include "RE_pipeline.h"
36
37#include "draw_cache.hh"
38#include "draw_common.hh"
39#include "draw_sculpt.hh"
40#include "draw_view_data.hh"
41
42#include "workbench_private.hh"
43
44#include "workbench_engine.h" /* Own include. */
45
46namespace blender {
47
48namespace workbench {
49
50using namespace draw;
51
52class Instance : public DrawEngine {
53 private:
54 View view_ = {"DefaultView"};
55
56 SceneState scene_state_;
57
58 SceneResources resources_;
59
60 OpaquePass opaque_ps_;
61 TransparentPass transparent_ps_;
62 TransparentDepthPass transparent_depth_ps_;
63
64 ShadowPass shadow_ps_;
65 VolumePass volume_ps_;
66 OutlinePass outline_ps_;
67 DofPass dof_ps_;
68 AntiAliasingPass anti_aliasing_ps_;
69
70 /* An array of nullptr GPUMaterial pointers so we can call DRW_cache_object_surface_material_get.
71 * They never get actually used. */
72 Vector<GPUMaterial *> dummy_gpu_materials_ = {1, nullptr, {}};
73
74 /* Used to detect any scene data update. */
75 uint64_t depsgraph_last_update_ = 0;
76
77 const char *hair_buffer_overflow_error_ = nullptr;
78
79 public:
80 const DRWContext *draw_ctx = nullptr;
81
83 {
84 return "Workbench";
85 }
86
88 {
89 if (material_count > dummy_gpu_materials_.size()) {
90 dummy_gpu_materials_.resize(material_count, nullptr);
91 }
92 return dummy_gpu_materials_.as_span().slice(IndexRange(material_count));
93 };
94
95 void init() final
96 {
97 this->draw_ctx = DRW_context_get();
99 }
100
101 void init(Depsgraph *depsgraph, Object *camera_ob = nullptr)
102 {
103 this->draw_ctx = DRW_context_get();
104 bool scene_updated = assign_if_different(depsgraph_last_update_,
106
107 scene_state_.init(this->draw_ctx, scene_updated, camera_ob);
108 shadow_ps_.init(scene_state_, resources_);
109 resources_.init(scene_state_, this->draw_ctx);
110
111 outline_ps_.init(scene_state_);
112 dof_ps_.init(scene_state_, this->draw_ctx);
113 anti_aliasing_ps_.init(scene_state_);
114 }
115
117 {
118 resources_.material_buf.clear_and_trim();
119
120 opaque_ps_.sync(scene_state_, resources_);
121 transparent_ps_.sync(scene_state_, resources_);
122 transparent_depth_ps_.sync(scene_state_, resources_);
123
124 shadow_ps_.sync(resources_);
125 volume_ps_.sync(resources_);
126 outline_ps_.sync(resources_);
127 dof_ps_.sync(resources_, this->draw_ctx);
128 anti_aliasing_ps_.sync(scene_state_, resources_);
129
130 hair_buffer_overflow_error_ = nullptr;
131 }
132
134 {
135 resources_.material_buf.push_update();
136 shadow_ps_.end_sync();
137 }
138
139 Material get_material(ObjectRef ob_ref, eV3DShadingColorType color_type, int slot = 0)
140 {
141 switch (color_type) {
143 return Material(*ob_ref.object);
145 return Material(*ob_ref.object, true);
147 return scene_state_.material_override;
149 return scene_state_.material_attribute_color;
153 if (blender::Material *_mat = BKE_object_material_get_eval(ob_ref.object, slot + 1)) {
154 return Material(*_mat);
155 }
157 default:
159 }
160 }
161
162 void object_sync(ObjectRef &ob_ref, Manager &manager) final
163 {
164 if (scene_state_.render_finished) {
165 return;
166 }
167
168 Object *ob = ob_ref.object;
169 if (!DRW_object_is_renderable(ob)) {
170 return;
171 }
172
173 ObjectState object_state = ObjectState(this->draw_ctx, scene_state_, resources_, ob, manager);
174
175 bool is_object_data_visible = (DRW_object_visibility_in_active_context(ob) &
177 (ob->dt >= OB_SOLID || draw_ctx->is_scene_render());
178
179 if (!(ob->base_flag & BASE_FROM_DUPLI)) {
181 if (md && BKE_modifier_is_enabled(scene_state_.scene, md, eModifierMode_Realtime)) {
182 FluidModifierData *fmd = reinterpret_cast<FluidModifierData *>(md);
183 if (fmd->domain) {
184 volume_ps_.object_sync_modifier(manager, resources_, scene_state_, ob_ref, md);
185
186 if (fmd->domain->type == FLUID_DOMAIN_TYPE_GAS) {
187 /* Do not draw solid in this case. */
188 is_object_data_visible = false;
189 }
190 }
191 }
192 }
193
194 ResourceHandleRange emitter_handle = {};
195
196 if (is_object_data_visible) {
197 if (object_state.sculpt_pbvh) {
199 this->sculpt_sync(manager, ob_ref, handle, object_state);
200 emitter_handle = handle;
201 }
202 else if (ob->type == OB_MESH) {
204 this->mesh_sync(manager, ob_ref, handle, object_state);
205 emitter_handle = handle;
206 }
207 else if (ob->type == OB_POINTCLOUD) {
208 this->pointcloud_sync(manager, ob_ref, object_state);
209 }
210 else if (ob->type == OB_CURVES) {
211 this->curves_sync(manager, ob_ref, object_state);
212 }
213 else if (ob->type == OB_VOLUME) {
214 if (scene_state_.shading.type != OB_WIRE) {
215 volume_ps_.object_sync_volume(manager,
216 resources_,
217 scene_state_,
218 ob_ref,
219 get_material(ob_ref, object_state.color_type).base_color);
220 }
221 }
222 }
223
224 if (ob->type == OB_MESH && ob->modifiers.first() != nullptr) {
225 for (ModifierData &md : ob->modifiers) {
227 continue;
228 }
229 ParticleSystem *psys = (reinterpret_cast<ParticleSystemModifierData *>(&md))->psys;
231 continue;
232 }
233 ParticleSettings *part = psys->part;
234 const int draw_as = (part->draw_as == PART_DRAW_REND) ? part->ren_as : part->draw_as;
235
236 if (draw_as == PART_DRAW_PATH) {
237 this->hair_sync(manager, ob_ref, emitter_handle, object_state, psys, &md);
238 }
239 }
240 }
241 }
242
243 template<typename F>
244 void draw_to_mesh_pass(ObjectRef &ob_ref, bool is_transparent, F draw_callback)
245 {
246 const bool in_front = (ob_ref.object->dtx & OB_DRAW_IN_FRONT) != 0;
247
248 if (scene_state_.xray_mode || is_transparent) {
249 if (in_front) {
250 draw_callback(transparent_ps_.accumulation_in_front_ps_);
251 draw_callback(transparent_depth_ps_.in_front_ps_);
252 }
253 else {
254 draw_callback(transparent_ps_.accumulation_ps_);
255 draw_callback(transparent_depth_ps_.main_ps_);
256 }
257 }
258 else {
259 if (in_front) {
260 draw_callback(opaque_ps_.gbuffer_in_front_ps_);
261 }
262 else {
263 draw_callback(opaque_ps_.gbuffer_ps_);
264 }
265 }
266 }
267
268 void draw_mesh(ObjectRef &ob_ref,
269 Material &material,
270 gpu::Batch *batch,
271 ResourceHandleRange handle,
272 const MaterialTexture *texture = nullptr,
273 bool show_missing_texture = false)
274 {
275 resources_.material_buf.append(material);
276 int material_index = resources_.material_buf.size() - 1;
277
278 if (show_missing_texture && (!texture || !texture->gpu.texture)) {
279 texture = &resources_.missing_texture;
280 }
281
282 this->draw_to_mesh_pass(ob_ref, material.is_transparent(), [&](MeshPass &mesh_pass) {
283 mesh_pass.get_subpass(eGeometryType::MESH, texture).draw(batch, handle, material_index);
284 });
285 }
286
288 ObjectRef &ob_ref,
289 ResourceHandleRange handle,
290 const ObjectState &object_state)
291 {
292 bool has_transparent_material = false;
293
294 if (object_state.use_per_material_batches) {
295 const int material_count = BKE_object_material_used_with_fallback_eval(*ob_ref.object);
296
297 Span<gpu::Batch *> batches;
298 if (object_state.color_type == V3D_SHADING_TEXTURE_COLOR) {
300 }
301 else {
303 ob_ref.object, this->get_dummy_gpu_materials(material_count));
304 }
305
306 if (!batches.is_empty()) {
307 for (auto i : IndexRange(material_count)) {
308 if (batches[i] == nullptr) {
309 continue;
310 }
311
312 int material_slot = i;
313 Material mat = this->get_material(ob_ref, object_state.color_type, material_slot);
314 has_transparent_material = has_transparent_material || mat.is_transparent();
315
317 if (object_state.color_type == V3D_SHADING_TEXTURE_COLOR) {
318 texture = MaterialTexture(manager, ob_ref.object, material_slot);
319 }
320
321 this->draw_mesh(
322 ob_ref, mat, batches[i], handle, &texture, object_state.show_missing_texture);
323 }
324 }
325 }
326 else {
327 gpu::Batch *batch;
328 if (object_state.color_type == V3D_SHADING_TEXTURE_COLOR) {
330 }
331 else if (object_state.color_type == V3D_SHADING_VERTEX_COLOR) {
332 if (ob_ref.object->mode & OB_MODE_VERTEX_PAINT) {
334 }
335 else {
337 }
338 }
339 else {
340 batch = DRW_cache_object_surface_get(ob_ref.object);
341 }
342
343 if (batch) {
344 Material mat = this->get_material(ob_ref, object_state.color_type);
345 has_transparent_material = has_transparent_material || mat.is_transparent();
346
347 this->draw_mesh(ob_ref, mat, batch, handle, &object_state.image_paint_override);
348 }
349 }
350
351 if (object_state.draw_shadow) {
352 shadow_ps_.object_sync(scene_state_, ob_ref, handle, has_transparent_material);
353 }
354 }
355
357 ObjectRef &ob_ref,
358 ResourceHandleRange handle,
359 const ObjectState &object_state)
360 {
362 if (object_state.color_type == V3D_SHADING_VERTEX_COLOR) {
363 features = SCULPT_BATCH_VERTEX_COLOR;
364 }
365 else if (object_state.color_type == V3D_SHADING_TEXTURE_COLOR) {
366 features = SCULPT_BATCH_UV;
367 }
368
369 if (object_state.use_per_material_batches) {
370 for (SculptBatch &batch : sculpt_batches_get(ob_ref.object, features)) {
371 Material mat = this->get_material(ob_ref, object_state.color_type, batch.material_slot);
372 if (scene_state_.show_paint_bvh_debug) {
373 mat.base_color = batch.debug_color();
374 }
375
377 if (object_state.color_type == V3D_SHADING_TEXTURE_COLOR) {
378 texture = MaterialTexture(manager, ob_ref.object, batch.material_slot);
379 }
380
381 this->draw_mesh(
382 ob_ref, mat, batch.batch, handle, &texture, object_state.show_missing_texture);
383 }
384 }
385 else {
386 Material mat = this->get_material(ob_ref, object_state.color_type);
387 for (SculptBatch &batch : sculpt_batches_get(ob_ref.object, features)) {
388 if (scene_state_.show_paint_bvh_debug) {
389 mat.base_color = batch.debug_color();
390 }
391
392 this->draw_mesh(ob_ref, mat, batch.batch, handle, &object_state.image_paint_override);
393 }
394 }
395 }
396
397 void pointcloud_sync(Manager &manager, ObjectRef &ob_ref, const ObjectState &object_state)
398 {
400
401 Material mat = this->get_material(ob_ref, object_state.color_type);
402 resources_.material_buf.append(mat);
403 int material_index = resources_.material_buf.size() - 1;
404
405 const bool is_gsplat = pointcloud_is_gsplat(ob_ref.object);
406 const bool has_transparency = mat.is_transparent();
407
408 this->draw_to_mesh_pass(ob_ref, has_transparency, [&](MeshPass &mesh_pass) {
409 PassMain::Sub &pass =
410 is_gsplat ? mesh_pass.get_subpass(eGeometryType::GSPLAT).sub("GSplatSubPass") :
411 mesh_pass.get_subpass(eGeometryType::POINTCLOUD).sub("PointCloudSubPass");
412 gpu::Batch *batch = pointcloud_sub_pass_setup(pass, ob_ref, handle);
413 pass.draw(batch, handle, material_index);
414 });
415 }
416
418 ObjectRef &ob_ref,
419 ResourceHandle emitter_handle,
420 const ObjectState &object_state,
421 ParticleSystem *psys,
422 ModifierData *md)
423 {
425 ob_ref.object->object_to_world());
426
427 Material mat = this->get_material(ob_ref, object_state.color_type, psys->part->omat - 1);
429 if (object_state.color_type == V3D_SHADING_TEXTURE_COLOR) {
430 texture = MaterialTexture(manager, ob_ref.object, psys->part->omat - 1);
431 }
432 resources_.material_buf.append(mat);
433 int material_index = resources_.material_buf.size() - 1;
434
435 this->draw_to_mesh_pass(ob_ref, mat.is_transparent(), [&](MeshPass &mesh_pass) {
436 PassMain::Sub &pass =
437 mesh_pass.get_subpass(eGeometryType::CURVES, &texture).sub("Hair SubPass");
438 pass.push_constant("emitter_object_id", int(emitter_handle.raw()));
439 gpu::Batch *batch = hair_sub_pass_setup(pass, scene_state_.scene, ob_ref, psys, md);
440 pass.draw(batch, {ResourceID(handle)}, material_index);
441 });
442 }
443
444 void curves_sync(Manager &manager, ObjectRef &ob_ref, const ObjectState &object_state)
445 {
446 ResourceHandleRange handle = manager.unique_handle(ob_ref);
447
448 Material mat = this->get_material(ob_ref, object_state.color_type);
449 resources_.material_buf.append(mat);
450 int material_index = resources_.material_buf.size() - 1;
451
452 this->draw_to_mesh_pass(ob_ref, mat.is_transparent(), [&](MeshPass &mesh_pass) {
453 PassMain::Sub &pass = mesh_pass.get_subpass(eGeometryType::CURVES).sub("Curves SubPass");
454
455 const char *error = nullptr;
456 gpu::Batch *batch = curves_sub_pass_setup(pass, scene_state_.scene, ob_ref.object, error);
457 if (error) {
458 hair_buffer_overflow_error_ = error;
459 }
460 pass.draw(batch, handle, material_index);
461 });
462 }
463
464 void draw(Manager &manager,
465 gpu::Texture *depth_tx,
466 gpu::Texture *depth_in_front_tx,
467 gpu::Texture *color_tx)
468 {
469 int2 resolution = scene_state_.resolution;
470
471 /* Always setup in-front depth, since Overlays can be updated without causing a Workbench
472 * re-sync (See #113580). */
473 bool needs_depth_in_front = !transparent_ps_.accumulation_in_front_ps_.is_empty() ||
474 (!opaque_ps_.gbuffer_in_front_ps_.is_empty() &&
475 scene_state_.sample == 0);
476 resources_.depth_in_front_tx.wrap(needs_depth_in_front ? depth_in_front_tx : nullptr);
477 if (!needs_depth_in_front || opaque_ps_.gbuffer_in_front_ps_.is_empty()) {
478 resources_.clear_in_front_fb.ensure(GPU_ATTACHMENT_TEXTURE(depth_in_front_tx));
479 resources_.clear_in_front_fb.bind();
480 GPU_framebuffer_clear_depth_stencil(resources_.clear_in_front_fb, 1.0f, 0x00);
481 }
482
483 resources_.depth_tx.wrap(depth_tx);
484 resources_.color_tx.wrap(color_tx);
485
486 if (scene_state_.render_finished) {
487 /* Just copy back the already rendered result */
488 anti_aliasing_ps_.draw(
489 draw_ctx, manager, View::default_get(), scene_state_, resources_, depth_in_front_tx);
490 return;
491 }
492
493 anti_aliasing_ps_.setup_view(view_, scene_state_);
494
495 /* Hand off gsplat compute workload before draws. */
496 DRW_gsplat_ensure_radiance(manager, view_);
497
498 GPUAttachment id_attachment = GPU_ATTACHMENT_NONE;
499 if (scene_state_.draw_object_id) {
500 resources_.object_id_tx.acquire_2d(resolution,
501 gpu::TextureFormat::UINT_16,
504 id_attachment = GPU_ATTACHMENT_TEXTURE(resources_.object_id_tx);
505 }
506 resources_.clear_fb.ensure(GPU_ATTACHMENT_TEXTURE(resources_.depth_tx),
507 GPU_ATTACHMENT_TEXTURE(resources_.color_tx),
508 id_attachment);
509 resources_.clear_fb.bind();
510 std::array<double4, 2> clear_colors = {double4(scene_state_.background_color), double4(0.0f)};
511 GPU_framebuffer_multi_clear(resources_.clear_fb, clear_colors);
512 GPU_framebuffer_clear_depth_stencil(resources_.clear_fb, 1.0f, 0x00);
513
514 opaque_ps_.draw(
515 manager, view_, resources_, resolution, scene_state_.draw_shadows ? &shadow_ps_ : nullptr);
516 transparent_ps_.draw(manager, view_, resources_, resolution);
517 transparent_depth_ps_.draw(manager, view_, resources_);
518
519 volume_ps_.draw(manager, view_, resources_);
520 outline_ps_.draw(manager, resources_);
521 dof_ps_.draw(manager, view_, resources_, resolution);
522 anti_aliasing_ps_.draw(draw_ctx, manager, view_, scene_state_, resources_, depth_in_front_tx);
523
524 resources_.object_id_tx.release();
525 }
526
527 void draw_viewport(Manager &manager,
528 gpu::Texture *depth_tx,
529 gpu::Texture *depth_in_front_tx,
530 gpu::Texture *color_tx)
531 {
532 this->draw(manager, depth_tx, depth_in_front_tx, color_tx);
533
534 if (scene_state_.sample + 1 < scene_state_.samples_len) {
536 }
537
538 if (hair_buffer_overflow_error_) {
539 STRNCPY(info, hair_buffer_overflow_error_);
540 }
541 else {
542 STRNCPY(info, "");
543 }
544 }
545
546 void draw(Manager &manager) final
547 {
548 DefaultTextureList *dtxl = draw_ctx->viewport_texture_list_get();
549
551 if (draw_ctx->is_viewport_image_render()) {
552 draw_image_render(manager, dtxl->depth, dtxl->depth_in_front, dtxl->color);
553 }
554 else {
555 draw_viewport(manager, dtxl->depth, dtxl->depth_in_front, dtxl->color);
556 }
558 }
559
561 gpu::Texture *depth_tx,
562 gpu::Texture *depth_in_front_tx,
563 gpu::Texture *color_tx,
564 RenderEngine *engine = nullptr)
565 {
566 if (scene_state_.render_finished) {
567 /* This can happen in viewport animation renders, if the scene didn't have any updates
568 * between frames. */
569 this->draw(manager, depth_tx, depth_in_front_tx, color_tx);
570 return;
571 }
572
573 BLI_assert(scene_state_.sample == 0);
574 for (auto i : IndexRange(scene_state_.samples_len)) {
575 if (hair_buffer_overflow_error_) {
576 RE_engine_set_error_message(engine, hair_buffer_overflow_error_);
577 }
578
579 if (engine && RE_engine_test_break(engine)) {
580 break;
581 }
582 if (i != 0) {
583 scene_state_.sample = i;
584 /* Re-sync anything dependent on scene_state.sample. */
585 resources_.init(scene_state_, draw_ctx);
586 dof_ps_.init(scene_state_, draw_ctx);
587 anti_aliasing_ps_.sync(scene_state_, resources_);
588 }
589 this->draw(manager, depth_tx, depth_in_front_tx, color_tx);
590 /* Metal: Perform render step between samples to allow flushing of freed GPUBackend
591 * resources. Vulkan: Perform render step between samples to avoid allocation of a high
592 * amount of command buffer memory that can eventually result in out-of-memory errors or a
593 * TDR when submitted as one large command buffer. */
595 GPU_flush();
596 }
598 }
599 }
600};
601
603{
604 return new Instance();
605}
606
607void Engine::free_static()
608{
609 ShaderCache::release();
610}
611
612} // namespace workbench
613
614/* -------------------------------------------------------------------- */
617
618/* RENDER */
619
621{
622 /* For image render, allocate own buffers because we don't have a viewport. */
623 const float2 viewport_size = draw_ctx->viewport_size_get();
624 const int2 size = {int(viewport_size.x), int(viewport_size.y)};
625
627
628 /* When doing a multi view rendering the first view will allocate the buffers
629 * the other views will reuse these buffers */
630 if (dtxl->color == nullptr) {
631 BLI_assert(dtxl->depth == nullptr);
634 "txl.color", size.x, size.y, 1, gpu::TextureFormat::SFLOAT_16_16_16_16, usage, nullptr);
635 dtxl->depth = GPU_texture_create_2d("txl.depth",
636 size.x,
637 size.y,
638 1,
639 gpu::TextureFormat::SFLOAT_32_DEPTH_UINT_8,
640 usage,
641 nullptr);
642 dtxl->depth_in_front = GPU_texture_create_2d("txl.depth_in_front",
643 size.x,
644 size.y,
645 1,
646 gpu::TextureFormat::SFLOAT_32_DEPTH_UINT_8,
647 usage,
648 nullptr);
649 }
650
651 if (!(dtxl->depth && dtxl->color && dtxl->depth_in_front)) {
652 return false;
653 }
654
656
658 &dfbl->default_fb,
659 {GPU_ATTACHMENT_TEXTURE(dtxl->depth), GPU_ATTACHMENT_TEXTURE(dtxl->color)});
660
662 {GPU_ATTACHMENT_TEXTURE(dtxl->depth), GPU_ATTACHMENT_NONE});
663
665 {GPU_ATTACHMENT_NONE, GPU_ATTACHMENT_TEXTURE(dtxl->color)});
666
667 return GPU_framebuffer_check_valid(dfbl->default_fb, nullptr) &&
670}
671
673 const char *viewname,
675 const rcti *rect)
676{
678 if (rp) {
681 rect->xmin,
682 rect->ymin,
683 BLI_rcti_size_x(rect),
684 BLI_rcti_size_y(rect),
685 4,
686 0,
689 }
690}
691
693 const char *viewname,
695 const rcti *rect,
696 const float4x4 &winmat)
697{
699 if (rp) {
702 rect->xmin,
703 rect->ymin,
704 BLI_rcti_size_x(rect),
705 BLI_rcti_size_y(rect),
708
709 int pix_num = BLI_rcti_size_x(rect) * BLI_rcti_size_y(rect);
710
711 /* Convert GPU depth [0..1] to view Z [near..far] */
712 if (draw::View::default_get().is_persp()) {
713 for (float &z : MutableSpan(rp->ibuf->float_data_for_write(), pix_num)) {
714 if (z == 1.0f) {
715 z = 1e10f; /* Background */
716 }
717 else {
718 z = z * 2.0f - 1.0f;
719 z = winmat[3][2] / (z + winmat[2][2]);
720 }
721 }
722 }
723 else {
724 /* Keep in mind, near and far distance are negatives. */
725 float near = draw::View::default_get().near_clip();
726 float far = draw::View::default_get().far_clip();
727 float range = fabsf(far - near);
728
729 for (float &z : MutableSpan(rp->ibuf->float_data_for_write(), pix_num)) {
730 if (z == 1.0f) {
731 z = 1e10f; /* Background */
732 }
733 else {
734 z = z * range - near;
735 }
736 }
737 }
738 }
739}
740
742{
743 using namespace blender::draw;
744 const DRWContext *draw_ctx = DRW_context_get();
745
746 if (!workbench_render_framebuffers_init(draw_ctx)) {
747 RE_engine_report(engine, RPT_ERROR, "Failed to allocate GPU buffers");
748 return;
749 }
750
751 /* Setup */
753 Depsgraph *depsgraph = draw_ctx->depsgraph;
754
755 workbench::Instance instance;
756
757 /* TODO(sergey): Shall render hold pointer to an evaluated camera instead? */
758 Object *camera_ob = DEG_get_evaluated(depsgraph, RE_GetCamera(engine->re));
759
760 /* Set the perspective, view and window matrix. */
761 float4x4 winmat, viewmat, viewinv;
762 RE_GetCameraWindow(engine->re, camera_ob, winmat.ptr());
763 RE_GetCameraModelMatrix(engine->re, camera_ob, viewinv.ptr());
764 viewmat = math::invert(viewinv);
765
766 /* Render */
767 /* TODO: Remove old draw manager calls. */
769 draw::View::default_set(float4x4(viewmat), float4x4(winmat));
770
771 instance.init(depsgraph, camera_ob);
772
773 draw::Manager &manager = *DRW_manager_get();
774 manager.begin_sync();
775
776 instance.begin_sync();
778 engine,
779 depsgraph,
780 [&](draw::ObjectRef &ob_ref, RenderEngine * /*engine*/, Depsgraph * /*depsgraph*/) {
781 instance.object_sync(ob_ref, manager);
782 });
783 instance.end_sync();
784
785 manager.end_sync();
786
788
790 instance.draw_image_render(manager, dtxl.depth, dtxl.depth_in_front, dtxl.color, engine);
791
793
794 /* Write image */
795 const char *viewname = RE_GetActiveRenderView(engine->re);
796 write_render_color_output(layer, viewname, dfbl->default_fb, &rect);
797 write_render_z_output(layer, viewname, dfbl->default_fb, &rect, winmat);
798}
799
801 Scene *scene,
802 ViewLayer *view_layer)
803{
804 if (view_layer->passflag & SCE_PASS_COMBINED) {
805 RE_engine_register_pass(engine, scene, view_layer, RE_PASSNAME_COMBINED, 4, "RGBA", SOCK_RGBA);
806 }
807 if (view_layer->passflag & SCE_PASS_DEPTH) {
808 RE_engine_register_pass(engine, scene, view_layer, RE_PASSNAME_DEPTH, 1, "Z", SOCK_FLOAT);
809 }
810}
811
812static void workbench_render(RenderEngine *engine, Depsgraph *depsgraph)
813{
814 DRW_render_to_image(engine, depsgraph, workbench_render_to_image, [](RenderResult *) {});
815}
816
818 /*next*/ nullptr,
819 /*prev*/ nullptr,
820 /*idname*/ "BLENDER_WORKBENCH",
821 /*name*/ N_("Workbench"),
823 /*update*/ nullptr,
824 /*render*/ &workbench_render,
825 /*render_frame_finish*/ nullptr,
826 /*draw*/ nullptr,
827 /*bake*/ nullptr,
828 /*view_update*/ nullptr,
829 /*view_draw*/ nullptr,
830 /*view_pause*/ nullptr,
831 /*view_resume*/ nullptr,
832 /*update_script_node*/ nullptr,
833 /*update_render_passes*/ &workbench_render_update_passes,
834 /*update_custom_camera*/ nullptr,
835 /*rna_ext*/
836 {
837 /*data*/ nullptr,
838 /*srna*/ nullptr,
839 /*call*/ nullptr,
840 },
841};
842
844
845} // namespace blender
General operations, lookup, etc. for materials.
General operations, lookup, etc. for blender objects.
A BVH for high poly meshes.
#define ATTR_FALLTHROUGH
#define final(a, b, c)
Definition BLI_hash_c.hh:21
#define RE_PASSNAME_COMBINED
#define RE_PASSNAME_DEPTH
#define GPU_ATTACHMENT_TEXTURE(_texture)
#define GPU_ATTACHMENT_NONE
#define GPU_framebuffer_ensure_config(_fb,...)
#define ELEM(...)
SIMD_FORCE_INLINE const btScalar & z() const
Return the z value.
Definition btQuadWord.h:117
constexpr IndexRange()=default
constexpr bool is_empty() const
Definition BLI_span.hh:264
void begin_sync(Object *object_active=nullptr)
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)
static void default_set(const float4x4 &view_mat, const float4x4 &win_mat)
Definition draw_view.cc:322
static View & default_get()
Definition draw_view.cc:317
PassBase< DrawCommandBufType > & sub(const char *name)
Definition draw_pass.hh:701
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
A running instance of the engine.
void object_sync(ObjectRef &ob_ref, Manager &manager) final
void init(const int2 &output_res, const rcti *output_rect, const rcti *visible_rect, RenderEngine *render, Depsgraph *depsgraph, Object *camera_object=nullptr, const RenderLayer *render_layer=nullptr, View *drw_view_=nullptr, const View3D *v3d=nullptr, const RegionView3D *rv3d=nullptr)
void draw(Manager &manager) final
void draw_viewport(Manager &manager, gpu::Texture *depth_tx, gpu::Texture *depth_in_front_tx, gpu::Texture *color_tx)
void draw_mesh(ObjectRef &ob_ref, Material &material, gpu::Batch *batch, ResourceHandleRange handle, const MaterialTexture *texture=nullptr, bool show_missing_texture=false)
Material get_material(ObjectRef ob_ref, eV3DShadingColorType color_type, int slot=0)
void draw(Manager &manager, gpu::Texture *depth_tx, gpu::Texture *depth_in_front_tx, gpu::Texture *color_tx)
void object_sync(ObjectRef &ob_ref, Manager &manager) final
void draw_to_mesh_pass(ObjectRef &ob_ref, bool is_transparent, F draw_callback)
Span< const GPUMaterial * > get_dummy_gpu_materials(int material_count)
void curves_sync(Manager &manager, ObjectRef &ob_ref, const ObjectState &object_state)
void draw_image_render(Manager &manager, gpu::Texture *depth_tx, gpu::Texture *depth_in_front_tx, gpu::Texture *color_tx, RenderEngine *engine=nullptr)
void init(Depsgraph *depsgraph, Object *camera_ob=nullptr)
StringRefNull name_get() final
void pointcloud_sync(Manager &manager, ObjectRef &ob_ref, const ObjectState &object_state)
void mesh_sync(Manager &manager, ObjectRef &ob_ref, ResourceHandleRange handle, const ObjectState &object_state)
void hair_sync(Manager &manager, ObjectRef &ob_ref, ResourceHandle emitter_handle, const ObjectState &object_state, ParticleSystem *psys, ModifierData *md)
void sculpt_sync(Manager &manager, ObjectRef &ob_ref, ResourceHandleRange handle, const ObjectState &object_state)
#define layer
TEX_TEMPLATE DataVec texture(T, FltCoord, float=0.0f) RET
#define F
void DRW_gsplat_ensure_radiance(draw::Manager &manager, draw::View &view)
Span< gpu::Batch * > DRW_cache_mesh_surface_texpaint_get(Object *ob)
gpu::Batch * DRW_cache_mesh_surface_sculptcolors_get(Object *ob)
gpu::Batch * DRW_cache_mesh_surface_vertpaint_get(Object *ob)
Vector< SculptBatch > sculpt_batches_get(const Object *ob, SculptBatchFeature features)
bool pointcloud_is_gsplat(Object *ob)
gpu::Batch * DRW_cache_object_surface_get(Object *ob)
@ SCULPT_BATCH_VERTEX_COLOR
gpu::Batch * pointcloud_sub_pass_setup(PassMain::Sub &sub_ps, const ObjectRef &ob_ref, const ResourceHandleRange &res_handle, GPUMaterial *gpu_material=nullptr)
Span< gpu::Batch * > DRW_cache_object_surface_material_get(Object *ob, const Span< const GPUMaterial * > materials)
gpu::Batch * DRW_cache_mesh_surface_texpaint_single_get(Object *ob)
CartesianBasis invert(const CartesianBasis &basis)
BMesh Iterators.
void GPU_framebuffer_bind(gpu::FrameBuffer *fb)
BLI_assert(slot->slot_subtype.map==BMO_OP_SLOT_SUBTYPE_MAP_FLT)
void DRW_render_to_image(RenderEngine *engine, Depsgraph *depsgraph, std::function< void(RenderEngine *, RenderLayer *, const rcti)> render_view_cb, std::function< void(RenderResult *)> store_metadata_cb)
unsigned __int64 uint64_t
Definition BKE_text.h:91
void GPU_framebuffer_read_color(gpu::FrameBuffer *fb, int x, int y, int width, int height, int channels, int slot, eGPUDataFormat data_format, void *r_data)
static void workbench_render_to_image(RenderEngine *engine, RenderLayer *layer, const rcti rect)
char * STRNCPY(char(&dst)[N], const char *src)
int BKE_object_material_used_with_fallback_eval(const Object &ob)
RenderEngineType DRW_engine_viewport_workbench_type
bool assign_if_different(T &old_value, T &&new_value)
bool GPU_framebuffer_check_valid(gpu::FrameBuffer *fb, char err_out[256])
void RE_engine_report(RenderEngine *engine, int type, const char *msg)
BLI_INLINE float fb(float length, float L)
uint64_t DEG_get_update_count(const Depsgraph *depsgraph)
Definition depsgraph.cc:356
void GPU_framebuffer_clear_depth_stencil(gpu::FrameBuffer *fb, float clear_depth, uint clear_stencil)
bool DRW_object_is_renderable(const Object *ob)
GPUBackendType GPU_backend_get_type()
void DRW_cache_restart()
@ eModifierType_ParticleSystem
@ RE_USE_GPU_CONTEXT
Definition RE_engine.h:52
@ RE_INTERNAL
Definition RE_engine.h:45
@ RE_USE_STEREO_VIEWPORT
Definition RE_engine.h:51
void GPU_render_step(bool force_resource_release=false)
void RE_GetCameraWindow(Render *re, const Object *camera, float r_winmat[4][4])
Definition initrender.cc:62
Material * BKE_material_default_empty()
MatBase< float, 4, 4 > float4x4
int DRW_object_visibility_in_active_context(const Object *ob)
void DRW_render_object_iter(RenderEngine *engine, Depsgraph *depsgraph, std::function< void(draw::ObjectRef &, RenderEngine *, Depsgraph *)> callback)
static void workbench_render(RenderEngine *engine, Depsgraph *depsgraph)
const char * RE_GetActiveRenderView(Render *re)
BLI_INLINE int BLI_rcti_size_y(const struct rcti *rct)
Definition BLI_rect.hh:207
void RE_GetCameraModelMatrix(const Render *re, const Object *camera, float r_modelmat[4][4])
Definition initrender.cc:74
VecBase< int32_t, 2 > int2
VecBase< float, 2 > float2
void RE_engine_register_pass(RenderEngine *engine, Scene *scene, ViewLayer *view_layer, const char *name, int channels, const char *chanid, eNodeSocketDatatype type)
@ GPU_TEXTURE_USAGE_SHADER_READ
@ GPU_TEXTURE_USAGE_GENERAL
@ GPU_TEXTURE_USAGE_ATTACHMENT
static void write_render_color_output(RenderLayer *layer, const char *viewname, gpu::FrameBuffer *fb, const rcti *rect)
T * DEG_get_evaluated(const Depsgraph *depsgraph, T *id)
bool RE_engine_test_break(RenderEngine *engine)
bool DRW_object_is_visible_psys_in_active_context(const Object *object, const ParticleSystem *psys)
void DRW_submission_end()
const DRWContext * DRW_context_get()
@ V3D_SHADING_SINGLE_COLOR
@ V3D_SHADING_RANDOM_COLOR
@ V3D_SHADING_MATERIAL_COLOR
@ V3D_SHADING_VERTEX_COLOR
@ V3D_SHADING_TEXTURE_COLOR
@ V3D_SHADING_OBJECT_COLOR
BLI_INLINE int BLI_rcti_size_x(const struct rcti *rct)
Definition BLI_rect.hh:203
static bool workbench_render_framebuffers_init(const DRWContext *draw_ctx)
void GPU_framebuffer_multi_clear(gpu::FrameBuffer *fb, Span< double4 > clear_colors)
RenderPass * RE_pass_find_by_name(RenderLayer *rl, const char *name, const char *viewname)
void RE_engine_set_error_message(RenderEngine *engine, const char *msg)
ModifierData * BKE_modifiers_findby_type(const Object *ob, ModifierType type)
void GPU_flush()
Definition gpu_state.cc:284
Material * BKE_object_material_get_eval(Object *ob, short act)
void DRW_submission_start()
draw::Manager * DRW_manager_get()
void GPU_framebuffer_read_depth(gpu::FrameBuffer *fb, int x, int y, int width, int height, eGPUDataFormat data_format, void *r_data)
static void workbench_render_update_passes(RenderEngine *engine, Scene *scene, ViewLayer *view_layer)
bool BKE_modifier_is_enabled(const Scene *scene, ModifierData *md, int required_mode)
VecBase< double, 4 > double4
@ OB_VISIBLE_SELF
void DRW_viewport_request_redraw()
gpu::Texture * GPU_texture_create_2d(const char *name, int width, int height, int mip_len, gpu::TextureFormat format, eGPUTextureUsage usage, const float *data)
static void write_render_z_output(RenderLayer *layer, const char *viewname, gpu::FrameBuffer *fb, const rcti *rect, const float4x4 &winmat)
@ FLUID_DOMAIN_TYPE_GAS
Object * RE_GetCamera(Render *re)
Definition initrender.cc:31
#define fabsf
DefaultTextureList * viewport_texture_list_get() const
Depsgraph * depsgraph
float2 viewport_size_get() const
DefaultFramebufferList * viewport_framebuffer_list_get() const
bool is_scene_render() const
char info[GPU_INFO_SIZE]
Definition DRW_render.hh:74
struct FluidDomainSettings * domain
float * float_data_for_write()
Definition allocimbuf.cc:79
const c_style_mat & ptr() const
ListBaseT< ModifierData > modifiers
eObject_DrawExtraFlag dtx
struct Render * re
Definition RE_engine.h:148
struct ImBuf * ibuf
Definition RE_pipeline.h:72
DrawEngine * create_instance() final
#define N_(msgid)