Direct3DBase.cpp 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. #include "pch.h"
  2. #include "Direct3DBase.h"
  3. using namespace DirectX;
  4. using namespace Microsoft::WRL;
  5. using namespace Windows::UI::Core;
  6. using namespace Windows::Foundation;
  7. using namespace Windows::Graphics::Display;
  8. // Constructor.
  9. Direct3DBase::Direct3DBase()
  10. {
  11. }
  12. // Initialize the Direct3D resources required to run.
  13. void Direct3DBase::Initialize()
  14. {
  15. CreateDeviceResources();
  16. }
  17. // These are the resources that depend on the device.
  18. void Direct3DBase::CreateDeviceResources()
  19. {
  20. // This flag adds support for surfaces with a different color channel ordering
  21. // than the API default. It is required for compatibility with Direct2D.
  22. UINT creationFlags = D3D11_CREATE_DEVICE_BGRA_SUPPORT;
  23. #if defined(_DEBUG)
  24. // If the project is in a debug build, enable debugging via SDK Layers with this flag.
  25. creationFlags |= D3D11_CREATE_DEVICE_DEBUG;
  26. #endif
  27. // This array defines the set of DirectX hardware feature levels this app will support.
  28. // Note the ordering should be preserved.
  29. // Don't forget to declare your application's minimum required feature level in its
  30. // description. All applications are assumed to support 9.1 unless otherwise stated.
  31. D3D_FEATURE_LEVEL featureLevels[] =
  32. {
  33. D3D_FEATURE_LEVEL_11_1,
  34. D3D_FEATURE_LEVEL_11_0,
  35. D3D_FEATURE_LEVEL_10_1,
  36. D3D_FEATURE_LEVEL_10_0,
  37. D3D_FEATURE_LEVEL_9_3
  38. };
  39. // Create the Direct3D 11 API device object and a corresponding context.
  40. ComPtr<ID3D11Device> device;
  41. ComPtr<ID3D11DeviceContext> context;
  42. DX::ThrowIfFailed(
  43. D3D11CreateDevice(
  44. nullptr, // Specify nullptr to use the default adapter.
  45. D3D_DRIVER_TYPE_HARDWARE,
  46. nullptr,
  47. creationFlags, // Set set debug and Direct2D compatibility flags.
  48. featureLevels, // List of feature levels this app can support.
  49. ARRAYSIZE(featureLevels),
  50. D3D11_SDK_VERSION, // Always set this to D3D11_SDK_VERSION.
  51. &device, // Returns the Direct3D device created.
  52. &m_featureLevel, // Returns feature level of device created.
  53. &context // Returns the device immediate context.
  54. )
  55. );
  56. // Get the Direct3D 11.1 API device and context interfaces.
  57. DX::ThrowIfFailed(
  58. device.As(&m_d3dDevice)
  59. );
  60. DX::ThrowIfFailed(
  61. context.As(&m_d3dContext)
  62. );
  63. }
  64. // Allocate all memory resources that depend on the window size.
  65. void Direct3DBase::CreateWindowSizeDependentResources()
  66. {
  67. // Create a descriptor for the render target buffer.
  68. CD3D11_TEXTURE2D_DESC renderTargetDesc(
  69. DXGI_FORMAT_B8G8R8A8_UNORM,
  70. static_cast<UINT>(m_renderTargetSize.Width),
  71. static_cast<UINT>(m_renderTargetSize.Height),
  72. 1,
  73. 1,
  74. D3D11_BIND_RENDER_TARGET | D3D11_BIND_SHADER_RESOURCE
  75. );
  76. renderTargetDesc.MiscFlags = D3D11_RESOURCE_MISC_SHARED_KEYEDMUTEX | D3D11_RESOURCE_MISC_SHARED_NTHANDLE;
  77. // Allocate a 2-D surface as the render target buffer.
  78. DX::ThrowIfFailed(
  79. m_d3dDevice->CreateTexture2D(
  80. &renderTargetDesc,
  81. nullptr,
  82. &m_renderTarget
  83. )
  84. );
  85. DX::ThrowIfFailed(
  86. m_d3dDevice->CreateRenderTargetView(
  87. m_renderTarget.Get(),
  88. nullptr,
  89. &m_renderTargetView
  90. )
  91. );
  92. // Create a depth stencil view.
  93. CD3D11_TEXTURE2D_DESC depthStencilDesc(
  94. DXGI_FORMAT_D24_UNORM_S8_UINT,
  95. static_cast<UINT>(m_renderTargetSize.Width),
  96. static_cast<UINT>(m_renderTargetSize.Height),
  97. 1,
  98. 1,
  99. D3D11_BIND_DEPTH_STENCIL
  100. );
  101. ComPtr<ID3D11Texture2D> depthStencil;
  102. DX::ThrowIfFailed(
  103. m_d3dDevice->CreateTexture2D(
  104. &depthStencilDesc,
  105. nullptr,
  106. &depthStencil
  107. )
  108. );
  109. CD3D11_DEPTH_STENCIL_VIEW_DESC depthStencilViewDesc(D3D11_DSV_DIMENSION_TEXTURE2D);
  110. DX::ThrowIfFailed(
  111. m_d3dDevice->CreateDepthStencilView(
  112. depthStencil.Get(),
  113. &depthStencilViewDesc,
  114. &m_depthStencilView
  115. )
  116. );
  117. // Set the rendering viewport to target the entire window.
  118. CD3D11_VIEWPORT viewport(
  119. 0.0f,
  120. 0.0f,
  121. m_renderTargetSize.Width,
  122. m_renderTargetSize.Height
  123. );
  124. m_d3dContext->RSSetViewports(1, &viewport);
  125. }
  126. void Direct3DBase::UpdateForRenderResolutionChange(float width, float height)
  127. {
  128. m_renderTargetSize.Width = width;
  129. m_renderTargetSize.Height = height;
  130. ID3D11RenderTargetView* nullViews[] = {nullptr};
  131. m_d3dContext->OMSetRenderTargets(ARRAYSIZE(nullViews), nullViews, nullptr);
  132. m_renderTarget = nullptr;
  133. m_renderTargetView = nullptr;
  134. m_depthStencilView = nullptr;
  135. m_d3dContext->Flush();
  136. CreateWindowSizeDependentResources();
  137. }
  138. void Direct3DBase::UpdateForWindowSizeChange(float width, float height)
  139. {
  140. m_windowBounds.Width = width;
  141. m_windowBounds.Height = height;
  142. }