Blender V5.3
render/intern/compositor.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 <cstring>
10#include <string>
11
12#include "BLI_listbase.hh"
14#include "BLI_memory_utils.hh"
15#include "BLI_threads.hh"
16#include "BLI_vector.hh"
17
18#include "MEM_guardedalloc.h"
19
20#include "DNA_node_types.h"
21
22#include "BKE_compositor.hh"
24#include "BKE_cryptomatte.hh"
25#include "BKE_global.hh"
26#include "BKE_image.hh"
27#include "BKE_node.hh"
28#include "BKE_node_runtime.hh"
29#include "BKE_scene.hh"
30#include "BKE_scene_runtime.hh"
31
32#include "DRW_engine.hh"
33#include "DRW_render.hh"
34
36#include "IMB_imbuf.hh"
37#include "IMB_partial_update.hh"
38
40
41#include "COM_context.hh"
43#include "COM_domain.hh"
46#include "COM_render_context.hh"
47#include "COM_result.hh"
49
50#include "NOD_dependencies.hh"
51#include "NOD_eval_log.hh"
52
53#include "RE_compositor.hh"
54#include "RE_pipeline.h"
55
56#include "WM_api.hh"
57
58#include "GPU_context.hh"
59#include "GPU_state.hh"
60#include "GPU_texture_pool.hh"
61
62#include "render_types.h"
63
64namespace blender {
65
66namespace render {
67
69 private:
70 /* Input data. */
71 CompositorInputData input_data_;
72 /* The compute context of the active viewer if one exists. */
73 const ComputeContext *viewer_compute_context_;
74
75 /* Cached GPU and CPU passes that the compositor took ownership of. Those had their reference
76 * count incremented when accessed and need to be freed/have their reference count decremented
77 * when destroying the context. */
78 Vector<gpu::Texture *> cached_gpu_passes_;
79 Vector<ImBuf *> cached_cpu_passes_;
80
81 /* True if GPU compute is supported and can be used, if false, we fallback to CPU. */
82 bool gpu_supported_ = true;
83
84 public:
87 const CompositorInputData &input_data)
89 input_data_(input_data),
90 viewer_compute_context_(bke::compositor::compute_viewer_compute_context(
91 input_data_.scene, compute_context_cache))
92 {
93 }
94
95 virtual ~Context()
96 {
97 for (gpu::Texture *pass : cached_gpu_passes_) {
98 GPU_texture_free(pass);
99 }
100 for (ImBuf *pass : cached_cpu_passes_) {
101 IMB_freeImBuf(pass);
102 }
103 }
104
105 const Main &get_main() const override
106 {
107 return input_data_.main;
108 }
109
110 const Scene &get_scene() const override
111 {
112 return input_data_.scene;
113 }
114
115 void set_gpu_supported(const bool supported)
116 {
117 gpu_supported_ = supported;
118 }
119
120 bool use_gpu() const override
121 {
122 return gpu_supported_ &&
124 }
125
127 {
128 return input_data_.needed_side_effects_outputs;
129 }
130
132 {
133 return viewer_compute_context_;
134 }
135
136 const RenderData &get_render_data() const override
137 {
138 return input_data_.render_data;
139 }
140
142 {
143 Render *render = RE_GetSceneRender(&input_data_.scene);
144 RenderResult *render_result = RE_AcquireResultRead(render);
145
146 /* If a render result already exist, use its size, since the compositor operates on the render
147 * settings at which the render happened. Otherwise, use the size from the render data. */
148 int2 size;
149 if (render_result) {
150 size = int2(render_result->rectx, render_result->recty);
151 }
152 else {
153 BKE_render_resolution(&input_data_.render_data, true, &size.x, &size.y);
154 }
155
157
158 return size;
159 }
160
162 {
163 return compositor::Domain(this->get_render_size());
164 }
165
167 {
168 Render *render = RE_GetSceneRender(&input_data_.scene);
169 RenderResult *render_result = RE_AcquireResultWrite(render);
170
171 if (render_result) {
172 RenderView *render_view = RE_RenderViewGetByName(render_result,
173 input_data_.view_name.c_str());
174 ImBuf *image_buffer = RE_RenderViewEnsureImBuf(render_result, render_view);
175 render_result->have_combined = true;
176
177 if (result.is_single_value()) {
179 4 * size_t(render_result->rectx) * size_t(render_result->recty), __func__);
180 image_buffer->assign_float_data(data);
181 IMB_rectfill(image_buffer, result.get_single_value<compositor::Color>());
182 }
183 else if (this->use_gpu()) {
185 float *output_buffer = static_cast<float *>(GPU_texture_read(result, GPU_DATA_FLOAT, 0));
186 image_buffer->assign_float_data(output_buffer);
187 }
188 else {
189 if (result.sharing_info()) {
190 image_buffer->float_buffer = ImBufFloatBuffer{
191 .data = static_cast<const float *>(result.cpu_data().data()),
192 .sharing_info = result.sharing_info(),
193 .colorspace = nullptr};
194 }
195 else {
197 4 * size_t(render_result->rectx) * size_t(render_result->recty), __func__);
198 image_buffer->assign_float_data(data);
199 std::memcpy(image_buffer->float_data_for_write(),
200 result.cpu_data().data(),
201 render_result->rectx * render_result->recty * 4 * sizeof(float));
202 }
203 }
204
205 /* Compositor output is scene linear, previous pixels were replaced. */
206 image_buffer->float_buffer.colorspace = nullptr;
207
208 /* Free outdated GPU texture. */
209 IMB_free_gpu_textures(image_buffer);
210 IMB_partial_update_mark_full(image_buffer);
211 }
213 }
214
216 {
217 using namespace compositor;
218
219 /* Realize the output on the compositing domain if needed. */
220 const Domain compositing_domain = this->get_compositing_domain();
221 const InputDescriptor input_descriptor = {ResultType::Color,
222 InputRealizationMode::OperationDomain};
223 SimpleOperation *realization_operation = RealizeOnDomainOperation::construct_if_needed(
224 *this, result, input_descriptor, compositing_domain);
225 if (!realization_operation) {
227 return;
228 }
229
230 Result realize_input = this->create_result(result.type(), result.precision());
231 realize_input.share_data(result);
232 realization_operation->map_input_to_result(&realize_input);
233 realization_operation->evaluate();
234 Result &realized_result = realization_operation->get_result();
235 this->write_output_image(realized_result);
236 realized_result.release();
237 delete realization_operation;
238 }
239
241 {
242 /* Caching disabled. */
243 if (!flag_is_set(this->get_render_data().compositor_cache_flags, SCE_COMPOSITOR_CACHE_FRAMES))
244 {
245 return false;
246 }
247
248 /* Not an interactive compositor, so no need to cache. */
249 if (this->render_context()) {
250 return false;
251 }
252
253 /* Only cache if any of the effects are time dependent. */
254 const Scene &original_scene = *DEG_get_original(&input_data_.scene);
256 original_scene,
258 {
259 return true;
260 }
261
262 return false;
263 }
264
265 void write_viewer_image(const compositor::Result &viewer_result)
266 {
267 Image *image = BKE_image_ensure_viewer(G.main, IMA_TYPE_COMPOSITE, "Viewer Node");
268
269 ImageUser image_user = {nullptr};
270 const int view_identifier = BKE_scene_multiview_view_id_get(&input_data_.render_data,
271 input_data_.view_name.c_str());
272 image_user.multi_index = view_identifier;
273
274 if (BKE_scene_multiview_is_render_view_first(&input_data_.render_data,
275 input_data_.view_name.c_str()))
276 {
277 BKE_image_ensure_viewer_views(&input_data_.render_data, image, &image_user);
278 }
279
281
282 void *lock;
283 ImBuf *image_buffer = BKE_image_acquire_ibuf_gpu(image, &image_user, &lock);
284
285 const int2 size = viewer_result.is_single_value() ? this->get_render_size() :
286 viewer_result.domain().data_size;
287
288 /* The image buffer has a different size than the viewer result, set the new size and free all
289 * data to be reallocated later. */
290 if (int2(image_buffer->x, image_buffer->y) != size) {
291 IMB_free_byte_pixels(image_buffer);
292 IMB_free_float_pixels(image_buffer);
293 IMB_free_gpu_textures(image_buffer);
294 image_buffer->x = size.x;
295 image_buffer->y = size.y;
296 }
297
298 if (this->use_gpu()) {
299 /* If using GPU, free any potential previous CPU data. */
300 IMB_free_float_pixels(image_buffer);
301
302 /* Allocate a GPU texture if using GPU and no texture exists or one exists but with a
303 * different format. */
304 if (!image_buffer->gpu.texture ||
305 GPU_texture_format(image_buffer->gpu.texture) != viewer_result.get_gpu_texture_format())
306 {
307 gpu::TextureFormat format = viewer_result.get_gpu_texture_format();
309 __func__, size.x, size.y, 1, format, GPU_TEXTURE_USAGE_GENERAL, nullptr);
310 IMB_assign_gpu_texture(image_buffer, texture);
311 }
312 }
313 else {
314 /* If not using GPU, free any potential previous GPU data. */
315 IMB_free_gpu_textures(image_buffer);
316 }
317
318 if (this->use_gpu()) {
319 if (viewer_result.is_single_value()) {
320 GPU_texture_clear(image_buffer->gpu.texture,
322 viewer_result.get_single_value<compositor::Color>());
323 }
324 else {
325 GPU_texture_copy(image_buffer->gpu.texture, viewer_result);
326 }
327 image_buffer->userflags |= IB_HOST_BUFFER_INVALID;
328 }
329 else {
330 if (viewer_result.is_single_value()) {
331 IMB_alloc_float_pixels(image_buffer, 4, false);
332 IMB_rectfill(image_buffer, viewer_result.get_single_value<compositor::Color>());
333 }
334 else if (viewer_result.sharing_info()) {
335 image_buffer->channels = 4;
336 image_buffer->float_buffer = ImBufFloatBuffer{
337 .data = static_cast<const float *>(viewer_result.cpu_data().data()),
338 .sharing_info = viewer_result.sharing_info(),
339 .colorspace = nullptr};
340 }
341 else if (viewer_result.cpu_data().data() != image_buffer->float_data()) {
342 IMB_alloc_float_pixels(image_buffer, 4, false);
343 std::memcpy(image_buffer->float_data_for_write(),
344 viewer_result.cpu_data().data(),
345 size.x * size.y * 4 * sizeof(float));
346 }
347 }
348
349 if (!viewer_result.is_single_value()) {
350 image_buffer->flags |= ImBufFlags::HasDisplayWindow;
351 const int2 display_offset = int2(viewer_result.domain().transformation.location());
352 copy_v2_v2_int(image_buffer->display_size, viewer_result.domain().display_size);
353 copy_v2_v2_int(image_buffer->display_offset, display_offset);
354 copy_v2_v2_int(image_buffer->data_offset, viewer_result.domain().data_offset);
355 }
356 else {
357 image_buffer->flags &= ~ImBufFlags::HasDisplayWindow;
358 }
359
360 if (viewer_result.meta_data.is_non_color_data) {
361 image->flag &= ~IMA_VIEW_AS_RENDER;
364 }
365 else {
366 image->flag |= IMA_VIEW_AS_RENDER;
369 }
370
371 if (this->should_cache_viewer_result()) {
372 /* Duplicate the viewer image buffer to store in the cache, making sure to manually
373 * duplicate the GPU texture since it is not done in IMB_dupImBuf. */
374 ImBuf *cached_buffer = IMB_dupImBuf(image_buffer);
375 if (this->use_gpu()) {
377 __func__,
378 GPU_texture_width(image_buffer->gpu.texture),
379 GPU_texture_height(image_buffer->gpu.texture),
380 1,
381 GPU_texture_format(image_buffer->gpu.texture),
383 nullptr);
384 GPU_texture_copy(texture, image_buffer->gpu.texture);
385 IMB_assign_gpu_texture(cached_buffer, texture);
386 }
387
388 const Scene *original_scene = DEG_get_original(&this->get_scene());
389 original_scene->runtime->compositor.cache.add_frame(
390 this->get_frame_number(), view_identifier, cached_buffer);
391 }
392
393 IMB_partial_update_mark_full(image_buffer);
394 BKE_image_release_ibuf(image, image_buffer, lock);
396 }
397
398 void write_viewer(compositor::Result &viewer_result) override
399 {
400 using namespace compositor;
401
402 /* Realize the transforms if needed. */
403 const InputDescriptor input_descriptor = {ResultType::Color,
404 InputRealizationMode::OperationDomain};
405 SimpleOperation *realization_operation = RealizeOnDomainOperation::construct_if_needed(
406 *this, viewer_result, input_descriptor, viewer_result.domain());
407
408 if (!realization_operation) {
409 this->write_viewer_image(viewer_result);
410 return;
411 }
412
413 Result realize_input = this->create_result(ResultType::Color, viewer_result.precision());
414 realize_input.share_data(viewer_result);
415 realization_operation->map_input_to_result(&realize_input);
416 realization_operation->evaluate();
417
418 Result &realized_viewer_result = realization_operation->get_result();
419 this->write_viewer_image(realized_viewer_result);
420 realized_viewer_result.release();
421 delete realization_operation;
422 }
423
425 {
426 switch (pass->channels) {
427 case 1:
429 case 2:
431 case 3:
433 case 4:
434 if (StringRef(pass->chan_id) == "XYZW") {
436 }
437 else {
439 }
440 default:
441 break;
442 }
443
446 }
447
449 {
450 switch (pass->channels) {
451 case 1:
453 case 2:
455 case 3:
456 if (StringRef(pass->chan_id) == "RGB") {
458 }
459 else {
461 }
462 case 4:
463 if (StringRef(pass->chan_id) == "XYZW") {
465 }
466 else {
468 }
469 default:
470 break;
471 }
472
475 }
476
478 {
480 invalid_pass.allocate_invalid();
481 return invalid_pass;
482 }
483
484 compositor::Result get_pass(const Scene *scene, int view_layer_id, const char *name) override
485 {
486 /* Blender aliases the Image pass name to be the Combined pass, so we return the combined pass
487 * in that case. */
488 const char *pass_name = StringRef(name) == "Image" ? "Combined" : name;
489
490 if (!scene) {
491 return this->get_invalid_pass();
492 }
493
494 ViewLayer *view_layer = static_cast<ViewLayer *>(
495 BLI_findlink(&scene->view_layers, view_layer_id));
496 if (!view_layer) {
497 return this->get_invalid_pass();
498 }
499
501 if (!render) {
502 return this->get_invalid_pass();
503 }
504
506
507 RenderResult *render_result = RE_AcquireResultRead(render);
508 if (!render_result) {
509 return this->get_invalid_pass();
510 }
511
512 RenderLayer *render_layer = RE_GetRenderLayer(render_result, view_layer->name);
513 if (!render_layer) {
514 return this->get_invalid_pass();
515 }
516
517 RenderPass *render_pass = RE_pass_find_by_name(
518 render_layer, pass_name, this->get_view_name().data());
519 if (!render_pass) {
520 return this->get_invalid_pass();
521 }
522
523 if (!render_pass || !render_pass->ibuf || !render_pass->ibuf->float_data()) {
524 return this->get_invalid_pass();
525 }
526
527 compositor::Result pass_data = this->create_result(this->get_pass_data_type(render_pass));
528 if (this->use_gpu()) {
530 __func__,
531 render_pass->ibuf,
533 render->result_has_gpu_texture_caches = true;
535 pass_data.share_data(pass_texture);
536 cached_gpu_passes_.append(pass_texture);
537 }
538 else {
539 /* Don't assume render will keep pass data stored, add our own reference. */
540 IMB_refImBuf(render_pass->ibuf);
541 pass_data.share_data(render_pass->ibuf->float_buffer.data,
542 int2(render_pass->ibuf->x, render_pass->ibuf->y),
543 render_pass->ibuf->float_buffer.sharing_info);
544 cached_cpu_passes_.append(render_pass->ibuf);
545 }
546
547 compositor::Result pass = this->create_result(this->get_pass_type(render_pass));
548 if (pass.type() != pass_data.type()) {
549 compositor::ConversionOperation conversion_operation(*this, pass_data.type(), pass.type());
550 conversion_operation.map_input_to_result(&pass_data);
551 conversion_operation.evaluate();
552 pass.share_data(conversion_operation.get_result());
553 conversion_operation.get_result().release();
554 }
555 else {
556 pass.share_data(pass_data);
557 pass_data.release();
558 }
559
560 /* We assume the given pass is a Cryptomatte pass and retrieve its layer name. If it wasn't a
561 * Cryptomatte pass, the checks below will fail anyway. */
562 const std::string combined_pass_name = std::string(view_layer->name) + "." + pass_name;
564 combined_pass_name);
565
566 struct StampCallbackData {
567 std::string cryptomatte_layer_name;
568 compositor::MetaData *meta_data;
569 };
570
571 /* Go over the stamp data and add any Cryptomatte related meta data. */
572 StampCallbackData callback_data = {cryptomatte_layer_name, &pass.meta_data};
574 &callback_data,
575 render_result->stamp_data,
576 [](void *user_data, const char *key, char *value, int /*value_maxncpy*/) {
577 StampCallbackData *data = static_cast<StampCallbackData *>(user_data);
578
579 const std::string manifest_key = bke::cryptomatte::BKE_cryptomatte_meta_data_key(
580 data->cryptomatte_layer_name, "manifest");
581 if (key == manifest_key) {
582 data->meta_data->cryptomatte.manifest = value;
583 }
584
586 data->cryptomatte_layer_name, "hash");
587 if (key == hash_key) {
588 data->meta_data->cryptomatte.hash = value;
589 }
590
591 const std::string conversion_key = bke::cryptomatte::BKE_cryptomatte_meta_data_key(
592 data->cryptomatte_layer_name, "conversion");
593 if (key == conversion_key) {
594 data->meta_data->cryptomatte.conversion = value;
595 }
596 },
597 false);
598
599 return pass;
600 }
601
602 StringRef get_view_name() const override
603 {
604 return input_data_.view_name;
605 }
606
608 {
609 switch (input_data_.scene.r.compositor_precision) {
611 /* Auto uses full precision for final renders and half precision otherwise. */
612 if (this->render_context()) {
614 }
615 else {
617 }
620 }
621
624 }
625
627 {
628 return input_data_.render_context;
629 }
630
632 {
633 return this->get_scene().runtime->compositor.nodes_evaluation_log.get();
634 }
635
636 void evaluate_operation_post() const override
637 {
638 /* If the compositor is executing due to a user edit the node tree, we wait until the operation
639 * finishes executing on the GPU before we continue to improve interactivity. The improvement
640 * comes from the fact that the user might be rapidly changing values, so we need to cancel
641 * previous evaluations to make editing faster, but we can't do that if all operations are
642 * submitted to the GPU all at once, and we can't cancel work that was already submitted to the
643 * GPU. This does have a performance penalty, but in practice, the improved interactivity is
644 * worth it according to user feedback. */
645 if (this->use_gpu() && input_data_.triggered_by_user) {
646 GPU_finish();
647 }
648 }
649
650 bool is_canceled() const override
651 {
652 return input_data_.render.display->test_break();
653 }
654
655 /* Checks if a cached viewer result exists for the current frame. If no cache is found, false is
656 * returned and nothing is done. If the cache exists, write it to the viewer image and return
657 * true. */
659 {
660 const Scene *original_scene = DEG_get_original(&this->get_scene());
661 const int view_identifier = BKE_scene_multiview_view_id_get(&input_data_.render_data,
662 input_data_.view_name.c_str());
663 ImBuf *cached_buffer = original_scene->runtime->compositor.cache.get_frame(
664 this->get_frame_number(), view_identifier);
665 if (!cached_buffer) {
666 return false;
667 }
668
669 Image *image = BKE_image_ensure_viewer(G.main, IMA_TYPE_COMPOSITE, "Viewer Node");
670
671 ImageUser image_user = {nullptr};
672 image_user.multi_index = view_identifier;
673
674 if (BKE_scene_multiview_is_render_view_first(&input_data_.render_data,
675 input_data_.view_name.c_str()))
676 {
677 BKE_image_ensure_viewer_views(&input_data_.render_data, image, &image_user);
678 }
679
681
682 void *lock;
683 ImBuf *image_buffer = BKE_image_acquire_ibuf_gpu(image, &image_user, &lock);
684
685 image_buffer->x = cached_buffer->x;
686 image_buffer->y = cached_buffer->y;
687 copy_v2_v2_int(image_buffer->display_size, cached_buffer->display_size);
688 copy_v2_v2_int(image_buffer->data_offset, cached_buffer->data_offset);
689 copy_v2_v2_int(image_buffer->display_offset, cached_buffer->display_offset);
690 image_buffer->color_mode = cached_buffer->color_mode;
691 image_buffer->channels = cached_buffer->channels;
692 image_buffer->flags = cached_buffer->flags;
693 image_buffer->byte_buffer = cached_buffer->byte_buffer;
694 image_buffer->float_buffer = cached_buffer->float_buffer;
695 IMB_free_gpu_textures(image_buffer);
696 if (cached_buffer->gpu.texture) {
698 GPU_texture_width(cached_buffer->gpu.texture),
699 GPU_texture_height(cached_buffer->gpu.texture),
700 1,
701 GPU_texture_format(cached_buffer->gpu.texture),
703 nullptr);
704 GPU_texture_copy(texture, cached_buffer->gpu.texture);
705 IMB_assign_gpu_texture(image_buffer, texture);
706 }
707 image_buffer->userflags = cached_buffer->userflags;
708
710 image->flag &= ~IMA_VIEW_AS_RENDER;
711 }
712 else {
713 image->flag |= IMA_VIEW_AS_RENDER;
714 }
715
716 IMB_freeImBuf(cached_buffer);
717
718 IMB_partial_update_mark_full(image_buffer);
719 BKE_image_release_ibuf(image, image_buffer, lock);
721
722 return true;
723 }
724
725 void evaluate()
726 {
727 if (this->write_frame_cache()) {
728 return;
729 }
730
731 /* Reset log before evaluation. */
732 this->get_scene().runtime->compositor.nodes_evaluation_log =
733 std::make_unique<nodes::eval_log::NodesEvalLog>();
734
737 compositor::Result combined_pass = this->get_pass(&this->get_scene(), 0, RE_PASSNAME_COMBINED);
738 operation.map_input_to_result(&combined_pass);
739 operation.evaluate();
740
741 if (!operation.has_output()) {
742 operation.free_results();
743 return;
744 }
745
746 compositor::Result &output_result = operation.get_result();
747
748 /* If the operation does not have a viewer output but one is needed, write the output as a
749 * viewer. */
750 const bool needs_viewer_output = flag_is_set(this->needed_side_effect_output_types(),
752 if (!this->viewer_compute_context() && needs_viewer_output) {
753 this->write_viewer(output_result);
754 }
755
756 if (this->render_context()) {
757 this->write_output(output_result);
758 }
759
760 output_result.release();
761 }
762};
763
764/* Render Compositor */
765
767 private:
768 /* Render instance for GPU context to run compositor in. */
769 Render &render_;
770
771 compositor::StaticCacheManager cache_manager_;
772
773 /* Stores the execution device and precision used in the last evaluation of the compositor. Those
774 * might be different from the current values returned by the context, since the user might have
775 * changed them since the last evaluation. See the needs_to_be_recreated method for more info on
776 * why those are needed. */
777 bool last_evaluation_used_gpu_ = false;
779
780 public:
782
784 {
785 /* Use last_evaluation_used_gpu_ instead of the currently used device because we are freeing
786 * resources from the last evaluation. See last_evaluation_used_gpu_ for more information. */
787 if (last_evaluation_used_gpu_) {
788 /* Free resources with GPU context enabled. Cleanup may happen from the main thread, and we
789 * must use the main context there. */
790 if (BLI_thread_is_main()) {
792 }
793 else {
795 }
796 }
797
798 cache_manager_.free();
799
800 /* See comment above on context enabling. */
801 if (last_evaluation_used_gpu_) {
802 if (BLI_thread_is_main()) {
804 }
805 else {
807 }
808 }
809 }
810
811 void execute(const CompositorInputData &input_data)
812 {
813 bke::ComputeContextCache compute_context_cache;
814 Context context(cache_manager_, compute_context_cache, input_data);
815
816 if (context.use_gpu()) {
817 /* For main thread rendering in background mode, blocking rendering, or when we do not have a
818 * render system GPU context, use the DRW context directly, while for threaded rendering when
819 * we have a render system GPU context, use the render's system GPU context to avoid blocking
820 * with the global DST. */
821 GHOST_IContext *re_system_gpu_context = RE_system_gpu_context_get(&render_);
822 if (BLI_thread_is_main() || re_system_gpu_context == nullptr) {
824 context.set_gpu_supported(DRW_gpu_context_is_enabled());
825 }
826 else if (re_system_gpu_context) {
827 WM_system_gpu_context_activate(re_system_gpu_context);
828
829 void *re_blender_gpu_context = RE_blender_gpu_context_ensure(&render_);
830
832 GPU_context_active_set(static_cast<GPUContext *>(re_blender_gpu_context));
833 }
834 else {
835 context.set_gpu_supported(false);
836 }
837 }
838
839 {
840 context.evaluate();
841
842 /* Reset the cache, but only if the evaluation did not get canceled, because in that case, we
843 * wouldn't want to invalidate the cache because not all operations that use cached resources
844 * got the chance to mark their used resources as still in use. So we wait until a full
845 * evaluation happen before we decide that some resources are no longer needed. */
846 if (!context.is_canceled()) {
847 context.cache_manager().reset();
848 }
849
850 last_evaluation_used_gpu_ = context.use_gpu();
851 last_evaluation_precision_ = context.get_precision();
852 }
853
854 if (context.use_gpu()) {
855 gpu::TexturePool::get().reset();
856
857 GHOST_IContext *re_system_gpu_context = RE_system_gpu_context_get(&render_);
858 if (BLI_thread_is_main() || re_system_gpu_context == nullptr) {
860 }
861 else {
862 GPU_context_active_set(nullptr);
864 GHOST_IContext *re_system_gpu_context = RE_system_gpu_context_get(&render_);
865 WM_system_gpu_context_release(re_system_gpu_context);
866 }
867 }
868 }
869
870 /* Returns true if the compositor should be freed and reconstructed, which is needed when the
871 * compositor execution device or precision changed, because we either need to update all cached
872 * resources for the new execution device and precision, or we simply recreate the entire
873 * compositor, since it is much easier and safer. */
875 {
876 bke::ComputeContextCache compute_context_cache;
877 Context context(cache_manager_, compute_context_cache, input_data);
878 /* See last_evaluation_used_gpu_ and last_evaluation_precision_ for more information what how
879 * they are different from the ones returned from the context. */
880 return context.use_gpu() != last_evaluation_used_gpu_ ||
881 context.get_precision() != last_evaluation_precision_;
882 }
883};
884
885} // namespace render
886
888{
889 std::unique_lock lock(this->compositor_mutex);
890
891 if (this->compositor && this->compositor->needs_to_be_recreated(input_data)) {
892 /* Free it here and it will be recreated in the check below. */
893 delete this->compositor;
894 this->compositor = nullptr;
895 }
896
897 if (!this->compositor) {
898 this->compositor = new render::Compositor(*this);
899 }
900
901 this->compositor->execute(input_data);
902}
903
905{
906 std::unique_lock lock(this->compositor_mutex);
907
908 if (this->compositor != nullptr) {
909 delete this->compositor;
910 this->compositor = nullptr;
911 }
912}
913
915{
916 input_data.render.compositor_execute(input_data);
917}
918
920{
921 render.compositor_free();
922}
923
924} // namespace blender
#define BLI_SCOPED_DEFER(function_to_defer)
#define RE_PASSNAME_COMBINED
Read Blender memory allocation and freeing.
volatile int lock
#define BLI_assert_unreachable()
const void * data() const
constexpr StringRef()
Result create_result(ResultType type, ResultPrecision precision)
bke::ComputeContextCache & compute_context_cache() const
void share_data(const Result &source)
Definition result.cc:996
const ImplicitSharingPtr & sharing_info() const
static ResultType type(gpu::TextureFormat format)
Definition result.cc:337
gpu::TextureFormat get_gpu_texture_format() const
Definition result.cc:487
void set_precision(ResultPrecision precision)
Definition result.cc:1154
static ResultPrecision precision(gpu::TextureFormat format)
Definition result.cc:307
const T & get_single_value() const
static TexturePool & get()
bool needs_to_be_recreated(const CompositorInputData &input_data)
void execute(const CompositorInputData &input_data)
const Scene & get_scene() const override
StringRef get_view_name() const override
compositor::RenderContext * render_context() const override
void write_output(compositor::Result &result)
compositor::Result get_pass(const Scene *scene, int view_layer_id, const char *name) override
compositor::ResultPrecision get_precision() const override
compositor::SideEffectOutputTypes needed_side_effect_output_types() const override
void evaluate_operation_post() const override
const ComputeContext * viewer_compute_context() const override
compositor::ResultType get_pass_data_type(const RenderPass *pass)
Context(compositor::StaticCacheManager &cache_manager, bke::ComputeContextCache &compute_context_cache, const CompositorInputData &input_data)
void write_output_image(const compositor::Result &result)
void write_viewer(compositor::Result &viewer_result) override
void write_viewer_image(const compositor::Result &viewer_result)
compositor::Domain get_compositing_domain() const override
const RenderData & get_render_data() const override
void set_gpu_supported(const bool supported)
nodes::eval_log::NodesEvalLog * nodes_evaluation_log() const override
const Main & get_main() const override
compositor::ResultType get_pass_type(const RenderPass *pass)
TEX_TEMPLATE DataVec texture(T, FltCoord, float=0.0f) RET
StringRef BKE_cryptomatte_extract_layer_name(StringRef render_pass_name)
std::string BKE_cryptomatte_meta_data_key(StringRef layer_name, StringRefNull key_name)
ColorSceneLinear4f< eAlpha::Premultiplied > Color
Definition COM_result.hh:96
BMesh Iterators.
void DRW_gpu_context_enable()
void * RE_blender_gpu_context_ensure(Render *re)
MINLINE void copy_v2_v2_int(int r[2], const int a[2])
void DRW_render_context_enable(Render *render)
void IMB_free_byte_pixels(ImBuf *ibuf)
void IMB_assign_gpu_texture(ImBuf *ibuf, gpu::Texture *texture)
Definition util_gpu.cc:850
gpu::TextureFormat GPU_texture_format(const gpu::Texture *texture)
void DRW_render_context_disable(Render *render)
void RE_compositor_free(Render &render)
void * MEM_new_array_uninitialized(size_t len, size_t size, const char *str) ATTR_WARN_UNUSED_RESULT ATTR_ALLOC_SIZE(1
int BKE_scene_multiview_view_id_get(const RenderData *rd, const char *viewname)
Definition scene.cc:3335
RenderLayer * RE_GetRenderLayer(RenderResult *rr, const char *name)
void IMB_rectfill(ImBuf *drect, const float col[4])
Definition rectop.cc:1165
void GPU_texture_copy(gpu::Texture *dst, gpu::Texture *src)
void WM_system_gpu_context_release(GHOST_IContext *context)
void IMB_refImBuf(ImBuf *ibuf)
@ LOCK_DRAW_IMAGE
void GPU_render_begin()
void GPU_context_active_set(GPUContext *)
bool DRW_gpu_context_is_enabled()
void BKE_render_resolution(const RenderData *r, const bool use_crop, int *r_width, int *r_height)
Definition scene.cc:3168
ImBuf * IMB_dupImBuf(const ImBuf *ibuf1)
@ SCE_COMPOSITOR_CACHE_FRAMES
Global G
Definition blender.cc:57
void * BLI_findlink(const ListBase *listbase, int number) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL(1)
Definition listbase.cc:541
void DRW_gpu_context_disable()
Render * RE_GetSceneRender(const Scene *scene)
bool BKE_scene_multiview_is_render_view_first(const RenderData *rd, const char *viewname)
Definition scene.cc:3267
ImBuf * BKE_image_acquire_ibuf_gpu(Image *ima, ImageUser *iuser, void **r_lock, bool *r_load_failed=nullptr, bool cached_only=false)
void IMB_freeImBuf(ImBuf *ibuf)
VecBase< int32_t, 2 > int2
@ GPU_TEXTURE_USAGE_GENERAL
void GPU_render_end()
RenderResult * RE_AcquireResultWrite(Render *re)
void GPU_memory_barrier(GPUBarrier barrier)
Definition gpu_state.cc:305
@ SCE_COMPOSITOR_DEVICE_GPU
const char * IMB_colormanagement_role_colorspace_name_get(int role)
BMesh const char void * data
int GPU_texture_width(const gpu::Texture *texture)
int BLI_thread_is_main()
Definition threads.cc:184
void IMB_partial_update_mark_full(ImBuf *ibuf)
@ GPU_BARRIER_TEXTURE_UPDATE
Definition GPU_state.hh:42
void WM_system_gpu_context_activate(GHOST_IContext *context)
uint32_t hash_key
RenderResult * RE_AcquireResultRead(Render *re)
RenderPass * RE_pass_find_by_name(RenderLayer *rl, const char *name, const char *viewname)
void RE_compositor_execute(const render::CompositorInputData input_data)
T * DEG_get_original(T *id)
bool IMB_colormanagement_space_is_data(const ColorSpace *colorspace)
void BKE_stamp_info_callback(void *data, StampData *stamp_data, StampCallback callback, bool noskip)
gpu::Texture * IMB_acquire_gpu_texture(const char *name, ImBuf *ibuf, GPUTextureCreateFlags texture_create_flags, bool try_only=false)
Definition util_gpu.cc:778
void GPU_texture_clear(gpu::Texture *texture, eGPUDataFormat data_format, const void *data)
int GPU_texture_height(const gpu::Texture *texture)
void GPU_texture_free(gpu::Texture *texture)
const ColorSpace * IMB_colormanagement_space_get_named(const char *name)
void BKE_image_ensure_viewer_views(const RenderData *rd, Image *ima, ImageUser *iuser)
Image * BKE_image_ensure_viewer(Main *bmain, int type, const char *name)
void IMB_free_gpu_textures(ImBuf *ibuf)
Definition util_gpu.cc:835
RenderView * RE_RenderViewGetByName(RenderResult *rr, const char *viewname)
ImBuf * RE_RenderViewEnsureImBuf(const RenderResult *render_result, RenderView *render_view)
void * GPU_texture_read(gpu::Texture *texture, eGPUDataFormat data_format, int mip_level)
@ SCE_COMPOSITOR_PRECISION_AUTO
@ SCE_COMPOSITOR_PRECISION_FULL
bool IMB_alloc_float_pixels(ImBuf *ibuf, unsigned int channels, bool initialize_pixels=true)
void BLI_thread_unlock(int type)
Definition threads.cc:336
void GPU_finish()
Definition gpu_state.cc:289
bool DEG_scene_component_depends_on_time(const Depsgraph &depsgraph, const Scene &scene, const eDepsSceneComponentType component_type)
void IMB_free_float_pixels(ImBuf *ibuf)
GHOST_IContext * RE_system_gpu_context_get(Render *re)
gpu::Texture * GPU_texture_create_2d(const char *name, int width, int height, int mip_len, gpu::TextureFormat format, eGPUTextureUsage usage, const float *data)
void BLI_thread_lock(int type)
Definition threads.cc:331
void BKE_image_release_ibuf(Image *ima, ImBuf *ibuf, void *lock)
ImplicitSharingPtr sharing_info
const ColorSpace * colorspace
std::atomic< gpu::Texture * > texture
ImBufFloatBuffer float_buffer
ImBufByteBuffer byte_buffer
const float * float_data() const
ImColorMode color_mode
float * float_data_for_write()
Definition allocimbuf.cc:79
void assign_float_data(float *data)
eCompositorDevice compositor_device
struct ImBuf * ibuf
Definition RE_pipeline.h:72
void compositor_execute(render::CompositorInputData input_data) override
render::Compositor * compositor
void compositor_free() override
ListBaseT< ViewLayer > view_layers
bke::SceneRuntime * runtime
void add_frame(int frame_number, int view_identifier, ImBuf *image_buffer)
ImBuf * get_frame(int frame_number, int view_identifier)