DX12ChunkData.cpp 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272
  1. #include "Dx12ChunkData.h"
  2. #include <DX12CommandQueue.h>
  3. #include "Constants.h"
  4. DX12ChunkData::DX12ChunkData(Framework::Point center)
  5. : ReferenceCounter(),
  6. aabbs(0),
  7. chunkInfoBuffer(0),
  8. blasScratchBuffer(0),
  9. blasBuffer(0),
  10. blockIndexBuffer(0),
  11. textureIdBuffer(0),
  12. chunkCenter(center),
  13. blockIndices(),
  14. textureIds(),
  15. changed(0),
  16. bufferChanged(0)
  17. {}
  18. DX12ChunkData::~DX12ChunkData()
  19. {
  20. if (aabbs)
  21. {
  22. aabbs->release();
  23. }
  24. if (chunkInfoBuffer)
  25. {
  26. chunkInfoBuffer->release();
  27. }
  28. if (blasScratchBuffer)
  29. {
  30. blasScratchBuffer->release();
  31. }
  32. if (blasBuffer)
  33. {
  34. blasBuffer->release();
  35. }
  36. if (blockIndexBuffer)
  37. {
  38. blockIndexBuffer->release();
  39. }
  40. if (textureIdBuffer)
  41. {
  42. textureIdBuffer->release();
  43. }
  44. }
  45. Framework::DX12Buffer* DX12ChunkData::zChunkInfoBuffer() const
  46. {
  47. return chunkInfoBuffer;
  48. }
  49. Framework::DX12Buffer* DX12ChunkData::zBlasBuffer() const
  50. {
  51. return blasBuffer;
  52. }
  53. Framework::DX12Buffer* DX12ChunkData::zBlockIndexBuffer() const
  54. {
  55. return blockIndexBuffer;
  56. }
  57. Framework::DX12Buffer* DX12ChunkData::zTextureIdBuffer() const
  58. {
  59. return textureIdBuffer;
  60. }
  61. void DX12ChunkData::setBlock(int index, Block* zBlock)
  62. {
  63. if (zBlock)
  64. {
  65. ArrayIterator<int> it = blockIndices.begin();
  66. ArrayIterator<int> textureIt = textureIds.begin();
  67. int pos = 0;
  68. while (it)
  69. {
  70. if (it.val() == index)
  71. {
  72. for (int i = 0; i < 6; ++i)
  73. {
  74. textureIt.set(
  75. zBlock->zTexture()->zPolygonTexture(i)->getId());
  76. ++textureIt;
  77. }
  78. changed = 1;
  79. return;
  80. }
  81. ++it;
  82. ++pos;
  83. for (int i = 0; i < 6; ++i)
  84. {
  85. ++textureIt;
  86. }
  87. }
  88. blockIndices.add(index);
  89. for (int i = 0; i < 6; ++i)
  90. {
  91. textureIds.add(zBlock->zTexture()->zPolygonTexture(i)->getId());
  92. }
  93. changed = 1;
  94. }
  95. else
  96. {
  97. ArrayIterator<int> it = blockIndices.begin();
  98. ArrayIterator<int> textureIt = textureIds.begin();
  99. while (it)
  100. {
  101. if (it.val() == index)
  102. {
  103. for (int i = 0; i < 6; ++i)
  104. {
  105. textureIt.remove();
  106. }
  107. it.remove();
  108. changed = 1;
  109. return;
  110. }
  111. ++it;
  112. for (int i = 0; i < 6; ++i)
  113. {
  114. ++textureIt;
  115. }
  116. }
  117. }
  118. }
  119. void DX12ChunkData::updateBuffers(
  120. ID3D12Device5* zDevice, Framework::DX12CommandQueue* zQueue)
  121. {
  122. if (!aabbs)
  123. {
  124. aabbs = new DX12Buffer(sizeof(D3D12_RAYTRACING_AABB),
  125. zDevice,
  126. dynamic_cast<Framework::DX12CommandQueue*>(zQueue->getThis()),
  127. D3D12_RESOURCE_FLAG_NONE);
  128. aabbs->setLength(sizeof(D3D12_RAYTRACING_AABB));
  129. D3D12_RAYTRACING_AABB data = {chunkCenter.x - 8.f,
  130. chunkCenter.y - 8.f,
  131. 0,
  132. chunkCenter.x + 8.f,
  133. chunkCenter.y + 8.f,
  134. WORLD_HEIGHT};
  135. aabbs->setData(&data, 1);
  136. aabbs->copyToGPU();
  137. }
  138. if (!blasBuffer)
  139. {
  140. D3D12_RAYTRACING_GEOMETRY_DESC geometryDesc = {};
  141. geometryDesc.Type
  142. = D3D12_RAYTRACING_GEOMETRY_TYPE_PROCEDURAL_PRIMITIVE_AABBS;
  143. geometryDesc.Flags = D3D12_RAYTRACING_GEOMETRY_FLAG_NONE;
  144. geometryDesc.AABBs.AABBCount = 1;
  145. geometryDesc.AABBs.AABBs.StartAddress
  146. = aabbs->zBuffer()->GetGPUVirtualAddress();
  147. geometryDesc.AABBs.AABBs.StrideInBytes = sizeof(D3D12_RAYTRACING_AABB);
  148. D3D12_BUILD_RAYTRACING_ACCELERATION_STRUCTURE_INPUTS prebuildDesc;
  149. prebuildDesc.Type
  150. = D3D12_RAYTRACING_ACCELERATION_STRUCTURE_TYPE_BOTTOM_LEVEL;
  151. prebuildDesc.DescsLayout = D3D12_ELEMENTS_LAYOUT_ARRAY;
  152. prebuildDesc.NumDescs = 1;
  153. prebuildDesc.pGeometryDescs = &geometryDesc;
  154. prebuildDesc.Flags
  155. = D3D12_RAYTRACING_ACCELERATION_STRUCTURE_BUILD_FLAG_NONE;
  156. D3D12_RAYTRACING_ACCELERATION_STRUCTURE_PREBUILD_INFO info = {};
  157. zDevice->GetRaytracingAccelerationStructurePrebuildInfo(
  158. &prebuildDesc, &info);
  159. blasBuffer = new DX12Buffer(1,
  160. zDevice,
  161. dynamic_cast<DX12CommandQueue*>(zQueue->getThis()),
  162. D3D12_RESOURCE_FLAG_ALLOW_UNORDERED_ACCESS);
  163. blasBuffer->setLength(
  164. ROUND_UP_POWER_OF_2(info.ResultDataMaxSizeInBytes, 256));
  165. blasBuffer->createBufferWithoutData(
  166. D3D12_RESOURCE_STATE_RAYTRACING_ACCELERATION_STRUCTURE);
  167. blasScratchBuffer = new DX12Buffer(1,
  168. zDevice,
  169. dynamic_cast<DX12CommandQueue*>(zQueue->getThis()),
  170. D3D12_RESOURCE_FLAG_ALLOW_UNORDERED_ACCESS);
  171. blasScratchBuffer->setLength(
  172. ROUND_UP_POWER_OF_2(info.ScratchDataSizeInBytes, 256));
  173. blasScratchBuffer->createBufferWithoutData(D3D12_RESOURCE_STATE_COMMON);
  174. D3D12_BUILD_RAYTRACING_ACCELERATION_STRUCTURE_DESC buildDesc;
  175. buildDesc.Inputs.Type
  176. = D3D12_RAYTRACING_ACCELERATION_STRUCTURE_TYPE_BOTTOM_LEVEL;
  177. buildDesc.Inputs.DescsLayout = D3D12_ELEMENTS_LAYOUT_ARRAY;
  178. buildDesc.Inputs.NumDescs = 1;
  179. buildDesc.Inputs.pGeometryDescs = &geometryDesc;
  180. buildDesc.DestAccelerationStructureData
  181. = {blasBuffer->zBuffer()->GetGPUVirtualAddress()};
  182. buildDesc.ScratchAccelerationStructureData
  183. = {blasScratchBuffer->zBuffer()->GetGPUVirtualAddress()};
  184. buildDesc.SourceAccelerationStructureData = 0;
  185. buildDesc.Inputs.Flags
  186. = D3D12_RAYTRACING_ACCELERATION_STRUCTURE_BUILD_FLAG_NONE;
  187. zQueue->zCommandList()->BuildRaytracingAccelerationStructure(
  188. &buildDesc, 0, nullptr);
  189. }
  190. if (blockIndexBuffer
  191. && blockIndexBuffer->getElementCount() < blockIndices.getEntryCount())
  192. {
  193. blockIndexBuffer->release();
  194. blockIndexBuffer = 0;
  195. }
  196. if (!blockIndexBuffer)
  197. {
  198. blockIndexBuffer = new DX12Buffer(sizeof(int),
  199. zDevice,
  200. dynamic_cast<DX12CommandQueue*>(zQueue->getThis()),
  201. D3D12_RESOURCE_FLAG_NONE);
  202. blockIndexBuffer->setLength(blockIndices.getEntryCount() * sizeof(int));
  203. bufferChanged = 1;
  204. }
  205. if (textureIdBuffer
  206. && textureIdBuffer->getElementCount() < textureIds.getEntryCount())
  207. {
  208. textureIdBuffer->release();
  209. textureIdBuffer = 0;
  210. }
  211. if (!textureIdBuffer)
  212. {
  213. textureIdBuffer = new DX12Buffer(sizeof(int),
  214. zDevice,
  215. dynamic_cast<DX12CommandQueue*>(zQueue->getThis()),
  216. D3D12_RESOURCE_FLAG_NONE);
  217. textureIdBuffer->setLength(textureIds.getEntryCount() * sizeof(int));
  218. bufferChanged = 1;
  219. }
  220. if (!chunkInfoBuffer)
  221. {
  222. chunkInfoBuffer = new DX12Buffer(sizeof(ChunkShaderInfo),
  223. zDevice,
  224. dynamic_cast<DX12CommandQueue*>(zQueue->getThis()),
  225. D3D12_RESOURCE_FLAG_NONE);
  226. chunkInfoBuffer->setLength(sizeof(ChunkShaderInfo));
  227. }
  228. if (changed)
  229. {
  230. int* blockIndexData = new int[blockIndices.getEntryCount()];
  231. int* textureIdData = new int[textureIds.getEntryCount()];
  232. ArrayIterator<int> it = blockIndices.begin();
  233. ArrayIterator<int> textureIt = textureIds.begin();
  234. int index = 0;
  235. while (it)
  236. {
  237. blockIndexData[index] = it.val();
  238. for (int i = 0; i < 6; i++)
  239. {
  240. textureIdData[index * 6 + i] = textureIt.val();
  241. ++textureIt;
  242. }
  243. ++it;
  244. }
  245. blockIndexBuffer->setData(blockIndexData, 1);
  246. textureIdBuffer->setData(textureIdData, 1);
  247. blockIndexBuffer->copyToGPU(sizeof(int) * blockIndices.getEntryCount());
  248. textureIdBuffer->copyToGPU(sizeof(int) * textureIds.getEntryCount());
  249. delete[] blockIndexData;
  250. delete[] textureIdData;
  251. ChunkShaderInfo info
  252. = {chunkCenter, (unsigned int)blockIndices.getEntryCount()};
  253. chunkInfoBuffer->setData(&info, 1);
  254. chunkInfoBuffer->copyToGPU();
  255. }
  256. }