X Tutup
Skip to content

Resolve depth buffer in mobile renderer when required#78598

Merged
Repiteo merged 1 commit intogodotengine:masterfrom
BastiaanOlij:resolve_depth_buffer_mobile
Nov 20, 2025
Merged

Resolve depth buffer in mobile renderer when required#78598
Repiteo merged 1 commit intogodotengine:masterfrom
BastiaanOlij:resolve_depth_buffer_mobile

Conversation

@BastiaanOlij
Copy link
Contributor

This PR fixes a long standing issue with the mobile renderer when using MSAA. The subpass logic never had an option for resolving the MSAA depth buffer to a normal depth buffer.
This meant that:

  • reading from depth texture broke as the depth texture being copied to the back buffer was empty
  • when using an external depth texture (XR) the depth texture wasn't populated

This PR does rely on the VK_KHR_depth_stencil_resolve extension. This has been core since Vulkan 1.2 and was a KHR extension in Vulkan 1.0/1.1 and may not be supported on all GPUs.

Todos:

  • consider whether we want a fallback solution when the resolve extension is not available or adjust the back buffer copy to read from the MSAA buffers
  • add check that the MSAA depth buffer and normal depth buffer have compatible formats to enable the resolve
  • implement fallback if the XR solution can't provide a compatible buffer

@BastiaanOlij BastiaanOlij added this to the 4.2 milestone Jun 23, 2023
@BastiaanOlij BastiaanOlij self-assigned this Jun 23, 2023
@BastiaanOlij BastiaanOlij force-pushed the resolve_depth_buffer_mobile branch from d4b4f44 to db2e8d9 Compare June 23, 2023 04:15
@BastiaanOlij
Copy link
Contributor Author

I need to do some more testing but I think this also solves #78544

@Saul2022
Copy link

I think there should be 100% a fallback as the mobile renderer works better on older pc and it also has to support mobile, which nowadays it vulkan support is not the best.

@BastiaanOlij
Copy link
Contributor Author

@Saul2022 agreed though I'd like to mark all the "todos" for future PRs. This is an important fix I'd like to get into 4.1 and the fix window closes Monday. As currently resolving doesn't work plus we have a number of validation errors that are solved by this, this is a step forward and shouldn't break anything that isn't already broken.

@clayjohn @akien-mga I'm marking this for review due to the above reason if you guys are ok with that.

@BastiaanOlij BastiaanOlij marked this pull request as ready for review June 24, 2023 01:30
@BastiaanOlij BastiaanOlij requested a review from a team as a code owner June 24, 2023 01:30
@akien-mga
Copy link
Member

As discussed on chat, let's not rush it now, but this is a good candidate for 4.1.1 once finalized and merged for 4.2.

@Calinou
Copy link
Member

Calinou commented Jun 28, 2023

Does this resolve #78785?

@BastiaanOlij
Copy link
Contributor Author

@Calinou I don't think so, but that is one I need to find time for

@LeandroSQ
Copy link

Is this still getting merged? I also got the same issue on my XR project so I wonder if there are any workarounds too.

@Stratapeum
Copy link

@BastiaanOlij Do you plan to update this one now that the subpasses merge PR got upstreamed?

@BastiaanOlij
Copy link
Contributor Author

@Stratapeum this got stuck because we have no fallback if the extension is not supported. I'm not sure if @DarioSamo is looking into this as resolving depth buffer has become important for other purposes.

@BastiaanOlij
Copy link
Contributor Author

cc @clayjohn is it worth reviving this as is or do you guys have other plans around the work that has recently been done?

@clayjohn
Copy link
Member

clayjohn commented May 8, 2025

cc @clayjohn is it worth reviving this as is or do you guys have other plans around the work that has recently been done?

I don't have any plans for working on this myself if that is what you are asking.

@BastiaanOlij BastiaanOlij force-pushed the resolve_depth_buffer_mobile branch from f12e564 to e46f61b Compare October 8, 2025 12:24

if (use_msaa && has_depth_texture_override && !supports_depth_resolve) {
// We don't have a fallback for this, See PR #111322
WARN_PRINT_ONCE("MSAA Depth buffer resolve is not supported on this platform.");
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I just added this warning in JIC, this would require completion of #111322 however see notes on that PR for the issues we're running into.

That said, on the devices where this is important, it seems that our Vulkan Extension is available so I feel this should not hold up merging.

SUPPORTS_BUFFER_DEVICE_ADDRESS,
SUPPORTS_IMAGE_ATOMIC_32_BIT,
SUPPORTS_VULKAN_MEMORY_MODEL,
SUPPORTS_FRAMEBUFFER_DEPTH_RESOLVE,
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am not sold on this name...

@BastiaanOlij
Copy link
Contributor Author

So far only tested on desktop (Nvidia 40xx), will do more testing, but it resolves depth just fine using the extension:
image

@BastiaanOlij BastiaanOlij force-pushed the resolve_depth_buffer_mobile branch from e46f61b to 9e5fad8 Compare October 8, 2025 12:37
@SpockBauru
Copy link
Contributor

SpockBauru commented Oct 9, 2025

I'm out of the loop for a while so tested for both 4.6-dev1 and this PR artifacts, using the Windows editor with the Android template.

For the both versions, the old Samsung Galaxy s10 (2019) and the Samsung Galaxy s25 seems to work with and without MSAA 4x. for 4.5-stable the depth buffer doens't work with MSAA 4x as expected.

Not sure why it works on 4.6-dev1 (Edit: just found #108636, that's awesome), but everything seems to run ok. Also tested my personal project, and shaders that depends on the depth buffer seems to work without issues.

Excellent job!

This was referenced Nov 5, 2025
@huisedenanhai
Copy link
Contributor

Bump for progress. This is required before we can enable MSAA on visionOS immersive rendering.

@clayjohn
Copy link
Member

clayjohn commented Nov 5, 2025

Bump for progress. This is required before we can enable MSAA on visionOS immersive rendering.

Are you sure? This is an optimization for certain GPUs, but doesn't change functionality

@huisedenanhai
Copy link
Contributor

huisedenanhai commented Nov 5, 2025

Are you sure? This is an optimization for certain GPUs, but doesn't change functionality

The only thing I need is a correctly resolved depth buffer. VisionOS needs depth output to display correctly. Here is an example showing what will happen if depth is not correctly configured.

Depth buffer is not correctly resolved in current implementation. I tried other workarounds, including force scene_state.used_depth_texture = true, call RenderingDevice::texture_resolve_multisample for depth buffer. They all don't work. I don't know how these methods behave on other platforms. I can only confirm they have some issue on metal.

So yes, there can be two way to fix this. One is fixing manual depth resolve on metal (for layered depth texture), the other one is implement hardware depth resolve for metal after this PR get merged.

It is very easy to implement depth resolve on metal, and I have a working implementation. Hardware depth resolve should be supported on all apple platforms current godot targets. Its support starts from A9 chip, used ten years ago in iPhone 6s. iPhones older than that does not support iOS 14, the min target iOS version of godot.

So implement hardware depth resolve is my goto solution. It is faster, more general, does not introduce platform specific code in forward mobile renderer. I don't see the value to fix manual depth resolve on metal.

I don't know why this PR is stalled from merging if this is merely an optimization. If you worry about extension coverage, this extension have more than 70% coverage on android, 100% coverage on iOS (moltenVK) if you check vulkan database.

@BastiaanOlij
Copy link
Contributor Author

@huisedenanhai indeed very important for VisionPro however the solution there is found inside the metal driver, not related to Vulkan.

@huisedenanhai
Copy link
Contributor

indeed very important for VisionPro however the solution there is found inside the metal driver, not related to Vulkan.

It is not related to vulkan. But the fix in metal driver needs new RenderingDevice depth attachment resolve APIs, which is introduced in this pr.

@BastiaanOlij
Copy link
Contributor Author

indeed very important for VisionPro however the solution there is found inside the metal driver, not related to Vulkan.

It is not related to vulkan. But the fix in metal driver needs new RenderingDevice depth attachment resolve APIs, which is introduced in this pr.

Ah, gotcha!

I got a few small changes left to do but we should be able to move on it quickly.

@BastiaanOlij BastiaanOlij force-pushed the resolve_depth_buffer_mobile branch from 9e5fad8 to 21ffbec Compare November 17, 2025 06:29
@BastiaanOlij BastiaanOlij requested review from a team as code owners November 17, 2025 06:29
@BastiaanOlij
Copy link
Contributor Author

@clayjohn I've added in the TEXTURE_USAGE_DEPTH_RESOLVE_ATTACHMENT_BIT change. As far as I can tell this is working as expected.

There are a few XR use cases waiting on this so it would be good if you could have a peek at it :)

Copy link
Member

@clayjohn clayjohn left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks great!

@clayjohn clayjohn modified the milestones: 4.x, 4.6 Nov 20, 2025
@Repiteo Repiteo merged commit 2edc43d into godotengine:master Nov 20, 2025
20 checks passed
@Repiteo
Copy link
Contributor

Repiteo commented Nov 20, 2025

Thanks!

@harrisyu
Copy link
Contributor

harrisyu commented Nov 22, 2025

With this PR, I got a lots of error in console output (Windows 11) :

Godot Engine v4.6.dev.custom_build.2edc43df8 (2025-11-20 17:10:48 UTC) - https://godotengine.org
Vulkan 1.4.312 - Forward+ - Using Device #0: NVIDIA - NVIDIA GeForce RTX 3060

ERROR: Format 'D24_Unorm_S8_Uint' does not support usage as storage image.
at: (servers\rendering\rendering_device.cpp:1033)
ERROR: Attempted to name invalid ID: 0
at: RenderingDevice::set_resource_name (servers\rendering\rendering_device.cpp:6360)
ERROR: Condition "p_named_texture.texture.is_null()" is true.
at: RenderSceneBuffersRD::update_sizes (servers\rendering\renderer_rd\storage_rd\render_scene_buffers_rd.cpp:82)
ERROR: Texture (binding: 3) should provide one ID referencing a texture (IDs provided: 0).
at: (servers\rendering\rendering_device.cpp:3724)
ERROR: Condition "named_texture.texture.is_null()" is true. Returning: RID()
at: RenderSceneBuffersRD::get_texture_slice_view (servers\rendering\renderer_rd\storage_rd\render_scene_buffers_rd.cpp:431)
ERROR: Image (binding: 0) should provide one ID referencing a texture (IDs provided: 0).
at: (servers\rendering\rendering_device.cpp:3769)
ERROR: Condition "rid.is_null()" is true. Returning: rid
at: UniformSetCacheRD::_allocate_from_uniforms (.\servers/rendering/renderer_rd/uniform_set_cache_rd.h:120)
ERROR: Parameter "uniform_set" is null.
at: RenderingDevice::compute_list_bind_uniform_set (servers\rendering\rendering_device.cpp:5364)
ERROR: Uniforms were never supplied for set (1) at the time of drawing, which are required by the pipeline.
at: (servers\rendering\rendering_device.cpp:5453)
ERROR: Condition "named_texture.texture.is_null()" is true. Returning: RID()
at: RenderSceneBuffersRD::get_texture_slice_view (servers\rendering\renderer_rd\storage_rd\render_scene_buffers_rd.cpp:431)
ERROR: Image (binding: 0) should provide one ID referencing a texture (IDs provided: 0).
at: (servers\rendering\rendering_device.cpp:3769)
ERROR: Condition "rid.is_null()" is true. Returning: rid
at: UniformSetCacheRD::_allocate_from_uniforms (.\servers/rendering/renderer_rd/uniform_set_cache_rd.h:120)
ERROR: Parameter "uniform_set" is null.
at: RenderingDevice::compute_list_bind_uniform_set (servers\rendering\rendering_device.cpp:5364)
ERROR: Uniforms were never supplied for set (1) at the time of drawing, which are required by the pipeline.
at: (servers\rendering\rendering_device.cpp:5453)
ERROR: Condition "named_texture.texture.is_null()" is true. Returning: RID()
at: RenderSceneBuffersRD::get_texture_slice_view (servers\rendering\renderer_rd\storage_rd\render_scene_buffers_rd.cpp:431)
ERROR: Image (binding: 0) should provide one ID referencing a texture (IDs provided: 0).
at: (servers\rendering\rendering_device.cpp:3769)

To reproduce:

  • Create empty project, create a 3d scene.
  • Create a MeshInstance3D node.
  • Create a BoxMesh.
  • Click the BoxMesh data resource to see the preview.
    Error with keep output in viewport.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

X Tutup