DX12ChunkData.cpp 32 KB

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