DX12Shader.h 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  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. namespace Framework
  10. {
  11. class Texture;
  12. class DX12TLAS;
  13. enum DX12ShaderRegister
  14. {
  15. DX12_SHADER_REGISTER_B_CONST_BUFFER = 0,
  16. DX12_SHADER_REGISTER_T_SHADER_RESOURCE = 1,
  17. DX12_SHADER_REGISTER_U_UNORDERED_ACCESS = 2,
  18. // TODO: Do we need Sampler?
  19. };
  20. struct DX12ShaderRegisterUsage
  21. {
  22. DX12ShaderRegister registerType;
  23. int registerIndex;
  24. int spaceIndex;
  25. int descriptorHeapIndex;
  26. };
  27. class DX12ShaderHeap;
  28. class DX12ShaderSignature : public ReferenceCounter
  29. {
  30. private:
  31. ID3D12RootSignature* signature;
  32. Array<DX12ShaderRegisterUsage> registerUsages;
  33. bool changed;
  34. public:
  35. DX12ShaderSignature();
  36. ~DX12ShaderSignature();
  37. /**
  38. * for each datastructure with : register(...) in the shader code,
  39. * either this function or addRegisterUsageLinkedToDescriptorHeap must
  40. * be called to link the register usage to the shader binding table or
  41. * descriptor heap.
  42. *
  43. * \param registerType the register type e.g.
  44. * DX12_SHADER_REGISTER_B_CONST_BUFFER for : register(b0)
  45. * \param registerIndex the register index e.g. 1 for : register(b1)
  46. * \param spaceIndex the optional space index e.g. 3 for : register(b1,
  47. * space3)
  48. */
  49. void addRegisterUsageLinkedToShaderBindingTable(
  50. DX12ShaderRegister registerType,
  51. int registerIndex,
  52. int spaceIndex = 0);
  53. /**
  54. * for each datastructure with : register(...) in the shader code,
  55. * either this function or addRegisterUsageLinkedToDescriptorHeap must
  56. * be called to link the register usage to the shader binding table or
  57. * descriptor heap.
  58. *
  59. * \param descriptorHeapIndex the index in the descriptor heap witch
  60. * contains the resource for this register usage
  61. * \param registerType the register type e.g.
  62. * DX12_SHADER_REGISTER_B_CONST_BUFFER for : register(b0)
  63. * \param registerIndex the register index e.g. 1 for : register(b1)
  64. * \param spaceIndex the optional space index e.g. 3 for : register(b1,
  65. * space3)
  66. */
  67. void addRegisterUsageLinkedToDescriptorHeap(int descriptorHeapIndex,
  68. DX12ShaderRegister registerType,
  69. int registerIndex,
  70. int spaceIndex = 0);
  71. /**
  72. * Creates the root signature.
  73. */
  74. void createSignature(ID3D12Device5* zDevice,
  75. PFN_D3D12_SERIALIZE_ROOT_SIGNATURE pfnD3D12SerializeRootSignature);
  76. ID3D12RootSignature* zSignature() const;
  77. const Array<DX12ShaderRegisterUsage>& getRegisterUsages() const;
  78. };
  79. class DX12ShaderFunction : public ReferenceCounter
  80. {
  81. private:
  82. Text functionName;
  83. DX12ShaderSignature* signature;
  84. D3D12_EXPORT_DESC* exportDesc;
  85. public:
  86. DX12ShaderFunction(
  87. const Text& functionName, DX12ShaderSignature* signature);
  88. ~DX12ShaderFunction();
  89. const Text& getFunctionName() const;
  90. DX12ShaderSignature* zSignature() const;
  91. D3D12_EXPORT_DESC* zExportDesc() const;
  92. };
  93. class DX12Shader : public ReferenceCounter
  94. {
  95. private:
  96. RCArray<DX12ShaderFunction> functions;
  97. const unsigned char* shaderBytes;
  98. int shaderBytesSize;
  99. D3D12_DXIL_LIBRARY_DESC* libraryDesc;
  100. public:
  101. DX12Shader(const unsigned char* shaderBytes, int shaderBytesSize);
  102. ~DX12Shader();
  103. void addFunction(DX12ShaderFunction* function);
  104. int getShaderBytesSize() const;
  105. const unsigned char* getShaderBytes() const;
  106. const RCArray<DX12ShaderFunction>& getFunctions() const;
  107. D3D12_DXIL_LIBRARY_DESC* zLibraryDesc() const;
  108. };
  109. class DX12ShaderHitGroup : public ReferenceCounter
  110. {
  111. private:
  112. Text name;
  113. DX12ShaderFunction* closestHitShaderFunction;
  114. DX12ShaderFunction* anyHitShaderFunction;
  115. DX12ShaderFunction* intersectionShaderFunction;
  116. int payloadSize;
  117. int attributeSize;
  118. D3D12_HIT_GROUP_DESC* hitGroupDesc;
  119. public:
  120. DX12ShaderHitGroup(const Text name);
  121. ~DX12ShaderHitGroup();
  122. void setClosestHitShaderFunction(
  123. DX12ShaderFunction* zClosestHitShaderFunction);
  124. void setAnyHitShaderFunction(DX12ShaderFunction* zAnyHitShaderFunction);
  125. void setIntersectionShaderFunction(
  126. DX12ShaderFunction* zIntersectionShaderFunction);
  127. void setPayloadSize(int payloadSize);
  128. void setAttributeSize(int attributeSize);
  129. const Text& getName() const;
  130. DX12ShaderFunction* zClosestHitShaderFunction() const;
  131. DX12ShaderFunction* zAnyHitShaderFunction() const;
  132. DX12ShaderFunction* zIntersectionShaderFunction() const;
  133. int getPayloadSize() const;
  134. int getAttributeSize() const;
  135. D3D12_HIT_GROUP_DESC* zHitGroupDesc() const;
  136. };
  137. class DX12ShaderBindingTable;
  138. class DX12GlobalDescriptorHeap;
  139. class DX12Pipeline : public ReferenceCounter
  140. {
  141. private:
  142. RCArray<DX12Shader> shaders;
  143. RCArray<DX12ShaderHitGroup> hitGroups;
  144. ID3D12RootSignature* emptyGlobalRootSignature;
  145. ID3D12RootSignature* emptyLocalRootSignature;
  146. ID3D12StateObject* pipelineState;
  147. int maxRecursionDepth;
  148. public:
  149. DX12Pipeline();
  150. ~DX12Pipeline();
  151. void addShader(DX12Shader* shader);
  152. void addHitGroup(DX12ShaderHitGroup* hitGroup);
  153. void setMaxRecursionDepth(int maxRecursionDepth);
  154. void createPipelineState(ID3D12Device5* zDevice,
  155. PFN_D3D12_SERIALIZE_ROOT_SIGNATURE pfnD3D12SerializeRootSignature);
  156. ID3D12StateObject* zPipelineState() const;
  157. DX12ShaderBindingTable* createShaderBindingTable();
  158. DX12GlobalDescriptorHeap* createGlobalDescriptorHeap();
  159. const RCArray<DX12Shader>& getShaders() const;
  160. }; // namespace Framework
  161. struct DX12ShaderRegisterInput
  162. {
  163. DX12ShaderRegister registerType;
  164. ReferenceCounter*
  165. inputResource; // Can be Texture*, DXBuffer*, or DX12TLAS*
  166. };
  167. class DX12GlobalDescriptorHeap : public ReferenceCounter
  168. {
  169. private:
  170. DX12Pipeline* pipeline;
  171. ID3D12DescriptorHeap* descriptorHeap;
  172. Array<DX12ShaderRegisterInput*> registerInputs;
  173. int lastDescriptorHeapSize;
  174. public:
  175. DX12GlobalDescriptorHeap(DX12Pipeline* pipeline);
  176. ~DX12GlobalDescriptorHeap();
  177. private:
  178. void addInput(DX12ShaderRegister type, ReferenceCounter* inputResource);
  179. public:
  180. void addTextureInput(DX12ShaderRegister type, Texture* zTexture);
  181. void addBufferInput(DX12ShaderRegister type, DXBuffer* zBuffer);
  182. void addTLASInput(DX12ShaderRegister type, DX12TLAS* zTLAS);
  183. void updateDescriptorHeap(ID3D12Device5* zDevice);
  184. DX12Pipeline* zPipeline() const;
  185. ID3D12DescriptorHeap* zDescriptorHeap() const;
  186. };
  187. class DX12ShaderBindingTable : public ReferenceCounter
  188. {
  189. private:
  190. DX12Pipeline* pipeline;
  191. DX12Buffer* shaderBindingTableBuffer;
  192. DX12GlobalDescriptorHeap* globalDescriptorHeap;
  193. int rayGenRecordSize;
  194. int rayGenCount;
  195. int missRecordSize;
  196. int missCount;
  197. int hitGroupRecordSize;
  198. int hitGroupCount;
  199. bool changed;
  200. public:
  201. DX12ShaderBindingTable(DX12Pipeline* pipeline);
  202. ~DX12ShaderBindingTable();
  203. void setGlobalDescriptorHeap(
  204. DX12GlobalDescriptorHeap* zGlobalDescriptorHeap);
  205. void startUpdate();
  206. void setShaderInputs(DX12ShaderFunction* zFunction,
  207. std::initializer_list<unsigned __int64> gpuAddresses);
  208. void setHitGroupShaderInputs(int instanceIndex,
  209. DX12ShaderHitGroup* zHitGroup,
  210. std::initializer_list<unsigned __int64> gpuAddresses);
  211. void endUpdate(ID3D12Device5* zDevice);
  212. void fillDispatchRaysDesc(D3D12_DISPATCH_RAYS_DESC* dispatchRaysDesc);
  213. DX12Pipeline* zPipeline() const;
  214. };
  215. } // namespace Framework