Blender V5.3
result.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
5#include <cstdint>
6#include <optional>
7#include <string>
8#include <variant>
9
10#include "BLI_assert.hh"
11#include "BLI_cpp_type.hh"
12#include "BLI_generic_array.hh"
14#include "BLI_generic_span.hh"
18#include "BLI_string_ref.hh"
19
20#include "BLT_translation.hh"
21
22#include "GPU_context.hh"
23#include "GPU_shader.hh"
24#include "GPU_state.hh"
25#include "GPU_texture.hh"
26#include "GPU_texture_pool.hh"
27
29
30#include "COM_closure.hh"
31#include "COM_context.hh"
33#include "COM_domain.hh"
34#include "COM_result.hh"
35#include "COM_utilities.hh"
36
37namespace blender::compositor {
38
39Result::Result(Context &context) : context_(&context) {}
40
42 : context_(&context), type_(type), precision_(precision)
43{
44}
45
47 : context_(&context), type_(Result::type(format)), precision_(Result::precision(format))
48{
49}
50
52{
53 switch (type) {
59 case ResultType::Int:
67 return false;
77 return true;
78 }
79
81 return true;
82}
83
85{
86 switch (precision) {
88 switch (type) {
90 return gpu::TextureFormat::SFLOAT_16;
93 return gpu::TextureFormat::SFLOAT_16_16_16_16;
95 /* RGB textures are not fully supported by hardware, so we store Float3 results in RGBA
96 * textures. */
97 return gpu::TextureFormat::SFLOAT_16_16_16_16;
99 return gpu::TextureFormat::SFLOAT_16_16;
100 case ResultType::Int:
101 return gpu::TextureFormat::SINT_16;
102 case ResultType::Int2:
103 return gpu::TextureFormat::SINT_16_16;
104 case ResultType::Int3:
105 /* RGB textures are not fully supported by hardware, so we store Int3 results in RGBA
106 * textures. */
107 return gpu::TextureFormat::SINT_16_16_16_16;
108 case ResultType::Int4:
109 return gpu::TextureFormat::SINT_16_16_16_16;
110 case ResultType::Bool:
111 /* No bool texture formats, so we store in an 8-bit integer. Precision doesn't matter. */
112 return gpu::TextureFormat::SINT_8;
114 /* Stored as an array of 4 RGBA texture. */
115 return gpu::TextureFormat::SFLOAT_16_16_16_16;
116 case ResultType::Menu:
117 /* Menu values are technically stored in 32-bit integers, but 8 is sufficient in
118 * practice. */
119 return gpu::TextureFormat::SINT_8;
121 return gpu::TextureFormat::SFLOAT_16_16_16_16;
125 case ResultType::Font:
127 case ResultType::Text:
128 case ResultType::Mask:
131 /* Single only types do not support GPU code path. */
134 break;
135 }
136 break;
138 switch (type) {
140 return gpu::TextureFormat::SFLOAT_32;
143 return gpu::TextureFormat::SFLOAT_32_32_32_32;
145 /* RGB textures are not fully supported by hardware, so we store Float3 results in RGBA
146 * textures. */
147 return gpu::TextureFormat::SFLOAT_32_32_32_32;
149 return gpu::TextureFormat::SFLOAT_32_32;
150 case ResultType::Int:
151 return gpu::TextureFormat::SINT_32;
152 case ResultType::Int2:
153 return gpu::TextureFormat::SINT_32_32;
154 case ResultType::Int3:
155 /* RGB textures are not fully supported by hardware, so we store Int3 results in RGBA
156 * textures. */
157 return gpu::TextureFormat::SINT_32_32_32_32;
158 case ResultType::Int4:
159 return gpu::TextureFormat::SINT_32_32_32_32;
160 case ResultType::Bool:
161 /* No bool texture formats, so we store in an 8-bit integer. Precision doesn't matter. */
162 return gpu::TextureFormat::SINT_8;
164 /* Stored as an array of 4 RGBA texture. */
165 return gpu::TextureFormat::SFLOAT_32_32_32_32;
166 case ResultType::Menu:
167 /* Menu values are technically stored in 32-bit integers, but 8 is sufficient in
168 * practice. */
169 return gpu::TextureFormat::SINT_8;
171 return gpu::TextureFormat::SFLOAT_32_32_32_32;
175 case ResultType::Font:
177 case ResultType::Text:
178 case ResultType::Mask:
181 /* Single only types do not support GPU storage. */
184 break;
185 }
186 break;
187 }
188
190 return gpu::TextureFormat::SFLOAT_32_32_32_32;
191}
192
194{
195 switch (type) {
203 return GPU_DATA_FLOAT;
204 case ResultType::Int:
205 case ResultType::Int2:
206 case ResultType::Int3:
207 case ResultType::Int4:
208 case ResultType::Bool:
209 case ResultType::Menu:
210 return GPU_DATA_INT;
214 case ResultType::Font:
216 case ResultType::Text:
217 case ResultType::Mask:
220 /* Single only types do not support GPU storage. */
223 break;
224 }
225
227 return GPU_DATA_FLOAT;
228}
229
231{
232 switch (precision) {
234 switch (format) {
235 /* Already half precision, return the input format. */
236 case gpu::TextureFormat::SFLOAT_16:
237 case gpu::TextureFormat::SFLOAT_16_16:
238 case gpu::TextureFormat::SFLOAT_16_16_16:
239 case gpu::TextureFormat::SFLOAT_16_16_16_16:
240 case gpu::TextureFormat::SINT_16:
241 case gpu::TextureFormat::SINT_16_16:
242 case gpu::TextureFormat::SINT_16_16_16_16:
243 return format;
244
245 /* Used to store booleans where precision doesn't matter. */
246 case gpu::TextureFormat::SINT_8:
247 return format;
248
249 case gpu::TextureFormat::SFLOAT_32:
250 return gpu::TextureFormat::SFLOAT_16;
251 case gpu::TextureFormat::SFLOAT_32_32:
252 return gpu::TextureFormat::SFLOAT_16_16;
253 case gpu::TextureFormat::SFLOAT_32_32_32:
254 return gpu::TextureFormat::SFLOAT_16_16_16;
255 case gpu::TextureFormat::SFLOAT_32_32_32_32:
256 return gpu::TextureFormat::SFLOAT_16_16_16_16;
257 case gpu::TextureFormat::SINT_32:
258 return gpu::TextureFormat::SINT_16;
259 case gpu::TextureFormat::SINT_32_32:
260 return gpu::TextureFormat::SINT_16_16;
261 case gpu::TextureFormat::SINT_32_32_32_32:
262 return gpu::TextureFormat::SINT_16_16_16_16;
263 default:
264 break;
265 }
266 break;
268 switch (format) {
269 /* Already full precision, return the input format. */
270 case gpu::TextureFormat::SFLOAT_32:
271 case gpu::TextureFormat::SFLOAT_32_32:
272 case gpu::TextureFormat::SFLOAT_32_32_32:
273 case gpu::TextureFormat::SFLOAT_32_32_32_32:
274 case gpu::TextureFormat::SINT_32:
275 case gpu::TextureFormat::SINT_32_32:
276 case gpu::TextureFormat::SINT_32_32_32_32:
277 return format;
278
279 /* Used to store booleans where precision doesn't matter. */
280 case gpu::TextureFormat::SINT_8:
281 return format;
282
283 case gpu::TextureFormat::SFLOAT_16:
284 return gpu::TextureFormat::SFLOAT_32;
285 case gpu::TextureFormat::SFLOAT_16_16:
286 return gpu::TextureFormat::SFLOAT_32_32;
287 case gpu::TextureFormat::SFLOAT_16_16_16:
288 return gpu::TextureFormat::SFLOAT_32_32_32;
289 case gpu::TextureFormat::SFLOAT_16_16_16_16:
290 return gpu::TextureFormat::SFLOAT_32_32_32_32;
291 case gpu::TextureFormat::SINT_16:
292 return gpu::TextureFormat::SINT_32;
293 case gpu::TextureFormat::SINT_16_16:
294 return gpu::TextureFormat::SINT_32_32;
295 case gpu::TextureFormat::SINT_16_16_16_16:
296 return gpu::TextureFormat::SINT_32_32_32_32;
297 default:
298 break;
299 }
300 break;
301 }
302
304 return format;
305}
306
308{
309 switch (format) {
310 case gpu::TextureFormat::SFLOAT_16:
311 case gpu::TextureFormat::SFLOAT_16_16:
312 case gpu::TextureFormat::SFLOAT_16_16_16:
313 case gpu::TextureFormat::SFLOAT_16_16_16_16:
314 case gpu::TextureFormat::SINT_16:
315 case gpu::TextureFormat::SINT_16_16:
316 case gpu::TextureFormat::SINT_16_16_16_16:
318 case gpu::TextureFormat::SFLOAT_32:
319 case gpu::TextureFormat::SFLOAT_32_32:
320 case gpu::TextureFormat::SFLOAT_32_32_32:
321 case gpu::TextureFormat::SFLOAT_32_32_32_32:
322 case gpu::TextureFormat::SINT_32:
323 case gpu::TextureFormat::SINT_32_32:
324 case gpu::TextureFormat::SINT_32_32_32_32:
326 /* Used to store booleans where precision doesn't matter. */
327 case gpu::TextureFormat::SINT_8:
329 default:
330 break;
331 }
332
335}
336
338{
339 switch (format) {
340 case gpu::TextureFormat::SFLOAT_16:
341 case gpu::TextureFormat::SFLOAT_32:
342 return ResultType::Float;
343 case gpu::TextureFormat::SFLOAT_16_16:
344 case gpu::TextureFormat::SFLOAT_32_32:
345 return ResultType::Float2;
346 case gpu::TextureFormat::SFLOAT_16_16_16:
347 case gpu::TextureFormat::SFLOAT_32_32_32:
348 return ResultType::Float3;
349 case gpu::TextureFormat::SFLOAT_16_16_16_16:
350 case gpu::TextureFormat::SFLOAT_32_32_32_32:
351 return ResultType::Color;
352 case gpu::TextureFormat::SINT_16:
353 case gpu::TextureFormat::SINT_32:
354 return ResultType::Int;
355 case gpu::TextureFormat::SINT_16_16:
356 case gpu::TextureFormat::SINT_32_32:
357 return ResultType::Int2;
358 case gpu::TextureFormat::SINT_16_16_16_16:
359 case gpu::TextureFormat::SINT_32_32_32_32:
360 return ResultType::Int4;
361 case gpu::TextureFormat::SINT_8:
362 return ResultType::Bool;
363 default:
364 break;
365 }
366
368 return ResultType::Color;
369}
370
372{
373 switch (type) {
375 return CPPType::get<float>();
377 return CPPType::get<float2>();
379 return CPPType::get<float3>();
381 return CPPType::get<float4>();
383 return CPPType::get<Color>();
384 case ResultType::Int:
385 return CPPType::get<int32_t>();
386 case ResultType::Int2:
387 return CPPType::get<int2>();
388 case ResultType::Int3:
389 return CPPType::get<int3>();
390 case ResultType::Int4:
391 return CPPType::get<int4>();
392 case ResultType::Bool:
393 return CPPType::get<bool>();
395 return CPPType::get<float4x4>();
396 case ResultType::Menu:
403 return CPPType::get<Object *>();
405 return CPPType::get<Image *>();
406 case ResultType::Font:
407 return CPPType::get<VFont *>();
409 return CPPType::get<Scene *>();
410 case ResultType::Text:
411 return CPPType::get<Text *>();
412 case ResultType::Mask:
413 return CPPType::get<Mask *>();
418 }
419
421 return CPPType::get<float>();
422}
423
425{
426 switch (type) {
428 return "float";
430 return "float2";
432 return "float3";
434 return "float4";
436 return "color";
437 case ResultType::Int:
438 return "int";
439 case ResultType::Int2:
440 return "int2";
441 case ResultType::Int3:
442 return "int3";
443 case ResultType::Int4:
444 return "int4";
445 case ResultType::Bool:
446 return "bool";
448 return "float4x4";
449 case ResultType::Menu:
450 return "menu";
452 return "quaternion";
454 return "string";
456 return "object";
458 return "image";
459 case ResultType::Font:
460 return "font";
462 return "scene";
463 case ResultType::Text:
464 return "text";
465 case ResultType::Mask:
466 return "mask";
468 return "bundle";
470 return "closure";
471 }
472
474 return "";
475}
476
477Result::operator gpu::Texture *() const
478{
479 return this->gpu_texture();
480}
481
483{
484 return Result::cpp_type(this->type());
485}
486
488{
489 return Result::gpu_texture_format(type_, precision_);
490}
491
496
498 const Context &context,
499 const std::optional<ResultStorageType> storage_type)
500{
501 Domain sanitized_domain = domain;
502 const bool use_gpu = storage_type.has_value() ?
503 storage_type.value() == ResultStorageType::GPUImage :
504 context.use_gpu();
505 const int max_size = use_gpu ? 8192 : 32768;
506 sanitized_domain.data_size = math::clamp(domain.data_size, int2(1), int2(max_size));
507 return sanitized_domain;
508}
509
510/* A RAII structure that makes it easier to manage the different ways of allocating and freeing
511 * GPU textures. */
512class GPUData {
513 public:
514 /* The allocated texture. */
516
517 private:
518 /* If true, the GPU texture was allocated from the texture pool of the context and should be
519 * released back into the pool instead of being freed. */
520 const bool is_from_pool_;
521
522 public:
523 GPUData(const int2 size,
524 const ResultType type,
525 const ResultPrecision precision,
526 const bool is_from_pool)
527 : is_from_pool_(type == ResultType::Float4x4 ? false : is_from_pool)
528 {
529 const gpu::TextureFormat format = Result::gpu_texture_format(type, precision);
531 if (type == ResultType::Float4x4) {
532 this->texture = GPU_texture_create_2d_array(
533 __func__, size.x, size.y, 4, 1, format, usage, nullptr);
534 }
535 else if (is_from_pool) {
536 this->texture = gpu::TexturePool::get().acquire_texture_2d(size, 1, format, usage);
537 }
538 else {
539 this->texture = GPU_texture_create_2d(__func__, size.x, size.y, 1, format, usage, nullptr);
540 }
541 }
542
544 {
545 if (is_from_pool_) {
546 gpu::TexturePool::get().release_texture(this->texture);
547 }
548 else {
549 GPU_texture_free(this->texture);
550 }
551 }
552};
553
555 const bool from_pool,
556 const std::optional<ResultStorageType> storage_type)
557{
558 BLI_assert(this->should_compute());
559 BLI_assert(!this->is_allocated());
561
562 domain_ = sanitize_domain_data_size(domain, *context_, storage_type);
563
564 const bool use_gpu = storage_type.has_value() ?
565 storage_type.value() == ResultStorageType::GPUImage :
566 context_->use_gpu();
567 if (use_gpu) {
568 storage_type_ = ResultStorageType::GPUImage;
569 auto *new_texture = new ImplicitSharedValue<GPUData>(
570 domain_.data_size, this->type(), this->precision(), from_pool);
571 sharing_info_ = ImplicitSharingPtr<>(new_texture);
572 gpu_texture_ = new_texture->data.texture;
573 return;
574 }
575
576 storage_type_ = ResultStorageType::CPUImage;
577 const int64_t array_size = int64_t(domain_.data_size.x) * int64_t(domain_.data_size.y);
578 auto *new_array = new ImplicitSharedValue<GArray<>>(this->get_cpp_type(), array_size);
579 sharing_info_ = ImplicitSharingPtr<>(new_array);
580 cpu_data_ = new_array->data.as_span();
581}
582
584{
585 /* Make sure we are not allocating a result that should not be computed. */
586 BLI_assert(this->should_compute());
587 BLI_assert(!this->is_allocated());
588
589 storage_type_ = ResultStorageType::SingleValue;
590
591 /* Single values do not have a domain, but we assign them an identity domain by convention. */
592 domain_ = Domain::identity();
593
594 /* It is important that we initialize single values because the variant member that stores single
595 * values need to have its type initialized. */
596 switch (type_) {
598 single_value_ = 0.0f;
599 break;
601 single_value_ = float2(0.0f);
602 break;
604 single_value_ = float3(0.0f);
605 break;
607 single_value_ = float4(0.0f);
608 break;
610 single_value_ = Color(0.0f);
611 break;
612 case ResultType::Int:
613 single_value_ = 0;
614 break;
615 case ResultType::Int2:
616 single_value_ = int2(0);
617 break;
618 case ResultType::Int3:
619 single_value_ = int3(0);
620 break;
621 case ResultType::Int4:
622 single_value_ = int4(0);
623 break;
624 case ResultType::Bool:
625 single_value_ = false;
626 break;
628 single_value_ = float4x4::zero();
629 break;
630 case ResultType::Menu:
631 single_value_ = nodes::MenuValue(0);
632 break;
634 single_value_ = math::Quaternion(0.0f, 0.0f, 0.0f, 0.0f);
635 break;
637 single_value_ = std::string("");
638 break;
640 single_value_ = static_cast<Object *>(nullptr);
641 break;
643 single_value_ = static_cast<Image *>(nullptr);
644 break;
645 case ResultType::Font:
646 single_value_ = static_cast<VFont *>(nullptr);
647 break;
649 single_value_ = static_cast<Scene *>(nullptr);
650 break;
651 case ResultType::Text:
652 single_value_ = static_cast<Text *>(nullptr);
653 break;
654 case ResultType::Mask:
655 single_value_ = static_cast<Mask *>(nullptr);
656 break;
658 single_value_ = nodes::Bundle::create();
659 break;
661 single_value_ = ClosurePtr();
662 break;
663 }
664}
665
670
671Result Result::upload_to_gpu(const bool from_pool) const
672{
673 BLI_assert(storage_type_ == ResultStorageType::CPUImage);
674 BLI_assert(this->is_allocated());
675
676 Result result = Result(*context_, this->type(), this->precision());
677 result.allocate_texture(this->domain(), from_pool, ResultStorageType::GPUImage);
678
679 switch (this->type()) {
685 case ResultType::Int:
686 case ResultType::Int2:
687 case ResultType::Int4:
688 case ResultType::Bool:
689 case ResultType::Menu:
691 break;
692 case ResultType::Int3: {
693 /* Int3 is stored as an Int4 on GPU due to hardware limitations, so copy to an Int4 result
694 * before uploading. */
695 Result temporary_result = Result(*context_, ResultType::Int4, this->precision());
696 temporary_result.allocate_texture(this->domain(), false, ResultStorageType::CPUImage);
697 parallel_for(this->domain().data_size, [&](const int2 texel) {
698 temporary_result.store_pixel(texel, int4(this->load_pixel<int3>(texel), 0));
699 });
700 GPU_texture_update(result, this->get_gpu_data_format(), temporary_result.cpu_data().data());
701 temporary_result.release();
702 break;
703 }
704 case ResultType::Float3: {
705 /* Float3 is stored as a Float4 on GPU due to hardware limitations, so copy to a Float4
706 * result before uploading. */
707 Result temporary_result = Result(*context_, ResultType::Float4, this->precision());
708 temporary_result.allocate_texture(this->domain(), false, ResultStorageType::CPUImage);
709 parallel_for(this->domain().data_size, [&](const int2 texel) {
710 temporary_result.store_pixel(texel, float4(this->load_pixel<float3>(texel), 0.0f));
711 });
712 GPU_texture_update(result, this->get_gpu_data_format(), temporary_result.cpu_data().data());
713 temporary_result.release();
714 break;
715 }
717 const int2 size = this->domain().data_size;
718 Result temporary_result = Result(*context_, ResultType::Float4, this->precision());
719 /* Float4x4 is stored in a 4-layer array texture, with each layer storing a column, so copy
720 * to a result with 4 times the height, each slice storing a column. */
721 temporary_result.allocate_texture(size * int2(1, 4), false, ResultStorageType::CPUImage);
722 for (int i = 0; i < 4; i++) {
723 parallel_for(this->domain().data_size, [&](const int2 texel) {
724 temporary_result.store_pixel(texel + int2(0, size.y * i),
725 this->load_pixel<float4x4>(texel)[i]);
726 });
727 }
728 GPU_texture_update(result, this->get_gpu_data_format(), temporary_result.cpu_data().data());
729 temporary_result.release();
730 break;
731 }
735 case ResultType::Font:
737 case ResultType::Text:
738 case ResultType::Mask:
741 /* Single only types do not support GPU. */
742 break;
743 }
744 return result;
745}
746
748{
749 BLI_assert(storage_type_ == ResultStorageType::GPUImage);
750 BLI_assert(this->is_allocated());
751
752 Result result = Result(*context_, this->type(), this->precision());
753 result.allocate_texture(this->domain(), false, ResultStorageType::CPUImage);
754
756
757 switch (this->type()) {
763 case ResultType::Int:
764 case ResultType::Int2:
765 case ResultType::Int4:
766 case ResultType::Bool:
767 case ResultType::Menu:
768 GPU_texture_read(*this, this->get_gpu_data_format(), 0, result.cpu_data_for_write().data());
769 break;
770 case ResultType::Int3: {
771 if (this->channels_count() == 3) {
773 *this, this->get_gpu_data_format(), 0, result.cpu_data_for_write().data());
774 break;
775 }
776 /* Int3 is stored as an Int4 on GPU due to hardware limitations, so read to an Int4 result
777 * before copying to result. */
778 Result temporary_result = Result(*context_, ResultType::Int4, this->precision());
779 temporary_result.allocate_texture(this->domain(), false, ResultStorageType::CPUImage);
781 *this, this->get_gpu_data_format(), 0, temporary_result.cpu_data_for_write().data());
782 parallel_for(this->domain().data_size, [&](const int2 texel) {
783 result.store_pixel(texel, temporary_result.load_pixel<int4>(texel).xyz());
784 });
785 temporary_result.release();
786 break;
787 }
788 case ResultType::Float3: {
789 if (this->channels_count() == 3) {
791 *this, this->get_gpu_data_format(), 0, result.cpu_data_for_write().data());
792 break;
793 }
794 /* Float3 is stored as a Float4 on GPU due to hardware limitations, so read to a Float4
795 * result before copying to result. */
796 Result temporary_result = Result(*context_, ResultType::Float4, this->precision());
797 temporary_result.allocate_texture(this->domain(), false, ResultStorageType::CPUImage);
799 *this, this->get_gpu_data_format(), 0, temporary_result.cpu_data_for_write().data());
800 parallel_for(this->domain().data_size, [&](const int2 texel) {
801 result.store_pixel(texel, temporary_result.load_pixel<float4>(texel).xyz());
802 });
803 temporary_result.release();
804 break;
805 }
807 /* Float4x4 is stored in a 4-layer array texture, with each layer storing a column, so read
808 * to a result with 4 times the height, each slice storing a column, before finally copying
809 * and constructing the float4x4 in the result. */
810 const int2 size = this->domain().data_size;
811 Result temporary_result = Result(*context_, ResultType::Float4, this->precision());
812 temporary_result.allocate_texture(size * int2(1, 4), false, ResultStorageType::CPUImage);
814 *this, this->get_gpu_data_format(), 0, temporary_result.cpu_data_for_write().data());
815 parallel_for(this->domain().data_size, [&](const int2 texel) {
816 result.store_pixel(
817 texel,
818 float4x4(temporary_result.load_pixel<float4>(texel + int2(0, size.y * 0)),
819 temporary_result.load_pixel<float4>(texel + int2(0, size.y * 1)),
820 temporary_result.load_pixel<float4>(texel + int2(0, size.y * 2)),
821 temporary_result.load_pixel<float4>(texel + int2(0, size.y * 3))));
822 });
823 temporary_result.release();
824 break;
825 }
829 case ResultType::Font:
831 case ResultType::Text:
832 case ResultType::Mask:
835 /* Single only types do not support GPU. */
836 break;
837 }
838
839 return result;
840}
841
842/* Bind the given GPU texture to the texture image unit with the given name in the currently bound
843 * given shader. This also inserts a memory barrier for texture fetches to ensure any prior writes
844 * to the texture are reflected before reading from it. */
845static void bind_texture(gpu::Shader *shader, gpu::Texture *texture, const StringRef texture_name)
846{
847 /* Make sure any prior writes to the texture are reflected before reading from it. */
849
850 const int texture_image_unit = GPU_shader_get_sampler_binding(shader, texture_name.data());
851 GPU_texture_bind(texture, texture_image_unit);
852}
853
854void Result::bind_as_texture(gpu::Shader *shader, const char *texture_name) const
855{
856 BLI_assert(this->is_allocated());
857 BLI_assert(storage_type_ == ResultStorageType::GPUImage);
858 bind_texture(shader, this->gpu_texture(), texture_name);
859}
860
862 const char *texture_name) const
863{
864 if (!this->is_single_value()) {
865 bind_as_texture(shader, texture_name);
866 return nullptr;
867 }
868
869 BLI_assert(this->is_allocated());
870
871 /* Create a 1x1 texture where the value will be stored. */
872 const gpu::TextureFormat format = this->get_gpu_texture_format();
874 gpu::Texture *single_value_texture = nullptr;
875
876 /* Texture pool does not work on Vulkan at the moment. */
878 if (this->type() == ResultType::Float4x4) {
879 single_value_texture = GPU_texture_create_2d_array(
880 __func__, 1, 1, 4, 1, format, usage, nullptr);
881 }
882 else {
883 single_value_texture = GPU_texture_create_2d(__func__, 1, 1, 1, format, usage, nullptr);
884 }
885 }
886 else {
887 if (this->type() == ResultType::Float4x4) {
888 single_value_texture = gpu::TexturePool::get().acquire_texture_2d_array(
889 int2(1, 1), 4, 1, format, usage);
890 }
891 else {
892 single_value_texture = gpu::TexturePool::get().acquire_texture_2d(
893 int2(1, 1), 1, format, usage);
894 }
895 }
896
897 /* Upload the single value to the texture. */
898 switch (this->type()) {
903 case ResultType::Int:
904 case ResultType::Int2:
905 case ResultType::Int4:
906 case ResultType::Bool:
907 case ResultType::Menu:
910 single_value_texture, this->get_gpu_data_format(), this->single_value().get());
911 break;
912 case ResultType::Float3: {
913 /* Float3 results are stored in 4-component textures due to hardware limitations. So
914 * pad the value with a zero before updating. */
915 const float4 vector_value = float4(this->get_single_value<float3>(), 0.0f);
916 GPU_texture_update(single_value_texture, GPU_DATA_FLOAT, vector_value);
917 break;
918 }
919 case ResultType::Int3: {
920 /* Int3 results are stored in 4-component textures due to hardware limitations. So
921 * pad the value with a zero before updating. */
922 const int4 vector_value = int4(this->get_single_value<int3>(), 0);
923 GPU_texture_update(single_value_texture, GPU_DATA_INT, vector_value);
924 break;
925 }
927 /* Float4x4 stores each column in one texture layer in a 4-layer texture. */
929 single_value_texture, GPU_DATA_FLOAT, this->single_value().get(), 0, 0, 0, 1, 1, 4);
930 break;
931 }
935 case ResultType::Font:
937 case ResultType::Text:
938 case ResultType::Mask:
941 /* Single only types do not support GPU storage. */
944 break;
945 }
946
947 bind_texture(shader, single_value_texture, texture_name);
948 return single_value_texture;
949}
950
951void Result::bind_as_image(gpu::Shader *shader, const char *image_name, bool read) const
952{
953 BLI_assert(storage_type_ == ResultStorageType::GPUImage);
954
955 /* Make sure any prior writes to the texture are reflected before reading from it. */
956 if (read) {
958 }
959
960 const int image_unit = GPU_shader_get_sampler_binding(shader, image_name);
961 GPU_texture_image_bind(this->gpu_texture(), image_unit);
962}
963
965{
966 BLI_assert(storage_type_ == ResultStorageType::GPUImage);
968}
969
971{
972 if (!this->is_single_value()) {
973 BLI_assert(!single_value_texture);
974 this->unbind_as_texture();
975 return;
976 }
977
978 BLI_assert(single_value_texture);
979 GPU_texture_unbind(single_value_texture);
980
981 /* Texture pool does not work on Vulkan at the moment. */
983 GPU_texture_free(single_value_texture);
984 }
985 else {
986 gpu::TexturePool::get().release_texture(single_value_texture);
987 }
988}
989
995
996void Result::share_data(const Result &source)
997{
998 BLI_assert(type_ == source.type_);
999 BLI_assert(!this->is_allocated() && source.is_allocated());
1000
1001 /* Overwrite everything except reference count. */
1002 const int reference_count = reference_count_;
1003 *this = source;
1004 reference_count_ = reference_count;
1005
1006 /* Derived resources can't be shared, so reset them. */
1007 derived_resources_ = nullptr;
1008}
1009
1010/* Returns true if the given GPU texture is compatible with the type of the given result. */
1011[[maybe_unused]] static bool is_compatible_texture(const gpu::Texture *texture,
1012 const Result &result)
1013{
1014 /* Float3 and Int3 types are an exception, see the documentation on the get_gpu_texture_format
1015 * method for more information. */
1016 if (result.type() == ResultType::Float3) {
1018 gpu::TextureFormat::SFLOAT_32_32_32,
1019 gpu::TextureFormat::SFLOAT_16_16_16))
1020 {
1021 return true;
1022 }
1023 }
1024 else if (result.type() == ResultType::Int3) {
1026 gpu::TextureFormat::SINT_32_32_32,
1027 gpu::TextureFormat::SINT_16_16_16))
1028 {
1029 return true;
1030 }
1031 }
1032
1034 result.get_gpu_texture_format();
1035}
1036
1038{
1040 BLI_assert(!this->is_allocated());
1041
1042 gpu_texture_ = texture;
1043 storage_type_ = ResultStorageType::GPUImage;
1045 sharing_info_ = std::move(sharing_info);
1046}
1047
1049{
1050 BLI_assert(!this->is_allocated());
1051
1052 const int64_t array_size = int64_t(size.x) * int64_t(size.y);
1053 cpu_data_ = GSpan(this->get_cpp_type(), data, array_size);
1054 storage_type_ = ResultStorageType::CPUImage;
1055 domain_ = Domain(size);
1056 sharing_info_ = std::move(sharing_info);
1057}
1058
1059void Result::set_transformation(const float3x3 &transformation)
1060{
1061 domain_.transformation = transformation;
1062}
1063
1064void Result::transform(const float3x3 &transformation)
1065{
1066 domain_.transform(transformation);
1067}
1068
1070{
1071 return domain_.realization_options;
1072}
1073
1075{
1076 return domain_.realization_options;
1077}
1078
1080{
1081 reference_count_ = count;
1082}
1083
1085{
1086 reference_count_ -= count;
1087}
1088
1090{
1091 /* Decrement the reference count, and if it is not yet zero, return and do not free. */
1092 reference_count_--;
1093 BLI_assert(reference_count_ >= 0);
1094 if (reference_count_ != 0) {
1095 return;
1096 }
1097
1098 this->free();
1099}
1100
1102{
1103 if (!this->is_allocated()) {
1104 return;
1105 }
1106
1107 delete derived_resources_;
1108 derived_resources_ = nullptr;
1109
1110 sharing_info_ = {};
1111 switch (storage_type_) {
1113 single_value_ = std::monostate{};
1114 break;
1116 gpu_texture_ = nullptr;
1117 break;
1119 cpu_data_ = GSpan();
1120 break;
1121 }
1122}
1123
1125{
1126 return reference_count_ != 0;
1127}
1128
1130{
1131 if (!derived_resources_) {
1132 derived_resources_ = new DerivedResources();
1133 }
1134 return *derived_resources_;
1135}
1136
1138{
1139 return type_;
1140}
1141
1143{
1144 return precision_;
1145}
1146
1148{
1149 /* Changing the type can only be done if it wasn't allocated yet. */
1150 BLI_assert(!this->is_allocated());
1151 type_ = type;
1152}
1153
1155{
1156 /* Changing the precision can only be done if it wasn't allocated yet. */
1157 BLI_assert(!this->is_allocated());
1158 precision_ = precision;
1159}
1160
1162{
1163 switch (storage_type_) {
1165 return !std::holds_alternative<std::monostate>(single_value_);
1167 return this->gpu_texture();
1169 return this->cpu_data().data();
1170 }
1171
1172 return false;
1173}
1174
1176{
1177 return reference_count_;
1178}
1179
1181{
1182 if (storage_type_ == ResultStorageType::GPUImage) {
1184 }
1185
1186 switch (type_) {
1187 case ResultType::Float:
1188 case ResultType::Int:
1189 case ResultType::Bool:
1190 case ResultType::Menu:
1191 return 1;
1192 case ResultType::Float2:
1193 case ResultType::Int2:
1194 return 2;
1195 case ResultType::Float3:
1196 case ResultType::Int3:
1197 return 3;
1198 case ResultType::Color:
1199 case ResultType::Float4:
1200 case ResultType::Int4:
1202 return 4;
1204 return 16;
1205 case ResultType::String:
1206 case ResultType::Object:
1207 case ResultType::Image:
1208 case ResultType::Font:
1209 case ResultType::Scene:
1210 case ResultType::Text:
1211 case ResultType::Mask:
1212 case ResultType::Bundle:
1214 /* Single only types do not have channels. */
1217 break;
1218 }
1219
1221 return 4;
1222}
1223
1225{
1226 const int64_t pixel_size = this->get_cpp_type().size;
1227 if (this->is_single_value()) {
1228 return pixel_size;
1229 }
1230 const int2 image_size = this->domain().data_size;
1231 return pixel_size * image_size.x * image_size.y;
1232}
1233
1235{
1236 BLI_assert(this->is_single_value());
1237 BLI_assert(this->is_allocated());
1238 return std::visit([](const auto &value) { return GPointer(&value); }, single_value_);
1239}
1240
1242{
1243 BLI_assert(this->is_single_value());
1244 BLI_assert(this->is_allocated());
1245 return std::visit([](auto &value) { return GMutablePointer(&value); }, single_value_);
1246}
1247
1249{
1250 switch (precision) {
1252 return N_("Full");
1254 return N_("Half");
1255 }
1256
1258 return "None";
1259}
1260
1261} // namespace blender::compositor
#define BLI_assert_unreachable()
#define ELEM(...)
static const CPPType & get()
GPointer()=default
GSpan()=default
const void * data() const
constexpr const char * data() const
Domain(const int2 &size)
Definition domain.cc:20
static Domain identity()
Definition domain.cc:47
GPUData(const int2 size, const ResultType type, const ResultPrecision precision, const bool is_from_pool)
Definition result.cc:523
void decrement_reference_count(int count=1)
Definition result.cc:1084
void share_data(const Result &source)
Definition result.cc:996
RealizationOptions & get_realization_options()
Definition result.cc:1069
Result upload_to_gpu(const bool from_pool) const
Definition result.cc:671
void unbind_as_texture_or_single_value(gpu::Texture *single_value_texture) const
Definition result.cc:970
const CPPType & get_cpp_type() const
Definition result.cc:482
void store_pixel(const int2 &texel, const T &pixel_value)
bool should_compute() const
Definition result.cc:1124
void allocate_texture(const Domain domain, const bool from_pool=true, const std::optional< ResultStorageType > storage_type=std::nullopt)
Definition result.cc:554
static eGPUDataFormat gpu_data_format(const ResultType type)
Definition result.cc:193
int64_t size_in_bytes() const
Definition result.cc:1224
void unbind_as_texture() const
Definition result.cc:964
int64_t channels_count() const
Definition result.cc:1180
static gpu::TextureFormat gpu_texture_format(ResultType type, ResultPrecision precision)
Definition result.cc:84
Result download_to_cpu() const
Definition result.cc:747
static const char * type_name(const ResultType type)
Definition result.cc:424
void set_transformation(const float3x3 &transformation)
Definition result.cc:1059
static bool is_single_value_only_type(ResultType type)
Definition result.cc:51
GPointer single_value() const
Definition result.cc:1234
const ImplicitSharingPtr & sharing_info() const
eGPUDataFormat get_gpu_data_format() const
Definition result.cc:492
gpu::Texture * gpu_texture() const
GMutableSpan cpu_data_for_write()
static ResultType type(gpu::TextureFormat format)
Definition result.cc:337
void bind_as_texture(gpu::Shader *shader, const char *texture_name) const
Definition result.cc:854
gpu::TextureFormat get_gpu_texture_format() const
Definition result.cc:487
void set_precision(ResultPrecision precision)
Definition result.cc:1154
void transform(const float3x3 &transformation)
Definition result.cc:1064
static const CPPType & cpp_type(const ResultType type)
Definition result.cc:371
T load_pixel(const int2 &texel) const
void unbind_as_image() const
Definition result.cc:990
Result(Context &context)
Definition result.cc:39
static ResultPrecision precision(gpu::TextureFormat format)
Definition result.cc:307
DerivedResources & derived_resources()
Definition result.cc:1129
void bind_as_image(gpu::Shader *shader, const char *image_name, bool read=false) const
Definition result.cc:951
void set_reference_count(int count)
Definition result.cc:1079
gpu::Texture * bind_as_texture_or_single_value(gpu::Shader *shader, const char *texture_name) const
Definition result.cc:861
const T & get_single_value() const
void set_type(ResultType type)
Definition result.cc:1147
virtual Texture * acquire_texture_2d_array(int2 extent, int layer_len, int mip_len, TextureFormat format, eGPUTextureUsage usage, const char *name=nullptr)=0
virtual void release_texture(Texture *tex)=0
virtual Texture * acquire_texture_2d(int2 extent, int mip_len, TextureFormat format, eGPUTextureUsage usage, const char *name=nullptr)=0
static TexturePool & get()
TEX_TEMPLATE DataVec texture(T, FltCoord, float=0.0f) RET
StringRefNull to_string(const Interpolation &interpolation)
Definition domain.cc:112
static Domain sanitize_domain_data_size(const Domain domain, const Context &context, const std::optional< ResultStorageType > storage_type)
Definition result.cc:497
static bool is_compatible_texture(const gpu::Texture *texture, const Result &result)
Definition result.cc:1011
ColorSceneLinear4f< eAlpha::Premultiplied > Color
Definition COM_result.hh:96
ImplicitSharingPtr< Closure > ClosurePtr
static void bind_texture(gpu::Shader *shader, gpu::Texture *texture, const StringRef texture_name)
Definition result.cc:845
void parallel_for(const int2 range, const Function &function)
QuaternionBase< float > Quaternion
T clamp(const T &a, const T &min, const T &max)
BLI_assert(slot->slot_subtype.map==BMO_OP_SLOT_SUBTYPE_MAP_FLT)
int GPU_shader_get_sampler_binding(gpu::Shader *shader, const char *name)
__int64 int64_t
Definition BKE_text.h:90
void GPU_texture_update(gpu::Texture *texture, eGPUDataFormat data_format, const void *data)
gpu::TextureFormat GPU_texture_format(const gpu::Texture *texture)
void GPU_texture_image_unbind(gpu::Texture *texture)
GPUBackendType GPU_backend_get_type()
gpu::Texture * GPU_texture_create_2d_array(const char *name, int width, int height, int layer_len, int mip_len, gpu::TextureFormat format, eGPUTextureUsage usage, const float *data)
void GPU_texture_update_sub(gpu::Texture *texture, eGPUDataFormat data_format, const void *pixels, int offset_x, int offset_y, int offset_z, int width, int height, int depth, uint unpack_row_length=0)
@ GPU_TEXTURE_USAGE_GENERAL
void GPU_memory_barrier(GPUBarrier barrier)
Definition gpu_state.cc:305
BMesh const char void * data
int GPU_texture_width(const gpu::Texture *texture)
@ GPU_BARRIER_TEXTURE_FETCH
Definition GPU_state.hh:40
@ GPU_BARRIER_TEXTURE_UPDATE
Definition GPU_state.hh:42
@ GPU_BARRIER_SHADER_IMAGE_ACCESS
Definition GPU_state.hh:38
size_t GPU_texture_component_len(gpu::TextureFormat format)
void GPU_texture_bind(gpu::Texture *texture, int unit)
int GPU_texture_height(const gpu::Texture *texture)
void GPU_texture_free(gpu::Texture *texture)
void GPU_texture_image_bind(gpu::Texture *texture, int unit)
void GPU_texture_unbind(gpu::Texture *texture)
void * GPU_texture_read(gpu::Texture *texture, eGPUDataFormat data_format, int mip_level)
gpu::Texture * GPU_texture_create_2d(const char *name, int width, int height, int mip_len, gpu::TextureFormat format, eGPUTextureUsage usage, const float *data)
#define N_(msgid)