Commit Graph

150 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
596062329d GPU: Improve shader dependency logger
Add shader name to graph.
Strip CLOG prefix for mermaid lines.
2025-09-29 16:31:47 +02:00
Clément Foucault
36151962d6 Cleanup: GL: Remove unused AMD workaround
_No response_

Pull Request: https://projects.blender.org/blender/blender/pulls/146404
2025-09-17 14:23:32 +02:00
Clément Foucault
680fec144c Cleanup: GPU: Remove prefix 'e' from enum types
_No response_

Pull Request: https://projects.blender.org/blender/blender/pulls/146034
2025-09-15 15:11:02 +02:00
Clément Foucault
f729e639f4 GPU: Simplify Codegen dependency injection
Dependencies were previously merged manually
inside the generated_sources by EEVEE.
This caused issues with double includes.

Instead, we now only gather the name of the
nodetree dependencies and add them to the
dependencies of the `GeneratedSource`.

This also make the compositor use the `GeneratedSource`
mechanism.

Pull Request: https://projects.blender.org/blender/blender/pulls/146106
2025-09-12 16:07:55 +02:00
Clément Foucault
e19e9e57ee EEVEE: Split eevee_shader_shared.hh into multiple files
The goal of this patch is to reduce final shader code footprint to
hopefully reduce shader compile time (see #145347).

This also contains a pass over most shader file to remove unused
include or use more granular ones to reduce final shader code
length.

Testing with the same setup as #145347:
|          | main (ms) |  PR (ms) |   Delta (ms) |
| -------- | ----------- | ------------ |------------ |
| Nvidia   |         257 |  207 (1.24x) |         50 |
| Mesa AMD |         323 |  295 (1.09x)    |        28 |

In barbershop test scene however the saving are not so noticeable:
|          | main (s) |  PR (s) |   Delta (s) |
| -------- | ----------- | ------------ |------------ |
| Nvidia (OpenGL)  |         40 |  39 (1.02x) |         1 |
| Nvidia (Vulkan)   |         29 |  29 (1.0x) |         0 |

Pull Request: https://projects.blender.org/blender/blender/pulls/145803
2025-09-12 14:09:35 +02:00
luz paz
81c8b22eeb Cleanup: Fix typos in source/blender/gpu
Fix some non-user-facing typos.
Found via codespell

Pull Request: https://projects.blender.org/blender/blender/pulls/145815
2025-09-09 09:07:23 +02:00
Miguel Pozo
0cf4d39aa9 GPU: Improve the do_static_compilation_ check coverage (2)
162a24e05d had to be reverted, since it
didn't take into account other types of dynamically generated
`ShaderCreateInfo` (external shaders like OCIO or Python ones).
This just marks `ShaderCreateInfo`s as generated by default and only
sets the ones from gpu_shader_create_info_list as non generated.

Pull Request: https://projects.blender.org/blender/blender/pulls/145128
2025-08-27 20:17:04 +02:00
Miguel Pozo
9f59a36181 Revert "GPU: Improve the do_static_compilation_ check coverage"
This reverts commit 162a24e05d.

Needs testing.
There are asserts triggering on startup.
2025-08-25 16:31:43 +02:00
Miguel Pozo
162a24e05d GPU: Improve the do_static_compilation_ check coverage
Since we use StaticShader now in many places, the check is often
bypassed.

Ensure that any compiled shader is either marked as static or as a
GPUPass.

Pull Request: https://projects.blender.org/blender/blender/pulls/144950
2025-08-25 15:55:26 +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
YimingWu
37df7ea850 Fix #137429: GPU: Allow missing attribute in pygpu_shader_attrs_info_get
In `pygpu_shader_attrs_info_get`, it tries to check information for all
vertex attributes that are added via `VERTEX_IN`, however some drivers
will optimize compiled shaders so some vertex attributes that are not
used will be removed. This fix makes sure that the input length that
is used in `GPU_shader_get_attribute_len` does not exceed actual max
binding number.

Pull Request: https://projects.blender.org/blender/blender/pulls/137584
2025-07-17 15:54:12 +02:00
Clément Foucault
0b5b1932a0 Merge branch 'blender-v4.5-release' 2025-06-16 11:02:55 +02:00
Clément Foucault
7da81b5f39 Fix #140450: Vulkan: profile.json files appears in home directory
Was caused by the shader compilation profiling not checking
for the profiler being enabled.
2025-06-16 11:02:18 +02:00
Miguel Pozo
5459d11ed9 Merge branch 'blender-v4.5-release' 2025-06-10 17:25:37 +02:00
Miguel Pozo
fca4a16975 Fix: GPU: GPUWorker lock
Prevent race conditions caused by calling `GPUWorker::wake_up` when the
worker is not waiting.
Found to be an issue in #139627, since `wake_up` is likely to be called
before the thread has fully started.

Pull Request: https://projects.blender.org/blender/blender/pulls/139842
2025-06-10 17:24:18 +02:00
Clément Foucault
a5dc8026ee GPU: Shader: Add runtime dependency resolution
This allows to generate source file that will
be injected in a predefined source dependance tree.

This allow much cleaner shader workflow where
all sources are explicitly referenced from the
main source file.

Pull Request: https://projects.blender.org/blender/blender/pulls/140047
2025-06-10 17:22:58 +02:00
Clément Foucault
368a64f386 GPU: Shader: Add support for shader entry points
This works by wrapping the entry point call inside a
`main` function.

Since resources are still defined in global space,
function accessing these are marked with a custom
attribute. This custom attribute expands in a
`#ifdef` guard for the matching stage.
This is a temporary solution and will eventually
be lifted once we support SRD.

### TODO
- [ ] Implement `[[gpu::vertex/fragment_function]]`.

Pull Request: https://projects.blender.org/blender/blender/pulls/139233
2025-06-10 12:37:20 +02:00
Clément Foucault
023865b314 Vulkan: Add CPU profiling
This has limited use cases since it doesn't
profile the heavy part of the vulkan backend.

Almost 1:1 port of the metal implementation from #139551.
Doesn't cover rendergraph submission nor GPU timings.

Pull Request: https://projects.blender.org/blender/blender/pulls/139899
2025-06-06 14:39:51 +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
Clément Foucault
8bba1c8056 Fix #139191: GPU: Blender crashes on startup with addons that use pyGPU shader
This was caused by missing `shader->constants`.
Creating an empty `SpecializationConstants` fixes the issue.
2025-05-26 11:09:49 +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
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
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
Miguel Pozo
992e7c95a7 GPU: Converge ShaderCompiler implementations
Part of #136993.

Share as much of the ShaderCompiler implementations as possible.
Remove the ShaderCompiler/ShaderCompilerGeneric split and make most of
its functions non virtual.
Move the `get_compiler` function from `Context` to `GPUBackend` and
creation/deletion to `GPUBackend::init/delete_resources`.
Add a `batch_cancel` function to `ShaderCompiler` (needed for the
GPUPass refactor).

As a nice extra, the multithreaded OpenGL compilation has become faster
too.
The barbershop materials + EEVEE static shaders have gone from 27s to
22s.

I have not observed any performance difference on Vulkan or Metal.

Pull Request: https://projects.blender.org/blender/blender/pulls/136676
2025-05-08 18:16:47 +02:00
Clément Foucault
58d32b482b Fix #138354: GPU: Race condition with SRGB builtin uniform
Multiple threads would be setting the globals
`g_shader_builtin_srgb_transform` and
`g_shader_builtin_srgb_is_dirty`.

These are use for color management inside the builtin
shaders. But the render thread could modify these
values even if its shader have no use of these.

The fix is to move these globals to the `gpu::Context`
class. This way we remove the race condition.
2025-05-07 16:43:42 +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
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
Miguel Pozo
a5ed5dc4bf GPU: Support deferred compilation in ShaderCompilerGeneric
Update the `ShaderCompilerGeneric` to support deferred compilation
using the batch compilation API, so we can get rid of
`drw_manager_shader`.
This approach also allows supporting non-blocking compilation
for static shaders.

This shouldn't cause any behavior changes at the moment, since batch
compilation is not yet used when parallel compilation is disabled.

This adds a `GPUWorker` and a `GPUSecondaryContext` as an easy to use
wrapper for managing secondary GPU contexts.

(Part of #133674)
Pull Request: https://projects.blender.org/blender/blender/pulls/136518
2025-04-07 15:26:25 +02:00
Clément Foucault
3179cb0069 Cleanup: GPU: Remove unused legacy_resource_location 2025-04-04 18:21:52 +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
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
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
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
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
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
Miguel Pozo
e24eadbb42 GPU: Add assert_framebuffer_shader_compatibility
Ensure all the framebuffer color attachments are written to.

Pull Request: https://projects.blender.org/blender/blender/pulls/130995
2024-12-10 17:13:06 +01:00
Clément Foucault
3e9a2fbf23 Cleanup: Formating 2024-12-05 23:17:44 +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
Clément Foucault
93a10cbf7e GPU: Enable printf only between render boundaries and use a stack
Printf buffer read needs to be inside render boundaries
to work. Since render boundaries can be nested, use a stack.

Fixes assert when quitting blender.
2024-11-08 20:01:08 +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
191b347f58 Cleanup: GPU: Use function parameters instead of template 2024-10-12 14:21:51 +02:00
Clément Foucault
1c0247c871 Cleanup: GPU: Preprocessor: Use report callback on processing functions 2024-10-11 18:52:34 +02:00