DX12Textur.cpp 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. #include "DX12Textur.h"
  2. #include "d3dx12.h"
  3. #include "Bild.h"
  4. #include "DX12CommandQueue.h"
  5. using namespace Framework;
  6. DX12Textur::DX12Textur( ID3D12Device* device, DX12CopyCommandQueue* copy, DX12DirectCommandQueue* direct )
  7. : Textur(),
  8. buffer( 0 ),
  9. intermediate( 0 ),
  10. device( device ),
  11. copy( copy ),
  12. direct( direct ),
  13. shaderResource( 0 )
  14. {}
  15. DX12Textur::~DX12Textur()
  16. {
  17. #ifdef WIN32
  18. if( buffer )
  19. buffer->Release();
  20. if( intermediate )
  21. intermediate->Release();
  22. #endif
  23. }
  24. // Aktualisiert die Textur. Die Pixel des aktuellen Bildes werden in den Graphikspeicher kopiert
  25. bool DX12Textur::updateTextur()
  26. {
  27. if( !bild )
  28. return 0;
  29. #ifdef WIN32
  30. if( !buffer )
  31. {
  32. D3D12_RESOURCE_DESC description;
  33. ZeroMemory( &description, sizeof( D3D12_RESOURCE_DESC ) );
  34. description.Dimension = D3D12_RESOURCE_DIMENSION_TEXTURE2D;
  35. description.Height = bild->getHeight();
  36. description.Width = bild->getBreite();
  37. description.DepthOrArraySize = 1;
  38. description.MipLevels = 1;
  39. description.Format = DXGI_FORMAT_B8G8R8A8_UNORM;
  40. description.SampleDesc.Count = 1;
  41. description.Layout = D3D12_TEXTURE_LAYOUT_UNKNOWN;
  42. description.Flags = D3D12_RESOURCE_FLAG_NONE;
  43. D3D12_HEAP_PROPERTIES hprop;
  44. hprop.Type = D3D12_HEAP_TYPE_DEFAULT;
  45. hprop.CPUPageProperty = D3D12_CPU_PAGE_PROPERTY_UNKNOWN;
  46. hprop.MemoryPoolPreference = D3D12_MEMORY_POOL_UNKNOWN;
  47. hprop.CreationNodeMask = 1;
  48. hprop.VisibleNodeMask = 1;
  49. device->CreateCommittedResource( &hprop, D3D12_HEAP_FLAG_NONE, &description, D3D12_RESOURCE_STATE_COPY_DEST, 0, __uuidof(ID3D12Resource), (void**)&buffer );
  50. const UINT64 uploadBufferSize = GetRequiredIntermediateSize( buffer, 0, 1 );
  51. D3D12_RESOURCE_DESC iDescription;
  52. iDescription.Dimension = D3D12_RESOURCE_DIMENSION_BUFFER;
  53. iDescription.Alignment = 0;
  54. iDescription.Width = uploadBufferSize;
  55. iDescription.Height = 1;
  56. iDescription.DepthOrArraySize = 1;
  57. iDescription.MipLevels = 1;
  58. iDescription.Format = DXGI_FORMAT_UNKNOWN;
  59. iDescription.SampleDesc.Count = 1;
  60. iDescription.SampleDesc.Quality = 0;
  61. iDescription.Layout = D3D12_TEXTURE_LAYOUT_ROW_MAJOR;
  62. iDescription.Flags = D3D12_RESOURCE_FLAG_NONE;
  63. hprop.Type = D3D12_HEAP_TYPE_UPLOAD;
  64. device->CreateCommittedResource( &hprop, D3D12_HEAP_FLAG_NONE, &iDescription, D3D12_RESOURCE_STATE_GENERIC_READ, 0, __uuidof(ID3D12Resource), (void**)&intermediate );
  65. shaderResource = 0;
  66. }
  67. if( bild )
  68. {
  69. if( shaderResource )
  70. {
  71. D3D12_RESOURCE_BARRIER barrier;
  72. barrier.Type = D3D12_RESOURCE_BARRIER_TYPE_TRANSITION;
  73. barrier.Transition.pResource = buffer;
  74. barrier.Transition.StateBefore = D3D12_RESOURCE_STATE_PIXEL_SHADER_RESOURCE | D3D12_RESOURCE_STATE_NON_PIXEL_SHADER_RESOURCE;
  75. barrier.Transition.StateAfter = D3D12_RESOURCE_STATE_COPY_DEST;
  76. barrier.Transition.Subresource = 0;
  77. barrier.Flags = D3D12_RESOURCE_BARRIER_FLAG_NONE;
  78. direct->getCommandList()->ResourceBarrier( 1, &barrier );
  79. shaderResource = 0;
  80. }
  81. D3D12_SUBRESOURCE_DATA textureData = {};
  82. textureData.pData = bild->getBuffer();
  83. textureData.RowPitch = bild->getBreite() * sizeof( int );
  84. textureData.SlicePitch = textureData.RowPitch * bild->getHeight();
  85. UpdateSubresources( direct->getCommandList(), buffer, intermediate, 0, 0, 1, &textureData );
  86. D3D12_RESOURCE_BARRIER barrier;
  87. barrier.Type = D3D12_RESOURCE_BARRIER_TYPE_TRANSITION;
  88. barrier.Transition.pResource = buffer;
  89. barrier.Transition.StateBefore = D3D12_RESOURCE_STATE_COPY_DEST;
  90. barrier.Transition.StateAfter = D3D12_RESOURCE_STATE_PIXEL_SHADER_RESOURCE | D3D12_RESOURCE_STATE_NON_PIXEL_SHADER_RESOURCE;
  91. barrier.Transition.Subresource = 0;
  92. barrier.Flags = D3D12_RESOURCE_BARRIER_FLAG_NONE;
  93. direct->getCommandList()->ResourceBarrier( 1, &barrier );
  94. shaderResource = 1;
  95. }
  96. #endif
  97. return 1;
  98. }
  99. // Gibt true zurük, wenn updateTextur aufgerufen werden muss
  100. bool DX12Textur::brauchtUpdate() const
  101. {
  102. return bild && !buffer;
  103. }
  104. // Gibt die DX12 Resource zurück
  105. ID3D12Resource* DX12Textur::getResource()
  106. {
  107. return buffer;
  108. }