DX12Texture.cpp 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  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. {}
  13. DX12Texture::~DX12Texture()
  14. {
  15. #ifdef WIN32
  16. if (buffer) buffer->Release();
  17. #endif
  18. }
  19. // Updates the texture. The pixels of the current image are copied into the
  20. // graphics memory
  21. bool DX12Texture::updateTextur()
  22. {
  23. if (!bild) return 0;
  24. #ifdef WIN32
  25. if (!buffer)
  26. {
  27. D3D12_RESOURCE_DESC description;
  28. ZeroMemory(&description, sizeof(D3D12_RESOURCE_DESC));
  29. description.Dimension = D3D12_RESOURCE_DIMENSION_TEXTURE2D;
  30. description.Height = bild->getHeight();
  31. description.Width = bild->getWidth();
  32. description.DepthOrArraySize = 1;
  33. description.MipLevels = 1;
  34. description.Format = DXGI_FORMAT_B8G8R8A8_UNORM;
  35. description.SampleDesc.Count = 1;
  36. description.Layout = D3D12_TEXTURE_LAYOUT_UNKNOWN;
  37. description.Flags = getDirection() == GPU_TO_RAM
  38. ? D3D12_RESOURCE_FLAG_ALLOW_UNORDERED_ACCESS
  39. : D3D12_RESOURCE_FLAG_NONE;
  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. // TODO: copy from image to texture or from texture to image
  55. /* const UINT64 uploadBufferSize
  56. = GetRequiredIntermediateSize(buffer, 0, 1);
  57. D3D12_RESOURCE_DESC iDescription;
  58. iDescription.Dimension = D3D12_RESOURCE_DIMENSION_BUFFER;
  59. iDescription.Alignment = 0;
  60. iDescription.Width = uploadBufferSize;
  61. iDescription.Height = 1;
  62. iDescription.DepthOrArraySize = 1;
  63. iDescription.MipLevels = 1;
  64. iDescription.Format = DXGI_FORMAT_UNKNOWN;
  65. iDescription.SampleDesc.Count = 1;
  66. iDescription.SampleDesc.Quality = 0;
  67. iDescription.Layout = D3D12_TEXTURE_LAYOUT_ROW_MAJOR;
  68. iDescription.Flags = D3D12_RESOURCE_FLAG_NONE;
  69. hprop.Type = D3D12_HEAP_TYPE_UPLOAD;
  70. device->CreateCommittedResource(&hprop,
  71. D3D12_HEAP_FLAG_NONE,
  72. &iDescription,
  73. D3D12_RESOURCE_STATE_GENERIC_READ,
  74. 0,
  75. __uuidof(ID3D12Resource),
  76. (void**)&intermediate);
  77. shaderResource = 0;*/
  78. }
  79. /* if (bild && (changed || bild->getNeedRender()))
  80. {
  81. changed = 0;
  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->zCommandList()->ResourceBarrier(1, &barrier);
  94. shaderResource = 0;
  95. }
  96. D3D12_SUBRESOURCE_DATA textureData = {};
  97. textureData.pData = bild->getBuffer();
  98. textureData.RowPitch = bild->getWidth() * sizeof(int);
  99. textureData.SlicePitch = textureData.RowPitch * bild->getHeight();
  100. UpdateSubresources(direct->zCommandList(),
  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->zCommandList()->ResourceBarrier(1, &barrier);
  117. shaderResource = 1;
  118. }*/
  119. #endif
  120. return 1;
  121. }
  122. // Returns true if updateTextur needs to be called
  123. bool DX12Texture::needsUpdate() const
  124. {
  125. return bild && !buffer;
  126. }
  127. // Returns the DX12 resource
  128. ID3D12Resource* DX12Texture::zResource()
  129. {
  130. return buffer;
  131. }