ShaderProgramResourceSystem.h 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  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/Resource/Common.h>
  7. #include <AnKi/Gr/ShaderProgram.h>
  8. #include <AnKi/Util/HashMap.h>
  9. #include <AnKi/Util/StringList.h>
  10. #include <AnKi/ShaderCompiler/ShaderBinary.h>
  11. namespace anki {
  12. /// @addtogroup resource
  13. /// @{
  14. enum class RayTracingShaderGroupType : U8
  15. {
  16. kRayGen,
  17. kMiss,
  18. kHit,
  19. kCount,
  20. kFirst = 0
  21. };
  22. /// This is a ray tracing library. Essentially a shader program with some functionality on how to get group indices.
  23. class ShaderProgramRaytracingLibrary
  24. {
  25. friend class ShaderProgramResourceSystem;
  26. public:
  27. CString getLibraryName() const
  28. {
  29. return m_libraryName;
  30. }
  31. U32 getRayTypeCount() const
  32. {
  33. ANKI_ASSERT(m_rayTypeCount < kMaxU32);
  34. return m_rayTypeCount;
  35. }
  36. const ShaderProgramPtr& getShaderProgram() const
  37. {
  38. return m_program;
  39. }
  40. /// Given the filename of a program (that contains ray tracing shaders) and a specific mutation get the shader handle index.
  41. U32 getShaderGroupHandleIndex(CString resourceFilename, U64 mutationHash, RayTracingShaderGroupType groupType) const
  42. {
  43. return getIndex(generateShaderGroupGroupHash(resourceFilename, mutationHash, groupType));
  44. }
  45. private:
  46. ResourceString m_libraryName;
  47. U32 m_rayTypeCount = kMaxU32;
  48. ShaderProgramPtr m_program;
  49. ResourceHashMap<U64, U32> m_resourceHashToShaderGroupHandleIndex;
  50. /// Given the filename of a program (that contains ray tracing shaders) and a specific mutation get a hash back.
  51. static U64 generateShaderGroupGroupHash(CString resourceFilename, U64 mutationHash, RayTracingShaderGroupType groupType);
  52. /// The hash generated by generateShaderGroupGroupHash() can be used to retrieve the group position in the m_program.
  53. U32 getIndex(U64 groupHash) const
  54. {
  55. auto it = m_resourceHashToShaderGroupHandleIndex.find(groupHash);
  56. ANKI_ASSERT(it != m_resourceHashToShaderGroupHandleIndex.getEnd());
  57. return *it;
  58. }
  59. };
  60. /// A system that does some work on shader programs before resources start loading.
  61. class ShaderProgramResourceSystem : public MakeSingleton<ShaderProgramResourceSystem>
  62. {
  63. public:
  64. ShaderProgramResourceSystem()
  65. {
  66. }
  67. ~ShaderProgramResourceSystem() = default;
  68. Error init();
  69. ConstWeakArray<ShaderProgramRaytracingLibrary> getRayTracingLibraries() const
  70. {
  71. return m_rtLibraries;
  72. }
  73. private:
  74. class ShaderH;
  75. class ShaderGroup;
  76. class Lib;
  77. ResourceDynamicArray<ShaderProgramRaytracingLibrary> m_rtLibraries;
  78. static Error createRayTracingPrograms(ResourceDynamicArray<ShaderProgramRaytracingLibrary>& outLibs);
  79. };
  80. /// @}
  81. } // end namespace anki