DX12Textur.cpp 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. #include "DX12Textur.h"
  2. #include "Bild.h"
  3. #include "d3dx12.h"
  4. #include "DX12CommandQueue.h"
  5. using namespace Framework;
  6. DX12Textur::DX12Textur(ID3D12Device* device,
  7. DX12CopyCommandQueue* copy,
  8. DX12DirectCommandQueue* direct)
  9. : Textur(),
  10. buffer(0),
  11. intermediate(0),
  12. device(device),
  13. copy(copy),
  14. direct(direct),
  15. shaderResource(0)
  16. {}
  17. DX12Textur::~DX12Textur()
  18. {
  19. #ifdef WIN32
  20. if (buffer) buffer->Release();
  21. if (intermediate) intermediate->Release();
  22. #endif
  23. }
  24. // Aktualisiert die Textur. Die Pixel des aktuellen Bildes werden in den
  25. // Graphikspeicher kopiert
  26. bool DX12Textur::updateTextur()
  27. {
  28. if (!bild) 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,
  50. D3D12_HEAP_FLAG_NONE,
  51. &description,
  52. D3D12_RESOURCE_STATE_COPY_DEST,
  53. 0,
  54. __uuidof(ID3D12Resource),
  55. (void**)&buffer);
  56. const UINT64 uploadBufferSize
  57. = GetRequiredIntermediateSize(buffer, 0, 1);
  58. D3D12_RESOURCE_DESC iDescription;
  59. iDescription.Dimension = D3D12_RESOURCE_DIMENSION_BUFFER;
  60. iDescription.Alignment = 0;
  61. iDescription.Width = uploadBufferSize;
  62. iDescription.Height = 1;
  63. iDescription.DepthOrArraySize = 1;
  64. iDescription.MipLevels = 1;
  65. iDescription.Format = DXGI_FORMAT_UNKNOWN;
  66. iDescription.SampleDesc.Count = 1;
  67. iDescription.SampleDesc.Quality = 0;
  68. iDescription.Layout = D3D12_TEXTURE_LAYOUT_ROW_MAJOR;
  69. iDescription.Flags = D3D12_RESOURCE_FLAG_NONE;
  70. hprop.Type = D3D12_HEAP_TYPE_UPLOAD;
  71. device->CreateCommittedResource(&hprop,
  72. D3D12_HEAP_FLAG_NONE,
  73. &iDescription,
  74. D3D12_RESOURCE_STATE_GENERIC_READ,
  75. 0,
  76. __uuidof(ID3D12Resource),
  77. (void**)&intermediate);
  78. shaderResource = 0;
  79. }
  80. if (bild && (changed || bild->getRend()))
  81. {
  82. changed = 0;
  83. if (shaderResource)
  84. {
  85. D3D12_RESOURCE_BARRIER barrier;
  86. barrier.Type = D3D12_RESOURCE_BARRIER_TYPE_TRANSITION;
  87. barrier.Transition.pResource = buffer;
  88. barrier.Transition.StateBefore
  89. = D3D12_RESOURCE_STATE_PIXEL_SHADER_RESOURCE
  90. | D3D12_RESOURCE_STATE_NON_PIXEL_SHADER_RESOURCE;
  91. barrier.Transition.StateAfter = D3D12_RESOURCE_STATE_COPY_DEST;
  92. barrier.Transition.Subresource = 0;
  93. barrier.Flags = D3D12_RESOURCE_BARRIER_FLAG_NONE;
  94. direct->getCommandList()->ResourceBarrier(1, &barrier);
  95. shaderResource = 0;
  96. }
  97. D3D12_SUBRESOURCE_DATA textureData = {};
  98. textureData.pData = bild->getBuffer();
  99. textureData.RowPitch = bild->getBreite() * sizeof(int);
  100. textureData.SlicePitch = textureData.RowPitch * bild->getHeight();
  101. UpdateSubresources(direct->getCommandList(),
  102. buffer,
  103. intermediate,
  104. 0,
  105. 0,
  106. 1,
  107. &textureData);
  108. D3D12_RESOURCE_BARRIER barrier;
  109. barrier.Type = D3D12_RESOURCE_BARRIER_TYPE_TRANSITION;
  110. barrier.Transition.pResource = buffer;
  111. barrier.Transition.StateBefore = D3D12_RESOURCE_STATE_COPY_DEST;
  112. barrier.Transition.StateAfter
  113. = D3D12_RESOURCE_STATE_PIXEL_SHADER_RESOURCE
  114. | D3D12_RESOURCE_STATE_NON_PIXEL_SHADER_RESOURCE;
  115. barrier.Transition.Subresource = 0;
  116. barrier.Flags = D3D12_RESOURCE_BARRIER_FLAG_NONE;
  117. direct->getCommandList()->ResourceBarrier(1, &barrier);
  118. shaderResource = 1;
  119. }
  120. #endif
  121. return 1;
  122. }
  123. // Gibt true zurük, wenn updateTextur aufgerufen werden muss
  124. bool DX12Textur::brauchtUpdate() const
  125. {
  126. return bild && !buffer;
  127. }
  128. // Gibt die DX12 Resource zurück
  129. ID3D12Resource* DX12Textur::getResource()
  130. {
  131. return buffer;
  132. }