DX12ChunkData.cpp 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796
  1. #include "Dx12ChunkData.h"
  2. #include <DX12BLASModel.h>
  3. #include <DX12CommandQueue.h>
  4. #include "Block.h"
  5. #include "Constants.h"
  6. #include "d3dx12.h"
  7. #include "Dimension.h"
  8. DX12ChunkData::DX12ChunkData(Framework::Point center)
  9. : ReferenceCounter(),
  10. aabbs(0),
  11. chunkInfoBuffer(0),
  12. blasScratchBuffer(0),
  13. customBlocksBlasScratchBuffer(0),
  14. oldBlasBuffer(0),
  15. blasBuffer(0),
  16. customBlocksBlasBuffer(0),
  17. blockIndexBuffer(0),
  18. textureIdBuffer(0),
  19. customBlockTextures(0),
  20. customBlocksCombinedIndexBuffer(0),
  21. customBlocksVertexDataBuffer(0),
  22. customBlocksIndexOffsetBuffer(0),
  23. customBlockMatrixBuffer(0),
  24. lastObjectIndex(-1),
  25. lastCustomObjectIndex(-1),
  26. lastHitGroupIndex(-1),
  27. lastCustomHitGroupIndex(-1),
  28. chunkCenter(center),
  29. blockIndices(new int[CHUNK_SIZE * CHUNK_SIZE * WORLD_HEIGHT]),
  30. textureIds(),
  31. changed(0),
  32. bufferChanged(0),
  33. minZ(WORLD_HEIGHT),
  34. maxZ(0),
  35. minMaxChanged(0),
  36. customBufferChanged(0),
  37. customBlocksChanged(0)
  38. {
  39. memset(
  40. blockIndices, -1, sizeof(int) * CHUNK_SIZE * CHUNK_SIZE * WORLD_HEIGHT);
  41. }
  42. DX12ChunkData::~DX12ChunkData()
  43. {
  44. if (aabbs)
  45. {
  46. aabbs->release();
  47. }
  48. if (chunkInfoBuffer)
  49. {
  50. chunkInfoBuffer->release();
  51. }
  52. if (blasScratchBuffer)
  53. {
  54. blasScratchBuffer->release();
  55. }
  56. if (customBlocksBlasScratchBuffer)
  57. {
  58. customBlocksBlasScratchBuffer->release();
  59. }
  60. if (oldBlasBuffer)
  61. {
  62. oldBlasBuffer->Release();
  63. }
  64. if (blasBuffer)
  65. {
  66. blasBuffer->release();
  67. }
  68. if (customBlocksBlasBuffer)
  69. {
  70. customBlocksBlasBuffer->release();
  71. }
  72. if (blockIndexBuffer)
  73. {
  74. blockIndexBuffer->release();
  75. }
  76. if (textureIdBuffer)
  77. {
  78. textureIdBuffer->release();
  79. }
  80. if (customBlockTextures)
  81. {
  82. customBlockTextures->release();
  83. }
  84. if (customBlocksCombinedIndexBuffer)
  85. {
  86. customBlocksCombinedIndexBuffer->release();
  87. }
  88. if (customBlocksVertexDataBuffer)
  89. {
  90. customBlocksVertexDataBuffer->release();
  91. }
  92. if (customBlocksIndexOffsetBuffer)
  93. {
  94. customBlocksIndexOffsetBuffer->release();
  95. }
  96. if (customBlockMatrixBuffer)
  97. {
  98. customBlockMatrixBuffer->release();
  99. }
  100. delete[] blockIndices;
  101. }
  102. Framework::DX12Buffer* DX12ChunkData::zChunkInfoBuffer() const
  103. {
  104. return chunkInfoBuffer;
  105. }
  106. Framework::DX12Buffer* DX12ChunkData::zBlasBuffer() const
  107. {
  108. return blasBuffer;
  109. }
  110. Framework::DX12Buffer* DX12ChunkData::zBlockIndexBuffer() const
  111. {
  112. return blockIndexBuffer;
  113. }
  114. Framework::DX12Buffer* DX12ChunkData::zTextureIdBuffer() const
  115. {
  116. return textureIdBuffer;
  117. }
  118. Framework::DX12Buffer* DX12ChunkData::zCustomBlocksBlasBuffer() const
  119. {
  120. return customBlocksBlasBuffer;
  121. }
  122. Framework::DX12Buffer* DX12ChunkData::zCustomBlocksTextureBuffer() const
  123. {
  124. return customBlockTextures;
  125. }
  126. Framework::DX12Buffer* DX12ChunkData::zCustomBlocksVertexDataBuffer() const
  127. {
  128. return customBlocksVertexDataBuffer;
  129. }
  130. Framework::DX12Buffer* DX12ChunkData::zCustomBlocksCombinedIndexBuffer() const
  131. {
  132. return customBlocksCombinedIndexBuffer;
  133. }
  134. Framework::DX12Buffer* DX12ChunkData::zCustomBlocksIndexOffsetBuffer() const
  135. {
  136. return customBlocksIndexOffsetBuffer;
  137. }
  138. void DX12ChunkData::setBlock(int index, Block* zBlock)
  139. {
  140. cs.lock();
  141. if (zBlock
  142. && zBlock->zBlockType()->getModelInfo().getModelName().isEqual("cube"))
  143. {
  144. if (blockIndices[index] < 0)
  145. {
  146. blockIndices[index] = textureIds.getEntryCount();
  147. for (int i = 0; i < 6; ++i)
  148. {
  149. textureIds.add(zBlock->zTexture()->zPolygonTexture(i)->getId());
  150. }
  151. }
  152. else
  153. {
  154. ArrayIterator<int> textureIt = textureIds.begin();
  155. int tmp = blockIndices[index];
  156. while (tmp > 0)
  157. {
  158. ++textureIt;
  159. --tmp;
  160. }
  161. for (int i = 0; i < 6; ++i)
  162. {
  163. textureIt.set(zBlock->zTexture()->zPolygonTexture(i)->getId());
  164. ++textureIt;
  165. }
  166. }
  167. if (zBlock->getLocation().z + 1 > maxZ)
  168. {
  169. maxZ = zBlock->getLocation().z + 1;
  170. minMaxChanged = 1;
  171. }
  172. if (zBlock->getLocation().z < minZ)
  173. {
  174. minZ = zBlock->getLocation().z;
  175. minMaxChanged = 1;
  176. }
  177. }
  178. else
  179. {
  180. if (blockIndices[index] >= 0)
  181. {
  182. int tmp = blockIndices[index];
  183. ArrayIterator<int> textureIt = textureIds.begin();
  184. while (tmp > 0)
  185. {
  186. ++textureIt;
  187. --tmp;
  188. }
  189. for (int i = 0; i < 6; ++i)
  190. {
  191. textureIt.remove();
  192. }
  193. int oldMinZ = minZ;
  194. int oldMaxZ = maxZ;
  195. maxZ = 0;
  196. minZ = WORLD_HEIGHT;
  197. for (int i = 0; i < CHUNK_SIZE * CHUNK_SIZE * WORLD_HEIGHT; ++i)
  198. {
  199. if (blockIndices[i] > blockIndices[index])
  200. {
  201. blockIndices[i] -= 6;
  202. }
  203. if (blockIndices[i] >= 0)
  204. {
  205. if (i % WORLD_HEIGHT + 1 > maxZ)
  206. {
  207. maxZ = i % WORLD_HEIGHT + 1;
  208. }
  209. if (i % WORLD_HEIGHT < minZ)
  210. {
  211. minZ = i % WORLD_HEIGHT;
  212. }
  213. }
  214. }
  215. if (minZ != oldMinZ || maxZ != oldMaxZ)
  216. {
  217. minMaxChanged = 1;
  218. }
  219. }
  220. blockIndices[index] = -1;
  221. }
  222. if (zBlock
  223. && !zBlock->zBlockType()->getModelInfo().getModelName().isEqual("cube"))
  224. {
  225. auto iterator = customBlocks.begin();
  226. bool found = 0;
  227. while (iterator)
  228. {
  229. if (iterator->getLocation() == zBlock->getLocation())
  230. {
  231. iterator.set(dynamic_cast<Block*>(zBlock->getThis()));
  232. found = 1;
  233. break;
  234. }
  235. ++iterator;
  236. }
  237. if (!found)
  238. {
  239. customBlocks.add(dynamic_cast<Block*>(zBlock->getThis()));
  240. }
  241. customBlocksChanged = 1;
  242. }
  243. if (!zBlock
  244. || zBlock->zBlockType()->getModelInfo().getModelName().isEqual("cube"))
  245. {
  246. auto iterator = customBlocks.begin();
  247. while (iterator)
  248. {
  249. if (Chunk::index(
  250. Dimension::chunkCoordinates(iterator->getLocation()))
  251. == index)
  252. {
  253. iterator.remove();
  254. customBlocksChanged = 1;
  255. break;
  256. }
  257. ++iterator;
  258. }
  259. }
  260. changed = 1;
  261. cs.unlock();
  262. }
  263. struct ModelIdMapping
  264. {
  265. int modelId;
  266. int vertexBufferOffset;
  267. int indexBufferOffset;
  268. };
  269. void DX12ChunkData::updateBuffers(
  270. ID3D12Device5* zDevice, Framework::DX12CommandQueue* zQueue)
  271. {
  272. cs.lock();
  273. if (!aabbs)
  274. {
  275. aabbs = new DX12Buffer(sizeof(D3D12_RAYTRACING_AABB),
  276. zDevice,
  277. dynamic_cast<Framework::DX12CommandQueue*>(zQueue->getThis()),
  278. D3D12_RESOURCE_FLAG_NONE);
  279. aabbs->setLength(sizeof(D3D12_RAYTRACING_AABB));
  280. D3D12_RAYTRACING_AABB data = {chunkCenter.x - 8.f,
  281. chunkCenter.y - 8.f,
  282. (float)minZ,
  283. chunkCenter.x + 8.f,
  284. chunkCenter.y + 8.f,
  285. (float)maxZ};
  286. aabbs->setData(&data, 1);
  287. aabbs->copyToGPU();
  288. minMaxChanged = 0;
  289. }
  290. if (minMaxChanged)
  291. {
  292. D3D12_RAYTRACING_AABB data = {chunkCenter.x - 8.f,
  293. chunkCenter.y - 8.f,
  294. (float)minZ,
  295. chunkCenter.x + 8.f,
  296. chunkCenter.y + 8.f,
  297. (float)maxZ};
  298. aabbs->setData(&data, 1);
  299. aabbs->copyToGPU();
  300. minMaxChanged = 0;
  301. }
  302. if (!blasBuffer || minMaxChanged)
  303. {
  304. D3D12_RAYTRACING_GEOMETRY_DESC geometryDesc = {};
  305. geometryDesc.Type
  306. = D3D12_RAYTRACING_GEOMETRY_TYPE_PROCEDURAL_PRIMITIVE_AABBS;
  307. geometryDesc.Flags = D3D12_RAYTRACING_GEOMETRY_FLAG_NONE;
  308. geometryDesc.AABBs.AABBCount = 1;
  309. geometryDesc.AABBs.AABBs.StartAddress
  310. = aabbs->zBuffer()->GetGPUVirtualAddress();
  311. geometryDesc.AABBs.AABBs.StrideInBytes = sizeof(D3D12_RAYTRACING_AABB);
  312. D3D12_BUILD_RAYTRACING_ACCELERATION_STRUCTURE_INPUTS prebuildDesc;
  313. prebuildDesc.Type
  314. = D3D12_RAYTRACING_ACCELERATION_STRUCTURE_TYPE_BOTTOM_LEVEL;
  315. prebuildDesc.DescsLayout = D3D12_ELEMENTS_LAYOUT_ARRAY;
  316. prebuildDesc.NumDescs = 1;
  317. prebuildDesc.pGeometryDescs = &geometryDesc;
  318. prebuildDesc.Flags
  319. = D3D12_RAYTRACING_ACCELERATION_STRUCTURE_BUILD_FLAG_ALLOW_UPDATE;
  320. D3D12_RAYTRACING_ACCELERATION_STRUCTURE_PREBUILD_INFO info = {};
  321. zDevice->GetRaytracingAccelerationStructurePrebuildInfo(
  322. &prebuildDesc, &info);
  323. if (oldBlasBuffer)
  324. {
  325. oldBlasBuffer->Release();
  326. oldBlasBuffer = 0;
  327. }
  328. if (blasBuffer)
  329. {
  330. oldBlasBuffer = blasBuffer->zBuffer();
  331. oldBlasBuffer->AddRef();
  332. blasBuffer->release();
  333. }
  334. blasBuffer = new DX12Buffer(1,
  335. zDevice,
  336. dynamic_cast<DX12CommandQueue*>(zQueue->getThis()),
  337. D3D12_RESOURCE_FLAG_ALLOW_UNORDERED_ACCESS);
  338. blasBuffer->setLength(
  339. ROUND_UP_POWER_OF_2(info.ResultDataMaxSizeInBytes, 256));
  340. blasBuffer->createBufferWithoutData(
  341. D3D12_RESOURCE_STATE_RAYTRACING_ACCELERATION_STRUCTURE);
  342. blasScratchBuffer = new DX12Buffer(1,
  343. zDevice,
  344. dynamic_cast<DX12CommandQueue*>(zQueue->getThis()),
  345. D3D12_RESOURCE_FLAG_ALLOW_UNORDERED_ACCESS);
  346. blasScratchBuffer->setLength(
  347. ROUND_UP_POWER_OF_2(info.ScratchDataSizeInBytes, 256));
  348. blasScratchBuffer->createBufferWithoutData(D3D12_RESOURCE_STATE_COMMON);
  349. D3D12_BUILD_RAYTRACING_ACCELERATION_STRUCTURE_DESC buildDesc;
  350. buildDesc.Inputs.Type
  351. = D3D12_RAYTRACING_ACCELERATION_STRUCTURE_TYPE_BOTTOM_LEVEL;
  352. buildDesc.Inputs.DescsLayout = D3D12_ELEMENTS_LAYOUT_ARRAY;
  353. buildDesc.Inputs.NumDescs = 1;
  354. buildDesc.Inputs.pGeometryDescs = &geometryDesc;
  355. buildDesc.DestAccelerationStructureData
  356. = {blasBuffer->zBuffer()->GetGPUVirtualAddress()};
  357. buildDesc.ScratchAccelerationStructureData
  358. = {blasScratchBuffer->zBuffer()->GetGPUVirtualAddress()};
  359. buildDesc.SourceAccelerationStructureData
  360. = oldBlasBuffer ? oldBlasBuffer->GetGPUVirtualAddress() : 0;
  361. buildDesc.Inputs.Flags
  362. = oldBlasBuffer
  363. ? D3D12_RAYTRACING_ACCELERATION_STRUCTURE_BUILD_FLAG_PERFORM_UPDATE
  364. : D3D12_RAYTRACING_ACCELERATION_STRUCTURE_BUILD_FLAG_ALLOW_UPDATE;
  365. zQueue->zCommandList()->BuildRaytracingAccelerationStructure(
  366. &buildDesc, 0, nullptr);
  367. bufferChanged = 1;
  368. }
  369. if (!blockIndexBuffer)
  370. {
  371. blockIndexBuffer = new DX12Buffer(sizeof(int),
  372. zDevice,
  373. dynamic_cast<DX12CommandQueue*>(zQueue->getThis()),
  374. D3D12_RESOURCE_FLAG_NONE);
  375. blockIndexBuffer->setLength(
  376. sizeof(int) * CHUNK_SIZE * CHUNK_SIZE * WORLD_HEIGHT);
  377. bufferChanged = 1;
  378. }
  379. if (textureIdBuffer
  380. && textureIdBuffer->getElementCount() < textureIds.getEntryCount())
  381. {
  382. textureIdBuffer->release();
  383. textureIdBuffer = 0;
  384. }
  385. if (!textureIdBuffer)
  386. {
  387. textureIdBuffer = new DX12Buffer(sizeof(int),
  388. zDevice,
  389. dynamic_cast<DX12CommandQueue*>(zQueue->getThis()),
  390. D3D12_RESOURCE_FLAG_NONE);
  391. textureIdBuffer->setLength(textureIds.getEntryCount() * sizeof(int));
  392. bufferChanged = 1;
  393. }
  394. if (!chunkInfoBuffer)
  395. {
  396. chunkInfoBuffer = new DX12Buffer(sizeof(ChunkShaderInfo),
  397. zDevice,
  398. dynamic_cast<DX12CommandQueue*>(zQueue->getThis()),
  399. D3D12_RESOURCE_FLAG_NONE);
  400. chunkInfoBuffer->setLength(sizeof(ChunkShaderInfo));
  401. }
  402. if (customBlocksChanged)
  403. {
  404. int* cbts = new int[customBlocks.getEntryCount()];
  405. int index = 0;
  406. D3D12_RAYTRACING_GEOMETRY_DESC* simpleBlockDescs
  407. = new D3D12_RAYTRACING_GEOMETRY_DESC[customBlocks.getEntryCount()];
  408. int* indexOffsets = new int[customBlocks.getEntryCount()];
  409. int vertexCount = 0;
  410. int indexCount = 0;
  411. Array<ModelIdMapping> idMapping;
  412. for (Block* block : customBlocks)
  413. {
  414. int id = block->zModelData()->getId();
  415. bool found = 0;
  416. for (const ModelIdMapping& m : idMapping)
  417. {
  418. if (m.modelId == id)
  419. {
  420. found = 1;
  421. break;
  422. }
  423. }
  424. if (!found)
  425. {
  426. ModelIdMapping mapping;
  427. mapping.modelId = id;
  428. mapping.vertexBufferOffset = vertexCount;
  429. mapping.indexBufferOffset = indexCount;
  430. idMapping.add(mapping);
  431. vertexCount += block->zModelData()->getVertexCount();
  432. indexCount += block->zModelData()->getIndexCount();
  433. }
  434. }
  435. int* combinedIndexBuffer = new int[indexCount];
  436. Framework::DX2VertexData* vertexDataBuffer
  437. = new Framework::DX2VertexData[vertexCount];
  438. int vdIndex = 0;
  439. Array<int> modelBuild;
  440. float* matrixDataBuffer = new float[customBlocks.getEntryCount() * 12];
  441. int blockIndex = 0;
  442. for (Block* block : customBlocks)
  443. {
  444. Mat4<float> transform
  445. = Mat4<float>::translation(
  446. (Vec3<float>)Dimension::chunkCoordinates(block->getLocation())
  447. + Vec3<float>(
  448. 0.5f - CHUNK_SIZE / 2, 0.5f - CHUNK_SIZE / 2, 0.5f))
  449. * Mat4<float>::rotationZ(block->getZRotation())
  450. * Mat4<float>::rotationX(block->getXRotation())
  451. * Mat4<float>::rotationY(block->getYRotation())
  452. * Mat4<float>::scaling(block->getSize());
  453. memcpy(matrixDataBuffer + 12 * blockIndex,
  454. &transform,
  455. 12 * sizeof(float));
  456. ++blockIndex;
  457. }
  458. if (!customBlockMatrixBuffer)
  459. {
  460. customBlockMatrixBuffer = new DX12Buffer(sizeof(float) * 12,
  461. zDevice,
  462. dynamic_cast<DX12CommandQueue*>(zQueue->getThis()),
  463. D3D12_RESOURCE_FLAG_NONE);
  464. }
  465. customBlockMatrixBuffer->setLength(
  466. sizeof(float) * 12 * customBlocks.getEntryCount());
  467. customBlockMatrixBuffer->setData(matrixDataBuffer, 1);
  468. customBlockMatrixBuffer->copyToGPU();
  469. CD3DX12_RESOURCE_BARRIER transition
  470. = CD3DX12_RESOURCE_BARRIER::Transition(
  471. customBlockMatrixBuffer->zBuffer(),
  472. D3D12_RESOURCE_STATE_COMMON,
  473. D3D12_RESOURCE_STATE_NON_PIXEL_SHADER_RESOURCE);
  474. zQueue->zCommandList()->ResourceBarrier(1, &transition);
  475. for (Block* block : customBlocks)
  476. {
  477. int id = block->zModelData()->getId();
  478. ModelIdMapping mapping;
  479. int mappingIndex = 0;
  480. for (const ModelIdMapping& m : idMapping)
  481. {
  482. if (m.modelId == id)
  483. {
  484. mapping = m;
  485. break;
  486. }
  487. ++mappingIndex;
  488. }
  489. if (modelBuild.getValueIndex(id) < 0)
  490. {
  491. modelBuild.add(id);
  492. while (vertexBuffers.getEntryCount() <= mappingIndex)
  493. {
  494. DX12Buffer* vBuffer = new DX12Buffer(sizeof(Vec3<float>),
  495. zDevice,
  496. dynamic_cast<DX12CommandQueue*>(zQueue->getThis()),
  497. D3D12_RESOURCE_FLAG_NONE);
  498. vertexBuffers.add(vBuffer);
  499. }
  500. while (indexBuffers.getEntryCount() <= mappingIndex)
  501. {
  502. DX12Buffer* iBuffer = new DX12Buffer(sizeof(int),
  503. zDevice,
  504. dynamic_cast<DX12CommandQueue*>(zQueue->getThis()),
  505. D3D12_RESOURCE_FLAG_NONE);
  506. indexBuffers.add(iBuffer);
  507. }
  508. DX12Buffer* vBuffer = vertexBuffers.z(mappingIndex);
  509. Vec3<float>* vertexBuffer
  510. = new Vec3<float>[block->zModelData()->getVertexCount()];
  511. const Vertex3D* rawVertexData
  512. = block->zModelData()->zVertexBuffer();
  513. for (int i = 0; i < block->zModelData()->getVertexCount(); ++i)
  514. {
  515. vertexBuffer[i] = rawVertexData[i].pos;
  516. vertexDataBuffer[mapping.vertexBufferOffset + i]
  517. = {rawVertexData[i].tPos, rawVertexData[i].normal};
  518. }
  519. const int* rawIndexBuffer
  520. = block->zModelData()->getIndexBuffer();
  521. for (int i = 0; i < block->zModelData()->getIndexCount(); i++)
  522. {
  523. combinedIndexBuffer[mapping.indexBufferOffset + i]
  524. = rawIndexBuffer[i] + mapping.vertexBufferOffset;
  525. }
  526. vBuffer->setLength(sizeof(Vec3<float>)
  527. * block->zModelData()->getVertexCount());
  528. vBuffer->setData(vertexBuffer, 1);
  529. vBuffer->copyToGPU();
  530. delete[] vertexBuffer;
  531. DX12Buffer* iBuffer = indexBuffers.z(mappingIndex);
  532. iBuffer->setLength(
  533. sizeof(int) * block->zModelData()->getIndexCount());
  534. iBuffer->setData((void*)rawIndexBuffer, 1);
  535. iBuffer->copyToGPU();
  536. }
  537. DX12Buffer* vBuffer = vertexBuffers.z(mappingIndex);
  538. DX12Buffer* iBuffer = indexBuffers.z(mappingIndex);
  539. simpleBlockDescs[index].Type
  540. = D3D12_RAYTRACING_GEOMETRY_TYPE_TRIANGLES;
  541. simpleBlockDescs[index].Flags = D3D12_RAYTRACING_GEOMETRY_FLAG_NONE;
  542. simpleBlockDescs[index].Triangles.VertexBuffer.StartAddress
  543. = vBuffer->zBuffer()->GetGPUVirtualAddress();
  544. simpleBlockDescs[index].Triangles.VertexBuffer.StrideInBytes
  545. = vBuffer->getElementLength();
  546. simpleBlockDescs[index].Triangles.VertexFormat
  547. = DXGI_FORMAT_R32G32B32_FLOAT;
  548. simpleBlockDescs[index].Triangles.VertexCount
  549. = vBuffer->getElementCount();
  550. simpleBlockDescs[index].Triangles.IndexBuffer
  551. = iBuffer->zBuffer()->GetGPUVirtualAddress();
  552. simpleBlockDescs[index].Triangles.IndexFormat
  553. = DXGI_FORMAT_R32_UINT;
  554. simpleBlockDescs[index].Triangles.IndexCount
  555. = iBuffer->getElementCount();
  556. simpleBlockDescs[index].Triangles.Transform3x4
  557. = customBlockMatrixBuffer->zBuffer()->GetGPUVirtualAddress()
  558. + 12 * index * sizeof(float);
  559. cbts[index] = block->zTexture()->zPolygonTexture(0)->getId();
  560. indexOffsets[index] = mapping.indexBufferOffset;
  561. ++index;
  562. }
  563. D3D12_BUILD_RAYTRACING_ACCELERATION_STRUCTURE_INPUTS prebuildDesc;
  564. prebuildDesc.Type
  565. = D3D12_RAYTRACING_ACCELERATION_STRUCTURE_TYPE_BOTTOM_LEVEL;
  566. prebuildDesc.DescsLayout = D3D12_ELEMENTS_LAYOUT_ARRAY;
  567. prebuildDesc.NumDescs = customBlocks.getEntryCount();
  568. prebuildDesc.pGeometryDescs = simpleBlockDescs;
  569. prebuildDesc.Flags
  570. = D3D12_RAYTRACING_ACCELERATION_STRUCTURE_BUILD_FLAG_NONE;
  571. D3D12_RAYTRACING_ACCELERATION_STRUCTURE_PREBUILD_INFO info = {};
  572. zDevice->GetRaytracingAccelerationStructurePrebuildInfo(
  573. &prebuildDesc, &info);
  574. if (!customBlocksBlasScratchBuffer)
  575. {
  576. customBlocksBlasScratchBuffer = new DX12Buffer(1,
  577. zDevice,
  578. dynamic_cast<DX12CommandQueue*>(zQueue->getThis()),
  579. D3D12_RESOURCE_FLAG_ALLOW_UNORDERED_ACCESS);
  580. }
  581. if (!customBlocksBlasBuffer)
  582. {
  583. customBlocksBlasBuffer = new DX12Buffer(1,
  584. zDevice,
  585. dynamic_cast<DX12CommandQueue*>(zQueue->getThis()),
  586. D3D12_RESOURCE_FLAG_ALLOW_UNORDERED_ACCESS);
  587. }
  588. customBlocksBlasScratchBuffer->setLength(
  589. ROUND_UP_POWER_OF_2((int)info.ScratchDataSizeInBytes, 256));
  590. customBlocksBlasScratchBuffer->createBufferWithoutData(
  591. D3D12_RESOURCE_STATE_COMMON);
  592. customBlocksBlasBuffer->setLength(
  593. ROUND_UP_POWER_OF_2((int)info.ResultDataMaxSizeInBytes, 256));
  594. customBlocksBlasBuffer->createBufferWithoutData(
  595. D3D12_RESOURCE_STATE_RAYTRACING_ACCELERATION_STRUCTURE);
  596. D3D12_BUILD_RAYTRACING_ACCELERATION_STRUCTURE_DESC buildDesc;
  597. buildDesc.Inputs.Type
  598. = D3D12_RAYTRACING_ACCELERATION_STRUCTURE_TYPE_BOTTOM_LEVEL;
  599. buildDesc.Inputs.DescsLayout = D3D12_ELEMENTS_LAYOUT_ARRAY;
  600. buildDesc.Inputs.NumDescs = customBlocks.getEntryCount();
  601. buildDesc.Inputs.pGeometryDescs = simpleBlockDescs;
  602. buildDesc.DestAccelerationStructureData
  603. = {customBlocksBlasBuffer->zBuffer()->GetGPUVirtualAddress()};
  604. buildDesc.ScratchAccelerationStructureData = {
  605. customBlocksBlasScratchBuffer->zBuffer()->GetGPUVirtualAddress()};
  606. buildDesc.SourceAccelerationStructureData = 0;
  607. buildDesc.Inputs.Flags
  608. = D3D12_RAYTRACING_ACCELERATION_STRUCTURE_BUILD_FLAG_NONE;
  609. zQueue->zCommandList()->BuildRaytracingAccelerationStructure(
  610. &buildDesc, 0, nullptr);
  611. if (!customBlockTextures)
  612. {
  613. customBlockTextures = new DX12Buffer(sizeof(int),
  614. zDevice,
  615. dynamic_cast<DX12CommandQueue*>(zQueue->getThis()),
  616. D3D12_RESOURCE_FLAG_NONE);
  617. }
  618. customBlockTextures->setLength(
  619. sizeof(int) * customBlocks.getEntryCount());
  620. customBlockTextures->setData(cbts, 1);
  621. customBlockTextures->copyToGPU();
  622. if (!customBlocksIndexOffsetBuffer)
  623. {
  624. customBlocksIndexOffsetBuffer = new DX12Buffer(sizeof(int),
  625. zDevice,
  626. dynamic_cast<DX12CommandQueue*>(zQueue->getThis()),
  627. D3D12_RESOURCE_FLAG_NONE);
  628. }
  629. customBlocksIndexOffsetBuffer->setLength(
  630. sizeof(int) * customBlocks.getEntryCount());
  631. customBlocksIndexOffsetBuffer->setData(indexOffsets, 1);
  632. customBlocksIndexOffsetBuffer->copyToGPU();
  633. if (!customBlocksVertexDataBuffer)
  634. {
  635. customBlocksVertexDataBuffer
  636. = new DX12Buffer(sizeof(Framework::DX2VertexData),
  637. zDevice,
  638. dynamic_cast<DX12CommandQueue*>(zQueue->getThis()),
  639. D3D12_RESOURCE_FLAG_NONE);
  640. }
  641. customBlocksVertexDataBuffer->setLength(
  642. sizeof(Framework::DX2VertexData) * vertexCount);
  643. customBlocksVertexDataBuffer->setData(vertexDataBuffer, 1);
  644. customBlocksVertexDataBuffer->copyToGPU();
  645. if (!customBlocksCombinedIndexBuffer)
  646. {
  647. customBlocksCombinedIndexBuffer = new DX12Buffer(sizeof(int),
  648. zDevice,
  649. dynamic_cast<DX12CommandQueue*>(zQueue->getThis()),
  650. D3D12_RESOURCE_FLAG_NONE);
  651. }
  652. customBlocksCombinedIndexBuffer->setLength(sizeof(int) * indexCount);
  653. customBlocksCombinedIndexBuffer->setData(combinedIndexBuffer, 1);
  654. customBlocksCombinedIndexBuffer->copyToGPU();
  655. delete[] cbts;
  656. delete[] simpleBlockDescs;
  657. delete[] vertexDataBuffer;
  658. delete[] indexOffsets;
  659. delete[] combinedIndexBuffer;
  660. customBlocksChanged = 0;
  661. customBufferChanged = 1;
  662. }
  663. if (changed)
  664. {
  665. for (int i = 0; i < CHUNK_SIZE * CHUNK_SIZE * WORLD_HEIGHT; ++i)
  666. {
  667. assert(blockIndices[i] < 0
  668. || blockIndices[i] <= textureIds.getEntryCount() - 6);
  669. }
  670. int* textureIdData = new int[textureIds.getEntryCount()];
  671. ArrayIterator<int> textureIt = textureIds.begin();
  672. int index = 0;
  673. while (textureIt)
  674. {
  675. textureIdData[index++] = textureIt.val();
  676. ++textureIt;
  677. }
  678. blockIndexBuffer->setData(blockIndices, 1);
  679. textureIdBuffer->setData(textureIdData, 1);
  680. blockIndexBuffer->copyToGPU();
  681. textureIdBuffer->copyToGPU(sizeof(int) * textureIds.getEntryCount());
  682. delete[] textureIdData;
  683. ChunkShaderInfo info
  684. = {chunkCenter, (unsigned int)textureIds.getEntryCount()};
  685. chunkInfoBuffer->setData(&info, 1);
  686. chunkInfoBuffer->copyToGPU();
  687. changed = 0;
  688. }
  689. cs.unlock();
  690. }
  691. int DX12ChunkData::getLastObjectIndex() const
  692. {
  693. return lastObjectIndex;
  694. }
  695. int DX12ChunkData::getLastCustomObjectIndex() const
  696. {
  697. return lastCustomObjectIndex;
  698. }
  699. int DX12ChunkData::getLastHitGroupIndex() const
  700. {
  701. return lastHitGroupIndex;
  702. }
  703. int DX12ChunkData::getLastCustomHitGroupIndex() const
  704. {
  705. return lastCustomHitGroupIndex;
  706. }
  707. void DX12ChunkData::setLastObjectIndex(int index)
  708. {
  709. lastObjectIndex = index;
  710. }
  711. void DX12ChunkData::setLastCustomObjectIndex(int index)
  712. {
  713. lastCustomObjectIndex = index;
  714. }
  715. void DX12ChunkData::setLastHitGroupIndex(int index)
  716. {
  717. lastHitGroupIndex = index;
  718. }
  719. void DX12ChunkData::setLastCustomHitGroupIndex(int index)
  720. {
  721. lastCustomHitGroupIndex = index;
  722. }
  723. bool DX12ChunkData::wasBufferChanged() const
  724. {
  725. return bufferChanged;
  726. }
  727. void DX12ChunkData::setBufferChanged(bool changed)
  728. {
  729. bufferChanged = changed;
  730. }
  731. bool DX12ChunkData::isSimpleBlock(Block* zBlock) const
  732. {
  733. return zBlock->zBlockType()->getModelInfo().getModelName().isEqual("cube")
  734. || ((!zBlock->zModelData()->zSkeleton()
  735. || zBlock->zModelData()->zSkeleton()->getNextBoneId() <= 1)
  736. && zBlock->zModelData()->getPolygonCount() == 1);
  737. }
  738. bool DX12ChunkData::hasCustomBlocks() const
  739. {
  740. return customBlocks.getEntryCount() > 0;
  741. }
  742. bool DX12ChunkData::wasCustomBufferChanged() const
  743. {
  744. return customBufferChanged;
  745. }
  746. void DX12ChunkData::setCustomBufferChanged(bool changed)
  747. {
  748. customBufferChanged = changed;
  749. }