ShaderProgram.h 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. // Copyright (C) 2009-present, Panagiotis Christopoulos Charitos and contributors.
  2. // All rights reserved.
  3. // Code licensed under the BSD License.
  4. // http://www.anki3d.org/LICENSE
  5. #pragma once
  6. #include <AnKi/Gr/GrObject.h>
  7. #include <AnKi/Gr/Shader.h>
  8. namespace anki {
  9. /// @addtogroup graphics
  10. /// @{
  11. /// @memberof RayTracingShaders
  12. class RayTracingHitGroup
  13. {
  14. public:
  15. Shader* m_closestHitShader = nullptr;
  16. Shader* m_anyHitShader = nullptr;
  17. };
  18. /// @memberof ShaderProgramInitInfo
  19. class WorkGraphNodeSpecialization
  20. {
  21. public:
  22. CString m_nodeName;
  23. UVec3 m_maxNodeDispatchGrid;
  24. };
  25. /// ShaderProgram init info.
  26. class ShaderProgramInitInfo : public GrBaseInitInfo
  27. {
  28. public:
  29. /// Option 1
  30. Array<Shader*, U32(ShaderType::kLastGraphics + 1)> m_graphicsShaders = {};
  31. /// Option 2
  32. Shader* m_computeShader = nullptr;
  33. /// Option 3
  34. class
  35. {
  36. public:
  37. WeakArray<Shader*> m_rayGenShaders;
  38. WeakArray<Shader*> m_missShaders;
  39. WeakArray<RayTracingHitGroup> m_hitGroups;
  40. U32 m_maxRecursionDepth = 1;
  41. } m_rayTracingShaders;
  42. /// Option 4
  43. class
  44. {
  45. public:
  46. Shader* m_shader = nullptr;
  47. ConstWeakArray<WorkGraphNodeSpecialization> m_nodeSpecializations;
  48. } m_workGraph;
  49. ShaderProgramInitInfo(CString name = {})
  50. : GrBaseInitInfo(name)
  51. {
  52. }
  53. Bool isValid() const;
  54. };
  55. /// GPU program.
  56. class ShaderProgram : public GrObject
  57. {
  58. ANKI_GR_OBJECT
  59. public:
  60. static constexpr GrObjectType kClassType = GrObjectType::kShaderProgram;
  61. /// Get the shader group handles that will be used in the SBTs. The size of each handle is GpuDeviceCapabilities::m_shaderGroupHandleSize. To
  62. /// access a handle use:
  63. /// @code
  64. /// const U8* handleBegin = &getShaderGroupHandles()[handleIdx * devCapabilities.m_shaderGroupHandleSize];
  65. /// const U8* handleEnd = &getShaderGroupHandles()[(handleIdx + 1) * devCapabilities.m_shaderGroupHandleSize];
  66. /// @endcode
  67. /// The handleIdx is defined via a convention. The ray gen shaders appear first where handleIdx is in the same order as the shader in
  68. /// RayTracingShaders::m_rayGenShaders. Then miss shaders follow with a similar rule. Then hit groups follow.
  69. ConstWeakArray<U8> getShaderGroupHandles() const;
  70. /// Same as getShaderGroupHandles but the data live in a GPU buffer.
  71. Buffer& getShaderGroupHandlesGpuBuffer() const;
  72. ShaderTypeBit getShaderTypes() const
  73. {
  74. return m_shaderTypes;
  75. }
  76. /// Get the size of the shader. Can be an indication of the complexity of the shader.
  77. U32 getShaderBinarySize(ShaderType type) const
  78. {
  79. ANKI_ASSERT(!!(ShaderTypeBit(1u << type) & m_shaderTypes));
  80. ANKI_ASSERT(m_shaderBinarySizes[type] > 0);
  81. return m_shaderBinarySizes[type];
  82. }
  83. /// The pixel shader of the program has a discard.
  84. Bool hasDiscard() const
  85. {
  86. ANKI_ASSERT(!!(m_shaderTypes & ShaderTypeBit::kPixel));
  87. return m_refl.m_pixel.m_discards;
  88. }
  89. const ShaderReflection& getReflection() const
  90. {
  91. return m_refl;
  92. }
  93. PtrSize getWorkGraphMemoryRequirements() const
  94. {
  95. ANKI_ASSERT(m_workGraphScratchBufferSize);
  96. return m_workGraphScratchBufferSize;
  97. }
  98. protected:
  99. Array<U32, U32(ShaderType::kCount)> m_shaderBinarySizes = {};
  100. PtrSize m_workGraphScratchBufferSize = 0;
  101. ShaderTypeBit m_shaderTypes = ShaderTypeBit::kNone;
  102. ShaderReflection m_refl;
  103. /// Construct.
  104. ShaderProgram(CString name)
  105. : GrObject(kClassType, name)
  106. {
  107. }
  108. /// Destroy.
  109. ~ShaderProgram()
  110. {
  111. }
  112. private:
  113. /// Allocate and initialize a new instance.
  114. [[nodiscard]] static ShaderProgram* newInstance(const ShaderProgramInitInfo& init);
  115. };
  116. /// @}
  117. } // end namespace anki