DX12Texture.cpp 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. #include "DX12Texture.h"
  2. #include "d3dx12.h"
  3. #include "DX12CommandQueue.h"
  4. #include "Image.h"
  5. using namespace Framework;
  6. DX12Texture::DX12Texture(ID3D12Device* device,
  7. DX12CopyCommandQueue* copy,
  8. DX12DirectCommandQueue* direct,
  9. TextureDirection dir)
  10. : Texture(dir),
  11. buffer(0),
  12. device(device),
  13. copy(copy),
  14. direct(direct)
  15. {}
  16. DX12Texture::~DX12Texture()
  17. {
  18. #ifdef WIN32
  19. if (buffer) buffer->Release();
  20. #endif
  21. }
  22. // Updates the texture. The pixels of the current image are copied into the
  23. // graphics memory
  24. bool DX12Texture::updateTextur()
  25. {
  26. if (!bild) return 0;
  27. #ifdef WIN32
  28. if (!buffer)
  29. {
  30. D3D12_RESOURCE_DESC description;
  31. ZeroMemory(&description, sizeof(D3D12_RESOURCE_DESC));
  32. description.Dimension = D3D12_RESOURCE_DIMENSION_TEXTURE2D;
  33. description.Height = bild->getHeight();
  34. description.Width = bild->getWidth();
  35. description.DepthOrArraySize = 1;
  36. description.MipLevels = 1;
  37. description.Format = DXGI_FORMAT_B8G8R8A8_UNORM;
  38. description.SampleDesc.Count = 1;
  39. description.Layout = D3D12_TEXTURE_LAYOUT_UNKNOWN;
  40. description.Flags = getDirection() == GPU_TO_RAM
  41. ? D3D12_RESOURCE_FLAG_ALLOW_UNORDERED_ACCESS
  42. : 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. getDirection() == RAM_TO_GPU ? D3D12_RESOURCE_STATE_COPY_DEST
  53. : D3D12_RESOURCE_STATE_COPY_SOURCE,
  54. 0,
  55. __uuidof(ID3D12Resource),
  56. (void**)&buffer);
  57. // TODO: copy from image to texture or from texture to image
  58. /* const UINT64 uploadBufferSize
  59. = GetRequiredIntermediateSize(buffer, 0, 1);
  60. D3D12_RESOURCE_DESC iDescription;
  61. iDescription.Dimension = D3D12_RESOURCE_DIMENSION_BUFFER;
  62. iDescription.Alignment = 0;
  63. iDescription.Width = uploadBufferSize;
  64. iDescription.Height = 1;
  65. iDescription.DepthOrArraySize = 1;
  66. iDescription.MipLevels = 1;
  67. iDescription.Format = DXGI_FORMAT_UNKNOWN;
  68. iDescription.SampleDesc.Count = 1;
  69. iDescription.SampleDesc.Quality = 0;
  70. iDescription.Layout = D3D12_TEXTURE_LAYOUT_ROW_MAJOR;
  71. iDescription.Flags = D3D12_RESOURCE_FLAG_NONE;
  72. hprop.Type = D3D12_HEAP_TYPE_UPLOAD;
  73. device->CreateCommittedResource(&hprop,
  74. D3D12_HEAP_FLAG_NONE,
  75. &iDescription,
  76. D3D12_RESOURCE_STATE_GENERIC_READ,
  77. 0,
  78. __uuidof(ID3D12Resource),
  79. (void**)&intermediate);
  80. shaderResource = 0;*/
  81. }
  82. /* if (bild && (changed || bild->getNeedRender()))
  83. {
  84. changed = 0;
  85. if (shaderResource)
  86. {
  87. D3D12_RESOURCE_BARRIER barrier;
  88. barrier.Type = D3D12_RESOURCE_BARRIER_TYPE_TRANSITION;
  89. barrier.Transition.pResource = buffer;
  90. barrier.Transition.StateBefore
  91. = D3D12_RESOURCE_STATE_PIXEL_SHADER_RESOURCE
  92. | D3D12_RESOURCE_STATE_NON_PIXEL_SHADER_RESOURCE;
  93. barrier.Transition.StateAfter = D3D12_RESOURCE_STATE_COPY_DEST;
  94. barrier.Transition.Subresource = 0;
  95. barrier.Flags = D3D12_RESOURCE_BARRIER_FLAG_NONE;
  96. direct->zCommandList()->ResourceBarrier(1, &barrier);
  97. shaderResource = 0;
  98. }
  99. D3D12_SUBRESOURCE_DATA textureData = {};
  100. textureData.pData = bild->getBuffer();
  101. textureData.RowPitch = bild->getWidth() * sizeof(int);
  102. textureData.SlicePitch = textureData.RowPitch * bild->getHeight();
  103. UpdateSubresources(direct->zCommandList(),
  104. buffer,
  105. intermediate,
  106. 0,
  107. 0,
  108. 1,
  109. &textureData);
  110. D3D12_RESOURCE_BARRIER barrier;
  111. barrier.Type = D3D12_RESOURCE_BARRIER_TYPE_TRANSITION;
  112. barrier.Transition.pResource = buffer;
  113. barrier.Transition.StateBefore = D3D12_RESOURCE_STATE_COPY_DEST;
  114. barrier.Transition.StateAfter
  115. = D3D12_RESOURCE_STATE_PIXEL_SHADER_RESOURCE
  116. | D3D12_RESOURCE_STATE_NON_PIXEL_SHADER_RESOURCE;
  117. barrier.Transition.Subresource = 0;
  118. barrier.Flags = D3D12_RESOURCE_BARRIER_FLAG_NONE;
  119. direct->zCommandList()->ResourceBarrier(1, &barrier);
  120. shaderResource = 1;
  121. }*/
  122. #endif
  123. return 1;
  124. }
  125. // Returns true if updateTextur needs to be called
  126. bool DX12Texture::needsUpdate() const
  127. {
  128. return bild && !buffer;
  129. }
  130. // Returns the DX12 resource
  131. ID3D12Resource* DX12Texture::zResource()
  132. {
  133. return buffer;
  134. }