ShaderProgramResourceSystem.h 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. // Copyright (C) 2009-2020, 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/shader_compiler/ShaderProgramBinary.h>
  10. namespace anki
  11. {
  12. /// @addtogroup resource
  13. /// @{
  14. /// This is a ray tracing library. Essentially a shader program with some functionality on how to get group indices.
  15. class ShaderProgramRaytracingLibrary
  16. {
  17. friend class ShaderProgramResourceSystem;
  18. public:
  19. CString getLibraryName() const
  20. {
  21. return m_libraryName;
  22. }
  23. U32 getRayTypeCount() const
  24. {
  25. ANKI_ASSERT(m_rayTypeCount < MAX_U32);
  26. return m_rayTypeCount;
  27. }
  28. const ShaderProgramPtr& getShaderProgram() const
  29. {
  30. return m_program;
  31. }
  32. /// Given the filename of a program (that contains hit shaders) and a specific mutation get the group handle.
  33. void getHitShaderGroupHandle(CString resourceFilename, U64 mutationHash, WeakArray<U8>& handle) const
  34. {
  35. const U32 hitGroupIndex = getHitGroupIndex(generateHitGroupHash(resourceFilename, mutationHash));
  36. getShaderGroupHandle(hitGroupIndex, handle);
  37. }
  38. void getMissShaderGroupHandle(U32 rayType, WeakArray<U8>& handle) const
  39. {
  40. ANKI_ASSERT(rayType < getRayTypeCount());
  41. getShaderGroupHandle(rayType + 1, handle);
  42. }
  43. void getRayGenShaderGroupHandle(WeakArray<U8>& handle) const
  44. {
  45. getShaderGroupHandle(0, handle);
  46. }
  47. private:
  48. String m_libraryName;
  49. U32 m_rayTypeCount = MAX_U32;
  50. ShaderProgramPtr m_program;
  51. HashMap<U64, U32> m_groupHashToGroupIndex;
  52. /// Given the filename of a program (that contains hit shaders) and a specific mutation get a hash back.
  53. static U64 generateHitGroupHash(CString resourceFilename, U64 mutationHash)
  54. {
  55. ANKI_ASSERT(resourceFilename.getLength() > 0);
  56. const U64 hash = appendHash(resourceFilename.cstr(), resourceFilename.getLength(), mutationHash);
  57. return hash;
  58. }
  59. /// The hash generated by generateHitGroupHash() can be used to retrieve the hit group position in the m_program.
  60. U32 getHitGroupIndex(U64 groupHash) const
  61. {
  62. auto it = m_groupHashToGroupIndex.find(groupHash);
  63. ANKI_ASSERT(it != m_groupHashToGroupIndex.getEnd());
  64. return *it;
  65. }
  66. void getShaderGroupHandle(U32 groupIndex, WeakArray<U8>& handle) const;
  67. };
  68. /// A system that does some work on shader programs before resources start loading.
  69. class ShaderProgramResourceSystem
  70. {
  71. public:
  72. ShaderProgramResourceSystem(CString cacheDir, GrManager* gr, ResourceFilesystem* fs,
  73. const GenericMemoryPoolAllocator<U8>& alloc)
  74. : m_alloc(alloc)
  75. , m_gr(gr)
  76. , m_fs(fs)
  77. {
  78. m_cacheDir.create(alloc, cacheDir);
  79. }
  80. ~ShaderProgramResourceSystem();
  81. ANKI_USE_RESULT Error init();
  82. ConstWeakArray<ShaderProgramRaytracingLibrary> getRayTracingLibraries() const
  83. {
  84. return m_rtLibraries;
  85. }
  86. private:
  87. GenericMemoryPoolAllocator<U8> m_alloc;
  88. String m_cacheDir;
  89. GrManager* m_gr;
  90. ResourceFilesystem* m_fs;
  91. DynamicArray<ShaderProgramRaytracingLibrary> m_rtLibraries;
  92. /// Iterate all programs in the filesystem and compile them to AnKi's binary format.
  93. static Error compileAllShaders(CString cacheDir, GrManager& gr, ResourceFilesystem& fs,
  94. GenericMemoryPoolAllocator<U8>& alloc);
  95. static Error createRayTracingPrograms(CString cacheDir, GrManager& gr, ResourceFilesystem& fs,
  96. GenericMemoryPoolAllocator<U8>& alloc,
  97. DynamicArray<ShaderProgramRaytracingLibrary>& libs);
  98. };
  99. /// @}
  100. } // end namespace anki