197 Commits

Author SHA1 Message Date
NetroScript
66d68707d3
Fix the VRS attachment being incorrectly added to color_attachments 2025-06-12 13:38:18 +02:00
clayjohn
cc9aedd898 Avoid crash when texture layers is greater than 1 and format is not an ARRAY type 2025-06-05 10:21:19 -07:00
Thaddeus Crews
de37627404
Merge pull request #102552 from DarioSamo/shader-baker
Add shader baker to project exporter.
2025-05-28 17:09:38 -05:00
Thaddeus Crews
90c75d02dd
Merge pull request #106732 from stuartcarnie/rd_reduce_allocations
Renderer: Eliminate `String` allocations for all labels in the renderer
2025-05-28 09:47:43 -05:00
Stuart Carnie
7d93119353 Renderer: Eliminates String allocations for all labels in the renderer
Uses `Span<char>` to avoid additional allocations in the graph.
2025-05-28 06:01:35 +10:00
Dario
5a30a7e7cd Add shader baker to project exporter.
Metal Support contributed by Migeran (https://migeran.com) and Stuart Carnie.

Co-authored-by: Stuart Carnie <stuart.carnie@gmail.com>
Co-authored-by: Gergely Kis <gergely.kis@migeran.com>
2025-05-27 12:45:27 -03:00
Lukas Tenbrink
4cb8a0c77e Add resize_initialized and resize_uninitialized to Vector. These functions serve as replacements for resize, to make sure the caller understands whether elements need to be initialized 'by hand' after the call. 2025-05-26 18:35:41 +02:00
Sander
6ae50cad17 RenderingDevice: introduce parameter 'mipmaps' for texture_create_from_extension() 2025-05-07 15:15:55 +03:00
lawnjelly
f8f350a32a Add GLOBAL_GET cached macros.
GLOBAL_GET is an expensive operation which should not be used each frame / tick.
This PR adds macros which do a cheaper revision check, and only call the expensive GLOBAL_GET when project settings have changed.

Co-authored-by: Lukas Tenbrink <lukas.tenbrink@gmail.com>
2025-04-30 15:08:50 +01:00
Lukas Tenbrink
bfdb0f0ecf Fix various sanitizer issues. 2025-04-28 15:23:35 +02:00
Yyf2333
1a70a06a43 Change RID_Owner::get_owned_list. 2025-04-21 01:04:27 +08:00
Thaddeus Crews
b743eb96e3
Merge pull request #98760 from ze2j/optimize_texture_create_shared_from_slice
Optimize shared texture creations
2025-04-18 12:21:25 -05:00
Thaddeus Crews
98aced4e65
Merge pull request #99768 from dsnopek/openxr-vulkan-foveated-rendering
OpenXR: Use the `XR_FB_foveation_vulkan` extension to get the density map for VRS
2025-04-17 09:14:23 -05:00
Thaddeus Crews
0d267e7b1e
Core: Add dedicated BitField template 2025-04-11 11:53:26 -05:00
A Thousand Ships
889410dcda
Add String::replace_char(s) methods for performance and convenience 2025-04-10 13:08:45 +02:00
Yufeng Ying
4f4031a675 Replace size() == 0 with is_empty(). 2025-04-02 19:18:43 +08:00
David Snopek
79f5a4d9fe OpenXR: Use the XR_FB_foveation_vulkan extension to get the density map for VRS 2025-03-31 07:21:58 -05:00
Rémi Verschelde
408d07109b
Merge pull request #99551 from DarioSamo/fragment-density-map
Implement Fragment density map support.
2025-03-28 14:31:19 +01:00
Dario
76d709be74 Implement support for fragment density maps.
Co-Authored-By: Bastiaan Olij <mux213@gmail.com>
2025-03-24 11:50:04 -05:00
Rémi Verschelde
92fb04c6cc
Merge pull request #103941 from stuartcarnie/metal_fx_features
Renderer: Expose and document `Features` enum for MetalFX
2025-03-21 13:03:19 +01:00
Stuart Carnie
e2066298d9 Renderer: Fix Metal handling of cube textures; assert equal dimensions 2025-03-19 11:52:59 +11:00
clayjohn
16b9667c62 Error when draw list is not active in draw_list_switch_to_next_pass
This fixes a mistake made when refactoring the draw list
2025-03-14 18:04:47 -07:00
Stuart Carnie
052fe9158d Renderer: Expose and document Features enum for METAL_FX 2025-03-11 12:35:58 +11:00
clayjohn
4cf9d58dce Clean up more dynamic allocations in the RD renderers with a focus on 2D. 2025-03-10 13:16:12 -07:00
ze2j
6413a8a94b Optimize shared texture creation
Texture::slice_trackers is now a pointer and is allocated on demand only when a shared texture is created.
This makes copying Texture significantly faster.
2025-03-10 20:16:50 +01:00
Thaddeus Crews
bf6031890e
Merge pull request #103814 from darksylinc/matias-update_perf_report-speedup
RenderingDevice: Delay expensive operations to `get_perf_report`
2025-03-09 09:05:24 -05:00
Matias N. Goldberg
21133425cc Delay expensive operations to get_perf_report
The function update_perf_report() is expensive and is called every
frame.
Most of it is not necessary unless the user calls get_perf_report

Affects #102173
2025-03-08 11:25:46 -03:00
clayjohn
5efcd64b23 Reduce per-frame memory allocations from the heap in the Mobile renderer. 2025-03-07 22:01:24 -08:00
Thaddeus Crews
4bafafaeb3
Merge pull request #102777 from darksylinc/matias-astc-hdr
Add ASTC HDR format variants
2025-03-06 16:36:06 -06:00
LuoZhihao
808c9fbcc5 RenderingDevice: Validate pre-raster (vertex) shader in render_pipeline_create 2025-03-03 05:19:55 +08:00
Matias N. Goldberg
fcd785ace2 Add ASTC HDR format variants 2025-02-13 11:45:42 -03:00
Thaddeus Crews
1939e87db1
Merge pull request #101561 from darksylinc/matias-device-address-api
Change how device address is requested to avoid future API breakage
2025-02-12 12:56:25 -06:00
Matias N. Goldberg
af900a5825 Change how device address is requested to avoid future API breakage
PR #100062 introduced BUFFER_USAGE_DEVICE_ADDRESS_BIT.

However it did so by adding a boolean to uniform_buffer_create(), called
"bool p_enable_device_address".

This makes maintaining backwards compatibility harder because I am
working on another feature that would require introducing yet another
bit flag.

This would save us the need to add fallback routines when the feature I
am working on makes it to Godot 4.5.

Even if my feature doesn't make it to 4.5 either, this PR makes the
routine more future-proof.

This PR also moves STORAGE_BUFFER_USAGE_DEVICE_ADDRESS into
BUFFER_CREATION_DEVICE_ADDRESS_BIT, since it's an option available to
both storage and uniforms.

This PR also moves the boolean use_as_storage into
BUFFER_CREATION_AS_STORAGE.
2025-02-11 20:00:18 -03:00
Dario
1bba9dbce9 Fix erroneous logic when flushes are involved on RD async methods. 2025-02-05 09:49:15 -03:00
thimenesup
3d92f406b2 Implement Buffer Device Address for Rendering Device Vulkan and DirectX12 2025-01-13 22:43:29 -08:00
Rémi Verschelde
62ea2f76b4
Merge pull request #101069 from DarioSamo/rd-thread-safety-comment
Remove TODO from RenderingDevice regarding thread safety.
2025-01-06 22:48:28 +01:00
Stuart Carnie
11dc4f2e5e Metal: Add MetalFX upscaling support
Co-authored-by: Hugo Locurcio <hugo.locurcio@hugo.pro>
2025-01-06 06:03:18 -07:00
Dario
dea083864b Remove TODO from RenderingDevice regarding thread safety. 2025-01-03 10:26:44 -03:00
A Thousand Ships
a1846b27ea
Improve use of Ref.is_null/valid
Use `is_null` over `!is_valid` and vice versa.
2024-12-23 16:35:02 -05:00
HP van Braam
062d74bb9c Fix ubsan reported errors in rendering
This allows the TPS demo to run without an ubsan reports from any of the
rendering code.
2024-12-13 21:27:37 +01:00
Thaddeus Crews
b7501d219c
Merge pull request #100322 from darksylinc/matias-tf-comment-fix
Fix grammar / spelling in comments
2024-12-12 16:13:25 -06:00
Matias N. Goldberg
a1b44ec7a7 Fix grammar / spelling in comments
Minor fixes for changes introduced in #99257 that could not be fixed in
time as the PR needed to be expedited.
2024-12-12 12:47:08 -03:00
Thaddeus Crews
23afda44e4
Merge pull request #100257 from darksylinc/matias-minimize-leak
Keep processing Graphics if there are pending operations
2024-12-11 17:35:35 -06:00
Dario
054891de04 Implement buffer_get_data_async and texture_get_data_async. 2024-12-11 11:55:23 -08:00
Matias N. Goldberg
acf439e96d Keep processing Graphics if there are pending operations
Fixes #90017
Fixes #90030
Fixes #98044

This PR makes the following changes:

# Force processing of GPU commands for frame_count frames

The variable `frames_pending_resources_for_processing` is added to track
this.

The ticket #98044 suggested to use `_flush_and_stall_for_all_frames()`
while minimized.

Technically this works and is a viable solution.

However I noticed that this issue was happening because Logic/Physics
continue to work "business as usual" while minimized(\*). Only Graphics
was being deactivated (which caused commands to accumulate until window
is restored).

To continue this behavior of "business as usual", I decided that GPU
work should also "continue as usual" by buffering commands in a double
or triple buffer scheme until all commands are done processing (if they
ever stop coming). This is specially important if the app specifically
intends to keep processing while minimized.

Calling `_flush_and_stall_for_all_frames()` would fix the leak, but it
would make  Godot's behavior different while minimized vs while the
window is presenting.

\* `OS::add_frame_delay` _does_ consider being minimized, but it just
throttles CPU usage. Some platforms such as Android completely disable
processing because the higher level code stops being called when the app
goes into background. But this seems like an implementation-detail that
diverges from the rest of the platforms (e.g. Windows, Linux & macOS
continue to process while minimized).

# Rename p_swap_buffers for p_present

**This is potentially a breaking change** (if it actually breaks
anything, I ignore. But I strongly suspect it doesn't break anything).

"Swap Buffers" is a concept carried from OpenGL, where a frame is "done"
when `glSwapBuffers()` is called, which basically means "present to the
screen".

However it _also_ means that OpenGL internally swaps its internal
buffers in a double/triple buffer scheme (in Vulkan, we do that
ourselves and is tracked by `RenderingDevice::frame`).

Modern APIs like Vulkan differentiate between "submitting GPU work" and
"presenting".

Before this PR, calling `RendererCompositorRD::end_frame(false)` would
literally do nothing. This is often undesired and the cause of the leak.
After this PR, calling `RendererCompositorRD::end_frame(false)` will now
process commands, swap our internal buffers in a double/triple buffer
scheme **but avoid presenting to the screen**.

Hence the rename of the variable from `p_swap_buffers` to `p_present`
(which slightly alters its behavior).
If we want `RendererCompositorRD::end_frame(false)` to do nothing, then
we should not call it at all.

This PR reflects such change: When we're minimized **_and_**
`has_pending_resources_for_processing()` returns false, we don't call
`RendererCompositorRD::end_frame()` at all.

But if `has_pending_resources_for_processing()` returns true, we will
call it, but with `p_present = false` because we're minimized.

There's still the issue that Godot keeps processing work (logic,
scripts, physics) while minimized, which we shouldn't do by default. But
that's work for follow up PR.
2024-12-11 14:13:29 -03:00
Matias N. Goldberg
c77cbf096b Improvements from TheForge (see description)
The work was performed by collaboration of TheForge and Google. I am
merely splitting it up into smaller PRs and cleaning it up.

This is the most "risky" PR so far because the previous ones have been
miscellaneous stuff aimed at either [improve
debugging](https://github.com/godotengine/godot/pull/90993) (e.g. device
lost), [improve Android
experience](https://github.com/godotengine/godot/pull/96439) (add Swappy
for better Frame Pacing + Pre-Transformed Swapchains for slightly better
performance), or harmless [ASTC
improvements](https://github.com/godotengine/godot/pull/96045) (better
performance by simply toggling a feature when available).

However this PR contains larger modifications aimed at improving
performance or reducing memory fragmentation. With greater
modifications, come greater risks of bugs or breakage.

Changes introduced by this PR:

TBDR GPUs (e.g. most of Android + iOS + M1 Apple) support rendering to
Render Targets that are not backed by actual GPU memory (everything
stays in cache). This works as long as load action isn't `LOAD`, and
store action must be `DONT_CARE`. This saves VRAM (it also makes
painfully obvious when a mistake introduces a performance regression).
Of particular usefulness is when doing MSAA and keeping the raw MSAA
content is not necessary.

Some GPUs get faster when the sampler settings are hard-coded into the
GLSL shaders (instead of being dynamically bound at runtime). This
required changes to the GLSL shaders, PSO creation routines, Descriptor
creation routines, and Descriptor binding routines.

 - `bool immutable_samplers_enabled = true`

Setting it to false enforces the old behavior. Useful for debugging bugs
and regressions.

Immutable samplers requires that the samplers stay... immutable, hence
this boolean is useful if the promise gets broken. We might want to turn
this into a `GLOBAL_DEF` setting.

Instead of creating dozen/hundreds/thousands of `VkDescriptorSet` every
frame that need to be freed individually when they are no longer needed,
they all get freed at once by resetting the whole pool. Once the whole
pool is no longer in use by the GPU, it gets reset and its memory
recycled. Descriptor sets that are created to be kept around for longer
or forever (i.e. not created and freed within the same frame) **must
not** use linear pools. There may be more than one pool per frame. How
many pools per frame Godot ends up with depends on its capacity, and
that is controlled by
`rendering/rendering_device/vulkan/max_descriptors_per_pool`.

- **Possible improvement for later:** It should be possible for Godot
to adapt to how many descriptors per pool are needed on a per-key basis
(i.e. grow their capacity like `std::vector` does) after rendering a few
frames; which would be better than the current solution of having a
single global value for all pools (`max_descriptors_per_pool`) that the
user needs to tweak.

 - `bool linear_descriptor_pools_enabled = true`

Setting it to false enforces the old behavior. Useful for debugging bugs
and regressions.
Setting it to false is required when workarounding driver bugs (e.g.
Adreno 730).

A ridiculous optimization. Ridiculous because the original code
should've done this in the first place. Previously Godot was doing the
following:

  1. Create a command buffer **pool**. One per frame.
  2. Create multiple command buffers from the pool in point 1.
3. Call `vkBeginCommandBuffer` on the cmd buffer in point 2. This
resets the cmd buffer because Godot requests the
`VK_COMMAND_POOL_CREATE_RESET_COMMAND_BUFFER_BIT` flag.
  4. Add commands to the cmd buffers from point 2.
  5. Submit those commands.
6. On frame N + 2, recycle the buffer pool and cmd buffers from pt 1 &
2, and repeat from step 3.

The problem here is that step 3 resets each command buffer individually.
Initially Godot used to have 1 cmd buffer per pool, thus the impact is
very low.

But not anymore (specially with Adreno workarounds to force splitting
compute dispatches into a new cmd buffer, more on this later). However
Godot keeps around a very low amount of command buffers per frame.

The recommended method is to reset the whole pool, to reset all cmd
buffers at once. Hence the new steps would be:

  1. Create a command buffer **pool**. One per frame.
  2. Create multiple command buffers from the pool in point 1.
3. Call `vkBeginCommandBuffer` on the cmd buffer in point 2, which is
already reset/empty (see step 6).
  4. Add commands to the cmd buffers from point 2.
  5. Submit those commands.
6. On frame N + 2, recycle the buffer pool and cmd buffers from pt 1 &
2, call `vkResetCommandPool` and repeat from step 3.

**Possible issues:** @dariosamo added `transfer_worker` which creates a
command buffer pool:

```cpp
transfer_worker->command_pool =
driver->command_pool_create(transfer_queue_family,
RDD::COMMAND_BUFFER_TYPE_PRIMARY);
```

As expected, validation was complaining that command buffers were being
reused without being reset (that's good, we now know Validation Layers
will warn us of wrong use).
I fixed it by adding:

```cpp
void RenderingDevice::_wait_for_transfer_worker(TransferWorker
*p_transfer_worker) {
	driver->fence_wait(p_transfer_worker->command_fence);
	driver->command_pool_reset(p_transfer_worker->command_pool); //
! New line !
```

**Secondary cmd buffers are subject to the same issue but I didn't alter
them. I talked this with Dario and he is aware of this.**
Secondary cmd buffers are currently disabled due to other issues (it's
disabled on master).

 - `bool RenderingDeviceCommons::command_pool_reset_enabled`

Setting it to false enforces the old behavior. Useful for debugging bugs
and regressions.

There's no other reason for this boolean. Possibly once it becomes well
tested, the boolean could be removed entirely.

Adds `command_bind_render_uniform_sets` and
`add_draw_list_bind_uniform_sets` (+ compute variants).

It performs the same as `add_draw_list_bind_uniform_set` (notice
singular vs plural), but on multiple consecutive uniform sets, thus
reducing graph and draw call overhead.

 - `bool descriptor_set_batching = true;`

Setting it to false enforces the old behavior. Useful for debugging bugs
and regressions.

There's no other reason for this boolean. Possibly once it becomes well
tested, the boolean could be removed entirely.

Godot currently does the following:

 1. Fill the entire cmd buffer with commands.
 2. `submit()`
    - Wait with a semaphore for the swapchain.
- Trigger a semaphore to indicate when we're done (so the swapchain
can submit).
 3. `present()`

The optimization opportunity here is that 95% of Godot's rendering is
done offscreen.
Then a fullscreen pass copies everything to the swapchain. Godot doesn't
practically render directly to the swapchain.

The problem with this is that the GPU has to wait for the swapchain to
be released **to start anything**, when we could start *much earlier*.
Only the final blit pass must wait for the swapchain.

TheForge changed it to the following (more complicated, I'm simplifying
the idea):

 1. Fill the entire cmd buffer with commands.
 2. In `screen_prepare_for_drawing` do `submit()`
    - There are no semaphore waits for the swapchain.
    - Trigger a semaphore to indicate when we're done.
3. Fill a new cmd buffer that only does the final blit to the
swapchain.
 4. `submit()`
    - Wait with a semaphore for the submit() from step 2.
- Wait with a semaphore for the swapchain (so the swapchain can
submit).
- Trigger a semaphore to indicate when we're done (so the swapchain
can submit).
 5. `present()`

Dario discovered this problem independently while working on a different
platform.

**However TheForge's solution had to be rewritten from scratch:** The
complexity to achieve the solution was high and quite difficult to
maintain with the way Godot works now (after Übershaders PR).
But on the other hand, re-implementing the solution became much simpler
because Dario already had to do something similar: To fix an Adreno 730
driver bug, he had to implement splitting command buffers. **This is
exactly what we need!**. Thus it was re-written using this existing
functionality for a new purpose.

To achieve this, I added a new argument, `bool p_split_cmd_buffer`, to
`RenderingDeviceGraph::add_draw_list_begin`, which is only set to true
by `RenderingDevice::draw_list_begin_for_screen`.

The graph will split the draw list into its own command buffer.

 - `bool split_swapchain_into_its_own_cmd_buffer = true;`

Setting it to false enforces the old behavior. This might be necessary
for consoles which follow an alternate solution to the same problem.
If not, then we should consider removing it.

PR #90993 added `shader_destroy_modules()` but it was not actually in
use.

This PR adds several places where `shader_destroy_modules()` is called
after initialization to free up memory of SPIR-V structures that are no
longer needed.
2024-12-09 11:49:28 -03:00
Thaddeus Crews
637239e979
Merge pull request #94193 from BastiaanOlij/buildin_includes
Add ability to include built-in include files
2024-12-05 14:12:23 -06:00
Rémi Verschelde
14ce5ba8a1
Merge pull request #94368 from CrazyRoka/optimize-render-pass-uniform-set
Optimize `RenderForwardClustered::_setup_render_pass_uniform_set` by reducing Vector allocations
2024-12-02 15:49:58 +01:00
CrazyRoka
d9ef826c54
Optimize RenderForwardClustered::_setup_render_pass_uniform_set by reducing Vector allocations during push_back operations 2024-12-02 15:03:50 +01:00
Joshua Staub
16ee2f22eb
Check device texture size limits in RenderingDevice::texture_create 2024-12-02 14:30:02 +01:00