DX12Shader.cpp 54 KB

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