DX12Buffer.cpp 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. #pragma once
  2. #include "DX12Buffer.h"
  3. #include "d3dx12.h"
  4. #include "DX12CommandQueue.h"
  5. using namespace Framework;
  6. // Constructor
  7. // eSize: length of an element in bytes
  8. DX12Buffer::DX12Buffer(int eSize, ID3D12Device5* device, int flags)
  9. : DXBuffer(eSize),
  10. buffer(0),
  11. device(device)
  12. {
  13. description = new D3D12_RESOURCE_DESC();
  14. ZeroMemory(description, sizeof(D3D12_RESOURCE_DESC));
  15. description->Dimension = D3D12_RESOURCE_DIMENSION_BUFFER;
  16. description->Alignment = 0;
  17. description->Height = 1;
  18. description->DepthOrArraySize = 1;
  19. description->MipLevels = 1;
  20. description->Format = DXGI_FORMAT_UNKNOWN;
  21. description->SampleDesc.Count = 1;
  22. description->Layout = D3D12_TEXTURE_LAYOUT_ROW_MAJOR;
  23. description->Flags = (D3D12_RESOURCE_FLAGS)flags;
  24. }
  25. // Destructor
  26. DX12Buffer::~DX12Buffer()
  27. {
  28. if (buffer) buffer->Release();
  29. delete description;
  30. }
  31. // Copies the data into the buffer if it has changed
  32. void DX12Buffer::copyToGPU(__int64 byteCount)
  33. {
  34. if (!len) return;
  35. if (byteCount < 0) byteCount = len;
  36. if (description->Width != (unsigned)len)
  37. {
  38. if (buffer) buffer->Release();
  39. buffer = 0;
  40. description->Width = (unsigned)len;
  41. }
  42. if (!buffer)
  43. {
  44. D3D12_HEAP_PROPERTIES hprop;
  45. hprop.Type = D3D12_HEAP_TYPE_UPLOAD;
  46. hprop.CPUPageProperty = D3D12_CPU_PAGE_PROPERTY_UNKNOWN;
  47. hprop.MemoryPoolPreference = D3D12_MEMORY_POOL_UNKNOWN;
  48. hprop.CreationNodeMask = 1;
  49. hprop.VisibleNodeMask = 1;
  50. device->CreateCommittedResource(&hprop,
  51. D3D12_HEAP_FLAG_NONE,
  52. description,
  53. D3D12_RESOURCE_STATE_GENERIC_READ,
  54. 0,
  55. __uuidof(ID3D12Resource),
  56. (void**)&buffer);
  57. }
  58. if (changed && data)
  59. {
  60. BYTE* pData;
  61. D3D12_RANGE r;
  62. r.Begin = 0;
  63. r.End = 0;
  64. buffer->Map(0, &r, (void**)&pData);
  65. memcpy(pData, data, byteCount);
  66. r.End = byteCount;
  67. buffer->Unmap(0, &r);
  68. changed = 0;
  69. }
  70. }
  71. void Framework::DX12Buffer::createBufferWithoutData(
  72. D3D12_RESOURCE_STATES states)
  73. {
  74. if (!len) return;
  75. if (description->Width != len)
  76. {
  77. if (buffer) buffer->Release();
  78. buffer = 0;
  79. description->Width = len;
  80. }
  81. if (!buffer)
  82. {
  83. D3D12_HEAP_PROPERTIES hprop;
  84. hprop.Type = D3D12_HEAP_TYPE_DEFAULT;
  85. hprop.CPUPageProperty = D3D12_CPU_PAGE_PROPERTY_UNKNOWN;
  86. hprop.MemoryPoolPreference = D3D12_MEMORY_POOL_UNKNOWN;
  87. hprop.CreationNodeMask = 0;
  88. hprop.VisibleNodeMask = 0;
  89. device->CreateCommittedResource(&hprop,
  90. D3D12_HEAP_FLAG_NONE,
  91. description,
  92. states,
  93. 0,
  94. __uuidof(ID3D12Resource),
  95. (void**)&buffer);
  96. }
  97. }
  98. // Returns the buffer
  99. ID3D12Resource* DX12Buffer::zBuffer() const
  100. {
  101. return buffer;
  102. }