DX11Buffer.cpp 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. #include "DX11Buffer.h"
  2. #include "Logging.h"
  3. #ifdef WIN32
  4. # include <d3d11.h>
  5. # include <d3d12.h>
  6. # include "d3dx12.h"
  7. // Contents of the DX11Buffer class
  8. // Constructor
  9. // eSize: The length of an element in bytes
  10. Framework::DX11Buffer::DX11Buffer(int eSize,
  11. ID3D11Device* device,
  12. ID3D11DeviceContext* context,
  13. int bindFlags,
  14. Critical& deviceLock)
  15. : DXBuffer(eSize),
  16. deviceLock(deviceLock)
  17. {
  18. buffer = 0;
  19. description = new D3D11_BUFFER_DESC();
  20. memset(description, 0, sizeof(description));
  21. description->Usage = D3D11_USAGE_DYNAMIC;
  22. description->CPUAccessFlags = D3D11_CPU_ACCESS_WRITE;
  23. description->BindFlags = bindFlags;
  24. this->device = device;
  25. this->context = context;
  26. }
  27. // Destructor
  28. Framework::DX11Buffer::~DX11Buffer()
  29. {
  30. if (buffer) buffer->Release();
  31. delete description;
  32. }
  33. // Copies the data into the buffer if it has changed
  34. // zRObj: The object used to communicate with the graphics card
  35. void Framework::DX11Buffer::copyToGPU(__int64 byteCount)
  36. {
  37. if (!len) return;
  38. if (byteCount < 0) byteCount = len;
  39. if (description->ByteWidth < (unsigned)len)
  40. {
  41. if (buffer) buffer->Release();
  42. buffer = 0;
  43. }
  44. if (!buffer)
  45. {
  46. description->ByteWidth = (unsigned)len;
  47. deviceLock.lock();
  48. device->CreateBuffer(description, 0, &buffer);
  49. deviceLock.unlock();
  50. if (data) changed = 1;
  51. }
  52. if (changed)
  53. {
  54. HRESULT res;
  55. D3D11_MAPPED_SUBRESOURCE map;
  56. deviceLock.lock();
  57. if ((description->Usage | D3D11_USAGE_DYNAMIC) == description->Usage)
  58. res = context->Map(
  59. buffer, 0, D3D11_MAP::D3D11_MAP_WRITE_DISCARD, 0, &map);
  60. else
  61. res = context->Map(buffer, 0, D3D11_MAP::D3D11_MAP_WRITE, 0, &map);
  62. deviceLock.unlock();
  63. if (res == S_OK)
  64. {
  65. memcpy(map.pData, data, byteCount);
  66. deviceLock.lock();
  67. context->Unmap(buffer, 0);
  68. deviceLock.unlock();
  69. changed = 0;
  70. }
  71. else
  72. {
  73. Logging::error()
  74. << "Could not update buffer: " << std::hex << res << std::endl;
  75. }
  76. }
  77. }
  78. // Returns the buffer
  79. ID3D11Buffer* Framework::DX11Buffer::zBuffer() const
  80. {
  81. return buffer;
  82. }
  83. // Contents of the DXStructuredBuffer class
  84. // Constructor
  85. // eSize: The length of an element in bytes
  86. Framework::DX11StructuredBuffer::DX11StructuredBuffer(int eSize,
  87. ID3D11Device* device,
  88. ID3D11DeviceContext* context,
  89. Critical& deviceLock)
  90. : DX11Buffer(eSize,
  91. device,
  92. context,
  93. D3D11_BIND_UNORDERED_ACCESS | D3D11_BIND_SHADER_RESOURCE,
  94. deviceLock)
  95. {
  96. description->MiscFlags = D3D11_RESOURCE_MISC_BUFFER_STRUCTURED;
  97. description->StructureByteStride = eSize;
  98. description->Usage = D3D11_USAGE_DEFAULT;
  99. view = 0;
  100. }
  101. // Destructor
  102. Framework::DX11StructuredBuffer::~DX11StructuredBuffer()
  103. {
  104. if (view) view->Release();
  105. }
  106. // Copies the data into the buffer if it has changed
  107. // zRObj: The object used to communicate with the graphics card
  108. void Framework::DX11StructuredBuffer::copyToGPU(__int64 byteCount)
  109. {
  110. ID3D11Buffer* old = buffer;
  111. DX11Buffer::copyToGPU(byteCount);
  112. if (buffer != old)
  113. {
  114. if (view) view->Release();
  115. D3D11_SHADER_RESOURCE_VIEW_DESC desc = {};
  116. desc.ViewDimension = D3D11_SRV_DIMENSION_BUFFEREX;
  117. desc.BufferEx.FirstElement = 0;
  118. desc.Format = DXGI_FORMAT_UNKNOWN;
  119. desc.BufferEx.NumElements
  120. = description->ByteWidth / description->StructureByteStride;
  121. deviceLock.lock();
  122. device->CreateShaderResourceView(buffer, &desc, &view);
  123. deviceLock.unlock();
  124. }
  125. }
  126. // Returns the used shader resource view
  127. Framework::DX11StructuredBuffer::operator ID3D11ShaderResourceView*() const
  128. {
  129. return view;
  130. }
  131. #endif