DX12Shader.cpp 60 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753
  1. #include "DX12Shader.h"
  2. #include "DX12CommandQueue.h"
  3. #include "DX12Texture.h"
  4. #include "DX12TLAS.h"
  5. #include "Logging.h"
  6. using namespace Framework;
  7. Framework::DX12ShaderSignature::DX12ShaderSignature()
  8. : ReferenceCounter(),
  9. signature(0),
  10. changed(1),
  11. shaderBindingTableParamCount(0)
  12. {}
  13. Framework::DX12ShaderSignature::~DX12ShaderSignature()
  14. {
  15. if (signature)
  16. {
  17. signature->Release();
  18. }
  19. for (DX12ShaderRegisterUsage* usage : descriptorHeapBindings)
  20. {
  21. delete usage;
  22. }
  23. for (DX12ShaderRegisterUsage* usage : bindingTableBindings)
  24. {
  25. delete usage;
  26. }
  27. }
  28. int* Framework::DX12ShaderSignature::addRegisterUsageLinkedToShaderBindingTable(
  29. DX12ShaderRegister registerType, int registerIndex, int spaceIndex)
  30. {
  31. for (DX12ShaderRegisterUsage* usage : descriptorHeapBindings)
  32. {
  33. if (usage->registerType == registerType
  34. && usage->registerIndex == registerIndex
  35. && usage->spaceIndex == spaceIndex)
  36. {
  37. Logging::error()
  38. << "Duplicate register usage in root signature: "
  39. << registerType << " " << registerIndex << " " << spaceIndex;
  40. throw std::invalid_argument(
  41. "Duplicate register usage in root signature");
  42. }
  43. }
  44. for (DX12ShaderRegisterUsage* usage : bindingTableBindings)
  45. {
  46. if (usage->registerType == registerType
  47. && usage->registerIndex == registerIndex
  48. && usage->spaceIndex == spaceIndex)
  49. {
  50. Logging::error()
  51. << "Duplicate register usage in root signature: "
  52. << registerType << " " << registerIndex << " " << spaceIndex;
  53. throw std::invalid_argument(
  54. "Duplicate register usage in root signature");
  55. }
  56. }
  57. DX12ShaderRegisterUsage* usage = new DX12ShaderRegisterUsage{
  58. registerType, registerIndex, spaceIndex, -1, 0};
  59. bindingTableBindings.add(usage);
  60. changed = 1;
  61. return &usage->bindingTableIndex;
  62. }
  63. void Framework::DX12ShaderSignature::addRegisterUsageLinkedToDescriptorHeap(
  64. int descriptorHeapIndex,
  65. DX12ShaderRegister registerType,
  66. int registerIndex,
  67. int spaceIndex)
  68. {
  69. if (descriptorHeapIndex < 0)
  70. {
  71. Logging::error() << "descriptorHeapIndex can not be below 0";
  72. throw std::invalid_argument("descriptorHeapIndex can not be below 0");
  73. }
  74. for (DX12ShaderRegisterUsage* usage : descriptorHeapBindings)
  75. {
  76. if (usage->registerType == registerType
  77. && usage->registerIndex == registerIndex
  78. && usage->spaceIndex == spaceIndex)
  79. {
  80. Logging::error()
  81. << "Duplicate register usage in root signature: "
  82. << registerType << " " << registerIndex << " " << spaceIndex;
  83. throw std::invalid_argument(
  84. "Duplicate register usage in root signature");
  85. }
  86. }
  87. // descriptor heap bindings sould be sorted by registerType -> spaceIndex ->
  88. // registerIndex
  89. ArrayIterator<DX12ShaderRegisterUsage*> it = descriptorHeapBindings.begin();
  90. bool found = 0;
  91. while (it)
  92. {
  93. if (it->registerType > registerType)
  94. {
  95. found = 1;
  96. break;
  97. }
  98. if (it->registerType == registerType)
  99. {
  100. if (it->spaceIndex > spaceIndex)
  101. {
  102. found = 1;
  103. break;
  104. }
  105. if (it->spaceIndex == spaceIndex)
  106. {
  107. if (it->registerIndex >= registerIndex)
  108. {
  109. found = 1;
  110. break;
  111. }
  112. }
  113. }
  114. ++it;
  115. }
  116. if (found)
  117. {
  118. if (it->registerIndex == registerIndex && it->spaceIndex == spaceIndex
  119. && it->registerType == registerType)
  120. {
  121. Logging::error()
  122. << "Duplicate register usage in root signature: "
  123. << registerType << " " << registerIndex << " " << spaceIndex;
  124. throw std::invalid_argument(
  125. "Duplicate register usage in root signature");
  126. }
  127. it.addBefore(new DX12ShaderRegisterUsage{
  128. registerType, registerIndex, spaceIndex, descriptorHeapIndex, 0});
  129. }
  130. else
  131. {
  132. descriptorHeapBindings.add(new DX12ShaderRegisterUsage{
  133. registerType, registerIndex, spaceIndex, descriptorHeapIndex, 0});
  134. }
  135. changed = 1;
  136. }
  137. void Framework::DX12ShaderSignature::createSignature(ID3D12Device5* zDevice,
  138. PFN_D3D12_SERIALIZE_ROOT_SIGNATURE pfnD3D12SerializeRootSignature)
  139. {
  140. if (!changed)
  141. {
  142. return;
  143. }
  144. changed = 0;
  145. if (signature)
  146. {
  147. signature->Release();
  148. signature = 0;
  149. }
  150. int paramCount = (descriptorHeapBindings.getEntryCount() > 0 ? 1 : 0)
  151. + bindingTableBindings.getEntryCount();
  152. D3D12_ROOT_PARAMETER* descriptorTable
  153. = new D3D12_ROOT_PARAMETER[paramCount];
  154. int index = 0;
  155. D3D12_DESCRIPTOR_RANGE* descriptorRanges = 0;
  156. if (descriptorHeapBindings.getEntryCount())
  157. {
  158. descriptorTable[0].ParameterType
  159. = D3D12_ROOT_PARAMETER_TYPE_DESCRIPTOR_TABLE;
  160. descriptorTable[0].ShaderVisibility = D3D12_SHADER_VISIBILITY_ALL;
  161. descriptorRanges = new D3D12_DESCRIPTOR_RANGE[descriptorHeapBindings
  162. .getEntryCount()];
  163. ArrayIterator<DX12ShaderRegisterUsage*> it
  164. = descriptorHeapBindings.begin();
  165. while (it)
  166. {
  167. it->bindingTableIndex = 0;
  168. D3D12_DESCRIPTOR_RANGE& range = descriptorRanges[index];
  169. switch (it->registerType)
  170. {
  171. case DX12_SHADER_REGISTER_B_CONST_BUFFER:
  172. range.RangeType = D3D12_DESCRIPTOR_RANGE_TYPE_CBV;
  173. break;
  174. case DX12_SHADER_REGISTER_T_SHADER_RESOURCE:
  175. range.RangeType = D3D12_DESCRIPTOR_RANGE_TYPE_SRV;
  176. break;
  177. case DX12_SHADER_REGISTER_U_UNORDERED_ACCESS:
  178. range.RangeType = D3D12_DESCRIPTOR_RANGE_TYPE_UAV;
  179. break;
  180. }
  181. range.BaseShaderRegister = it->registerIndex;
  182. range.OffsetInDescriptorsFromTableStart = it->descriptorHeapIndex;
  183. range.RegisterSpace = it->spaceIndex;
  184. ArrayIterator<DX12ShaderRegisterUsage*> next = it.next();
  185. int size = 1;
  186. while (
  187. next && next->registerType == it->registerType
  188. && next->spaceIndex == it->spaceIndex
  189. && next->registerIndex == it->registerIndex + size
  190. && next->descriptorHeapIndex == it->descriptorHeapIndex + size)
  191. {
  192. ++size;
  193. it = next;
  194. it->bindingTableIndex = 0;
  195. ++next;
  196. }
  197. range.NumDescriptors = size;
  198. ++it;
  199. ++index;
  200. }
  201. descriptorTable[0].DescriptorTable.pDescriptorRanges = descriptorRanges;
  202. descriptorTable[0].DescriptorTable.NumDescriptorRanges = index;
  203. index = 1;
  204. }
  205. for (DX12ShaderRegisterUsage* usage : bindingTableBindings)
  206. {
  207. usage->bindingTableIndex = index;
  208. switch (usage->registerType)
  209. {
  210. case DX12_SHADER_REGISTER_B_CONST_BUFFER:
  211. descriptorTable[index].ParameterType
  212. = D3D12_ROOT_PARAMETER_TYPE_CBV;
  213. break;
  214. case DX12_SHADER_REGISTER_T_SHADER_RESOURCE:
  215. descriptorTable[index].ParameterType
  216. = D3D12_ROOT_PARAMETER_TYPE_SRV;
  217. break;
  218. case DX12_SHADER_REGISTER_U_UNORDERED_ACCESS:
  219. descriptorTable[index].ParameterType
  220. = D3D12_ROOT_PARAMETER_TYPE_UAV;
  221. break;
  222. }
  223. descriptorTable[index].ShaderVisibility = D3D12_SHADER_VISIBILITY_ALL;
  224. descriptorTable[index].Descriptor.ShaderRegister = usage->registerIndex;
  225. descriptorTable[index].Descriptor.RegisterSpace = usage->spaceIndex;
  226. ++index;
  227. }
  228. shaderBindingTableParamCount = index;
  229. D3D12_ROOT_SIGNATURE_DESC rootDesc = {};
  230. rootDesc.NumParameters = shaderBindingTableParamCount;
  231. rootDesc.pParameters = descriptorTable;
  232. rootDesc.Flags = D3D12_ROOT_SIGNATURE_FLAG_LOCAL_ROOT_SIGNATURE;
  233. ID3DBlob* pSigBlob = 0;
  234. ID3DBlob* pErrorBlob = 0;
  235. HRESULT hr = pfnD3D12SerializeRootSignature(
  236. &rootDesc, D3D_ROOT_SIGNATURE_VERSION_1_0, &pSigBlob, &pErrorBlob);
  237. if (pSigBlob)
  238. {
  239. zDevice->CreateRootSignature(0,
  240. pSigBlob->GetBufferPointer(),
  241. pSigBlob->GetBufferSize(),
  242. __uuidof(ID3D12RootSignature),
  243. (void**)&signature);
  244. pSigBlob->Release();
  245. }
  246. if (pErrorBlob)
  247. {
  248. std::string errorMessage(
  249. static_cast<const char*>(pErrorBlob->GetBufferPointer()),
  250. pErrorBlob->GetBufferSize());
  251. Logging::error() << "Failed to serialize root signature: "
  252. << errorMessage;
  253. pErrorBlob->Release();
  254. }
  255. delete[] descriptorRanges;
  256. delete[] descriptorTable;
  257. }
  258. ID3D12RootSignature* Framework::DX12ShaderSignature::zSignature() const
  259. {
  260. return signature;
  261. }
  262. const Array<DX12ShaderRegisterUsage*>&
  263. Framework::DX12ShaderSignature::getDescriptorHeapBindings() const
  264. {
  265. return descriptorHeapBindings;
  266. }
  267. int Framework::DX12ShaderSignature::gerShaderBindingTableParamCount() const
  268. {
  269. return shaderBindingTableParamCount;
  270. }
  271. Framework::DX12ShaderFunction::DX12ShaderFunction(const Text& functionName,
  272. DX12ShaderSignature* signature,
  273. DX12ShaderFunctionType functionType)
  274. : ReferenceCounter(),
  275. functionName(functionName),
  276. signature(signature),
  277. functionType(functionType),
  278. exportDesc(new D3D12_EXPORT_DESC())
  279. {
  280. wchar_t* wc = new wchar_t[functionName.getLength() + 1];
  281. mbstowcs_s(0,
  282. wc,
  283. functionName.getLength() + 1,
  284. functionName.getText(),
  285. functionName.getLength() + 1);
  286. wc[functionName.getLength()] = 0;
  287. exportDesc->Name = wc;
  288. exportDesc->ExportToRename = 0;
  289. exportDesc->Flags = D3D12_EXPORT_FLAG_NONE;
  290. }
  291. Framework::DX12ShaderFunction::~DX12ShaderFunction()
  292. {
  293. signature->release();
  294. delete[] exportDesc->Name;
  295. delete exportDesc;
  296. }
  297. const Text& Framework::DX12ShaderFunction::getFunctionName() const
  298. {
  299. return functionName;
  300. }
  301. DX12ShaderSignature* Framework::DX12ShaderFunction::zSignature() const
  302. {
  303. return signature;
  304. }
  305. D3D12_EXPORT_DESC* Framework::DX12ShaderFunction::zExportDesc() const
  306. {
  307. return exportDesc;
  308. }
  309. DX12ShaderFunctionType Framework::DX12ShaderFunction::getFunctionType() const
  310. {
  311. return functionType;
  312. }
  313. Framework::DX12Shader::DX12Shader(
  314. const unsigned char* shaderBytes, int shaderBytesSize)
  315. : ReferenceCounter(),
  316. shaderBytes(shaderBytes),
  317. shaderBytesSize(shaderBytesSize),
  318. libraryDesc(new D3D12_DXIL_LIBRARY_DESC())
  319. {
  320. libraryDesc->DXILLibrary.pShaderBytecode = shaderBytes;
  321. libraryDesc->DXILLibrary.BytecodeLength = shaderBytesSize;
  322. libraryDesc->NumExports = 0;
  323. libraryDesc->pExports = 0;
  324. }
  325. Framework::DX12Shader::~DX12Shader()
  326. {
  327. delete[] libraryDesc->pExports;
  328. delete libraryDesc;
  329. }
  330. void Framework::DX12Shader::addFunction(DX12ShaderFunction* function)
  331. {
  332. functions.add(function);
  333. }
  334. int Framework::DX12Shader::getShaderBytesSize() const
  335. {
  336. return shaderBytesSize;
  337. }
  338. const unsigned char* Framework::DX12Shader::getShaderBytes() const
  339. {
  340. return shaderBytes;
  341. }
  342. const RCArray<DX12ShaderFunction>& Framework::DX12Shader::getFunctions() const
  343. {
  344. return functions;
  345. }
  346. D3D12_DXIL_LIBRARY_DESC* Framework::DX12Shader::zLibraryDesc() const
  347. {
  348. if (libraryDesc->NumExports != functions.getEntryCount())
  349. {
  350. delete[] libraryDesc->pExports;
  351. libraryDesc->NumExports = functions.getEntryCount();
  352. D3D12_EXPORT_DESC* pExports
  353. = new D3D12_EXPORT_DESC[functions.getEntryCount()];
  354. int index = 0;
  355. for (const auto& function : functions)
  356. {
  357. memcpy(pExports + index,
  358. function->zExportDesc(),
  359. sizeof(D3D12_EXPORT_DESC));
  360. index++;
  361. }
  362. libraryDesc->pExports = pExports;
  363. }
  364. return libraryDesc;
  365. }
  366. Framework::DX12ShaderHitGroup::DX12ShaderHitGroup(const Text name)
  367. : ReferenceCounter(),
  368. name(name),
  369. closestHitShaderFunction(0),
  370. anyHitShaderFunction(0),
  371. intersectionShaderFunction(0),
  372. payloadSize(0),
  373. attributeSize(0),
  374. hitGroupDesc(new D3D12_HIT_GROUP_DESC())
  375. {
  376. wchar_t* wc = new wchar_t[name.getLength() + 1];
  377. mbstowcs_s(
  378. 0, wc, name.getLength() + 1, name.getText(), name.getLength() + 1);
  379. wc[name.getLength()] = 0;
  380. hitGroupDesc->HitGroupExport = wc;
  381. hitGroupDesc->IntersectionShaderImport = 0;
  382. hitGroupDesc->AnyHitShaderImport = 0;
  383. hitGroupDesc->ClosestHitShaderImport = 0;
  384. }
  385. Framework::DX12ShaderHitGroup::~DX12ShaderHitGroup()
  386. {
  387. delete[] hitGroupDesc->HitGroupExport;
  388. delete hitGroupDesc;
  389. if (closestHitShaderFunction)
  390. {
  391. closestHitShaderFunction->release();
  392. }
  393. if (anyHitShaderFunction)
  394. {
  395. anyHitShaderFunction->release();
  396. }
  397. if (intersectionShaderFunction)
  398. {
  399. intersectionShaderFunction->release();
  400. }
  401. }
  402. void Framework::DX12ShaderHitGroup::setClosestHitShaderFunction(
  403. DX12ShaderFunction* closestHitShaderFunction)
  404. {
  405. if (anyHitShaderFunction
  406. && anyHitShaderFunction->zSignature()
  407. != closestHitShaderFunction->zSignature())
  408. {
  409. Logging::error()
  410. << "Any-hit shader function and closest-hit shader "
  411. "function must have the same root signature when they are "
  412. "combined in the same hit group. HitGroup Name: '"
  413. << name << "' Any Hit Shader Function: '"
  414. << anyHitShaderFunction->getFunctionName().getText()
  415. << "' Closest Hit Shader Function: '"
  416. << closestHitShaderFunction->getFunctionName().getText() << "'";
  417. throw std::runtime_error("Incompatible root signatures in hit group");
  418. }
  419. if (intersectionShaderFunction
  420. && intersectionShaderFunction->zSignature()
  421. != closestHitShaderFunction->zSignature())
  422. {
  423. Logging::error()
  424. << "Intersection shader function and closest-hit shader "
  425. "function must have the same root signature when they are "
  426. "combined in the same hit group. HitGroup Name: '"
  427. << name << "' Intersection Shader Function: '"
  428. << intersectionShaderFunction->getFunctionName().getText()
  429. << "' Closest Hit Shader Function: '"
  430. << closestHitShaderFunction->getFunctionName().getText() << "'";
  431. throw std::runtime_error("Incompatible root signatures in hit group");
  432. }
  433. if (this->closestHitShaderFunction)
  434. {
  435. this->closestHitShaderFunction->release();
  436. }
  437. this->closestHitShaderFunction = closestHitShaderFunction;
  438. hitGroupDesc->ClosestHitShaderImport = 0;
  439. if (closestHitShaderFunction)
  440. {
  441. hitGroupDesc->ClosestHitShaderImport
  442. = closestHitShaderFunction->zExportDesc()->Name;
  443. }
  444. }
  445. void Framework::DX12ShaderHitGroup::setAnyHitShaderFunction(
  446. DX12ShaderFunction* anyHitShaderFunction)
  447. {
  448. if (closestHitShaderFunction
  449. && closestHitShaderFunction->zSignature()
  450. != anyHitShaderFunction->zSignature())
  451. {
  452. Logging::error()
  453. << "Any-hit shader function and closest-hit shader "
  454. "function must have the same root signature when they are "
  455. "combined in the same hit group. HitGroup Name: '"
  456. << name << "' Any Hit Shader Function: '"
  457. << anyHitShaderFunction->getFunctionName().getText()
  458. << "' Closest Hit Shader Function: '"
  459. << closestHitShaderFunction->getFunctionName().getText() << "'";
  460. throw std::runtime_error("Incompatible root signatures in hit group");
  461. }
  462. if (intersectionShaderFunction
  463. && intersectionShaderFunction->zSignature()
  464. != anyHitShaderFunction->zSignature())
  465. {
  466. Logging::error()
  467. << "Intersection shader function and any-hit shader "
  468. "function must have the same root signature when they are "
  469. "combined in the same hit group. HitGroup Name: '"
  470. << name << "' Intersection Shader Function: '"
  471. << intersectionShaderFunction->getFunctionName().getText()
  472. << "' Any Hit Shader Function: '"
  473. << anyHitShaderFunction->getFunctionName().getText() << "'";
  474. throw std::runtime_error("Incompatible root signatures in hit group");
  475. }
  476. if (this->anyHitShaderFunction)
  477. {
  478. this->anyHitShaderFunction->release();
  479. }
  480. this->anyHitShaderFunction = anyHitShaderFunction;
  481. hitGroupDesc->AnyHitShaderImport = 0;
  482. if (anyHitShaderFunction)
  483. {
  484. hitGroupDesc->AnyHitShaderImport
  485. = anyHitShaderFunction->zExportDesc()->Name;
  486. }
  487. }
  488. void Framework::DX12ShaderHitGroup::setIntersectionShaderFunction(
  489. DX12ShaderFunction* zIntersectionShaderFunction)
  490. {
  491. if (closestHitShaderFunction
  492. && closestHitShaderFunction->zSignature()
  493. != zIntersectionShaderFunction->zSignature())
  494. {
  495. Logging::error()
  496. << "Intersection shader function and closest-hit shader "
  497. "function must have the same root signature when they are "
  498. "combined in the same hit group. HitGroup Name: '"
  499. << name << "' Intersection Shader Function: '"
  500. << zIntersectionShaderFunction->getFunctionName().getText()
  501. << "' Closest Hit Shader Function: '"
  502. << closestHitShaderFunction->getFunctionName().getText() << "'";
  503. throw std::runtime_error("Incompatible root signatures in hit group");
  504. }
  505. if (anyHitShaderFunction
  506. && anyHitShaderFunction->zSignature()
  507. != zIntersectionShaderFunction->zSignature())
  508. {
  509. Logging::error()
  510. << "Intersection shader function and any-hit shader "
  511. "function must have the same root signature when they are "
  512. "combined in the same hit group. HitGroup Name: '"
  513. << name << "' Intersection Shader Function: '"
  514. << zIntersectionShaderFunction->getFunctionName().getText()
  515. << "' Any Hit Shader Function: '"
  516. << anyHitShaderFunction->getFunctionName().getText() << "'";
  517. throw std::runtime_error("Incompatible root signatures in hit group");
  518. }
  519. if (intersectionShaderFunction)
  520. {
  521. intersectionShaderFunction->release();
  522. }
  523. intersectionShaderFunction = dynamic_cast<DX12ShaderFunction*>(
  524. zIntersectionShaderFunction->getThis());
  525. hitGroupDesc->IntersectionShaderImport = 0;
  526. if (intersectionShaderFunction)
  527. {
  528. hitGroupDesc->IntersectionShaderImport
  529. = intersectionShaderFunction->zExportDesc()->Name;
  530. }
  531. }
  532. void Framework::DX12ShaderHitGroup::setPayloadSize(int payloadSize)
  533. {
  534. this->payloadSize = payloadSize;
  535. }
  536. void Framework::DX12ShaderHitGroup::setAttributeSize(int attributeSize)
  537. {
  538. this->attributeSize = attributeSize;
  539. }
  540. const Text& Framework::DX12ShaderHitGroup::getName() const
  541. {
  542. return name;
  543. }
  544. DX12ShaderFunction*
  545. Framework::DX12ShaderHitGroup::zClosestHitShaderFunction() const
  546. {
  547. return closestHitShaderFunction;
  548. }
  549. DX12ShaderFunction* Framework::DX12ShaderHitGroup::zAnyHitShaderFunction() const
  550. {
  551. return anyHitShaderFunction;
  552. }
  553. DX12ShaderFunction*
  554. Framework::DX12ShaderHitGroup::zIntersectionShaderFunction() const
  555. {
  556. return intersectionShaderFunction;
  557. }
  558. int Framework::DX12ShaderHitGroup::getPayloadSize() const
  559. {
  560. return payloadSize;
  561. }
  562. int Framework::DX12ShaderHitGroup::getAttributeSize() const
  563. {
  564. return attributeSize;
  565. }
  566. D3D12_HIT_GROUP_DESC* Framework::DX12ShaderHitGroup::zHitGroupDesc() const
  567. {
  568. if (hitGroupDesc->IntersectionShaderImport)
  569. {
  570. hitGroupDesc->Type = D3D12_HIT_GROUP_TYPE_PROCEDURAL_PRIMITIVE;
  571. }
  572. else
  573. {
  574. hitGroupDesc->Type = D3D12_HIT_GROUP_TYPE_TRIANGLES;
  575. }
  576. return hitGroupDesc;
  577. }
  578. DX12ShaderSignature* Framework::DX12ShaderHitGroup::zSignature() const
  579. {
  580. if (closestHitShaderFunction)
  581. {
  582. return closestHitShaderFunction->zSignature();
  583. }
  584. if (anyHitShaderFunction)
  585. {
  586. return anyHitShaderFunction->zSignature();
  587. }
  588. if (intersectionShaderFunction)
  589. {
  590. return intersectionShaderFunction->zSignature();
  591. }
  592. return 0;
  593. }
  594. Framework::DX12Pipeline::DX12Pipeline()
  595. : ReferenceCounter(),
  596. emptyGlobalRootSignature(0),
  597. emptyLocalRootSignature(0),
  598. pipelineState(0),
  599. maxRecursionDepth(0)
  600. {}
  601. Framework::DX12Pipeline::~DX12Pipeline()
  602. {
  603. if (emptyGlobalRootSignature)
  604. {
  605. emptyGlobalRootSignature->Release();
  606. }
  607. if (emptyLocalRootSignature)
  608. {
  609. emptyLocalRootSignature->Release();
  610. }
  611. if (pipelineState)
  612. {
  613. pipelineState->Release();
  614. }
  615. }
  616. void Framework::DX12Pipeline::addShader(DX12Shader* shader)
  617. {
  618. shaders.add(shader);
  619. }
  620. void Framework::DX12Pipeline::addHitGroup(DX12ShaderHitGroup* hitGroup)
  621. {
  622. hitGroups.add(hitGroup);
  623. }
  624. void Framework::DX12Pipeline::setMaxRecursionDepth(int maxRecursionDepth)
  625. {
  626. this->maxRecursionDepth = maxRecursionDepth;
  627. }
  628. void Framework::DX12Pipeline::createPipelineState(ID3D12Device5* zDevice,
  629. PFN_D3D12_SERIALIZE_ROOT_SIGNATURE pfnD3D12SerializeRootSignature)
  630. {
  631. if (!emptyGlobalRootSignature)
  632. {
  633. D3D12_ROOT_SIGNATURE_DESC rootDesc = {};
  634. rootDesc.NumParameters = 0;
  635. rootDesc.pParameters = 0;
  636. rootDesc.Flags = D3D12_ROOT_SIGNATURE_FLAG_NONE;
  637. ID3DBlob* pSigBlob = 0;
  638. ID3DBlob* pErrorBlob = 0;
  639. HRESULT hr = pfnD3D12SerializeRootSignature(
  640. &rootDesc, D3D_ROOT_SIGNATURE_VERSION_1_0, &pSigBlob, &pErrorBlob);
  641. if (pSigBlob)
  642. {
  643. zDevice->CreateRootSignature(0,
  644. pSigBlob->GetBufferPointer(),
  645. pSigBlob->GetBufferSize(),
  646. __uuidof(ID3D12RootSignature),
  647. (void**)&emptyGlobalRootSignature);
  648. pSigBlob->Release();
  649. }
  650. if (pErrorBlob)
  651. {
  652. std::string errorMessage(
  653. static_cast<const char*>(pErrorBlob->GetBufferPointer()),
  654. pErrorBlob->GetBufferSize());
  655. Logging::error()
  656. << "Failed to serialize empty root signature: " << errorMessage;
  657. pErrorBlob->Release();
  658. }
  659. }
  660. if (!emptyLocalRootSignature)
  661. {
  662. D3D12_ROOT_SIGNATURE_DESC rootDesc = {};
  663. rootDesc.NumParameters = 0;
  664. rootDesc.pParameters = 0;
  665. rootDesc.Flags = D3D12_ROOT_SIGNATURE_FLAG_LOCAL_ROOT_SIGNATURE;
  666. ID3DBlob* pSigBlob = 0;
  667. ID3DBlob* pErrorBlob = 0;
  668. HRESULT hr = pfnD3D12SerializeRootSignature(
  669. &rootDesc, D3D_ROOT_SIGNATURE_VERSION_1_0, &pSigBlob, &pErrorBlob);
  670. if (pSigBlob)
  671. {
  672. zDevice->CreateRootSignature(0,
  673. pSigBlob->GetBufferPointer(),
  674. pSigBlob->GetBufferSize(),
  675. __uuidof(ID3D12RootSignature),
  676. (void**)&emptyLocalRootSignature);
  677. pSigBlob->Release();
  678. }
  679. if (pErrorBlob)
  680. {
  681. std::string errorMessage(
  682. static_cast<const char*>(pErrorBlob->GetBufferPointer()),
  683. pErrorBlob->GetBufferSize());
  684. Logging::error()
  685. << "Failed to serialize empty root signature: " << errorMessage;
  686. pErrorBlob->Release();
  687. }
  688. }
  689. unsigned int subobjectCount
  690. = shaders.getEntryCount() + hitGroups.getEntryCount() + 5;
  691. Array<DX12ShaderSignature*> distinctSignatures;
  692. for (const DX12Shader* shader : shaders)
  693. {
  694. for (const DX12ShaderFunction* function : shader->getFunctions())
  695. {
  696. DX12ShaderSignature* signature = function->zSignature();
  697. if (distinctSignatures.getValueIndex(signature) < 0)
  698. {
  699. distinctSignatures.add(signature);
  700. }
  701. }
  702. }
  703. subobjectCount += distinctSignatures.getEntryCount()
  704. * 2; // Local root signatures for each distinct signature
  705. D3D12_STATE_SUBOBJECT* subobjects
  706. = new D3D12_STATE_SUBOBJECT[subobjectCount];
  707. int index = 0;
  708. for (const DX12Shader* shader : shaders)
  709. {
  710. subobjects[index].Type = D3D12_STATE_SUBOBJECT_TYPE_DXIL_LIBRARY;
  711. subobjects[index].pDesc = shader->zLibraryDesc();
  712. index++;
  713. }
  714. int maxPayloadSize = 0;
  715. int maxAttributeSize = 0;
  716. for (const DX12ShaderHitGroup* hitGroup : hitGroups)
  717. {
  718. subobjects[index].Type = D3D12_STATE_SUBOBJECT_TYPE_HIT_GROUP;
  719. subobjects[index].pDesc = hitGroup->zHitGroupDesc();
  720. if (hitGroup->getPayloadSize() > maxPayloadSize)
  721. {
  722. maxPayloadSize = hitGroup->getPayloadSize();
  723. }
  724. if (hitGroup->getAttributeSize() > maxAttributeSize)
  725. {
  726. maxAttributeSize = hitGroup->getAttributeSize();
  727. }
  728. index++;
  729. }
  730. D3D12_RAYTRACING_SHADER_CONFIG shaderDesc = {};
  731. shaderDesc.MaxPayloadSizeInBytes = maxPayloadSize;
  732. shaderDesc.MaxAttributeSizeInBytes = maxAttributeSize;
  733. subobjects[index].Type
  734. = D3D12_STATE_SUBOBJECT_TYPE_RAYTRACING_SHADER_CONFIG;
  735. subobjects[index].pDesc = &shaderDesc;
  736. index++;
  737. functionsWithoutHitGroups.clear();
  738. // create export list for hole pipeline with all hitgroups and all functions
  739. // that are not part of a hit group
  740. for (const DX12Shader* shader : shaders)
  741. {
  742. for (const DX12ShaderFunction* function : shader->getFunctions())
  743. {
  744. bool found = 0;
  745. for (const DX12ShaderHitGroup* hitGroup : hitGroups)
  746. {
  747. if (hitGroup->zClosestHitShaderFunction() == function
  748. || hitGroup->zAnyHitShaderFunction() == function
  749. || hitGroup->zIntersectionShaderFunction() == function)
  750. {
  751. found = 1;
  752. break;
  753. }
  754. }
  755. if (!found)
  756. {
  757. functionsWithoutHitGroups.add(function);
  758. }
  759. }
  760. }
  761. const wchar_t** functionAndHitGroupNames
  762. = new const wchar_t*[functionsWithoutHitGroups.getEntryCount()
  763. + hitGroups.getEntryCount()];
  764. int nameIndex = 0;
  765. for (const DX12ShaderFunction* function : functionsWithoutHitGroups)
  766. {
  767. functionAndHitGroupNames[nameIndex]
  768. = const_cast<wchar_t*>(function->zExportDesc()->Name);
  769. nameIndex++;
  770. }
  771. for (const DX12ShaderHitGroup* hitGroup : hitGroups)
  772. {
  773. functionAndHitGroupNames[nameIndex]
  774. = const_cast<wchar_t*>(hitGroup->zHitGroupDesc()->HitGroupExport);
  775. nameIndex++;
  776. }
  777. D3D12_SUBOBJECT_TO_EXPORTS_ASSOCIATION shaderPayloadAssociation = {};
  778. shaderPayloadAssociation.NumExports
  779. = functionsWithoutHitGroups.getEntryCount() + hitGroups.getEntryCount();
  780. shaderPayloadAssociation.pExports = functionAndHitGroupNames;
  781. shaderPayloadAssociation.pSubobjectToAssociate
  782. = &subobjects[index - 1]; // shader config
  783. subobjects[index].Type
  784. = D3D12_STATE_SUBOBJECT_TYPE_SUBOBJECT_TO_EXPORTS_ASSOCIATION;
  785. subobjects[index].pDesc = &shaderPayloadAssociation;
  786. index++;
  787. D3D12_LOCAL_ROOT_SIGNATURE* localRootSignatures
  788. = new D3D12_LOCAL_ROOT_SIGNATURE[distinctSignatures.getEntryCount()];
  789. int rootSignatureIndex = 0;
  790. const wchar_t*** rootSignatureExports
  791. = new const wchar_t**[distinctSignatures.getEntryCount()];
  792. D3D12_SUBOBJECT_TO_EXPORTS_ASSOCIATION* localRootAssociations
  793. = new D3D12_SUBOBJECT_TO_EXPORTS_ASSOCIATION[distinctSignatures
  794. .getEntryCount()];
  795. for (DX12ShaderSignature* signature : distinctSignatures)
  796. {
  797. signature->createSignature(zDevice, pfnD3D12SerializeRootSignature);
  798. subobjects[index].Type
  799. = D3D12_STATE_SUBOBJECT_TYPE_LOCAL_ROOT_SIGNATURE;
  800. localRootSignatures[rootSignatureIndex].pLocalRootSignature
  801. = signature->zSignature();
  802. subobjects[index].pDesc = &localRootSignatures[rootSignatureIndex];
  803. index++;
  804. Array<const DX12ShaderFunction*> functionsWithThisSignature;
  805. for (const DX12Shader* shader : shaders)
  806. {
  807. for (const DX12ShaderFunction* function : shader->getFunctions())
  808. {
  809. if (function->zSignature() == signature)
  810. {
  811. bool found = 0;
  812. for (DX12ShaderHitGroup* hitGroup : hitGroups)
  813. {
  814. if (hitGroup->zClosestHitShaderFunction() == function
  815. || hitGroup->zAnyHitShaderFunction() == function
  816. || hitGroup->zIntersectionShaderFunction()
  817. == function)
  818. {
  819. found = 1;
  820. break;
  821. }
  822. }
  823. if (!found)
  824. {
  825. functionsWithThisSignature.add(function);
  826. }
  827. }
  828. }
  829. }
  830. Array<const DX12ShaderHitGroup*> hitGroupsWithThisSignature;
  831. for (const DX12ShaderHitGroup* hitGroup : hitGroups)
  832. {
  833. if (hitGroup->zClosestHitShaderFunction()
  834. && hitGroup->zClosestHitShaderFunction()->zSignature()
  835. == signature)
  836. {
  837. hitGroupsWithThisSignature.add(hitGroup);
  838. }
  839. else if (hitGroup->zAnyHitShaderFunction()
  840. && hitGroup->zAnyHitShaderFunction()->zSignature()
  841. == signature)
  842. {
  843. hitGroupsWithThisSignature.add(hitGroup);
  844. }
  845. else if (hitGroup->zIntersectionShaderFunction()
  846. && hitGroup->zIntersectionShaderFunction()->zSignature()
  847. == signature)
  848. {
  849. hitGroupsWithThisSignature.add(hitGroup);
  850. }
  851. }
  852. rootSignatureExports[rootSignatureIndex]
  853. = new const wchar_t*[functionsWithThisSignature.getEntryCount()
  854. + hitGroupsWithThisSignature.getEntryCount()];
  855. int nameIndex = 0;
  856. for (const DX12ShaderFunction* function : functionsWithThisSignature)
  857. {
  858. rootSignatureExports[rootSignatureIndex][nameIndex]
  859. = function->zExportDesc()->Name;
  860. nameIndex++;
  861. }
  862. for (const DX12ShaderHitGroup* hitGroup : hitGroupsWithThisSignature)
  863. {
  864. rootSignatureExports[rootSignatureIndex][nameIndex]
  865. = hitGroup->zHitGroupDesc()->HitGroupExport;
  866. nameIndex++;
  867. }
  868. localRootAssociations[rootSignatureIndex].NumExports
  869. = functionsWithThisSignature.getEntryCount()
  870. + hitGroupsWithThisSignature.getEntryCount();
  871. localRootAssociations[rootSignatureIndex].pExports
  872. = rootSignatureExports[rootSignatureIndex];
  873. localRootAssociations[rootSignatureIndex].pSubobjectToAssociate
  874. = &subobjects[index - 1]; // local root signature
  875. subobjects[index].Type
  876. = D3D12_STATE_SUBOBJECT_TYPE_SUBOBJECT_TO_EXPORTS_ASSOCIATION;
  877. subobjects[index].pDesc = &localRootAssociations[rootSignatureIndex];
  878. index++;
  879. rootSignatureIndex++;
  880. }
  881. subobjects[index].Type = D3D12_STATE_SUBOBJECT_TYPE_GLOBAL_ROOT_SIGNATURE;
  882. subobjects[index].pDesc = &emptyGlobalRootSignature;
  883. index++;
  884. subobjects[index].Type = D3D12_STATE_SUBOBJECT_TYPE_LOCAL_ROOT_SIGNATURE;
  885. subobjects[index].pDesc = &emptyLocalRootSignature;
  886. index++;
  887. D3D12_RAYTRACING_PIPELINE_CONFIG pipelineConfig = {};
  888. pipelineConfig.MaxTraceRecursionDepth = maxRecursionDepth;
  889. subobjects[index].Type
  890. = D3D12_STATE_SUBOBJECT_TYPE_RAYTRACING_PIPELINE_CONFIG;
  891. subobjects[index].pDesc = &pipelineConfig;
  892. index++;
  893. D3D12_STATE_OBJECT_DESC pipelineDesc = {};
  894. pipelineDesc.Type = D3D12_STATE_OBJECT_TYPE_RAYTRACING_PIPELINE;
  895. pipelineDesc.NumSubobjects = subobjectCount;
  896. pipelineDesc.pSubobjects = subobjects;
  897. HRESULT hr = zDevice->CreateStateObject(
  898. &pipelineDesc, __uuidof(ID3D12StateObject), (void**)&pipelineState);
  899. if (FAILED(hr))
  900. {
  901. Logging::error()
  902. << "Failed to create raytracing pipeline state object: " << std::hex
  903. << hr;
  904. throw std::logic_error("Could not create the raytracing state object");
  905. }
  906. /* delete[] functionAndHitGroupNames;
  907. delete[] localRootSignatures;
  908. for (int i = 0; i < distinctSignatures.getEntryCount(); i++)
  909. {
  910. delete[] rootSignatureExports[i];
  911. }
  912. delete[] rootSignatureExports;
  913. delete[] localRootAssociations;*/
  914. }
  915. ID3D12StateObject* Framework::DX12Pipeline::zPipelineState() const
  916. {
  917. return pipelineState;
  918. }
  919. DX12ShaderBindingTable* Framework::DX12Pipeline::createShaderBindingTable()
  920. {
  921. return new DX12ShaderBindingTable(dynamic_cast<DX12Pipeline*>(getThis()));
  922. }
  923. DX12GlobalDescriptorHeap* Framework::DX12Pipeline::createGlobalDescriptorHeap()
  924. {
  925. return new DX12GlobalDescriptorHeap(dynamic_cast<DX12Pipeline*>(getThis()));
  926. }
  927. const RCArray<DX12Shader>& Framework::DX12Pipeline::getShaders() const
  928. {
  929. return shaders;
  930. }
  931. const Array<const DX12ShaderFunction*>&
  932. Framework::DX12Pipeline::getFunctionsWithoutHitGroups() const
  933. {
  934. return functionsWithoutHitGroups;
  935. }
  936. const RCArray<DX12ShaderHitGroup>& Framework::DX12Pipeline::getHitGroups() const
  937. {
  938. return hitGroups;
  939. }
  940. Framework::DX12GlobalDescriptorHeap::DX12GlobalDescriptorHeap(
  941. DX12Pipeline* pipeline)
  942. : ReferenceCounter(),
  943. pipeline(pipeline),
  944. descriptorHeap(0),
  945. lastDescriptorHeapSize(0),
  946. zDevice(0)
  947. {}
  948. Framework::DX12GlobalDescriptorHeap::~DX12GlobalDescriptorHeap()
  949. {
  950. pipeline->release();
  951. if (descriptorHeap)
  952. {
  953. descriptorHeap->Release();
  954. }
  955. for (const DX12ShaderRegisterInput* input : registerInputs)
  956. {
  957. input->inputResource->release();
  958. delete input;
  959. }
  960. }
  961. void Framework::DX12GlobalDescriptorHeap::addInput(
  962. DX12ShaderRegister type, ReferenceCounter* inputResource)
  963. {
  964. bool found = 0;
  965. for (DX12Shader* shader : pipeline->getShaders())
  966. {
  967. for (DX12ShaderFunction* function : shader->getFunctions())
  968. {
  969. for (const DX12ShaderRegisterUsage* usage :
  970. function->zSignature()->getDescriptorHeapBindings())
  971. {
  972. if (usage->descriptorHeapIndex
  973. == registerInputs.getEntryCount())
  974. {
  975. if (usage->registerType != type)
  976. {
  977. Logging::error()
  978. << "Register type mismatch for register index "
  979. << usage->registerIndex << ", space index "
  980. << usage->spaceIndex << ". Expected register type: "
  981. << usage->registerType
  982. << ", given register type: " << type
  983. << ". The register type is specified in the "
  984. "signature of shader function '"
  985. << function->getFunctionName() << "'";
  986. throw std::logic_error(
  987. "Register type mismatch for shader input");
  988. }
  989. else
  990. {
  991. found = 1;
  992. break;
  993. }
  994. }
  995. }
  996. if (found)
  997. {
  998. break;
  999. }
  1000. }
  1001. if (found)
  1002. {
  1003. break;
  1004. }
  1005. }
  1006. registerInputs.add(new DX12ShaderRegisterInput{
  1007. type, inputResource ? inputResource->getThis() : 0});
  1008. }
  1009. void Framework::DX12GlobalDescriptorHeap::addTextureInput(
  1010. DX12ShaderRegister type, Texture* zTexture)
  1011. {
  1012. addInput(type, zTexture);
  1013. }
  1014. void Framework::DX12GlobalDescriptorHeap::updateTextureInput(
  1015. int heapIndex, DX12ShaderRegister type, Texture* zTexture)
  1016. {
  1017. DX12ShaderRegisterInput* input = registerInputs.get(heapIndex);
  1018. if (registerInputs.get(heapIndex)->inputResource
  1019. != dynamic_cast<ReferenceCounter*>(zTexture)
  1020. || zTexture->hasBufferChanged())
  1021. {
  1022. zTexture->setBufferChanged(0);
  1023. if (input->registerType != type)
  1024. {
  1025. Logging::error()
  1026. << "Register type mismatch for descriptor heap index "
  1027. << heapIndex
  1028. << ". Expected register type: " << input->registerType
  1029. << ", given register type: " << type << ".";
  1030. throw std::logic_error("Register type mismatch in descriptor heap");
  1031. }
  1032. registerInputs.get(heapIndex)->inputResource->release();
  1033. registerInputs.get(heapIndex)->inputResource = zTexture->getThis();
  1034. if (descriptorHeap)
  1035. {
  1036. D3D12_CPU_DESCRIPTOR_HANDLE descriptorHeapHandle
  1037. = descriptorHeap->GetCPUDescriptorHandleForHeapStart();
  1038. descriptorHeapHandle.ptr
  1039. += zDevice->GetDescriptorHandleIncrementSize(
  1040. D3D12_DESCRIPTOR_HEAP_TYPE_CBV_SRV_UAV)
  1041. * heapIndex;
  1042. switch (input->registerType)
  1043. {
  1044. case DX12_SHADER_REGISTER_T_SHADER_RESOURCE:
  1045. {
  1046. D3D12_SHADER_RESOURCE_VIEW_DESC srvDesc;
  1047. srvDesc.Format = DXGI_FORMAT_UNKNOWN;
  1048. srvDesc.ViewDimension = D3D12_SRV_DIMENSION_TEXTURE2D;
  1049. srvDesc.Texture2D.MipLevels = 0;
  1050. srvDesc.Texture2D.MostDetailedMip = 0;
  1051. srvDesc.Texture2D.PlaneSlice = 0;
  1052. srvDesc.Texture2D.ResourceMinLODClamp = 0.0f;
  1053. srvDesc.Shader4ComponentMapping
  1054. = D3D12_DEFAULT_SHADER_4_COMPONENT_MAPPING;
  1055. zDevice->CreateShaderResourceView(
  1056. ((DX12Texture*)zTexture)->zResource(),
  1057. &srvDesc,
  1058. descriptorHeapHandle);
  1059. break;
  1060. }
  1061. case DX12_SHADER_REGISTER_U_UNORDERED_ACCESS:
  1062. {
  1063. D3D12_UNORDERED_ACCESS_VIEW_DESC uavDesc = {};
  1064. uavDesc.ViewDimension = D3D12_UAV_DIMENSION_TEXTURE2D;
  1065. uavDesc.Format = DXGI_FORMAT_UNKNOWN;
  1066. uavDesc.Texture2D.MipSlice = 0;
  1067. uavDesc.Texture2D.PlaneSlice = 0;
  1068. zDevice->CreateUnorderedAccessView(
  1069. ((DX12Texture*)zTexture)->zResource(),
  1070. 0,
  1071. &uavDesc,
  1072. descriptorHeapHandle);
  1073. break;
  1074. }
  1075. }
  1076. }
  1077. }
  1078. }
  1079. void Framework::DX12GlobalDescriptorHeap::addBufferInput(
  1080. DX12ShaderRegister type, DXBuffer* zBuffer)
  1081. {
  1082. addInput(type, zBuffer);
  1083. }
  1084. void Framework::DX12GlobalDescriptorHeap::addTLASInput(
  1085. DX12ShaderRegister type, DX12TLAS* zTLAS)
  1086. {
  1087. addInput(type, zTLAS);
  1088. }
  1089. void Framework::DX12GlobalDescriptorHeap::updateTLASInput(
  1090. int heapIndex, DX12ShaderRegister type, DX12TLAS* zTLAS)
  1091. {
  1092. DX12ShaderRegisterInput* input = registerInputs.get(heapIndex);
  1093. if (registerInputs.get(heapIndex)->inputResource
  1094. != dynamic_cast<ReferenceCounter*>(zTLAS)
  1095. || zTLAS->hasBufferChanged())
  1096. {
  1097. zTLAS->setBufferChanged(0);
  1098. if (input->registerType != type)
  1099. {
  1100. Logging::error()
  1101. << "Register type mismatch for descriptor heap index "
  1102. << heapIndex
  1103. << ". Expected register type: " << input->registerType
  1104. << ", given register type: " << type << ".";
  1105. throw std::logic_error("Register type mismatch in descriptor heap");
  1106. }
  1107. if (registerInputs.get(heapIndex)->inputResource)
  1108. {
  1109. registerInputs.get(heapIndex)->inputResource->release();
  1110. }
  1111. registerInputs.get(heapIndex)->inputResource = zTLAS->getThis();
  1112. if (descriptorHeap)
  1113. {
  1114. D3D12_CPU_DESCRIPTOR_HANDLE descriptorHeapHandle
  1115. = descriptorHeap->GetCPUDescriptorHandleForHeapStart();
  1116. descriptorHeapHandle.ptr
  1117. += zDevice->GetDescriptorHandleIncrementSize(
  1118. D3D12_DESCRIPTOR_HEAP_TYPE_CBV_SRV_UAV)
  1119. * heapIndex;
  1120. switch (input->registerType)
  1121. {
  1122. case DX12_SHADER_REGISTER_T_SHADER_RESOURCE:
  1123. {
  1124. D3D12_SHADER_RESOURCE_VIEW_DESC srvDesc;
  1125. srvDesc.Format = DXGI_FORMAT_UNKNOWN;
  1126. srvDesc.ViewDimension
  1127. = D3D12_SRV_DIMENSION_RAYTRACING_ACCELERATION_STRUCTURE;
  1128. srvDesc.RaytracingAccelerationStructure.Location
  1129. = zTLAS->zResultBuffer()
  1130. ->zBuffer()
  1131. ->GetGPUVirtualAddress();
  1132. srvDesc.Shader4ComponentMapping
  1133. = D3D12_DEFAULT_SHADER_4_COMPONENT_MAPPING;
  1134. zDevice->CreateShaderResourceView(
  1135. 0, &srvDesc, descriptorHeapHandle);
  1136. break;
  1137. }
  1138. }
  1139. }
  1140. }
  1141. }
  1142. void Framework::DX12GlobalDescriptorHeap::updateDescriptorHeap(
  1143. ID3D12Device5* zDevice)
  1144. {
  1145. this->zDevice = zDevice;
  1146. if (!descriptorHeap
  1147. || lastDescriptorHeapSize != registerInputs.getEntryCount())
  1148. {
  1149. if (descriptorHeap)
  1150. {
  1151. descriptorHeap->Release();
  1152. }
  1153. D3D12_DESCRIPTOR_HEAP_DESC desc = {};
  1154. desc.NumDescriptors = registerInputs.getEntryCount();
  1155. desc.Type = D3D12_DESCRIPTOR_HEAP_TYPE_CBV_SRV_UAV;
  1156. desc.Flags = D3D12_DESCRIPTOR_HEAP_FLAG_SHADER_VISIBLE;
  1157. desc.NodeMask = 0;
  1158. HRESULT r = zDevice->CreateDescriptorHeap(
  1159. &desc, __uuidof(ID3D12DescriptorHeap), (void**)&descriptorHeap);
  1160. lastDescriptorHeapSize = registerInputs.getEntryCount();
  1161. }
  1162. D3D12_CPU_DESCRIPTOR_HANDLE descriptorHeapHandle
  1163. = descriptorHeap->GetCPUDescriptorHandleForHeapStart();
  1164. for (const DX12ShaderRegisterInput* input : registerInputs)
  1165. {
  1166. DX12TLAS* zTLAS = dynamic_cast<DX12TLAS*>(input->inputResource);
  1167. DX12Texture* zTexture
  1168. = dynamic_cast<DX12Texture*>(input->inputResource);
  1169. DX12Buffer* zBuffer = dynamic_cast<DX12Buffer*>(input->inputResource);
  1170. switch (input->registerType)
  1171. {
  1172. case DX12_SHADER_REGISTER_B_CONST_BUFFER:
  1173. {
  1174. D3D12_CONSTANT_BUFFER_VIEW_DESC cbvDesc = {};
  1175. if (!zBuffer)
  1176. {
  1177. Logging::error()
  1178. << "Expected a buffer resource for register type "
  1179. << input->registerType;
  1180. throw std::logic_error(
  1181. "Expected a buffer resource for register type "
  1182. + std::to_string(input->registerType));
  1183. }
  1184. cbvDesc.BufferLocation
  1185. = zBuffer->zBuffer()->GetGPUVirtualAddress();
  1186. cbvDesc.SizeInBytes = (unsigned)zBuffer->getElementCount()
  1187. * zBuffer->getElementLength();
  1188. zDevice->CreateConstantBufferView(
  1189. &cbvDesc, descriptorHeapHandle);
  1190. break;
  1191. }
  1192. case DX12_SHADER_REGISTER_T_SHADER_RESOURCE:
  1193. {
  1194. D3D12_SHADER_RESOURCE_VIEW_DESC srvDesc;
  1195. srvDesc.Format = DXGI_FORMAT_UNKNOWN;
  1196. bool doNothing = 0;
  1197. if (zTLAS)
  1198. {
  1199. srvDesc.ViewDimension
  1200. = D3D12_SRV_DIMENSION_RAYTRACING_ACCELERATION_STRUCTURE;
  1201. srvDesc.RaytracingAccelerationStructure.Location
  1202. = zTLAS->zResultBuffer()
  1203. ->zBuffer()
  1204. ->GetGPUVirtualAddress();
  1205. }
  1206. else if (zTexture)
  1207. {
  1208. srvDesc.ViewDimension = D3D12_SRV_DIMENSION_TEXTURE2D;
  1209. srvDesc.Texture2D.MipLevels = 0;
  1210. srvDesc.Texture2D.MostDetailedMip = 0;
  1211. srvDesc.Texture2D.PlaneSlice = 0;
  1212. srvDesc.Texture2D.ResourceMinLODClamp = 0.0f;
  1213. }
  1214. else if (zBuffer)
  1215. {
  1216. srvDesc.ViewDimension = D3D12_SRV_DIMENSION_BUFFER;
  1217. srvDesc.Buffer.FirstElement = 0;
  1218. srvDesc.Buffer.NumElements
  1219. = (unsigned)zBuffer->getElementCount();
  1220. srvDesc.Buffer.StructureByteStride
  1221. = zBuffer->getElementLength();
  1222. srvDesc.Buffer.Flags = D3D12_BUFFER_SRV_FLAG_NONE;
  1223. }
  1224. else
  1225. {
  1226. doNothing = 1;
  1227. }
  1228. if (!doNothing)
  1229. {
  1230. srvDesc.Shader4ComponentMapping
  1231. = D3D12_DEFAULT_SHADER_4_COMPONENT_MAPPING;
  1232. zDevice->CreateShaderResourceView(
  1233. zTexture ? zTexture->zResource()
  1234. : (zBuffer ? zBuffer->zBuffer() : 0),
  1235. &srvDesc,
  1236. descriptorHeapHandle);
  1237. }
  1238. break;
  1239. }
  1240. case DX12_SHADER_REGISTER_U_UNORDERED_ACCESS:
  1241. {
  1242. D3D12_UNORDERED_ACCESS_VIEW_DESC uavDesc = {};
  1243. if (zTexture)
  1244. {
  1245. uavDesc.ViewDimension = D3D12_UAV_DIMENSION_TEXTURE2D;
  1246. uavDesc.Format = DXGI_FORMAT_UNKNOWN;
  1247. uavDesc.Texture2D.MipSlice = 0;
  1248. uavDesc.Texture2D.PlaneSlice = 0;
  1249. }
  1250. else if (zBuffer)
  1251. {
  1252. uavDesc.ViewDimension = D3D12_UAV_DIMENSION_BUFFER;
  1253. uavDesc.Buffer.FirstElement = 0;
  1254. uavDesc.Buffer.NumElements
  1255. = (unsigned)zBuffer->getElementCount();
  1256. uavDesc.Buffer.StructureByteStride
  1257. = zBuffer->getElementLength();
  1258. uavDesc.Buffer.CounterOffsetInBytes = 0;
  1259. uavDesc.Buffer.Flags = D3D12_BUFFER_UAV_FLAG_NONE;
  1260. }
  1261. else
  1262. {
  1263. Logging::error()
  1264. << "Expected a texture or buffer resource for "
  1265. "register type "
  1266. << input->registerType;
  1267. throw std::logic_error(
  1268. "Expected a texture or buffer resource for "
  1269. "register "
  1270. "type "
  1271. + std::to_string(input->registerType));
  1272. }
  1273. zDevice->CreateUnorderedAccessView(
  1274. zTexture ? zTexture->zResource() : zBuffer->zBuffer(),
  1275. 0,
  1276. &uavDesc,
  1277. descriptorHeapHandle);
  1278. break;
  1279. }
  1280. }
  1281. descriptorHeapHandle.ptr += zDevice->GetDescriptorHandleIncrementSize(
  1282. D3D12_DESCRIPTOR_HEAP_TYPE_CBV_SRV_UAV);
  1283. }
  1284. }
  1285. DX12Pipeline* Framework::DX12GlobalDescriptorHeap::zPipeline() const
  1286. {
  1287. return pipeline;
  1288. }
  1289. ID3D12DescriptorHeap*
  1290. Framework::DX12GlobalDescriptorHeap::zDescriptorHeap() const
  1291. {
  1292. return descriptorHeap;
  1293. }
  1294. Framework::DX12ShaderBindingTable::DX12ShaderBindingTable(
  1295. DX12Pipeline* pipeline)
  1296. : ReferenceCounter(),
  1297. pipeline(pipeline),
  1298. shaderBindingTableBuffer(0),
  1299. globalDescriptorHeap(0),
  1300. rayGenRecordSize(0),
  1301. rayGenCount(0),
  1302. missRecordSize(0),
  1303. missCount(0),
  1304. callableRecordSize(0),
  1305. callableCount(0),
  1306. hitGroupRecordSize(0),
  1307. hitGroupCount(0),
  1308. tableBuffer(0),
  1309. tableBufferSize(0),
  1310. nextHitGroupOffset(0),
  1311. stateObjectProperties(0)
  1312. {
  1313. pipeline->zPipelineState()->QueryInterface(
  1314. __uuidof(ID3D12StateObjectProperties), (void**)&stateObjectProperties);
  1315. }
  1316. Framework::DX12ShaderBindingTable::~DX12ShaderBindingTable()
  1317. {
  1318. stateObjectProperties->Release();
  1319. if (pipeline)
  1320. {
  1321. pipeline->release();
  1322. }
  1323. if (shaderBindingTableBuffer)
  1324. {
  1325. shaderBindingTableBuffer->release();
  1326. }
  1327. if (globalDescriptorHeap)
  1328. {
  1329. globalDescriptorHeap->release();
  1330. }
  1331. for (const char* buffer : tempBuffers)
  1332. {
  1333. delete[] buffer;
  1334. }
  1335. }
  1336. void Framework::DX12ShaderBindingTable::setGlobalDescriptorHeap(
  1337. DX12GlobalDescriptorHeap* zGlobalDescriptorHeap)
  1338. {
  1339. if (this->globalDescriptorHeap != zGlobalDescriptorHeap)
  1340. {
  1341. if (this->globalDescriptorHeap)
  1342. {
  1343. this->globalDescriptorHeap->release();
  1344. }
  1345. this->globalDescriptorHeap = zGlobalDescriptorHeap;
  1346. if (this->globalDescriptorHeap)
  1347. {
  1348. this->globalDescriptorHeap->getThis();
  1349. }
  1350. }
  1351. }
  1352. void Framework::DX12ShaderBindingTable::startUpdate()
  1353. {
  1354. if (shaderBindingTableBuffer)
  1355. {
  1356. shaderBindingTableBuffer->zBuffer()->Map(0, 0, (void**)&tableBuffer);
  1357. tableBufferSize = (int)shaderBindingTableBuffer->getElementCount()
  1358. * shaderBindingTableBuffer->getElementLength();
  1359. }
  1360. else
  1361. {
  1362. tableBuffer = 0;
  1363. tableBufferSize = 0;
  1364. }
  1365. rayGenRecordSize = 0;
  1366. rayGenCount = 0;
  1367. missRecordSize = 0;
  1368. missCount = 0;
  1369. callableRecordSize = 0;
  1370. callableCount = 0;
  1371. for (const DX12ShaderFunction* function :
  1372. pipeline->getFunctionsWithoutHitGroups())
  1373. {
  1374. DX12ShaderSignature* signature = function->zSignature();
  1375. if (function->getFunctionType() == DX12_SHADER_FUNCTION_TYPE_RAY_GEN)
  1376. {
  1377. if (rayGenRecordSize < signature->gerShaderBindingTableParamCount())
  1378. {
  1379. rayGenRecordSize = signature->gerShaderBindingTableParamCount();
  1380. }
  1381. rayGenCount++;
  1382. }
  1383. else if (function->getFunctionType() == DX12_SHADER_FUNCTION_TYPE_MISS)
  1384. {
  1385. if (missRecordSize < signature->gerShaderBindingTableParamCount())
  1386. {
  1387. missRecordSize = signature->gerShaderBindingTableParamCount();
  1388. }
  1389. missCount++;
  1390. }
  1391. else if (function->getFunctionType()
  1392. == DX12_SHADER_FUNCTION_TYPE_CALLABLE)
  1393. {
  1394. if (callableRecordSize
  1395. < signature->gerShaderBindingTableParamCount())
  1396. {
  1397. callableRecordSize
  1398. = signature->gerShaderBindingTableParamCount();
  1399. }
  1400. callableCount++;
  1401. }
  1402. }
  1403. rayGenRecordSize = ROUND_UP_POWER_OF_2(
  1404. D3D12_RAYTRACING_SHADER_RECORD_BYTE_ALIGNMENT + rayGenRecordSize * 8,
  1405. D3D12_RAYTRACING_SHADER_RECORD_BYTE_ALIGNMENT);
  1406. missRecordSize = ROUND_UP_POWER_OF_2(
  1407. D3D12_RAYTRACING_SHADER_RECORD_BYTE_ALIGNMENT + missRecordSize * 8,
  1408. D3D12_RAYTRACING_SHADER_RECORD_BYTE_ALIGNMENT);
  1409. callableRecordSize = ROUND_UP_POWER_OF_2(
  1410. D3D12_RAYTRACING_SHADER_RECORD_BYTE_ALIGNMENT + callableRecordSize * 8,
  1411. D3D12_RAYTRACING_SHADER_RECORD_BYTE_ALIGNMENT);
  1412. hitGroupCount = 0;
  1413. hitGroupRecordSize = 0;
  1414. for (DX12ShaderHitGroup* hitGroup : pipeline->getHitGroups())
  1415. {
  1416. DX12ShaderSignature* signature = hitGroup->zSignature();
  1417. if (hitGroupRecordSize < signature->gerShaderBindingTableParamCount())
  1418. {
  1419. hitGroupRecordSize = signature->gerShaderBindingTableParamCount();
  1420. }
  1421. }
  1422. hitGroupRecordSize = ROUND_UP_POWER_OF_2(
  1423. D3D12_RAYTRACING_SHADER_RECORD_BYTE_ALIGNMENT + hitGroupRecordSize * 8,
  1424. D3D12_RAYTRACING_SHADER_RECORD_BYTE_ALIGNMENT);
  1425. nextHitGroupOffset = ROUND_UP_POWER_OF_2(
  1426. rayGenRecordSize * rayGenCount + missRecordSize * missCount
  1427. + callableRecordSize * callableCount,
  1428. 64);
  1429. }
  1430. void Framework::DX12ShaderBindingTable::set(int index, void* data, int size)
  1431. {
  1432. if (tableBufferSize >= index + size)
  1433. {
  1434. memcpy(tableBuffer + index, data, size);
  1435. }
  1436. else
  1437. {
  1438. index -= tableBufferSize;
  1439. ArrayIterator<char*> it = tempBuffers.begin();
  1440. while (it && 2048 < index + size)
  1441. {
  1442. index -= 2048;
  1443. it++;
  1444. }
  1445. while (2048 < index + size)
  1446. {
  1447. char* newBuffer = new char[2048];
  1448. memset(newBuffer, 0, 2048);
  1449. tempBuffers.add(newBuffer);
  1450. index -= 2048;
  1451. }
  1452. // because the buffer size is allways rounded up to multiples
  1453. // of 32 and the size written at once is always 8 or 32 it
  1454. // should never be possible to have a negative index here
  1455. assert(index >= 0);
  1456. if (!it)
  1457. {
  1458. char* newBuffer = new char[2048];
  1459. memset(newBuffer, 0, 2048);
  1460. tempBuffers.add(newBuffer);
  1461. memcpy(newBuffer + index, data, size);
  1462. }
  1463. else
  1464. {
  1465. memcpy(it.val() + index, data, size);
  1466. }
  1467. }
  1468. }
  1469. void Framework::DX12ShaderBindingTable::setShaderInput(
  1470. DX12ShaderFunction* zFunction, int* offsetPointer, __int64 gpuAddress)
  1471. {
  1472. int index = 0;
  1473. for (const DX12ShaderFunction* pf :
  1474. pipeline->getFunctionsWithoutHitGroups())
  1475. {
  1476. if (pf == zFunction)
  1477. {
  1478. break;
  1479. }
  1480. if (pf->getFunctionType() == zFunction->getFunctionType())
  1481. {
  1482. index++;
  1483. }
  1484. }
  1485. int offset = 0;
  1486. if (zFunction->getFunctionType() == DX12_SHADER_FUNCTION_TYPE_RAY_GEN)
  1487. {
  1488. offset = index * rayGenRecordSize;
  1489. }
  1490. else if (zFunction->getFunctionType() == DX12_SHADER_FUNCTION_TYPE_MISS)
  1491. {
  1492. offset = rayGenRecordSize * rayGenCount + index * missRecordSize;
  1493. }
  1494. else if (zFunction->getFunctionType() == DX12_SHADER_FUNCTION_TYPE_CALLABLE)
  1495. {
  1496. offset = rayGenRecordSize * rayGenCount + missRecordSize * missCount
  1497. + index * callableRecordSize;
  1498. }
  1499. else
  1500. {
  1501. throw std::logic_error("setShaderInput can only be used for ray "
  1502. "generation, miss, and callable "
  1503. "shader functions");
  1504. }
  1505. offset += D3D12_RAYTRACING_SHADER_RECORD_BYTE_ALIGNMENT
  1506. + sizeof(__int64) * *offsetPointer;
  1507. set(offset, &gpuAddress, sizeof(__int64));
  1508. }
  1509. int Framework::DX12ShaderBindingTable::addHitGroup(
  1510. DX12ShaderHitGroup* zHitGroup)
  1511. {
  1512. int index = nextHitGroupOffset;
  1513. set(index,
  1514. stateObjectProperties->GetShaderIdentifier(
  1515. zHitGroup->zHitGroupDesc()->HitGroupExport),
  1516. D3D12_RAYTRACING_SHADER_RECORD_BYTE_ALIGNMENT);
  1517. if (zHitGroup->zSignature()->getDescriptorHeapBindings().getEntryCount()
  1518. > 0)
  1519. {
  1520. D3D12_GPU_DESCRIPTOR_HANDLE gpuAddress
  1521. = globalDescriptorHeap->zDescriptorHeap()
  1522. ->GetGPUDescriptorHandleForHeapStart();
  1523. set(index + D3D12_RAYTRACING_SHADER_RECORD_BYTE_ALIGNMENT,
  1524. &gpuAddress.ptr,
  1525. sizeof(__int64));
  1526. }
  1527. hitGroupCount++;
  1528. nextHitGroupOffset += hitGroupRecordSize;
  1529. return index + D3D12_RAYTRACING_SHADER_RECORD_BYTE_ALIGNMENT;
  1530. }
  1531. void Framework::DX12ShaderBindingTable::setHitGroupShaderInput(
  1532. int hitGroupOffset, int* offsetPointer, __int64 gpuAddress)
  1533. {
  1534. set(hitGroupOffset + sizeof(__int64) * *offsetPointer,
  1535. &gpuAddress,
  1536. sizeof(__int64));
  1537. }
  1538. void Framework::DX12ShaderBindingTable::endUpdate(
  1539. ID3D12Device5* zDevice, DX12CommandQueue* zQueue)
  1540. {
  1541. int rayTracingIndex = 0;
  1542. int missIndex = 0;
  1543. int callableIndex = 0;
  1544. for (const DX12ShaderFunction* function :
  1545. pipeline->getFunctionsWithoutHitGroups())
  1546. {
  1547. int offset = -1;
  1548. DX12ShaderSignature* signature = function->zSignature();
  1549. if (function->getFunctionType() == DX12_SHADER_FUNCTION_TYPE_RAY_GEN)
  1550. {
  1551. offset = rayTracingIndex * rayGenRecordSize;
  1552. rayTracingIndex++;
  1553. }
  1554. else if (function->getFunctionType() == DX12_SHADER_FUNCTION_TYPE_MISS)
  1555. {
  1556. offset
  1557. = rayGenRecordSize * rayGenCount + missIndex * missRecordSize;
  1558. missIndex++;
  1559. }
  1560. else if (function->getFunctionType()
  1561. == DX12_SHADER_FUNCTION_TYPE_CALLABLE)
  1562. {
  1563. offset = rayGenRecordSize * rayGenCount + missRecordSize * missCount
  1564. + callableIndex * callableRecordSize;
  1565. callableIndex++;
  1566. }
  1567. if (offset >= 0)
  1568. {
  1569. set(offset,
  1570. stateObjectProperties->GetShaderIdentifier(
  1571. function->zExportDesc()->Name),
  1572. D3D12_RAYTRACING_SHADER_RECORD_BYTE_ALIGNMENT);
  1573. if (function->zSignature()
  1574. ->getDescriptorHeapBindings()
  1575. .getEntryCount()
  1576. > 0)
  1577. {
  1578. D3D12_GPU_DESCRIPTOR_HANDLE gpuAddress
  1579. = globalDescriptorHeap->zDescriptorHeap()
  1580. ->GetGPUDescriptorHandleForHeapStart();
  1581. set(offset + D3D12_RAYTRACING_SHADER_RECORD_BYTE_ALIGNMENT,
  1582. &gpuAddress.ptr,
  1583. sizeof(__int64));
  1584. }
  1585. }
  1586. }
  1587. if (nextHitGroupOffset > tableBufferSize)
  1588. {
  1589. DX12Buffer* newBuffer = new DX12Buffer(1,
  1590. zDevice,
  1591. dynamic_cast<DX12CommandQueue*>(zQueue->getThis()),
  1592. D3D12_RESOURCE_FLAG_NONE);
  1593. newBuffer->setLength(ROUND_UP_POWER_OF_2(nextHitGroupOffset, 256));
  1594. newBuffer->createBufferWithoutData(
  1595. D3D12_RESOURCE_STATE_GENERIC_READ, D3D12_HEAP_TYPE_UPLOAD);
  1596. void* newTableBuffer = 0;
  1597. newBuffer->zBuffer()->Map(0, 0, (void**)&newTableBuffer);
  1598. int sizeToCopy = nextHitGroupOffset;
  1599. int index = 0;
  1600. if (tableBufferSize > 0)
  1601. {
  1602. memcpy(newTableBuffer, tableBuffer, tableBufferSize);
  1603. sizeToCopy -= tableBufferSize;
  1604. index = tableBufferSize;
  1605. }
  1606. for (char* buffer : tempBuffers)
  1607. {
  1608. int bytesToCopy = sizeToCopy < 2048 ? sizeToCopy : 2048;
  1609. memcpy((char*)newTableBuffer + index, buffer, bytesToCopy);
  1610. sizeToCopy -= bytesToCopy;
  1611. index += bytesToCopy;
  1612. if (sizeToCopy == 0)
  1613. {
  1614. break;
  1615. }
  1616. }
  1617. assert(sizeToCopy == 0);
  1618. newBuffer->zBuffer()->Unmap(0, 0);
  1619. if (shaderBindingTableBuffer)
  1620. {
  1621. shaderBindingTableBuffer->zBuffer()->Unmap(0, 0);
  1622. shaderBindingTableBuffer->release();
  1623. }
  1624. shaderBindingTableBuffer = newBuffer;
  1625. }
  1626. else
  1627. {
  1628. shaderBindingTableBuffer->zBuffer()->Unmap(0, 0);
  1629. }
  1630. }
  1631. void Framework::DX12ShaderBindingTable::fillDispatchRaysDesc(
  1632. D3D12_DISPATCH_RAYS_DESC* dispatchRaysDesc)
  1633. {
  1634. dispatchRaysDesc->RayGenerationShaderRecord.SizeInBytes
  1635. = rayGenCount * rayGenRecordSize;
  1636. dispatchRaysDesc->RayGenerationShaderRecord.StartAddress
  1637. = shaderBindingTableBuffer->zBuffer()->GetGPUVirtualAddress();
  1638. dispatchRaysDesc->MissShaderTable.SizeInBytes = missCount * missRecordSize;
  1639. dispatchRaysDesc->MissShaderTable.StartAddress
  1640. = shaderBindingTableBuffer->zBuffer()->GetGPUVirtualAddress()
  1641. + rayGenCount * rayGenRecordSize;
  1642. dispatchRaysDesc->MissShaderTable.StrideInBytes = missRecordSize;
  1643. dispatchRaysDesc->HitGroupTable.SizeInBytes
  1644. = hitGroupCount * hitGroupRecordSize;
  1645. dispatchRaysDesc->HitGroupTable.StartAddress
  1646. = shaderBindingTableBuffer->zBuffer()->GetGPUVirtualAddress()
  1647. + ROUND_UP_POWER_OF_2(rayGenCount * rayGenRecordSize
  1648. + missCount * missRecordSize
  1649. + callableCount * callableRecordSize,
  1650. 64);
  1651. dispatchRaysDesc->HitGroupTable.StrideInBytes = hitGroupRecordSize;
  1652. dispatchRaysDesc->CallableShaderTable.SizeInBytes
  1653. = callableCount * callableRecordSize;
  1654. dispatchRaysDesc->CallableShaderTable.StartAddress
  1655. = callableCount > 0
  1656. ? shaderBindingTableBuffer->zBuffer()->GetGPUVirtualAddress()
  1657. + rayGenCount * rayGenRecordSize + missCount * missRecordSize
  1658. : 0;
  1659. dispatchRaysDesc->CallableShaderTable.StrideInBytes
  1660. = callableCount > 0 ? callableRecordSize : 0;
  1661. }
  1662. DX12Pipeline* Framework::DX12ShaderBindingTable::zPipeline() const
  1663. {
  1664. return pipeline;
  1665. }