Commit Graph

1321 Commits

Author SHA1 Message Date
Clément Foucault
8257fcb62f Fix #139354: Metal: AgX turns rendered image into black white
Caused by the initializer list syntax. Using the constructor
explicitly fixes the issue.
2025-05-23 18:28:05 +02:00
Clément Foucault
781b80c017 GPU: Python: Add missing diagonal constructors for square matrices
This was missing from #139185
2025-05-22 11:51:20 +02:00
Clément Foucault
16aee8efff Fix: #138928: Metal: Custom pyGPU shader has a compilation error in 4.4
This was caused by the removal of some compatibility code
in the metal backend.

This patch reintroduce this compatibility code in a cleaner
way than before.

This should be followed up by a documentation commit that
explains what is supported and what is not.

Pull Request: https://projects.blender.org/blender/blender/pulls/139185
2025-05-22 10:36:04 +02:00
Hans Goudey
91627b3d47 GPU: Remove int float fetch mode combination
This commit finishes removing the uses of the integer to float
vertex buffer fetch mode. Previous commits noted below already started
that process. The last usage was geometry attributes. Now integers are
converted to floats as part of the existing upload process.

The change makes the Vulkan vertex buffer type conversion unused, so
it's removed. That's nice because Vulkan vertex buffers go from 1040 to
568 bytes in size and have significantly less overhead on creation.

Related:
- 153abc372e
- 1e1ac2bb9b
- 617858e453

Pull Request: https://projects.blender.org/blender/blender/pulls/138873
2025-05-15 15:29:12 +02:00
Hans Goudey
1e1ac2bb9b Fix: Various asserts when creating GPU vertex formats
Caused by 617858e453.

These formats should use types aligned to 4 bytes. That's generally
required by modern GPUs. Uploading with these types also avoids
automatic conversion by the Vulkan backend which is something
we're hoping to remove fully.

In the end this PR removes a bunch of code related to supporting
the older single-byte formats.

Pull Request: https://projects.blender.org/blender/blender/pulls/138836
2025-05-13 19:07:01 +02:00
Clément Foucault
a68ad1b676 Fix: GPU: C++ GLSL stubs: Swizzle in wrong list 2025-05-08 20:55:24 +02:00
Alexandre-Cardaillac
921c2b9d61 Shader: New Volume Coefficients Shader
Add a new shader node to control volume coefficients (scattering,
absorption and emission) directly, making it easier to model existing
volumes with measured data.

Pull Request: https://projects.blender.org/blender/blender/pulls/136287
2025-05-08 19:19:35 +02:00
Clément Foucault
f6a6358836 EEVEE: Use enum instead of defines for ClosureType
This reduces the amount of macros and avoid `using` in shader
code (see #137445).
Also make sure that all switches have cases for all the closures.

Pull Request: https://projects.blender.org/blender/blender/pulls/138502
2025-05-06 18:34:46 +02:00
Clément Foucault
41ed07d55e GPU: Shader: Add support for basic template support through preprocessor
Allows basic usage of templated functions.
There is no support for templated struct.

Benefit:
- More readable than macros in shader sources.
- Compatible with C++ tools.
- More sharing possible with host C++ code.

Requirements/Limitations:
- No default arguments to template parameters.
- Must use explicit instantiation for all variant needed.
- Explicit instantiation needs to **not** use argument deduction.
- Calls to template needs to have all template argument explicit
  or all implicit.
- Template overload is not supported (redefining the same template
  with different template argument or function argument types).

Currently implemented as Macros inside the build-time pre-pocessor,
but that could change to copy-paste to allow better error reporting.
However, the Macros keep the shader code reduced in the final binary
and allow different file to declare different instantiation.

The implementation is done by declaring overloads for each explicit
instantiation.

If a template has arguments not present in function
arguments, then all arguments **values** are appended to the
function name. The explicit template callsite is then modified to use
`TEMPLATE_GLUE` which will call the correct function. This is
why template argument deduction is not supported in this case.

Rel #137446

Pull Request: https://projects.blender.org/blender/blender/pulls/137441
2025-05-06 10:41:25 +02:00
Clément Foucault
74e6d2c575 GPU: Shader: Add support for basic loop unrolling through preprocessor
This adds basic unrolling support for 2 syntax:
- `[[gpu::unroll]]` which does full loop unrolling
- `[[gpu::unroll(x)]]` which unrolls `x` iteration

Nesting is supported.

This change is motivated by the added cost in compilation
and execution time that some loops have even if they have
compile time defined iteration counts.

The syntax is inspired by `GL_EXT_control_flow_attributes`.
However, we might want to have our own prefix to show it is
a blender specific feature and that it differs from the standard.
I propose `[[gpu::unroll]]`.

In the future, we could extend this to support more directives that
can be expanded to backend specific extension / syntax. This would
avoid readability issue an error prone copy paste of large amount
of preprocessor directives.

Currently, given that GL's GLSL flavor doesn't support
any of these attributes, the preprocessor does some copy-pasting
that does the unrolling at the source level. Note that the added
`#line` allow for correct error logging.

For the `[[gpu::unroll]]` syntax, the `for` declaration
needs to follow a specific syntax to deduce the number
of loop iteration.
This variant removes the continue condition between iteration,
so all iterations are evaluated. This could be modified
using a special keyword.

For the `[[gpu::unroll(n)]]` syntax, the usercode needs
to make sure that `n` is large enough to cover all iterations
as the loop is completely removed.
We could add shader `assert` to make sure that there is
never a remaining iteration.
This behavior is usually different from what you see in other
implementation as we do not keep a loop at all. Usually, compilers
still keep the loop if it is not unrolled fully. But given we don't
have IR, this is the best we can do.

`break` and `continue` statement are forbidden at the unrolled loop
scope level. Nested loop and switch can contain these keywords.
This is accounted for by checks in the pre-processor.

Only `for` loops are supported for now. There are no real
incentive to add support for `while` given how rare it is
in the shader codebase.

Rel #137446

Pull Request: https://projects.blender.org/blender/blender/pulls/137444
2025-05-05 13:37:51 +02:00
Clément Foucault
8dee08996e GPU: Shader: Add wrapper to stage agnostic function
This avoid having to guards functions that are
only available in fragment shader stage.

Calling the function inside another stage is still
invalid and will yield a compile error on Metal.

The vulkan and opengl glsl patch need to be modified
per stage to allow the fragment specific function
to be defined.

This is not yet widely used, but a good example is
the change in `film_display_depth_amend`.

Rel #137261

Pull Request: https://projects.blender.org/blender/blender/pulls/138280
2025-05-05 09:59:00 +02:00
Clément Foucault
c0f191f580 GPU: Make polylines vertex shader use C++ shader compilation
_No response_

Pull Request: https://projects.blender.org/blender/blender/pulls/138411
2025-05-05 00:44:34 +02:00
Clément Foucault
0dba80c89c GPU: Add reserved GLSL keywords to C++ stubs
This avoid using them in our shader codebase.
2025-04-30 12:47:00 +02:00
Campbell Barton
c90e8bae0b Cleanup: spelling in comments & replace some use of single quotes
Previously spell checker ignored text in single quotes however this
meant incorrect spelling was ignored in text where it shouldn't have
been.

In cases single quotes were used for literal strings
(such as variables, code & compiler flags),
replace these with back-ticks.

In cases they were used for UI labels,
replace these with double quotes.

In cases they were used to reference symbols,
replace them with doxygens symbol link syntax (leading hash).

Apply some spelling corrections & tweaks (for check_spelling_* targets).
2025-04-26 11:17:13 +00:00
Clément Foucault
59df50c326 GPU: Refactor Qualifier and ImageType
This allow to use types closer to GLSL in resource
declaration.

These are aliased for clarity in the GPU
module (i.e. `isampler2D` is shortened to `Int2D`).

Rel #137446

Pull Request: https://projects.blender.org/blender/blender/pulls/137954
2025-04-24 14:38:13 +02:00
Clément Foucault
f2025f28e7 DRW: Move fullscreen vertex shader to gpu common
This makes no sense to have in the draw namespace.

Also take the opportunity for making the coordinates
a float2 and rename them to something more descriptive.
2025-04-16 20:09:28 +02:00
Clément Foucault
6048a7a765 GPU: Preprocessor: Allow function argument to be references
Pull Request: https://projects.blender.org/blender/blender/pulls/137612
2025-04-16 19:42:27 +02:00
Clément Foucault
47d2dffe8c GPU: Shader CodeBase use constexpr instead of const
Do this only when applicable.

This allow better compile time checking in Shader C++ compilation.
Moreover, this allows to have `constexpr` in shared code between
C++ and GLSL.

After investigation the `const` keyword in GLSL has the same
semantic than C/C++.

Rel #137333 and #137446

Pull Request: https://projects.blender.org/blender/blender/pulls/137497
2025-04-15 11:36:53 +02:00
Clément Foucault
3b3a5731df GPU: Shader: Change vector and matrix type to use blender convention
This unify the C++ and GLSL codebase style.

The GLSL types are still in the backend compatibility
layers to support python shaders. However, the C++
shader compilation layer doesn't have them to enforce
correct type usage.

Note that this is going to break pretty much all PRs
in flight that targets shader code.

Rel #137261

Pull Request: https://projects.blender.org/blender/blender/pulls/137369
2025-04-14 13:46:41 +02:00
Clément Foucault
9990273d04 GPU: Change Type enum to use lower case values
This is to help for future resource declaration
using macros.

Rel #137261

Pull Request: https://projects.blender.org/blender/blender/pulls/137367
2025-04-11 22:39:01 +02:00
Clément Foucault
bb52754652 GPU: Use f suffix for float literals
They are actually already some literals with the `f` suffix
that are in our shader codebase and we never had problem in
the past 5 years (or even 8 years).

So I think it is safe to do and improves convergence of codestyles.

Pull Request: https://projects.blender.org/blender/blender/pulls/137352
2025-04-11 18:28:45 +02:00
Omar Emara
0b67bbc16a Fix #136812: Glitches in White Noise node in compositor
The White Noise node in the compositor glitches. That's because the
alpha channel in GPU code is uninitialized. So we simply initialize it
to 1.
2025-04-03 19:09:58 +02:00
Clément Foucault
3064906eb7 Cleanup: GPU: Add ATTR_FALLTHROUGH for shader C++ compilation 2025-04-03 16:38:21 +02:00
Alaska
2e829ca4cf Fix #136303: Normalize the normals on the Ambient Occlusion node
This commit simply normalizes the normals of the Ambient occlusion
node before computing the output to avoid odd behaviour with
unnormalized normals.

Pull Request: https://projects.blender.org/blender/blender/pulls/136315
2025-03-27 02:58:19 +01:00
Brecht Van Lommel
a7026e817c Fix: EEVEE bump node issue after filter width addition
Ref #136465

Pull Request: https://projects.blender.org/blender/blender/pulls/136519
2025-03-25 21:03:44 +01:00
Brecht Van Lommel
f987ef7b6e Shaders: Add Filter Width input to Bump node
This makes it possible to restore previous Blender 4.3 behavior of bump
mapping, where the large filter width was sometimes (ab)used to get a bevel
like effect on stepwise textures.

For bump from the displacement socket, filter width remains fixed at 0.1.

Ref #133991, #135841

Pull Request: https://projects.blender.org/blender/blender/pulls/136465
2025-03-25 16:29:13 +01:00
Clément Foucault
5241133425 UI: Make area border width decoupled from scale factor
This is just the shader change.
It allows more freedom for the UI team to tweak the appearance.

The is not functional changes in this patch.

Rel #126334
2025-03-17 13:12:25 +01:00
Miguel Pozo
083878c46b Merge branch 'blender-v4.4-release' 2025-03-13 16:14:24 +01:00
Jesse Yurkovich
e492794e1e Fix: Resolve several int -> uint conversion shader warnings
Resolves several int -> uint conversion warnings. Warnings like the
following will be printed otherwise:

```
      |
  225 |   uint shadow_type = flags & 0xF;
      |                      ^
      | gpu_shader_text_vert.glsl:17:22: Warning: some implementations
        may not support implicit int -> uint conversions for `&'
        operators; consider casting explicitly for portability
```

Pull Request: https://projects.blender.org/blender/blender/pulls/135890
2025-03-13 14:22:21 +01:00
Campbell Barton
6ef7dae8ef Cleanup: spelling in comments (make check_spelling_*) 2025-03-13 13:41:17 +11:00
Omar Emara
8ade574b9f Shading: Retain alpha in Mix Color shader code
The Mix Color shader node does not retain the alpha channel of the first
input in both the Linear Light and Soft Light modes, while it is retain
for other modes. Further, result clamping also ignores the alpha due to
using the vector clamp function, which introduces implicit conversion
that removes the alpha.

This does not matter for EEVEE because it does nothing with the alpha
channel. But the code will now be shared with the compositor, which does
care about the alpha channel. So adjust the code accordingly to retain
the alpha in those cases.

Pull Request: https://projects.blender.org/blender/blender/pulls/135632
2025-03-10 09:36:44 +01:00
Clément Foucault
bb2d123fbd Cleanup: DRW: Remove ObjectInfos legacy GLSL macros
_No response_

Pull Request: https://projects.blender.org/blender/blender/pulls/135542
2025-03-06 11:06:26 +01:00
Clément Foucault
077115e936 Cleanup: DRW: Remove ViewMatrix legacy GLSL macros
Pull Request: https://projects.blender.org/blender/blender/pulls/135416
2025-03-05 15:31:36 +01:00
Clément Foucault
a92f6ebf13 Cleanup: DRW: Remove ModelMatrix legacy GLSL macros 2025-03-05 15:31:35 +01:00
Clément Foucault
127a5d6d3a Cleanup: GPU: Shader C++: Avoid #pragma once in main file warning
Pull Request: https://projects.blender.org/blender/blender/pulls/135384
2025-03-03 12:50:47 +01:00
Clément Foucault
783472671e Cleanup: GPU: Add macro for default constructor compatibility on MSL 2025-03-03 12:50:45 +01:00
Clément Foucault
2c20c200bf Cleanup: GPU: Remove warning about is_zero redundant declaration 2025-03-03 12:50:45 +01:00
Jeroen Bakker
667c8a914f Revert "Cleanup: GPU: Reduce compilation warnings"
This reverts commit 3ca5f6f62e.

There were some performance regressions detected.

Fixes: #135215, #134941
2025-02-27 08:23:26 +01:00
Omar Emara
6365110312 Cleanup: GPU: Remove common_math.glsl includes
This patch refactors GPU shaders to remove includes to the utility
gpu_shader_common_math.glsl file. This is done because it has duplicate
functions that exist in other files, and it was really created for use
in GPU material nodes.

The safe_divide and hypot functions were removed since they exist in
gpu_shader_math_base_lib.glsl.

The compatible_[mod|pow] and wrap functions were moved into
gpu_shader_math_base_lib.glsl.

The floor_to_int function was inlined since it was trivial and only used
in one place.

The quick_floor was removed because it was unused.

The euler_to_mat3 function was replaced with the from_rotation function
from gpu_shader_math_matrix_lib.glsl.

Now the file only contains some GPU material node utility functions.

Pull Request: https://projects.blender.org/blender/blender/pulls/135160
2025-02-26 13:37:20 +01: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
Clément Foucault
27c20aaae7 Overlay: Add support for CPP shader compilation
Rel #127983

Also removes some unused shaders.

Pull Request: https://projects.blender.org/blender/blender/pulls/135034
2025-02-24 16:17:18 +01:00
Jeroen Bakker
3ca5f6f62e Cleanup: GPU: Reduce compilation warnings
When compiling shaders using GCC there are warnings about functions
being declared twice. This PR will remove those warnings as they are
false positives. The warnings exists to identify typing errors.

Pull Request: https://projects.blender.org/blender/blender/pulls/134832
2025-02-20 11:17:49 +01:00
Dalai Felinto
1584cd9aa5 Cleanup: Rename point cloud to pointcloud / POINT_CLOUD to POINTCLOUD
Though "Point Cloud" written as two words is technically correct and should be used in the UI, as one word it's typically easier to write and parse when reading. We had a mix of both before this patch, so better to unify this as well.

This commit also renames the editor/intern/ files to remove pointcloud_ prefix.
point_cloud was only preserved on the user facing strings:

* is_type_point_cloud
* use_new_point_cloud_type

Pull Request: https://projects.blender.org/blender/blender/pulls/134803
2025-02-19 17:11:08 +01:00
Alaska
8ed69341d0 Shader: Expose light path "is volume scatter ray" in UI
This commit adds the "is volume scatter" output to the light path node
in the shader editor.

All the funcitonal code for this feature already exists in Cycles SVM
and OSL, but the output wasn't exposed on the node.

EEVEE does not support the feature, so it's output will
always be zero.

Pull Request: https://projects.blender.org/blender/blender/pulls/134343
2025-02-12 15:45:04 +01:00
Clément Foucault
623c81c7ec Fix: GPU: Broken gizmo color
Some unrelated change was brought back inside
86b70143d5

Revert the offending change.
2025-02-11 12:54:20 +01:00
Clément Foucault
86b70143d5 Cleanup: GPU: Remove unused Transform Feedback implementation
Most of the cleanup is inside the metal backend.

Pull Request: https://projects.blender.org/blender/blender/pulls/134349
2025-02-10 17:30:42 +01:00
Bastien Montagne
4b996baa76 Merge branch 'blender-v4.4-release' 2025-02-10 14:16:47 +01:00
Clément Foucault
34216a2b7b Fix #134234: GPU: Shader Compile Error on with gpu.types.GPUShader
The removal of the loose uniform made the shader not compile.
This patch adds a new define for these type of shaders and add
back the loose uniform.

Note that these shaders might no longer work on Metal as
the source is not parsed anymore.

Pull Request: https://projects.blender.org/blender/blender/pulls/134341
2025-02-10 14:06:47 +01:00
Campbell Barton
e659e87a67 Cleanup: cmake formatting, sort file lists 2025-02-10 19:40:06 +11:00
Weizhen Huang
d2db9927ed Fix #86648: reduce ray differentials size for bump mapping
Use sub-pixel differentials for bump mapping helps with reducing
artifacts when objects are moving or when textures have high frequency
details.

Currently we scale it by 0.1 because it seems to work good in practice,
we can adjust the value in the future if it turns out to be impractical.

Ref: #122892

Pull Request: https://projects.blender.org/blender/blender/pulls/133991
2025-02-05 13:39:27 +01:00