DX12Texture.cpp 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  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. bufferChanged = 1;
  55. if (getDirection() == RAM_TO_GPU)
  56. {
  57. const UINT64 uploadBufferSize
  58. = GetRequiredIntermediateSize(buffer, 0, 1);
  59. D3D12_RESOURCE_DESC iDescription;
  60. iDescription.Dimension = D3D12_RESOURCE_DIMENSION_BUFFER;
  61. iDescription.Alignment = 0;
  62. iDescription.Width = uploadBufferSize;
  63. iDescription.Height = 1;
  64. iDescription.DepthOrArraySize = 1;
  65. iDescription.MipLevels = 1;
  66. iDescription.Format = DXGI_FORMAT_UNKNOWN;
  67. iDescription.SampleDesc.Count = 1;
  68. iDescription.SampleDesc.Quality = 0;
  69. iDescription.Layout = D3D12_TEXTURE_LAYOUT_ROW_MAJOR;
  70. iDescription.Flags = D3D12_RESOURCE_FLAG_NONE;
  71. hprop.Type = D3D12_HEAP_TYPE_UPLOAD;
  72. device->CreateCommittedResource(&hprop,
  73. D3D12_HEAP_FLAG_NONE,
  74. &iDescription,
  75. D3D12_RESOURCE_STATE_GENERIC_READ,
  76. 0,
  77. __uuidof(ID3D12Resource),
  78. (void**)&upload);
  79. }
  80. }
  81. if (getDirection() == RAM_TO_GPU)
  82. {
  83. if (bild && (changed || bild->getNeedRender()))
  84. {
  85. changed = 0;
  86. }
  87. D3D12_SUBRESOURCE_DATA textureData = {};
  88. textureData.pData = bild->getBuffer();
  89. textureData.RowPitch = bild->getWidth() * sizeof(int);
  90. textureData.SlicePitch = textureData.RowPitch * bild->getHeight();
  91. UpdateSubresources(
  92. direct->zCommandList(), buffer, upload, 0, 0, 1, &textureData);
  93. }
  94. // TODO: Implement GPU_TO_RAM direction
  95. #endif
  96. return 1;
  97. }
  98. // Returns true if updateTextur needs to be called
  99. bool DX12Texture::needsUpdate() const
  100. {
  101. return bild && !buffer;
  102. }
  103. // Returns the DX12 resource
  104. ID3D12Resource* DX12Texture::zResource()
  105. {
  106. return buffer;
  107. }