From 19931c003f94da327b068f5b298134a1dca16eb3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Henrik=20Rydg=C3=A5rd?= Date: Mon, 1 Aug 2022 23:21:14 +0200 Subject: [PATCH 1/2] Clean up blit/copy feature detection. Don't need fast GPU flags for these. --- Common/GPU/D3D11/thin3d_d3d11.cpp | 1 + Common/GPU/D3D9/thin3d_d3d9.cpp | 23 ++++++++++++++++++----- Common/GPU/OpenGL/thin3d_gl.cpp | 4 +++- Common/GPU/Vulkan/thin3d_vulkan.cpp | 21 +++++++++++++++------ Common/GPU/thin3d.h | 3 +++ GPU/Common/FramebufferManagerCommon.cpp | 17 +++++++++-------- GPU/D3D11/GPU_D3D11.cpp | 1 - GPU/GLES/GPU_GLES.cpp | 7 ------- GPU/GLES/StencilBufferGLES.cpp | 2 +- GPU/GPUState.h | 6 ++---- GPU/Vulkan/GPU_Vulkan.cpp | 6 ------ 11 files changed, 52 insertions(+), 39 deletions(-) diff --git a/Common/GPU/D3D11/thin3d_d3d11.cpp b/Common/GPU/D3D11/thin3d_d3d11.cpp index 9d13d0789d..a9a922cf24 100644 --- a/Common/GPU/D3D11/thin3d_d3d11.cpp +++ b/Common/GPU/D3D11/thin3d_d3d11.cpp @@ -283,6 +283,7 @@ D3D11DrawContext::D3D11DrawContext(ID3D11Device *device, ID3D11DeviceContext *de caps_.framebufferCopySupported = true; caps_.framebufferDepthBlitSupported = false; caps_.framebufferDepthCopySupported = true; + caps_.framebufferSeparateDepthCopySupported = false; caps_.texture3DSupported = true; D3D11_FEATURE_DATA_D3D11_OPTIONS options{}; diff --git a/Common/GPU/D3D9/thin3d_d3d9.cpp b/Common/GPU/D3D9/thin3d_d3d9.cpp index 9afba75110..684b727cc5 100644 --- a/Common/GPU/D3D9/thin3d_d3d9.cpp +++ b/Common/GPU/D3D9/thin3d_d3d9.cpp @@ -667,6 +667,7 @@ D3D9Context::D3D9Context(IDirect3D9 *d3d, IDirect3D9Ex *d3dEx, int adapterId, ID caps_.framebufferCopySupported = false; caps_.framebufferDepthBlitSupported = true; caps_.framebufferDepthCopySupported = false; + caps_.framebufferSeparateDepthCopySupported = false; caps_.texture3DSupported = true; if (d3d) { @@ -1241,14 +1242,26 @@ void D3D9Context::GetFramebufferDimensions(Framebuffer *fbo, int *w, int *h) { bool D3D9Context::BlitFramebuffer(Framebuffer *srcfb, int srcX1, int srcY1, int srcX2, int srcY2, Framebuffer *dstfb, int dstX1, int dstY1, int dstX2, int dstY2, int channelBits, FBBlitFilter filter, const char *tag) { D3D9Framebuffer *src = (D3D9Framebuffer *)srcfb; D3D9Framebuffer *dst = (D3D9Framebuffer *)dstfb; - if (channelBits != FB_COLOR_BIT) - return false; + + LPDIRECT3DSURFACE9 srcSurf; + LPDIRECT3DSURFACE9 dstSurf; RECT srcRect{ (LONG)srcX1, (LONG)srcY1, (LONG)srcX2, (LONG)srcY2 }; RECT dstRect{ (LONG)dstX1, (LONG)dstY1, (LONG)dstX2, (LONG)dstY2 }; - LPDIRECT3DSURFACE9 srcSurf = src ? src->surf : deviceRTsurf; - LPDIRECT3DSURFACE9 dstSurf = dst ? dst->surf : deviceRTsurf; + if (channelBits == FB_COLOR_BIT) { + srcSurf = src ? src->surf : deviceRTsurf; + dstSurf = dst ? dst->surf : deviceRTsurf; + } else if (channelBits & FB_DEPTH_BIT) { + if (!src || !dst) { + // Might have implications for non-buffered rendering. + return false; + } + srcSurf = src->depthstencil; + dstSurf = dst->depthstencil; + } else { + return false; + } stepId_++; - return SUCCEEDED(device_->StretchRect(srcSurf, &srcRect, dstSurf, &dstRect, filter == FB_BLIT_LINEAR ? D3DTEXF_LINEAR : D3DTEXF_POINT)); + return SUCCEEDED(device_->StretchRect(srcSurf, &srcRect, dstSurf, &dstRect, (filter == FB_BLIT_LINEAR && channelBits == FB_COLOR_BIT) ? D3DTEXF_LINEAR : D3DTEXF_POINT)); } void D3D9Context::HandleEvent(Event ev, int width, int height, void *param1, void *param2) { diff --git a/Common/GPU/OpenGL/thin3d_gl.cpp b/Common/GPU/OpenGL/thin3d_gl.cpp index e61cb93aae..b48d900ab1 100644 --- a/Common/GPU/OpenGL/thin3d_gl.cpp +++ b/Common/GPU/OpenGL/thin3d_gl.cpp @@ -541,7 +541,9 @@ OpenGLContext::OpenGLContext() { caps_.preferredDepthBufferFormat = DataFormat::D24_S8; caps_.texture3DSupported = true; } - caps_.framebufferBlitSupported = gl_extensions.NV_framebuffer_blit || gl_extensions.ARB_framebuffer_object; + + caps_.framebufferCopySupported = gl_extensions.OES_copy_image || gl_extensions.NV_copy_image || gl_extensions.EXT_copy_image || gl_extensions.ARB_copy_image; + caps_.framebufferBlitSupported = gl_extensions.NV_framebuffer_blit || gl_extensions.ARB_framebuffer_object || gl_extensions.GLES3; caps_.framebufferDepthBlitSupported = caps_.framebufferBlitSupported; caps_.depthClampSupported = gl_extensions.ARB_depth_clamp; if (gl_extensions.IsGLES) { diff --git a/Common/GPU/Vulkan/thin3d_vulkan.cpp b/Common/GPU/Vulkan/thin3d_vulkan.cpp index 06bca64a33..e2cae4c906 100644 --- a/Common/GPU/Vulkan/thin3d_vulkan.cpp +++ b/Common/GPU/Vulkan/thin3d_vulkan.cpp @@ -578,7 +578,7 @@ static int GetBpp(VkFormat format) { } } -VkFormat DataFormatToVulkan(DataFormat format) { +static VkFormat DataFormatToVulkan(DataFormat format) { switch (format) { case DataFormat::D16: return VK_FORMAT_D16_UNORM; case DataFormat::D32F: return VK_FORMAT_D32_SFLOAT; @@ -774,6 +774,8 @@ VKContext::VKContext(VulkanContext *vulkan, bool splitSubmit) : vulkan_(vulkan), renderManager_(vulkan) { shaderLanguageDesc_.Init(GLSL_VULKAN); + VkFormat depthStencilFormat = vulkan->GetDeviceInfo().preferredDepthStencilFormat; + caps_.anisoSupported = vulkan->GetDeviceFeatures().enabled.samplerAnisotropy != 0; caps_.geometryShaderSupported = vulkan->GetDeviceFeatures().enabled.geometryShader != 0; caps_.tesselationShaderSupported = vulkan->GetDeviceFeatures().enabled.tessellationShader != 0; @@ -784,8 +786,9 @@ VKContext::VKContext(VulkanContext *vulkan, bool splitSubmit) caps_.cullDistanceSupported = vulkan->GetDeviceFeatures().enabled.shaderCullDistance != 0; caps_.framebufferBlitSupported = true; caps_.framebufferCopySupported = true; - caps_.framebufferDepthBlitSupported = false; // Can be checked for. + caps_.framebufferDepthBlitSupported = vulkan->GetDeviceInfo().canBlitToPreferredDepthStencilFormat; caps_.framebufferDepthCopySupported = true; // Will pretty much always be the case. + caps_.framebufferSeparateDepthCopySupported = true; // Will pretty much always be the case. caps_.preferredDepthBufferFormat = DataFormat::D24_S8; // TODO: Ask vulkan. caps_.texture3DSupported = true; @@ -1440,18 +1443,24 @@ uint32_t VKContext::GetDataFormatSupport(DataFormat fmt) const { VkFormatProperties properties; vkGetPhysicalDeviceFormatProperties(vulkan_->GetCurrentPhysicalDevice(), vulkan_format, &properties); uint32_t flags = 0; - if (properties.optimalTilingFeatures & VkFormatFeatureFlagBits::VK_FORMAT_FEATURE_COLOR_ATTACHMENT_BIT) { + if (properties.optimalTilingFeatures & VK_FORMAT_FEATURE_COLOR_ATTACHMENT_BIT) { flags |= FMT_RENDERTARGET; } - if (properties.optimalTilingFeatures & VkFormatFeatureFlagBits::VK_FORMAT_FEATURE_DEPTH_STENCIL_ATTACHMENT_BIT) { + if (properties.optimalTilingFeatures & VK_FORMAT_FEATURE_DEPTH_STENCIL_ATTACHMENT_BIT) { flags |= FMT_DEPTHSTENCIL; } - if (properties.optimalTilingFeatures & VkFormatFeatureFlagBits::VK_FORMAT_FEATURE_SAMPLED_IMAGE_BIT) { + if (properties.optimalTilingFeatures & VK_FORMAT_FEATURE_SAMPLED_IMAGE_BIT) { flags |= FMT_TEXTURE; } - if (properties.bufferFeatures & VkFormatFeatureFlagBits::VK_FORMAT_FEATURE_VERTEX_BUFFER_BIT) { + if (properties.bufferFeatures & VK_FORMAT_FEATURE_VERTEX_BUFFER_BIT) { flags |= FMT_INPUTLAYOUT; } + if ((properties.optimalTilingFeatures & VK_FORMAT_FEATURE_BLIT_SRC_BIT) && (properties.optimalTilingFeatures & VK_FORMAT_FEATURE_BLIT_DST_BIT)) { + flags |= FMT_BLIT; + } + if (properties.optimalTilingFeatures & VK_FORMAT_FEATURE_STORAGE_IMAGE_BIT) { + flags |= FMT_STORAGE_IMAGE; + } return flags; } diff --git a/Common/GPU/thin3d.h b/Common/GPU/thin3d.h index 55093b83d0..9a618df79e 100644 --- a/Common/GPU/thin3d.h +++ b/Common/GPU/thin3d.h @@ -202,6 +202,8 @@ enum FormatSupport { FMT_INPUTLAYOUT = 4, FMT_DEPTHSTENCIL = 8, FMT_AUTOGEN_MIPS = 16, + FMT_BLIT = 32, + FMT_STORAGE_IMAGE = 64, }; enum InfoField { @@ -530,6 +532,7 @@ struct DeviceCaps { bool framebufferCopySupported; bool framebufferBlitSupported; bool framebufferDepthCopySupported; + bool framebufferSeparateDepthCopySupported; bool framebufferDepthBlitSupported; bool framebufferFetchSupported; bool texture3DSupported; diff --git a/GPU/Common/FramebufferManagerCommon.cpp b/GPU/Common/FramebufferManagerCommon.cpp index 1471f6bcf5..46b8255ea5 100644 --- a/GPU/Common/FramebufferManagerCommon.cpp +++ b/GPU/Common/FramebufferManagerCommon.cpp @@ -581,15 +581,16 @@ void FramebufferManagerCommon::BlitFramebufferDepth(VirtualFramebuffer *src, Vir int w = std::min(src->renderWidth, dst->renderWidth); int h = std::min(src->renderHeight, dst->renderHeight); - // Note: We prefer Blit ahead of Copy here, since at least on GL, Copy will always also copy stencil which we don't want. - // See #9740. - // TODO: This ordering should probably apply to GL only, since in Vulkan you can totally copy just the depth aspect. - if (gstate_c.Supports(GPU_SUPPORTS_FRAMEBUFFER_BLIT_TO_DEPTH)) { - draw_->BlitFramebuffer(src->fbo, 0, 0, w, h, dst->fbo, 0, 0, w, h, Draw::FB_DEPTH_BIT, Draw::FB_BLIT_NEAREST, "BlitFramebufferDepth"); - RebindFramebuffer("After BlitFramebufferDepth"); - } else if (gstate_c.Supports(GPU_SUPPORTS_COPY_IMAGE)) { + // TODO: It might even be advantageous on some GPUs to do this copy using a fragment shader that writes to Z, that way upcoming commands can just continue that render pass. + + // Some GPUs can copy depth but only if stencil gets to come along for the ride. We only want to use this if there is no blit functionality. + if (draw_->GetDeviceCaps().framebufferSeparateDepthCopySupported || !draw_->GetDeviceCaps().framebufferDepthBlitSupported) { draw_->CopyFramebufferImage(src->fbo, 0, 0, 0, 0, dst->fbo, 0, 0, 0, 0, w, h, 1, Draw::FB_DEPTH_BIT, "BlitFramebufferDepth"); RebindFramebuffer("After BlitFramebufferDepth"); + } else if (draw_->GetDeviceCaps().framebufferDepthBlitSupported) { + // We'll accept whether we get a separate depth blit or not... + draw_->BlitFramebuffer(src->fbo, 0, 0, w, h, dst->fbo, 0, 0, w, h, Draw::FB_DEPTH_BIT, Draw::FB_BLIT_NEAREST, "BlitFramebufferDepth"); + RebindFramebuffer("After BlitFramebufferDepth"); } dst->last_frame_depth_updated = gpuStats.numFlips; } @@ -693,7 +694,7 @@ void FramebufferManagerCommon::ReinterpretFramebuffer(VirtualFramebuffer *vfb, G bool doReinterpret = PSP_CoreParameter().compat.flags().ReinterpretFramebuffers && (lang == HLSL_D3D11 || lang == GLSL_VULKAN || lang == GLSL_3xx); // Copy image required for now. - if (!gstate_c.Supports(GPU_SUPPORTS_COPY_IMAGE)) + if (!draw_->GetDeviceCaps().framebufferCopySupported) doReinterpret = false; if (!doReinterpret) { // Fake reinterpret - just clear the way we always did on Vulkan. Just clear color and stencil. diff --git a/GPU/D3D11/GPU_D3D11.cpp b/GPU/D3D11/GPU_D3D11.cpp index f02d93547e..ba68c1bd4d 100644 --- a/GPU/D3D11/GPU_D3D11.cpp +++ b/GPU/D3D11/GPU_D3D11.cpp @@ -139,7 +139,6 @@ void GPU_D3D11::CheckGPUFeatures() { } } - features |= GPU_SUPPORTS_COPY_IMAGE; features |= GPU_SUPPORTS_TEXTURE_FLOAT; features |= GPU_SUPPORTS_INSTANCE_RENDERING; features |= GPU_SUPPORTS_TEXTURE_LOD_CONTROL; diff --git a/GPU/GLES/GPU_GLES.cpp b/GPU/GLES/GPU_GLES.cpp index 57937cd9a8..4be07ecb42 100644 --- a/GPU/GLES/GPU_GLES.cpp +++ b/GPU/GLES/GPU_GLES.cpp @@ -185,10 +185,6 @@ void GPU_GLES::CheckGPUFeatures() { } } - if (gl_extensions.ARB_framebuffer_object || gl_extensions.NV_framebuffer_blit || gl_extensions.GLES3) { - features |= GPU_SUPPORTS_FRAMEBUFFER_BLIT | GPU_SUPPORTS_FRAMEBUFFER_BLIT_TO_DEPTH; - } - if ((gl_extensions.gpuVendor == GPU_VENDOR_NVIDIA) || (gl_extensions.gpuVendor == GPU_VENDOR_AMD)) features |= GPU_PREFER_REVERSE_COLOR_ORDER; @@ -198,9 +194,6 @@ void GPU_GLES::CheckGPUFeatures() { if (gl_extensions.EXT_blend_minmax) features |= GPU_SUPPORTS_BLEND_MINMAX; - if (gl_extensions.OES_copy_image || gl_extensions.NV_copy_image || gl_extensions.EXT_copy_image || gl_extensions.ARB_copy_image) - features |= GPU_SUPPORTS_COPY_IMAGE; - if (!gl_extensions.IsGLES) features |= GPU_SUPPORTS_LOGIC_OP; diff --git a/GPU/GLES/StencilBufferGLES.cpp b/GPU/GLES/StencilBufferGLES.cpp index 4c35fb4967..665f534137 100644 --- a/GPU/GLES/StencilBufferGLES.cpp +++ b/GPU/GLES/StencilBufferGLES.cpp @@ -158,7 +158,7 @@ bool FramebufferManagerGLES::NotifyStencilUpload(u32 addr, int size, StencilUplo shaderManager_->DirtyLastShader(); - bool useBlit = gstate_c.Supports(GPU_SUPPORTS_FRAMEBUFFER_BLIT); + bool useBlit = draw_->GetDeviceCaps().framebufferDepthBlitSupported; // Our fragment shader (and discard) is slow. Since the source is 1x, we can stencil to 1x. // Then after we're done, we'll just blit it across and stretch it there. diff --git a/GPU/GPUState.h b/GPU/GPUState.h index 2d8db478b9..bc0ede6d1c 100644 --- a/GPU/GPUState.h +++ b/GPU/GPUState.h @@ -479,15 +479,13 @@ enum { GPU_SUPPORTS_32BIT_INT_FSHADER = FLAG_BIT(15), GPU_SUPPORTS_DEPTH_TEXTURE = FLAG_BIT(16), GPU_SUPPORTS_ACCURATE_DEPTH = FLAG_BIT(17), - // Free bit: 18 - GPU_SUPPORTS_COPY_IMAGE = FLAG_BIT(19), + // Free bits: 18-19 GPU_SUPPORTS_ANY_FRAMEBUFFER_FETCH = FLAG_BIT(20), GPU_SCALE_DEPTH_FROM_24BIT_TO_16BIT = FLAG_BIT(21), GPU_ROUND_FRAGMENT_DEPTH_TO_16BIT = FLAG_BIT(22), GPU_ROUND_DEPTH_TO_16BIT = FLAG_BIT(23), // Can be disabled either per game or if we use a real 16-bit depth buffer GPU_SUPPORTS_TEXTURE_LOD_CONTROL = FLAG_BIT(24), - GPU_SUPPORTS_FRAMEBUFFER_BLIT = FLAG_BIT(26), - GPU_SUPPORTS_FRAMEBUFFER_BLIT_TO_DEPTH = FLAG_BIT(27), + // Free bits: 25-27 GPU_SUPPORTS_TEXTURE_NPOT = FLAG_BIT(28), GPU_SUPPORTS_CLIP_DISTANCE = FLAG_BIT(29), GPU_SUPPORTS_CULL_DISTANCE = FLAG_BIT(30), diff --git a/GPU/Vulkan/GPU_Vulkan.cpp b/GPU/Vulkan/GPU_Vulkan.cpp index 96c2841f3d..8b6b980f5e 100644 --- a/GPU/Vulkan/GPU_Vulkan.cpp +++ b/GPU/Vulkan/GPU_Vulkan.cpp @@ -232,19 +232,13 @@ void GPU_Vulkan::CheckGPUFeatures() { // Mandatory features on Vulkan, which may be checked in "centralized" code features |= GPU_SUPPORTS_TEXTURE_LOD_CONTROL; - features |= GPU_SUPPORTS_FRAMEBUFFER_BLIT; features |= GPU_SUPPORTS_BLEND_MINMAX; - features |= GPU_SUPPORTS_COPY_IMAGE; features |= GPU_SUPPORTS_TEXTURE_NPOT; features |= GPU_SUPPORTS_INSTANCE_RENDERING; features |= GPU_SUPPORTS_VERTEX_TEXTURE_FETCH; features |= GPU_SUPPORTS_TEXTURE_FLOAT; features |= GPU_SUPPORTS_DEPTH_TEXTURE; - if (vulkan->GetDeviceInfo().canBlitToPreferredDepthStencilFormat) { - features |= GPU_SUPPORTS_FRAMEBUFFER_BLIT_TO_DEPTH; - } - auto &enabledFeatures = vulkan->GetDeviceFeatures().enabled; if (enabledFeatures.depthClamp) { features |= GPU_SUPPORTS_DEPTH_CLAMP; From 770cb49f9464af00ef6c3baf581f0fd001e26388 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Henrik=20Rydg=C3=A5rd?= Date: Mon, 1 Aug 2022 23:21:41 +0200 Subject: [PATCH 2/2] GLES: FramebufferBlit: Don't blit if the target is the current framebuffer. --- GPU/GLES/FramebufferManagerGLES.cpp | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/GPU/GLES/FramebufferManagerGLES.cpp b/GPU/GLES/FramebufferManagerGLES.cpp index 5a14ff051e..983e3dc7c4 100644 --- a/GPU/GLES/FramebufferManagerGLES.cpp +++ b/GPU/GLES/FramebufferManagerGLES.cpp @@ -257,7 +257,13 @@ void FramebufferManagerGLES::BlitFramebuffer(VirtualFramebuffer *dst, int dstX, return; } - bool useBlit = gstate_c.Supports(GPU_SUPPORTS_FRAMEBUFFER_BLIT); + bool useBlit = draw_->GetDeviceCaps().framebufferBlitSupported; + bool useCopy = draw_->GetDeviceCaps().framebufferCopySupported; + if (dst == currentRenderVfb_) { + // If already bound, using either a blit or a copy is unlikely to be an optimization. + useBlit = false; + useCopy = false; + } float srcXFactor = useBlit ? src->renderScaleFactor : 1.0f; float srcYFactor = useBlit ? src->renderScaleFactor : 1.0f; @@ -287,7 +293,7 @@ void FramebufferManagerGLES::BlitFramebuffer(VirtualFramebuffer *dst, int dstX, return; } - if (gstate_c.Supports(GPU_SUPPORTS_COPY_IMAGE)) { + if (useCopy && draw_->GetDeviceCaps().framebufferCopySupported) { // glBlitFramebuffer can clip, but glCopyImageSubData is more restricted. // In case the src goes outside, we just skip the optimization in that case. const bool sameSize = dstX2 - dstX1 == srcX2 - srcX1 && dstY2 - dstY1 == srcY2 - srcY1; @@ -304,8 +310,7 @@ void FramebufferManagerGLES::BlitFramebuffer(VirtualFramebuffer *dst, int dstX, if (useBlit) { draw_->BlitFramebuffer(src->fbo, srcX1, srcY1, srcX2, srcY2, dst->fbo, dstX1, dstY1, dstX2, dstY2, Draw::FB_COLOR_BIT, Draw::FB_BLIT_NEAREST, tag); } else { - // TODO: Move this workaround out into thin3d, instead of dirtying up the code here. - + // TODO: Use thin3d for this, instead of dirtying up the code here. draw_->BindFramebufferAsRenderTarget(dst->fbo, { Draw::RPAction::KEEP, Draw::RPAction::KEEP, Draw::RPAction::KEEP }, tag); draw_->BindFramebufferAsTexture(src->fbo, 0, Draw::FB_COLOR_BIT, 0);