Blender V5.3
COM_result.hh
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#pragma once
6
7#include <cstdint>
8#include <optional>
9#include <string>
10#include <variant>
11
12#include "BLI_assert.hh"
13#include "BLI_color_types.hh"
15#include "BLI_cpp_type.hh"
17#include "BLI_generic_span.hh"
19#include "BLI_math_interp.hh"
22#include "BLI_math_vector.hh"
23#include "BLI_math_vector_c.hh"
25#include "BLI_memory_utils.hh"
26#include "BLI_string_ref.hh"
27
28#include "GPU_shader.hh"
29#include "GPU_texture.hh"
30
32#include "NOD_menu_value.hh"
33
34#include "COM_closure.hh"
35#include "COM_domain.hh"
36#include "COM_meta_data.hh"
37
38namespace blender {
39struct Object;
40struct Image;
41struct VFont;
42struct Scene;
43struct Text;
44struct Mask;
45} // namespace blender
46
47namespace blender::compositor {
48
49class Context;
51
52/* Make sure to update the format related static methods in the Result class. */
53enum class ResultType : uint8_t {
59 Int,
67
68 /* Single value only types. See Result::is_single_value_only_type. */
78};
79
80/* The precision of the data. CPU data is always stored using full precision at the moment. */
84};
85
86/* The type of storage used to hold the result data. */
88 /* Stored as a single value in an #std::variant of all types. */
90 /* Stored as an image in a #gpu::Texture on the GPU. */
92 /* Stored as an image in a buffer on the CPU. */
94};
95
97
98/* ------------------------------------------------------------------------------------------------
99 * Result
100 *
101 * A result represents the computed value of an output of an operation. A result can either
102 * represent an image or a single value. A result is typed, and can be of types like color, vector,
103 * or float. The image of a result can be stored on a GPU texture or a CPU buffer.The GPU texture
104 * can either be allocated from the texture pool of the context referenced by the result or it can
105 * be allocated directly from the GPU module, see the allocation method for more information.
106 *
107 * Results are reference counted and their data are released once their reference count reaches
108 * zero. After constructing a result, the set_reference_count method is called to declare the
109 * number of operations that needs this result. Once each operation that needs the result no longer
110 * needs it, the release method is called and the reference count is decremented, until it reaches
111 * zero, where the result's data is then released.
112 *
113 * A result not only represents an image, but also the area it occupies in the virtual compositing
114 * space. This area is called the Domain of the result, see the discussion in COM_domain.hh for
115 * more information.
116 *
117 * The result data can be shared by multiple results or shared with some external entity outside of
118 * the compositor. This is achieved by managing the data in an ImplicitSharingInfo, which is heap
119 * allocated and shared by all results that share the same data. See the sharing_info_ and the
120 * share_data method for more information.
121 *
122 * A result may store resources that are computed and cached in case they are needed by multiple
123 * operations. Those are called Derived Resources and can be accessed using the derived_resources
124 * method. */
125class Result {
126 private:
127 /* The context that the result was created within, this should be initialized during
128 * construction. */
129 Context *context_ = nullptr;
130 /* The base type of the result's image or single value. */
132 /* The precision of the result's data. Only relevant for GPU textures. CPU buffers and single
133 * values are always stored using full precision. */
135 /* The type of storage used to hold the data. Used to correctly interpret the data union. */
137 /* Stores a reference to the result's pixel data managed by the sharing info, either stored in a
138 * GPU texture or a buffer that is wrapped in a GSpan on CPU. */
139 union {
140 /* A reference to the result's image data managed by the sharing info. This will be a 2D
141 * texture for most types, but can be a 2D texture array for large types like float4x4 where
142 * each column will be stored in a layer. */
143 gpu::Texture *gpu_texture_;
144 /* A reference to the result's image data managed by the sharing info. */
145 GSpan cpu_data_;
146 };
147 /* Implicit sharing info manages the result's data, allowing it to be shared between multiple
148 * results and eventually freeing it when it is no longer needed. It is heap allocated during
149 * data allocation, it gains new users through calls to share_data, and its users get removed
150 * and its data potentially deleted in the free method. Notice that implicit sharing in Blender
151 * allows copying shared data to make it mutable, this is not allowed in the compositor, and it
152 * does not implement a copy-on-write mechanism, so copying needs to be done explicitly. The
153 * result may contain data with a nullptr sharing info, this is a special case where the data is
154 * considered external and needn't be managed/freed by the result. */
155 ImplicitSharingPtr<> sharing_info_ = nullptr;
156 /* The number of users that currently needs this result. Operations initializes this by calling
157 * the set_reference_count method before evaluation. Once each operation that needs the result no
158 * longer needs it, the release method is called and the reference count is decremented, until it
159 * reaches zero, where the result's data is then released. */
160 int reference_count_ = 1;
161 /* The single value stored in the result. This will be std::monostate if not a single value or
162 * not yet allocated. */
163 std::variant<std::monostate,
164 float,
165 float2,
166 float3,
167 float4,
168 Color,
169 int32_t,
170 int2,
171 int3,
172 int4,
173 bool,
174 float4x4,
177 std::string,
178 Object *,
179 Image *,
180 VFont *,
181 Scene *,
182 Text *,
183 Mask *,
186 single_value_ = std::monostate{};
187 /* The domain of the result. This only matters if the result was not a single value. See the
188 * discussion in COM_domain.hh for more information. */
189 Domain domain_ = Domain::identity();
190 /* Stores resources that are derived from this result. Lazily allocated if needed. See the class
191 * description for more information. */
192 DerivedResources *derived_resources_ = nullptr;
193
194 public:
195 /* Stores extra information about the result such as image meta data that can eventually be
196 * written to file. */
198
199 /* Construct a result within the given context. */
200 Result(Context &context);
201
202 /* Construct a result of the given type and precision within the given context. */
204
205 /* Construct a result of an appropriate type and precision based on the given GPU texture format
206 * within the given context. */
207 Result(Context &context, gpu::TextureFormat format);
208
209 /* Returns true if the given type can only be used with single value results. Consequently, it is
210 * always allocated on the CPU and GPU code paths needn't support the type. */
212
213 /* Returns the appropriate GPU texture format based on the given result type and precision. A
214 * special case is given to Float3 and Int3, because 3-component textures can't be used as write
215 * targets in shaders, so we need to allocate 4-component textures for them, and ignore the
216 * fourth channel during processing. */
218
219 /* Returns the GPU data format that corresponds to the give result type. */
221
222 /* Returns the GPU texture format that corresponds to the give one, but whose precision is the
223 * given precision. */
226
227 /* Returns the precision of the given GPU texture format. */
229
230 /* Returns the type of the given GPU texture format. */
231 static ResultType type(gpu::TextureFormat format);
232
233 /* Returns the CPP type corresponding to the given result type. */
234 static const CPPType &cpp_type(const ResultType type);
235
236 /* Returns a string representation of the given result type. */
237 static const char *type_name(const ResultType type);
238
239 /* Implicit conversion to the internal GPU texture. */
240 operator gpu::Texture *() const;
241
242 /* Returns the CPP type of the result. */
243 const CPPType &get_cpp_type() const;
244
245 /* Returns the appropriate texture format based on the result's type and precision. This is
246 * identical to the gpu_texture_format static method. This will match the format of the allocated
247 * texture, with one exception. Results of type Float3 or Int3 that share external textures might
248 * hold a 3-component texture as opposed to a 4-component one, which would have been created by
249 * uploading data from CPU. */
251
252 /* Identical to gpu_data_format but assumes the result's type. */
254
255 /* Declare the result to be a texture result, allocate a texture of an appropriate type with
256 * the size of the given domain, and set the domain of the result to the given domain.
257 *
258 * The data is allocated on the CPU or GPU depending on the given storage_type. A nullopt may be
259 * passed to storage_type, in which case, the data will be allocated on the device of the
260 * result's context as specified by context.use_gpu().
261 *
262 * If from_pool is true, GPU textures will be allocated from the texture pool of the context,
263 * otherwise, a new texture will be allocated. Pooling should not be used for persistent results
264 * that might span more than one evaluation, like cached resources. While pooling should be used
265 * for most other cases where the result will be allocated then later released in the same
266 * evaluation. Some types do not support pooling, since they require array textures which are not
267 * supported by the texture pool. */
268 void allocate_texture(const Domain domain,
269 const bool from_pool = true,
270 const std::optional<ResultStorageType> storage_type = std::nullopt);
271
272 /* Declare the result to be a single value result, allocate a value for it, and zero the
273 * initialize the value. */
275
276 /* Allocate a single value result whose value is zero. This is called for results whose value
277 * can't be computed and are considered invalid. */
278 void allocate_invalid();
279
280 /* Creates and allocates a new result that matches the type and precision of this result and
281 * uploads the CPU data that exist in this result. The result is assumed to be allocated on the
282 * CPU. See the allocate_data method for more information on the from_pool parameters. */
283 Result upload_to_gpu(const bool from_pool) const;
284
285 /* Creates and allocates a new result that matches the type and precision of this result and
286 * downloads the GPU data that exist in this result. The result is assumed to be allocated on the
287 * GPU. */
288 Result download_to_cpu() const;
289
290 /* Bind the GPU texture of the result to the texture image unit with the given name in the
291 * currently bound given shader. This also inserts a memory barrier for texture fetches to ensure
292 * any prior writes to the texture are reflected before reading from it. The unbind_as_texture
293 * method should be used to unbind the texture. */
294 void bind_as_texture(gpu::Shader *shader, const char *texture_name) const;
295
296 /* Identical to bind_as_texture but can be used in case the result could be a single value, in
297 * which case, a temporary 1x1 texture carrying the single value will be bound and returned. The
298 * shader should then use extended boundary to fetch the pixels to be able to retrieve the same
299 * value for all pixels. The unbind_as_texture_or_single_value method should be used to unbind
300 * the texture, passing the returned temporary texture. */
302 const char *texture_name) const;
303
304 /* Bind the GPU texture of the result to the image unit with the given name in the currently
305 * bound given shader. If read is true, a memory barrier will be inserted for image reads to
306 * ensure any prior writes to the images are reflected before reading from it. */
307 void bind_as_image(gpu::Shader *shader, const char *image_name, bool read = false) const;
308
309 /* Unbind the GPU texture which was previously bound using bind_as_texture. */
310 void unbind_as_texture() const;
311
312 /* Unbind the GPU texture which was previously bound using bind_as_texture_or_single_value given
313 * its return value as the argument. This will additionally release the temporary 1x1 as well,
314 * not just unbind it. */
315 void unbind_as_texture_or_single_value(gpu::Texture *single_value_texture) const;
316
317 /* Unbind the GPU texture which was previously bound using bind_as_image. */
318 void unbind_as_image() const;
319
320 /* Share the data of the given source result. This is done by simply adding a new user to the
321 * sharing info of the result. The source data is expect to be allocated and have the same type
322 * and precision as this result. */
323 void share_data(const Result &source);
324
325 /* Share the data of a GPU texture that is managed by the given implicit sharing info. If no
326 * implicit sharing info is provided, the texture is assumed to be external, has a lifetime that
327 * covers the entire evaluation of the compositor, and will thus not be freed. The domain will be
328 * set to have the data and display size as the texture size. The given texture should have a
329 * format that is compatible with the result. */
331
332 /* Share the data of a GPU buffer that is managed by the given implicit sharing info. If no
333 * implicit sharing info is provided, the buffer is assumed to be external, has a lifetime that
334 * covers the entire evaluation of the compositor, and will thus not be freed. The domain will be
335 * set to have the data and display size as the given size. The given buffer should have a format
336 * that is compatible with the result. */
337 void share_data(const void *data, int2 size, ImplicitSharingPtr<> sharing_info = nullptr);
338
339 /* Sets the transformation of the domain of the result to the given transformation. */
340 void set_transformation(const float3x3 &transformation);
341
342 /* Transform the result by the given transformation. This effectively pre-multiply the given
343 * transformation by the current transformation of the domain of the result. */
344 void transform(const float3x3 &transformation);
345
346 /* Get a reference to the realization options of this result. See the RealizationOptions struct
347 * for more information. */
350
351 /* Set the value of reference_count_, see that member for more details. This should be called
352 * after constructing the result to declare the number of operations that needs it. */
353 void set_reference_count(int count);
354
355 /* Decrement the reference count of the result by the given count. */
356 void decrement_reference_count(int count = 1);
357
358 /* Decrement the reference count of the result and free its data if it reaches zero. */
359 void release();
360
361 /* Remove a user from the result's data and frees the data if there are no more owners. If the
362 * result is not allocated, this will do nothing. */
363 void free();
364
365 /* Returns true if this result should be computed and false otherwise. The result should be
366 * computed if its reference count is not zero, that is, its result is used by at least one
367 * operation. */
368 bool should_compute() const;
369
370 /* Returns a reference to the derived resources of the result, which is allocated if it was not
371 * allocated already. */
373
374 /* Returns the type of the result. */
375 ResultType type() const;
376
377 /* Returns the precision of the result. */
379
380 /* Sets the type of the result. */
382
383 /* Sets the precision of the result. */
385
386 /* Returns true if the result is allocated. */
387 bool is_allocated() const;
388
389 /* Returns the reference count of the result. */
390 int reference_count() const;
391
392 /* Returns a reference to the domain of the result. See the Domain class. */
393 Domain &domain();
394 const Domain &domain() const;
395
396 /* Computes the number of channels of the result based on its type. */
397 int64_t channels_count() const;
398
399 /* Computes the size of the result's data in bytes. */
400 int64_t size_in_bytes() const;
401
402 gpu::Texture *gpu_texture() const;
403
404 GSpan cpu_data() const;
406
407 const ImplicitSharingPtr<> &sharing_info() const;
408
409 /* Returns true if the result is a single value and false of it is an image. */
410 bool is_single_value() const;
411
412 /* It is important to call update_single_value_data after adjusting the single value. See that
413 * method for more information. */
414 GPointer single_value() const;
416
417 /* Gets the single value stored in the result. Assumes the result stores a value of the given
418 * template type. */
419 template<typename T> const T &get_single_value() const;
420
421 /* Gets the single value stored in the result, if the result is not a single value, the default
422 * identity value of the type is returned. Assumes the result stores a value of the same type as
423 * the template type. */
424 template<typename T> T get_single_value_default() const;
425
426 /* Sets the single value of the result to the given value, which also involves setting the single
427 * pixel in the image to that value. See the class description for more information. Assumes
428 * the result stores a value of the given template type. */
429 template<typename T> void set_single_value(const T &value);
430
431 /* Loads the pixel at the given texel coordinates. Assumes the result stores a value of the given
432 * template type. If the CouldBeSingleValue template argument is true and the result is a single
433 * value result, then that single value is returned for all texel coordinates. */
434 template<typename T, bool CouldBeSingleValue = false> T load_pixel(const int2 &texel) const;
435
436 /* Identical to load_pixel but with the specified boundary extensions. */
437 template<typename T, bool CouldBeSingleValue = false>
438 T load_pixel(const int2 &texel,
439 const Extension &extension_mode_x,
440 const Extension &extension_mode_y) const;
441
442 /* Identical to load_pixel but with extended boundary extension. */
443 template<typename T, bool CouldBeSingleValue = false>
444 T load_pixel_extended(const int2 &texel) const;
445
446 /* Identical to load_pixel but with a fallback value for out of bound access. */
447 template<typename T, bool CouldBeSingleValue = false>
448 T load_pixel_fallback(const int2 &texel, const T &fallback) const;
449
450 /* Identical to load_pixel but with zero boundary extension. */
451 template<typename T, bool CouldBeSingleValue = false> T load_pixel_zero(const int2 &texel) const;
452
453 /* Stores the given pixel value in the pixel at the given texel coordinates. Assumes the result
454 * stores a value of the given template type. */
455 template<typename T> void store_pixel(const int2 &texel, const T &pixel_value);
456
457 /* Samples the result at the given normalized coordinates with the given interpolation and
458 * boundary extension. The interpolation is ignored for non float types that do not support
459 * interpolation. The jacobian represents the change of the given coordinates across space, if
460 * provided, the function will do area sampling for the area spanned by the jacobian, but if not
461 * provided, standard point sampling will be done. Assumes the result stores a value of the given
462 * template type. If the CouldBeSingleValue template argument is true and the result is a single
463 * value result, then that single value is returned for all coordinates. */
464 template<typename T, bool CouldBeSingleValue = false>
465 T sample(const float2 &coordinates,
466 const Interpolation &interpolation,
467 const Extension &extension_mode_x,
468 const Extension &extension_mode_y,
469 std::optional<float2x2> jacobian = std::nullopt) const;
470
471 /* Shorthand for sample() with bilinear interpolation and zero boundary extension. */
472 template<typename T, bool CouldBeSingleValue = false>
473 T sample_bilinear_zero(const float2 &coordinates) const;
474
475 /* Shorthand for sample() with bilinear interpolation and extended boundary extension. */
476 template<typename T, bool CouldBeSingleValue = false>
477 T sample_bilinear_extended(const float2 &coordinates) const;
478
479 private:
480 /* Same as get_pixel_index but can be used when the type of the result is not known at compile
481 * time. */
482 int64_t get_pixel_index(const int2 &texel) const;
483};
484
485StringRefNull to_string(const ResultPrecision &precision);
486
487/* -------------------------------------------------------------------- */
488/* Inline Methods.
489 */
490
492{
493 return domain_;
494}
495
497{
498 return domain_;
499}
500
502{
503 BLI_assert(storage_type_ == ResultStorageType::GPUImage);
504 return gpu_texture_;
505}
506
508{
509 BLI_assert(storage_type_ == ResultStorageType::CPUImage);
510 return cpu_data_;
511}
512
514{
515 return sharing_info_;
516}
517
519{
520 return storage_type_ == ResultStorageType::SingleValue;
521}
522
524{
525 BLI_assert(storage_type_ == ResultStorageType::CPUImage);
526 BLI_assert(sharing_info_ && sharing_info_->is_mutable());
527 return GMutableSpan(cpu_data_.type(), const_cast<void *>(cpu_data_.data()), cpu_data_.size());
528}
529
530template<typename T> BLI_INLINE_METHOD const T &Result::get_single_value() const
531{
532 BLI_assert(this->is_allocated());
534
535 return std::get<T>(single_value_);
536}
537
539{
540 if (this->is_single_value()) {
541 return this->get_single_value<T>();
542 }
543 return *static_cast<const T *>(this->get_cpp_type().default_value());
544}
545
546template<typename T> BLI_INLINE_METHOD void Result::set_single_value(const T &value)
547{
548 BLI_assert(this->is_allocated());
550
551 single_value_ = value;
552}
553
554template<typename T, bool CouldBeSingleValue>
556{
557 if constexpr (CouldBeSingleValue) {
558 if (this->is_single_value()) {
559 return this->get_single_value<T>();
560 }
561 }
562 else {
563 BLI_assert(!this->is_single_value());
564 }
565
566 return this->cpu_data().typed<T>()[this->get_pixel_index(texel)];
567}
568
582
583template<typename T, bool CouldBeSingleValue>
585 const Extension &extension_mode_x,
586 const Extension &extension_mode_y) const
587{
588 if constexpr (CouldBeSingleValue) {
589 if (this->is_single_value()) {
590 return this->get_single_value<T>();
591 }
592 }
593 else {
594 BLI_assert(!this->is_single_value());
595 }
596
597 const math::InterpWrapMode wrap_mode_x = map_extension_mode_to_wrap_mode(extension_mode_x);
598 const math::InterpWrapMode wrap_mode_y = map_extension_mode_to_wrap_mode(extension_mode_y);
599 const int x = wrap_coord(texel.x, domain_.data_size.x, wrap_mode_x);
600 const int y = wrap_coord(texel.y, domain_.data_size.y, wrap_mode_y);
601 if (x < 0 || y < 0) {
602 return T{};
603 }
604 return this->load_pixel<T>(int2(x, y));
605}
606
607template<typename T, bool CouldBeSingleValue>
609{
610 if constexpr (CouldBeSingleValue) {
611 if (this->is_single_value()) {
612 return this->get_single_value<T>();
613 }
614 }
615 else {
616 BLI_assert(!this->is_single_value());
617 }
618
619 const int2 clamped_texel = math::clamp(texel, int2(0), domain_.data_size - int2(1));
620 return this->cpu_data().typed<T>()[this->get_pixel_index(clamped_texel)];
621}
622
623template<typename T, bool CouldBeSingleValue>
624BLI_INLINE_METHOD T Result::load_pixel_fallback(const int2 &texel, const T &fallback) const
625{
626 if constexpr (CouldBeSingleValue) {
627 if (this->is_single_value()) {
628 return this->get_single_value<T>();
629 }
630 }
631 else {
632 BLI_assert(!this->is_single_value());
633 }
634
635 if (texel.x < 0 || texel.y < 0 || texel.x >= domain_.data_size.x ||
636 texel.y >= domain_.data_size.y)
637 {
638 return fallback;
639 }
640
641 return this->cpu_data().typed<T>()[this->get_pixel_index(texel)];
642}
643
644template<typename T, bool CouldBeSingleValue>
646{
647 return this->load_pixel_fallback<T, CouldBeSingleValue>(texel, T(0));
648}
649
650template<typename T>
651BLI_INLINE_METHOD void Result::store_pixel(const int2 &texel, const T &pixel_value)
652{
653 this->cpu_data_for_write().typed<T>()[this->get_pixel_index(texel)] = pixel_value;
654}
655
661
662/* Given a result and its extension modes as the userdata argument with the type EWASamplingData,
663 * load the pixel at the given texel coordinates with the given extension modes and write the pixel
664 * to the result argument. */
665static inline void sample_ewa_read_callback(void *userdata, int x, int y, float result[4])
666{
667 const EWASamplingData *sampling_data = static_cast<const EWASamplingData *>(userdata);
668 const Color sampled_result = sampling_data->result.load_pixel<Color>(
669 int2(x, y), sampling_data->extension_mode_x, sampling_data->extension_mode_y);
670 copy_v4_v4(result, sampled_result);
671}
672
673template<typename T, bool CouldBeSingleValue>
675 const Interpolation &interpolation,
676 const Extension &extension_mode_x,
677 const Extension &extension_mode_y,
678 std::optional<float2x2> jacobian) const
679{
680 if constexpr (CouldBeSingleValue) {
681 if (this->is_single_value()) {
682 return this->get_single_value<T>();
683 }
684 }
685
686 const int2 size = domain_.data_size;
687 const float2 texel_coordinates = coordinates * float2(size);
688
689 const math::InterpWrapMode wrap_mode_x = map_extension_mode_to_wrap_mode(extension_mode_x);
690 const math::InterpWrapMode wrap_mode_y = map_extension_mode_to_wrap_mode(extension_mode_y);
692 T pixel_value;
693 const float *buffer = static_cast<const float *>(this->cpu_data().data());
694 float *output = nullptr;
695 if constexpr (std::is_same_v<T, float>) {
696 output = &pixel_value;
697 }
698 else {
699 output = pixel_value;
700 }
701
702 switch (interpolation) {
705 output,
706 size.x,
707 size.y,
708 sizeof(T) / sizeof(float),
709 texel_coordinates.x,
710 texel_coordinates.y,
711 wrap_mode_x,
712 wrap_mode_y);
713 break;
716 output,
717 size.x,
718 size.y,
719 sizeof(T) / sizeof(float),
720 texel_coordinates.x - 0.5f,
721 texel_coordinates.y - 0.5f,
722 wrap_mode_x,
723 wrap_mode_y);
724 break;
727 output,
728 size.x,
729 size.y,
730 sizeof(T) / sizeof(float),
731 texel_coordinates.x - 0.5f,
732 texel_coordinates.y - 0.5f,
733 wrap_mode_x,
734 wrap_mode_y);
735 break;
738 if (jacobian.has_value()) {
739 EWASamplingData sampling_data = EWASamplingData{
740 *this, extension_mode_x, extension_mode_y};
741 BLI_ewa_filter(size.x,
742 size.y,
743 false,
744 true,
745 coordinates,
746 jacobian.value()[0],
747 jacobian.value()[1],
749 &sampling_data,
750 output,
751 extension_mode_x == Extension::Clip &&
752 extension_mode_y == Extension::Clip);
753 }
754 else {
756 output,
757 size.x,
758 size.y,
759 sizeof(T) / sizeof(float),
760 texel_coordinates.x - 0.5f,
761 texel_coordinates.y - 0.5f,
762 wrap_mode_x,
763 wrap_mode_y);
764 }
765 break;
766 }
767
768 return pixel_value;
769 }
770 else {
771 const int wrapped_x = wrap_coord(texel_coordinates.x, size.x, wrap_mode_x);
772 const int wrapped_y = wrap_coord(texel_coordinates.y, size.y, wrap_mode_y);
773 /* The wrap_coord function returns -1 if a zero should be returned. */
774 if (wrapped_x < 0 || wrapped_y < 0) {
775 return T{};
776 }
777 return this->load_pixel<T>(int2(wrapped_x, wrapped_y));
778 }
779}
780
781template<typename T, bool CouldBeSingleValue>
787
788template<typename T, bool CouldBeSingleValue>
794
795BLI_INLINE_METHOD int64_t Result::get_pixel_index(const int2 &texel) const
796{
798 BLI_assert(this->is_allocated());
799 BLI_assert(texel.x >= 0 && texel.y >= 0 && texel.x < domain_.data_size.x &&
800 texel.y < domain_.data_size.y);
801 return int64_t(texel.y) * domain_.data_size.x + texel.x;
802}
803
804} // namespace blender::compositor
#define BLI_assert_unreachable()
#define BLI_INLINE_METHOD
#define BLI_INLINE
#define output
const void * default_value() const
MutableSpan< T > typed() const
Span< T > typed() const
const void * data() const
static Domain identity()
Definition domain.cc:47
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
T sample(const float2 &coordinates, const Interpolation &interpolation, const Extension &extension_mode_x, const Extension &extension_mode_y, std::optional< float2x2 > jacobian=std::nullopt) const
T sample_bilinear_extended(const float2 &coordinates) const
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
T load_pixel_zero(const int2 &texel) const
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
void set_single_value(const T &value)
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
T sample_bilinear_zero(const float2 &coordinates) const
T load_pixel_extended(const int2 &texel) const
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
T load_pixel_fallback(const int2 &texel, const T &fallback) const
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
T load_pixel(const int2 &texel, const Extension &extension_mode_x, const Extension &extension_mode_y) const
void set_type(ResultType type)
Definition result.cc:1147
TEX_TEMPLATE DataVec texture(T, FltCoord, float=0.0f) RET
#define T
StringRefNull to_string(const Interpolation &interpolation)
Definition domain.cc:112
static void sample_ewa_read_callback(void *userdata, int x, int y, float result[4])
ColorSceneLinear4f< eAlpha::Premultiplied > Color
Definition COM_result.hh:96
ImplicitSharingPtr< Closure > ClosurePtr
BLI_INLINE math::InterpWrapMode map_extension_mode_to_wrap_mode(const Extension &mode)
QuaternionBase< float > Quaternion
T clamp(const T &a, const T &min, const T &max)
BLI_INLINE void interpolate_bilinear_wrapmode_fl(const float *buffer, float *output, int width, int height, int components, float u, float v, InterpWrapMode wrap_x, InterpWrapMode wrap_y)
BLI_INLINE void interpolate_nearest_wrapmode_fl(const float *buffer, float *output, int width, int height, int components, float u, float v, InterpWrapMode wrap_u, InterpWrapMode wrap_v)
void interpolate_cubic_bspline_wrapmode_fl(const float *buffer, float *output, int width, int height, int components, float u, float v, InterpWrapMode wrap_u, InterpWrapMode wrap_v)
ImplicitSharingPtr< Bundle > BundlePtr
BMesh Iterators.
BLI_assert(slot->slot_subtype.map==BMO_OP_SLOT_SUBTYPE_MAP_FLT)
void BLI_ewa_filter(int width, int height, bool intpol, bool use_alpha, const float uv[2], const float du[2], const float dv[2], ewa_filter_read_pixel_cb read_pixel_cb, void *userdata, float result[4], bool clip=true)
__int64 int64_t
Definition BKE_text.h:90
constexpr bool is_same_any_v
BMesh const char void * data
unsigned char uint8_t
Definition BKE_text.h:79
signed int int32_t
Definition BKE_text.h:78
MINLINE void copy_v4_v4(float r[4], const float a[4])
@ String
Definition types.hh:154
KERNEL_FEATURE_LIGHT_PASSES KERNEL_FEATURE_PATH_GUIDING float