DirectX11 swapchain texture rendering issue when windowed?

1k Views Asked by At

I'm programming a game framework based on DirectX11 but I'm getting a problem, my textures are badly shown on screen, this is a screenshot:

Texture shown badly on swapchain initialized to windowed

As you can see the image is not perfect but I've noticed that this is happening only if I initialize the swap-chain to windowed, if I don't and I initialize it to full screen the sprite is shown correctly, even if during runtimes I swap from full screen to windowed it still shown correctly, this is the image shown on screen:

Texture shown correctly on swapchain initialized to full screen and then toggled to windowed

There is the initialization of my swapchain:

RECT dimensions;
GetClientRect(game->Window, &dimensions);

unsigned int width = dimensions.right - dimensions.left;
unsigned int height = dimensions.bottom - dimensions.top;

D3D_DRIVER_TYPE driverTypes[] =
{
    D3D_DRIVER_TYPE_HARDWARE,
    D3D_DRIVER_TYPE_WARP,
    D3D_DRIVER_TYPE_REFERENCE,
    D3D_DRIVER_TYPE_SOFTWARE
};

unsigned int totalDriverTypes = ARRAYSIZE(driverTypes);

D3D_FEATURE_LEVEL featureLevels[] =
{
    D3D_FEATURE_LEVEL_11_0,
    D3D_FEATURE_LEVEL_10_1,
    D3D_FEATURE_LEVEL_10_0
};

unsigned int totalFeatureLevels = ARRAYSIZE(featureLevels);

DXGI_SWAP_CHAIN_DESC swapChainDesc;
ZeroMemory(&swapChainDesc, sizeof(swapChainDesc));
swapChainDesc.BufferCount = DXGI_SWAP_EFFECT_SEQUENTIAL;
swapChainDesc.BufferDesc.Width = width;
swapChainDesc.BufferDesc.Height = height;
swapChainDesc.BufferDesc.Format = DXGI_FORMAT_R8G8B8A8_UNORM;
swapChainDesc.BufferDesc.RefreshRate.Numerator = 60;
swapChainDesc.BufferDesc.RefreshRate.Denominator = 1;
swapChainDesc.BufferUsage = DXGI_USAGE_RENDER_TARGET_OUTPUT;
swapChainDesc.OutputWindow = game->Window;
swapChainDesc.Windowed = true;
swapChainDesc.SampleDesc.Count = 1;
swapChainDesc.SampleDesc.Quality = 0;

unsigned int creationFlags = 0;

#ifdef _DEBUG
creationFlags |= D3D11_CREATE_DEVICE_DEBUG;
#endif

HRESULT result;
unsigned int driver = 0;

pin_ptr<IDXGISwapChain*> swapChainPointer;
swapChainPointer = &swapChain_;

pin_ptr<ID3D11Device*> d3dDevicePointer;
d3dDevicePointer = &d3dDevice_;

pin_ptr<D3D_FEATURE_LEVEL> featureLevelPointer;
featureLevelPointer = &featureLevel_;

pin_ptr<ID3D11DeviceContext*> d3dContextPointer;
d3dContextPointer = &d3dContext_;

for (driver = 0; driver < totalDriverTypes; ++driver)
{
    result = D3D11CreateDeviceAndSwapChain(0, driverTypes[driver], 0, creationFlags, featureLevels, totalFeatureLevels,
        D3D11_SDK_VERSION, &swapChainDesc, swapChainPointer,
        d3dDevicePointer, featureLevelPointer, d3dContextPointer);

    if (SUCCEEDED(result))
    {
        driverType_ = driverTypes[driver];
        break;
    }
}

And this is the code to toggle the full screen:

swapChain_->SetFullscreenState(isFullScreen, NULL);

Where IsFullScreen is a boolean passed to the containig function.

Can anyone help me? Thanks in advance!

EDIT:

Solved:

I've change WS_OVERLAPPED parameter on my window creation:

RECT rc = { 0, 0, WindowWidth, WindowHeight };
AdjustWindowRect(&rc, WS_OVERLAPPED | WS_MINIMIZEBOX | WS_SYSMENU, FALSE);

LPCTSTR title = Utilities::StringToLPCSTR(Title);

HWND hwnd = CreateWindowA("BSGame", title, WS_OVERLAPPED | WS_MINIMIZEBOX | WS_SYSMENU, CW_USEDEFAULT, CW_USEDEFAULT, rc.right - rc.left, rc.bottom - rc.top, NULL, NULL, windowHandler, NULL);

To WS_OVERLAPPEDWINDOW

2

There are 2 best solutions below

2
On

When you call SetFullScreen on you SwapChain, you only request a mode change.

Normally after the call, you should receive a WM_SIZE message from your main form, you then need to do the following:

Release any associated resource bound to your swapchain (eg: Texture and RenderTargetView) Make sure you also call ClearState on device context, since if you SwapChain is still bound to pipeline you will also have issue (runtime will not destroy a resource if it's bound to pipeline).

next you call resize on swapchain, like:

 HRESULT result = mSwapChain->ResizeBuffers(0,0,0,DXGI_FORMAT_UNKNOWN,0);

Finally, you can query texture again:

ID3D11Texture2D* texture;
mSwapChain->GetBuffer(0,__uuidof(ID3D11Texture2D),(void**)(&texture));
//Get Buffer does an AddRef on top so we release
texture->Release();

And create a new RenderTargetView, plus update your ViewPort to the new size (get Description from texture).

0
On

In my case the problem was during window creation, this were my situation:

RECT rc = { 0, 0, WindowWidth, WindowHeight };
AdjustWindowRect(&rc, WS_OVERLAPPED | WS_MINIMIZEBOX | WS_SYSMENU, FALSE);

LPCTSTR title = Utilities::StringToLPCSTR(Title);

HWND hwnd = CreateWindowA("BSGame", title, WS_OVERLAPPED | WS_MINIMIZEBOX | WS_SYSMENU, CW_USEDEFAULT, CW_USEDEFAULT, rc.right - rc.left, rc.bottom - rc.top, NULL, NULL, windowHandler, NULL);

To solve I've changed the WS_OVERLAPPED to WS_OVERLAPPEDWINDOW