Commit Graph

37 Commits

Author SHA1 Message Date
Jeroen Bakker
e2dc63c5de Fix #147618: PyGPU incorrect colors when drawing images
644fb2b679 fixed a long standing issue
that offscreen example showed the wrong colors. However the fix assumes
that input texture color space is always sRGB.

This adds a shader variation that draws textures that are stored in scene referred
linear color space (like all of our Image data-block).

Co-authored-by: Clément Foucault <foucault.clem@gmail.com>
Pull Request: https://projects.blender.org/blender/blender/pulls/147788
2025-10-16 19:12:16 +02:00
Clément Foucault
9d0fe5573b GPU: FrameBuffer: Remove GPUFrameBuffer wrapper opaque type
This is the first step into merging DRW_gpu_wrapper.hh into
the GPU module.

This is very similar to #119825.

Pull Request: https://projects.blender.org/blender/blender/pulls/146372
2025-09-16 17:50:48 +02:00
Clément Foucault
2f2ff44e56 GPU: Shader: Remove legacy creation API
This API is no longer used for 5.0.

Pull Request: https://projects.blender.org/blender/blender/pulls/144819
2025-08-20 09:25:32 +02:00
Clément Foucault
1388a70914 GPU: Remove wrapper type for gpu::Shader
This is the first step into merging DRW_gpu_wrapper.hh into
the GPU module.

This is very similar to #119825.

Pull Request: https://projects.blender.org/blender/blender/pulls/144229
2025-08-11 09:34:28 +02:00
Miguel Pozo
0730d3f256 Fix: Python API: bpy.app.is_job_running('SHADER_COMPILATION')
Shader compilation no longer uses the `WM_job` API.
Add a `GPU_shader_batch_is_compiling` function to query if there's any
shader compilation happening, and update `bpy_app_is_job_running` to
handle this as a special case.

Pull Request: https://projects.blender.org/blender/blender/pulls/143559
2025-07-31 20:06:27 +02:00
Clément Foucault
60f7ca21c3 GPU: Shader: Also move the compilation handle in static shaders
Small oversight.
2025-06-03 17:39:33 +02:00
Clément Foucault
940ef330c8 Fix: GPU: Make StaticShader::ensure_compile_async request for ready state
This was missing from the initial implementation.

This did not affect any user code path since they were not
checking for completion and only blocking on first `get`.
2025-05-30 15:21:06 +02:00
Clément Foucault
f82673afd9 Fix: GPU: Invalid logic in StaticShader::get()
Simple oversight. Didn't have any effect.
2025-05-30 15:21:06 +02:00
Clément Foucault
c3f4dd27f1 GPU: Add async compilation to StaticShader class
This allow to easily request async compilation
in a safe way for every static shader.

The get function will always wait if async
compilation has been requested.
2025-05-30 09:14:02 +02:00
Miguel Pozo
2db0b92280 GPU: Add CompilationPriority
Allow adding compilation batches to different priority queues.
Set priorities so static shaders are always compiled first,
then materials, and optimized materials last.

Pull Request: https://projects.blender.org/blender/blender/pulls/139456
2025-05-29 19:43:02 +02:00
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
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
Miguel Pozo
c16aba915f GPU: Add GPU_shader_batch_cancel
Fix the recently implemented ShaderCompiler::batch_cancel.
Expose it with GPU_shader_batch_cancel and
GPU_shader_specialization_batch_cancel.
Use them in the EEVEE ShaderModule destructor, to prevent blocking on
destruction when there are in-flight compilations.

Pull Request: https://projects.blender.org/blender/blender/pulls/138774
2025-05-12 19:54:03 +02:00
Clément Foucault
ac7e361f3d GPU: Expose preprocessor through API
This is needed to preprocess some source inside the
`intern` directory.
2025-04-16 18:50:10 +02:00
Miguel Pozo
712e35a627 Cleanup: Don't use atomic aliases 2025-04-09 20:05:29 +02:00
Clément Foucault
3562433ae7 pyGPU: Deprecate Shader.program getter
This is getting in the way of making the
GPUShader API more threadsafe.

This getter already doesn't work for vulkan
and Metal, and has very limited usage.

Keeping the python function to avoid errors
and display a deprecation warning.

Pull Request: https://projects.blender.org/blender/blender/pulls/136983
2025-04-04 14:23:09 +02: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
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
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
Miguel Pozo
b295df7345 Refactor: Overlay: Port "next" shaders declarations to static CreateInfos
Use static CreateInfos for Overlay-Next shaders using a similar approach to Workbench shader variations.

Remove unused infos and shader sources.
Remove the `gpu_shader_create_info_get_unfinalized_copy` workaround.

Pull Request: https://projects.blender.org/blender/blender/pulls/131514
2024-12-12 23:05:00 +01:00
Germano Cavalcante
19fee82b72 Fix #131236: 'batch_for_shader' not working with POLYLINE shaders
In Blender 4.4 (since commit 00a8d006fe), polyline shaders stopped
using geometry shaders and now rely on SSBOs.

In C++, workarounds allow these shaders to function as before, albeit
with some limitations.

However, this change broke the `batch_for_shader` function in Python,
as `GPUShader.attrs_info_get()` only reads attributes and does not
support SSBOs.

To address this, the method now treats polyline shaders differently,
accessing SSBO inputs instead of attributes.

fix
2024-12-11 20:39:24 +01:00
Clément Foucault
f8da7ecfe1 Cleanup: DRW: Move DRW_SHADER_FREE_SAFE and DRW_UBO_FREE_SAFE to GPU 2024-12-11 00:43:32 +01:00
Clément Foucault
994c43413a Metal: Remove SSBO Vertex Fetch
This API was used as a workaround to the lack of
geometry shader. It has been rendered redundant
since the introduction of #125782.
2024-12-05 22:58:52 +01:00
Clément Foucault
00a8d006fe GPU: Move Polyline shader to primitive expansion
This port is not so straightforward.

This shader is used in different configurations and is
available to python bindings. So we need to keep
compatibility with different attributes configurations.

This is why attributes are loaded per component and a
uniform sets the length of the component.

Since this shader can be used from both the imm and batch
API, we need to inject some workarounds to bind the buffers
correctly.

The end result is still less versatile than the previous
metal workaround (i.e.: more attribute fetch mode supported),
but it is also way less code.

### Limitations:
The new shader has some limitation:
- Both `color` and `pos` attributes need to be `F32`.
- Each attribute needs to be 4byte aligned.
- Fetch type needs to be `GPU_FETCH_FLOAT`.
- Primitive type needs to be `GPU_PRIM_LINES`, `GPU_PRIM_LINE_STRIP` or `GPU_PRIM_LINE_LOOP`.
- If drawing using an index buffer, it must contain no primitive restart.

Rel #127493

Co-authored-by: Jeroen Bakker <jeroen@blender.org>
Pull Request: https://projects.blender.org/blender/blender/pulls/129315
2024-11-27 17:37:04 +01:00
Hans Goudey
9b97ba1462 Cleanup: GPU: Avoid raw pointers for shader API strings
Avoid measuring the length of strings repeatedly by passing their
length along with their data with `StringRefNull`. Null termination
seems to be necessary still for passing the shader sources to OpenGL.
Though I doubt this is a bottleneck, it's still nice to avoid overhead from
string operations and this helps move in that direction.

Pull Request: https://projects.blender.org/blender/blender/pulls/127702
2024-11-01 20:00:31 +01:00
Clément Foucault
9ee81411bf GPU: Multithread compilation of shaders during tests 2024-11-01 18:57:22 +01:00
Clément Foucault
324517fd78 Cleanup: GPU: Fix clang tidy warnings
Removes some other things like:
- `TRUST_NO_ONE` which was the same as `#ifndef NDEBUG`.
- Replace `reinterpret_cast` by `unwrap`

Pull Request: https://projects.blender.org/blender/blender/pulls/129631
2024-10-31 15:18:29 +01:00
Clément Foucault
9c0321ae9b Metal: Simplify MSL translation
Move most of the string preprocessing used for MSL
compatibility to `glsl_preprocess`.

Enforce some changes like matrix constructor and
array constructor to the GLSL codebase. This is
for C++ compatibility.

Additionally reduce the amount of code duplication
inside the compatibility code.

Pull Request: https://projects.blender.org/blender/blender/pulls/128634
2024-10-07 12:54:10 +02:00
Miguel Pozo
ba2faf4285 Workaround: Allow patching CreateInfos at runtime
Temporary workaround for Overlay Next.

Pull Request: https://projects.blender.org/blender/blender/pulls/128452
2024-10-04 19:06:37 +02:00
Campbell Barton
c6fd26a3f5 Cleanup: spelling in comments 2024-10-01 09:59:33 +10:00
Jason Fielder
eb3fe75392 Metal: Add support for parallel compilation and precompilation specialisation
This speeds up EEVEE startup and material compilation time.

Authored by Apple: James McCarthy
Pull Request: https://projects.blender.org/blender/blender/pulls/125657
2024-09-30 11:21:28 +02:00
Jeroen Bakker
a407186dbf GPU: Make shader cache clearing backend independent
Parallel shader compilation introduced `GPU_shader_cache_dir_clear_old`.
The implementation was specific to OpenGL and could not be overwritten
by other backends. This PR improves the implementation so the backend
can have its own implementation.

This is needed for upcoming changes to the Vulkan backend where we
want to use similar mechanisms to speed up shader compilation and caching.

Pull Request: https://projects.blender.org/blender/blender/pulls/127680
2024-09-16 14:03:14 +02:00
Miguel Pozo
33005ad716 GPU: Non-blocking specialization constants compilation
Update the batch specializations compilation to allow using it in an
async way.

The implementation has 2 main limitations:
- Only one batch at a time can be processed, extra batches will be
  added to a queue.
- Binding a specialization variant that is still being compiled will fail.

Pull Request: https://projects.blender.org/blender/blender/pulls/123015
2024-06-20 18:02:44 +02:00
Miguel Pozo
22652b305e GPU: Add GPU_shaders_precompile_specializations
Allow precompiling specialization constants variations in parallel.
Only supported in OpenGL as the rest of the batch compilation API,
on the other backends the function is a no-op.
This also moves the `SpecializationConstant` from
`gpu_shader_create_info` (private API) into`GPU_common_types`
(public API).

Pull Request: https://projects.blender.org/blender/blender/pulls/122796
2024-06-07 18:45:31 +02:00
Miguel Pozo
74224b25a5 GPU: Add GPU_shader_batch_create_from_infos
This is the first commit of the several required to support
subprocess-based parallel compilation on OpenGL.
This provides the base API and implementation, and exposes the max
subprocesses setting on the UI, but it's not used by any code yet.

More information and the rest of the code can be found in #121925.

This one includes:
- A new `GPU_shader_batch` API that allows requesting the compilation
  of multiple shaders at once, allowing GPU backed to compile them in
  parallel and asynchronously without blocking the Blender UI.
- A virtual `ShaderCompiler` class that backends can use to add their
  own implementation.
- A `ShaderCompilerGeneric` class that implements synchronous/blocking
  compilation of batches for backends that don't have their own
  implementation yet.
- A `GLShaderCompiler` that supports parallel compilation using
  subprocesses.
- A new `BLI_subprocess` API, including IPC (required for the
  `GLShaderCompiler` implementation).
- The implementation of the subprocess program in
  `GPU_compilation_subprocess`.
- A new `Max Shader Compilation Subprocesses` option in
  `Preferences > System > Memory & Limits` to enable parallel shader
  compilation and the max number of subprocesses to allocate (each
  subprocess has a relatively high memory footprint).

Implementation Overview:
There's a single `GLShaderCompiler` shared by all OpenGL contexts.
This class stores a pool of up to `GCaps.max_parallel_compilations`
subprocesses that can be used for compilation.
Each subprocess has a shared memory pool used for sending the shader
source code from the main Blender process and for receiving the already
compiled shader binary from the subprocess. This is synchronized using
a series of shared semaphores.
The subprocesses maintain a shader cache on disk inside a
`BLENDER_SHADER_CACHE` folder at the OS temporary folder.
Shaders that fail to compile are tried to be compiled again locally for
proper error reports.
Hanged subprocesses are currently detected using a timeout of 30s.

Pull Request: https://projects.blender.org/blender/blender/pulls/122232
2024-06-05 18:45:57 +02:00
Hans Goudey
fe76d8c946 Refactor: Remove unnecessary C wrappers for vertex and index buffers
Now that all relevant code is C++, the indirection from the C struct
`GPUVertBuf` to the C++ `blender::gpu::VertBuf` class just adds
complexity and necessitates a wrapper API, making more cleanups like
use of RAII or other C++ types more difficult.

This commit replaces the C wrapper structs with direct use of the
vertex and index buffer base classes. In C++ we can choose which parts
of a class are private, so we don't risk exposing too many
implementation details here.

Pull Request: https://projects.blender.org/blender/blender/pulls/119825
2024-03-24 16:38:30 +01:00
Hans Goudey
8b514bccd1 Cleanup: Move remaining GPU headers to C++
Pull Request: https://projects.blender.org/blender/blender/pulls/119807
2024-03-23 01:24:18 +01:00