61 Commits

Author SHA1 Message Date
Thaddeus Crews
207a2b6472
Core: Integrate warning suppression macro helpers 2025-04-03 10:13:46 -05:00
Thaddeus Crews
f25fc34439
SCons: Add CPPEXTPATH for external includes 2025-04-02 07:29:08 -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
Stuart Carnie
e2066298d9 Renderer: Fix Metal handling of cube textures; assert equal dimensions 2025-03-19 11:52:59 +11:00
Stuart Carnie
2123368dca Metal: Use reference, so we're not copying every frame 2025-03-13 07:15:59 +11:00
Thaddeus Crews
713a1ba554
Style: Standardize Obj-C #import syntax 2025-03-08 09:28:34 -06:00
Thaddeus Crews
324512e11c
Style: Replace header guards with #pragma once 2025-03-07 17:33:47 -06:00
Thaddeus Crews
ec5e096388
Merge pull request #103645 from stuartcarnie/fix_101696_pt_2
Metal: Use `p_set_index` when binding uniforms, to use correct data
2025-03-06 16:36:12 -06: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
Stuart Carnie
a4fb68f43e Metal: Use p_set_index when binding uniforms, to use correct data 2025-03-06 06:29:49 +11:00
Thaddeus Crews
59d75a704e
Merge pull request #103613 from stuartcarnie/fix_101696
Metal: Use uniform set index passed by `RenderingDevice`
2025-03-05 12:08:11 -06:00
Thaddeus Crews
aef8ed2901
Merge pull request #87388 from clayjohn/Vulkan-limit-add
Implement `LIMIT_MAX_COMPUTE_SHARED_MEMORY_SIZE` to `limit_get` in the Vulkan backend
2025-03-05 12:08:00 -06:00
Pāvels Nadtočajevs
76e2e9f81f [Metal] Add missing stage info to shader description. 2025-03-05 09:06:06 +02:00
Stuart Carnie
2b8cb36434 Metal: Use uniform set index passed by RenderingDevice
This does not resolve the errors noted in #101696, as the project is
incorrectly binding incompatible uniform set descriptors, as set 0
and set 1 have a single `readonly` image and set 2 has a single
`writeonly` texture. The `RenderingDevice` is reporting the errors with
Metal, as it uses a different `_reflect_spirv`, which correctly
determines the read / write attribute. A separate PR will be required
to fix the shared `_reflect_spirv`

Helps #101696
2025-03-05 17:52:52 +11:00
clayjohn
338c12fc9a Implement LIMIT_MAX_COMPUTE_SHARED_MEMORY_SIZE to limit_get in all Rendering backends.
Also add a more helpful warning that is only displayed in dev builds to
match the D3D12 backend
2025-03-03 21:43:12 -08:00
Stuart Carnie
5312811c4d Metal: Fix SPIR-V → MSL compilation on iOS targets 2025-02-27 10:22:39 +11:00
Stuart Carnie
818afcb327 Metal: Compile MTLLibrary on demand when pipeline is created
This changes the default shader loading strategy, implemented in the
Metal driver, to compile the `MTLLibrary` on demand when the pipeline
is created, which reduces cold startup time on IPHONE target OSs.

Normally, the `MTLLibrary` is compiled from Metal source asynchronously
when Godot calls
`RenderingDeviceDriverMetal::shader_create_from_bytecode`; however, this
changes this behaviour on mobile platforms to do it on demand when the
pipeline is created, as noted in #96052, Godot will ask to create
many more shaders from bytecode than are initially required. Mobile
OSs like iOS are limited to compiling to shader libraries concurrently,
which results in a significant bottleneck.

This is not the default for macOS, as it can concurrently compile many
shaders at once, resulting in faster startup times for the Godot editor.
2025-02-25 05:32:38 +11:00
clayjohn
35100396e4 Validate varying count when compiling shaders
This avoids crashing on devices when a number of varyings greater than the device limit is used.

For now this accurately prints an error when compiling the shader, but the error text only pops up in the editor if the number of user varyings is above the limit.
2025-02-13 15:07:15 -08:00
Matias N. Goldberg
fcd785ace2 Add ASTC HDR format variants 2025-02-13 11:45:42 -03:00
Stuart Carnie
01b4fd3522 Metal: enable GPU buffer address support 2025-01-16 06:10:50 +11:00
thimenesup
3d92f406b2 Implement Buffer Device Address for Rendering Device Vulkan and DirectX12 2025-01-13 22:43:29 -08:00
Stuart Carnie
9fc39ae321 Metal: Use retained references; shared pixel format code
Most important is a fix for an occasional crash due to a use-after-free
bug.

A number of API availability declarations were updated to include tvOS.

The code is now simplified and generic for all platforms, which makes
way for future tvOS support.

Co-authored-by: A Thousand Ships <96648715+AThousandShips@users.noreply.github.com>
2025-01-12 07:11:22 +11:00
Rémi Verschelde
03c83cea1e Style: Enable clang-format's InsertBraces config
This was part of our style guide since we started using clang-format
but the feature was only added in clang-format 15, and we hadn't
noticed it yet.
2025-01-09 16:46:51 +01:00
Aarni Koskela
f134769506 Fix various typos
* Add TODO notes for typos that should be fixed for 5.0

Co-authored-by: Rémi Verschelde <rverschelde@gmail.com>
2025-01-08 14:47:42 +02: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
Thaddeus Crews
e06d83860d
Style: Enforce AllowShortFunctionsOnASingleLine 2025-01-02 10:09:41 -06:00
Stuart Carnie
b643599749 Metal: Fix crash when uniform set is empty for slot binding mode
Co-authored-by: Hugo Locurcio <hugo.locurcio@hugo.pro>
2024-12-24 07:53:21 -07:00
Stuart Carnie
952cd796ff Metal: Support Apple4 GPUs (2017 era iOS devices)
Closes #99682
2024-12-18 09:14:38 -07: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
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
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
70dae45dd0
Merge pull request #99905 from stuartcarnie/fix_external_texture
Metal: Ensure `texture_create_from_extension` returns correct pixel format
2024-12-03 14:40:55 -06:00
Stuart Carnie
7caa039183
Metal: Ensure texture_create_from_extension returns correct pixel format 2024-12-03 05:45:50 +11:00
mrsaturnsan
627d10e4c4 Fix vsync on macOS getting disabled when using afterMinimumDuration 2024-11-26 15:38:32 -06:00
Thaddeus Crews
2dbf195af5
Merge pull request #98983 from nikitalita/patch-2
Prevent stack-use-after-scope in rendering_device_driver_metal.mm
2024-11-18 09:23:33 -06:00
Stuart Carnie
9c2ca820f2
Metal: Ensure position invariance is captured from SPIRV-Cross
Closes #99029
2024-11-15 14:06:32 +11:00
mrsaturnsan
376c6c0c7d Use afterMinimumDuration to correct frame pacing 2024-11-12 21:19:38 -06:00
nikitalita
78895c709c
Prevent stack-use-after-scope in rendering_device_driver_metal.mm 2024-11-09 00:07:33 -06:00
Stuart Carnie
3dac3887e9
Metal: Multiview support
* Adds support for multiview
* Returns native handles for more driver resources
2024-11-06 05:37:35 +11:00
Adam Scott
0d350e7108
Set clang-format RemoveSemicolon rule to true
- Set clang-format `Standard` rule to `c++20`
2024-10-25 13:49:43 -04:00
Stuart Carnie
83ac274e25
Metal: Performance improvements and bug fixes 2024-10-20 11:15:13 +11:00
Clay John
e7c39efdb1
Merge pull request #97309 from kroketio/metal-expose-device-handle
Metal: expose MTLDevice
2024-10-07 11:00:53 -06:00
Rémi Verschelde
f032af7453
Merge pull request #97304 from kroketio/metal-return-logical-device-handle
Metal: Implement `texture_create_from_extension`
2024-10-04 12:58:38 +02:00
Rémi Verschelde
a4c1804cab
Merge pull request #97483 from akien-mga/clang-format-19.1.0
CI: Update `clang-format` pre-commit hook to 19.1.0
2024-09-26 12:46:04 +02:00
Rémi Verschelde
c92a6c7e27
CI: Update clang-format pre-commit hook to 19.1.0 2024-09-26 11:46:12 +02:00
Thaddeus Crews
9f9ee0c813
SCons: Add unobtrusive type hints in SCons files 2024-09-25 09:34:35 -05:00
Sander
e9c85be76f Metal: expose DRIVER_RESOURCE_LOGICAL_DEVICE for get_resource_native_handle() 2024-09-22 03:19:06 +03:00
Sander
0ad1820b1a Metal: implement texture_create_from_extension
Parameters p_type, p_format, p_array_layers, p_depth_stencil are
ignored - MTLTexture (and the callee) already have this information
and is only relevant when reinterpreting or remaping the texture in
different ways.
2024-09-22 02:32:37 +03:00
Stuart Carnie
e826ab9ba9
[2D,Metal]: Fix subpixel blending; fix inconsistent blend state in Metal 2024-09-20 15:16:31 +10:00