DX12Texture.cpp 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  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. wcName(0)
  14. {}
  15. DX12Texture::~DX12Texture()
  16. {
  17. #ifdef WIN32
  18. if (buffer) buffer->Release();
  19. if (upload) upload->Release();
  20. #endif
  21. delete[] wcName;
  22. }
  23. // Updates the texture. The pixels of the current image are copied into the
  24. // graphics memory
  25. bool DX12Texture::updateTextur()
  26. {
  27. if (!bild) return 0;
  28. #ifdef WIN32
  29. if (!buffer)
  30. {
  31. D3D12_RESOURCE_DESC description;
  32. ZeroMemory(&description, sizeof(D3D12_RESOURCE_DESC));
  33. description.Dimension = D3D12_RESOURCE_DIMENSION_TEXTURE2D;
  34. description.Height = bild->getHeight();
  35. description.Width = bild->getWidth();
  36. description.DepthOrArraySize = 1;
  37. description.MipLevels = 1;
  38. description.Format = DXGI_FORMAT_B8G8R8A8_UNORM;
  39. description.SampleDesc.Count = 1;
  40. description.Layout = D3D12_TEXTURE_LAYOUT_UNKNOWN;
  41. description.Flags = D3D12_RESOURCE_FLAG_ALLOW_UNORDERED_ACCESS;
  42. D3D12_HEAP_PROPERTIES hprop;
  43. hprop.Type = D3D12_HEAP_TYPE_DEFAULT;
  44. hprop.CPUPageProperty = D3D12_CPU_PAGE_PROPERTY_UNKNOWN;
  45. hprop.MemoryPoolPreference = D3D12_MEMORY_POOL_UNKNOWN;
  46. hprop.CreationNodeMask = 1;
  47. hprop.VisibleNodeMask = 1;
  48. device->CreateCommittedResource(&hprop,
  49. D3D12_HEAP_FLAG_NONE,
  50. &description,
  51. getDirection() == RAM_TO_GPU ? D3D12_RESOURCE_STATE_COPY_DEST
  52. : D3D12_RESOURCE_STATE_COPY_SOURCE,
  53. 0,
  54. __uuidof(ID3D12Resource),
  55. (void**)&buffer);
  56. if (!wcName)
  57. {
  58. wcName = new wchar_t[name.getLength() + 1];
  59. mbstowcs_s(0,
  60. wcName,
  61. name.getLength() + 1,
  62. name.getText(),
  63. name.getLength() + 1);
  64. }
  65. buffer->SetName(wcName);
  66. bufferChanged = 1;
  67. if (getDirection() == RAM_TO_GPU)
  68. {
  69. const UINT64 uploadBufferSize
  70. = GetRequiredIntermediateSize(buffer, 0, 1);
  71. D3D12_RESOURCE_DESC iDescription;
  72. iDescription.Dimension = D3D12_RESOURCE_DIMENSION_BUFFER;
  73. iDescription.Alignment = 0;
  74. iDescription.Width = uploadBufferSize;
  75. iDescription.Height = 1;
  76. iDescription.DepthOrArraySize = 1;
  77. iDescription.MipLevels = 1;
  78. iDescription.Format = DXGI_FORMAT_UNKNOWN;
  79. iDescription.SampleDesc.Count = 1;
  80. iDescription.SampleDesc.Quality = 0;
  81. iDescription.Layout = D3D12_TEXTURE_LAYOUT_ROW_MAJOR;
  82. iDescription.Flags = D3D12_RESOURCE_FLAG_NONE;
  83. hprop.Type = D3D12_HEAP_TYPE_UPLOAD;
  84. device->CreateCommittedResource(&hprop,
  85. D3D12_HEAP_FLAG_NONE,
  86. &iDescription,
  87. D3D12_RESOURCE_STATE_GENERIC_READ,
  88. 0,
  89. __uuidof(ID3D12Resource),
  90. (void**)&upload);
  91. }
  92. }
  93. if (getDirection() == RAM_TO_GPU)
  94. {
  95. if (bild && (changed || bild->getNeedRender()))
  96. {
  97. changed = 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(
  104. direct->zCommandList(), buffer, upload, 0, 0, 1, &textureData);
  105. }
  106. // TODO: Implement GPU_TO_RAM direction
  107. #endif
  108. return 1;
  109. }
  110. // Returns true if updateTextur needs to be called
  111. bool DX12Texture::needsUpdate() const
  112. {
  113. return bild && !buffer;
  114. }
  115. // Returns the DX12 resource
  116. ID3D12Resource* DX12Texture::zResource()
  117. {
  118. return buffer;
  119. }