From 0cb0e9af9b6359630a3cf03d32ce9ac195566098 Mon Sep 17 00:00:00 2001 From: Swung0x48 Date: Sun, 20 Sep 2026 12:44:37 -0400 Subject: [PATCH] [MG_Backend] (Disaggregated): resolve multisample depth and stencil through renderpass2 --- .../DirectVulkan/Renderer/VulkanRenderer.cpp | 39 +++++ .../DirectVulkan/Renderer/VulkanRenderer.h | 6 + .../DirectVulkan/Renderer/WireFramebuffer.inc | 146 ++++++++++++++++++ .../DepthStencilReadbackMatrixScenario.cpp | 20 ++- 4 files changed, 209 insertions(+), 2 deletions(-) diff --git a/MobileGL/MG_Backend/DirectVulkan/Renderer/VulkanRenderer.cpp b/MobileGL/MG_Backend/DirectVulkan/Renderer/VulkanRenderer.cpp index c2828439..4aab0c74 100644 --- a/MobileGL/MG_Backend/DirectVulkan/Renderer/VulkanRenderer.cpp +++ b/MobileGL/MG_Backend/DirectVulkan/Renderer/VulkanRenderer.cpp @@ -14378,6 +14378,38 @@ void main() { getPhysicalDeviceProperties2 = reinterpret_cast( vkGetInstanceProcAddr(m_instance, "vkGetPhysicalDeviceProperties2KHR")); } +#if MOBILEGL_BUILD_DISAGGREGATED + // Linux/Android request Vulkan 1.1: a 1.2 physical device alone does + // not expose the promoted renderpass2/depth-resolve API to this app. +#ifdef VK_USE_PLATFORM_WIN32_KHR + const Bool wireDepthResolveCore = m_physicalDevice.properties.apiVersion >= VK_API_VERSION_1_2; +#else + const Bool wireDepthResolveCore = false; +#endif + const Bool wireDepthResolveExtensions = + m_physicalDevice.properties.apiVersion >= VK_API_VERSION_1_1 && + IsExtensionSupported(availableExtensions, VK_KHR_CREATE_RENDERPASS_2_EXTENSION_NAME) && + IsExtensionSupported(availableExtensions, VK_KHR_DEPTH_STENCIL_RESOLVE_EXTENSION_NAME); + const Bool wireDepthResolveEnabled = MG_Config::Transport != MG_Config::TransportMode::Monolith && + getPhysicalDeviceProperties2 && (wireDepthResolveCore || wireDepthResolveExtensions); + m_wireCreateRenderPass2 = nullptr; + m_wireDepthResolveModes = m_wireStencilResolveModes = 0; + if (wireDepthResolveEnabled) { + if (!wireDepthResolveCore) { + EnableOptionalDeviceExtension(availableExtensions, enabledDeviceExtensions, + VK_KHR_CREATE_RENDERPASS_2_EXTENSION_NAME); + EnableOptionalDeviceExtension(availableExtensions, enabledDeviceExtensions, + VK_KHR_DEPTH_STENCIL_RESOLVE_EXTENSION_NAME); + } + VkPhysicalDeviceDepthStencilResolveProperties resolveProperties{ + VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_DEPTH_STENCIL_RESOLVE_PROPERTIES}; + VkPhysicalDeviceProperties2 properties{VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_PROPERTIES_2}; + properties.pNext = &resolveProperties; + getPhysicalDeviceProperties2(m_physicalDevice.handle, &properties); + m_wireDepthResolveModes = resolveProperties.supportedDepthResolveModes; + m_wireStencilResolveModes = resolveProperties.supportedStencilResolveModes; + } +#endif if ((descriptorIndexingCore || descriptorIndexingExtension) && getPhysicalDeviceFeatures2 != nullptr && getPhysicalDeviceProperties2 != nullptr) { VkPhysicalDeviceFeatures2 featureQuery{}; @@ -14896,6 +14928,13 @@ void main() { m_shaderDrawParametersFeatureEnabled ? "true" : "false"); VK_VERIFY(vkCreateDevice(m_physicalDevice.handle, &deviceCreateInfo, nullptr, &m_device), "vkCreateDevice"); +#if MOBILEGL_BUILD_DISAGGREGATED + if (wireDepthResolveEnabled) { + m_wireCreateRenderPass2 = reinterpret_cast( + vkGetDeviceProcAddr(m_device, wireDepthResolveCore ? "vkCreateRenderPass2" : "vkCreateRenderPass2KHR")); + } +#endif + s_vkCmdDrawIndexedIndirectCount = reinterpret_cast( vkGetDeviceProcAddr(m_device, "vkCmdDrawIndexedIndirectCountKHR")); if (s_vkCmdDrawIndexedIndirectCount == nullptr) { diff --git a/MobileGL/MG_Backend/DirectVulkan/Renderer/VulkanRenderer.h b/MobileGL/MG_Backend/DirectVulkan/Renderer/VulkanRenderer.h index e6db9227..4fe7d092 100644 --- a/MobileGL/MG_Backend/DirectVulkan/Renderer/VulkanRenderer.h +++ b/MobileGL/MG_Backend/DirectVulkan/Renderer/VulkanRenderer.h @@ -469,6 +469,12 @@ namespace MobileGL::MG_Backend::DirectVulkan { const ClearAttachmentPayload& payload, GLint drawbuffer = -1); void BlitWireFramebuffers(GLint sx0, GLint sy0, GLint sx1, GLint sy1, GLint dx0, GLint dy0, GLint dx1, GLint dy1, GLbitfield mask, GLenum filter); + void ResolveWireDepthStencil(WireImage source, WireImage destination, + GLint sx0, GLint sy0, GLint sx1, GLint sy1, + GLint dx0, GLint dy0, GLint dx1, GLint dy1); + PFN_vkCreateRenderPass2 m_wireCreateRenderPass2 = nullptr; + VkResolveModeFlags m_wireDepthResolveModes = 0; + VkResolveModeFlags m_wireStencilResolveModes = 0; Bool BlitWireColorToDefault(WireImage source, WireImage destination, GLint sx0, GLint sy0, GLint sx1, GLint sy1, GLint dx0, GLint dy0, GLint dx1, GLint dy1, GLenum filter); diff --git a/MobileGL/MG_Backend/DirectVulkan/Renderer/WireFramebuffer.inc b/MobileGL/MG_Backend/DirectVulkan/Renderer/WireFramebuffer.inc index b0e7124d..07f1e427 100644 --- a/MobileGL/MG_Backend/DirectVulkan/Renderer/WireFramebuffer.inc +++ b/MobileGL/MG_Backend/DirectVulkan/Renderer/WireFramebuffer.inc @@ -267,6 +267,145 @@ MagmaWireFatal("read-conversion"); } + void VulkanRenderer::ResolveWireDepthStencil(WireImage source, WireImage destination, + GLint sx0, GLint sy0, GLint sx1, GLint sy1, GLint dx0, GLint dy0, GLint dx1, GLint dy1) { + const Int64 width = static_cast(sx1) - sx0; + const Int64 height = static_cast(sy1) - sy0; + if (!width || !height) return; + if (source.isDefault || destination.isDefault || source.is3D || destination.is3D || + width <= 0 || height <= 0 || width != static_cast(dx1) - dx0 || + height != static_cast(dy1) - dy0 || sx0 < 0 || sy0 < 0 || dx0 < 0 || dy0 < 0 || + static_cast(sx1) > source.extent.width || static_cast(sy1) > source.extent.height || + static_cast(dx1) > destination.extent.width || static_cast(dy1) > destination.extent.height) + MagmaWireFatal("multisample-depth-resolve-region"); + VkImageAspectFlags formatAspects = VK_IMAGE_ASPECT_DEPTH_BIT; + if (source.format == VK_FORMAT_S8_UINT) formatAspects = VK_IMAGE_ASPECT_STENCIL_BIT; + else if (source.format == VK_FORMAT_D16_UNORM_S8_UINT || source.format == VK_FORMAT_D24_UNORM_S8_UINT || + source.format == VK_FORMAT_D32_SFLOAT_S8_UINT) + formatAspects |= VK_IMAGE_ASPECT_STENCIL_BIT; + if (!m_wireCreateRenderPass2 || + ((formatAspects & VK_IMAGE_ASPECT_DEPTH_BIT) && !(m_wireDepthResolveModes & VK_RESOLVE_MODE_SAMPLE_ZERO_BIT)) || + ((formatAspects & VK_IMAGE_ASPECT_STENCIL_BIT) && !(m_wireStencilResolveModes & VK_RESOLVE_MODE_SAMPLE_ZERO_BIT))) + MagmaWireFatal("multisample-depth-resolve-capability"); + + // vkCmdResolveImage is color-only. A depth/stencil resolve subpass must + // write at the source coordinates and may require both aspects to use + // the same mode. Resolve both into owned scratch storage, then copy + // only the GL-requested aspect, preserving the other destination aspect. + auto scratch = MakeUnique(); + VkImageCreateInfo imageInfo{VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO}; + imageInfo.imageType = VK_IMAGE_TYPE_2D; + imageInfo.format = source.format; + imageInfo.extent = {source.extent.width, source.extent.height, 1}; + imageInfo.mipLevels = imageInfo.arrayLayers = 1; + imageInfo.samples = VK_SAMPLE_COUNT_1_BIT; + imageInfo.tiling = VK_IMAGE_TILING_OPTIMAL; + imageInfo.usage = VK_IMAGE_USAGE_DEPTH_STENCIL_ATTACHMENT_BIT | VK_IMAGE_USAGE_TRANSFER_SRC_BIT; + imageInfo.sharingMode = VK_SHARING_MODE_EXCLUSIVE; + VmaAllocationCreateInfo allocation{}; + allocation.usage = VMA_MEMORY_USAGE_AUTO_PREFER_DEVICE; + allocation.requiredFlags = VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT; + if (vmaCreateImage(m_allocator, &imageInfo, &allocation, &scratch->image, &scratch->allocation, nullptr) != VK_SUCCESS) + MagmaWireFatal("multisample-depth-resolve-allocation"); + scratch->format = source.format; + scratch->extent = source.extent; + scratch->aspect = formatAspects; + WireImage resolved{}; + resolved.image = scratch->image; + resolved.format = scratch->format; + resolved.extent = scratch->extent; + resolved.aspect = formatAspects; + resolved.trackedLayout = &scratch->layout; + TransitionWireImage(source, VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL); + TransitionWireImage(resolved, VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL); + + VkImageView views[2]{}; + VkImageViewCreateInfo viewInfo{VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO}; + viewInfo.image = source.image; + viewInfo.viewType = VK_IMAGE_VIEW_TYPE_2D; + viewInfo.format = source.format; + viewInfo.subresourceRange = {formatAspects, source.level, 1, source.layer, 1}; + VK_VERIFY(vkCreateImageView(m_device, &viewInfo, nullptr, &views[0])); + viewInfo.image = resolved.image; + viewInfo.subresourceRange = {formatAspects, 0, 1, 0, 1}; + VK_VERIFY(vkCreateImageView(m_device, &viewInfo, nullptr, &views[1])); + VkAttachmentDescription2 attachments[2]{}; + for (Uint32 i = 0; i < 2; ++i) { + auto& attachment = attachments[i]; + attachment.sType = VK_STRUCTURE_TYPE_ATTACHMENT_DESCRIPTION_2; + attachment.format = source.format; + attachment.samples = i == 0 ? source.samples : VK_SAMPLE_COUNT_1_BIT; + attachment.loadOp = attachment.stencilLoadOp = i == 0 ? VK_ATTACHMENT_LOAD_OP_LOAD : VK_ATTACHMENT_LOAD_OP_DONT_CARE; + attachment.storeOp = attachment.stencilStoreOp = VK_ATTACHMENT_STORE_OP_STORE; + attachment.initialLayout = attachment.finalLayout = VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL; + } + VkAttachmentReference2 sourceReference{VK_STRUCTURE_TYPE_ATTACHMENT_REFERENCE_2}; + sourceReference.attachment = 0; + sourceReference.layout = VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL; + sourceReference.aspectMask = formatAspects; + VkAttachmentReference2 resolveReference = sourceReference; + resolveReference.attachment = 1; + VkSubpassDescriptionDepthStencilResolve resolve{VK_STRUCTURE_TYPE_SUBPASS_DESCRIPTION_DEPTH_STENCIL_RESOLVE}; + resolve.depthResolveMode = (formatAspects & VK_IMAGE_ASPECT_DEPTH_BIT) ? VK_RESOLVE_MODE_SAMPLE_ZERO_BIT : VK_RESOLVE_MODE_NONE; + resolve.stencilResolveMode = (formatAspects & VK_IMAGE_ASPECT_STENCIL_BIT) ? VK_RESOLVE_MODE_SAMPLE_ZERO_BIT : VK_RESOLVE_MODE_NONE; + resolve.pDepthStencilResolveAttachment = &resolveReference; + VkSubpassDescription2 subpass{VK_STRUCTURE_TYPE_SUBPASS_DESCRIPTION_2}; + subpass.pNext = &resolve; + subpass.pipelineBindPoint = VK_PIPELINE_BIND_POINT_GRAPHICS; + subpass.pDepthStencilAttachment = &sourceReference; + VkRenderPassCreateInfo2 passInfo{VK_STRUCTURE_TYPE_RENDER_PASS_CREATE_INFO_2}; + passInfo.attachmentCount = 2; + passInfo.pAttachments = attachments; + passInfo.subpassCount = 1; + passInfo.pSubpasses = &subpass; + VkRenderPass pass = VK_NULL_HANDLE; + VK_VERIFY(m_wireCreateRenderPass2(m_device, &passInfo, nullptr, &pass)); + VkFramebufferCreateInfo framebufferInfo{VK_STRUCTURE_TYPE_FRAMEBUFFER_CREATE_INFO}; + framebufferInfo.renderPass = pass; + framebufferInfo.attachmentCount = 2; + framebufferInfo.pAttachments = views; + framebufferInfo.width = source.extent.width; + framebufferInfo.height = source.extent.height; + framebufferInfo.layers = 1; + VkFramebuffer framebuffer = VK_NULL_HANDLE; + VK_VERIFY(vkCreateFramebuffer(m_device, &framebufferInfo, nullptr, &framebuffer)); + VkRenderPassBeginInfo begin{VK_STRUCTURE_TYPE_RENDER_PASS_BEGIN_INFO}; + begin.renderPass = pass; + begin.framebuffer = framebuffer; + begin.renderArea = {{sx0, sy0}, {static_cast(width), static_cast(height)}}; + auto& frame = m_frameContext.GetCurrent(); + vkCmdBeginRenderPass(frame.commandBuffer, &begin, VK_SUBPASS_CONTENTS_INLINE); + vkCmdEndRenderPass(frame.commandBuffer); + TransitionWireImage(resolved, VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL); + TransitionWireImage(destination, VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL); + if (source.format == destination.format) { + VkImageCopy copy{}; + copy.srcSubresource = {source.aspect, 0, 0, 1}; + copy.srcOffset = {sx0, sy0, 0}; + copy.dstSubresource = {source.aspect, destination.level, destination.layer, 1}; + copy.dstOffset = {dx0, dy0, 0}; + copy.extent = {static_cast(width), static_cast(height), 1}; + vkCmdCopyImage(frame.commandBuffer, resolved.image, VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL, + destination.image, VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL, 1, ©); + } else { + // A logical GL depth format may use different native renderbuffer + // and texture formats. Re-encode only after producing real resolved + // samples. Keep all scratch objects local across this helper's flush. + if (!BlitDepthAcrossFormats(frame, resolved.image, resolved.format, &scratch->layout, 0, 0, + destination.image, destination.format, destination.trackedLayout, destination.level, destination.layer, + sx0, sy0, dx0, dy0, static_cast(width), static_cast(height), + VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL, VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL, + source.aspect == VK_IMAGE_ASPECT_STENCIL_BIT)) + MagmaWireFatal("multisample-depth-resolve-conversion"); + } + WireRetiredObjects retired{}; + retired.framebuffers.push_back(framebuffer); + retired.renderPasses.push_back(pass); + retired.imageViews = {views[0], views[1]}; + retired.textures.push_back(Move(scratch)); + RetireWireObjects(Move(retired)); + } + void VulkanRenderer::BlitWireFramebuffers(GLint sx0, GLint sy0, GLint sx1, GLint sy1, GLint dx0, GLint dy0, GLint dx1, GLint dy1, GLbitfield mask, GLenum filter) { RetireWireDrawPass(); @@ -314,6 +453,13 @@ WireImage destination = ResolveWireImage(*draw,dst,aspect); if (!source.image || !destination.image) return; m_textureManager->FlushPendingUploads(); + if (aspect != VK_IMAGE_ASPECT_COLOR_BIT && source.samples != VK_SAMPLE_COUNT_1_BIT && + destination.samples == VK_SAMPLE_COUNT_1_BIT) { + ResolveWireDepthStencil(source, destination, sx0, sy0, sx1, sy1, dx0, dy0, dx1, dy1); + if (dst.Kind == MG_Pipe::kMGPipeSurfaceKindTexture) + m_textureManager->MarkWireTextureGpuWritten(destination.storage, destination.level, destination.layer, 1); + return; + } if (aspect == VK_IMAGE_ASPECT_COLOR_BIT && source.samples != VK_SAMPLE_COUNT_1_BIT && destination.samples == VK_SAMPLE_COUNT_1_BIT) { // A resolve cannot flip axes or convert RGBA to a BGRA/default diff --git a/MobileGL/MG_IntegrationTest/Scenarios/DepthStencilReadbackMatrixScenario.cpp b/MobileGL/MG_IntegrationTest/Scenarios/DepthStencilReadbackMatrixScenario.cpp index 24c91944..afdd2f0e 100644 --- a/MobileGL/MG_IntegrationTest/Scenarios/DepthStencilReadbackMatrixScenario.cpp +++ b/MobileGL/MG_IntegrationTest/Scenarios/DepthStencilReadbackMatrixScenario.cpp @@ -362,13 +362,13 @@ namespace MGITest { GTEST_SKIP() << "this driver cannot host a 4x multisample DEPTH24_STENCIL8 renderbuffer"; } FirstGLError(); - ClearDepthStencil(GL_DEPTH24_STENCIL8, 0.875f, 0); + ClearDepthStencil(GL_DEPTH24_STENCIL8, 0.875f, 63); ASSERT_EQ(FirstGLError(), 0u); // The destination starts at a depth the resolve must overwrite everywhere. DepthSource resolved = MakeTextureSource(GL_DEPTH24_STENCIL8); ASSERT_TRUE(SourceIsUsable()); - ClearDepthStencil(GL_DEPTH24_STENCIL8, 0.125f, 0); + ClearDepthStencil(GL_DEPTH24_STENCIL8, 0.125f, 17); ASSERT_EQ(FirstGLError(), 0u); glBindFramebuffer(GL_READ_FRAMEBUFFER, multisampled.fbo); @@ -381,6 +381,22 @@ namespace MGITest { const std::vector depth = ReadDepthFloat(0, 0, kWidth, kHeight); EXPECT_EQ(FirstGLError(), 0u); ExpectAllDepth(depth, 0.875f, "resolved multisample depth"); + const std::vector stencil = ReadStencilInt(0, 0, kWidth, kHeight); + EXPECT_EQ(FirstGLError(), 0u); + ExpectAllStencil(stencil, 17, "depth-only resolve preserves destination stencil"); + + // Exercise stencil independently too: resolving a combined native image + // must not leak its depth into a stencil-only GL blit. + ClearDepthStencil(GL_DEPTH24_STENCIL8, 0.375f, 17); + glBindFramebuffer(GL_READ_FRAMEBUFFER, multisampled.fbo); + glBindFramebuffer(GL_DRAW_FRAMEBUFFER, resolved.fbo); + glBlitFramebuffer(0, 0, kWidth, kHeight, 0, 0, kWidth, kHeight, GL_STENCIL_BUFFER_BIT, GL_NEAREST); + ASSERT_EQ(FirstGLError(), 0u); + glBindFramebuffer(GL_FRAMEBUFFER, resolved.fbo); + ExpectAllStencil(ReadStencilInt(0, 0, kWidth, kHeight), 63, "resolved multisample stencil"); + ExpectAllDepth(ReadDepthFloat(0, 0, kWidth, kHeight), 0.375f, + "stencil-only resolve preserves destination depth"); + EXPECT_EQ(FirstGLError(), 0u); DestroySource(resolved); DestroySource(multisampled);