From be1832a348e9f6bcc41eb6ff429762a7f364cf3b Mon Sep 17 00:00:00 2001 From: Philip Rebohle Date: Sun, 24 Mar 2019 18:02:19 +0100 Subject: [PATCH] [d3d11] Don't sample gamma texture if the gamma curve is identity Saves some GPU time in games that don't use DXGI gamma control at all. --- src/d3d11/d3d11_swapchain.cpp | 33 ++++++++++++++--------- src/d3d11/d3d11_swapchain.h | 2 ++ src/dxgi/shaders/dxgi_presenter_frag.frag | 16 ++++++----- 3 files changed, 32 insertions(+), 19 deletions(-) diff --git a/src/d3d11/d3d11_swapchain.cpp b/src/d3d11/d3d11_swapchain.cpp index 7d099d37f..09b846f54 100644 --- a/src/d3d11/d3d11_swapchain.cpp +++ b/src/d3d11/d3d11_swapchain.cpp @@ -35,8 +35,6 @@ namespace dxvk { InitSamplers(); InitShaders(); - SetGammaControl(0, nullptr); - // The present fence seems to introduce stutter on ANV if (m_device->adapter()->matchesDriver(DxvkGpuVendor::Intel, VK_DRIVER_ID_INTEL_OPEN_SOURCE_MESA_KHR, 0, 0)) m_usePresentFence = false; @@ -135,31 +133,34 @@ namespace dxvk { HRESULT STDMETHODCALLTYPE D3D11SwapChain::SetGammaControl( UINT NumControlPoints, const DXGI_RGB* pControlPoints) { - if (NumControlPoints > 0) { + bool isIdentity = true; + + if (NumControlPoints > 1) { std::array cp; if (NumControlPoints > cp.size()) return E_INVALIDARG; for (uint32_t i = 0; i < NumControlPoints; i++) { + uint16_t identity = MapGammaControlPoint(float(i) / float(NumControlPoints - 1)); + cp[i].R = MapGammaControlPoint(pControlPoints[i].Red); cp[i].G = MapGammaControlPoint(pControlPoints[i].Green); cp[i].B = MapGammaControlPoint(pControlPoints[i].Blue); cp[i].A = 0; + + isIdentity &= cp[i].R == identity + && cp[i].G == identity + && cp[i].B == identity; } - CreateGammaTexture(NumControlPoints, cp.data()); - } else { - std::array cp; - - for (uint32_t i = 0; i < cp.size(); i++) { - const uint16_t value = 257 * i; - cp[i] = { value, value, value, 0 }; - } - - CreateGammaTexture(cp.size(), cp.data()); + if (!isIdentity) + CreateGammaTexture(NumControlPoints, cp.data()); } + if (isIdentity) + DestroyGammaTexture(); + return S_OK; } @@ -570,6 +571,12 @@ namespace dxvk { } + void D3D11SwapChain::DestroyGammaTexture() { + m_gammaTexture = nullptr; + m_gammaTextureView = nullptr; + } + + void D3D11SwapChain::CreateHud() { m_hud = hud::Hud::createHud(m_device); } diff --git a/src/d3d11/d3d11_swapchain.h b/src/d3d11/d3d11_swapchain.h index 66d7d1b24..4099bb607 100644 --- a/src/d3d11/d3d11_swapchain.h +++ b/src/d3d11/d3d11_swapchain.h @@ -139,6 +139,8 @@ namespace dxvk { UINT NumControlPoints, const D3D11_VK_GAMMA_CP* pControlPoints); + void DestroyGammaTexture(); + void CreateHud(); void InitRenderState(); diff --git a/src/dxgi/shaders/dxgi_presenter_frag.frag b/src/dxgi/shaders/dxgi_presenter_frag.frag index 73118a07a..7ff685156 100644 --- a/src/dxgi/shaders/dxgi_presenter_frag.frag +++ b/src/dxgi/shaders/dxgi_presenter_frag.frag @@ -1,5 +1,7 @@ #version 450 +layout(constant_id = 3) const bool s_gamma_bound = false; + layout(binding = 0) uniform sampler s_sampler; layout(binding = 1) uniform texture2D t_texture; @@ -10,11 +12,13 @@ layout(location = 0) in vec2 i_texcoord; layout(location = 0) out vec4 o_color; void main() { - vec4 color = texture(sampler2D(t_texture, s_sampler), i_texcoord); + o_color = texture(sampler2D(t_texture, s_sampler), i_texcoord); - o_color = vec4( - texture(sampler1D(t_gamma, s_gamma), color.r).r, - texture(sampler1D(t_gamma, s_gamma), color.g).g, - texture(sampler1D(t_gamma, s_gamma), color.b).b, - color.a); + if (s_gamma_bound) { + o_color = vec4( + texture(sampler1D(t_gamma, s_gamma), o_color.r).r, + texture(sampler1D(t_gamma, s_gamma), o_color.g).g, + texture(sampler1D(t_gamma, s_gamma), o_color.b).b, + o_color.a); + } } \ No newline at end of file