Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <Windows.h>
- #include <dxgi.h>
- #include <d3d11.h>
- #include <DirectXColors.h>
- #include <string>
- #pragma comment(lib, "dxgi.lib")
- #pragma comment(lib, "d3d11.lib")
- template <typename T>
- class ComPtr
- {
- public:
- explicit ComPtr(T* ptr = nullptr) : ptr{ptr} { }
- ~ComPtr() { if (ptr) ptr->Release(); }
- void Release() { if (ptr) ptr->Release(); ptr = nullptr; }
- T* operator->() const { return ptr; }
- T* const* operator&() const { return &ptr; }
- T** operator&() { return &ptr; }
- operator T*() const { return ptr; }
- private:
- T* ptr;
- };
- LRESULT CALLBACK WindowProc(HWND handle, UINT message, WPARAM wParam, LPARAM lParam);
- void OnResize(int width, int height);
- const std::wstring WindowClassName{L"DXGIBugTestWindow"};
- const int WindowWidth = 800;
- const int WindowHeight = 600;
- bool isRunning = true;
- ComPtr<ID3D11Device> device;
- ComPtr<ID3D11DeviceContext> context;
- ComPtr<IDXGISwapChain> swapChain;
- ComPtr<ID3D11RenderTargetView> renderTargetView;
- int WINAPI wWinMain(HINSTANCE, HINSTANCE, PWSTR, int)
- {
- WNDCLASSW windowClass{ };
- windowClass.lpszClassName = WindowClassName.c_str();
- windowClass.lpfnWndProc = &WindowProc;
- windowClass.hInstance = GetModuleHandleW(nullptr);
- windowClass.hbrBackground = reinterpret_cast<HBRUSH>(COLOR_WINDOW + 1);
- windowClass.hIcon = LoadIconW(nullptr, IDI_APPLICATION);
- windowClass.hCursor = LoadCursorW(nullptr, IDC_ARROW);
- windowClass.style = CS_HREDRAW | CS_VREDRAW;
- RegisterClassW(&windowClass);
- const auto windowStyle = WS_VISIBLE | WS_CAPTION | WS_MINIMIZEBOX | WS_SYSMENU | WS_OVERLAPPED;
- auto windowX = GetSystemMetrics(SM_CXSCREEN) / 2 - WindowWidth / 2;
- auto windowY = GetSystemMetrics(SM_CYSCREEN) / 2 - WindowHeight / 2;
- RECT windowRect{windowX, windowY, windowX + WindowWidth, windowY + WindowHeight};
- AdjustWindowRect(&windowRect, windowStyle, false);
- auto window = CreateWindowW(
- WindowClassName.c_str(),
- L"DXGI Bug Test",
- windowStyle,
- windowRect.left,
- windowRect.top,
- windowRect.right - windowRect.left,
- windowRect.bottom - windowRect.top,
- nullptr,
- nullptr,
- GetModuleHandleW(nullptr),
- nullptr
- );
- UINT deviceFlags = 0;
- #ifdef _DEBUG
- deviceFlags |= D3D11_CREATE_DEVICE_DEBUG;
- #endif
- D3D11CreateDevice(
- nullptr,
- D3D_DRIVER_TYPE_HARDWARE,
- nullptr,
- deviceFlags,
- nullptr, 0,
- D3D11_SDK_VERSION,
- &device,
- nullptr,
- &context
- );
- ComPtr<IDXGIDevice> dxgiDevice;
- device->QueryInterface(IID_PPV_ARGS(&dxgiDevice));
- ComPtr<IDXGIAdapter> adapter;
- dxgiDevice->GetParent(IID_PPV_ARGS(&adapter));
- ComPtr<IDXGIFactory> factory;
- adapter->GetParent(IID_PPV_ARGS(&factory));
- DXGI_SWAP_CHAIN_DESC swapChainDesc;
- swapChainDesc.BufferCount = 1;
- swapChainDesc.BufferDesc.Format = DXGI_FORMAT_R8G8B8A8_UNORM;
- swapChainDesc.BufferDesc.Width = WindowWidth;
- swapChainDesc.BufferDesc.Height = WindowHeight;
- swapChainDesc.BufferDesc.RefreshRate.Numerator = 0;
- swapChainDesc.BufferDesc.RefreshRate.Denominator = 0;
- swapChainDesc.BufferDesc.Scaling = DXGI_MODE_SCALING_UNSPECIFIED;
- swapChainDesc.BufferDesc.ScanlineOrdering = DXGI_MODE_SCANLINE_ORDER_UNSPECIFIED;
- swapChainDesc.BufferUsage = DXGI_USAGE_RENDER_TARGET_OUTPUT;
- swapChainDesc.Flags = DXGI_SWAP_CHAIN_FLAG_ALLOW_MODE_SWITCH;
- swapChainDesc.OutputWindow = window;
- swapChainDesc.SampleDesc.Count = 1;
- swapChainDesc.SampleDesc.Quality = 0;
- swapChainDesc.SwapEffect = DXGI_SWAP_EFFECT_DISCARD;
- swapChainDesc.Windowed = true;
- factory->CreateSwapChain(device, &swapChainDesc, &swapChain);
- OnResize(WindowWidth, WindowHeight);
- MSG message{ };
- while (isRunning)
- {
- if (PeekMessageW(&message, nullptr, 0, 0, PM_REMOVE))
- {
- TranslateMessage(&message);
- DispatchMessageW(&message);
- }
- else
- {
- context->ClearRenderTargetView(renderTargetView, DirectX::Colors::CornflowerBlue);
- swapChain->Present(0, 0);
- }
- }
- return 0;
- }
- LRESULT CALLBACK WindowProc(HWND handle, UINT message, WPARAM wParam, LPARAM lParam)
- {
- switch (message)
- {
- case WM_CLOSE:
- isRunning = false;
- break;
- case WM_SIZE:
- OnResize(LOWORD(lParam), HIWORD(lParam));
- break;
- default:
- return DefWindowProcW(handle, message, wParam, lParam);
- }
- return 0;
- }
- void OnResize(int width, int height)
- {
- if (swapChain && width > 0 && height > 0)
- {
- if (renderTargetView)
- {
- context->OMSetRenderTargets(0, nullptr, nullptr);
- renderTargetView.Release();
- swapChain->ResizeBuffers(0, 0, 0, DXGI_FORMAT_UNKNOWN, DXGI_SWAP_CHAIN_FLAG_ALLOW_MODE_SWITCH);
- }
- ComPtr<ID3D11Texture2D> backBuffer;
- swapChain->GetBuffer(0, IID_PPV_ARGS(&backBuffer));
- device->CreateRenderTargetView(backBuffer, nullptr, &renderTargetView);
- context->OMSetRenderTargets(1, &renderTargetView, nullptr);
- D3D11_VIEWPORT viewport{ };
- viewport.Width = static_cast<float>(width);
- viewport.Height = static_cast<float>(height);
- viewport.MaxDepth = 1.0f;
- context->RSSetViewports(1, &viewport);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement