DX12Shader.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321
  1. #pragma once
  2. #include "Array.h"
  3. #include "DX12Buffer.h"
  4. struct ID3D12Device5;
  5. struct ID3D12GraphicsCommandList;
  6. struct D3D12_INPUT_ELEMENT_DESC;
  7. struct D3D12_ROOT_PARAMETER1;
  8. struct D3D12_CONSTANT_BUFFER_VIEW_DESC;
  9. struct ID3D12StateObjectProperties;
  10. namespace Framework
  11. {
  12. class Texture;
  13. class DX12TLAS;
  14. enum DX12ShaderRegister
  15. {
  16. DX12_SHADER_REGISTER_B_CONST_BUFFER = 0,
  17. DX12_SHADER_REGISTER_T_SHADER_RESOURCE = 1,
  18. DX12_SHADER_REGISTER_U_UNORDERED_ACCESS = 2,
  19. // TODO: Do we need Sampler?
  20. };
  21. struct DX12ShaderRegisterUsage
  22. {
  23. DX12ShaderRegister registerType;
  24. int registerIndex;
  25. int spaceIndex;
  26. int descriptorHeapIndex;
  27. int bindingTableIndex;
  28. };
  29. class DX12ShaderHeap;
  30. class DX12ShaderSignature : public ReferenceCounter
  31. {
  32. private:
  33. ID3D12RootSignature* signature;
  34. Array<DX12ShaderRegisterUsage*> descriptorHeapBindings;
  35. Array<DX12ShaderRegisterUsage*> bindingTableBindings;
  36. bool changed;
  37. int shaderBindingTableParamCount;
  38. public:
  39. DX12ShaderSignature();
  40. ~DX12ShaderSignature();
  41. /**
  42. * for each datastructure with : register(...) in the shader code,
  43. * either this function or addRegisterUsageLinkedToDescriptorHeap must
  44. * be called to link the register usage to the shader binding table or
  45. * descriptor heap.
  46. *
  47. * \param registerType the register type e.g.
  48. * DX12_SHADER_REGISTER_B_CONST_BUFFER for : register(b0)
  49. * \param registerIndex the register index e.g. 1 for : register(b1)
  50. * \param spaceIndex the optional space index e.g. 3 for : register(b1,
  51. * space3)
  52. * \return pointer to the offset in the shader binding table where this
  53. * parameter needs to be placed. This pointer is only valid after the
  54. * call to createSignature() and should be used to fill the shader
  55. * binding table with the correct GPU addresses of the resources.
  56. */
  57. int* addRegisterUsageLinkedToShaderBindingTable(
  58. DX12ShaderRegister registerType,
  59. int registerIndex,
  60. int spaceIndex = 0);
  61. /**
  62. * for each datastructure with : register(...) in the shader code,
  63. * either this function or addRegisterUsageLinkedToDescriptorHeap must
  64. * be called to link the register usage to the shader binding table or
  65. * descriptor heap.
  66. * For optimal performance the registers that are linked to the
  67. * descriptor heaps should be consecutive and use consecutive descriptor
  68. * heap spaces. Example: register(b3), register(b4), register(b5) linked
  69. * to descriptor heap index 2, 3, 4.
  70. *
  71. * \param descriptorHeapIndex the index in the descriptor heap witch
  72. * contains the resource for this register usage
  73. * \param registerType the register type e.g.
  74. * DX12_SHADER_REGISTER_B_CONST_BUFFER for : register(b0)
  75. * \param registerIndex the register index e.g. 1 for : register(b1)
  76. * \param spaceIndex the optional space index e.g. 3 for : register(b1,
  77. * space3)
  78. */
  79. void addRegisterUsageLinkedToDescriptorHeap(int descriptorHeapIndex,
  80. DX12ShaderRegister registerType,
  81. int registerIndex,
  82. int spaceIndex = 0);
  83. /**
  84. * Creates the root signature.
  85. */
  86. void createSignature(ID3D12Device5* zDevice,
  87. PFN_D3D12_SERIALIZE_ROOT_SIGNATURE pfnD3D12SerializeRootSignature);
  88. ID3D12RootSignature* zSignature() const;
  89. const Array<DX12ShaderRegisterUsage*>&
  90. getDescriptorHeapBindings() const;
  91. int gerShaderBindingTableParamCount() const;
  92. };
  93. enum DX12ShaderFunctionType
  94. {
  95. DX12_SHADER_FUNCTION_TYPE_RAY_GEN,
  96. DX12_SHADER_FUNCTION_TYPE_INTERSECTION,
  97. DX12_SHADER_FUNCTION_TYPE_ANY_HIT,
  98. DX12_SHADER_FUNCTION_TYPE_CLOSEST_HIT,
  99. DX12_SHADER_FUNCTION_TYPE_MISS,
  100. DX12_SHADER_FUNCTION_TYPE_CALLABLE,
  101. };
  102. class DX12ShaderFunction : public ReferenceCounter
  103. {
  104. private:
  105. Text functionName;
  106. DX12ShaderSignature* signature;
  107. D3D12_EXPORT_DESC* exportDesc;
  108. DX12ShaderFunctionType functionType;
  109. public:
  110. DX12ShaderFunction(const Text& functionName,
  111. DX12ShaderSignature* signature,
  112. DX12ShaderFunctionType functionType);
  113. ~DX12ShaderFunction();
  114. const Text& getFunctionName() const;
  115. DX12ShaderSignature* zSignature() const;
  116. D3D12_EXPORT_DESC* zExportDesc() const;
  117. DX12ShaderFunctionType getFunctionType() const;
  118. };
  119. class DX12Shader : public ReferenceCounter
  120. {
  121. private:
  122. RCArray<DX12ShaderFunction> functions;
  123. const unsigned char* shaderBytes;
  124. int shaderBytesSize;
  125. D3D12_DXIL_LIBRARY_DESC* libraryDesc;
  126. public:
  127. DX12Shader(const unsigned char* shaderBytes, int shaderBytesSize);
  128. ~DX12Shader();
  129. void addFunction(DX12ShaderFunction* function);
  130. int getShaderBytesSize() const;
  131. const unsigned char* getShaderBytes() const;
  132. const RCArray<DX12ShaderFunction>& getFunctions() const;
  133. D3D12_DXIL_LIBRARY_DESC* zLibraryDesc() const;
  134. };
  135. class DX12ShaderHitGroup : public ReferenceCounter
  136. {
  137. private:
  138. Text name;
  139. DX12ShaderFunction* closestHitShaderFunction;
  140. DX12ShaderFunction* anyHitShaderFunction;
  141. DX12ShaderFunction* intersectionShaderFunction;
  142. int payloadSize;
  143. int attributeSize;
  144. D3D12_HIT_GROUP_DESC* hitGroupDesc;
  145. public:
  146. DX12ShaderHitGroup(const Text name);
  147. ~DX12ShaderHitGroup();
  148. void setClosestHitShaderFunction(
  149. DX12ShaderFunction* zClosestHitShaderFunction);
  150. void setAnyHitShaderFunction(DX12ShaderFunction* zAnyHitShaderFunction);
  151. void setIntersectionShaderFunction(
  152. DX12ShaderFunction* zIntersectionShaderFunction);
  153. void setPayloadSize(int payloadSize);
  154. void setAttributeSize(int attributeSize);
  155. const Text& getName() const;
  156. DX12ShaderFunction* zClosestHitShaderFunction() const;
  157. DX12ShaderFunction* zAnyHitShaderFunction() const;
  158. DX12ShaderFunction* zIntersectionShaderFunction() const;
  159. int getPayloadSize() const;
  160. int getAttributeSize() const;
  161. D3D12_HIT_GROUP_DESC* zHitGroupDesc() const;
  162. DX12ShaderSignature* zSignature() const;
  163. };
  164. class DX12ShaderBindingTable;
  165. class DX12GlobalDescriptorHeap;
  166. class DX12Pipeline : public ReferenceCounter
  167. {
  168. private:
  169. RCArray<DX12Shader> shaders;
  170. RCArray<DX12ShaderHitGroup> hitGroups;
  171. Array<const DX12ShaderFunction*> functionsWithoutHitGroups;
  172. ID3D12RootSignature* emptyGlobalRootSignature;
  173. ID3D12RootSignature* emptyLocalRootSignature;
  174. ID3D12StateObject* pipelineState;
  175. int maxRecursionDepth;
  176. public:
  177. DX12Pipeline();
  178. ~DX12Pipeline();
  179. void addShader(DX12Shader* shader);
  180. void addHitGroup(DX12ShaderHitGroup* hitGroup);
  181. void setMaxRecursionDepth(int maxRecursionDepth);
  182. void createPipelineState(ID3D12Device5* zDevice,
  183. PFN_D3D12_SERIALIZE_ROOT_SIGNATURE pfnD3D12SerializeRootSignature);
  184. ID3D12StateObject* zPipelineState() const;
  185. DX12ShaderBindingTable* createShaderBindingTable();
  186. DX12GlobalDescriptorHeap* createGlobalDescriptorHeap();
  187. const RCArray<DX12Shader>& getShaders() const;
  188. const Array<const DX12ShaderFunction*>&
  189. getFunctionsWithoutHitGroups() const;
  190. const RCArray<DX12ShaderHitGroup>& getHitGroups() const;
  191. }; // namespace Framework
  192. struct DX12ShaderRegisterInput
  193. {
  194. DX12ShaderRegister registerType;
  195. ReferenceCounter*
  196. inputResource; // Can be Texture*, DXBuffer*, or DX12TLAS*
  197. };
  198. class DX12GlobalDescriptorHeap : public ReferenceCounter
  199. {
  200. private:
  201. DX12Pipeline* pipeline;
  202. ID3D12DescriptorHeap* descriptorHeap;
  203. Array<DX12ShaderRegisterInput*> registerInputs;
  204. int lastDescriptorHeapSize;
  205. public:
  206. DX12GlobalDescriptorHeap(DX12Pipeline* pipeline);
  207. ~DX12GlobalDescriptorHeap();
  208. private:
  209. void addInput(DX12ShaderRegister type, ReferenceCounter* inputResource);
  210. public:
  211. void addTextureInput(DX12ShaderRegister type, Texture* zTexture);
  212. void addBufferInput(DX12ShaderRegister type, DXBuffer* zBuffer);
  213. void addTLASInput(DX12ShaderRegister type, DX12TLAS* zTLAS);
  214. void updateDescriptorHeap(ID3D12Device5* zDevice);
  215. DX12Pipeline* zPipeline() const;
  216. ID3D12DescriptorHeap* zDescriptorHeap() const;
  217. };
  218. class DX12ShaderBindingTable : public ReferenceCounter
  219. {
  220. private:
  221. DX12Pipeline* pipeline;
  222. DX12Buffer* shaderBindingTableBuffer;
  223. DX12GlobalDescriptorHeap* globalDescriptorHeap;
  224. int rayGenRecordSize;
  225. int rayGenCount;
  226. int missRecordSize;
  227. int missCount;
  228. int callableRecordSize;
  229. int callableCount;
  230. int hitGroupRecordSize;
  231. int hitGroupCount;
  232. char* tableBuffer;
  233. int tableBufferSize;
  234. Array<char*> tempBuffers;
  235. int nextHitGroupOffset;
  236. ID3D12StateObjectProperties* stateObjectProperties;
  237. public:
  238. DX12ShaderBindingTable(DX12Pipeline* pipeline);
  239. ~DX12ShaderBindingTable();
  240. void setGlobalDescriptorHeap(
  241. DX12GlobalDescriptorHeap* zGlobalDescriptorHeap);
  242. void startUpdate();
  243. private:
  244. void set(int index, void* data, int size);
  245. public:
  246. /**
  247. * sets inputs for shader outside of any hit groups for the current
  248. * update of the shader binding table.
  249. *
  250. * \param zFunction the sample shader function for which the inputs are
  251. * set. The function must be part of the pipeline.
  252. * \param offsetPointer pointer for the offset in the shader binding
  253. * table witch was obtained by adding the register usage to the
  254. * segnature of the shader function by calling
  255. * DX12ShaderSignature::addRegisterUsageLinkedToShaderBindingTable.
  256. * \param gpuAddress the parameter to be placed in the shader binding
  257. * table for this input. This is usually the GPU address of a buffer or
  258. * texture.
  259. */
  260. void setShaderInput(DX12ShaderFunction* zFunction,
  261. int* offsetPointer,
  262. __int64 gpuAddress);
  263. /**
  264. * adds a hit group to the current update of the shader binding table.
  265. * each hit group mus be readded for each update of the shader binding
  266. * table.
  267. *
  268. * \param zHitGroup the hit group to be added. The hit group must be
  269. * part of the pipeline.
  270. * \return the index of the hit group in the shader binding table. This
  271. * index can be used to set the inputs for this hit group by calling
  272. * setHitGroupShaderInput.
  273. */
  274. int addHitGroup(DX12ShaderHitGroup* zHitGroup);
  275. /**
  276. * sets a specific input for a previously added hitgroup.
  277. *
  278. * \param hitGroupOffset the hit group offset returned by addHitGroup
  279. * for the hit group for which the input is set.
  280. * \param offsetPointer pointer for the offset in the shader binding
  281. * table witch was obtained by adding the register usage to the
  282. * segnature of the shader function by calling
  283. * DX12ShaderSignature::addRegisterUsageLinkedToShaderBindingTable.
  284. * \param gpuAddress the parameter to be placed in the shader binding
  285. * table for this input. This is usually the GPU address of a buffer or
  286. * texture.
  287. */
  288. void setHitGroupShaderInput(
  289. int hitGroupOffset, int* offsetPointer, __int64 gpuAddress);
  290. void endUpdate(ID3D12Device5* zDevice);
  291. void fillDispatchRaysDesc(D3D12_DISPATCH_RAYS_DESC* dispatchRaysDesc);
  292. DX12Pipeline* zPipeline() const;
  293. };
  294. } // namespace Framework