diff --git a/Source/Core/VideoCommon/Src/VideoCommon.h b/Source/Core/VideoCommon/Src/VideoCommon.h index b2e9a42f2f..1d3d2a5fdf 100644 --- a/Source/Core/VideoCommon/Src/VideoCommon.h +++ b/Source/Core/VideoCommon/Src/VideoCommon.h @@ -181,6 +181,19 @@ inline u32 Z24ToZ16ToZ24(u32 src) return (src & 0xFFFF00) | (src >> 16); } +/* Returns the smallest power of 2 which is greater than or equal to num */ +inline u32 MakePow2(u32 num) +{ + --num; + num |= num >> 1; + num |= num >> 2; + num |= num >> 4; + num |= num >> 8; + num |= num >> 16; + ++num; + return num; +} + // returns the exponent of the smallest power of two which is greater than val inline unsigned int GetPow2(unsigned int val) { diff --git a/Source/Plugins/Plugin_VideoDX11/Src/FramebufferManager.cpp b/Source/Plugins/Plugin_VideoDX11/Src/FramebufferManager.cpp index 8b1d215286..afcc9a8e01 100644 --- a/Source/Plugins/Plugin_VideoDX11/Src/FramebufferManager.cpp +++ b/Source/Plugins/Plugin_VideoDX11/Src/FramebufferManager.cpp @@ -65,7 +65,7 @@ FramebufferManager::FramebufferManager() D3D11_TEXTURE2D_DESC texdesc; HRESULT hr; - // create framebuffer color texture + // EFB color texture - primary render target texdesc = CD3D11_TEXTURE2D_DESC(DXGI_FORMAT_R8G8B8A8_UNORM, target_width, target_height, 1, 1, D3D11_BIND_SHADER_RESOURCE|D3D11_BIND_RENDER_TARGET, D3D11_USAGE_DEFAULT, 0, sample_desc.Count, sample_desc.Quality); hr = D3D::device->CreateTexture2D(&texdesc, NULL, &buf); CHECK(hr==S_OK, "create EFB color texture (size: %dx%d; hr=%#x)", target_width, target_height, hr); @@ -76,13 +76,13 @@ FramebufferManager::FramebufferManager() D3D::SetDebugObjectName((ID3D11DeviceChild*)m_efb.color_tex->GetSRV(), "EFB color texture shader resource view"); D3D::SetDebugObjectName((ID3D11DeviceChild*)m_efb.color_tex->GetRTV(), "EFB color texture render target view"); - // create a staging texture for Renderer::AccessEFB + // AccessEFB - Sysmem buffer used to retrieve the pixel data from color_tex texdesc = CD3D11_TEXTURE2D_DESC(DXGI_FORMAT_R8G8B8A8_UNORM, 1, 1, 1, 1, 0, D3D11_USAGE_STAGING, D3D11_CPU_ACCESS_READ); hr = D3D::device->CreateTexture2D(&texdesc, NULL, &m_efb.color_staging_buf); CHECK(hr==S_OK, "create EFB color staging buffer (hr=%#x)", hr); D3D::SetDebugObjectName((ID3D11DeviceChild*)m_efb.color_staging_buf, "EFB color staging texture (used for Renderer::AccessEFB)"); - // EFB depth buffer + // EFB depth buffer - primary depth buffer texdesc = CD3D11_TEXTURE2D_DESC(DXGI_FORMAT_R24G8_TYPELESS, target_width, target_height, 1, 1, D3D11_BIND_DEPTH_STENCIL|D3D11_BIND_SHADER_RESOURCE, D3D11_USAGE_DEFAULT, 0, sample_desc.Count, sample_desc.Quality); hr = D3D::device->CreateTexture2D(&texdesc, NULL, &buf); CHECK(hr==S_OK, "create EFB depth texture (size: %dx%d; hr=%#x)", target_width, target_height, hr); @@ -92,7 +92,7 @@ FramebufferManager::FramebufferManager() D3D::SetDebugObjectName((ID3D11DeviceChild*)m_efb.depth_tex->GetDSV(), "EFB depth texture depth stencil view"); D3D::SetDebugObjectName((ID3D11DeviceChild*)m_efb.depth_tex->GetSRV(), "EFB depth texture shader resource view"); - // render target for depth buffer access in Renderer::AccessEFB + // Render buffer for AccessEFB (depth data) texdesc = CD3D11_TEXTURE2D_DESC(DXGI_FORMAT_R32_FLOAT, 1, 1, 1, 1, D3D11_BIND_RENDER_TARGET); hr = D3D::device->CreateTexture2D(&texdesc, NULL, &buf); CHECK(hr==S_OK, "create EFB depth read texture (hr=%#x)", hr); @@ -101,7 +101,7 @@ FramebufferManager::FramebufferManager() D3D::SetDebugObjectName((ID3D11DeviceChild*)m_efb.depth_read_texture->GetTex(), "EFB depth read texture (used in Renderer::AccessEFB)"); D3D::SetDebugObjectName((ID3D11DeviceChild*)m_efb.depth_read_texture->GetRTV(), "EFB depth read texture render target view (used in Renderer::AccessEFB)"); - // staging texture to which we copy the data from m_efb.depth_read_texture + // AccessEFB - Sysmem buffer used to retrieve the pixel data from depth_read_texture texdesc = CD3D11_TEXTURE2D_DESC(DXGI_FORMAT_R32_FLOAT, 1, 1, 1, 1, 0, D3D11_USAGE_STAGING, D3D11_CPU_ACCESS_READ); hr = D3D::device->CreateTexture2D(&texdesc, NULL, &m_efb.depth_staging_buf); CHECK(hr==S_OK, "create EFB depth staging buffer (hr=%#x)", hr); @@ -109,7 +109,7 @@ FramebufferManager::FramebufferManager() if (g_ActiveConfig.iMultisampleMode) { - // create framebuffer resolve textures (color+depth) + // Framebuffer resolve textures (color+depth) texdesc = CD3D11_TEXTURE2D_DESC(DXGI_FORMAT_R8G8B8A8_UNORM, target_width, target_height, 1, 1, D3D11_BIND_SHADER_RESOURCE, D3D11_USAGE_DEFAULT, 0, 1); hr = D3D::device->CreateTexture2D(&texdesc, NULL, &buf); m_efb.resolved_color_tex = new D3DTexture2D(buf, D3D11_BIND_SHADER_RESOURCE, DXGI_FORMAT_R8G8B8A8_UNORM); diff --git a/Source/Plugins/Plugin_VideoDX9/Src/D3DBase.cpp b/Source/Plugins/Plugin_VideoDX9/Src/D3DBase.cpp index 7a007cb1d8..02300fdbd2 100644 --- a/Source/Plugins/Plugin_VideoDX9/Src/D3DBase.cpp +++ b/Source/Plugins/Plugin_VideoDX9/Src/D3DBase.cpp @@ -444,8 +444,8 @@ bool FixTextureSize(int& width, int& height) if ((caps.TextureCaps & D3DPTEXTURECAPS_POW2) && !(caps.TextureCaps & D3DPTEXTURECAPS_NONPOW2CONDITIONAL)) { // all texture dimensions need to be powers of two - width = GetPow2(width); - height = GetPow2(height); + width = (int)MakePow2((u32)width); + height = (int)MakePow2((u32)height); } if (caps.TextureCaps & D3DPTEXTURECAPS_SQUAREONLY) { @@ -458,6 +458,17 @@ bool FixTextureSize(int& width, int& height) return (width != oldw) || (height != oldh); } +// returns true if format is supported +bool CheckTextureSupport(DWORD usage, D3DFORMAT tex_format) +{ + return D3D_OK == D3D->CheckDeviceFormat(cur_adapter, D3DDEVTYPE_HAL, D3DFMT_X8R8G8B8, usage, D3DRTYPE_TEXTURE, tex_format); +} + +bool CheckDepthStencilSupport(D3DFORMAT target_format, D3DFORMAT depth_format) +{ + return D3D_OK == D3D->CheckDepthStencilMatch(cur_adapter, D3DDEVTYPE_HAL, D3DFMT_X8R8G8B8, target_format, depth_format); +} + const char *VertexShaderVersionString() { static const char *versions[5] = {"ERROR", "vs_1_4", "vs_2_0", "vs_3_0", "vs_4_0"}; diff --git a/Source/Plugins/Plugin_VideoDX9/Src/D3DBase.h b/Source/Plugins/Plugin_VideoDX9/Src/D3DBase.h index f24f323bb8..a3b7b8e26e 100644 --- a/Source/Plugins/Plugin_VideoDX9/Src/D3DBase.h +++ b/Source/Plugins/Plugin_VideoDX9/Src/D3DBase.h @@ -74,6 +74,10 @@ void ShowD3DError(HRESULT err); // returns true if size was changed bool FixTextureSize(int& width, int& height); +// returns true if format is supported +bool CheckTextureSupport(DWORD usage, D3DFORMAT tex_format); +bool CheckDepthStencilSupport(D3DFORMAT target_format, D3DFORMAT depth_format); + // The following are "filtered" versions of the corresponding D3Ddev-> functions. void SetTexture(DWORD Stage, IDirect3DBaseTexture9 *pTexture); void SetRenderState(D3DRENDERSTATETYPE State, DWORD Value); diff --git a/Source/Plugins/Plugin_VideoDX9/Src/FramebufferManager.cpp b/Source/Plugins/Plugin_VideoDX9/Src/FramebufferManager.cpp index 8b92944ea5..57636b33bf 100644 --- a/Source/Plugins/Plugin_VideoDX9/Src/FramebufferManager.cpp +++ b/Source/Plugins/Plugin_VideoDX9/Src/FramebufferManager.cpp @@ -29,100 +29,102 @@ #undef CHECK #define CHECK(hr, Message, ...) if (FAILED(hr)) { PanicAlert(__FUNCTION__ "Failed in %s at line %d: " Message, __FILE__, __LINE__, __VA_ARGS__); } +inline void GetSurface(IDirect3DTexture9* texture, IDirect3DSurface9** surface) +{ + if (!texture) return; + texture->GetSurfaceLevel(0, surface); +} + FramebufferManager::Efb FramebufferManager::s_efb; FramebufferManager::FramebufferManager() { - // Simplest possible setup to start with. int target_width = Renderer::GetFullTargetWidth(); int target_height = Renderer::GetFullTargetHeight(); - s_efb.color_surface_Format = D3DFMT_A8R8G8B8; - // Get the framebuffer texture + + // EFB color texture - primary render target HRESULT hr = D3D::dev->CreateTexture(target_width, target_height, 1, D3DUSAGE_RENDERTARGET, s_efb.color_surface_Format, - D3DPOOL_DEFAULT, &s_efb.color_texture, NULL); - if (s_efb.color_texture) - { - hr = s_efb.color_texture->GetSurfaceLevel(0, &s_efb.color_surface); - } + D3DPOOL_DEFAULT, &s_efb.color_texture, NULL); + GetSurface(s_efb.color_texture, &s_efb.color_surface); CHECK(hr, "Create color texture (size: %dx%d; hr=%#x)", target_width, target_height, hr); + // Render buffer for AccessEFB (color data) hr = D3D::dev->CreateTexture(1, 1, 1, D3DUSAGE_RENDERTARGET, s_efb.color_surface_Format, - D3DPOOL_DEFAULT, &s_efb.colorRead_texture, NULL); + D3DPOOL_DEFAULT, &s_efb.colorRead_texture, NULL); + GetSurface(s_efb.colorRead_texture, &s_efb.color_ReadBuffer); CHECK(hr, "Create Color Read Texture (hr=%#x)", hr); - if (s_efb.colorRead_texture) - { - s_efb.colorRead_texture->GetSurfaceLevel(0, &s_efb.color_ReadBuffer); - } - // Create an offscreen surface that we can lock to retrieve the data + + // AccessEFB - Sysmem buffer used to retrieve the pixel data from color_ReadBuffer hr = D3D::dev->CreateOffscreenPlainSurface(1, 1, s_efb.color_surface_Format, D3DPOOL_SYSTEMMEM, &s_efb.color_OffScreenReadBuffer, NULL); CHECK(hr, "Create offscreen color surface (hr=%#x)", hr); // Select a Z-buffer format with hardware support - D3DFORMAT DepthTexFormats[5] = { + D3DFORMAT DepthTexFormats[] = { FOURCC_INTZ, FOURCC_DF24, FOURCC_RAWZ, FOURCC_DF16, - D3DFMT_D24X8 + D3DFMT_D24X8, + D3DFMT_D24X4S4, + D3DFMT_D24S8, + D3DFMT_D24FS8, + D3DFMT_D32, // too much precision, but who cares + D3DFMT_D16, // much lower precision, but better than nothing + D3DFMT_D15S1, }; - for (int i = 0; i < 5; ++i) + // check format support + for (int i = 0; i < sizeof(DepthTexFormats)/sizeof(D3DFORMAT); ++i) { - s_efb.depth_surface_Format = DepthTexFormats[i]; - // Create the framebuffer depth texture - hr = D3D::dev->CreateTexture(target_width, target_height, 1, D3DUSAGE_DEPTHSTENCIL, s_efb.depth_surface_Format, - D3DPOOL_DEFAULT, &s_efb.depth_texture, NULL); - if (!FAILED(hr)) + if (D3D::CheckDepthStencilSupport(s_efb.color_surface_Format, DepthTexFormats[i])) + { + s_efb.depth_surface_Format = DepthTexFormats[i]; break; + } } - - CHECK(hr, "Framebuffer depth texture (size: %dx%d; hr=%#x)", target_width, target_height, hr); - // Get the Surface - if (s_efb.depth_texture) + if (s_efb.depth_surface_Format == D3DFMT_UNKNOWN) { - s_efb.depth_texture->GetSurfaceLevel(0, &s_efb.depth_surface); - } - - // Create a 4x4 pixel texture to work as a buffer for peeking - if (s_efb.depth_surface_Format == FOURCC_RAWZ || s_efb.depth_surface_Format == D3DFMT_D24X8) - { - DepthTexFormats[0] = D3DFMT_A8R8G8B8; + PanicAlert("No supported depth format found, disabling depth buffers.\nExpect glitches."); } else { - DepthTexFormats[0] = D3DFMT_R32F; - } + // EFB depth buffer - primary depth buffer + hr = D3D::dev->CreateTexture(target_width, target_height, 1, D3DUSAGE_DEPTHSTENCIL, s_efb.depth_surface_Format, + D3DPOOL_DEFAULT, &s_efb.depth_texture, NULL); + GetSurface(s_efb.depth_texture, &s_efb.depth_surface); + CHECK(hr, "Framebuffer depth texture (size: %dx%d; hr=%#x)", target_width, target_height, hr); - DepthTexFormats[1] = D3DFMT_A8R8G8B8; + // Render buffer for AccessEFB (depth data) + if (s_efb.depth_surface_Format == FOURCC_RAWZ || s_efb.depth_surface_Format == D3DFMT_D24X8) + DepthTexFormats[0] = D3DFMT_A8R8G8B8; + else + DepthTexFormats[0] = D3DFMT_R32F; - for (int i = 0; i < 2; ++i) - { - s_efb.depth_ReadBuffer_Format = DepthTexFormats[i]; - // Get the framebuffer Depth texture + DepthTexFormats[1] = D3DFMT_A8R8G8B8; + + for (int i = 0; i < 2; ++i) + { + if (D3D::CheckTextureSupport(D3DUSAGE_RENDERTARGET, DepthTexFormats[i])) + { + s_efb.depth_ReadBuffer_Format = DepthTexFormats[i]; + break; + } + } hr = D3D::dev->CreateTexture(4, 4, 1, D3DUSAGE_RENDERTARGET, s_efb.depth_ReadBuffer_Format, - D3DPOOL_DEFAULT, &s_efb.depthRead_texture, NULL); - if (!FAILED(hr)) - break; - } - - CHECK(hr, "Create depth read texture (hr=%#x)", hr); - if (s_efb.depthRead_texture) - { - s_efb.depthRead_texture->GetSurfaceLevel(0, &s_efb.depth_ReadBuffer); + D3DPOOL_DEFAULT, &s_efb.depthRead_texture, NULL); + GetSurface(s_efb.depthRead_texture, &s_efb.depth_ReadBuffer); + CHECK(hr, "Create depth read texture (hr=%#x)", hr); + + // AccessEFB - Sysmem buffer used to retrieve the pixel data from depth_ReadBuffer + hr = D3D::dev->CreateOffscreenPlainSurface(4, 4, s_efb.depth_ReadBuffer_Format, D3DPOOL_SYSTEMMEM, &s_efb.depth_OffScreenReadBuffer, NULL); + CHECK(hr, "Create depth offscreen surface (hr=%#x)", hr); } - // Create an offscreen surface that we can lock to retrieve the data - hr = D3D::dev->CreateOffscreenPlainSurface(4, 4, s_efb.depth_ReadBuffer_Format, D3DPOOL_SYSTEMMEM, &s_efb.depth_OffScreenReadBuffer, NULL); - CHECK(hr, "Create depth offscreen surface (hr=%#x)", hr); - - // create resources for ReinterpretPixelData + // ReinterpretPixelData - EFB color data will be copy-converted to this texture and the buffers are swapped then hr = D3D::dev->CreateTexture(target_width, target_height, 1, D3DUSAGE_RENDERTARGET, s_efb.color_surface_Format, - D3DPOOL_DEFAULT, &s_efb.color_reinterpret_texture, NULL); - if (s_efb.color_reinterpret_texture) - { - hr = s_efb.color_reinterpret_texture->GetSurfaceLevel(0, &s_efb.color_reinterpret_surface); - } + D3DPOOL_DEFAULT, &s_efb.color_reinterpret_texture, NULL); + GetSurface(s_efb.color_reinterpret_texture, &s_efb.color_reinterpret_surface); CHECK(hr, "Create color reinterpret texture (size: %dx%d; hr=%#x)", target_width, target_height, hr); } @@ -140,6 +142,9 @@ FramebufferManager::~FramebufferManager() SAFE_RELEASE(s_efb.depthRead_texture); SAFE_RELEASE(s_efb.color_reinterpret_texture); SAFE_RELEASE(s_efb.color_reinterpret_surface); + s_efb.color_surface_Format = D3DFMT_UNKNOWN; + s_efb.depth_surface_Format = D3DFMT_UNKNOWN; + s_efb.depth_ReadBuffer_Format = D3DFMT_UNKNOWN; } XFBSourceBase* FramebufferManager::CreateXFBSource(unsigned int target_width, unsigned int target_height)