Commit Graph

3434 Commits

Author SHA1 Message Date
Campbell Barton
c3320a1082 Fix crash starting with only the Vulkan backend enabled
When built with only the Vulkan back-end this wasn't being checked for
causing Blender to start with no GPU backend which crashed.

Ref !136110
2025-03-18 22:23:15 +11:00
Hans Goudey
e5d9b7a43f Cleanup: GPU: Remove unused index buffer function
This information is set when creating the indices,
there should be no need to compute it later.

Pull Request: https://projects.blender.org/blender/blender/pulls/135986
2025-03-14 16:55:55 +01:00
Jeroen Bakker
15d88e544a GPU: Storage buffer allocation alignment
Since the introduction of storage buffers in Blender, the calling
code has been responsible for ensuring the buffer meets allocation
requirements. All backends require the allocation size to be divisible
by 16 bytes. Until now, this was sufficient, but with GPU subdivision
changes, an external library must also adhere to these requirements.

For OpenSubdiv (OSD), some buffers are not 16-byte aligned, leading
to potential misallocation. Currently, this is mitigated by allocating
a few extra bytes, but this approach has the drawback of potentially
reading unintended bytes beyond the source buffer.

This PR adopts a similar approach to vertex buffers: the backend handles
extra byte allocation while ensuring data uploads and downloads function
correctly without requiring those additional bytes.

No changes were needed for Metal, as its allocation size is already
aligned to 256 bytes.

**Alternative solutions considered**:

- Copying the CPU buffer to a larger buffer when needed (performance impact).
- Modifying OSD buffers to allocate extra space (requires changes to an external library).
- Implementing GPU_storagebuf_update_sub.

Ref #135873

Pull Request: https://projects.blender.org/blender/blender/pulls/135716
2025-03-13 15:05:16 +01:00
Campbell Barton
6ef7dae8ef Cleanup: spelling in comments (make check_spelling_*) 2025-03-13 13:41:17 +11:00
Jeroen Bakker
40696ca452 SubDiv: Migrate GPU subdivision to use GPU module
Blender already had its own copy of OpenSubDiv containing some local fixes
and code-style. This code still used gl-calls. This PR updates the calls
to use GPU module. This allows us to use OpenSubDiv to be usable on other
backends as well.

This PR was tested on OpenGL, Vulkan and Metal. Metal can be enabled,
but Vulkan requires some API changes to work with loose geometry.

![metal.png](/attachments/bb042c3a-1a87-4140-9958-a80da10d417b)

# Considerations

**ShaderCreateInfo**

intern/opensubdiv now requires access to GPU module. This to create buffers
in the correct context and trigger correct dispatches. ShaderCreateInfo is used
to construct the shader for cross compilation to Metal/Vulkan. However opensubdiv
shader caching structures are still used.

**Vertex buffers vs storage buffers**

Implementation tries to keep as close to the original OSD implementation. If
they used storage buffers for data, we will use GPUStorageBuf. If it uses vertex
buffers, we will use gpu::VertBuf.

**Evaluator const**

The evaluator cannot be const anymore as the GPU module API only allows
updating SSBOs when constructing. API could be improved to support updating
SSBOs.

Current implementation has a change to use reads out of bounds when constructing
SSBOs. An API change is in the planning to remove this issue. This will be fixed in
an upcoming PR. We wanted to land this PR as the visibility of the issue is not
common and multiple other changes rely on this PR to land.

Pull Request: https://projects.blender.org/blender/blender/pulls/135296
2025-03-10 07:31:59 +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
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
Clément Foucault
04fcf2f907 Merge branch 'blender-v4.4-release' 2025-03-05 12:06:00 +01:00
Clément Foucault
326ce59961 Fix #134509: GPU: Add workaround for Intel ARC nodelink driver bug
This disables the instancing optimization for this specific
hardware.

Pull Request: https://projects.blender.org/blender/blender/pulls/135458
2025-03-05 12:05:34 +01:00
Brecht Van Lommel
3dab100860 Fix: ASAN errors after addition of texture pool
Same fix as #132504. Free the texture pool before the derived GPU context
class, as that one is used as part of freeing the texture pool.

Pull Request: https://projects.blender.org/blender/blender/pulls/135444
2025-03-04 16:54:05 +01:00
Clément Foucault
fb6c68aaef Fix: GPU: Shader source log flooding the console on error
This has been commited by mistake in 86b70143d5
2025-03-04 13:05:22 +01:00
Jeroen Bakker
e866137cc8 Fix: Compiling shaders without opensubdiv
When blender is compiled with `WITH_OPENSUBDIV=Off` Blender just works
fine. However when compiling all the static shaders the OpenSubDiv
shaders are also compiled and fail as they rely on OpenSubDiv.

This PR fixes this by only adding the shaders when OpenSubDiv is
available.

This issue could be reproduced using the `--debug-gpu-compile-shaders`
option or running GPU test cases.

Pull Request: https://projects.blender.org/blender/blender/pulls/135285
2025-02-28 09:58:08 +01:00
Miguel Pozo
f930d71a1e GPU: Threadsafe shader creation and acquisition
Move the `StaticShader` class from Workbench to `GPU_shader` and make
compilation thread-safe (Shader usage is still not thread-safe).
Use `StaticShader`s for all shader caches.

Subdivision shaders are still not ported.

(Part of #134690)

Pull Request: https://projects.blender.org/blender/blender/pulls/134812
2025-02-27 19:20:33 +01:00
Jeroen Bakker
22aad0fde7 SubDiv: Use shader create info for patch evaluation shader
This PR migrates the subdiv_patch_evaluation_comp.glsl to use
shader create info.

The part of OSD that is used is included as a typedef source (osd_patch_basis.glsl).

Pull Request: https://projects.blender.org/blender/blender/pulls/134917
2025-02-27 15:42:08 +01:00
Clément Foucault
3a7d086200 Cleanup: DRW: Remove legacy create info and rename new ones
This is just mass renaming
2025-02-25 23:05:12 +01:00
Hans Goudey
e522a6c826 Cleanup: GPU: Use StringRef for vertex format function arguments
And slightly simplify two string processing functions in this API,
`GPU_vertformat_safe_attr_name` and `copy_attr_name`.
This makes the API easier to interface with from C++ code,
and can avoid unnecessary string length measurements.

Pull Request: https://projects.blender.org/blender/blender/pulls/134882
2025-02-25 14:53:51 +01:00
Hans Goudey
d0a6189b50 Refactor: DRW: Centralize and clean up packed normals conversion
Move the code dealing with converting float3 to GPU normals
out of the vertex format header into a separate header. Use a
proper C++ namespace and remove duplication by only using
the more recently added C++ templated conversions.

Most of the diff comes from the removal of the indirect includes
from GPU_vertex_format.hh. A lot of files ended up mistakenly
depending on that.

Pull Request: https://projects.blender.org/blender/blender/pulls/134873
2025-02-24 16:08:30 +01:00
Clément Foucault
966a424f00 Fix: GPU: Printf Memory leak
The previous fix 8f00f068ad
doesn't work as the printf buffer gets recreated.

Ensure render boundaries at lower level and do the printf
flush manually.
2025-02-21 14:35:36 +01:00
Clément Foucault
8f00f068ad Fix: GPU: Memory leak from shader printf buffer
The printf buffer were not flushed in some cases
before the context destruction.
2025-02-21 13:47:28 +01:00
Jeroen Bakker
e39669e3c3 SubDiv: Use shader create info for SubdivNormalsFinalize shaders.
This change migrates the first 2 subdiv shaders  to use the ShaderCreateInfo.
Other shaders will follow in separate PRs.

- Should compile when using `WITH_GPU_SHADER_CPP_COMPILATION`
- A `subdiv_` prefix is added only to the functions related to `PosNorLoop`.
    But eventually the prefix should also be added to other lib functions.
- Due to Metal restrictions `subdiv_set_vertex_*` is implemented using a
    functional paradigma. Our Metal backend only supports `inout` qualifier
    on thead local data structures.

Pull Request: https://projects.blender.org/blender/blender/pulls/134218
2025-02-20 12:30:51 +01:00
Miguel Pozo
f5d56a3124 GPU: Add --profile-gpu
Add a `--profile-gpu` launch argument.

When set, it generates a profile in the Trace Event Format with CPU and
GPU metrics based on GPU debug scopes.
https://profilerpedia.markhansen.co.nz/formats/trace-event-format/

The profiles are best viewed at https://ui.perfetto.dev/

Notes:
- The profiler captures everything form app start to exit.
- Being JSON based the profiles can become relatively large, but they
  compress very well.
- Only OpenGL profiling is supported for now, but the report formatting
  code can be shared across backends.

Pull Request: https://projects.blender.org/blender/blender/pulls/133557
2025-02-18 15:36:50 +01:00
Clément Foucault
a6364eae75 DRW: Make vertex format threadsafe
Wrapping the vertformat into lambda expression to make
them threadsafe.

Pull Request: https://projects.blender.org/blender/blender/pulls/134685
2025-02-17 18:24:31 +01:00
Clément Foucault
28ad3736e8 DRW: Move cube batch generation to GPU module
Avoid access on global DRWShapeCache and still
share the code for batch creation.
Each module is then responsible owner of their
own batch.
2025-02-17 12:36:34 +01:00
Clément Foucault
c91d60cda5 DRW: Move procedural batch to GPUContext
This remove relying on a global for storing
the batch and allow for more streamlined usage
of procedural drawing.
2025-02-17 12:36:34 +01:00
Bastien Montagne
c43a583370 Merge branch 'blender-v4.4-release' 2025-02-14 17:21:17 +01:00
Bastien Montagne
bbebf72a33 Cleanup: Add explicit defaults to MEM_new-allocated structs with no constructors.
While not strictly needed (currently!), relying on C++ 'default
initialization' is fragile and unreliable.
2025-02-14 17:07:23 +01:00
Bastien Montagne
978f2d1caa Merge branch 'blender-v4.4-release' 2025-02-14 12:50:10 +01:00
Bastien Montagne
194e233d86 Fix (unreported) more invalid C-style allocation of non-trivial C++ data. 2025-02-14 12:23:38 +01:00
Clément Foucault
6138ee64a9 GPU: TexturePool: Add texture pool functionality to the GPU module
This patch adds the texture pool functionality that was previously
only available in the DRW module to the GPU module.

This allows to not rely on global `DST` variable for the managment
of these temporary textures.

Moreover, this can be extended using dedicated GPU backend
specific behavior to reduce the amount of memory needed
to render.

The implementation is mostly copy pasted from the draw implementation
but with more documentation. Also it is simplified since the
`DRW_texture_pool_query` functionality is not needed.

Pull Request: https://projects.blender.org/blender/blender/pulls/134403
2025-02-11 18:58:46 +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
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
b02bbbdb37 Cleanup: rename "GL" selection to "DEPTH"
The reference to OpenGL is no longer relevant.
2025-02-05 11:21:50 +11:00
Clément Foucault
976ed42533 Cleanup: GPU: Use functional cast for scalar casting 2025-01-31 18:26:44 +01:00
Brecht Van Lommel
c7502b092d Cleanup: Various clang-tidy warnings in gpu
Pull Request: https://projects.blender.org/blender/blender/pulls/133734
2025-01-31 17:03:18 +01:00
Jeroen Bakker
4b99bc8515 Fix: Renderdoc: Corruption in debug stack
In renderdoc the debug stack got corrupted when render graphs where
reused. The previous usage didn't clear the stack. This PR clears
the debug stack when render graphs are reset.
2025-01-30 13:46:21 +01:00
Jacques Lucke
e1753900b7 BLI: improve UTF-8 safety when copying StringRef to char buffers
Previously, there was a `StringRef.copy` method which would copy the string into
the given buffer. However, it was not defined for the case when the buffer was
too small. It moved the responsibility of making sure the buffer is large enough
to the caller.

Unfortunately, in practice that easily hides bugs in builds without asserts
which don't come up in testing much. Now, the method is replaced with
`StringRef.copy_utf8_truncated` which has much more well defined semantics and
also makes sure that the string remains valid utf-8.

This also renames `unsafe_copy` to `copy_unsafe` to make the naming more similar
to `copy_utf8_truncated`.

Pull Request: https://projects.blender.org/blender/blender/pulls/133677
2025-01-29 12:12:27 +01:00
Clément Foucault
10c0b19213 Cleanup: GPUMaterial: Remove leftover EEVEE Legacy code 2025-01-28 17:48:34 +01:00
Clément Foucault
1ac4651778 Cleanup: DRW: Remove legacy common_view_lib.glsl
No functional changes. Only moving and renaming stuff.

Pull Request: https://projects.blender.org/blender/blender/pulls/131558
2025-01-23 18:06:22 +01:00
Brecht Van Lommel
7a0a173d39 Fix: Missed case of OCIO luminance coefficients in EEVEE
Following up on #133368. Thanks Omar for spotting this.

Pull Request: https://projects.blender.org/blender/blender/pulls/133400
2025-01-22 11:19:02 +01:00
Jeroen Bakker
2f18e4fe29 Vulkan: Add debug group for swapchain
Improves debugging swapchains when using renderdoc.

Pull Request: https://projects.blender.org/blender/blender/pulls/133190
2025-01-17 11:40:11 +01:00
Jeroen Bakker
9091085277 Fix: GPU: Compiling python gpu shaders
Compiling of graphics shaders via gpu crashed. The vulkan backend found
a compute source and continued the evaluation as if it was a compute
shader.

The compute source was added by the preprocessor that wraps the shader
source. Even empty sources were wrapped. Detection based on empty shader
sources failed.

This is not a Vulkan only issue as other platforms would have similar issues when
creating a compute shader.

Pull Request: https://projects.blender.org/blender/blender/pulls/133036
2025-01-14 10:51:24 +01:00
Falk David
d413b0064f Cleanup: Move BKE_material.h to C++
The `BKE_material.h` is only renamed to `.hh` to preserve
the history of the file. Changes to the file are done in
the following commit.
2025-01-09 18:11:46 +01:00
Omar Emara
8f8ae302ba Refactor: Compositor: Moved wrapped translation to its own code
This patch moves wrapped translation from a special case of the general
transform algorithm to the Translate node. Since the Translate node is
the only user of this special case, it doesn't make sense to complicate
a generate algorithm with it. This will make future refactors of this
code easier.

Pull Request: https://projects.blender.org/blender/blender/pulls/132793
2025-01-08 12:42:13 +01:00
Brecht Van Lommel
920e709069 Refactor: Make header files more clangd and clang-tidy friendly
When using clangd or running clang-tidy on headers there are
currently many errors. These are noisy in IDEs, make auto fixes
impossible, and break features like code completion, refactoring
and navigation.

This makes source/blender headers work by themselves, which is
generally the goal anyway. But #includes and forward declarations
were often incomplete.

* Add #includes and forward declarations
* Add IWYU pragma: export in a few places
* Remove some unused #includes (but there are many more)
* Tweak ShaderCreateInfo macros to work better with clangd

Some types of headers still have errors, these could be fixed or
worked around with more investigation. Mostly preprocessor
template headers like NOD_static_types.h.

Note that that disabling WITH_UNITY_BUILD is required for clangd to
work properly, otherwise compile_commands.json does not contain
the information for the relevant source files.

For more details see the developer docs:
https://developer.blender.org/docs/handbook/tooling/clangd/

Pull Request: https://projects.blender.org/blender/blender/pulls/132608
2025-01-07 12:39:13 +01:00
Jeroen Bakker
c2b8186b07 Fix #132590: Python/GPU: Builtin clipped shaders messages
Confusing error messages are printed when requesting a clipped builtin
shader via Python that does not exist.

This PR will remove the confusion of the messaging:
- Replaced BLI_assert_unreachable with an assert as it is reachable
  code.
- Adding clipped configuration for POLYLINE_UNIFORM_COLOR

Pull Request: https://projects.blender.org/blender/blender/pulls/132686
2025-01-06 15:42:07 +01:00
Brecht Van Lommel
24e5226ff0 Fix #128186: Invalid GPU framebuffer free from context
Framebuffers are getting freed in the GPUContext base class destructor. But
the framebuffer destructors use the MTL/VK/GLContext derived class, whose
destructor has already completed at this point. So these contexts are no
longer valid to use.

Now free the framebuffers earlier.

This caused ASAN warnings, it's not known to cause actual bugs.

Pull Request: https://projects.blender.org/blender/blender/pulls/132504
2025-01-06 11:32:02 +01:00
Hans Goudey
3d0988a719 Cleanup: Move some math C-API usage to C++ API
Mostly part of an attempt to remove some of the lesser-used
parts of the C math API.
2024-12-20 21:46:59 +01:00
Omar Emara
33f758dcd1 Cleanup: Remove mention of realtime compositor 2024-12-19 14:43:02 +02:00
Jeroen Bakker
28ea90f127 GPU: Split GPU backend and Draw manager unit tests
Both the draw manager and gpu backend used the same compilation
directive for enablement. This PR seperates them into
`WITH_GPU_DRAW_TESTS` for draw manager related tests and
`WITH_GPU_BACKEND_TESTS` for gpu backend related tests.

Pull Request: https://projects.blender.org/blender/blender/pulls/132018
2024-12-17 13:28:21 +01:00
Aras Pranckevicius
59871814ff Cleanup: remove various mentions of "realtime compositor"
Some variable renames, some comment edits.

Pull Request: https://projects.blender.org/blender/blender/pulls/132014
2024-12-17 13:00:50 +01:00