Advertisement
Guest User

DXGIBugTest

a guest
Dec 14th, 2013
230
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.91 KB | None | 0 0
  1. #include <Windows.h>
  2. #include <dxgi.h>
  3. #include <d3d11.h>
  4. #include <DirectXColors.h>
  5. #include <string>
  6.  
  7. #pragma comment(lib, "dxgi.lib")
  8. #pragma comment(lib, "d3d11.lib")
  9.  
  10. template <typename T>
  11. class ComPtr
  12. {
  13. public:
  14.     explicit ComPtr(T* ptr = nullptr) : ptr{ptr} { }
  15.     ~ComPtr() { if (ptr) ptr->Release(); }
  16.     void Release() { if (ptr) ptr->Release(); ptr = nullptr; }
  17.     T* operator->() const { return ptr; }
  18.     T* const* operator&() const { return &ptr; }
  19.     T** operator&() { return &ptr; }
  20.     operator T*() const { return ptr; }
  21. private:
  22.     T* ptr;
  23. };
  24.  
  25. LRESULT CALLBACK WindowProc(HWND handle, UINT message, WPARAM wParam, LPARAM lParam);
  26. void OnResize(int width, int height);
  27.  
  28. const std::wstring WindowClassName{L"DXGIBugTestWindow"};
  29. const int WindowWidth = 800;
  30. const int WindowHeight = 600;
  31.  
  32. bool isRunning = true;
  33. ComPtr<ID3D11Device> device;
  34. ComPtr<ID3D11DeviceContext> context;
  35. ComPtr<IDXGISwapChain> swapChain;
  36. ComPtr<ID3D11RenderTargetView> renderTargetView;
  37.  
  38. int WINAPI wWinMain(HINSTANCE, HINSTANCE, PWSTR, int)
  39. {
  40.     WNDCLASSW windowClass{ };
  41.     windowClass.lpszClassName = WindowClassName.c_str();
  42.     windowClass.lpfnWndProc = &WindowProc;
  43.     windowClass.hInstance = GetModuleHandleW(nullptr);
  44.     windowClass.hbrBackground = reinterpret_cast<HBRUSH>(COLOR_WINDOW + 1);
  45.     windowClass.hIcon = LoadIconW(nullptr, IDI_APPLICATION);
  46.     windowClass.hCursor = LoadCursorW(nullptr, IDC_ARROW);
  47.     windowClass.style = CS_HREDRAW | CS_VREDRAW;
  48.     RegisterClassW(&windowClass);
  49.  
  50.     const auto windowStyle = WS_VISIBLE | WS_CAPTION | WS_MINIMIZEBOX | WS_SYSMENU | WS_OVERLAPPED;
  51.     auto windowX = GetSystemMetrics(SM_CXSCREEN) / 2 - WindowWidth / 2;
  52.     auto windowY = GetSystemMetrics(SM_CYSCREEN) / 2 - WindowHeight / 2;
  53.  
  54.     RECT windowRect{windowX, windowY, windowX + WindowWidth, windowY + WindowHeight};
  55.     AdjustWindowRect(&windowRect, windowStyle, false);
  56.  
  57.     auto window = CreateWindowW(
  58.         WindowClassName.c_str(),
  59.         L"DXGI Bug Test",
  60.         windowStyle,
  61.         windowRect.left,
  62.         windowRect.top,
  63.         windowRect.right - windowRect.left,
  64.         windowRect.bottom - windowRect.top,
  65.         nullptr,
  66.         nullptr,
  67.         GetModuleHandleW(nullptr),
  68.         nullptr
  69.         );
  70.  
  71.     UINT deviceFlags = 0;
  72. #ifdef _DEBUG
  73.     deviceFlags |= D3D11_CREATE_DEVICE_DEBUG;
  74. #endif
  75.    
  76.     D3D11CreateDevice(
  77.         nullptr,
  78.         D3D_DRIVER_TYPE_HARDWARE,
  79.         nullptr,
  80.         deviceFlags,
  81.         nullptr, 0,
  82.         D3D11_SDK_VERSION,
  83.         &device,
  84.         nullptr,
  85.         &context
  86.         );
  87.  
  88.     ComPtr<IDXGIDevice> dxgiDevice;
  89.     device->QueryInterface(IID_PPV_ARGS(&dxgiDevice));
  90.  
  91.     ComPtr<IDXGIAdapter> adapter;
  92.     dxgiDevice->GetParent(IID_PPV_ARGS(&adapter));
  93.  
  94.     ComPtr<IDXGIFactory> factory;
  95.     adapter->GetParent(IID_PPV_ARGS(&factory));
  96.  
  97.     DXGI_SWAP_CHAIN_DESC swapChainDesc;
  98.     swapChainDesc.BufferCount = 1;
  99.     swapChainDesc.BufferDesc.Format = DXGI_FORMAT_R8G8B8A8_UNORM;
  100.     swapChainDesc.BufferDesc.Width = WindowWidth;
  101.     swapChainDesc.BufferDesc.Height = WindowHeight;
  102.     swapChainDesc.BufferDesc.RefreshRate.Numerator = 0;
  103.     swapChainDesc.BufferDesc.RefreshRate.Denominator = 0;
  104.     swapChainDesc.BufferDesc.Scaling = DXGI_MODE_SCALING_UNSPECIFIED;
  105.     swapChainDesc.BufferDesc.ScanlineOrdering = DXGI_MODE_SCANLINE_ORDER_UNSPECIFIED;
  106.     swapChainDesc.BufferUsage = DXGI_USAGE_RENDER_TARGET_OUTPUT;
  107.     swapChainDesc.Flags = DXGI_SWAP_CHAIN_FLAG_ALLOW_MODE_SWITCH;
  108.     swapChainDesc.OutputWindow = window;
  109.     swapChainDesc.SampleDesc.Count = 1;
  110.     swapChainDesc.SampleDesc.Quality = 0;
  111.     swapChainDesc.SwapEffect = DXGI_SWAP_EFFECT_DISCARD;
  112.     swapChainDesc.Windowed = true;
  113.  
  114.     factory->CreateSwapChain(device, &swapChainDesc, &swapChain);
  115.     OnResize(WindowWidth, WindowHeight);
  116.  
  117.     MSG message{ };
  118.     while (isRunning)
  119.     {
  120.         if (PeekMessageW(&message, nullptr, 0, 0, PM_REMOVE))
  121.         {
  122.             TranslateMessage(&message);
  123.             DispatchMessageW(&message);
  124.         }
  125.         else
  126.         {
  127.             context->ClearRenderTargetView(renderTargetView, DirectX::Colors::CornflowerBlue);
  128.             swapChain->Present(0, 0);
  129.         }
  130.     }
  131.  
  132.     return 0;
  133. }
  134.  
  135. LRESULT CALLBACK WindowProc(HWND handle, UINT message, WPARAM wParam, LPARAM lParam)
  136. {
  137.     switch (message)
  138.     {
  139.     case WM_CLOSE:
  140.         isRunning = false;
  141.         break;
  142.  
  143.     case WM_SIZE:
  144.         OnResize(LOWORD(lParam), HIWORD(lParam));
  145.         break;
  146.  
  147.     default:
  148.         return DefWindowProcW(handle, message, wParam, lParam);
  149.     }
  150.  
  151.     return 0;
  152. }
  153.  
  154. void OnResize(int width, int height)
  155. {
  156.     if (swapChain && width > 0 && height > 0)
  157.     {
  158.         if (renderTargetView)
  159.         {
  160.             context->OMSetRenderTargets(0, nullptr, nullptr);
  161.             renderTargetView.Release();
  162.             swapChain->ResizeBuffers(0, 0, 0, DXGI_FORMAT_UNKNOWN, DXGI_SWAP_CHAIN_FLAG_ALLOW_MODE_SWITCH);
  163.         }
  164.  
  165.         ComPtr<ID3D11Texture2D> backBuffer;
  166.         swapChain->GetBuffer(0, IID_PPV_ARGS(&backBuffer));
  167.         device->CreateRenderTargetView(backBuffer, nullptr, &renderTargetView);
  168.         context->OMSetRenderTargets(1, &renderTargetView, nullptr);
  169.  
  170.         D3D11_VIEWPORT viewport{ };
  171.         viewport.Width = static_cast<float>(width);
  172.         viewport.Height = static_cast<float>(height);
  173.         viewport.MaxDepth = 1.0f;
  174.         context->RSSetViewports(1, &viewport);
  175.     }
  176. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement