Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /***********************************************
- * Filename: Renderer.cpp
- * Date: 05/12/2015
- * Mod. Date:
- * Mod. Initials:
- * Author:
- * Purpose: The .cpp file associated with DirectX11
- ************************************************/
- #include "Renderer.h"
- //DirectX11 includes
- #include <DirectXMath.h>
- #include <DirectXColors.h>
- //c++ schtuff
- #include <algorithm>
- #include <exception>
- #include <memory>
- //DirectX11 library linking
- #pragma comment(lib, "DXGI.lib" )
- #pragma comment(lib, "d3d11.lib" )
- #pragma comment(lib, "D3DCompiler.lib")
- /*****************************************************************
- * CRenderer():
- *
- * Ins:
- *
- * Outs:
- *
- * Returns:
- *
- * Mod. Date: 05/12/2015
- * Mod. Initials:
- *****************************************************************/
- CRenderer::CRenderer()
- {
- }
- /*****************************************************************
- * ~CRenderer():
- *
- * Ins:
- *
- * Outs:
- *
- * Returns:
- *
- * Mod. Date: 05/12/2015
- * Mod. Initials:
- *****************************************************************/
- CRenderer::~CRenderer()
- {
- }
- /*****************************************************************
- * Initialize(): Initialize DirectX11 for to Rendering. This function is
- * responsible for creating the basis for drawing to the screen.
- * essentially, this function creates Device-Dependent objects.
- *
- * Ins: HWND - the Handle to the window to Render to
- * UINT - the Width of the window
- * UINT - the Height of the window
- *
- * Outs: (void)
- *
- * Returns: bool
- *
- * Mod. Date: 05/12/2015
- * Mod. Initials:
- *****************************************************************/
- /*static*/ bool CRenderer::Initialize(HWND hWnd,
- UINT unWidth,
- UINT unHeight)
- {
- m_unWidth = unWidth;
- m_unHeight = unHeight;
- UINT creationFlags = D3D11_CREATE_DEVICE_SINGLETHREADED;
- #ifdef _DEBUG
- creationFlags |= D3D11_CREATE_DEVICE_DEBUG;
- #endif
- const D3D_FEATURE_LEVEL featureLevels[] =
- {
- D3D_FEATURE_LEVEL_11_1,
- D3D_FEATURE_LEVEL_11_0,
- D3D_FEATURE_LEVEL_10_1,
- D3D_FEATURE_LEVEL_10_0,
- D3D_FEATURE_LEVEL_9_3,
- D3D_FEATURE_LEVEL_9_2,
- D3D_FEATURE_LEVEL_9_1,
- };
- //since *technically* the SwapChain is almost immediately released [WM_SIZE],
- // it's rather pointless to create one. i mean like i'll change it
- // if that's the reason my thing was crashing...i doubt it though.
- // [I may have to regardless because of constant buffer creation,
- // but we'll see]
- //
- HRESULT hr = D3D11CreateDevice(
- nullptr, // specify null to use the default adapter
- D3D_DRIVER_TYPE_HARDWARE,
- nullptr, // leave as nullptr unless software device
- creationFlags, // optionally set debug and Direct2D compatibility flags
- featureLevels, // list of feature levels this app can support
- _countof(featureLevels), // number of entries in above list
- D3D11_SDK_VERSION, // always set this to D3D11_SDK_VERSION
- &m_d3dDevice, // returns the Direct3D device created
- &m_tFeatureLevel, // returns feature level of device created
- &m_d3dContext // returns the device immediate context
- );
- if (hr == E_INVALIDARG)
- {
- //if 11.1 isn't supported, fallback to 11.0
- hr = D3D11CreateDevice(nullptr,
- D3D_DRIVER_TYPE_HARDWARE,
- nullptr,
- creationFlags,
- &featureLevels[1],
- _countof(featureLevels) - 1,
- D3D11_SDK_VERSION,
- &m_d3dDevice,
- &m_tFeatureLevel,
- &m_d3dContext
- );
- }
- //if device creation failed again then like well *shrug*
- HR(hr);
- // TODO: Initialize device dependent objects here (independent of window size)
- // [Rasterizer State (for instance)]
- // [Other States]
- }
- /*****************************************************************
- * ResizeBuffers(): Responsible for creating and/or resizing what depends on
- * the size of a window:
- * [Render Target Views]
- * [Depth Stencil View]
- * [Back Buffer]
- * [Swap Chain]
- * [Viewport]
- * [Camera]
- *
- * Ins: (void)
- *
- * Outs: (void)
- *
- * Returns: (void)
- *
- * Mod. Date: 05/12/2015
- * Mod. Initials:
- *****************************************************************/
- /*static*/ void CRenderer::ResizeBuffers()
- {
- //Now this method is the Meat and Potatoes of the initialization.
- // Since a [WM_SIZE] message is sent upon window creation, we can call
- // this function almost immediately to finish initializing DirectX11.
- // Mainly, this function will be initializing a Render Target View,
- // the Depth Stencil Buffer & View, and the Back Buffer.
- // It will also create the Swap Chain [if it doesn't already exist]
- //so first we have to clear the previous size-specific stuff [reference to the swap chain]
- ID3D11RenderTargetView* null[] = { nullptr, };
- m_d3dContext->OMSetRenderTargets(_countof(null), null, nullptr);
- SafeRelease(m_d3dRenderTargetView);
- SafeRelease(m_d3dDepthStencilBuffer);
- SafeRelease(m_d3dDepthStencilView);
- SafeRelease(m_d3dBackBuffer);
- //clearing the context wipes the current pipeline,
- // and flush destroys deferred destroyed stuff [shouldn't actually need to do the Flush]
- m_d3dContext->ClearState();
- m_d3dContext->Flush();
- //if the Swap Chain already exists, there's no reason to make a new one
- // calling ResizeBuffers should take care of it.
- if (m_d3dSwapChain)
- {
- HRESULT hr = m_d3dSwapChain->ResizeBuffers(0, m_unWidth, m_unHeight, DXGI_FORMAT_UNKNOWN, /*DXGI_SWAP_CHAIN_FLAG_ALLOW_MODE_SWITCH*/0);
- if (hr == DXGI_ERROR_DEVICE_REMOVED || hr == DXGI_ERROR_DEVICE_RESET)
- {
- //if we get here, an uh-oh happened. we have to destroy it all *lol*
- // but we can recreate new stuff so that's cool.
- return;
- }
- else
- {
- HR(hr);
- }
- }
- else
- {
- // First, retrieve the underlying DXGI Device from the D3D Device
- IDXGIDevice* pDXGIDevice;
- HR(m_d3dDevice->QueryInterface(__uuidof(IDXGIDevice), (void **)&pDXGIDevice));
- // Identify the physical adapter (GPU or card) this device is running on.
- IDXGIAdapter* pDXGIAdapter;
- HR(pDXGIDevice->GetParent(__uuidof(IDXGIAdapter), (void **)&pDXGIAdapter));
- // And obtain the factory object that created it.
- IDXGIFactory* pIDXGIFactory;
- HR(pDXGIAdapter->GetParent(__uuidof(IDXGIFactory), (void **)&pIDXGIFactory));
- // Creating just the swap chain, with the Render_Target_Output so it can be bound as an SRV
- DXGI_SWAP_CHAIN_DESC swapChainDesc = { 0 };
- swapChainDesc.BufferCount = 2;
- swapChainDesc.BufferDesc.Width = m_unWidth;
- swapChainDesc.BufferDesc.Height = m_unHeight;
- swapChainDesc.BufferDesc.Format = DXGI_FORMAT_R8G8B8A8_UNORM;
- swapChainDesc.BufferUsage = DXGI_USAGE_RENDER_TARGET_OUTPUT;
- swapChainDesc.OutputWindow = m_window;
- swapChainDesc.SampleDesc.Count = 1;
- swapChainDesc.SampleDesc.Quality = 0;
- swapChainDesc.Windowed = TRUE;
- //swapChainDesc.Flags = DXGI_SWAP_CHAIN_FLAG_ALLOW_MODE_SWITCH;
- HR(pIDXGIFactory->CreateSwapChain(m_d3dDevice, &swapChainDesc, &m_d3dSwapChain));
- }
- // so, now that the swap chain is resized or created, we can re/make everything.
- // obtain the backbuffer
- HR(m_d3dSwapChain->GetBuffer(0, __uuidof(ID3D11Texture2D), (LPVOID*)&m_d3dBackBuffer));
- // create a render target view
- HR(m_d3dDevice->CreateRenderTargetView(m_d3dBackBuffer, nullptr, &m_d3dRenderTargetView));
- // create the depth stencil view
- DXGI_SWAP_CHAIN_DESC swapChainDesc = {};
- m_d3dSwapChain->GetDesc(&swapChainDesc);
- CD3D11_TEXTURE2D_DESC depthBufferDesc(DXGI_FORMAT_R24G8_TYPELESS,
- swapChainDesc.BufferDesc.Width,
- swapChainDesc.BufferDesc.Height);
- depthBufferDesc.BindFlags = D3D11_BIND_DEPTH_STENCIL | D3D11_BIND_SHADER_RESOURCE;
- m_d3dDevice->CreateTexture2D(
- &depthBufferDesc,
- nullptr,
- &m_d3dDepthStencilBuffer);
- CD3D11_DEPTH_STENCIL_VIEW_DESC depthStencilViewDesc(D3D11_DSV_DIMENSION_TEXTURE2D);
- depthStencilViewDesc.Format = DXGI_FORMAT_D24_UNORM_S8_UINT;
- m_d3dDevice->CreateDepthStencilView(m_d3dDepthStencilBuffer,
- &depthStencilViewDesc,
- &m_d3dDepthStencilView);
- //create the viewport to be full size
- m_tScreenViewport = CD3D11_VIEWPORT(0.0f, 0.0f, static_cast<float>(m_unWidth), static_cast<float>(m_unHeight));
- // set the viewport
- m_d3dContext->RSSetViewports(1, &m_tScreenViewport);
- // TODO: Initialize windows-size dependent objects here
- }
- /*****************************************************************
- * SetResolution(): Gets a window width/height, then calls ResizeBuffers
- *
- * Ins: UINT - the width of the window
- * UINT - the Height of the window
- *
- * Outs: (void)
- *
- * Returns: (void)
- *
- * Mod. Date: 05/12/2015
- * Mod. Initials:
- *****************************************************************/
- /*static*/ void CRenderer::SetResolution(UINT unWidth,
- UINT unHeight)
- {
- m_unWidth = unWidth;
- m_unHeight = unHeight;
- ResizeBuffers();
- }
- /*****************************************************************
- * Shutdown(): Cleans up DirectX11
- *
- * Ins: (void)
- *
- * Outs: (void)
- *
- * Returns: (void)
- *
- * Mod. Date: 05/12/2015
- * Mod. Initials:
- *****************************************************************/
- /*static*/ void CRenderer::Shutdown()
- {
- SafeRelease(m_d3dDepthStencilView);
- SafeRelease(m_d3dDepthStencilBuffer);
- SafeRelease(m_d3dRenderTargetView);
- SafeRelease(m_d3dBackBuffer);
- SafeRelease(m_d3dDepthStencilSRV);
- SafeRelease(m_d3dSwapChain);
- SafeRelease(m_d3dContext);
- SafeRelease(m_d3dDevice);
- }
- //Basic Initialization
- //
- ID3D11Device* CRenderer::m_d3dDevice;
- ID3D11DeviceContext* CRenderer::m_d3dContext;
- IDXGISwapChain* CRenderer::m_d3dSwapChain;
- ID3D11RenderTargetView* CRenderer::m_d3dRenderTargetView;
- ID3D11Texture2D* CRenderer::m_d3dBackBuffer;
- ID3D11DepthStencilView* CRenderer::m_d3dDepthStencilView;
- ID3D11Texture2D* CRenderer::m_d3dDepthStencilBuffer;
- D3D11_VIEWPORT CRenderer::m_tScreenViewport;
- //
- //Basic Initialization
- ID3D11ShaderResourceView* CRenderer::m_d3dDepthStencilSRV;
- UINT CRenderer::m_unWidth;
- UINT CRenderer::m_unHeight;
- HWND CRenderer::m_window;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement