| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683 |
- #include "DX12Shader.h"
- #include "DX12CommandQueue.h"
- #include "DX12Texture.h"
- #include "DX12TLAS.h"
- #include "Logging.h"
- using namespace Framework;
- Framework::DX12ShaderSignature::DX12ShaderSignature()
- : ReferenceCounter(),
- signature(0),
- changed(1),
- shaderBindingTableParamCount(0)
- {}
- Framework::DX12ShaderSignature::~DX12ShaderSignature()
- {
- if (signature)
- {
- signature->Release();
- }
- for (DX12ShaderRegisterUsage* usage : descriptorHeapBindings)
- {
- delete usage;
- }
- for (DX12ShaderRegisterUsage* usage : bindingTableBindings)
- {
- delete usage;
- }
- }
- int* Framework::DX12ShaderSignature::addRegisterUsageLinkedToShaderBindingTable(
- DX12ShaderRegister registerType, int registerIndex, int spaceIndex)
- {
- for (DX12ShaderRegisterUsage* usage : descriptorHeapBindings)
- {
- if (usage->registerType == registerType
- && usage->registerIndex == registerIndex
- && usage->spaceIndex == spaceIndex)
- {
- Logging::error()
- << "Duplicate register usage in root signature: "
- << registerType << " " << registerIndex << " " << spaceIndex;
- throw std::invalid_argument(
- "Duplicate register usage in root signature");
- }
- }
- for (DX12ShaderRegisterUsage* usage : bindingTableBindings)
- {
- if (usage->registerType == registerType
- && usage->registerIndex == registerIndex
- && usage->spaceIndex == spaceIndex)
- {
- Logging::error()
- << "Duplicate register usage in root signature: "
- << registerType << " " << registerIndex << " " << spaceIndex;
- throw std::invalid_argument(
- "Duplicate register usage in root signature");
- }
- }
- DX12ShaderRegisterUsage* usage = new DX12ShaderRegisterUsage{
- registerType, registerIndex, spaceIndex, -1, 0};
- bindingTableBindings.add(usage);
- changed = 1;
- return &usage->bindingTableIndex;
- }
- void Framework::DX12ShaderSignature::addRegisterUsageLinkedToDescriptorHeap(
- int descriptorHeapIndex,
- DX12ShaderRegister registerType,
- int registerIndex,
- int spaceIndex)
- {
- if (descriptorHeapIndex < 0)
- {
- Logging::error() << "descriptorHeapIndex can not be below 0";
- throw std::invalid_argument("descriptorHeapIndex can not be below 0");
- }
- for (DX12ShaderRegisterUsage* usage : descriptorHeapBindings)
- {
- if (usage->registerType == registerType
- && usage->registerIndex == registerIndex
- && usage->spaceIndex == spaceIndex)
- {
- Logging::error()
- << "Duplicate register usage in root signature: "
- << registerType << " " << registerIndex << " " << spaceIndex;
- throw std::invalid_argument(
- "Duplicate register usage in root signature");
- }
- }
- // descriptor heap bindings sould be sorted by registerType -> spaceIndex ->
- // registerIndex
- ArrayIterator<DX12ShaderRegisterUsage*> it = descriptorHeapBindings.begin();
- bool found = 0;
- while (it)
- {
- if (it->registerType > registerType)
- {
- found = 1;
- break;
- }
- if (it->registerType == registerType)
- {
- if (it->spaceIndex > spaceIndex)
- {
- found = 1;
- break;
- }
- if (it->spaceIndex == spaceIndex)
- {
- if (it->registerIndex >= registerIndex)
- {
- found = 1;
- break;
- }
- }
- }
- ++it;
- }
- if (found)
- {
- if (it->registerIndex == registerIndex)
- {
- Logging::error()
- << "Duplicate register usage in root signature: "
- << registerType << " " << registerIndex << " " << spaceIndex;
- throw std::invalid_argument(
- "Duplicate register usage in root signature");
- }
- it.addBefore(new DX12ShaderRegisterUsage{
- registerType, registerIndex, spaceIndex, descriptorHeapIndex, 0});
- }
- else
- {
- descriptorHeapBindings.add(new DX12ShaderRegisterUsage{
- registerType, registerIndex, spaceIndex, descriptorHeapIndex, 0});
- }
- changed = 1;
- }
- void Framework::DX12ShaderSignature::createSignature(ID3D12Device5* zDevice,
- PFN_D3D12_SERIALIZE_ROOT_SIGNATURE pfnD3D12SerializeRootSignature)
- {
- if (!changed)
- {
- return;
- }
- changed = 0;
- if (signature)
- {
- signature->Release();
- signature = 0;
- }
- int paramCount = (descriptorHeapBindings.getEntryCount() > 0 ? 1 : 0)
- + bindingTableBindings.getEntryCount();
- D3D12_ROOT_PARAMETER* descriptorTable
- = new D3D12_ROOT_PARAMETER[paramCount];
- int index = 0;
- D3D12_DESCRIPTOR_RANGE* descriptorRanges = 0;
- if (descriptorHeapBindings.getEntryCount())
- {
- descriptorTable[0].ParameterType
- = D3D12_ROOT_PARAMETER_TYPE_DESCRIPTOR_TABLE;
- descriptorTable[0].ShaderVisibility = D3D12_SHADER_VISIBILITY_ALL;
- descriptorRanges = new D3D12_DESCRIPTOR_RANGE[descriptorHeapBindings
- .getEntryCount()];
- ArrayIterator<DX12ShaderRegisterUsage*> it
- = descriptorHeapBindings.begin();
- while (it)
- {
- it->bindingTableIndex = 0;
- D3D12_DESCRIPTOR_RANGE& range = descriptorRanges[index];
- switch (it->registerType)
- {
- case DX12_SHADER_REGISTER_B_CONST_BUFFER:
- range.RangeType = D3D12_DESCRIPTOR_RANGE_TYPE_CBV;
- break;
- case DX12_SHADER_REGISTER_T_SHADER_RESOURCE:
- range.RangeType = D3D12_DESCRIPTOR_RANGE_TYPE_SRV;
- break;
- case DX12_SHADER_REGISTER_U_UNORDERED_ACCESS:
- range.RangeType = D3D12_DESCRIPTOR_RANGE_TYPE_UAV;
- break;
- }
- range.BaseShaderRegister = it->registerIndex;
- range.OffsetInDescriptorsFromTableStart = it->descriptorHeapIndex;
- range.RegisterSpace = it->spaceIndex;
- ArrayIterator<DX12ShaderRegisterUsage*> next = it.next();
- int size = 1;
- while (
- next && next->registerType == it->registerType
- && next->spaceIndex == it->spaceIndex
- && next->registerIndex == it->registerIndex + size
- && next->descriptorHeapIndex == it->descriptorHeapIndex + size)
- {
- ++size;
- it = next;
- it->bindingTableIndex = 0;
- ++next;
- }
- range.NumDescriptors = size;
- ++it;
- ++index;
- }
- descriptorTable[0].DescriptorTable.pDescriptorRanges = descriptorRanges;
- descriptorTable[0].DescriptorTable.NumDescriptorRanges = index;
- index = 1;
- }
- for (DX12ShaderRegisterUsage* usage : bindingTableBindings)
- {
- usage->bindingTableIndex = index;
- switch (usage->registerType)
- {
- case DX12_SHADER_REGISTER_B_CONST_BUFFER:
- descriptorTable[index].ParameterType
- = D3D12_ROOT_PARAMETER_TYPE_CBV;
- break;
- case DX12_SHADER_REGISTER_T_SHADER_RESOURCE:
- descriptorTable[index].ParameterType
- = D3D12_ROOT_PARAMETER_TYPE_SRV;
- break;
- case DX12_SHADER_REGISTER_U_UNORDERED_ACCESS:
- descriptorTable[index].ParameterType
- = D3D12_ROOT_PARAMETER_TYPE_UAV;
- break;
- }
- descriptorTable[index].ShaderVisibility = D3D12_SHADER_VISIBILITY_ALL;
- descriptorTable[index].Descriptor.ShaderRegister = usage->registerIndex;
- descriptorTable[index].Descriptor.RegisterSpace = usage->spaceIndex;
- ++index;
- }
- shaderBindingTableParamCount = index;
- D3D12_ROOT_SIGNATURE_DESC rootDesc = {};
- rootDesc.NumParameters = shaderBindingTableParamCount;
- rootDesc.pParameters = descriptorTable;
- rootDesc.Flags = D3D12_ROOT_SIGNATURE_FLAG_LOCAL_ROOT_SIGNATURE;
- ID3DBlob* pSigBlob = 0;
- ID3DBlob* pErrorBlob = 0;
- HRESULT hr = pfnD3D12SerializeRootSignature(
- &rootDesc, D3D_ROOT_SIGNATURE_VERSION_1_0, &pSigBlob, &pErrorBlob);
- if (pSigBlob)
- {
- zDevice->CreateRootSignature(0,
- pSigBlob->GetBufferPointer(),
- pSigBlob->GetBufferSize(),
- __uuidof(ID3D12RootSignature),
- (void**)&signature);
- pSigBlob->Release();
- }
- if (pErrorBlob)
- {
- std::string errorMessage(
- static_cast<const char*>(pErrorBlob->GetBufferPointer()),
- pErrorBlob->GetBufferSize());
- Logging::error() << "Failed to serialize root signature: "
- << errorMessage;
- pErrorBlob->Release();
- }
- delete[] descriptorRanges;
- delete[] descriptorTable;
- }
- ID3D12RootSignature* Framework::DX12ShaderSignature::zSignature() const
- {
- return signature;
- }
- const Array<DX12ShaderRegisterUsage*>&
- Framework::DX12ShaderSignature::getDescriptorHeapBindings() const
- {
- return descriptorHeapBindings;
- }
- int Framework::DX12ShaderSignature::gerShaderBindingTableParamCount() const
- {
- return shaderBindingTableParamCount;
- }
- Framework::DX12ShaderFunction::DX12ShaderFunction(const Text& functionName,
- DX12ShaderSignature* signature,
- DX12ShaderFunctionType functionType)
- : ReferenceCounter(),
- functionName(functionName),
- signature(signature),
- functionType(functionType),
- exportDesc(new D3D12_EXPORT_DESC())
- {
- wchar_t* wc = new wchar_t[functionName.getLength() + 1];
- mbstowcs_s(0,
- wc,
- functionName.getLength() + 1,
- functionName.getText(),
- functionName.getLength() + 1);
- wc[functionName.getLength()] = 0;
- exportDesc->Name = wc;
- exportDesc->ExportToRename = 0;
- exportDesc->Flags = D3D12_EXPORT_FLAG_NONE;
- }
- Framework::DX12ShaderFunction::~DX12ShaderFunction()
- {
- signature->release();
- delete[] exportDesc->Name;
- delete exportDesc;
- }
- const Text& Framework::DX12ShaderFunction::getFunctionName() const
- {
- return functionName;
- }
- DX12ShaderSignature* Framework::DX12ShaderFunction::zSignature() const
- {
- return signature;
- }
- D3D12_EXPORT_DESC* Framework::DX12ShaderFunction::zExportDesc() const
- {
- return exportDesc;
- }
- DX12ShaderFunctionType Framework::DX12ShaderFunction::getFunctionType() const
- {
- return functionType;
- }
- Framework::DX12Shader::DX12Shader(
- const unsigned char* shaderBytes, int shaderBytesSize)
- : ReferenceCounter(),
- shaderBytes(shaderBytes),
- shaderBytesSize(shaderBytesSize),
- libraryDesc(new D3D12_DXIL_LIBRARY_DESC())
- {
- libraryDesc->DXILLibrary.pShaderBytecode = shaderBytes;
- libraryDesc->DXILLibrary.BytecodeLength = shaderBytesSize;
- libraryDesc->NumExports = 0;
- libraryDesc->pExports = 0;
- }
- Framework::DX12Shader::~DX12Shader()
- {
- delete[] libraryDesc->pExports;
- delete libraryDesc;
- }
- void Framework::DX12Shader::addFunction(DX12ShaderFunction* function)
- {
- functions.add(function);
- }
- int Framework::DX12Shader::getShaderBytesSize() const
- {
- return shaderBytesSize;
- }
- const unsigned char* Framework::DX12Shader::getShaderBytes() const
- {
- return shaderBytes;
- }
- const RCArray<DX12ShaderFunction>& Framework::DX12Shader::getFunctions() const
- {
- return functions;
- }
- D3D12_DXIL_LIBRARY_DESC* Framework::DX12Shader::zLibraryDesc() const
- {
- if (libraryDesc->NumExports != functions.getEntryCount())
- {
- delete[] libraryDesc->pExports;
- libraryDesc->NumExports = functions.getEntryCount();
- D3D12_EXPORT_DESC* pExports
- = new D3D12_EXPORT_DESC[functions.getEntryCount()];
- int index = 0;
- for (const auto& function : functions)
- {
- memcpy(pExports + index,
- function->zExportDesc(),
- sizeof(D3D12_EXPORT_DESC));
- index++;
- }
- libraryDesc->pExports = pExports;
- }
- return libraryDesc;
- }
- Framework::DX12ShaderHitGroup::DX12ShaderHitGroup(const Text name)
- : ReferenceCounter(),
- name(name),
- closestHitShaderFunction(0),
- anyHitShaderFunction(0),
- intersectionShaderFunction(0),
- payloadSize(0),
- attributeSize(0),
- hitGroupDesc(new D3D12_HIT_GROUP_DESC())
- {
- wchar_t* wc = new wchar_t[name.getLength() + 1];
- mbstowcs_s(
- 0, wc, name.getLength() + 1, name.getText(), name.getLength() + 1);
- wc[name.getLength()] = 0;
- hitGroupDesc->HitGroupExport = wc;
- hitGroupDesc->IntersectionShaderImport = 0;
- hitGroupDesc->AnyHitShaderImport = 0;
- hitGroupDesc->ClosestHitShaderImport = 0;
- }
- Framework::DX12ShaderHitGroup::~DX12ShaderHitGroup()
- {
- delete[] hitGroupDesc->HitGroupExport;
- delete hitGroupDesc;
- if (closestHitShaderFunction)
- {
- closestHitShaderFunction->release();
- }
- if (anyHitShaderFunction)
- {
- anyHitShaderFunction->release();
- }
- if (intersectionShaderFunction)
- {
- intersectionShaderFunction->release();
- }
- }
- void Framework::DX12ShaderHitGroup::setClosestHitShaderFunction(
- DX12ShaderFunction* closestHitShaderFunction)
- {
- if (anyHitShaderFunction
- && anyHitShaderFunction->zSignature()
- != closestHitShaderFunction->zSignature())
- {
- Logging::error()
- << "Any-hit shader function and closest-hit shader "
- "function must have the same root signature when they are "
- "combined in the same hit group. HitGroup Name: '"
- << name << "' Any Hit Shader Function: '"
- << anyHitShaderFunction->getFunctionName().getText()
- << "' Closest Hit Shader Function: '"
- << closestHitShaderFunction->getFunctionName().getText() << "'";
- throw std::runtime_error("Incompatible root signatures in hit group");
- }
- if (intersectionShaderFunction
- && intersectionShaderFunction->zSignature()
- != closestHitShaderFunction->zSignature())
- {
- Logging::error()
- << "Intersection shader function and closest-hit shader "
- "function must have the same root signature when they are "
- "combined in the same hit group. HitGroup Name: '"
- << name << "' Intersection Shader Function: '"
- << intersectionShaderFunction->getFunctionName().getText()
- << "' Closest Hit Shader Function: '"
- << closestHitShaderFunction->getFunctionName().getText() << "'";
- throw std::runtime_error("Incompatible root signatures in hit group");
- }
- if (this->closestHitShaderFunction)
- {
- this->closestHitShaderFunction->release();
- }
- this->closestHitShaderFunction = closestHitShaderFunction;
- hitGroupDesc->ClosestHitShaderImport = 0;
- if (closestHitShaderFunction)
- {
- hitGroupDesc->ClosestHitShaderImport
- = closestHitShaderFunction->zExportDesc()->Name;
- }
- }
- void Framework::DX12ShaderHitGroup::setAnyHitShaderFunction(
- DX12ShaderFunction* anyHitShaderFunction)
- {
- if (closestHitShaderFunction
- && closestHitShaderFunction->zSignature()
- != anyHitShaderFunction->zSignature())
- {
- Logging::error()
- << "Any-hit shader function and closest-hit shader "
- "function must have the same root signature when they are "
- "combined in the same hit group. HitGroup Name: '"
- << name << "' Any Hit Shader Function: '"
- << anyHitShaderFunction->getFunctionName().getText()
- << "' Closest Hit Shader Function: '"
- << closestHitShaderFunction->getFunctionName().getText() << "'";
- throw std::runtime_error("Incompatible root signatures in hit group");
- }
- if (intersectionShaderFunction
- && intersectionShaderFunction->zSignature()
- != anyHitShaderFunction->zSignature())
- {
- Logging::error()
- << "Intersection shader function and any-hit shader "
- "function must have the same root signature when they are "
- "combined in the same hit group. HitGroup Name: '"
- << name << "' Intersection Shader Function: '"
- << intersectionShaderFunction->getFunctionName().getText()
- << "' Any Hit Shader Function: '"
- << anyHitShaderFunction->getFunctionName().getText() << "'";
- throw std::runtime_error("Incompatible root signatures in hit group");
- }
- if (this->anyHitShaderFunction)
- {
- this->anyHitShaderFunction->release();
- }
- this->anyHitShaderFunction = anyHitShaderFunction;
- hitGroupDesc->AnyHitShaderImport = 0;
- if (anyHitShaderFunction)
- {
- hitGroupDesc->AnyHitShaderImport
- = anyHitShaderFunction->zExportDesc()->Name;
- }
- }
- void Framework::DX12ShaderHitGroup::setIntersectionShaderFunction(
- DX12ShaderFunction* zIntersectionShaderFunction)
- {
- if (closestHitShaderFunction
- && closestHitShaderFunction->zSignature()
- != zIntersectionShaderFunction->zSignature())
- {
- Logging::error()
- << "Intersection shader function and closest-hit shader "
- "function must have the same root signature when they are "
- "combined in the same hit group. HitGroup Name: '"
- << name << "' Intersection Shader Function: '"
- << zIntersectionShaderFunction->getFunctionName().getText()
- << "' Closest Hit Shader Function: '"
- << closestHitShaderFunction->getFunctionName().getText() << "'";
- throw std::runtime_error("Incompatible root signatures in hit group");
- }
- if (anyHitShaderFunction
- && anyHitShaderFunction->zSignature()
- != zIntersectionShaderFunction->zSignature())
- {
- Logging::error()
- << "Intersection shader function and any-hit shader "
- "function must have the same root signature when they are "
- "combined in the same hit group. HitGroup Name: '"
- << name << "' Intersection Shader Function: '"
- << zIntersectionShaderFunction->getFunctionName().getText()
- << "' Any Hit Shader Function: '"
- << anyHitShaderFunction->getFunctionName().getText() << "'";
- throw std::runtime_error("Incompatible root signatures in hit group");
- }
- if (intersectionShaderFunction)
- {
- intersectionShaderFunction->release();
- }
- intersectionShaderFunction = dynamic_cast<DX12ShaderFunction*>(
- zIntersectionShaderFunction->getThis());
- hitGroupDesc->IntersectionShaderImport = 0;
- if (intersectionShaderFunction)
- {
- hitGroupDesc->IntersectionShaderImport
- = intersectionShaderFunction->zExportDesc()->Name;
- }
- }
- void Framework::DX12ShaderHitGroup::setPayloadSize(int payloadSize)
- {
- this->payloadSize = payloadSize;
- }
- void Framework::DX12ShaderHitGroup::setAttributeSize(int attributeSize)
- {
- this->attributeSize = attributeSize;
- }
- const Text& Framework::DX12ShaderHitGroup::getName() const
- {
- return name;
- }
- DX12ShaderFunction*
- Framework::DX12ShaderHitGroup::zClosestHitShaderFunction() const
- {
- return closestHitShaderFunction;
- }
- DX12ShaderFunction* Framework::DX12ShaderHitGroup::zAnyHitShaderFunction() const
- {
- return anyHitShaderFunction;
- }
- DX12ShaderFunction*
- Framework::DX12ShaderHitGroup::zIntersectionShaderFunction() const
- {
- return intersectionShaderFunction;
- }
- int Framework::DX12ShaderHitGroup::getPayloadSize() const
- {
- return payloadSize;
- }
- int Framework::DX12ShaderHitGroup::getAttributeSize() const
- {
- return attributeSize;
- }
- D3D12_HIT_GROUP_DESC* Framework::DX12ShaderHitGroup::zHitGroupDesc() const
- {
- if (hitGroupDesc->IntersectionShaderImport)
- {
- hitGroupDesc->Type = D3D12_HIT_GROUP_TYPE_PROCEDURAL_PRIMITIVE;
- }
- else
- {
- hitGroupDesc->Type = D3D12_HIT_GROUP_TYPE_TRIANGLES;
- }
- return hitGroupDesc;
- }
- DX12ShaderSignature* Framework::DX12ShaderHitGroup::zSignature() const
- {
- if (closestHitShaderFunction)
- {
- return closestHitShaderFunction->zSignature();
- }
- if (anyHitShaderFunction)
- {
- return anyHitShaderFunction->zSignature();
- }
- if (intersectionShaderFunction)
- {
- return intersectionShaderFunction->zSignature();
- }
- return 0;
- }
- Framework::DX12Pipeline::DX12Pipeline()
- : ReferenceCounter(),
- emptyGlobalRootSignature(0),
- emptyLocalRootSignature(0),
- pipelineState(0),
- maxRecursionDepth(0)
- {}
- Framework::DX12Pipeline::~DX12Pipeline()
- {
- if (emptyGlobalRootSignature)
- {
- emptyGlobalRootSignature->Release();
- }
- if (emptyLocalRootSignature)
- {
- emptyLocalRootSignature->Release();
- }
- if (pipelineState)
- {
- pipelineState->Release();
- }
- }
- void Framework::DX12Pipeline::addShader(DX12Shader* shader)
- {
- shaders.add(shader);
- }
- void Framework::DX12Pipeline::addHitGroup(DX12ShaderHitGroup* hitGroup)
- {
- hitGroups.add(hitGroup);
- }
- void Framework::DX12Pipeline::setMaxRecursionDepth(int maxRecursionDepth)
- {
- this->maxRecursionDepth = maxRecursionDepth;
- }
- void Framework::DX12Pipeline::createPipelineState(ID3D12Device5* zDevice,
- PFN_D3D12_SERIALIZE_ROOT_SIGNATURE pfnD3D12SerializeRootSignature)
- {
- if (!emptyGlobalRootSignature)
- {
- D3D12_ROOT_SIGNATURE_DESC rootDesc = {};
- rootDesc.NumParameters = 0;
- rootDesc.pParameters = 0;
- rootDesc.Flags = D3D12_ROOT_SIGNATURE_FLAG_NONE;
- ID3DBlob* pSigBlob = 0;
- ID3DBlob* pErrorBlob = 0;
- HRESULT hr = pfnD3D12SerializeRootSignature(
- &rootDesc, D3D_ROOT_SIGNATURE_VERSION_1_0, &pSigBlob, &pErrorBlob);
- if (pSigBlob)
- {
- zDevice->CreateRootSignature(0,
- pSigBlob->GetBufferPointer(),
- pSigBlob->GetBufferSize(),
- __uuidof(ID3D12RootSignature),
- (void**)&emptyGlobalRootSignature);
- pSigBlob->Release();
- }
- if (pErrorBlob)
- {
- std::string errorMessage(
- static_cast<const char*>(pErrorBlob->GetBufferPointer()),
- pErrorBlob->GetBufferSize());
- Logging::error()
- << "Failed to serialize empty root signature: " << errorMessage;
- pErrorBlob->Release();
- }
- }
- if (!emptyLocalRootSignature)
- {
- D3D12_ROOT_SIGNATURE_DESC rootDesc = {};
- rootDesc.NumParameters = 0;
- rootDesc.pParameters = 0;
- rootDesc.Flags = D3D12_ROOT_SIGNATURE_FLAG_LOCAL_ROOT_SIGNATURE;
- ID3DBlob* pSigBlob = 0;
- ID3DBlob* pErrorBlob = 0;
- HRESULT hr = pfnD3D12SerializeRootSignature(
- &rootDesc, D3D_ROOT_SIGNATURE_VERSION_1_0, &pSigBlob, &pErrorBlob);
- if (pSigBlob)
- {
- zDevice->CreateRootSignature(0,
- pSigBlob->GetBufferPointer(),
- pSigBlob->GetBufferSize(),
- __uuidof(ID3D12RootSignature),
- (void**)&emptyLocalRootSignature);
- pSigBlob->Release();
- }
- if (pErrorBlob)
- {
- std::string errorMessage(
- static_cast<const char*>(pErrorBlob->GetBufferPointer()),
- pErrorBlob->GetBufferSize());
- Logging::error()
- << "Failed to serialize empty root signature: " << errorMessage;
- pErrorBlob->Release();
- }
- }
- unsigned int subobjectCount
- = shaders.getEntryCount() + hitGroups.getEntryCount() + 5;
- Array<DX12ShaderSignature*> distinctSignatures;
- for (const DX12Shader* shader : shaders)
- {
- for (const DX12ShaderFunction* function : shader->getFunctions())
- {
- DX12ShaderSignature* signature = function->zSignature();
- if (distinctSignatures.getValueIndex(signature) < 0)
- {
- distinctSignatures.add(signature);
- }
- }
- }
- subobjectCount += distinctSignatures.getEntryCount()
- * 2; // Local root signatures for each distinct signature
- D3D12_STATE_SUBOBJECT* subobjects
- = new D3D12_STATE_SUBOBJECT[subobjectCount];
- int index = 0;
- for (const DX12Shader* shader : shaders)
- {
- subobjects[index].Type = D3D12_STATE_SUBOBJECT_TYPE_DXIL_LIBRARY;
- subobjects[index].pDesc = shader->zLibraryDesc();
- index++;
- }
- int maxPayloadSize = 0;
- int maxAttributeSize = 0;
- for (const DX12ShaderHitGroup* hitGroup : hitGroups)
- {
- subobjects[index].Type = D3D12_STATE_SUBOBJECT_TYPE_HIT_GROUP;
- subobjects[index].pDesc = hitGroup->zHitGroupDesc();
- if (hitGroup->getPayloadSize() > maxPayloadSize)
- {
- maxPayloadSize = hitGroup->getPayloadSize();
- }
- if (hitGroup->getAttributeSize() > maxAttributeSize)
- {
- maxAttributeSize = hitGroup->getAttributeSize();
- }
- index++;
- }
- D3D12_RAYTRACING_SHADER_CONFIG shaderDesc = {};
- shaderDesc.MaxPayloadSizeInBytes = maxPayloadSize;
- shaderDesc.MaxAttributeSizeInBytes = maxAttributeSize;
- subobjects[index].Type
- = D3D12_STATE_SUBOBJECT_TYPE_RAYTRACING_SHADER_CONFIG;
- subobjects[index].pDesc = &shaderDesc;
- index++;
- functionsWithoutHitGroups.clear();
- // create export list for hole pipeline with all hitgroups and all functions
- // that are not part of a hit group
- for (const DX12Shader* shader : shaders)
- {
- for (const DX12ShaderFunction* function : shader->getFunctions())
- {
- bool found = 0;
- for (const DX12ShaderHitGroup* hitGroup : hitGroups)
- {
- if (hitGroup->zClosestHitShaderFunction() == function
- || hitGroup->zAnyHitShaderFunction() == function
- || hitGroup->zIntersectionShaderFunction() == function)
- {
- found = 1;
- break;
- }
- }
- if (!found)
- {
- functionsWithoutHitGroups.add(function);
- }
- }
- }
- const wchar_t** functionAndHitGroupNames
- = new const wchar_t*[functionsWithoutHitGroups.getEntryCount()
- + hitGroups.getEntryCount()];
- int nameIndex = 0;
- for (const DX12ShaderFunction* function : functionsWithoutHitGroups)
- {
- functionAndHitGroupNames[nameIndex]
- = const_cast<wchar_t*>(function->zExportDesc()->Name);
- nameIndex++;
- }
- for (const DX12ShaderHitGroup* hitGroup : hitGroups)
- {
- functionAndHitGroupNames[nameIndex]
- = const_cast<wchar_t*>(hitGroup->zHitGroupDesc()->HitGroupExport);
- nameIndex++;
- }
- D3D12_SUBOBJECT_TO_EXPORTS_ASSOCIATION shaderPayloadAssociation = {};
- shaderPayloadAssociation.NumExports
- = functionsWithoutHitGroups.getEntryCount() + hitGroups.getEntryCount();
- shaderPayloadAssociation.pExports = functionAndHitGroupNames;
- shaderPayloadAssociation.pSubobjectToAssociate
- = &subobjects[index - 1]; // shader config
- subobjects[index].Type
- = D3D12_STATE_SUBOBJECT_TYPE_SUBOBJECT_TO_EXPORTS_ASSOCIATION;
- subobjects[index].pDesc = &shaderPayloadAssociation;
- index++;
- D3D12_LOCAL_ROOT_SIGNATURE* localRootSignatures
- = new D3D12_LOCAL_ROOT_SIGNATURE[distinctSignatures.getEntryCount()];
- int rootSignatureIndex = 0;
- const wchar_t*** rootSignatureExports
- = new const wchar_t**[distinctSignatures.getEntryCount()];
- D3D12_SUBOBJECT_TO_EXPORTS_ASSOCIATION* localRootAssociations
- = new D3D12_SUBOBJECT_TO_EXPORTS_ASSOCIATION[distinctSignatures
- .getEntryCount()];
- for (DX12ShaderSignature* signature : distinctSignatures)
- {
- signature->createSignature(zDevice, pfnD3D12SerializeRootSignature);
- subobjects[index].Type
- = D3D12_STATE_SUBOBJECT_TYPE_LOCAL_ROOT_SIGNATURE;
- localRootSignatures[rootSignatureIndex].pLocalRootSignature
- = signature->zSignature();
- subobjects[index].pDesc = &localRootSignatures[rootSignatureIndex];
- index++;
- Array<const DX12ShaderFunction*> functionsWithThisSignature;
- for (const DX12Shader* shader : shaders)
- {
- for (const DX12ShaderFunction* function : shader->getFunctions())
- {
- if (function->zSignature() == signature)
- {
- bool found = 0;
- for (DX12ShaderHitGroup* hitGroup : hitGroups)
- {
- if (hitGroup->zClosestHitShaderFunction() == function
- || hitGroup->zAnyHitShaderFunction() == function
- || hitGroup->zIntersectionShaderFunction()
- == function)
- {
- found = 1;
- break;
- }
- }
- if (!found)
- {
- functionsWithThisSignature.add(function);
- }
- }
- }
- }
- Array<const DX12ShaderHitGroup*> hitGroupsWithThisSignature;
- for (const DX12ShaderHitGroup* hitGroup : hitGroups)
- {
- if (hitGroup->zClosestHitShaderFunction()
- && hitGroup->zClosestHitShaderFunction()->zSignature()
- == signature)
- {
- hitGroupsWithThisSignature.add(hitGroup);
- }
- else if (hitGroup->zAnyHitShaderFunction()
- && hitGroup->zAnyHitShaderFunction()->zSignature()
- == signature)
- {
- hitGroupsWithThisSignature.add(hitGroup);
- }
- else if (hitGroup->zIntersectionShaderFunction()
- && hitGroup->zIntersectionShaderFunction()->zSignature()
- == signature)
- {
- hitGroupsWithThisSignature.add(hitGroup);
- }
- }
- rootSignatureExports[rootSignatureIndex]
- = new const wchar_t*[functionsWithThisSignature.getEntryCount()
- + hitGroupsWithThisSignature.getEntryCount()];
- int nameIndex = 0;
- for (const DX12ShaderFunction* function : functionsWithThisSignature)
- {
- rootSignatureExports[rootSignatureIndex][nameIndex]
- = function->zExportDesc()->Name;
- nameIndex++;
- }
- for (const DX12ShaderHitGroup* hitGroup : hitGroupsWithThisSignature)
- {
- rootSignatureExports[rootSignatureIndex][nameIndex]
- = hitGroup->zHitGroupDesc()->HitGroupExport;
- nameIndex++;
- }
- localRootAssociations[rootSignatureIndex].NumExports
- = functionsWithThisSignature.getEntryCount()
- + hitGroupsWithThisSignature.getEntryCount();
- localRootAssociations[rootSignatureIndex].pExports
- = rootSignatureExports[rootSignatureIndex];
- localRootAssociations[rootSignatureIndex].pSubobjectToAssociate
- = &subobjects[index - 1]; // local root signature
- subobjects[index].Type
- = D3D12_STATE_SUBOBJECT_TYPE_SUBOBJECT_TO_EXPORTS_ASSOCIATION;
- subobjects[index].pDesc = &localRootAssociations[rootSignatureIndex];
- index++;
- rootSignatureIndex++;
- }
- subobjects[index].Type = D3D12_STATE_SUBOBJECT_TYPE_GLOBAL_ROOT_SIGNATURE;
- subobjects[index].pDesc = &emptyGlobalRootSignature;
- index++;
- subobjects[index].Type = D3D12_STATE_SUBOBJECT_TYPE_LOCAL_ROOT_SIGNATURE;
- subobjects[index].pDesc = &emptyLocalRootSignature;
- index++;
- D3D12_RAYTRACING_PIPELINE_CONFIG pipelineConfig = {};
- pipelineConfig.MaxTraceRecursionDepth = maxRecursionDepth;
- subobjects[index].Type
- = D3D12_STATE_SUBOBJECT_TYPE_RAYTRACING_PIPELINE_CONFIG;
- subobjects[index].pDesc = &pipelineConfig;
- index++;
- D3D12_STATE_OBJECT_DESC pipelineDesc = {};
- pipelineDesc.Type = D3D12_STATE_OBJECT_TYPE_RAYTRACING_PIPELINE;
- pipelineDesc.NumSubobjects = subobjectCount;
- pipelineDesc.pSubobjects = subobjects;
- HRESULT hr = zDevice->CreateStateObject(
- &pipelineDesc, __uuidof(ID3D12StateObject), (void**)&pipelineState);
- if (FAILED(hr))
- {
- Logging::error()
- << "Failed to create raytracing pipeline state object: " << std::hex
- << hr;
- throw std::logic_error("Could not create the raytracing state object");
- }
- delete[] functionAndHitGroupNames;
- delete[] localRootSignatures;
- for (int i = 0; i < distinctSignatures.getEntryCount(); i++)
- {
- delete[] rootSignatureExports[i];
- }
- delete[] rootSignatureExports;
- delete[] localRootAssociations;
- }
- ID3D12StateObject* Framework::DX12Pipeline::zPipelineState() const
- {
- return pipelineState;
- }
- DX12ShaderBindingTable* Framework::DX12Pipeline::createShaderBindingTable()
- {
- return new DX12ShaderBindingTable(dynamic_cast<DX12Pipeline*>(getThis()));
- }
- DX12GlobalDescriptorHeap* Framework::DX12Pipeline::createGlobalDescriptorHeap()
- {
- return new DX12GlobalDescriptorHeap(dynamic_cast<DX12Pipeline*>(getThis()));
- }
- const RCArray<DX12Shader>& Framework::DX12Pipeline::getShaders() const
- {
- return shaders;
- }
- const Array<const DX12ShaderFunction*>&
- Framework::DX12Pipeline::getFunctionsWithoutHitGroups() const
- {
- return functionsWithoutHitGroups;
- }
- const RCArray<DX12ShaderHitGroup>& Framework::DX12Pipeline::getHitGroups() const
- {
- return hitGroups;
- }
- Framework::DX12GlobalDescriptorHeap::DX12GlobalDescriptorHeap(
- DX12Pipeline* pipeline)
- : ReferenceCounter(),
- pipeline(pipeline),
- descriptorHeap(0),
- lastDescriptorHeapSize(0),
- zDevice(0)
- {}
- Framework::DX12GlobalDescriptorHeap::~DX12GlobalDescriptorHeap()
- {
- pipeline->release();
- if (descriptorHeap)
- {
- descriptorHeap->Release();
- }
- for (const DX12ShaderRegisterInput* input : registerInputs)
- {
- input->inputResource->release();
- delete input;
- }
- }
- void Framework::DX12GlobalDescriptorHeap::addInput(
- DX12ShaderRegister type, ReferenceCounter* inputResource)
- {
- bool found = 0;
- for (DX12Shader* shader : pipeline->getShaders())
- {
- for (DX12ShaderFunction* function : shader->getFunctions())
- {
- for (const DX12ShaderRegisterUsage* usage :
- function->zSignature()->getDescriptorHeapBindings())
- {
- if (usage->descriptorHeapIndex
- == registerInputs.getEntryCount())
- {
- if (usage->registerType != type)
- {
- Logging::error()
- << "Register type mismatch for register index "
- << usage->registerIndex << ", space index "
- << usage->spaceIndex << ". Expected register type: "
- << usage->registerType
- << ", given register type: " << type
- << ". The register type is specified in the "
- "signature of shader function '"
- << function->getFunctionName() << "'";
- throw std::logic_error(
- "Register type mismatch for shader input");
- }
- else
- {
- found = 1;
- break;
- }
- }
- }
- if (found)
- {
- break;
- }
- }
- if (found)
- {
- break;
- }
- }
- registerInputs.add(
- new DX12ShaderRegisterInput{type, inputResource->getThis()});
- }
- void Framework::DX12GlobalDescriptorHeap::addTextureInput(
- DX12ShaderRegister type, Texture* zTexture)
- {
- addInput(type, zTexture);
- }
- void Framework::DX12GlobalDescriptorHeap::updateTextureInput(
- int heapIndex, DX12ShaderRegister type, Texture* zTexture)
- {
- DX12ShaderRegisterInput* input = registerInputs.get(heapIndex);
- if (registerInputs.get(heapIndex)->inputResource
- != dynamic_cast<ReferenceCounter*>(zTexture))
- {
- if (input->registerType != type)
- {
- Logging::error()
- << "Register type mismatch for descriptor heap index "
- << heapIndex
- << ". Expected register type: " << input->registerType
- << ", given register type: " << type << ".";
- throw std::logic_error("Register type mismatch in descriptor heap");
- }
- registerInputs.get(heapIndex)->inputResource->release();
- registerInputs.get(heapIndex)->inputResource = zTexture->getThis();
- if (descriptorHeap)
- {
- D3D12_CPU_DESCRIPTOR_HANDLE descriptorHeapHandle
- = descriptorHeap->GetCPUDescriptorHandleForHeapStart();
- descriptorHeapHandle.ptr
- += zDevice->GetDescriptorHandleIncrementSize(
- D3D12_DESCRIPTOR_HEAP_TYPE_CBV_SRV_UAV)
- * heapIndex;
- switch (input->registerType)
- {
- case DX12_SHADER_REGISTER_T_SHADER_RESOURCE:
- {
- D3D12_SHADER_RESOURCE_VIEW_DESC srvDesc;
- srvDesc.Format = DXGI_FORMAT_UNKNOWN;
- srvDesc.ViewDimension = D3D12_SRV_DIMENSION_TEXTURE2D;
- srvDesc.Texture2D.MipLevels = 0;
- srvDesc.Texture2D.MostDetailedMip = 0;
- srvDesc.Texture2D.PlaneSlice = 0;
- srvDesc.Texture2D.ResourceMinLODClamp = 0.0f;
- srvDesc.Shader4ComponentMapping
- = D3D12_DEFAULT_SHADER_4_COMPONENT_MAPPING;
- zDevice->CreateShaderResourceView(
- ((DX12Texture*)zTexture)->zResource(),
- &srvDesc,
- descriptorHeapHandle);
- break;
- }
- case DX12_SHADER_REGISTER_U_UNORDERED_ACCESS:
- {
- D3D12_UNORDERED_ACCESS_VIEW_DESC uavDesc = {};
- uavDesc.ViewDimension = D3D12_UAV_DIMENSION_TEXTURE2D;
- uavDesc.Format = DXGI_FORMAT_UNKNOWN;
- uavDesc.Texture2D.MipSlice = 0;
- uavDesc.Texture2D.PlaneSlice = 0;
- zDevice->CreateUnorderedAccessView(
- ((DX12Texture*)zTexture)->zResource(),
- 0,
- &uavDesc,
- descriptorHeapHandle);
- break;
- }
- }
- }
- }
- }
- void Framework::DX12GlobalDescriptorHeap::addBufferInput(
- DX12ShaderRegister type, DXBuffer* zBuffer)
- {
- addInput(type, zBuffer);
- }
- void Framework::DX12GlobalDescriptorHeap::addTLASInput(
- DX12ShaderRegister type, DX12TLAS* zTLAS)
- {
- addInput(type, zTLAS);
- }
- void Framework::DX12GlobalDescriptorHeap::updateDescriptorHeap(
- ID3D12Device5* zDevice)
- {
- this->zDevice = zDevice;
- if (!descriptorHeap
- || lastDescriptorHeapSize != registerInputs.getEntryCount())
- {
- if (descriptorHeap)
- {
- descriptorHeap->Release();
- }
- D3D12_DESCRIPTOR_HEAP_DESC desc = {};
- desc.NumDescriptors = registerInputs.getEntryCount();
- desc.Type = D3D12_DESCRIPTOR_HEAP_TYPE_CBV_SRV_UAV;
- desc.Flags = D3D12_DESCRIPTOR_HEAP_FLAG_SHADER_VISIBLE;
- desc.NodeMask = 0;
- HRESULT r = zDevice->CreateDescriptorHeap(
- &desc, __uuidof(ID3D12DescriptorHeap), (void**)&descriptorHeap);
- lastDescriptorHeapSize = registerInputs.getEntryCount();
- }
- D3D12_CPU_DESCRIPTOR_HANDLE descriptorHeapHandle
- = descriptorHeap->GetCPUDescriptorHandleForHeapStart();
- for (const DX12ShaderRegisterInput* input : registerInputs)
- {
- DX12TLAS* zTLAS = dynamic_cast<DX12TLAS*>(input->inputResource);
- DX12Texture* zTexture
- = dynamic_cast<DX12Texture*>(input->inputResource);
- DX12Buffer* zBuffer = dynamic_cast<DX12Buffer*>(input->inputResource);
- switch (input->registerType)
- {
- case DX12_SHADER_REGISTER_B_CONST_BUFFER:
- {
- D3D12_CONSTANT_BUFFER_VIEW_DESC cbvDesc = {};
- if (!zBuffer)
- {
- Logging::error()
- << "Expected a buffer resource for register type "
- << input->registerType;
- throw std::logic_error(
- "Expected a buffer resource for register type "
- + std::to_string(input->registerType));
- }
- cbvDesc.BufferLocation
- = zBuffer->zBuffer()->GetGPUVirtualAddress();
- cbvDesc.SizeInBytes = (unsigned)zBuffer->getElementCount()
- * zBuffer->getElementLength();
- zDevice->CreateConstantBufferView(
- &cbvDesc, descriptorHeapHandle);
- break;
- }
- case DX12_SHADER_REGISTER_T_SHADER_RESOURCE:
- {
- D3D12_SHADER_RESOURCE_VIEW_DESC srvDesc;
- srvDesc.Format = DXGI_FORMAT_UNKNOWN;
- if (zTLAS)
- {
- srvDesc.ViewDimension
- = D3D12_SRV_DIMENSION_RAYTRACING_ACCELERATION_STRUCTURE;
- srvDesc.RaytracingAccelerationStructure.Location
- = zTLAS->zResultBuffer()
- ->zBuffer()
- ->GetGPUVirtualAddress();
- }
- else if (zTexture)
- {
- srvDesc.ViewDimension = D3D12_SRV_DIMENSION_TEXTURE2D;
- srvDesc.Texture2D.MipLevels = 0;
- srvDesc.Texture2D.MostDetailedMip = 0;
- srvDesc.Texture2D.PlaneSlice = 0;
- srvDesc.Texture2D.ResourceMinLODClamp = 0.0f;
- }
- else if (zBuffer)
- {
- srvDesc.ViewDimension = D3D12_SRV_DIMENSION_BUFFER;
- srvDesc.Buffer.FirstElement = 0;
- srvDesc.Buffer.NumElements
- = (unsigned)zBuffer->getElementCount();
- srvDesc.Buffer.StructureByteStride
- = zBuffer->getElementLength();
- srvDesc.Buffer.Flags = D3D12_BUFFER_SRV_FLAG_NONE;
- }
- srvDesc.Shader4ComponentMapping
- = D3D12_DEFAULT_SHADER_4_COMPONENT_MAPPING;
- zDevice->CreateShaderResourceView(
- zTexture ? zTexture->zResource()
- : (zBuffer ? zBuffer->zBuffer() : 0),
- &srvDesc,
- descriptorHeapHandle);
- break;
- }
- case DX12_SHADER_REGISTER_U_UNORDERED_ACCESS:
- {
- D3D12_UNORDERED_ACCESS_VIEW_DESC uavDesc = {};
- if (zTexture)
- {
- uavDesc.ViewDimension = D3D12_UAV_DIMENSION_TEXTURE2D;
- uavDesc.Format = DXGI_FORMAT_UNKNOWN;
- uavDesc.Texture2D.MipSlice = 0;
- uavDesc.Texture2D.PlaneSlice = 0;
- }
- else if (zBuffer)
- {
- uavDesc.ViewDimension = D3D12_UAV_DIMENSION_BUFFER;
- uavDesc.Buffer.FirstElement = 0;
- uavDesc.Buffer.NumElements
- = (unsigned)zBuffer->getElementCount();
- uavDesc.Buffer.StructureByteStride
- = zBuffer->getElementLength();
- uavDesc.Buffer.CounterOffsetInBytes = 0;
- uavDesc.Buffer.Flags = D3D12_BUFFER_UAV_FLAG_NONE;
- }
- else
- {
- Logging::error()
- << "Expected a texture or buffer resource for "
- "register type "
- << input->registerType;
- throw std::logic_error(
- "Expected a texture or buffer resource for register "
- "type "
- + std::to_string(input->registerType));
- }
- zDevice->CreateUnorderedAccessView(
- zTexture ? zTexture->zResource() : zBuffer->zBuffer(),
- 0,
- &uavDesc,
- descriptorHeapHandle);
- break;
- }
- }
- descriptorHeapHandle.ptr += zDevice->GetDescriptorHandleIncrementSize(
- D3D12_DESCRIPTOR_HEAP_TYPE_CBV_SRV_UAV);
- }
- }
- DX12Pipeline* Framework::DX12GlobalDescriptorHeap::zPipeline() const
- {
- return pipeline;
- }
- ID3D12DescriptorHeap*
- Framework::DX12GlobalDescriptorHeap::zDescriptorHeap() const
- {
- return descriptorHeap;
- }
- Framework::DX12ShaderBindingTable::DX12ShaderBindingTable(
- DX12Pipeline* pipeline)
- : ReferenceCounter(),
- pipeline(pipeline),
- shaderBindingTableBuffer(0),
- globalDescriptorHeap(0),
- rayGenRecordSize(0),
- rayGenCount(0),
- missRecordSize(0),
- missCount(0),
- callableRecordSize(0),
- callableCount(0),
- hitGroupRecordSize(0),
- hitGroupCount(0),
- tableBuffer(0),
- tableBufferSize(0),
- nextHitGroupOffset(0),
- stateObjectProperties(0)
- {
- pipeline->zPipelineState()->QueryInterface(
- __uuidof(ID3D12StateObjectProperties), (void**)&stateObjectProperties);
- }
- Framework::DX12ShaderBindingTable::~DX12ShaderBindingTable()
- {
- stateObjectProperties->Release();
- if (pipeline)
- {
- pipeline->release();
- }
- if (shaderBindingTableBuffer)
- {
- shaderBindingTableBuffer->release();
- }
- if (globalDescriptorHeap)
- {
- globalDescriptorHeap->release();
- }
- for (const char* buffer : tempBuffers)
- {
- delete[] buffer;
- }
- }
- void Framework::DX12ShaderBindingTable::setGlobalDescriptorHeap(
- DX12GlobalDescriptorHeap* zGlobalDescriptorHeap)
- {
- if (this->globalDescriptorHeap)
- {
- this->globalDescriptorHeap->release();
- }
- this->globalDescriptorHeap = zGlobalDescriptorHeap;
- if (this->globalDescriptorHeap)
- {
- this->globalDescriptorHeap->getThis();
- }
- }
- void Framework::DX12ShaderBindingTable::startUpdate()
- {
- if (shaderBindingTableBuffer)
- {
- shaderBindingTableBuffer->zBuffer()->Map(0, 0, (void**)&tableBuffer);
- tableBufferSize = (int)shaderBindingTableBuffer->getElementCount()
- * shaderBindingTableBuffer->getElementLength();
- }
- else
- {
- tableBuffer = 0;
- tableBufferSize = 0;
- }
- rayGenRecordSize = 0;
- rayGenCount = 0;
- missRecordSize = 0;
- missCount = 0;
- callableRecordSize = 0;
- callableCount = 0;
- for (const DX12ShaderFunction* function :
- pipeline->getFunctionsWithoutHitGroups())
- {
- DX12ShaderSignature* signature = function->zSignature();
- if (function->getFunctionType() == DX12_SHADER_FUNCTION_TYPE_RAY_GEN)
- {
- if (rayGenRecordSize < signature->gerShaderBindingTableParamCount())
- {
- rayGenRecordSize = signature->gerShaderBindingTableParamCount();
- }
- rayGenCount++;
- }
- else if (function->getFunctionType() == DX12_SHADER_FUNCTION_TYPE_MISS)
- {
- if (missRecordSize < signature->gerShaderBindingTableParamCount())
- {
- missRecordSize = signature->gerShaderBindingTableParamCount();
- }
- missCount++;
- }
- else if (function->getFunctionType()
- == DX12_SHADER_FUNCTION_TYPE_CALLABLE)
- {
- if (callableRecordSize
- < signature->gerShaderBindingTableParamCount())
- {
- callableRecordSize
- = signature->gerShaderBindingTableParamCount();
- }
- callableCount++;
- }
- }
- rayGenRecordSize = ROUND_UP_POWER_OF_2(
- D3D12_RAYTRACING_SHADER_RECORD_BYTE_ALIGNMENT + rayGenRecordSize * 8,
- D3D12_RAYTRACING_SHADER_RECORD_BYTE_ALIGNMENT);
- missRecordSize = ROUND_UP_POWER_OF_2(
- D3D12_RAYTRACING_SHADER_RECORD_BYTE_ALIGNMENT + missRecordSize * 8,
- D3D12_RAYTRACING_SHADER_RECORD_BYTE_ALIGNMENT);
- callableRecordSize = ROUND_UP_POWER_OF_2(
- D3D12_RAYTRACING_SHADER_RECORD_BYTE_ALIGNMENT + callableRecordSize * 8,
- D3D12_RAYTRACING_SHADER_RECORD_BYTE_ALIGNMENT);
- hitGroupCount = 0;
- hitGroupRecordSize = 0;
- for (DX12ShaderHitGroup* hitGroup : pipeline->getHitGroups())
- {
- DX12ShaderSignature* signature = hitGroup->zSignature();
- if (hitGroupRecordSize < signature->gerShaderBindingTableParamCount())
- {
- hitGroupRecordSize = signature->gerShaderBindingTableParamCount();
- }
- hitGroupCount++;
- }
- hitGroupRecordSize = ROUND_UP_POWER_OF_2(
- D3D12_RAYTRACING_SHADER_RECORD_BYTE_ALIGNMENT + hitGroupRecordSize * 8,
- D3D12_RAYTRACING_SHADER_RECORD_BYTE_ALIGNMENT);
- nextHitGroupOffset = ROUND_UP_POWER_OF_2(
- rayGenRecordSize * rayGenCount + missRecordSize * missCount
- + callableRecordSize * callableCount,
- 64);
- }
- void Framework::DX12ShaderBindingTable::set(int index, void* data, int size)
- {
- if (tableBufferSize >= index + size)
- {
- memcpy(tableBuffer + index, data, size);
- }
- else
- {
- index -= tableBufferSize;
- ArrayIterator<char*> it = tempBuffers.begin();
- while (it && 2048 < index + size)
- {
- index -= 2048;
- it++;
- }
- while (2048 < index + size)
- {
- char* newBuffer = new char[2048];
- memset(newBuffer, 0, 2048);
- tempBuffers.add(newBuffer);
- index -= 2048;
- }
- // because the buffer size is allways rounded up to multiples
- // of 32 and the size written at once is always 8 or 32 it
- // should never be possible to have a negative index here
- assert(index >= 0);
- if (!it)
- {
- char* newBuffer = new char[2048];
- memset(newBuffer, 0, 2048);
- tempBuffers.add(newBuffer);
- memcpy(newBuffer + index, data, size);
- }
- else
- {
- memcpy(it.val() + index, data, size);
- }
- }
- }
- void Framework::DX12ShaderBindingTable::setShaderInput(
- DX12ShaderFunction* zFunction, int* offsetPointer, __int64 gpuAddress)
- {
- int index = 0;
- for (const DX12ShaderFunction* pf :
- pipeline->getFunctionsWithoutHitGroups())
- {
- if (pf == zFunction)
- {
- break;
- }
- if (pf->getFunctionType() == zFunction->getFunctionType())
- {
- index++;
- }
- }
- int offset = 0;
- if (zFunction->getFunctionType() == DX12_SHADER_FUNCTION_TYPE_RAY_GEN)
- {
- offset = index * rayGenRecordSize;
- }
- else if (zFunction->getFunctionType() == DX12_SHADER_FUNCTION_TYPE_MISS)
- {
- offset = rayGenRecordSize * rayGenCount + index * missRecordSize;
- }
- else if (zFunction->getFunctionType() == DX12_SHADER_FUNCTION_TYPE_CALLABLE)
- {
- offset = rayGenRecordSize * rayGenCount + missRecordSize * missCount
- + index * callableRecordSize;
- }
- else
- {
- throw std::logic_error("setShaderInput can only be used for ray "
- "generation, miss, and callable "
- "shader functions");
- }
- offset += D3D12_RAYTRACING_SHADER_RECORD_BYTE_ALIGNMENT
- + sizeof(__int64) * *offsetPointer;
- set(offset, &gpuAddress, sizeof(__int64));
- }
- int Framework::DX12ShaderBindingTable::addHitGroup(
- DX12ShaderHitGroup* zHitGroup)
- {
- int index = nextHitGroupOffset;
- set(index,
- stateObjectProperties->GetShaderIdentifier(
- zHitGroup->zHitGroupDesc()->HitGroupExport),
- D3D12_RAYTRACING_SHADER_RECORD_BYTE_ALIGNMENT);
- if (zHitGroup->zSignature()->getDescriptorHeapBindings().getEntryCount()
- > 0)
- {
- D3D12_GPU_DESCRIPTOR_HANDLE gpuAddress
- = globalDescriptorHeap->zDescriptorHeap()
- ->GetGPUDescriptorHandleForHeapStart();
- set(index + D3D12_RAYTRACING_SHADER_RECORD_BYTE_ALIGNMENT,
- &gpuAddress.ptr,
- sizeof(__int64));
- }
- nextHitGroupOffset += hitGroupRecordSize;
- return index + D3D12_RAYTRACING_SHADER_RECORD_BYTE_ALIGNMENT;
- }
- void Framework::DX12ShaderBindingTable::setHitGroupShaderInput(
- int hitGroupOffset, int* offsetPointer, __int64 gpuAddress)
- {
- set(hitGroupOffset + sizeof(__int64) * *offsetPointer,
- &gpuAddress,
- sizeof(__int64));
- }
- void Framework::DX12ShaderBindingTable::endUpdate(
- ID3D12Device5* zDevice, DX12CommandQueue* zQueue)
- {
- int rayTracingIndex = 0;
- int missIndex = 0;
- int callableIndex = 0;
- for (const DX12ShaderFunction* function :
- pipeline->getFunctionsWithoutHitGroups())
- {
- int offset = -1;
- DX12ShaderSignature* signature = function->zSignature();
- if (function->getFunctionType() == DX12_SHADER_FUNCTION_TYPE_RAY_GEN)
- {
- offset = rayTracingIndex * rayGenRecordSize;
- rayTracingIndex++;
- }
- else if (function->getFunctionType() == DX12_SHADER_FUNCTION_TYPE_MISS)
- {
- offset
- = rayGenRecordSize * rayGenCount + missIndex * missRecordSize;
- missIndex++;
- }
- else if (function->getFunctionType()
- == DX12_SHADER_FUNCTION_TYPE_CALLABLE)
- {
- offset = rayGenRecordSize * rayGenCount + missRecordSize * missCount
- + callableIndex * callableRecordSize;
- callableIndex++;
- }
- if (offset >= 0)
- {
- set(offset,
- stateObjectProperties->GetShaderIdentifier(
- function->zExportDesc()->Name),
- D3D12_RAYTRACING_SHADER_RECORD_BYTE_ALIGNMENT);
- if (function->zSignature()
- ->getDescriptorHeapBindings()
- .getEntryCount()
- > 0)
- {
- D3D12_GPU_DESCRIPTOR_HANDLE gpuAddress
- = globalDescriptorHeap->zDescriptorHeap()
- ->GetGPUDescriptorHandleForHeapStart();
- set(offset + D3D12_RAYTRACING_SHADER_RECORD_BYTE_ALIGNMENT,
- &gpuAddress.ptr,
- sizeof(__int64));
- }
- }
- }
- if (nextHitGroupOffset > tableBufferSize)
- {
- DX12Buffer* newBuffer = new DX12Buffer(1,
- zDevice,
- dynamic_cast<DX12CommandQueue*>(zQueue->getThis()),
- D3D12_RESOURCE_FLAG_NONE);
- newBuffer->setLength(ROUND_UP_POWER_OF_2(nextHitGroupOffset, 256));
- newBuffer->createBufferWithoutData(
- D3D12_RESOURCE_STATE_GENERIC_READ, D3D12_HEAP_TYPE_UPLOAD);
- void* newTableBuffer = 0;
- newBuffer->zBuffer()->Map(0, 0, (void**)&newTableBuffer);
- int sizeToCopy = nextHitGroupOffset;
- int index = 0;
- if (tableBufferSize > 0)
- {
- memcpy(newTableBuffer, tableBuffer, tableBufferSize);
- sizeToCopy -= tableBufferSize;
- index = tableBufferSize;
- }
- for (char* buffer : tempBuffers)
- {
- int bytesToCopy = sizeToCopy < 2048 ? sizeToCopy : 2048;
- memcpy((char*)newTableBuffer + index, buffer, bytesToCopy);
- sizeToCopy -= bytesToCopy;
- index += bytesToCopy;
- if (sizeToCopy == 0)
- {
- break;
- }
- }
- assert(sizeToCopy == 0);
- newBuffer->zBuffer()->Unmap(0, 0);
- if (shaderBindingTableBuffer)
- {
- shaderBindingTableBuffer->zBuffer()->Unmap(0, 0);
- shaderBindingTableBuffer->release();
- }
- shaderBindingTableBuffer = newBuffer;
- }
- else
- {
- shaderBindingTableBuffer->zBuffer()->Unmap(0, 0);
- }
- }
- void Framework::DX12ShaderBindingTable::fillDispatchRaysDesc(
- D3D12_DISPATCH_RAYS_DESC* dispatchRaysDesc)
- {
- dispatchRaysDesc->RayGenerationShaderRecord.SizeInBytes
- = rayGenCount * rayGenRecordSize;
- dispatchRaysDesc->RayGenerationShaderRecord.StartAddress
- = shaderBindingTableBuffer->zBuffer()->GetGPUVirtualAddress();
- dispatchRaysDesc->MissShaderTable.SizeInBytes = missCount * missRecordSize;
- dispatchRaysDesc->MissShaderTable.StartAddress
- = shaderBindingTableBuffer->zBuffer()->GetGPUVirtualAddress()
- + rayGenCount * rayGenRecordSize;
- dispatchRaysDesc->MissShaderTable.StrideInBytes = missRecordSize;
- dispatchRaysDesc->HitGroupTable.SizeInBytes
- = hitGroupCount * hitGroupRecordSize;
- dispatchRaysDesc->HitGroupTable.StartAddress
- = shaderBindingTableBuffer->zBuffer()->GetGPUVirtualAddress()
- + ROUND_UP_POWER_OF_2(rayGenCount * rayGenRecordSize
- + missCount * missRecordSize
- + callableCount * callableRecordSize,
- 64);
- dispatchRaysDesc->HitGroupTable.StrideInBytes = hitGroupRecordSize;
- dispatchRaysDesc->CallableShaderTable.SizeInBytes
- = callableCount * callableRecordSize;
- dispatchRaysDesc->CallableShaderTable.StartAddress
- = callableCount > 0 ? shaderBindingTableBuffer->zBuffer()
- ->GetGPUVirtualAddress()
- + rayGenCount * rayGenRecordSize + missCount * missRecordSize : 0;
- dispatchRaysDesc->CallableShaderTable.StrideInBytes
- = callableCount > 0 ? callableRecordSize : 0;
- }
- DX12Pipeline* Framework::DX12ShaderBindingTable::zPipeline() const
- {
- return pipeline;
- }
|