DX12Shader.cpp 60 KB

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