Ember

Initialize()

Oct 20th, 2013
100
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 10.81 KB | None | 0 0
  1. // DirectX device initialization and creation -----------------------------------------
  2. bool D3D::Initialize(int screenWidth, int screenHeight, bool vsync, HWND hwnd, bool fullscreen, float screenDepth, float screenNear)
  3. {
  4.     HRESULT result;
  5.     IDXGIFactory* factory;
  6.     IDXGIAdapter* adapter;
  7.     IDXGIOutput* adapterOutput;
  8.     unsigned int numModes, i, numerator, denominator, stringLength;
  9.     DXGI_MODE_DESC* displayModeList;
  10.     DXGI_ADAPTER_DESC adapterDesc;
  11.     int error;
  12.     DXGI_SWAP_CHAIN_DESC swapChainDesc;
  13.     D3D_FEATURE_LEVEL featureLevel;
  14.     ID3D11Texture2D* backBufferPtr;
  15.     D3D11_TEXTURE2D_DESC depthBufferDesc;
  16.     D3D11_DEPTH_STENCIL_DESC depthStencilDesc;
  17.     D3D11_DEPTH_STENCIL_VIEW_DESC depthStencilViewDesc;
  18.     D3D11_RASTERIZER_DESC rasterDesc;
  19.     D3D11_VIEWPORT viewport;
  20.     float fieldOfView, screenAspect;
  21.  
  22.     // Store the vsync and fullscreen settings
  23.     mVsyncEnabled = vsync;
  24.     mFullscreen = fullscreen;
  25.  
  26.     // Create a DirectX graphics interface factory
  27.     result = CreateDXGIFactory(__uuidof(IDXGIFactory), (void**)&factory);
  28.     if (FAILED(result)) { return false; }
  29.  
  30.     // Use the factory to create an adapter for the primary graphics interface (video card)
  31.     result = factory->EnumAdapters(0, &adapter);
  32.     if (FAILED(result)) { return false; }
  33.  
  34.     // Enumerate the primary adapter output (monitor)
  35.     result = adapter->EnumOutputs(0, &adapterOutput);
  36.     if (FAILED(result)) { return false; }
  37.  
  38.     // Get the number of modes that fit the DXGI_FORMAT_R8G8B8A8_UNORM display format for the adapter output (monitor)
  39.     result = adapterOutput->GetDisplayModeList(DXGI_FORMAT_R8G8B8A8_UNORM, DXGI_ENUM_MODES_INTERLACED, &numModes, NULL);
  40.     if (FAILED(result)) { return false; }
  41.  
  42.     // Create a list to hold all the possible display modes for this monitor/video card combination
  43.     displayModeList = new DXGI_MODE_DESC[numModes];
  44.     if (!displayModeList) { return false; }
  45.  
  46.     // Now fill the display mode list structures
  47.     result = adapterOutput->GetDisplayModeList(DXGI_FORMAT_R8G8B8A8_UNORM, DXGI_ENUM_MODES_INTERLACED, &numModes, displayModeList);
  48.     if (FAILED(result)) { return false; }
  49.  
  50.     // Now go through all the display modes and find the one that matches the screen width and height
  51.     // When a match is found store the numerator and denominator of the refresh rate for that monitor
  52.     for (i = 0; i<numModes; i++)
  53.     {
  54.         if (displayModeList[i].Width == (unsigned int)screenWidth)
  55.         {
  56.             if (displayModeList[i].Height == (unsigned int)screenHeight)
  57.             {
  58.                 numerator = displayModeList[i].RefreshRate.Numerator;
  59.                 denominator = displayModeList[i].RefreshRate.Denominator;
  60.             }
  61.         }
  62.     }
  63.  
  64.     // Get the adapter (video card) description
  65.     result = adapter->GetDesc(&adapterDesc);
  66.     if (FAILED(result)) { return false; }
  67.  
  68.     // Store the dedicated video card memory in megabytes
  69.     mVideoCardMemory = (int)(adapterDesc.DedicatedVideoMemory / 1024 / 1024);
  70.  
  71.     // Convert the name of the video card to a character array and store it
  72.     error = wcstombs_s(&stringLength, mVideoCardDescription, 128, adapterDesc.Description, 128);
  73.     if (error != 0) { return false; }
  74.  
  75.     // Release the display mode list
  76.     delete[] displayModeList;
  77.     displayModeList = 0;
  78.  
  79.     // Release the adapter output
  80.     adapterOutput->Release();
  81.     adapterOutput = 0;
  82.  
  83.     // Release the adapter
  84.     adapter->Release();
  85.     adapter = 0;
  86.  
  87.     // Release the factory
  88.     factory->Release();
  89.     factory = 0;
  90.  
  91.     // Initialize the swap chain description
  92.     ZeroMemory(&swapChainDesc, sizeof(swapChainDesc));
  93.  
  94.     // Set to a single back buffer
  95.     swapChainDesc.BufferCount = 1;
  96.  
  97.     // Set the width and height of the back buffer
  98.     swapChainDesc.BufferDesc.Width = screenWidth;
  99.     swapChainDesc.BufferDesc.Height = screenHeight;
  100.  
  101.     // Set regular 32-bit surface for the back buffer
  102.     swapChainDesc.BufferDesc.Format = DXGI_FORMAT_R8G8B8A8_UNORM;
  103.  
  104.     // Set the refresh rate of the back buffer
  105.     if (mVsyncEnabled)
  106.     {
  107.         swapChainDesc.BufferDesc.RefreshRate.Numerator = numerator;
  108.         swapChainDesc.BufferDesc.RefreshRate.Denominator = denominator;
  109.     }
  110.     else
  111.     {
  112.         swapChainDesc.BufferDesc.RefreshRate.Numerator = 0;
  113.         swapChainDesc.BufferDesc.RefreshRate.Denominator = 1;
  114.     }
  115.  
  116.     // Set the usage of the back buffer
  117.     swapChainDesc.BufferUsage = DXGI_USAGE_RENDER_TARGET_OUTPUT;
  118.  
  119.     // Set the handle for the window to render to
  120.     swapChainDesc.OutputWindow = hwnd;
  121.  
  122.     // Turn multisampling off
  123.     swapChainDesc.SampleDesc.Count = 1;
  124.     swapChainDesc.SampleDesc.Quality = 0;
  125.  
  126.     // Set to full screen or windowed mode
  127.     if (mFullscreen)
  128.     {
  129.         swapChainDesc.Windowed = false;
  130.     }
  131.     else
  132.     {
  133.         swapChainDesc.Windowed = true;
  134.     }
  135.  
  136.     // Set the scan line ordering and scaling to unspecified
  137.     swapChainDesc.BufferDesc.ScanlineOrdering = DXGI_MODE_SCANLINE_ORDER_UNSPECIFIED;
  138.     swapChainDesc.BufferDesc.Scaling = DXGI_MODE_SCALING_UNSPECIFIED;
  139.  
  140.     // Discard the back buffer contents after presenting
  141.     swapChainDesc.SwapEffect = DXGI_SWAP_EFFECT_DISCARD;
  142.  
  143.     // Don't set the advanced flags
  144.     swapChainDesc.Flags = 0;
  145.  
  146.     // Set the feature level to DirectX 11
  147.     featureLevel = D3D_FEATURE_LEVEL_11_0;
  148.  
  149.     // Create the swap chain, Direct3D device, and Direct3D device context
  150.     result = D3D11CreateDeviceAndSwapChain(NULL, D3D_DRIVER_TYPE_HARDWARE, NULL, 0, &featureLevel, 1, D3D11_SDK_VERSION, &swapChainDesc, &mSwapChain, &mDevice, NULL, &mDeviceContext);
  151.     if (FAILED(result)) { return false; }
  152.  
  153.     // Get the pointer to the back buffer.
  154.     result = mSwapChain->GetBuffer(0, __uuidof(ID3D11Texture2D), (LPVOID*)&backBufferPtr);
  155.     if (FAILED(result)) { return false; }
  156.  
  157.     // Create the render target view with the back buffer pointer
  158.     result = mDevice->CreateRenderTargetView(backBufferPtr, NULL, &mRenderTargetView);
  159.     if (FAILED(result)) { return false; }
  160.  
  161.     // Release pointer to the back buffer as we no longer need it
  162.     backBufferPtr->Release();
  163.     backBufferPtr = 0;
  164.  
  165.     // Initialize the description of the depth buffer
  166.     ZeroMemory(&depthBufferDesc, sizeof(depthBufferDesc));
  167.  
  168.     // Set up the description of the depth buffer
  169.     depthBufferDesc.Width = screenWidth;
  170.     depthBufferDesc.Height = screenHeight;
  171.     depthBufferDesc.MipLevels = 1;
  172.     depthBufferDesc.ArraySize = 1;
  173.     depthBufferDesc.Format = DXGI_FORMAT_D24_UNORM_S8_UINT;
  174.     depthBufferDesc.SampleDesc.Count = 1;
  175.     depthBufferDesc.SampleDesc.Quality = 0;
  176.     depthBufferDesc.Usage = D3D11_USAGE_DEFAULT;
  177.     depthBufferDesc.BindFlags = D3D11_BIND_DEPTH_STENCIL;
  178.     depthBufferDesc.CPUAccessFlags = 0;
  179.     depthBufferDesc.MiscFlags = 0;
  180.  
  181.     // Create the texture for the depth buffer using the filled out description
  182.     result = mDevice->CreateTexture2D(&depthBufferDesc, NULL, &mDepthStencilBuffer);
  183.     if (FAILED(result)) { return false; }
  184.  
  185.     // Initialize the description of the stencil state
  186.     ZeroMemory(&depthStencilDesc, sizeof(depthStencilDesc));
  187.  
  188.     // Set up the description of the stencil state
  189.     depthStencilDesc.DepthEnable = true;
  190.     depthStencilDesc.DepthWriteMask = D3D11_DEPTH_WRITE_MASK_ALL;
  191.     depthStencilDesc.DepthFunc = D3D11_COMPARISON_LESS;
  192.     depthStencilDesc.StencilEnable = true;
  193.     depthStencilDesc.StencilReadMask = 0xFF;
  194.     depthStencilDesc.StencilWriteMask = 0xFF;
  195.  
  196.     // Stencil operations if pixel is front-facing
  197.     depthStencilDesc.FrontFace.StencilFailOp = D3D11_STENCIL_OP_KEEP;
  198.     depthStencilDesc.FrontFace.StencilDepthFailOp = D3D11_STENCIL_OP_INCR;
  199.     depthStencilDesc.FrontFace.StencilPassOp = D3D11_STENCIL_OP_KEEP;
  200.     depthStencilDesc.FrontFace.StencilFunc = D3D11_COMPARISON_ALWAYS;
  201.  
  202.     // Stencil operations if pixel is back-facing
  203.     depthStencilDesc.BackFace.StencilFailOp = D3D11_STENCIL_OP_KEEP;
  204.     depthStencilDesc.BackFace.StencilDepthFailOp = D3D11_STENCIL_OP_DECR;
  205.     depthStencilDesc.BackFace.StencilPassOp = D3D11_STENCIL_OP_KEEP;
  206.     depthStencilDesc.BackFace.StencilFunc = D3D11_COMPARISON_ALWAYS;
  207.  
  208.     // Create the depth stencil state
  209.     result = mDevice->CreateDepthStencilState(&depthStencilDesc, &mDepthStencilState);
  210.     if (FAILED(result)) { return false; }
  211.  
  212.     // Set the depth stencil state
  213.     mDeviceContext->OMSetDepthStencilState(mDepthStencilState, 1);
  214.  
  215.     // Initailze the depth stencil view
  216.     ZeroMemory(&depthStencilViewDesc, sizeof(depthStencilViewDesc));
  217.  
  218.     // Set up the depth stencil view description
  219.     depthStencilViewDesc.Format = DXGI_FORMAT_D24_UNORM_S8_UINT;
  220.     depthStencilViewDesc.ViewDimension = D3D11_DSV_DIMENSION_TEXTURE2D;
  221.     depthStencilViewDesc.Texture2D.MipSlice = 0;
  222.  
  223.     // Create the depth stencil view
  224.     result = mDevice->CreateDepthStencilView(mDepthStencilBuffer, &depthStencilViewDesc, &mDepthStencilView);
  225.     if (FAILED(result)) { return false; }
  226.  
  227.     // Bind the render target view and depth stencil buffer to the output render pipeline
  228.     mDeviceContext->OMSetRenderTargets(1, &mRenderTargetView, mDepthStencilView);
  229.  
  230.     // Setup the raster description which will determine how and what polygons will be drawn
  231.     rasterDesc.AntialiasedLineEnable = false;
  232.     rasterDesc.CullMode = D3D11_CULL_BACK;
  233.     rasterDesc.DepthBias = 0;
  234.     rasterDesc.DepthBiasClamp = 0.0f;
  235.     rasterDesc.DepthClipEnable = true;
  236.     rasterDesc.FillMode = D3D11_FILL_SOLID;
  237.     rasterDesc.FrontCounterClockwise = false;
  238.     rasterDesc.MultisampleEnable = false;
  239.     rasterDesc.ScissorEnable = false;
  240.     rasterDesc.SlopeScaledDepthBias = 0.0f;
  241.  
  242.     // Create the rasterizer state from the description we just filled out
  243.     result = mDevice->CreateRasterizerState(&rasterDesc, &mRasterState);
  244.     if (FAILED(result)) { return false; }
  245.  
  246.     // Now set the rasterizer state
  247.     mDeviceContext->RSSetState(mRasterState);
  248.  
  249.     // Setup the viewport for rendering.
  250.     viewport.Width = (float)screenWidth;
  251.     viewport.Height = (float)screenHeight;
  252.     viewport.MinDepth = 0.0f;
  253.     viewport.MaxDepth = 1.0f;
  254.     viewport.TopLeftX = 0.0f;
  255.     viewport.TopLeftY = 0.0f;
  256.  
  257.     // Create the viewport
  258.     mDeviceContext->RSSetViewports(1, &viewport);
  259.  
  260.     // Setup the projection matrix
  261.     fieldOfView = (float)D3DX_PI / 4.0f;
  262.     screenAspect = (float)screenWidth / (float)screenHeight;
  263.  
  264.     // Create the projection matrix for 3D rendering
  265.     D3DXMatrixPerspectiveFovLH(&mProjectionMatrix, fieldOfView, screenAspect, screenNear, screenDepth);
  266.  
  267.     // Initialize the world matrix to the identity matrix
  268.     D3DXMatrixIdentity(&mWorldMatrix);
  269.  
  270.     // Create an orthographic projection matrix for 2D rendering
  271.     D3DXMatrixOrthoLH(&mOrthoMatrix, (float)screenWidth, (float)screenHeight, screenNear, screenDepth);
  272.  
  273.     // SUCCESS
  274.     return true;
  275. }
Add Comment
Please, Sign In to add comment