Commit Graph

4309 Commits

Author SHA1 Message Date
Miguel Pozo
e6638d6e5e Refactor: GPU: GPUMaterial & GPUPass compilation
Cleanup and simplification of GPUMaterial and GPUPass compilation.
See #133674 for details/goals.

- Remove the `draw_manage_shader` thread.
  Deferred compilation is now handled by the gpu::ShaderCompiler
  through the batch compilation API.
  Batch management is handled by the `GPUPassCache`.
- Simplify `GPUMaterial` status tracking so it just queries the
  `GPUPass` status.
- Split the `GPUPass` and the `GPUCodegen` code.
- Replaced the (broken) `GPU_material_recalc_flag_get` with the new
  `GPU_pass_compilation_timestamp`.
- Add the `GPU_pass_cache_wait_for_all` and
  `GPU_shader_batch_wait_for_all`, and remove the busy waits from
   EEVEE.
- Remove many unused functions, properties, includes...

Pull Request: https://projects.blender.org/blender/blender/pulls/135637
2025-05-22 17:53:22 +02:00
Clément Foucault
78170cbf90 Fix #139189: Vulkan: UV Display Issues When Selecting Specific Mesh Regions
The format was not 4 byte aligned and not compatible with the new
format enums.

Pull Request: https://projects.blender.org/blender/blender/pulls/139202
2025-05-22 09:18:08 +02:00
Clément Foucault
decf99106a GPU: Add shader and pass name inside debug groups
This allows to know which shader specialization and
compilation are affecting performance.
2025-05-20 14:44:23 +02:00
Clément Foucault
d3053fead0 GPU: Assert that a shader is not bound when deleting it
This caused UB in the tests now that tests are all ran
inside the same context.

A shader could be free but its pointer would be dangling
inside the `Context`. A new shader could have the same
address and generate UB after binding.

This is not the best way to solve this issue but at least
we prevent the use of the UB.

Pull Request: https://projects.blender.org/blender/blender/pulls/139109
2025-05-20 12:43:58 +02:00
Clément Foucault
de8f13d1d6 Fix: GPU: Assert caused by specialization constant with primitive expansion
This path was not passing the the specialization constant state
to `GPU_shader_bind`.

Fix #139132
2025-05-20 11:23:42 +02:00
Campbell Barton
277dc3aef4 Cleanup: use doxygen style comments for doc-strings 2025-05-20 04:33:42 +00:00
Clément Foucault
caac241c84 GPU: Make Shader Specialization Constant API Thread Safe
This allows multiple threads to request different specializations without
locking usage of all specialized shaders program when a new specialization
is being compiled.

The specialization constants are bundled in a structure that is being
passed to the `Shader::bind()` method. The structure is owned by the
calling thread and only used by the `Shader::bind()`.
Only querying for the specialized shader (Map lookup) is locking the shader
usage.

The variant compilation is now also locking and ensured that
multiple thread trying to compile the same variant will never result
in race condition.

Note that this removes the `is_dirty` optimization. This can be added
back if this becomes a bottleneck in the future. Otherwise, the
performance impact is not noticeable.

Pull Request: https://projects.blender.org/blender/blender/pulls/136991
2025-05-19 17:42:55 +02:00
Brecht Van Lommel
40642343b6 Fix #138978: Modifier cage option not working when GPU subdivision is off
Need an equivalent of DRW_object_get_data_for_drawing for the cage mesh,
to ensure proper comparison between meshes.

Pull Request: https://projects.blender.org/blender/blender/pulls/138987
2025-05-19 11:50:00 +02:00
Campbell Barton
b3dfde88f3 Cleanup: spelling in comments (check_spelling_* target)
Also uppercase acronyms: API, UTF & ASCII.
2025-05-17 10:17:37 +10:00
Hans Goudey
7a125d5ebc Cleanup: Draw: Simplify trivial attribute requests clearing 2025-05-16 12:30:48 -04:00
Clément Foucault
8ac5940e33 GPU: Add GL_ARB_clip_control support to the GL backend
This adds support for the extension and always
set the clip state value to 0..1 to align with vulkan
and metal. Moreover this is needed for the Reverse Z
implementation.

Note that this is a OpenGL 4.5 feature and is not
required to start Blender. So there must still be
a fallback path for now.

Rel #138898

Pull Request: https://projects.blender.org/blender/blender/pulls/138941
2025-05-16 13:53:36 +02:00
Jeroen Bakker
729f6290b8 Fix: Draw: Asserts due to recent changes
cbcf421db7 could raise asserts as it
didn't use the `!is_empty()` function to test if the name is set.

Pull Request: https://projects.blender.org/blender/blender/pulls/138970
2025-05-16 11:20:53 +02:00
Hans Goudey
cbcf421db7 Cleanup: Draw: Use StringRef for attribute names
Avoid the need to measure string length potentially many times.
2025-05-15 14:53:51 -04:00
Hans Goudey
760cf70d63 Draw: Use VectorSet for generic attribute requests
Replace `DRW_Attributes` with a VectorSet of std::string. The max number of
attributes is still the same. The inline buffer size is 4, and std::string's inline
buffer is smaller than the previous char array size of 64, but it seems
reasonable to save those optimizations for shorter attribute names and
fewer attributes. In return we significantly decrease the size of the batch
caches, simplify the code, and remove the attribute name length limit.

I observed roughly an 8% increase in the 30k cube objects file, a change from
12 to 13 FPS. I'm guessing this is mostly because `VectorSet<std::string>` is
smaller than `DRW_Attributes`.

Pull Request: https://projects.blender.org/blender/blender/pulls/138946
2025-05-15 20:43:31 +02:00
Clément Foucault
97a1e1501b DRW: Add a new GPU context for rendering preview jobs
This avoid stalling the viewport when a preview job is running.
This is because both were fighting for the same GPU context.

This doesn't remove the blocking but allows to remove it using #136991.

Pull Request: https://projects.blender.org/blender/blender/pulls/138882
2025-05-15 20:09:37 +02:00
Hans Goudey
011b3f5cb3 Cleanup: Remove unused field from mesh draw batch cache 2025-05-15 13:14:47 -04:00
Hans Goudey
b13bdcfaeb Fix #138910: Crash with sculpt mode and "Texture" shading type
Using the wrong `std::string` constructor through an alias, oops..
2025-05-15 10:13:34 -04: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
Sean Kim
745582ee01 Fix #138851: Assert on entering Sculpting workspace
Introduced with 617858e453

Pull Request: https://projects.blender.org/blender/blender/pulls/138852
2025-05-14 20:47:31 +02:00
Hans Goudey
153abc372e GPU: Replace U32/I32 combination with GPU_FETCH_INT_TO_FLOAT in UI code
The conversion from int to float is not supported natively
so it ends up happening beforehand on the CPU or as a
step before the vertex buffer can be used. It's better to just
upload floats in the first place.

Related to:
- 1e1ac2bb9b
- 617858e453

Pull Request: https://projects.blender.org/blender/blender/pulls/138855
2025-05-14 15:15:21 +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
617858e453 GPU: Unified DataFormat enum
This unifies vertex and texture data formats
into a single base enum class.

`TextureFormat` and `VertexFormat` then mask
the invalid format for their respective usage.

Having a base enum allows casting between
`TextureFormat` and `VertexFormat` possible
(needed for Buffer Textures).

It also makes it easier to write and read data
to buffers/textures as each format will have an
associated host type.

These enum is generated from MACRO expansion.
This allow to centralize all information about
the formats in one place. This avoid duplicating
the list of enums for each backend.

This only creates the new enum. Porting older enums will
be done in other PRs.

Normalized integer CPU format are missing and waiting for #130640

Rel #130632

Pull Request: https://projects.blender.org/blender/blender/pulls/138069
2025-05-13 17:08:32 +02:00
Hans Goudey
eaa3dd0cd3 Refactor: Remove data type from draw attribute request struct
Similar to 93be6baa9c.

It doesn't make sense to store the type as part of the request
since we upload any generic attribute, and the vertex buffer
type is just chosen depending on the attribute's type in the
geometry anyway.

The code in `mesh_cd_calc_used_gpu_layers` is unfortunately
still way too complicated to remove the custom data layer lookup,
but this gets us one step closer.

Pull Request: https://projects.blender.org/blender/blender/pulls/138791
2025-05-13 13:35:30 +02:00
Hans Goudey
f2327a956b Cleanup: Remove disabled code
This only makes this function even more difficult to understand.
2025-05-12 16:38:14 -04:00
Hans Goudey
93be6baa9c Refactor: Remove domain from draw attribute request struct
Similar to b21cb20eeb.
This time the domain is removed. The idea is that the domain doesn't
change anything about how the attribute is stored on the vertex buffers
so it doesn't make sense as part of the request. If we continue that
logic to also remove the data type, we can avoid searching through the
geometry when creating the requests, instead handling invalid requests
when creating the buffers.

The complexity of the change comes from the fact that the request's
domain was used to determine whether the Curves drawing code needed to
interpolate the attribute to the evaluated points. This is now stored
separately in the curves cache. The change in the sculpt code is also
non-trivial since we delay more of the logic until after we have
looked up the attribute from the geometry.

Pull Request: https://projects.blender.org/blender/blender/pulls/138619
2025-05-12 21:16:15 +02:00
Hans Goudey
d018c12b61 Cleanup: Draw: Use StringRef for function argument 2025-05-08 13:09:06 -04:00
Hans Goudey
b21cb20eeb Refactor: Remove layer index from draw attribute request struct
The goal is to separate the draw attribute request from the
CustomData implementation. For the layer index this was
already started a while ago; it's only used in a couple places
and lookups are mostly name based anyway.

Conceptually the attribute request is just a request that the
extraction system create a buffer for a certain attribute name.
Information about where the attribute is stored doesn't fit.

Pull Request: https://projects.blender.org/blender/blender/pulls/138570
2025-05-08 16:55:23 +02:00
Jeroen Bakker
db10854e3c Fix #138304: DRAW: Texture wrapper not releasing texture views
When a texture wrapper wraps a second texture it doesn't free its
local resources based on  the previous texture. This resulted in texture
views still being used where the backed memory could already be reused
by other allocations.

In OpenGL this might be solved inside the driver by not freeing the
backed texture unless all views have been freed. However our Vulkan
backend doesn't do this, leading to crashes when resizing the viewport
when displaying a workbench volume. In OpenGL this could lead to small
resizing artifacts, although we haven't noticed them. Overlay also wraps
existing textures.

Pull Request: https://projects.blender.org/blender/blender/pulls/138582
2025-05-08 10:23:28 +02:00
Hans Goudey
1fd958fe7c Cleanup: Resolve missing declaration warning 2025-05-07 17:05:31 -04:00
Hans Goudey
f90e448c6a Cleanup: Use blender::Mutex for various static mutexes
Change almost all `static ThreadMutex` to `static blender::Mutex`.
See b7a1325c3c.

Pull Request: https://projects.blender.org/blender/blender/pulls/138556
2025-05-07 19:32:00 +02:00
YimingWu
518bef9ae9 Fix #138378: Grease Pencil: Correct logic of checking render requirement
`DRW_gpencil_engine_needed` only checks whether grease pencil is
excluded in the viewport or whether there's grease pencil ID exists,
this can not handle cases where grease pencil strokes are generated from
other types of object in geometry nodes. Now this function is split into
two `gpencil_engine_needed_viewport` and
`gpencil_excluded` calls to provide more granulated logic for
places that need them.

Pull Request: https://projects.blender.org/blender/blender/pulls/138386
2025-05-07 17:42:02 +02:00
Clément Foucault
b3732f057e Fix #137984: Sculpt mode crash with certain mesh
This was caused by the material indices not referring to an existing
material slot. Clamping the value to the maximum material index for
the given object fixes the issue.

Pull Request: https://projects.blender.org/blender/blender/pulls/138544
2025-05-07 16:46:47 +02:00
Jacques Lucke
b7a1325c3c BLI: use blender::Mutex by default which wraps tbb::mutex
This patch adds a new `BLI_mutex.hh` header which adds `blender::Mutex` as alias
for either `tbb::mutex` or `std::mutex` depending on whether TBB is enabled.

Description copied from the patch:
```
/**
 * blender::Mutex should be used as the default mutex in Blender. It implements a subset of the API
 * of std::mutex but has overall better guaranteed properties. It can be used with RAII helpers
 * like std::lock_guard. However, it is not compatible with e.g. std::condition_variable. So one
 * still has to use std::mutex for that case.
 *
 * The mutex provided by TBB has these properties:
 * - It's as fast as a spin-lock in the non-contended case, i.e. when no other thread is trying to
 *   lock the mutex at the same time.
 * - In the contended case, it spins a couple of times but then blocks to avoid draining system
 *   resources by spinning for a long time.
 * - It's only 1 byte large, compared to e.g. 40 bytes when using the std::mutex of GCC. This makes
 *   it more feasible to have many smaller mutexes which can improve scalability of algorithms
 *   compared to using fewer larger mutexes. Also it just reduces "memory slop" across Blender.
 * - It is *not* a fair mutex, i.e. it's not guaranteed that a thread will ever be able to lock the
 *   mutex when there are always more than one threads that try to lock it. In the majority of
 *   cases, using a fair mutex just causes extra overhead without any benefit. std::mutex is not
 *   guaranteed to be fair either.
 */
 ```

The performance benchmark suggests that the impact is negilible in almost
all cases. The only benchmarks that show interesting behavior are the once
testing foreach zones in Geometry Nodes. These tests are explicitly testing
overhead, which I still have to reduce over time. So it's not unexpected that
changing the mutex has an impact there. What's interesting is that on macos the
performance improves a lot while on linux it gets worse. Since that overhead
should eventually be removed almost entirely, I don't really consider that
blocking.

Links:
* Documentation of different mutex flavors in TBB:
  https://www.intel.com/content/www/us/en/docs/onetbb/developer-guide-api-reference/2021-12/mutex-flavors.html
* Older implementation of a similar mutex by me:
  https://archive.blender.org/developer/differential/0016/0016711/index.html
* Interesting read regarding how a mutex can be this small:
  https://webkit.org/blog/6161/locking-in-webkit/

Pull Request: https://projects.blender.org/blender/blender/pulls/138370
2025-05-07 04:53:16 +02:00
Hans Goudey
86ce88f25c Cleanup: Add missing include to avoid need for transitive includes 2025-05-06 12:09:53 -04:00
Falk David
01fb09b240 Fix #136091: Grease Pencil: Broken wireframe rendering
In the `grease_pencil_wire_batch_ensure` when computing the indices,
the function was using `points_by_curve` instead of
`evaluated_points_by_curve`.
Now the correct indices are used.

Pull Request: https://projects.blender.org/blender/blender/pulls/138489
2025-05-06 14:16:57 +02:00
Jacques Lucke
e8d1491e62 Refactor: Depsgraph: simplify query API further
* Remove `DEG_get_evaluated_object` in favor of `DEG_get_evaluated`.
* Remove `DEG_is_original_object` in favor of `DEG_is_original`.
* Remove `DEG_is_evaluated_object` in favor of `DEG_is_evaluated`.

Pull Request: https://projects.blender.org/blender/blender/pulls/138317
2025-05-02 15:08:29 +02:00
Campbell Barton
43af16a4c1 Cleanup: spelling in comments, correct comment block formatting
Also use doxygen comments more consistently.
2025-05-01 11:44:33 +10:00
Clément Foucault
4c4c21df76 DRW: Debug: Change debug scope to debug group
These are not the same. Debug Scope are for scoped
capture. These should be static to avoid flooding
debug tools with scopes. Since this code is
unlikely to be debugged, it is better to use
debug groups.
2025-04-30 13:40:27 +02:00
Clément Foucault
6c4b410d3e Cleanup: Remove unused draw_common_shader_shared.hh 2025-04-30 09:55:48 +02:00
Clément Foucault
ec0da5bfe7 Overlay: Move GlobalsUboStorage to overlay_shader_shared.hh 2025-04-30 09:55:48 +02:00
Clément Foucault
27a8c00157 Overlay: Split theme colors and sizes into different structs
Centralize processing of each and allow
more semanticaly correct references.
2025-04-30 09:55:48 +02:00
Jeroen Bakker
3aba58b94a Cleanup: Make format 2025-04-29 13:49:24 +02:00
Falk David
1d6f313995 Grease Pencil: Remove minimum thickness clamping
This removes the minimum thickness clamping in the shader.
The reason why this was clamped in the first place was to reduce
aliasing artifacts. With the new super sampling method for
rendering, this should no longer be an issue.

Note: This can break visual compatibility, but the previous radii
were arguably "wrong". This essentially fixes this and renders
the strokes with the actual radii from the attribute.
Previous files that might have relied on the clamping will have
to be updated.

Pull Request: https://projects.blender.org/blender/blender/pulls/138119
2025-04-29 12:46:48 +02:00
Clément Foucault
db22802652 DRW: Avoid warning in subdiv shader code
Was missing a return statement.
2025-04-28 00:55:57 +02:00
Clément Foucault
81c00bf272 EEVEE: Shadow Terminator Normal Bias
This implement the design detailed in #135935.

A new per object property called `Shadow Terminator Normal Offset` is
introduced to shift the shadowed position along the shading normal.

The amount of shift is defined in object space on the object datablock.

This amount is modulated by the facing ratio to the light. Faces
already facing the light will get no offset. This avoids most light
leaking artifacts.

In case of multiple shading normal, the normal used for the shift
is arbitrary. Note that this is the same behavior for other biases.

The magnitude of the bias is controlled by `Shadow Terminator Normal Offset`.
The amount of faces affected by the bias is controlled using
`Shadow Terminator Geometry Offset` just like cycles.

Tweaking the `Shadow Terminator Geometry Offset` allows to avoid too much
shadow distortion on surfaces with bump mapping.

Cycles properties are copied from the Cycles object datablock to the
blender datablock. This break the python API for Cycles.

The defaults are set to no bias because:
- There is no good default. The best value depends on the geometry.
- The best value might depend on real-time displacement.
- Any bias will introduce light leaking on surfaces that do not need it.
- There is an additional cost of enabling it, which is proportional
  to the amount of pixels on screen using it.

Pull Request: https://projects.blender.org/blender/blender/pulls/136935
2025-04-25 19:12:04 +02:00
Clément Foucault
417ffa8f54 Fix #137097: Cycles: No occlusion with Grease Pencil in the viewport
This was caused by missing depth of objects.
The old implementation was relying on the external engine
to provide the correct depth of the objects.
This patch does exactly this.

The downside is that, if overlays are present, the prepass
will be also drawn by the overlay engine.

Pull Request: https://projects.blender.org/blender/blender/pulls/138004
2025-04-25 19:05:57 +02:00
YimingWu
48f9212483 Fix #137700: Guard null lt.editlatt in DRW_cache_lattice_wire_get
Switching to lattice edit mode when it has an armature modifier can
crash if the armature modifier's `show_in_editmode` is turned on. Now
prevent null `editlatt` access.

Pull Request: https://projects.blender.org/blender/blender/pulls/137701
2025-04-25 13:07:54 +02: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
c1493a9518 Fix #137756: EEVEE: Crash when saving while using textured volume on world
This happened because the Volume module was not initialized
before `begin_sync` which was using it.
2025-04-24 12:52:02 +02:00
Clément Foucault
3f11d16501 Cleanup: DRW: Make all shader resource snake case
This make sure that the DRW folder is compliant to our codestyle.

Pull Request: https://projects.blender.org/blender/blender/pulls/137673
2025-04-24 12:50:45 +02:00