DeviceResources.h 4.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. // The MIT License(MIT)
  2. //
  3. // Copyright(c) 2022 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
  4. //
  5. // Permission is hereby granted, free of charge, to any person obtaining a copy of
  6. // this software and associated documentation files(the "Software"), to deal in
  7. // the Software without restriction, including without limitation the rights to
  8. // use, copy, modify, merge, publish, distribute, sublicense, and / or sell copies of
  9. // the Software, and to permit persons to whom the Software is furnished to do so,
  10. // subject to the following conditions :
  11. //
  12. // The above copyright notice and this permission notice shall be included in all
  13. // copies or substantial portions of the Software.
  14. //
  15. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
  17. // FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.IN NO EVENT SHALL THE AUTHORS OR
  18. // COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
  19. // IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  20. // CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  21. #pragma once
  22. #include <dxgi1_4.h>
  23. #include <d3d11.h>
  24. #include <wrl.h>
  25. #include <iostream>
  26. #include <vector>
  27. using namespace Microsoft::WRL;
  28. struct Adapter
  29. {
  30. std::string Description;
  31. uint32_t VendorId;
  32. uint32_t DeviceId;
  33. uint32_t SubSysId;
  34. uint32_t Revision;
  35. size_t DedicatedVideoMemory;
  36. size_t DedicatedSystemMemory;
  37. size_t SharedSystemMemory;
  38. };
  39. class DeviceResources
  40. {
  41. public:
  42. void create(HWND hWnd, uint32_t adapterIdx = 0);
  43. void initRenderTarget();
  44. void resizeRenderTarget(uint32_t Width, uint32_t Height, DXGI_FORMAT format);
  45. void clearRenderTargetView(const float color[4]);
  46. void present(uint32_t SyncInterval, uint32_t Flags) { m_swapChain->Present(SyncInterval, Flags); }
  47. ID3D11Device* device() { return m_d3dDevice.Get(); }
  48. ID3D11DeviceContext* context() { return m_d3dContext.Get(); }
  49. bool isInitialized() { return m_initialized; }
  50. IDXGISwapChain* swapChain() { return m_swapChain.Get(); }
  51. ID3D11Texture2D* renderTarget() { return m_d3dRenderTarget.Get(); }
  52. void DeviceResources::setRenderTarget() {
  53. m_d3dContext->OMSetRenderTargets(1, m_d3dRenderTargetView.GetAddressOf(), NULL);
  54. }
  55. ID3D11RenderTargetView* targetView() { return m_d3dRenderTargetView.Get(); }
  56. ID3D11RenderTargetView* const* targetViewAddress() { return m_d3dRenderTargetView.GetAddressOf(); }
  57. ID3D11UnorderedAccessView* const* targetUAVAddress() { return m_d3dRenderTargetUAV.GetAddressOf(); }
  58. void createTexture2D(int w, int h, DXGI_FORMAT format, D3D11_USAGE heapType, const void* data, uint32_t rowPitch, uint32_t imageSize, ID3D11Texture2D** ppTexture2D);
  59. void createUAV(ID3D11Resource* pResource, DXGI_FORMAT format, ID3D11UnorderedAccessView** ppUAView);
  60. void createSRV(ID3D11Resource* pResource, DXGI_FORMAT format, ID3D11ShaderResourceView** ppSRView);
  61. void createLinearClampSampler(ID3D11SamplerState** ppSampleState);
  62. void createConstBuffer(void* initialData, uint32_t size, ID3D11Buffer** ppBuffer);
  63. void updateConstBuffer(void* data, uint32_t size, ID3D11Buffer* ppBuffer);
  64. void getTextureData(ID3D11Texture2D* texture, std::vector<uint8_t>& data, uint32_t& width, uint32_t& height, uint32_t& rowPitch);
  65. uint32_t width() { return m_width; }
  66. uint32_t height() { return m_height; }
  67. Adapter getAdapter() { return m_adapter; }
  68. private:
  69. ComPtr<ID3D11Device> m_d3dDevice;
  70. ComPtr<ID3D11DeviceContext> m_d3dContext;
  71. ComPtr<IDXGISwapChain> m_swapChain;
  72. ComPtr<ID3D11Texture2D> m_d3dRenderTarget;
  73. ComPtr<ID3D11RenderTargetView> m_d3dRenderTargetView;
  74. ComPtr<ID3D11UnorderedAccessView> m_d3dRenderTargetUAV;
  75. uint32_t m_width;
  76. uint32_t m_height;
  77. bool m_initialized = false;
  78. Adapter m_adapter;
  79. };