DX12Shader.cpp 35 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034
  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. {}
  11. Framework::DX12ShaderSignature::~DX12ShaderSignature()
  12. {
  13. if (signature)
  14. {
  15. signature->Release();
  16. }
  17. }
  18. void Framework::DX12ShaderSignature::addRegisterUsage(
  19. DX12ShaderRegister registerType, int registerIndex, int spaceIndex)
  20. {
  21. // register usages sould be sorted by registerType -> spaceIndex ->
  22. // registerIndex
  23. ArrayIterator<DX12ShaderRegisterUsage> it = registerUsages.begin();
  24. bool found = 0;
  25. while (it)
  26. {
  27. const DX12ShaderRegisterUsage& usage = it.val();
  28. if (usage.registerType > registerType)
  29. {
  30. found = 1;
  31. break;
  32. }
  33. if (usage.registerType == registerType)
  34. {
  35. if (usage.spaceIndex > spaceIndex)
  36. {
  37. found = 1;
  38. break;
  39. }
  40. if (usage.spaceIndex == spaceIndex)
  41. {
  42. if (usage.registerIndex >= registerIndex)
  43. {
  44. found = 1;
  45. break;
  46. }
  47. }
  48. }
  49. ++it;
  50. }
  51. if (found)
  52. {
  53. it.addBefore({registerType, registerIndex, spaceIndex});
  54. }
  55. else
  56. {
  57. registerUsages.add({registerType, registerIndex, spaceIndex});
  58. }
  59. changed = 1;
  60. }
  61. void Framework::DX12ShaderSignature::createSignature(ID3D12Device5* zDevice,
  62. PFN_D3D12_SERIALIZE_ROOT_SIGNATURE pfnD3D12SerializeRootSignature)
  63. {
  64. if (!changed)
  65. {
  66. return;
  67. }
  68. changed = 0;
  69. if (signature)
  70. {
  71. signature->Release();
  72. signature = 0;
  73. }
  74. D3D12_ROOT_PARAMETER descriptorTable;
  75. descriptorTable.ParameterType = D3D12_ROOT_PARAMETER_TYPE_DESCRIPTOR_TABLE;
  76. descriptorTable.ShaderVisibility = D3D12_SHADER_VISIBILITY_ALL;
  77. descriptorTable.DescriptorTable.NumDescriptorRanges
  78. = registerUsages.getEntryCount();
  79. D3D12_DESCRIPTOR_RANGE* descriptorRanges
  80. = new D3D12_DESCRIPTOR_RANGE[registerUsages.getEntryCount()];
  81. int index = 0;
  82. ArrayIterator<DX12ShaderRegisterUsage> it = registerUsages.begin();
  83. while (it)
  84. {
  85. const auto& usage = it.val();
  86. D3D12_DESCRIPTOR_RANGE* range = &descriptorRanges[index];
  87. switch (usage.registerType)
  88. {
  89. case DX12_SHADER_REGISTER_B_CONST_BUFFER:
  90. range->RangeType = D3D12_DESCRIPTOR_RANGE_TYPE_CBV;
  91. break;
  92. case DX12_SHADER_REGISTER_T_SHADER_RESOURCE:
  93. range->RangeType = D3D12_DESCRIPTOR_RANGE_TYPE_SRV;
  94. break;
  95. case DX12_SHADER_REGISTER_U_UNORDERED_ACCESS:
  96. range->RangeType = D3D12_DESCRIPTOR_RANGE_TYPE_UAV;
  97. break;
  98. default:
  99. Logging::error() << "Unknown register type for root signature: "
  100. << usage.registerType;
  101. range->RangeType = D3D12_DESCRIPTOR_RANGE_TYPE_SRV;
  102. }
  103. ArrayIterator<DX12ShaderRegisterUsage> next = it.next();
  104. int size = 1;
  105. while (next && next.val().registerType == usage.registerType
  106. && next.val().spaceIndex == usage.spaceIndex
  107. && next.val().registerIndex == usage.registerIndex + size)
  108. {
  109. ++size;
  110. it = next;
  111. ++next;
  112. }
  113. range->NumDescriptors = size;
  114. range->BaseShaderRegister = usage.registerIndex;
  115. range->RegisterSpace = usage.spaceIndex;
  116. range->OffsetInDescriptorsFromTableStart = index;
  117. ++it;
  118. index++;
  119. }
  120. descriptorTable.DescriptorTable.pDescriptorRanges = descriptorRanges;
  121. D3D12_ROOT_SIGNATURE_DESC rootDesc = {};
  122. rootDesc.NumParameters = 1;
  123. rootDesc.pParameters = &descriptorTable;
  124. rootDesc.Flags = D3D12_ROOT_SIGNATURE_FLAG_LOCAL_ROOT_SIGNATURE;
  125. ID3DBlob* pSigBlob = 0;
  126. ID3DBlob* pErrorBlob = 0;
  127. HRESULT hr = pfnD3D12SerializeRootSignature(
  128. &rootDesc, D3D_ROOT_SIGNATURE_VERSION_1_0, &pSigBlob, &pErrorBlob);
  129. if (pSigBlob)
  130. {
  131. zDevice->CreateRootSignature(0,
  132. pSigBlob->GetBufferPointer(),
  133. pSigBlob->GetBufferSize(),
  134. __uuidof(ID3D12RootSignature),
  135. (void**)&signature);
  136. pSigBlob->Release();
  137. }
  138. if (pErrorBlob)
  139. {
  140. std::string errorMessage(
  141. static_cast<const char*>(pErrorBlob->GetBufferPointer()),
  142. pErrorBlob->GetBufferSize());
  143. Logging::error() << "Failed to serialize root signature: "
  144. << errorMessage;
  145. pErrorBlob->Release();
  146. }
  147. delete[] descriptorRanges;
  148. }
  149. ID3D12RootSignature* Framework::DX12ShaderSignature::zSignature() const
  150. {
  151. return signature;
  152. }
  153. const Array<DX12ShaderRegisterUsage>&
  154. Framework::DX12ShaderSignature::getRegisterUsagesOrder() const
  155. {
  156. return registerUsages;
  157. }
  158. ShaderHeap* Framework::DX12ShaderSignature::createShaderHeap()
  159. {
  160. return new ShaderHeap(dynamic_cast<DX12ShaderSignature*>(getThis()));
  161. }
  162. Framework::DX12ShaderFunction::DX12ShaderFunction(
  163. const Text& functionName, DX12ShaderSignature* signature)
  164. : ReferenceCounter(),
  165. functionName(functionName),
  166. signature(signature),
  167. exportDesc(new D3D12_EXPORT_DESC())
  168. {
  169. wchar_t* wc = new wchar_t[functionName.getLength() + 1];
  170. mbtowc(wc, functionName.getText(), functionName.getLength() + 1);
  171. wc[functionName.getLength()] = 0;
  172. exportDesc->Name = wc;
  173. exportDesc->ExportToRename = 0;
  174. exportDesc->Flags = D3D12_EXPORT_FLAG_NONE;
  175. }
  176. Framework::DX12ShaderFunction::~DX12ShaderFunction()
  177. {
  178. signature->release();
  179. delete[] exportDesc->Name;
  180. delete exportDesc;
  181. }
  182. const Text& Framework::DX12ShaderFunction::getFunctionName() const
  183. {
  184. return functionName;
  185. }
  186. DX12ShaderSignature* Framework::DX12ShaderFunction::zSignature() const
  187. {
  188. return signature;
  189. }
  190. D3D12_EXPORT_DESC* Framework::DX12ShaderFunction::zExportDesc() const
  191. {
  192. return exportDesc;
  193. }
  194. Framework::DX12Shader::DX12Shader(const char* shaderBytes, int shaderBytesSize)
  195. : ReferenceCounter(),
  196. shaderBytes(shaderBytes),
  197. shaderBytesSize(shaderBytesSize),
  198. libraryDesc(new D3D12_DXIL_LIBRARY_DESC())
  199. {
  200. libraryDesc->DXILLibrary.pShaderBytecode = shaderBytes;
  201. libraryDesc->DXILLibrary.BytecodeLength = shaderBytesSize;
  202. libraryDesc->NumExports = 0;
  203. libraryDesc->pExports = 0;
  204. }
  205. Framework::DX12Shader::~DX12Shader()
  206. {
  207. delete[] libraryDesc->pExports;
  208. delete libraryDesc;
  209. }
  210. void Framework::DX12Shader::addFunction(DX12ShaderFunction* function)
  211. {
  212. functions.add(function);
  213. }
  214. int Framework::DX12Shader::getShaderBytesSize() const
  215. {
  216. return shaderBytesSize;
  217. }
  218. const char* Framework::DX12Shader::getShaderBytes() const
  219. {
  220. return shaderBytes;
  221. }
  222. const RCArray<DX12ShaderFunction>& Framework::DX12Shader::getFunctions() const
  223. {
  224. return functions;
  225. }
  226. D3D12_DXIL_LIBRARY_DESC* Framework::DX12Shader::zLibraryDesc() const
  227. {
  228. if (libraryDesc->NumExports != functions.getEntryCount())
  229. {
  230. delete[] libraryDesc->pExports;
  231. libraryDesc->NumExports = functions.getEntryCount();
  232. D3D12_EXPORT_DESC* pExports
  233. = new D3D12_EXPORT_DESC[functions.getEntryCount()];
  234. int index = 0;
  235. for (const auto& function : functions)
  236. {
  237. memcpy(pExports + index,
  238. function->zExportDesc(),
  239. sizeof(D3D12_EXPORT_DESC));
  240. index++;
  241. }
  242. libraryDesc->pExports = pExports;
  243. }
  244. return libraryDesc;
  245. }
  246. Framework::DX12ShaderHitGroup::DX12ShaderHitGroup(const Text name)
  247. : ReferenceCounter(),
  248. name(name),
  249. closestHitShaderFunction(0),
  250. anyHitShaderFunction(0),
  251. intersectionShaderFunction(0),
  252. payloadSize(0),
  253. attributeSize(0),
  254. hitGroupDesc(new D3D12_HIT_GROUP_DESC())
  255. {
  256. wchar_t* wc = new wchar_t[name.getLength() + 1];
  257. mbtowc(wc, name.getText(), name.getLength() + 1);
  258. wc[name.getLength()] = 0;
  259. hitGroupDesc->HitGroupExport = wc;
  260. hitGroupDesc->IntersectionShaderImport = 0;
  261. hitGroupDesc->AnyHitShaderImport = 0;
  262. hitGroupDesc->ClosestHitShaderImport = 0;
  263. }
  264. Framework::DX12ShaderHitGroup::~DX12ShaderHitGroup()
  265. {
  266. delete[] hitGroupDesc->HitGroupExport;
  267. delete hitGroupDesc;
  268. if (closestHitShaderFunction)
  269. {
  270. closestHitShaderFunction->release();
  271. }
  272. if (anyHitShaderFunction)
  273. {
  274. anyHitShaderFunction->release();
  275. }
  276. if (intersectionShaderFunction)
  277. {
  278. intersectionShaderFunction->release();
  279. }
  280. }
  281. void Framework::DX12ShaderHitGroup::setClosestHitShaderFunction(
  282. DX12ShaderFunction* closestHitShaderFunction)
  283. {
  284. if (anyHitShaderFunction
  285. && anyHitShaderFunction->zSignature()
  286. != closestHitShaderFunction->zSignature())
  287. {
  288. Logging::error()
  289. << "Any-hit shader function and closest-hit shader "
  290. "function must have the same root signature when they are "
  291. "combined in the same hit group. HitGroup Name: '"
  292. << name << "' Any Hit Shader Function: '"
  293. << anyHitShaderFunction->getFunctionName().getText()
  294. << "' Closest Hit Shader Function: '"
  295. << closestHitShaderFunction->getFunctionName().getText() << "'";
  296. throw std::runtime_error("Incompatible root signatures in hit group");
  297. }
  298. if (intersectionShaderFunction
  299. && intersectionShaderFunction->zSignature()
  300. != closestHitShaderFunction->zSignature())
  301. {
  302. Logging::error()
  303. << "Intersection shader function and closest-hit shader "
  304. "function must have the same root signature when they are "
  305. "combined in the same hit group. HitGroup Name: '"
  306. << name << "' Intersection Shader Function: '"
  307. << intersectionShaderFunction->getFunctionName().getText()
  308. << "' Closest Hit Shader Function: '"
  309. << closestHitShaderFunction->getFunctionName().getText() << "'";
  310. throw std::runtime_error("Incompatible root signatures in hit group");
  311. }
  312. if (this->closestHitShaderFunction)
  313. {
  314. this->closestHitShaderFunction->release();
  315. }
  316. this->closestHitShaderFunction = closestHitShaderFunction;
  317. hitGroupDesc->ClosestHitShaderImport = 0;
  318. if (closestHitShaderFunction)
  319. {
  320. hitGroupDesc->ClosestHitShaderImport
  321. = closestHitShaderFunction->zExportDesc()->Name;
  322. }
  323. }
  324. void Framework::DX12ShaderHitGroup::setAnyHitShaderFunction(
  325. DX12ShaderFunction* anyHitShaderFunction)
  326. {
  327. if (closestHitShaderFunction
  328. && closestHitShaderFunction->zSignature()
  329. != anyHitShaderFunction->zSignature())
  330. {
  331. Logging::error()
  332. << "Any-hit shader function and closest-hit shader "
  333. "function must have the same root signature when they are "
  334. "combined in the same hit group. HitGroup Name: '"
  335. << name << "' Any Hit Shader Function: '"
  336. << anyHitShaderFunction->getFunctionName().getText()
  337. << "' Closest Hit Shader Function: '"
  338. << closestHitShaderFunction->getFunctionName().getText() << "'";
  339. throw std::runtime_error("Incompatible root signatures in hit group");
  340. }
  341. if (intersectionShaderFunction
  342. && intersectionShaderFunction->zSignature()
  343. != anyHitShaderFunction->zSignature())
  344. {
  345. Logging::error()
  346. << "Intersection shader function and any-hit shader "
  347. "function must have the same root signature when they are "
  348. "combined in the same hit group. HitGroup Name: '"
  349. << name << "' Intersection Shader Function: '"
  350. << intersectionShaderFunction->getFunctionName().getText()
  351. << "' Any Hit Shader Function: '"
  352. << anyHitShaderFunction->getFunctionName().getText() << "'";
  353. throw std::runtime_error("Incompatible root signatures in hit group");
  354. }
  355. if (this->anyHitShaderFunction)
  356. {
  357. this->anyHitShaderFunction->release();
  358. }
  359. this->anyHitShaderFunction = anyHitShaderFunction;
  360. hitGroupDesc->AnyHitShaderImport = 0;
  361. if (anyHitShaderFunction)
  362. {
  363. hitGroupDesc->AnyHitShaderImport
  364. = anyHitShaderFunction->zExportDesc()->Name;
  365. }
  366. }
  367. void Framework::DX12ShaderHitGroup::setIntersectionShaderFunction(
  368. DX12ShaderFunction* intersectionShaderFunction)
  369. {
  370. if (closestHitShaderFunction
  371. && closestHitShaderFunction->zSignature()
  372. != intersectionShaderFunction->zSignature())
  373. {
  374. Logging::error()
  375. << "Intersection shader function and closest-hit shader "
  376. "function must have the same root signature when they are "
  377. "combined in the same hit group. HitGroup Name: '"
  378. << name << "' Intersection Shader Function: '"
  379. << intersectionShaderFunction->getFunctionName().getText()
  380. << "' Closest Hit Shader Function: '"
  381. << closestHitShaderFunction->getFunctionName().getText() << "'";
  382. throw std::runtime_error("Incompatible root signatures in hit group");
  383. }
  384. if (anyHitShaderFunction
  385. && anyHitShaderFunction->zSignature()
  386. != intersectionShaderFunction->zSignature())
  387. {
  388. Logging::error()
  389. << "Intersection shader function and any-hit shader "
  390. "function must have the same root signature when they are "
  391. "combined in the same hit group. HitGroup Name: '"
  392. << name << "' Intersection Shader Function: '"
  393. << intersectionShaderFunction->getFunctionName().getText()
  394. << "' Any Hit Shader Function: '"
  395. << anyHitShaderFunction->getFunctionName().getText() << "'";
  396. throw std::runtime_error("Incompatible root signatures in hit group");
  397. }
  398. if (this->intersectionShaderFunction)
  399. {
  400. this->intersectionShaderFunction->release();
  401. }
  402. this->intersectionShaderFunction = intersectionShaderFunction;
  403. hitGroupDesc->IntersectionShaderImport = 0;
  404. if (intersectionShaderFunction)
  405. {
  406. hitGroupDesc->IntersectionShaderImport
  407. = intersectionShaderFunction->zExportDesc()->Name;
  408. }
  409. }
  410. void Framework::DX12ShaderHitGroup::setPayloadSize(int payloadSize)
  411. {
  412. this->payloadSize = payloadSize;
  413. }
  414. void Framework::DX12ShaderHitGroup::setAttributeSize(int attributeSize)
  415. {
  416. this->attributeSize = attributeSize;
  417. }
  418. const Text& Framework::DX12ShaderHitGroup::getName() const
  419. {
  420. return name;
  421. }
  422. DX12ShaderFunction*
  423. Framework::DX12ShaderHitGroup::zClosestHitShaderFunction() const
  424. {
  425. return closestHitShaderFunction;
  426. }
  427. DX12ShaderFunction* Framework::DX12ShaderHitGroup::zAnyHitShaderFunction() const
  428. {
  429. return anyHitShaderFunction;
  430. }
  431. DX12ShaderFunction*
  432. Framework::DX12ShaderHitGroup::zIntersectionShaderFunction() const
  433. {
  434. return intersectionShaderFunction;
  435. }
  436. int Framework::DX12ShaderHitGroup::getPayloadSize() const
  437. {
  438. return payloadSize;
  439. }
  440. int Framework::DX12ShaderHitGroup::getAttributeSize() const
  441. {
  442. return attributeSize;
  443. }
  444. D3D12_HIT_GROUP_DESC* Framework::DX12ShaderHitGroup::zHitGroupDesc() const
  445. {
  446. if (hitGroupDesc->IntersectionShaderImport)
  447. {
  448. hitGroupDesc->Type = D3D12_HIT_GROUP_TYPE_PROCEDURAL_PRIMITIVE;
  449. }
  450. else
  451. {
  452. hitGroupDesc->Type = D3D12_HIT_GROUP_TYPE_TRIANGLES;
  453. }
  454. return hitGroupDesc;
  455. }
  456. Framework::DX12Pipeline::DX12Pipeline()
  457. : ReferenceCounter(),
  458. emptyGlobalRootSignature(0),
  459. emptyLocalRootSignature(0),
  460. pipelineState(0),
  461. maxRecursionDepth(0)
  462. {}
  463. Framework::DX12Pipeline::~DX12Pipeline()
  464. {
  465. if (emptyGlobalRootSignature)
  466. {
  467. emptyGlobalRootSignature->Release();
  468. }
  469. if (emptyLocalRootSignature)
  470. {
  471. emptyLocalRootSignature->Release();
  472. }
  473. if (pipelineState)
  474. {
  475. pipelineState->Release();
  476. }
  477. }
  478. void Framework::DX12Pipeline::addShader(DX12Shader* shader)
  479. {
  480. shaders.add(shader);
  481. }
  482. void Framework::DX12Pipeline::addHitGroup(DX12ShaderHitGroup* hitGroup)
  483. {
  484. hitGroups.add(hitGroup);
  485. }
  486. void Framework::DX12Pipeline::setMaxRecursionDepth(int maxRecursionDepth)
  487. {
  488. this->maxRecursionDepth = maxRecursionDepth;
  489. }
  490. void Framework::DX12Pipeline::createPipelineState(ID3D12Device5* zDevice,
  491. PFN_D3D12_SERIALIZE_ROOT_SIGNATURE pfnD3D12SerializeRootSignature)
  492. {
  493. if (!emptyGlobalRootSignature)
  494. {
  495. D3D12_ROOT_SIGNATURE_DESC rootDesc = {};
  496. rootDesc.NumParameters = 0;
  497. rootDesc.pParameters = 0;
  498. rootDesc.Flags = D3D12_ROOT_SIGNATURE_FLAG_NONE;
  499. ID3DBlob* pSigBlob = 0;
  500. ID3DBlob* pErrorBlob = 0;
  501. HRESULT hr = pfnD3D12SerializeRootSignature(
  502. &rootDesc, D3D_ROOT_SIGNATURE_VERSION_1_0, &pSigBlob, &pErrorBlob);
  503. if (pSigBlob)
  504. {
  505. zDevice->CreateRootSignature(0,
  506. pSigBlob->GetBufferPointer(),
  507. pSigBlob->GetBufferSize(),
  508. __uuidof(ID3D12RootSignature),
  509. (void**)&emptyGlobalRootSignature);
  510. pSigBlob->Release();
  511. }
  512. if (pErrorBlob)
  513. {
  514. std::string errorMessage(
  515. static_cast<const char*>(pErrorBlob->GetBufferPointer()),
  516. pErrorBlob->GetBufferSize());
  517. Logging::error()
  518. << "Failed to serialize empty root signature: " << errorMessage;
  519. pErrorBlob->Release();
  520. }
  521. }
  522. if (!emptyLocalRootSignature)
  523. {
  524. D3D12_ROOT_SIGNATURE_DESC rootDesc = {};
  525. rootDesc.NumParameters = 0;
  526. rootDesc.pParameters = 0;
  527. rootDesc.Flags = D3D12_ROOT_SIGNATURE_FLAG_LOCAL_ROOT_SIGNATURE;
  528. ID3DBlob* pSigBlob = 0;
  529. ID3DBlob* pErrorBlob = 0;
  530. HRESULT hr = pfnD3D12SerializeRootSignature(
  531. &rootDesc, D3D_ROOT_SIGNATURE_VERSION_1_0, &pSigBlob, &pErrorBlob);
  532. if (pSigBlob)
  533. {
  534. zDevice->CreateRootSignature(0,
  535. pSigBlob->GetBufferPointer(),
  536. pSigBlob->GetBufferSize(),
  537. __uuidof(ID3D12RootSignature),
  538. (void**)&emptyGlobalRootSignature);
  539. pSigBlob->Release();
  540. }
  541. if (pErrorBlob)
  542. {
  543. std::string errorMessage(
  544. static_cast<const char*>(pErrorBlob->GetBufferPointer()),
  545. pErrorBlob->GetBufferSize());
  546. Logging::error()
  547. << "Failed to serialize empty root signature: " << errorMessage;
  548. pErrorBlob->Release();
  549. }
  550. }
  551. unsigned int subobjectCount
  552. = shaders.getEntryCount() + hitGroups.getEntryCount() + 5;
  553. Array<DX12ShaderSignature*> distinctSignatures;
  554. for (const DX12Shader* shader : shaders)
  555. {
  556. for (const DX12ShaderFunction* function : shader->getFunctions())
  557. {
  558. DX12ShaderSignature* signature = function->zSignature();
  559. if (distinctSignatures.getValueIndex(signature) < 0)
  560. {
  561. distinctSignatures.add(signature);
  562. }
  563. }
  564. }
  565. subobjectCount += distinctSignatures.getEntryCount()
  566. * 2; // Local root signatures for each distinct signature
  567. D3D12_STATE_SUBOBJECT* subobjects
  568. = new D3D12_STATE_SUBOBJECT[subobjectCount];
  569. int index = 0;
  570. for (const DX12Shader* shader : shaders)
  571. {
  572. subobjects[index].Type = D3D12_STATE_SUBOBJECT_TYPE_DXIL_LIBRARY;
  573. subobjects[index].pDesc = shader->zLibraryDesc();
  574. index++;
  575. }
  576. int maxPayloadSize = 0;
  577. int maxAttributeSize = 0;
  578. for (const DX12ShaderHitGroup* hitGroup : hitGroups)
  579. {
  580. subobjects[index].Type = D3D12_STATE_SUBOBJECT_TYPE_HIT_GROUP;
  581. subobjects[index].pDesc = hitGroup->zHitGroupDesc();
  582. if (hitGroup->getPayloadSize() > maxPayloadSize)
  583. {
  584. maxPayloadSize = hitGroup->getPayloadSize();
  585. }
  586. if (hitGroup->getAttributeSize() > maxAttributeSize)
  587. {
  588. maxAttributeSize = hitGroup->getAttributeSize();
  589. }
  590. index++;
  591. }
  592. D3D12_RAYTRACING_SHADER_CONFIG shaderDesc = {};
  593. shaderDesc.MaxPayloadSizeInBytes = maxPayloadSize;
  594. shaderDesc.MaxAttributeSizeInBytes = maxAttributeSize;
  595. subobjects[index].Type
  596. = D3D12_STATE_SUBOBJECT_TYPE_RAYTRACING_SHADER_CONFIG;
  597. subobjects[index].pDesc = &shaderDesc;
  598. index++;
  599. // create export list for hole pipeline with all hitgroups and all functions
  600. // that are not part of a hit group
  601. Array<const DX12ShaderFunction*> functionsWithoutHitGroups;
  602. for (const DX12Shader* shader : shaders)
  603. {
  604. for (const DX12ShaderFunction* function : shader->getFunctions())
  605. {
  606. bool found = 0;
  607. for (const DX12ShaderHitGroup* hitGroup : hitGroups)
  608. {
  609. if (hitGroup->zClosestHitShaderFunction() == function
  610. || hitGroup->zAnyHitShaderFunction() == function
  611. || hitGroup->zIntersectionShaderFunction() == function)
  612. {
  613. found = 1;
  614. break;
  615. }
  616. }
  617. if (!found)
  618. {
  619. functionsWithoutHitGroups.add(function);
  620. }
  621. }
  622. }
  623. const wchar_t** functionAndHitGroupNames
  624. = new const wchar_t*[functionsWithoutHitGroups.getEntryCount()
  625. + hitGroups.getEntryCount()];
  626. int nameIndex = 0;
  627. for (const DX12ShaderFunction* function : functionsWithoutHitGroups)
  628. {
  629. functionAndHitGroupNames[nameIndex]
  630. = const_cast<wchar_t*>(function->zExportDesc()->Name);
  631. nameIndex++;
  632. }
  633. for (const DX12ShaderHitGroup* hitGroup : hitGroups)
  634. {
  635. functionAndHitGroupNames[nameIndex]
  636. = const_cast<wchar_t*>(hitGroup->zHitGroupDesc()->HitGroupExport);
  637. nameIndex++;
  638. }
  639. D3D12_SUBOBJECT_TO_EXPORTS_ASSOCIATION shaderPayloadAssociation = {};
  640. shaderPayloadAssociation.NumExports
  641. = functionsWithoutHitGroups.getEntryCount() + hitGroups.getEntryCount();
  642. shaderPayloadAssociation.pExports = functionAndHitGroupNames;
  643. shaderPayloadAssociation.pSubobjectToAssociate
  644. = &subobjects[index - 1]; // shader config
  645. subobjects[index].Type
  646. = D3D12_STATE_SUBOBJECT_TYPE_SUBOBJECT_TO_EXPORTS_ASSOCIATION;
  647. subobjects[index].pDesc = &shaderPayloadAssociation;
  648. index++;
  649. D3D12_LOCAL_ROOT_SIGNATURE* localRootSignatures
  650. = new D3D12_LOCAL_ROOT_SIGNATURE[distinctSignatures.getEntryCount()];
  651. int rootSignatureIndex = 0;
  652. const wchar_t*** rootSignatureExports
  653. = new const wchar_t**[distinctSignatures.getEntryCount()];
  654. D3D12_SUBOBJECT_TO_EXPORTS_ASSOCIATION* localRootAssociations
  655. = new D3D12_SUBOBJECT_TO_EXPORTS_ASSOCIATION[distinctSignatures
  656. .getEntryCount()];
  657. for (DX12ShaderSignature* signature : distinctSignatures)
  658. {
  659. signature->createSignature(zDevice, pfnD3D12SerializeRootSignature);
  660. subobjects[index].Type
  661. = D3D12_STATE_SUBOBJECT_TYPE_LOCAL_ROOT_SIGNATURE;
  662. localRootSignatures[rootSignatureIndex].pLocalRootSignature
  663. = signature->zSignature();
  664. subobjects[index].pDesc = &localRootSignatures[rootSignatureIndex];
  665. index++;
  666. Array<const DX12ShaderFunction*> functionsWithThisSignature;
  667. for (const DX12Shader* shader : shaders)
  668. {
  669. for (const DX12ShaderFunction* function : shader->getFunctions())
  670. {
  671. if (function->zSignature() == signature)
  672. {
  673. bool found = 0;
  674. for (DX12ShaderHitGroup* hitGroup : hitGroups)
  675. {
  676. if (hitGroup->zClosestHitShaderFunction() == function
  677. || hitGroup->zAnyHitShaderFunction() == function
  678. || hitGroup->zIntersectionShaderFunction()
  679. == function)
  680. {
  681. found = 1;
  682. break;
  683. }
  684. }
  685. if (!found)
  686. {
  687. functionsWithThisSignature.add(function);
  688. }
  689. }
  690. }
  691. }
  692. Array<const DX12ShaderHitGroup*> hitGroupsWithThisSignature;
  693. for (const DX12ShaderHitGroup* hitGroup : hitGroups)
  694. {
  695. if (hitGroup->zClosestHitShaderFunction()
  696. && hitGroup->zClosestHitShaderFunction()->zSignature()
  697. == signature)
  698. {
  699. hitGroupsWithThisSignature.add(hitGroup);
  700. }
  701. else if (hitGroup->zAnyHitShaderFunction()
  702. && hitGroup->zAnyHitShaderFunction()->zSignature()
  703. == signature)
  704. {
  705. hitGroupsWithThisSignature.add(hitGroup);
  706. }
  707. else if (hitGroup->zIntersectionShaderFunction()
  708. && hitGroup->zIntersectionShaderFunction()->zSignature()
  709. == signature)
  710. {
  711. hitGroupsWithThisSignature.add(hitGroup);
  712. }
  713. }
  714. rootSignatureExports[rootSignatureIndex]
  715. = new const wchar_t*[functionsWithThisSignature.getEntryCount()
  716. + hitGroupsWithThisSignature.getEntryCount()];
  717. int nameIndex = 0;
  718. for (const DX12ShaderFunction* function : functionsWithThisSignature)
  719. {
  720. rootSignatureExports[rootSignatureIndex][nameIndex]
  721. = function->zExportDesc()->Name;
  722. nameIndex++;
  723. }
  724. for (const DX12ShaderHitGroup* hitGroup : hitGroupsWithThisSignature)
  725. {
  726. rootSignatureExports[rootSignatureIndex][nameIndex]
  727. = hitGroup->zHitGroupDesc()->HitGroupExport;
  728. nameIndex++;
  729. }
  730. localRootAssociations[rootSignatureIndex].NumExports
  731. = functionsWithThisSignature.getEntryCount()
  732. + hitGroupsWithThisSignature.getEntryCount();
  733. localRootAssociations[rootSignatureIndex].pExports
  734. = rootSignatureExports[rootSignatureIndex];
  735. localRootAssociations[rootSignatureIndex].pSubobjectToAssociate
  736. = &subobjects[index - 1]; // local root signature
  737. subobjects[index].Type
  738. = D3D12_STATE_SUBOBJECT_TYPE_SUBOBJECT_TO_EXPORTS_ASSOCIATION;
  739. subobjects[index].pDesc = &localRootAssociations[rootSignatureIndex];
  740. index++;
  741. rootSignatureIndex++;
  742. }
  743. subobjects[index].Type = D3D12_STATE_SUBOBJECT_TYPE_GLOBAL_ROOT_SIGNATURE;
  744. subobjects[index].pDesc = &emptyGlobalRootSignature;
  745. index++;
  746. subobjects[index].Type = D3D12_STATE_SUBOBJECT_TYPE_LOCAL_ROOT_SIGNATURE;
  747. subobjects[index].pDesc = &emptyLocalRootSignature;
  748. index++;
  749. D3D12_RAYTRACING_PIPELINE_CONFIG pipelineConfig = {};
  750. pipelineConfig.MaxTraceRecursionDepth = maxRecursionDepth;
  751. subobjects[index].Type
  752. = D3D12_STATE_SUBOBJECT_TYPE_RAYTRACING_PIPELINE_CONFIG;
  753. subobjects[index].pDesc = &pipelineConfig;
  754. index++;
  755. D3D12_STATE_OBJECT_DESC pipelineDesc = {};
  756. pipelineDesc.Type = D3D12_STATE_OBJECT_TYPE_RAYTRACING_PIPELINE;
  757. pipelineDesc.NumSubobjects = subobjectCount;
  758. pipelineDesc.pSubobjects = subobjects;
  759. HRESULT hr = zDevice->CreateStateObject(
  760. &pipelineDesc, __uuidof(ID3D12StateObject), (void**)&pipelineState);
  761. if (FAILED(hr))
  762. {
  763. Logging::error()
  764. << "Failed to create raytracing pipeline state object: " << std::hex
  765. << hr;
  766. throw std::logic_error("Could not create the raytracing state object");
  767. }
  768. delete[] functionAndHitGroupNames;
  769. delete[] localRootSignatures;
  770. for (int i = 0; i < distinctSignatures.getEntryCount(); i++)
  771. {
  772. delete[] rootSignatureExports[i];
  773. }
  774. delete[] rootSignatureExports;
  775. delete[] localRootAssociations;
  776. }
  777. ID3D12StateObject* Framework::DX12Pipeline::zPipelineState() const
  778. {
  779. return pipelineState;
  780. }
  781. Framework::ShaderHeap::ShaderHeap(DX12ShaderSignature* signature)
  782. : ReferenceCounter(),
  783. signature(signature),
  784. descriptorHeap(0),
  785. lastDescriptorHeapSize(0)
  786. {}
  787. Framework::ShaderHeap::~ShaderHeap()
  788. {
  789. signature->release();
  790. if (descriptorHeap)
  791. {
  792. descriptorHeap->Release();
  793. }
  794. for (const DX12ShaderRegisterInput* input : registerInputs)
  795. {
  796. input->inputResource->release();
  797. delete input;
  798. }
  799. }
  800. void Framework::ShaderHeap::setRegisterInput(DX12ShaderRegister registerType,
  801. int registerIndex,
  802. int spaceIndex,
  803. ReferenceCounter* inputResource)
  804. {
  805. for (DX12ShaderRegisterInput* input : registerInputs)
  806. {
  807. if (input->registerIndex == registerIndex
  808. && input->spaceIndex == spaceIndex
  809. && input->registerType == registerType)
  810. {
  811. input->inputResource->release();
  812. input->inputResource = inputResource->getThis();
  813. return;
  814. }
  815. }
  816. bool found = 0;
  817. for (const DX12ShaderRegisterUsage& usage :
  818. signature->getRegisterUsagesOrder())
  819. {
  820. if (usage.registerIndex == registerIndex
  821. && usage.spaceIndex == spaceIndex
  822. && usage.registerType == registerType)
  823. {
  824. found = 1;
  825. break;
  826. }
  827. }
  828. if (found)
  829. {
  830. registerInputs.add(new DX12ShaderRegisterInput{
  831. registerType, registerIndex, spaceIndex, inputResource->getThis()});
  832. }
  833. else
  834. {
  835. Logging::error() << "Register type " << registerType
  836. << ", register index " << registerIndex
  837. << ", space index " << spaceIndex
  838. << " is not used in the shader signature. The given "
  839. "input will be ignored.";
  840. }
  841. }
  842. void Framework::ShaderHeap::setRegisterInput(
  843. Texture* zTexture, int registerIndex, int spaceIndex)
  844. {
  845. setRegisterInput(DX12_SHADER_REGISTER_U_UNORDERED_ACCESS,
  846. registerIndex,
  847. spaceIndex,
  848. zTexture);
  849. }
  850. void Framework::ShaderHeap::setRegisterInput(
  851. DXBuffer* zBuffer, int registerIndex, int spaceIndex)
  852. {
  853. setRegisterInput(DX12_SHADER_REGISTER_B_CONST_BUFFER,
  854. registerIndex,
  855. spaceIndex,
  856. zBuffer);
  857. }
  858. void Framework::ShaderHeap::setRegisterInput(
  859. DX12TLAS* zTLAS, int registerIndex, int spaceIndex)
  860. {
  861. setRegisterInput(DX12_SHADER_REGISTER_T_SHADER_RESOURCE,
  862. registerIndex,
  863. spaceIndex,
  864. zTLAS);
  865. }
  866. void Framework::ShaderHeap::updateDescriptorHeap(ID3D12Device5* zDevice)
  867. {
  868. const Array<DX12ShaderRegisterUsage>& registerUsages
  869. = signature->getRegisterUsagesOrder();
  870. if (!descriptorHeap
  871. || lastDescriptorHeapSize
  872. != signature->getRegisterUsagesOrder().getEntryCount())
  873. {
  874. if (descriptorHeap)
  875. {
  876. descriptorHeap->Release();
  877. }
  878. D3D12_DESCRIPTOR_HEAP_DESC desc = {};
  879. desc.NumDescriptors = registerUsages.getEntryCount();
  880. desc.Type = D3D12_DESCRIPTOR_HEAP_TYPE_CBV_SRV_UAV;
  881. desc.Flags = D3D12_DESCRIPTOR_HEAP_FLAG_SHADER_VISIBLE;
  882. desc.NodeMask = 0;
  883. HRESULT r = zDevice->CreateDescriptorHeap(
  884. &desc, __uuidof(ID3D12DescriptorHeap), (void**)&descriptorHeap);
  885. lastDescriptorHeapSize = registerUsages.getEntryCount();
  886. }
  887. D3D12_CPU_DESCRIPTOR_HANDLE descriptorHeapHandle
  888. = descriptorHeap->GetCPUDescriptorHandleForHeapStart();
  889. ArrayIterator<DX12ShaderRegisterUsage> it = registerUsages.begin();
  890. while (it)
  891. {
  892. const DX12ShaderRegisterUsage& usage = it.val();
  893. ArrayIterator<DX12ShaderRegisterInput*> inputIt
  894. = registerInputs.begin();
  895. bool found = 0;
  896. while (inputIt)
  897. {
  898. if (inputIt->registerType == usage.registerType
  899. && inputIt->registerIndex == usage.registerIndex
  900. && inputIt->spaceIndex == usage.spaceIndex)
  901. {
  902. switch (inputIt->registerType)
  903. {
  904. case DX12_SHADER_REGISTER_B_CONST_BUFFER:
  905. D3D12_CONSTANT_BUFFER_VIEW_DESC cbvDesc = {};
  906. DX12Buffer* buffer
  907. = dynamic_cast<DX12Buffer*>(inputIt->inputResource);
  908. cbvDesc.BufferLocation
  909. = buffer->zBuffer()->GetGPUVirtualAddress();
  910. cbvDesc.SizeInBytes = buffer->getElementCount()
  911. * buffer->getElementLength();
  912. zDevice->CreateConstantBufferView(
  913. &cbvDesc, descriptorHeapHandle);
  914. break;
  915. case DX12_SHADER_REGISTER_T_SHADER_RESOURCE:
  916. D3D12_SHADER_RESOURCE_VIEW_DESC srvDesc;
  917. srvDesc.Format = DXGI_FORMAT_UNKNOWN;
  918. srvDesc.ViewDimension
  919. = D3D12_SRV_DIMENSION_RAYTRACING_ACCELERATION_STRUCTURE;
  920. srvDesc.Shader4ComponentMapping
  921. = D3D12_DEFAULT_SHADER_4_COMPONENT_MAPPING;
  922. srvDesc.RaytracingAccelerationStructure.Location
  923. = dynamic_cast<DX12TLAS*>(inputIt->inputResource)
  924. ->zResultBuffer()
  925. ->zBuffer()
  926. ->GetGPUVirtualAddress();
  927. zDevice->CreateShaderResourceView(
  928. 0, &srvDesc, descriptorHeapHandle);
  929. break;
  930. case DX12_SHADER_REGISTER_U_UNORDERED_ACCESS:
  931. D3D12_UNORDERED_ACCESS_VIEW_DESC uavDesc = {};
  932. uavDesc.ViewDimension = D3D12_UAV_DIMENSION_TEXTURE2D;
  933. uavDesc.Format = DXGI_FORMAT_UNKNOWN;
  934. uavDesc.Texture2D.MipSlice = 0;
  935. uavDesc.Texture2D.PlaneSlice = 0;
  936. zDevice->CreateUnorderedAccessView(
  937. dynamic_cast<DX12Texture*>(inputIt->inputResource)
  938. ->zResource(),
  939. 0,
  940. &uavDesc,
  941. descriptorHeapHandle);
  942. break;
  943. default:
  944. Logging::error()
  945. << "Unknown register type for descriptor heap: "
  946. << inputIt->registerType;
  947. }
  948. found = 1;
  949. break;
  950. }
  951. ++inputIt;
  952. }
  953. if (!found)
  954. {
  955. Logging::error()
  956. << "No input resource found for register type "
  957. << usage.registerType << ", register index "
  958. << usage.registerIndex << ", space index " << usage.spaceIndex
  959. << ". This will result in an uninitialized descriptor in the "
  960. "descriptor heap. Access to the register in the shader "
  961. "might lead to undefined behaviour.";
  962. }
  963. descriptorHeapHandle.ptr += zDevice->GetDescriptorHandleIncrementSize(
  964. D3D12_DESCRIPTOR_HEAP_TYPE_CBV_SRV_UAV);
  965. ++it;
  966. }
  967. }
  968. ID3D12DescriptorHeap* Framework::ShaderHeap::zDescriptorHeap() const
  969. {
  970. return descriptorHeap;
  971. }