| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376 |
- #pragma once
- #include "Array.h"
- #include "DX12Buffer.h"
- struct ID3D12Device5;
- struct ID3D12GraphicsCommandList;
- struct D3D12_INPUT_ELEMENT_DESC;
- struct D3D12_ROOT_PARAMETER1;
- struct D3D12_CONSTANT_BUFFER_VIEW_DESC;
- struct ID3D12StateObjectProperties;
- namespace Framework
- {
- class Texture;
- class DX12TLAS;
- class DX12SamplerState;
- enum DX12ShaderRegister
- {
- DX12_SHADER_REGISTER_B_CONST_BUFFER = 0,
- DX12_SHADER_REGISTER_T_SHADER_RESOURCE = 1,
- DX12_SHADER_REGISTER_U_UNORDERED_ACCESS = 2,
- DX12_SHADER_REGISTER_S_SAMPLER = 3,
- };
- enum DX12DescriptorHeapType
- {
- GLOBAL_DESCRIPTOR_HEAP = 0,
- TEXTURE_DESCRIPTOR_HEAP = 1,
- SAMPLER_DESCRIPTOR_HEAP = 2,
- __DESCRIPTOR_HEAP_TYPE_COUNT__
- };
- struct DX12ShaderRegisterUsage
- {
- DX12ShaderRegister registerType;
- DX12DescriptorHeapType descriptorHeapType;
- int registerIndex;
- int spaceIndex;
- int descriptorHeapIndex;
- int bindingTableIndex;
- bool array;
- int arraySize;
- };
- class DX12ShaderHeap;
- class DX12ShaderSignature : public ReferenceCounter
- {
- private:
- ID3D12RootSignature* signature;
- Array<DX12ShaderRegisterUsage*> descriptorHeapBindings;
- Array<DX12ShaderRegisterUsage*> bindingTableBindings;
- bool changed;
- int shaderBindingTableParamCount;
- bool useGlobalDescriptorHeap;
- bool useTextureDescriptorHeap;
- bool useSamplerDescriptorHeap;
- bool global;
- public:
- DLLEXPORT DX12ShaderSignature(bool global = false);
- DLLEXPORT ~DX12ShaderSignature();
- /**
- * for each datastructure with : register(...) in the shader code,
- * either this function or addRegisterUsageLinkedToDescriptorHeap must
- * be called to link the register usage to the shader binding table or
- * descriptor heap.
- *
- * \param registerType the register type e.g.
- * DX12_SHADER_REGISTER_B_CONST_BUFFER for : register(b0)
- * \param registerIndex the register index e.g. 1 for : register(b1)
- * \param spaceIndex the optional space index e.g. 3 for : register(b1,
- * space3)
- * \return pointer to the offset in the shader binding table where this
- * parameter needs to be placed. This pointer is only valid after the
- * call to createSignature() and should be used to fill the shader
- * binding table with the correct GPU addresses of the resources.
- */
- DLLEXPORT int* addRegisterUsageLinkedToShaderBindingTable(
- DX12ShaderRegister registerType,
- int registerIndex,
- int spaceIndex = 0);
- /**
- * for each datastructure with : register(...) in the shader code,
- * either this function or addRegisterUsageLinkedToDescriptorHeap must
- * be called to link the register usage to the shader binding table or
- * descriptor heap.
- * For optimal performance the registers that are linked to the
- * descriptor heaps should be consecutive and use consecutive descriptor
- * heap spaces. Example: register(b3), register(b4), register(b5) linked
- * to descriptor heap index 2, 3, 4.
- *
- * \param descriptorHeapIndex the index in the descriptor heap witch
- * contains the resource for this register usage
- * \param registerType the register type e.g.
- * DX12_SHADER_REGISTER_B_CONST_BUFFER for : register(b0)
- * \param registerIndex the register index e.g. 1 for : register(b1)
- * \param spaceIndex the optional space index e.g. 3 for : register(b1,
- * space3)
- * \param array used to specify if the register referes to an array in
- * hlsl
- * \param arraySize the size of the array if array is true or -1 if the
- * size is unknown (unbound array)
- */
- DLLEXPORT void addRegisterUsageLinkedToDescriptorHeap(
- int descriptorHeapIndex,
- DX12ShaderRegister registerType,
- int registerIndex,
- int spaceIndex = 0,
- DX12DescriptorHeapType descriptorHeapType = GLOBAL_DESCRIPTOR_HEAP,
- bool array = false,
- int arraySize = -1);
- /**
- * Creates the root signature.
- */
- DLLEXPORT void createSignature(ID3D12Device5* zDevice,
- PFN_D3D12_SERIALIZE_ROOT_SIGNATURE pfnD3D12SerializeRootSignature);
- DLLEXPORT ID3D12RootSignature* zSignature() const;
- DLLEXPORT const Array<DX12ShaderRegisterUsage*>&
- getDescriptorHeapBindings() const;
- DLLEXPORT int gerShaderBindingTableParamCount() const;
- DLLEXPORT bool doesUseGlobalDescriptorHeap() const;
- DLLEXPORT bool doesUseTextureDescriptorHeap() const;
- DLLEXPORT bool doesUseSamplerDescriptorHeap() const;
- };
- enum DX12ShaderFunctionType
- {
- DX12_SHADER_FUNCTION_TYPE_RAY_GEN,
- DX12_SHADER_FUNCTION_TYPE_INTERSECTION,
- DX12_SHADER_FUNCTION_TYPE_ANY_HIT,
- DX12_SHADER_FUNCTION_TYPE_CLOSEST_HIT,
- DX12_SHADER_FUNCTION_TYPE_MISS,
- DX12_SHADER_FUNCTION_TYPE_CALLABLE,
- };
- class DX12ShaderFunction : public ReferenceCounter
- {
- private:
- Text functionName;
- DX12ShaderSignature* signature;
- D3D12_EXPORT_DESC* exportDesc;
- DX12ShaderFunctionType functionType;
- public:
- DLLEXPORT DX12ShaderFunction(const Text& functionName,
- DX12ShaderSignature* signature,
- DX12ShaderFunctionType functionType);
- DLLEXPORT ~DX12ShaderFunction();
- DLLEXPORT const Text& getFunctionName() const;
- DLLEXPORT DX12ShaderSignature* zSignature() const;
- DLLEXPORT D3D12_EXPORT_DESC* zExportDesc() const;
- DLLEXPORT DX12ShaderFunctionType getFunctionType() const;
- };
- class DX12Shader : public ReferenceCounter
- {
- private:
- RCArray<DX12ShaderFunction> functions;
- const unsigned char* shaderBytes;
- int shaderBytesSize;
- D3D12_DXIL_LIBRARY_DESC* libraryDesc;
- public:
- DLLEXPORT DX12Shader(
- const unsigned char* shaderBytes, int shaderBytesSize);
- DLLEXPORT ~DX12Shader();
- DLLEXPORT void addFunction(DX12ShaderFunction* function);
- DLLEXPORT int getShaderBytesSize() const;
- DLLEXPORT const unsigned char* getShaderBytes() const;
- DLLEXPORT const RCArray<DX12ShaderFunction>& getFunctions() const;
- DLLEXPORT D3D12_DXIL_LIBRARY_DESC* zLibraryDesc() const;
- };
- class DX12ShaderHitGroup : public ReferenceCounter
- {
- private:
- Text name;
- DX12ShaderFunction* closestHitShaderFunction;
- DX12ShaderFunction* anyHitShaderFunction;
- DX12ShaderFunction* intersectionShaderFunction;
- int payloadSize;
- int attributeSize;
- D3D12_HIT_GROUP_DESC* hitGroupDesc;
- public:
- DLLEXPORT DX12ShaderHitGroup(const Text name);
- DLLEXPORT ~DX12ShaderHitGroup();
- DLLEXPORT void setClosestHitShaderFunction(
- DX12ShaderFunction* zClosestHitShaderFunction);
- DLLEXPORT void setAnyHitShaderFunction(
- DX12ShaderFunction* zAnyHitShaderFunction);
- DLLEXPORT void setIntersectionShaderFunction(
- DX12ShaderFunction* zIntersectionShaderFunction);
- DLLEXPORT void setPayloadSize(int payloadSize);
- DLLEXPORT void setAttributeSize(int attributeSize);
- DLLEXPORT const Text& getName() const;
- DLLEXPORT DX12ShaderFunction* zClosestHitShaderFunction() const;
- DLLEXPORT DX12ShaderFunction* zAnyHitShaderFunction() const;
- DLLEXPORT DX12ShaderFunction* zIntersectionShaderFunction() const;
- DLLEXPORT int getPayloadSize() const;
- DLLEXPORT int getAttributeSize() const;
- DLLEXPORT D3D12_HIT_GROUP_DESC* zHitGroupDesc() const;
- DLLEXPORT DX12ShaderSignature* zSignature() const;
- };
- class DX12ShaderBindingTable;
- class DX12GlobalDescriptorHeap;
- class DX12Pipeline : public ReferenceCounter
- {
- private:
- RCArray<DX12Shader> shaders;
- RCArray<DX12ShaderHitGroup> hitGroups;
- Array<const DX12ShaderFunction*> functionsWithoutHitGroups;
- DX12ShaderSignature* globalSignature;
- ID3D12StateObject* pipelineState;
- int maxRecursionDepth;
- public:
- DLLEXPORT DX12Pipeline();
- DLLEXPORT ~DX12Pipeline();
- DLLEXPORT void addShader(DX12Shader* shader);
- DLLEXPORT void addHitGroup(DX12ShaderHitGroup* hitGroup);
- DLLEXPORT void setMaxRecursionDepth(int maxRecursionDepth);
- DLLEXPORT void createPipelineState(ID3D12Device5* zDevice,
- PFN_D3D12_SERIALIZE_ROOT_SIGNATURE pfnD3D12SerializeRootSignature);
- DLLEXPORT ID3D12StateObject* zPipelineState() const;
- DLLEXPORT DX12ShaderBindingTable* createShaderBindingTable();
- DLLEXPORT DX12GlobalDescriptorHeap* createGlobalDescriptorHeap(
- DX12DescriptorHeapType type);
- DLLEXPORT const RCArray<DX12Shader>& getShaders() const;
- DLLEXPORT const Array<const DX12ShaderFunction*>&
- getFunctionsWithoutHitGroups() const;
- DLLEXPORT const RCArray<DX12ShaderHitGroup>& getHitGroups() const;
- DLLEXPORT DX12ShaderSignature* zGlobalSignature() const;
- }; // namespace Framework
- struct DX12ShaderRegisterInput
- {
- DX12ShaderRegister registerType;
- ReferenceCounter*
- inputResource; // Can be Texture*, DXBuffer*, or DX12TLAS*
- };
- class DX12GlobalDescriptorHeap : public ReferenceCounter
- {
- private:
- DX12Pipeline* pipeline;
- ID3D12DescriptorHeap* descriptorHeap;
- Array<DX12ShaderRegisterInput*> registerInputs;
- int lastDescriptorHeapSize;
- ID3D12Device5* zDevice;
- DX12DescriptorHeapType type;
- bool heapChanged;
- public:
- DX12GlobalDescriptorHeap(
- DX12Pipeline* pipeline, DX12DescriptorHeapType type);
- ~DX12GlobalDescriptorHeap();
- private:
- DLLEXPORT void addInput(
- DX12ShaderRegister type, ReferenceCounter* inputResource);
- public:
- DLLEXPORT void addTextureInput(
- DX12ShaderRegister type, Texture* zTexture);
- DLLEXPORT void updateTextureInput(
- int heapIndex, DX12ShaderRegister type, Texture* zTexture);
- DLLEXPORT void addBufferInput(
- DX12ShaderRegister type, DXBuffer* zBuffer);
- DLLEXPORT void addTLASInput(DX12ShaderRegister type, DX12TLAS* zTLAS);
- DLLEXPORT void updateTLASInput(
- int heapIndex, DX12ShaderRegister type, DX12TLAS* zTLAS);
- DLLEXPORT void addSamplerInput(DX12SamplerState* zSampler);
- DLLEXPORT void updateDescriptorHeap(ID3D12Device5* zDevice);
- DLLEXPORT DX12Pipeline* zPipeline() const;
- DLLEXPORT ID3D12DescriptorHeap* zDescriptorHeap() const;
- DLLEXPORT void setHeapChanged(bool changed);
- DLLEXPORT bool wasHeapChanged() const;
- };
- class DX12ShaderBindingTable : public ReferenceCounter
- {
- private:
- DX12Pipeline* pipeline;
- DX12Buffer* shaderBindingTableBuffer;
- DX12GlobalDescriptorHeap* globalDescriptorHeap;
- DX12GlobalDescriptorHeap* textureDescriptorHeap;
- DX12GlobalDescriptorHeap* samplerDescriptorHeap;
- int rayGenRecordSize;
- int rayGenCount;
- int missRecordSize;
- int missCount;
- int callableRecordSize;
- int callableCount;
- int hitGroupRecordSize;
- int hitGroupCount;
- char* tableBuffer;
- int tableBufferSize;
- Array<char*> tempBuffers;
- int nextHitGroupOffset;
- ID3D12StateObjectProperties* stateObjectProperties;
- public:
- DLLEXPORT DX12ShaderBindingTable(DX12Pipeline* pipeline);
- DLLEXPORT ~DX12ShaderBindingTable();
- DLLEXPORT void setGlobalDescriptorHeap(
- DX12GlobalDescriptorHeap* zGlobalDescriptorHeap);
- DLLEXPORT void setTextureDescriptorHeap(
- DX12GlobalDescriptorHeap* zTextureDescriptorHeap);
- DLLEXPORT void setSamplerDescriptorHeap(
- DX12GlobalDescriptorHeap* zSamplerDescriptorHeap);
- DLLEXPORT void startUpdate();
- private:
- DLLEXPORT void set(int index, void* data, int size);
- public:
- /**
- * sets inputs for shader outside of any hit groups for the current
- * update of the shader binding table.
- *
- * \param zFunction the sample shader function for which the inputs are
- * set. The function must be part of the pipeline.
- * \param offsetPointer pointer for the offset in the shader binding
- * table witch was obtained by adding the register usage to the
- * segnature of the shader function by calling
- * DX12ShaderSignature::addRegisterUsageLinkedToShaderBindingTable.
- * \param gpuAddress the parameter to be placed in the shader binding
- * table for this input. This is usually the GPU address of a buffer or
- * texture.
- */
- DLLEXPORT void setShaderInput(DX12ShaderFunction* zFunction,
- int* offsetPointer,
- __int64 gpuAddress);
- /**
- * adds a hit group to the current update of the shader binding table.
- * each hit group mus be readded for each update of the shader binding
- * table.
- *
- * \param zHitGroup the hit group to be added. The hit group must be
- * part of the pipeline.
- * \param lastIndex the index of the hit group when it was added at the
- * last frame
- * \return the index of the hit group in the shader binding table. This
- * index can be used to set the inputs for this hit group by calling
- * setHitGroupShaderInput.
- */
- DLLEXPORT int addHitGroup(
- DX12ShaderHitGroup* zHitGroup, int lastIndex = -1);
- /**
- * sets a specific input for a previously added hitgroup.
- *
- * \param hitGroupOffset the hit group offset returned by addHitGroup
- * for the hit group for which the input is set.
- * \param offsetPointer pointer for the offset in the shader binding
- * table witch was obtained by adding the register usage to the
- * segnature of the shader function by calling
- * DX12ShaderSignature::addRegisterUsageLinkedToShaderBindingTable.
- * \param gpuAddress the parameter to be placed in the shader binding
- * table for this input. This is usually the GPU address of a buffer or
- * texture.
- */
- DLLEXPORT void setHitGroupShaderInput(
- int hitGroupOffset, int* offsetPointer, __int64 gpuAddress);
- DLLEXPORT void endUpdate(
- ID3D12Device5* zDevice, DX12CommandQueue* zQueue);
- DLLEXPORT void fillDispatchRaysDesc(
- D3D12_DISPATCH_RAYS_DESC* dispatchRaysDesc);
- DLLEXPORT DX12Pipeline* zPipeline() const;
- };
- } // namespace Framework
|