DX12Textur.cpp 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  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)
  81. {
  82. if (shaderResource)
  83. {
  84. D3D12_RESOURCE_BARRIER barrier;
  85. barrier.Type = D3D12_RESOURCE_BARRIER_TYPE_TRANSITION;
  86. barrier.Transition.pResource = buffer;
  87. barrier.Transition.StateBefore
  88. = D3D12_RESOURCE_STATE_PIXEL_SHADER_RESOURCE
  89. | D3D12_RESOURCE_STATE_NON_PIXEL_SHADER_RESOURCE;
  90. barrier.Transition.StateAfter = D3D12_RESOURCE_STATE_COPY_DEST;
  91. barrier.Transition.Subresource = 0;
  92. barrier.Flags = D3D12_RESOURCE_BARRIER_FLAG_NONE;
  93. direct->getCommandList()->ResourceBarrier(1, &barrier);
  94. shaderResource = 0;
  95. }
  96. D3D12_SUBRESOURCE_DATA textureData = {};
  97. textureData.pData = bild->getBuffer();
  98. textureData.RowPitch = bild->getBreite() * sizeof(int);
  99. textureData.SlicePitch = textureData.RowPitch * bild->getHeight();
  100. UpdateSubresources(direct->getCommandList(),
  101. buffer,
  102. intermediate,
  103. 0,
  104. 0,
  105. 1,
  106. &textureData);
  107. D3D12_RESOURCE_BARRIER barrier;
  108. barrier.Type = D3D12_RESOURCE_BARRIER_TYPE_TRANSITION;
  109. barrier.Transition.pResource = buffer;
  110. barrier.Transition.StateBefore = D3D12_RESOURCE_STATE_COPY_DEST;
  111. barrier.Transition.StateAfter
  112. = D3D12_RESOURCE_STATE_PIXEL_SHADER_RESOURCE
  113. | D3D12_RESOURCE_STATE_NON_PIXEL_SHADER_RESOURCE;
  114. barrier.Transition.Subresource = 0;
  115. barrier.Flags = D3D12_RESOURCE_BARRIER_FLAG_NONE;
  116. direct->getCommandList()->ResourceBarrier(1, &barrier);
  117. shaderResource = 1;
  118. }
  119. #endif
  120. return 1;
  121. }
  122. // Gibt true zurük, wenn updateTextur aufgerufen werden muss
  123. bool DX12Textur::brauchtUpdate() const
  124. {
  125. return bild && !buffer;
  126. }
  127. // Gibt die DX12 Resource zurück
  128. ID3D12Resource* DX12Textur::getResource()
  129. {
  130. return buffer;
  131. }