Ember

Initialize

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