DX12Texture.cpp 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. #include "DX12Texture.h"
  2. #include "d3dx12.h"
  3. #include "DX12CommandQueue.h"
  4. #include "Image.h"
  5. using namespace Framework;
  6. DX12Texture::DX12Texture(
  7. ID3D12Device* device, DX12DirectCommandQueue* direct, TextureDirection dir)
  8. : Texture(dir),
  9. buffer(0),
  10. device(device),
  11. direct(direct),
  12. upload(0)
  13. {}
  14. DX12Texture::~DX12Texture()
  15. {
  16. #ifdef WIN32
  17. if (buffer) buffer->Release();
  18. if (upload) upload->Release();
  19. #endif
  20. }
  21. // Updates the texture. The pixels of the current image are copied into the
  22. // graphics memory
  23. bool DX12Texture::updateTextur()
  24. {
  25. if (!bild) return 0;
  26. #ifdef WIN32
  27. if (!buffer)
  28. {
  29. D3D12_RESOURCE_DESC description;
  30. ZeroMemory(&description, sizeof(D3D12_RESOURCE_DESC));
  31. description.Dimension = D3D12_RESOURCE_DIMENSION_TEXTURE2D;
  32. description.Height = bild->getHeight();
  33. description.Width = bild->getWidth();
  34. description.DepthOrArraySize = 1;
  35. description.MipLevels = 1;
  36. description.Format = DXGI_FORMAT_B8G8R8A8_UNORM;
  37. description.SampleDesc.Count = 1;
  38. description.Layout = D3D12_TEXTURE_LAYOUT_UNKNOWN;
  39. description.Flags = D3D12_RESOURCE_FLAG_ALLOW_UNORDERED_ACCESS;
  40. D3D12_HEAP_PROPERTIES hprop;
  41. hprop.Type = D3D12_HEAP_TYPE_DEFAULT;
  42. hprop.CPUPageProperty = D3D12_CPU_PAGE_PROPERTY_UNKNOWN;
  43. hprop.MemoryPoolPreference = D3D12_MEMORY_POOL_UNKNOWN;
  44. hprop.CreationNodeMask = 1;
  45. hprop.VisibleNodeMask = 1;
  46. device->CreateCommittedResource(&hprop,
  47. D3D12_HEAP_FLAG_NONE,
  48. &description,
  49. getDirection() == RAM_TO_GPU ? D3D12_RESOURCE_STATE_COPY_DEST
  50. : D3D12_RESOURCE_STATE_COPY_SOURCE,
  51. 0,
  52. __uuidof(ID3D12Resource),
  53. (void**)&buffer);
  54. if (getDirection() == RAM_TO_GPU)
  55. {
  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**)&upload);
  78. }
  79. }
  80. if (getDirection() == RAM_TO_GPU)
  81. {
  82. if (bild && (changed || bild->getNeedRender()))
  83. {
  84. changed = 0;
  85. }
  86. D3D12_SUBRESOURCE_DATA textureData = {};
  87. textureData.pData = bild->getBuffer();
  88. textureData.RowPitch = bild->getWidth() * sizeof(int);
  89. textureData.SlicePitch = textureData.RowPitch * bild->getHeight();
  90. UpdateSubresources(
  91. direct->zCommandList(), buffer, upload, 0, 0, 1, &textureData);
  92. }
  93. /* if (bild && (changed || bild->getNeedRender()))
  94. {
  95. changed = 0;
  96. if (shaderResource)
  97. {
  98. D3D12_RESOURCE_BARRIER barrier;
  99. barrier.Type = D3D12_RESOURCE_BARRIER_TYPE_TRANSITION;
  100. barrier.Transition.pResource = buffer;
  101. barrier.Transition.StateBefore
  102. = D3D12_RESOURCE_STATE_PIXEL_SHADER_RESOURCE
  103. | D3D12_RESOURCE_STATE_NON_PIXEL_SHADER_RESOURCE;
  104. barrier.Transition.StateAfter = D3D12_RESOURCE_STATE_COPY_DEST;
  105. barrier.Transition.Subresource = 0;
  106. barrier.Flags = D3D12_RESOURCE_BARRIER_FLAG_NONE;
  107. direct->zCommandList()->ResourceBarrier(1, &barrier);
  108. shaderResource = 0;
  109. }
  110. D3D12_SUBRESOURCE_DATA textureData = {};
  111. textureData.pData = bild->getBuffer();
  112. textureData.RowPitch = bild->getWidth() * sizeof(int);
  113. textureData.SlicePitch = textureData.RowPitch * bild->getHeight();
  114. UpdateSubresources(direct->zCommandList(),
  115. buffer,
  116. intermediate,
  117. 0,
  118. 0,
  119. 1,
  120. &textureData);
  121. D3D12_RESOURCE_BARRIER barrier;
  122. barrier.Type = D3D12_RESOURCE_BARRIER_TYPE_TRANSITION;
  123. barrier.Transition.pResource = buffer;
  124. barrier.Transition.StateBefore = D3D12_RESOURCE_STATE_COPY_DEST;
  125. barrier.Transition.StateAfter
  126. = D3D12_RESOURCE_STATE_PIXEL_SHADER_RESOURCE
  127. | D3D12_RESOURCE_STATE_NON_PIXEL_SHADER_RESOURCE;
  128. barrier.Transition.Subresource = 0;
  129. barrier.Flags = D3D12_RESOURCE_BARRIER_FLAG_NONE;
  130. direct->zCommandList()->ResourceBarrier(1, &barrier);
  131. shaderResource = 1;
  132. }*/
  133. #endif
  134. return 1;
  135. }
  136. // Returns true if updateTextur needs to be called
  137. bool DX12Texture::needsUpdate() const
  138. {
  139. return bild && !buffer;
  140. }
  141. // Returns the DX12 resource
  142. ID3D12Resource* DX12Texture::zResource()
  143. {
  144. return buffer;
  145. }