Commit Graph

2352 Commits

Author SHA1 Message Date
Omar Emara
781dea8442 Cleanup: Compositor: Use default case for blur types
Use a default case for blur type switch case, that's because adding all
types is not necessary, and I am trying to differentiate between switch
cases that need full type enumeration and those that do not.
2025-03-18 16:15:27 +02:00
Omar Emara
0a57002469 Cleanup: Compositor: Remove redundant SMAA types
This patch removes code that handles types other than Float and Color,
that's because SMAA is practically only used for those types only.
2025-03-18 15:51:51 +02:00
Omar Emara
383a6ce654 Refactor: Compositor: Formalize single value data update
This patch formalizes the process of data updates of single values.
Previously, this was done internally in the set_single_value method, but
there was a hack in multi-function procedure code to do the same. This
patch adds a new method for single value data updates and uses it
internally in set_single_value.
2025-03-18 15:03:55 +02:00
Omar Emara
ebaa12af6c Refactor: Compositor: Use data sharing for SMAA
Use data sharing instead of copying in the SMAA algorithm. This reduces
redundant copies and code duplication.
2025-03-18 12:38:48 +02:00
Omar Emara
a345c4597e Refactor: Compositor: Use GPU setter nodes for unlinked sockets
This patch refactors how values of unlinked sockets are provided to
nodes. Previously, the GPU node stack values were initialized at
construction time and linked by the node's compile methods. This meant
that we needed to handle implicit conversion if the socket is linked to
an unlinked node group input.

Alternatively, we now insert a GPU setter node for each unlinked socket
that carries the value and type of the origin socket, and let the GPU
code generator do implicit conversion at the shader level. This has
three advantages:

- It makes it easier to add new types since we no longer have to handle
  those types in shader node code and it reduces code duplication.
- It makes the code more inline with how we implement multi-function
  procedures. So refactoring is easier.
- It opens the door to implement things like implicit inputs, which will
  be needed later for things like texture nodes.
2025-03-18 11:23:43 +02:00
Omar Emara
2e1dd5efee Fix: Crash when canceling compositor job
Blender crashes when canceling a compositor job if a transform node is
used. This is because freeing shared data didn't reset data members, so
it still thinks it has allocated data, which will be double freed
causing crashes. To fix this, we simply clear data members if data is
still shared.
2025-03-17 13:36:32 +02:00
Clément Foucault
626118984e Compositor: Use last_update for ID update
This replaces the deprecated DrawData mechanism by the
usage of the update timestamp `last_update`.

The compositor keeps the `last_update` value of the cached ID
and compares it with the value on the ID at the time of evaluation.

Rel #134690

Pull Request: https://projects.blender.org/blender/blender/pulls/134878
2025-03-14 10:25:32 +01:00
Omar Emara
b978f51051 Refactor: Compositor: Use BKE conversion for single values
This patch refactors the conversion code for single values to use BKE
conversion rules. This reduces boilerplate and makes it easier to add
new types.
2025-03-14 10:37:05 +02:00
Omar Emara
e9af802330 Refactor: Compositor: Use generic pointer with MF operation
This patch refactors how single values are passed to MF operations by
utilizing generic pointers. This avoids boilerplate and makes it easier
to add new types.
2025-03-13 15:47:46 +02:00
Omar Emara
9bf05f4def Refactor: Compositor: Provide const variants for data accessors
This patch provides const variants to the cpu_data accessors that
returns GSpan. This is done to better const correctness. Some code need
non-const data even for read-only data, so we have to const cast those
for the moment.
2025-03-13 14:49:01 +02:00
Omar Emara
9f254ef08c Refactor: Compositor: Use BKE conversion for images
This patch refactors the conversion code for images to use BKE
conversion rules. This reduces boilerplate and makes it easier to add
new types.
2025-03-13 12:17:35 +02:00
Omar Emara
8f2f0520da Cleanup: Unused declaration
Removes a now unused declaration from the previous commit.
2025-03-13 11:49:46 +02:00
Omar Emara
cbd01d2a6f Refactor: Compositor: Reduce boilerplate in conversion code
This patch refactors GPU implicit conversion code by generating the name
of the shaders on the fly using fmt. This reduces boilerplate and makes
it easier to add new types.
2025-03-13 11:38:12 +02:00
Omar Emara
b37afc86d1 Refactor: Compositor: Replace proxy results with data sharing
This patch refactors the result class to replace proxy results with the
possibility of doing data sharing through a shared heap allocated data
reference count. This is more robust and simpler since proxy results no
longer need to be handled as a special case in a lot of the results
code. Additionally, it allows stronger const correctness since inputs to
operations can now be const.

This is somewhat similar to implicit sharing used in other parts of
Blender, so we can look into using that in the future.

Pull Request: https://projects.blender.org/blender/blender/pulls/135778
2025-03-12 16:42:26 +01:00
Omar Emara
39d0cff1dc Refactor: Compositor: Remove initial reference count
This patch removes the initial reference count mechanism from the Result
class. That's because results are no longer cached across evaluations
ever since 97ada4f307, so it is no longer useful.

Pull Request: https://projects.blender.org/blender/blender/pulls/135526
2025-03-06 08:50:22 +01:00
Bastien Montagne
dd168a35c5 Refactor: Replace MEM_cnew with a type-aware template version of MEM_callocN.
The general idea is to keep the 'old', C-style MEM_callocN signature, and slowly
replace most of its usages with the new, C++-style type-safer template version.

* `MEM_cnew<T>` allocation version is renamed to `MEM_callocN<T>`.
* `MEM_cnew_array<T>` allocation version is renamed to `MEM_calloc_arrayN<T>`.
* `MEM_cnew<T>` duplicate version is renamed to `MEM_dupallocN<T>`.

Similar templates type-safe version of `MEM_mallocN` will be added soon
as well.

Following discussions in !134452.

NOTE: For now static type checking in `MEM_callocN` and related are slightly
different for Windows MSVC. This compiler seems to consider structs using the
`DNA_DEFINE_CXX_METHODS` macro as non-trivial (likely because their default
copy constructors are deleted). So using checks on trivially
constructible/destructible instead on this compiler/system.

Pull Request: https://projects.blender.org/blender/blender/pulls/134771
2025-03-05 16:35:09 +01:00
Omar Emara
97ada4f307 Cleanup: Compositor: Remove operation stream cache
This patch removes the operation stream cache code from the compositor.
This code path has been disabled already since 4137fdf555, so this patch
just removes the dead code.

Pull Request: https://projects.blender.org/blender/blender/pulls/135513
2025-03-05 14:01:12 +01:00
Omar Emara
117b47840a Refactor: Compositor: Remove use of auto_resource_location
This patch removes the use of auto_resource_location during shader
operation material compilation. Instead, we assign slot locations
explicitly. Output images are just assigned incremental indices, while
input samplers are also assigned incremental indices, but starting from
the number of textures in the material, because color bands might be
reserving slots already.

Pull Request: https://projects.blender.org/blender/blender/pulls/135453
2025-03-05 09:12:52 +01:00
Omar Emara
b03e9be22d Refactor: Compositor: Assert for unneeded allocations
This patch refactors how the compositor deals with outputs that are
allocated but not needed. Previously we allowed such allocations and
implicitly release them right after operations are computed. This is a
weak design, so we now require developers to only allocate outputs that
are actually needed and assert otherwise. The release mechanism is
therefore removed.
2025-03-04 15:05:41 +02:00
Omar Emara
c55594f0a1 Fix: Z Combine asserts when one output is unused
The Z Combine node asserts if one of its outputs are unused. That's
because we compete both outputs even if they are not needed. To fix
this, we skip outputs that are not needed by splitting the shaders to
compute each output independently.
2025-03-04 14:05:55 +02:00
Omar Emara
b06ed9d406 Compositor: Track update count for cache invalidation
This patch refactors static cache invalidation of images by tracking an
update count. Images now store a runtime update count that is updated
every time the image is tagged for update. Cached images store a copy of
the update count at the moment they were cached, and are invalidated if
if it changed.

Compared to #134878, this is simpler and more robust, since update IDs
are isolated to images only and not to the DEG update count. Though this
only supports images specifically because they are not covered by the
copy-on-evaluation system, which means #134878 will cause multiple
depsgraph to fight over images.

Pull Request: https://projects.blender.org/blender/blender/pulls/134905
2025-03-04 12:04:38 +01:00
Omar Emara
2284b7538a Fix: Keying asserts when Edges output is unused
The Keying node asserts if its Edges output is unused. That's because we
compete the outputs even if it not needed. To fix this, we skip that
output if not needed.
2025-03-04 12:43:08 +02:00
Omar Emara
7356529895 Cleanup: Remove single value reduction operation
The ReduceToSingleValueOperation has been dead code since 2b2b27abf5, so
remove it.

Pull Request: https://projects.blender.org/blender/blender/pulls/135427
2025-03-04 10:44:16 +01:00
Aras Pranckevicius
cc2c6692c0 Cleanup: Name more IMB things as "byte" or "float" instead of "rect" and "rectFloat"
- IB_rect -> IB_byte_data
- IB_rectfloat -> IB_float_data
- Rename some functions:
	- IMB_get_rect_len -> IMB_get_pixel_count
	- IMB_rect_from_float -> IMB_byte_from_float
	- IMB_float_from_rect_ex -> IMB_float_from_byte_ex
	- IMB_float_from_rect -> IMB_float_from_byte
	- imb_addrectImBuf -> IMB_alloc_byte_pixels
	- imb_freerectImBuf -> IMB_free_byte_pixels
	- imb_addrectfloatImBuf -> IMB_alloc_float_pixels
	- imb_freerectfloatImBuf -> IMB_free_float_pixels
	- imb_freemipmapImBuf -> IMB_free_mipmaps
	- imb_freerectImbuf_all -> IMB_free_all_data
- Remove IB_multiview (not used at all)
- Remove obsolete "module" comments in public IMB headers

Pull Request: https://projects.blender.org/blender/blender/pulls/135348
2025-03-03 17:11:45 +01:00
Hans Goudey
9cade06f5f Refactor: Nodes: Use Map instead of GHash for compositor previews
Replace `bNodeInstanceHash` with a `Map`. Move it to the node tree
runtime data. Simplify some code by removing the tag from the hash
value and collecting unused previews directly. Then just remove a
bunch of code that's now unused.

Note that texture node previews haven't been working for a while
anyway, and the experimental shader node previews seem to use
a different system (this one is a remnant of Blender Internal).

Pull Request: https://projects.blender.org/blender/blender/pulls/135310
2025-03-03 14:04:20 +01:00
Omar Emara
2833c0f283 Fix: Compositor asserts for unsupported unavailable sockets
The compositor asserts if an unsupported unavailable socket exists. This
assert should not exist, because the GPU material compiler will itself
gracefully handle such sockets when their type is GPU_NONE, which is
already the case. So remove the assert and add a note about the
behavior.
2025-02-27 14:34:23 +02:00
Omar Emara
20897f5f20 Cleanup: Warning about integers of different signs
The SAT shaders in the compositor contain comparisons of different
signed integers, use uint to fix this.
2025-02-27 11:19:24 +02:00
Omar Emara
e224e437da Cleanup: Compositor: Remove common_math_utils includes
This patches removes common_math_utils includes from compositor shaders
and replaces them with math lib includes. This involves moving some
functions from that file to to the math lib files.

Pull Request: https://projects.blender.org/blender/blender/pulls/135157
2025-02-26 10:47:39 +01:00
Omar Emara
16d59b00a7 Refactor: Compositor: Use int32_t as result type
This patches uses an explicit int32_t fixed width integer instead of int
for result types. For consistency with other node systems.
2025-02-25 09:25:48 +02:00
Omar Emara
27bf89e933 Refactor: Compositor: Use BKE conversions for MF operation
This patch uses the BKE implicit conversion rules to implement implicit
conversion in multi-function procedure operations in the compositor.
Since conversions use ColorSceneLinear4f to represent colors instead of
float4, we need to insert extra implicit conversions around color
sockets.

Special attention was given to variable destruction in the
implementation because it is now possible for implicit variables to be
outputs, so we need to make sure they are not destructed.
2025-02-24 17:20:16 +02:00
Omar Emara
c29971c688 Fix #135010: Crash when using Denoise node
Blender crashes when using the Denoise node. That's because the code
assumes normal input would have 4-channels, while this may not be the
case. To fix this, use the channels count from the result or the GPU
texture directly.
2025-02-24 13:22:40 +02:00
Omar Emara
7ab7e1efa6 Fix #134939: Passes are incorrectly interpreted as vectors
Some passes are now interpreted as vectors by the compositor Image node.
This is because it assumes 3-channel passes are always vector, but this
is not the case for passes that are RGB without an alpha channel. To fix
this, we also consider channel IDs to disambiguate the type of the pass.
2025-02-24 11:17:36 +02:00
Omar Emara
367b6d2b55 Cleanup: Unused warning in release builds 2025-02-21 13:05:47 +02:00
Omar Emara
31aa6d9aee Cleanup: Compositor: Deduplicate CPPType getters 2025-02-20 16:01:51 +02:00
Omar Emara
5e8f96277d Compositor: Reduce vector type components to 3
Previously, the vector type in the compositor had 4-components to
accommodated float4 types, while the last component was ignored for the
rest of the vector types. But now that we have a dedicated type for
float4 in #134486. We can reduce that vector type to 3-components.

Pull Request: https://projects.blender.org/blender/blender/pulls/134570
2025-02-20 13:23:54 +01:00
Omar Emara
a5ecde48ae Compositor: Add Float4 type
The compositor previously overloaded the vector type to represent
multiple dimensions that are always stored in a 4D float vector. This
patch introduce a dedicated type for float4, leaving the vector type to
always represent a 3D vector, which will be done in a later commit.

This is not exposed to the user as a separate socket type with a
different color, it is only an internal type that uses the same vector
socket shape and color.

Since the vector socket represents both 4D and 3D vectors, code
generally assumes that such sockets represents 3D vectors, and the
developer is expected to set it to a 4D vector if needed in the node
operation constructor, or use the newly added skip_type_conversion flag
for nodes that do not care about types, like the File Output node.
Though this should be redundant once we add a dimension property for
vector sockets.

Pull Request: https://projects.blender.org/blender/blender/pulls/134486
2025-02-20 10:38:40 +01:00
Omar Emara
6946108678 Merge branch 'blender-v4.4-release' 2025-02-19 15:07:23 +02:00
Omar Emara
d5c662a305 Fix #134436: Compositor is much slower on Windows
The new CPU compositor in v4.4 is much slower than the old CPU
compositor in v4.3 on Windows. This is because MSVC does not inline many
of the core methods in the Result class of the compositor. To fix this,
we force inline those methods, adding a new macro for inlining methods
in the process, since the existing macro has the static keyword, which
only works for functions, not methods.

Pull Request: https://projects.blender.org/blender/blender/pulls/134748
2025-02-19 13:56:06 +01:00
Omar Emara
8dbd607df5 Merge branch 'blender-v4.4-release' 2025-02-17 16:57:36 +02:00
Omar Emara
003fb98f9d Fix #134567: Compositor memory leak in some setups
The compositor leaks memory in certain setups where a transformed result
is linked to two inputs in the same pixel node. This happens due to an
overestimation in the computation of reference counts of those results.
Since pixel operations might share the same input for multiple links in
the node tree, the reference count should be corrected to take that
sharing into account.

This sharing was previously accounted for as part of releasing inputs in
the pixel operation, but input processors didn't take that into account,
so the realize on domain input processor would leak memory. So to fix
this, we correct the reference count at the evaluator level instead,
such that input processors can safely operate on the correct reference
count.

Pull Request: https://projects.blender.org/blender/blender/pulls/134666
2025-02-17 15:55:22 +01:00
Hans Goudey
4727dfd627 Cleanup: Remove unnecessary const qualifiers in function declarations 2025-02-14 14:32:57 -05:00
Omar Emara
89e0472e49 Compositor: Use gpu::TexturePool instead of DRW pool
This patch removes the compositor texture pool implementation which
relies on the DRW texture pool, and replaces it with the new texture
pool implementation from the GPU module.

Since the GPU module texture pool does not rely on the global DST, we
can use it for both the viewport compositor engine and the GPU
compositor, so the virtual texture pool implementation is removed and
the GPU texture pool is used directly.

The viewport compositor engine does not need to reset the pool because
that is done by the draw manager. But the GPU compositor needs to reset
the pool every evaluation. The pool is deleted directly after rendering
using the render pipeline or through RE_FreeUnusedGPUResources for the
interactive compositor.

Pull Request: https://projects.blender.org/blender/blender/pulls/134437
2025-02-12 15:59:45 +01:00
Omar Emara
df69e6ab50 Merge branch 'blender-v4.4-release' 2025-02-12 11:54:29 +02:00
Omar Emara
25c4dd53dd Fix: Keying Screen and anti-aliasing produce bad results
The Keying Screen and the Anti-Aliasing nodes produce bad results in the
viewport compositor. This is because their cached resources are
allocated from the GPU texture pool, so they might get overwritten.

To fix this, we make sure those are not allocated from the texture pool.
2025-02-12 11:50:44 +02:00
Omar Emara
51789af3cb Cleanup: Compositor: Remove is_int_type method
The method is only used in one place and will be redundant in a future
change.
2025-02-11 14:29:52 +02:00
Clément Foucault
a961c9050d Cleanup: GPU: Remove dependency on legacy common_math_lib.glsl
Replace usage of `common_math_lib.glsl` (deprecated) with gpu shader libs.

Pull Request: https://projects.blender.org/blender/blender/pulls/131579
2025-02-10 18:14:50 +01:00
Omar Emara
a8141f47cf Merge branch 'blender-v4.4-release' 2025-02-10 13:59:08 +02:00
Omar Emara
d006924ebc Fix: Blur is two times slower in 4.4
The symmetric separate blur operation in the compositor is two times
slower in 4.4 compared to 4.3. On Linux, this only happens when Blender
is compiled with GCC, because Clang inlines a small function that GCC
doesn't.

To fix this, we specialize an if statement using templates to help GCC
inline the function. This results in a 3.5 times faster execution.

Pull Request: https://projects.blender.org/blender/blender/pulls/134336
2025-02-10 12:58:02 +01:00
Omar Emara
bd379e1892 Refactor: Compositor: Use gpu_fn for ShaderNode class
This patch refactors the ShaderNode class to be a concrete class that
is implemented in terms of the node type gpu_fn. This is done to make it
easier to reuse existing nodes in other parts of Blender.

Pull Request: https://projects.blender.org/blender/blender/pulls/134210
2025-02-10 11:51:57 +01:00
Omar Emara
31444fe5b1 Refactor: Compositor: Use generic container for results
This patch refactors the Result class in the compositor to use
GMutableSpan and std::variant to wrap the result's data. This reduces
the complexity of the code and slightly optimizes performance. This will
also make it easier to add new types and interface with other code like
multi-function procedures.

Pull Request: https://projects.blender.org/blender/blender/pulls/134112
2025-02-06 13:49:44 +01:00