Advertisement
Guest User

Untitled

a guest
May 12th, 2015
164
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 10.26 KB | None | 0 0
  1. /***********************************************
  2. * Filename:         Renderer.cpp
  3. * Date:             05/12/2015
  4. * Mod. Date:
  5. * Mod. Initials:
  6. * Author:          
  7. * Purpose:          The .cpp file associated with DirectX11
  8. ************************************************/
  9.  
  10. #include "Renderer.h"
  11.  
  12. //DirectX11 includes
  13. #include <DirectXMath.h>
  14. #include <DirectXColors.h>
  15.  
  16. //c++ schtuff
  17. #include <algorithm>
  18. #include <exception>
  19. #include <memory>
  20.  
  21. //DirectX11 library linking
  22. #pragma comment(lib, "DXGI.lib" )
  23. #pragma comment(lib, "d3d11.lib" )
  24. #pragma comment(lib, "D3DCompiler.lib")
  25.  
  26. /*****************************************************************
  27. * CRenderer():
  28. *
  29. * Ins:
  30. *
  31. * Outs:
  32. *
  33. * Returns:
  34. *
  35. * Mod. Date:              05/12/2015
  36. * Mod. Initials:
  37. *****************************************************************/
  38. CRenderer::CRenderer()
  39. {
  40. }
  41.  
  42. /*****************************************************************
  43. * ~CRenderer():
  44. *
  45. * Ins:
  46. *
  47. * Outs:
  48. *
  49. * Returns:
  50. *
  51. * Mod. Date:              05/12/2015
  52. * Mod. Initials:
  53. *****************************************************************/
  54. CRenderer::~CRenderer()
  55. {
  56. }
  57.  
  58.  
  59. /*****************************************************************
  60. * Initialize():     Initialize DirectX11 for to Rendering. This function is
  61. *                       responsible for creating the basis for drawing to the screen.
  62. *                       essentially, this function creates Device-Dependent objects.
  63. *
  64. * Ins:              HWND - the Handle to the window to Render to
  65. *                   UINT - the Width of the window
  66. *                   UINT - the Height of the window
  67. *
  68. * Outs:             (void)
  69. *
  70. * Returns:          bool
  71. *
  72. * Mod. Date:        05/12/2015
  73. * Mod. Initials:
  74. *****************************************************************/
  75. /*static*/ bool CRenderer::Initialize(HWND hWnd,
  76.                                       UINT unWidth,
  77.                                       UINT unHeight)
  78. {
  79.     m_unWidth = unWidth;
  80.     m_unHeight = unHeight;
  81.  
  82.     UINT creationFlags = D3D11_CREATE_DEVICE_SINGLETHREADED;
  83.  
  84. #ifdef _DEBUG
  85.     creationFlags |= D3D11_CREATE_DEVICE_DEBUG;
  86. #endif
  87.  
  88.     const D3D_FEATURE_LEVEL featureLevels[] =
  89.     {
  90.         D3D_FEATURE_LEVEL_11_1,
  91.         D3D_FEATURE_LEVEL_11_0,
  92.         D3D_FEATURE_LEVEL_10_1,
  93.         D3D_FEATURE_LEVEL_10_0,
  94.         D3D_FEATURE_LEVEL_9_3,
  95.         D3D_FEATURE_LEVEL_9_2,
  96.         D3D_FEATURE_LEVEL_9_1,
  97.     };
  98.  
  99.     //since *technically* the SwapChain is almost immediately released [WM_SIZE],
  100.     //  it's rather pointless to create one. i mean like i'll change it
  101.     //  if that's the reason my thing was crashing...i doubt it though.
  102.     //      [I may have to regardless because of constant buffer creation,
  103.     //          but we'll see]
  104.     //
  105.  
  106.     HRESULT hr = D3D11CreateDevice(
  107.         nullptr,                                // specify null to use the default adapter
  108.         D3D_DRIVER_TYPE_HARDWARE,
  109.         nullptr,                                // leave as nullptr unless software device
  110.         creationFlags,                          // optionally set debug and Direct2D compatibility flags
  111.         featureLevels,                          // list of feature levels this app can support
  112.         _countof(featureLevels),                // number of entries in above list
  113.         D3D11_SDK_VERSION,                      // always set this to D3D11_SDK_VERSION
  114.         &m_d3dDevice,                           // returns the Direct3D device created
  115.         &m_tFeatureLevel,                        // returns feature level of device created
  116.         &m_d3dContext                           // returns the device immediate context
  117.         );
  118.  
  119.     if (hr == E_INVALIDARG)
  120.     {
  121.         //if 11.1 isn't supported, fallback to 11.0
  122.         hr = D3D11CreateDevice(nullptr,
  123.                                D3D_DRIVER_TYPE_HARDWARE,
  124.                                nullptr,
  125.                                creationFlags,
  126.                                &featureLevels[1],
  127.                                _countof(featureLevels) - 1,
  128.                                D3D11_SDK_VERSION,
  129.                                &m_d3dDevice,
  130.                                &m_tFeatureLevel,
  131.                                &m_d3dContext
  132.                                );
  133.     }
  134.  
  135.     //if device creation failed again then like well *shrug*
  136.     HR(hr);
  137.  
  138.     // TODO: Initialize device dependent objects here (independent of window size)
  139.     //          [Rasterizer State (for instance)]
  140.     //          [Other States]
  141. }
  142.  
  143. /*****************************************************************
  144. * ResizeBuffers():  Responsible for creating and/or resizing what depends on
  145. *                       the size of a window:
  146. *                           [Render Target Views]
  147. *                           [Depth Stencil View]
  148. *                           [Back Buffer]
  149. *                           [Swap Chain]
  150. *                           [Viewport]
  151. *                           [Camera]
  152. *
  153. * Ins:              (void)
  154. *
  155. * Outs:             (void)
  156. *
  157. * Returns:          (void)
  158. *
  159. * Mod. Date:        05/12/2015
  160. * Mod. Initials:
  161. *****************************************************************/
  162. /*static*/ void CRenderer::ResizeBuffers()
  163. {
  164.     //Now this method is the Meat and Potatoes of the initialization.
  165.     //  Since a [WM_SIZE] message is sent upon window creation, we can call
  166.     //  this function almost immediately to finish initializing DirectX11.
  167.     //      Mainly, this function will be initializing a Render Target View,
  168.     //      the Depth Stencil Buffer & View, and the Back Buffer.
  169.     //      It will also create the Swap Chain [if it doesn't already exist]
  170.  
  171.     //so first we have to clear the previous size-specific stuff [reference to the swap chain]
  172.     ID3D11RenderTargetView* null[] = { nullptr, };
  173.     m_d3dContext->OMSetRenderTargets(_countof(null), null, nullptr);
  174.     SafeRelease(m_d3dRenderTargetView);
  175.  
  176.     SafeRelease(m_d3dDepthStencilBuffer);
  177.     SafeRelease(m_d3dDepthStencilView);
  178.  
  179.     SafeRelease(m_d3dBackBuffer);
  180.  
  181.     //clearing the context wipes the current pipeline,
  182.     //  and flush destroys deferred destroyed stuff [shouldn't actually need to do the Flush]
  183.     m_d3dContext->ClearState();
  184.     m_d3dContext->Flush();
  185.  
  186.     //if the Swap Chain already exists, there's no reason to make a new one
  187.     //  calling ResizeBuffers should take care of it.
  188.     if (m_d3dSwapChain)
  189.     {
  190.         HRESULT hr = m_d3dSwapChain->ResizeBuffers(0, m_unWidth, m_unHeight, DXGI_FORMAT_UNKNOWN, /*DXGI_SWAP_CHAIN_FLAG_ALLOW_MODE_SWITCH*/0);
  191.  
  192.         if (hr == DXGI_ERROR_DEVICE_REMOVED || hr == DXGI_ERROR_DEVICE_RESET)
  193.         {
  194.             //if we get here, an uh-oh happened. we have to destroy it all *lol*
  195.             //  but we can recreate new stuff so that's cool.
  196.             return;
  197.         }
  198.         else
  199.         {
  200.             HR(hr);
  201.         }
  202.     }
  203.     else
  204.     {
  205.         // First, retrieve the underlying DXGI Device from the D3D Device
  206.         IDXGIDevice* pDXGIDevice;
  207.         HR(m_d3dDevice->QueryInterface(__uuidof(IDXGIDevice), (void **)&pDXGIDevice));
  208.  
  209.         // Identify the physical adapter (GPU or card) this device is running on.
  210.         IDXGIAdapter* pDXGIAdapter;
  211.         HR(pDXGIDevice->GetParent(__uuidof(IDXGIAdapter), (void **)&pDXGIAdapter));
  212.  
  213.         // And obtain the factory object that created it.
  214.         IDXGIFactory* pIDXGIFactory;
  215.         HR(pDXGIAdapter->GetParent(__uuidof(IDXGIFactory), (void **)&pIDXGIFactory));
  216.  
  217.         // Creating just the swap chain, with the Render_Target_Output so it can be bound as an SRV
  218.         DXGI_SWAP_CHAIN_DESC swapChainDesc = { 0 };
  219.         swapChainDesc.BufferCount = 2;
  220.         swapChainDesc.BufferDesc.Width = m_unWidth;
  221.         swapChainDesc.BufferDesc.Height = m_unHeight;
  222.         swapChainDesc.BufferDesc.Format = DXGI_FORMAT_R8G8B8A8_UNORM;
  223.         swapChainDesc.BufferUsage = DXGI_USAGE_RENDER_TARGET_OUTPUT;
  224.         swapChainDesc.OutputWindow = m_window;
  225.         swapChainDesc.SampleDesc.Count = 1;
  226.         swapChainDesc.SampleDesc.Quality = 0;
  227.         swapChainDesc.Windowed = TRUE;
  228.         //swapChainDesc.Flags = DXGI_SWAP_CHAIN_FLAG_ALLOW_MODE_SWITCH;
  229.  
  230.         HR(pIDXGIFactory->CreateSwapChain(m_d3dDevice, &swapChainDesc, &m_d3dSwapChain));
  231.     }
  232.  
  233.     // so, now that the swap chain is resized or created, we can re/make everything.
  234.  
  235.     // obtain the backbuffer
  236.     HR(m_d3dSwapChain->GetBuffer(0, __uuidof(ID3D11Texture2D), (LPVOID*)&m_d3dBackBuffer));
  237.  
  238.     // create a render target view
  239.     HR(m_d3dDevice->CreateRenderTargetView(m_d3dBackBuffer, nullptr, &m_d3dRenderTargetView));
  240.  
  241.     // create the depth stencil view
  242.     DXGI_SWAP_CHAIN_DESC swapChainDesc = {};
  243.     m_d3dSwapChain->GetDesc(&swapChainDesc);
  244.  
  245.     CD3D11_TEXTURE2D_DESC depthBufferDesc(DXGI_FORMAT_R24G8_TYPELESS,
  246.                                           swapChainDesc.BufferDesc.Width,
  247.                                           swapChainDesc.BufferDesc.Height);
  248.     depthBufferDesc.BindFlags = D3D11_BIND_DEPTH_STENCIL | D3D11_BIND_SHADER_RESOURCE;
  249.  
  250.     m_d3dDevice->CreateTexture2D(
  251.         &depthBufferDesc,
  252.         nullptr,
  253.         &m_d3dDepthStencilBuffer);
  254.  
  255.     CD3D11_DEPTH_STENCIL_VIEW_DESC depthStencilViewDesc(D3D11_DSV_DIMENSION_TEXTURE2D);
  256.     depthStencilViewDesc.Format = DXGI_FORMAT_D24_UNORM_S8_UINT;
  257.  
  258.     m_d3dDevice->CreateDepthStencilView(m_d3dDepthStencilBuffer,
  259.                                         &depthStencilViewDesc,
  260.                                         &m_d3dDepthStencilView);
  261.  
  262.     //create the viewport to be full size
  263.     m_tScreenViewport = CD3D11_VIEWPORT(0.0f, 0.0f, static_cast<float>(m_unWidth), static_cast<float>(m_unHeight));
  264.  
  265.     // set the viewport
  266.     m_d3dContext->RSSetViewports(1, &m_tScreenViewport);
  267.  
  268.     // TODO: Initialize windows-size dependent objects here
  269.  
  270. }
  271.  
  272. /*****************************************************************
  273. * SetResolution():  Gets a window width/height, then calls ResizeBuffers
  274. *
  275. * Ins:              UINT - the width of the window
  276. *                   UINT - the Height of the window
  277. *
  278. * Outs:             (void)
  279. *
  280. * Returns:          (void)
  281. *
  282. * Mod. Date:        05/12/2015
  283. * Mod. Initials:
  284. *****************************************************************/
  285. /*static*/ void CRenderer::SetResolution(UINT unWidth,
  286.                                          UINT unHeight)
  287. {
  288.     m_unWidth = unWidth;
  289.     m_unHeight = unHeight;
  290.  
  291.     ResizeBuffers();
  292. }
  293.  
  294. /*****************************************************************
  295. * Shutdown():       Cleans up DirectX11
  296. *
  297. * Ins:              (void)
  298. *
  299. * Outs:             (void)
  300. *
  301. * Returns:          (void)
  302. *
  303. * Mod. Date:        05/12/2015
  304. * Mod. Initials:
  305. *****************************************************************/
  306. /*static*/ void CRenderer::Shutdown()
  307. {
  308.     SafeRelease(m_d3dDepthStencilView);
  309.     SafeRelease(m_d3dDepthStencilBuffer);
  310.     SafeRelease(m_d3dRenderTargetView);
  311.     SafeRelease(m_d3dBackBuffer);
  312.     SafeRelease(m_d3dDepthStencilSRV);
  313.  
  314.     SafeRelease(m_d3dSwapChain);
  315.     SafeRelease(m_d3dContext);
  316.     SafeRelease(m_d3dDevice);
  317. }
  318.  
  319. //Basic Initialization
  320. //
  321. ID3D11Device*               CRenderer::m_d3dDevice;
  322. ID3D11DeviceContext*        CRenderer::m_d3dContext;
  323. IDXGISwapChain*             CRenderer::m_d3dSwapChain;
  324.  
  325. ID3D11RenderTargetView*     CRenderer::m_d3dRenderTargetView;
  326. ID3D11Texture2D*            CRenderer::m_d3dBackBuffer;
  327.  
  328. ID3D11DepthStencilView*     CRenderer::m_d3dDepthStencilView;
  329. ID3D11Texture2D*            CRenderer::m_d3dDepthStencilBuffer;
  330.  
  331. D3D11_VIEWPORT              CRenderer::m_tScreenViewport;
  332. //                         
  333. //Basic Initialization     
  334.  
  335. ID3D11ShaderResourceView*   CRenderer::m_d3dDepthStencilSRV;
  336.  
  337. UINT                        CRenderer::m_unWidth;
  338. UINT                        CRenderer::m_unHeight;
  339.  
  340. HWND                        CRenderer::m_window;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement