directx.cpp 2.0 KB

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