Merge remote-tracking branch 'origin/master' into gi

This commit is contained in:
Wojciech Figat
2022-04-11 10:37:23 +02:00
59 changed files with 471 additions and 314 deletions

View File

@@ -266,6 +266,20 @@ bool GPUDeviceDX11::Init()
}
UpdateOutputs(adapter);
ComPtr<IDXGIFactory5> factory5;
_factoryDXGI->QueryInterface(IID_PPV_ARGS(&factory5));
if (factory5)
{
BOOL allowTearing;
if (SUCCEEDED(factory5->CheckFeatureSupport(DXGI_FEATURE_PRESENT_ALLOW_TEARING, &allowTearing, sizeof(allowTearing)))
&& allowTearing
#if PLATFORM_WINDOWS
&& GetModuleHandleA("renderdoc.dll") == nullptr // Disable tearing with RenderDoc (prevents crashing)
#endif
)
AllowTearing = true;
}
// Get flags and device type base on current configuration
uint32 flags = D3D11_CREATE_DEVICE_BGRA_SUPPORT;
#if GPU_ENABLE_DIAGNOSTICS
@@ -276,18 +290,7 @@ bool GPUDeviceDX11::Init()
// Create DirectX device
D3D_FEATURE_LEVEL createdFeatureLevel = static_cast<D3D_FEATURE_LEVEL>(0);
auto targetFeatureLevel = GetD3DFeatureLevel();
VALIDATE_DIRECTX_RESULT(D3D11CreateDevice(
adapter,
D3D_DRIVER_TYPE_UNKNOWN,
NULL,
flags,
&targetFeatureLevel,
1,
D3D11_SDK_VERSION,
&_device,
&createdFeatureLevel,
&_imContext
));
VALIDATE_DIRECTX_RESULT(D3D11CreateDevice(adapter, D3D_DRIVER_TYPE_UNKNOWN, NULL, flags, &targetFeatureLevel, 1, D3D11_SDK_VERSION, &_device, &createdFeatureLevel, &_imContext));
// Validate result
ASSERT(_device);

View File

@@ -50,6 +50,10 @@ public:
GPUDeviceDX11(IDXGIFactory* dxgiFactory, GPUAdapterDX* adapter);
~GPUDeviceDX11();
public:
bool AllowTearing = false;
public:
// Gets DX11 device

View File

@@ -17,6 +17,8 @@ GPUSwapChainDX11::GPUSwapChainDX11(GPUDeviceDX11* device, Window* window)
#endif
, _swapChain(nullptr)
, _backBuffer(nullptr)
, _allowTearing(false)
, _isFullscreen(false)
{
ASSERT(_windowHandle);
_window = window;
@@ -108,6 +110,18 @@ void GPUSwapChainDX11::SetFullscreen(bool isFullscreen)
{
LOG(Warning, "Cannot change fullscreen mode for '{0}' to {1}.", ToString(), isFullscreen);
}
_isFullscreen = isFullscreen;
// Buffers must be resized in flip presentation model
if (swapChainDesc.SwapEffect == DXGI_SWAP_EFFECT_FLIP_SEQUENTIAL ||
swapChainDesc.SwapEffect == DXGI_SWAP_EFFECT_FLIP_DISCARD)
{
const int32 width = _width;
const int32 height = _height;
_width = _height = 0;
Resize(width, height);
}
}
#else
LOG(Info, "Cannot change fullscreen mode on this platform");
@@ -123,7 +137,14 @@ void GPUSwapChainDX11::Present(bool vsync)
{
// Present frame
ASSERT(_swapChain);
const HRESULT result = _swapChain->Present(vsync ? 1 : 0, 0);
UINT presentFlags = 0;
#if PLATFORM_WINDOWS
if (!vsync && !_isFullscreen && _allowTearing)
{
presentFlags |= DXGI_PRESENT_ALLOW_TEARING;
}
#endif
const HRESULT result = _swapChain->Present(vsync ? 1 : 0, presentFlags);
LOG_DIRECTX_RESULT(result);
// Base
@@ -140,6 +161,9 @@ bool GPUSwapChainDX11::Resize(int32 width, int32 height)
_device->WaitForGPU();
GPUDeviceLock lock(_device);
#if PLATFORM_WINDOWS
_allowTearing = _device->AllowTearing;
#endif
_format = GPU_BACK_BUFFER_PIXEL_FORMAT;
#if PLATFORM_WINDOWS
@@ -177,6 +201,11 @@ bool GPUSwapChainDX11::Resize(int32 width, int32 height)
swapChainDesc.Windowed = TRUE;
swapChainDesc.SwapEffect = DXGI_SWAP_EFFECT_DISCARD;
swapChainDesc.Flags = DXGI_SWAP_CHAIN_FLAG_ALLOW_MODE_SWITCH;
if (_allowTearing)
{
swapChainDesc.SwapEffect = DXGI_SWAP_EFFECT_FLIP_DISCARD;
swapChainDesc.Flags |= DXGI_SWAP_CHAIN_FLAG_ALLOW_TEARING;
}
#else
swapChainDesc.Width = width;
swapChainDesc.Height = height;
@@ -202,7 +231,7 @@ bool GPUSwapChainDX11::Resize(int32 width, int32 height)
ASSERT(_swapChain);
// Disable DXGI changes to the window
VALIDATE_DIRECTX_RESULT(dxgi->MakeWindowAssociation(_windowHandle, 0));
VALIDATE_DIRECTX_RESULT(dxgi->MakeWindowAssociation(_windowHandle, DXGI_MWA_NO_ALT_ENTER));
#else
auto dxgiFactory = (IDXGIFactory2*)_device->GetDXGIFactory();
VALIDATE_DIRECTX_RESULT(dxgiFactory->CreateSwapChainForCoreWindow(_device->GetDevice(), static_cast<IUnknown*>(_windowHandle), &swapChainDesc, nullptr, &_swapChain));

View File

@@ -23,6 +23,7 @@ private:
#if PLATFORM_WINDOWS
HWND _windowHandle;
IDXGISwapChain* _swapChain;
bool _allowTearing, _isFullscreen;
#else
IUnknown* _windowHandle;
IDXGISwapChain1* _swapChain;

View File

@@ -264,17 +264,19 @@ bool GPUDeviceDX12::Init()
return true;
}
UpdateOutputs(adapter);
ComPtr<IDXGIFactory5> factory5;
_factoryDXGI->QueryInterface(IID_PPV_ARGS(&factory5));
if (factory5)
{
ComPtr<IDXGIFactory5> factory5;
_factoryDXGI->QueryInterface(IID_PPV_ARGS(&factory5));
if (factory5)
{
BOOL allowTearing;
if (SUCCEEDED(factory5->CheckFeatureSupport(DXGI_FEATURE_PRESENT_ALLOW_TEARING, &allowTearing, sizeof(allowTearing))) && allowTearing)
{
AllowTearing = true;
}
}
BOOL allowTearing;
if (SUCCEEDED(factory5->CheckFeatureSupport(DXGI_FEATURE_PRESENT_ALLOW_TEARING, &allowTearing, sizeof(allowTearing)))
&& allowTearing
#if PLATFORM_WINDOWS
&& GetModuleHandleA("renderdoc.dll") == nullptr // Disable tearing with RenderDoc (prevents crashing)
#endif
)
AllowTearing = true;
}
// Create DirectX device

View File

@@ -51,6 +51,8 @@ GPUSwapChainDX12::GPUSwapChainDX12(GPUDeviceDX12* device, Window* window)
, _windowHandle(static_cast<HWND>(window->GetNativePtr()))
, _swapChain(nullptr)
, _currentFrameIndex(0)
, _allowTearing(false)
, _isFullscreen(false)
{
ASSERT(_windowHandle);
_window = window;
@@ -135,6 +137,16 @@ void GPUSwapChainDX12::SetFullscreen(bool isFullscreen)
}
_isFullscreen = isFullscreen;
// Buffers must be resized in flip presentation model
if (swapChainDesc.SwapEffect == DXGI_SWAP_EFFECT_FLIP_SEQUENTIAL ||
swapChainDesc.SwapEffect == DXGI_SWAP_EFFECT_FLIP_DISCARD)
{
const int32 width = _width;
const int32 height = _height;
_width = _height = 0;
Resize(width, height);
}
}
#else
LOG(Info, "Cannot change fullscreen mode on this platform");
@@ -217,7 +229,7 @@ bool GPUSwapChainDX12::Resize(int32 width, int32 height)
_backBuffers.Resize(swapChainDesc.BufferCount);
// Disable DXGI changes to the window
dxgiFactory->MakeWindowAssociation(_windowHandle, 0);
VALIDATE_DIRECTX_RESULT(dxgiFactory->MakeWindowAssociation(_windowHandle, DXGI_MWA_NO_ALT_ENTER));
}
else
{

View File

@@ -39,6 +39,7 @@ typedef IGraphicsUnknown IDXGISwapChain3;
#include <D3D11.h>
#include <d3d11_1.h>
#include <dxgi1_3.h>
#include <dxgi1_5.h>
#endif
#if GRAPHICS_API_DIRECTX12
#include <dxgi1_5.h>