D3D12Texture2D.cpp 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. #include "pch.h"
  2. #include "D3D12Constants.h"
  3. #include "D3D12Texture2D.h"
  4. // nonstandard extension used : class rvalue used as lvalue
  5. #pragma clang diagnostic ignored "-Wlanguage-extension-token"
  6. namespace unity
  7. {
  8. namespace webrtc
  9. {
  10. //---------------------------------------------------------------------------------------------------------------------
  11. D3D12Texture2D::D3D12Texture2D(
  12. uint32_t w, uint32_t h, ID3D12Resource* nativeTex, HANDLE handle, ID3D11Texture2D* sharedTex)
  13. : ITexture2D(w, h)
  14. , m_nativeTexture(nativeTex)
  15. , m_sharedHandle(handle)
  16. , m_sharedTexture(sharedTex)
  17. , m_readbackResource(nullptr)
  18. {
  19. }
  20. //----------------------------------------------------------------------------------------------------------------------
  21. HRESULT D3D12Texture2D::CreateReadbackResource(ID3D12Device* device)
  22. {
  23. SAFE_RELEASE(m_readbackResource)
  24. D3D12_RESOURCE_DESC origDesc = m_nativeTexture->GetDesc();
  25. device->GetCopyableFootprints(
  26. &origDesc,
  27. 0,
  28. 1,
  29. 0,
  30. &m_nativeTextureFootprint.Footprint,
  31. &m_nativeTextureFootprint.NumRows,
  32. &m_nativeTextureFootprint.RowSize,
  33. &m_nativeTextureFootprint.ResourceSize);
  34. // Create the readback buffer for the texture.
  35. D3D12_RESOURCE_DESC desc {};
  36. desc.Dimension = D3D12_RESOURCE_DIMENSION_BUFFER;
  37. desc.Alignment = 0;
  38. desc.Width = m_nativeTextureFootprint.ResourceSize;
  39. desc.Height = 1;
  40. desc.DepthOrArraySize = 1;
  41. desc.MipLevels = 1;
  42. desc.Format = DXGI_FORMAT_UNKNOWN;
  43. desc.SampleDesc.Count = 1;
  44. desc.SampleDesc.Quality = 0;
  45. desc.Layout = D3D12_TEXTURE_LAYOUT_ROW_MAJOR;
  46. desc.Flags = D3D12_RESOURCE_FLAG_NONE;
  47. const HRESULT hr = device->CreateCommittedResource(
  48. &D3D12_READBACK_HEAP_PROPS,
  49. D3D12_HEAP_FLAG_NONE,
  50. &desc,
  51. D3D12_RESOURCE_STATE_COPY_DEST,
  52. nullptr,
  53. IID_PPV_ARGS(&m_readbackResource));
  54. return hr;
  55. }
  56. } // end namespace webrtc
  57. } // end namespace unity