Ember

CreateDevice

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