0

I'm trying to implement full screen support on my Direct3D11 application, but I've been having a issue with this. I'm using the ALT+ENTER key combination that makes the IDXGISwapChain change the target window to full screen and back. But when I go into full screen my back buffer starts blinking between the color that I have set on the call to ID3D11DeviceContext::ClearRenderTargetView to black.

I also have code in the WinProc function of my window, that when it receives the WM_SIZE message, this code is executed.

If I don't use this code to resize the buffers, the blinking doesn't happen for some reason. If I call ID3D11DeviceContext::ClearRenderTargetView once after creating the new swap chain, and again after calling IDXGISwapChain::Present, the blinking also doesn't happen.

It's a bit confusing, but I have a theory that maybe, when I go to full screen mode and call IDXGISwapChain::ResizeBuffers it somehow makes my swap chain have more than one buffer, and when I call IDXGISwapChain::Present it rotates to the next buffer, which is on the default color black.

Note that I have also tried to call IDXGISwapChain::SetFullscreenState, but the issue persists.

My code is too big to paste here, so I took the code from this tutorial and changed it a bit to show the behaviour I'm describing:

#include <windows.h>
#include <d3d11.h>

#pragma comment (lib, "d3d11.lib")

#define SCREEN_WIDTH  800
#define SCREEN_HEIGHT 600

// global declarations
IDXGISwapChain* swapchain;
ID3D11Device* dev;
ID3D11DeviceContext* devcon;
ID3D11RenderTargetView* backbuffer;

void InitD3D(HWND hWnd);
void RenderFrame(void);
void CleanD3D(void);

LRESULT CALLBACK WindowProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam);

int main()
{
HWND hWnd;
WNDCLASSEX wc;

ZeroMemory(&wc, sizeof(WNDCLASSEX));

wc.cbSize = sizeof(WNDCLASSEX);
wc.style = CS_HREDRAW | CS_VREDRAW;
wc.lpfnWndProc = WindowProc;
wc.hInstance = GetModuleHandle(nullptr);
wc.hCursor = LoadCursor(NULL, IDC_ARROW);
wc.lpszClassName = L"WindowClass";

RegisterClassEx(&wc);

RECT wr = { 0, 0, SCREEN_WIDTH, SCREEN_HEIGHT };
AdjustWindowRect(&wr, WS_OVERLAPPEDWINDOW, FALSE);

hWnd = CreateWindowEx(NULL,
    L"WindowClass",
    L"Our First Direct3D Program",
    WS_OVERLAPPEDWINDOW,
    300,
    300,
    wr.right - wr.left,
    wr.bottom - wr.top,
    NULL,
    NULL,
    GetModuleHandle(nullptr),
    NULL);

ShowWindow(hWnd, SW_SHOW);

InitD3D(hWnd);

MSG msg;

while (TRUE)
{
    if (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE))
    {
        TranslateMessage(&msg);
        DispatchMessage(&msg);

        if (msg.message == WM_QUIT)
            break;
    }

    RenderFrame();
}

CleanD3D();

return msg.wParam;
}

// this is the main message handler for the program
LRESULT CALLBACK WindowProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
switch (message)
{
case WM_DESTROY:
{
    PostQuitMessage(0);
    return 0;
} break;
case WM_SIZE: {
    if (swapchain)
    {
        devcon->OMSetRenderTargets(0, 0, 0);

        backbuffer->Release();

        HRESULT hr;
        hr = swapchain->ResizeBuffers(0, 0, 0, DXGI_FORMAT_UNKNOWN, 0);

        ID3D11Texture2D* pBuffer;
        hr = swapchain->GetBuffer(0, __uuidof(ID3D11Texture2D),
            (void**)&pBuffer);

        hr = dev->CreateRenderTargetView(pBuffer, NULL,
            &backbuffer);

        pBuffer->Release();

        devcon->OMSetRenderTargets(1, &backbuffer, NULL);

        float fColor[] = { 0.0f, 0.2f, 0.4f, 1.0f };
        devcon->ClearRenderTargetView(backbuffer, fColor);

        DXGI_SWAP_CHAIN_DESC Desc;
        ZeroMemory(&Desc, sizeof(DXGI_SWAP_CHAIN_DESC));

        swapchain->GetDesc(&Desc);

        D3D11_VIEWPORT vp;
        vp.Width = Desc.BufferDesc.Width;
        vp.Height = Desc.BufferDesc.Height;
        vp.MinDepth = 0.0f;
        vp.MaxDepth = 1.0f;
        vp.TopLeftX = 0;
        vp.TopLeftY = 0;

        devcon->RSSetViewports(1, &vp);
    }
    return 1;
}
}

return DefWindowProc(hWnd, message, wParam, lParam);
}

void InitD3D(HWND hWnd)
{
DXGI_SWAP_CHAIN_DESC scd;

ZeroMemory(&scd, sizeof(DXGI_SWAP_CHAIN_DESC));

scd.BufferCount = 1;
scd.BufferDesc.Format = DXGI_FORMAT_R8G8B8A8_UNORM;
scd.BufferDesc.Width = SCREEN_WIDTH;
scd.BufferDesc.Height = SCREEN_HEIGHT;
scd.BufferUsage = DXGI_USAGE_RENDER_TARGET_OUTPUT;
scd.OutputWindow = hWnd;
scd.SampleDesc.Count = 1;
scd.Windowed = TRUE;
scd.Flags = 0;

D3D11CreateDeviceAndSwapChain(NULL,
    D3D_DRIVER_TYPE_HARDWARE,
    NULL,
    NULL,
    NULL,
    NULL,
    D3D11_SDK_VERSION,
    &scd,
    &swapchain,
    &dev,
    NULL,
    &devcon);

ID3D11Texture2D* pBackBuffer;
swapchain->GetBuffer(0, __uuidof(ID3D11Texture2D), (LPVOID*)&pBackBuffer);

dev->CreateRenderTargetView(pBackBuffer, NULL, &backbuffer);
pBackBuffer->Release();

devcon->OMSetRenderTargets(1, &backbuffer, NULL);

float fColor[] = { 0.0f, 0.2f, 0.4f, 1.0f };
devcon->ClearRenderTargetView(backbuffer, fColor);

D3D11_VIEWPORT viewport;
ZeroMemory(&viewport, sizeof(D3D11_VIEWPORT));

viewport.TopLeftX = 0;
viewport.TopLeftY = 0;
viewport.Width = SCREEN_WIDTH;
viewport.Height = SCREEN_HEIGHT;

devcon->RSSetViewports(1, &viewport);
}

void RenderFrame(void)
{
swapchain->Present(1, 0);
}

void CleanD3D(void)
{
swapchain->SetFullscreenState(FALSE, NULL);

swapchain->Release();
backbuffer->Release();
dev->Release();
devcon->Release();

After messing a bit with the code, I've noticed that when the SampleDesc::Count variable from the DXGI_SWAP_CHAIN_DESC is not equal to 1, the blinking also doesn't happen.

Sol Sol
  • 25
  • 5

0 Answers0