directx.cpp 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. #include <windows.h>
  2. #include <d3d11.h>
  3. HINSTANCE g_hInst = NULL;
  4. D3D_DRIVER_TYPE g_driverType = D3D_DRIVER_TYPE_NULL;
  5. D3D_FEATURE_LEVEL g_featureLevel = D3D_FEATURE_LEVEL_11_0;
  6. ID3D11Device* g_pd3dDevice = NULL;
  7. ID3D11DeviceContext* g_pImmediateContext = NULL;
  8. IDXGISwapChain* g_pSwapChain = NULL;
  9. static HRESULT InitDevice()
  10. {
  11. HRESULT hr = S_OK;
  12. UINT width = 640;
  13. UINT height = 480;
  14. UINT createDeviceFlags = 0;
  15. D3D_DRIVER_TYPE driverTypes[] =
  16. {
  17. D3D_DRIVER_TYPE_HARDWARE,
  18. D3D_DRIVER_TYPE_WARP,
  19. D3D_DRIVER_TYPE_REFERENCE,
  20. };
  21. UINT numDriverTypes = ARRAYSIZE(driverTypes);
  22. D3D_FEATURE_LEVEL featureLevels[] =
  23. {
  24. D3D_FEATURE_LEVEL_11_0,
  25. D3D_FEATURE_LEVEL_10_1,
  26. D3D_FEATURE_LEVEL_10_0,
  27. };
  28. UINT numFeatureLevels = ARRAYSIZE(featureLevels);
  29. DXGI_SWAP_CHAIN_DESC sd;
  30. ZeroMemory( &sd, sizeof( sd ) );
  31. sd.BufferCount = 1;
  32. sd.BufferDesc.Width = width;
  33. sd.BufferDesc.Height = height;
  34. #ifdef CHECK_NV12
  35. sd.BufferDesc.Format = DXGI_FORMAT_NV12;
  36. #else
  37. sd.BufferDesc.Format = DXGI_FORMAT_R8G8B8A8_UNORM;
  38. #endif
  39. sd.BufferDesc.RefreshRate.Numerator = 60;
  40. sd.BufferDesc.RefreshRate.Denominator = 1;
  41. sd.BufferUsage = DXGI_USAGE_RENDER_TARGET_OUTPUT;
  42. sd.OutputWindow = NULL; //g_hWnd;
  43. sd.SampleDesc.Count = 1;
  44. sd.SampleDesc.Quality = 0;
  45. sd.Windowed = TRUE;
  46. for (UINT driverTypeIndex = 0; driverTypeIndex < numDriverTypes; driverTypeIndex++)
  47. {
  48. g_driverType = driverTypes[driverTypeIndex];
  49. hr = D3D11CreateDeviceAndSwapChain(NULL, g_driverType, NULL, createDeviceFlags, featureLevels, numFeatureLevels,
  50. D3D11_SDK_VERSION, &sd, &g_pSwapChain, &g_pd3dDevice, &g_featureLevel, &g_pImmediateContext);
  51. if (SUCCEEDED(hr))
  52. break;
  53. }
  54. if (FAILED(hr))
  55. return hr;
  56. return S_OK;
  57. }
  58. int main(int /*argc*/, char** /*argv*/)
  59. {
  60. InitDevice();
  61. return 0;
  62. }