Commit Graph

35 Commits

Author SHA1 Message Date
Clément Foucault
a68ad1b676 Fix: GPU: C++ GLSL stubs: Swizzle in wrong list 2025-05-08 20:55:24 +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
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
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
Clément Foucault
3064906eb7 Cleanup: GPU: Add ATTR_FALLTHROUGH for shader C++ compilation 2025-04-03 16:38:21 +02: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
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
Miguel Pozo
bd1f4ec23c Fix: GPU: CPP shader errors in VS2019
Continuation of #131332.

Including built-in headers in VS2019 ends up including `corecrt_math.h`
as a side effect, which has many functions that overlap in name with
our stubs.
This puts the conflicting functions inside its own namespace (`glsl`)
and declares macros for them.
(Note this has the side effect of not allowing us to use those as
variable names)

This also removes the `<cassert>` and `<cstdio>` includes.

Pull Request: https://projects.blender.org/blender/blender/pulls/131386
2024-12-04 18:03:42 +01:00
Clément Foucault
2c3ccdf77a GPU: GLSL compilation as C++ for eevee static shaders
Rel #127983

Pull Request: https://projects.blender.org/blender/blender/pulls/130298
2024-12-02 21:26:15 +01:00
Clément Foucault
e44ae763bc GPU: GLSL C++ Stubs: Add minor missing features 2024-11-30 10:20:37 +01:00
Clément Foucault
bda911bcaa GPU: GLSL C++ Stub: Add packed type and small types 2024-11-29 21:33:06 +01:00
Clément Foucault
6332be9700 GPU: C++ GLSL Stubs: Use unions instead of inheritance for swizzle
This avoid stack overflow on GCC because types were getting too
large (70 bytes for a float4) and created a lot of static
memory for UBOs declarations.
2024-11-29 12:00:01 +01:00
Clément Foucault
95233986cb GPU: C++ GLSL Stubs: Add atomic sampler 2024-11-29 10:19:23 +01:00
Clément Foucault
c1bc1ef3ea GPU: C++ GLSL Stubs: Fix isinf / isnan 2024-11-29 00:37:45 +01:00
Clément Foucault
b39843e7b0 GPU: Use extern for non static expressions and use non zero value for gl_WorkGroupSize
Pull Request: https://projects.blender.org/blender/blender/pulls/131064
2024-11-28 23:23:30 +01:00
Clément Foucault
dd480ab604 GPU: Refactor the GLSL C++ stub swizzle implementation
This removes support for nested swizzling but avoid
warning about static member accesses through class
instance.
2024-11-28 23:23:27 +01:00
Clément Foucault
94d2a1e6a2 GPU: GLSL CPP stubs: Allow printf in code 2024-11-17 19:17:12 +01:00
Clément Foucault
c0c816f846 GPU: GLSL compilation as C++ for workbench static shaders 2024-11-14 23:15:06 +01:00
Clément Foucault
1aea4fb5c7 GPU: GLSL C++ shaders: Silence unuseful warnings 2024-11-13 12:37:00 +01:00
Clément Foucault
29b3df7504 GPU: GLSL compilation as C++ for draw intern shaders
Allow compilation of shaders using C++ for linting and
IDE support.

Related #127983

Pull Request: https://projects.blender.org/blender/blender/pulls/130193
2024-11-13 12:32:39 +01:00
Clément Foucault
091004f1b8 GPU: GLSL compilation as C++ for gpu static shaders
Allow compilation of shaders using C++ for linting and
IDE support.

Related #127983

Pull Request: https://projects.blender.org/blender/blender/pulls/128724
2024-11-12 18:53:34 +01:00
Clément Foucault
4416e27b14 GPU: GLSL C++ stubs: Add support for depth and buffer sampler fetch 2024-11-08 00:28:59 +01:00
Clément Foucault
94a5f541c3 GPU: Improve GLSL-C++ compatibility
Add a few missing operator/functions and fix some argument
type deduction problems.
2024-11-05 00:39:10 +01:00
Clément Foucault
d9ff375e7e GPU: GLSL-C++ Stubs: Make compilation possible
- Add `main` workaournd.
- Define out create info class when compiling with stubs.
- Move interfaces and info namespace to global namespace.
2024-11-01 23:38:22 +01:00
Clément Foucault
0deec1005c GPU: Remove some warnings and errors in GLSL C++ Stubs 2024-10-31 10:18:32 +01:00
Campbell Barton
2e881eacd1 Cleanup: spelling in comments 2024-10-08 09:54:29 +11:00
Clément Foucault
dcd80dbe15 GPU: GLSL C++ stubs
Allows to compile GLSL code using a C++ compiler. The end result is that
IDE features such as autocompletion and error detection can work with
the GLSL codebase.

Rel #127983

Pull Request: https://projects.blender.org/blender/blender/pulls/128598
2024-10-04 17:44:24 +02:00