DX12Shader.cpp 57 KB

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