| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779 |
- #include "DX12Shader.h"
- #include "Logging.h"
- using namespace Framework;
- Framework::DX12ShaderSignature::DX12ShaderSignature()
- : ReferenceCounter(),
- signature(0),
- changed(1)
- {}
- Framework::DX12ShaderSignature::~DX12ShaderSignature()
- {
- if (signature)
- {
- signature->Release();
- }
- }
- void Framework::DX12ShaderSignature::addRegisterUsage(
- DX12ShaderRegister registerType, int registerIndex)
- {
- addRegisterUsage(registerType, registerIndex, 0);
- }
- void Framework::DX12ShaderSignature::addRegisterUsage(
- DX12ShaderRegister registerType, int registerIndex, int spaceIndex)
- {
- registerUsages.add({registerType, registerIndex, spaceIndex});
- 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;
- }
- D3D12_ROOT_PARAMETER descriptorTable;
- descriptorTable.ParameterType = D3D12_ROOT_PARAMETER_TYPE_DESCRIPTOR_TABLE;
- descriptorTable.ShaderVisibility = D3D12_SHADER_VISIBILITY_ALL;
- descriptorTable.DescriptorTable.NumDescriptorRanges
- = registerUsages.getEntryCount();
- D3D12_DESCRIPTOR_RANGE* descriptorRanges
- = new D3D12_DESCRIPTOR_RANGE[registerUsages.getEntryCount()];
- int index = 0;
- for (const auto& usage : registerUsages)
- {
- D3D12_DESCRIPTOR_RANGE* range = &descriptorRanges[index];
- switch (usage.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;
- default:
- Logging::error() << "Unknown register type for root signature: "
- << usage.registerType;
- range->RangeType = D3D12_DESCRIPTOR_RANGE_TYPE_SRV;
- }
- range->NumDescriptors
- = 1; // TODO: optimize when multiple descriptors are used in a row
- // e.g. u0, u1, u2 with the same range then NumDescriptors can
- // be 3 for u0 and u1 and u2 would not be needed in this array
- range->BaseShaderRegister = usage.registerIndex;
- range->RegisterSpace = usage.spaceIndex;
- range->OffsetInDescriptorsFromTableStart = index;
- index++;
- }
- descriptorTable.DescriptorTable.pDescriptorRanges = descriptorRanges;
- D3D12_ROOT_SIGNATURE_DESC rootDesc = {};
- rootDesc.NumParameters = 1;
- 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;
- }
- ID3D12RootSignature* Framework::DX12ShaderSignature::zSignature() const
- {
- return signature;
- }
- Framework::DX12ShaderFunction::DX12ShaderFunction(
- const Text& functionName, DX12ShaderSignature* signature)
- : ReferenceCounter(),
- functionName(functionName),
- signature(signature),
- exportDesc(new D3D12_EXPORT_DESC())
- {
- wchar_t* wc = new wchar_t[functionName.getLength() + 1];
- mbtowc(wc, 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;
- }
- Framework::DX12Shader::DX12Shader(const 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 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];
- mbtowc(wc, 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* intersectionShaderFunction)
- {
- if (closestHitShaderFunction
- && closestHitShaderFunction->zSignature()
- != intersectionShaderFunction->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 (anyHitShaderFunction
- && anyHitShaderFunction->zSignature()
- != intersectionShaderFunction->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->intersectionShaderFunction)
- {
- this->intersectionShaderFunction->release();
- }
- this->intersectionShaderFunction = intersectionShaderFunction;
- 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;
- }
- 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**)&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();
- }
- }
- 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++;
- // create export list for hole pipeline with all hitgroups and all functions
- // that are not part of a hit group
- Array<const DX12ShaderFunction*> functionsWithoutHitGroups;
- 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);
- }
- }
- }
- wchar_t** functionAndHitGroupNames
- = new 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;
- }
|