ShaderProgramResourceSystem.h 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. // Copyright (C) 2009-2021, 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/ShaderProgramBinary.h>
  11. namespace anki {
  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. ~ShaderProgramRaytracingLibrary()
  20. {
  21. m_libraryName.destroy(m_alloc);
  22. m_resourceHashToShaderGroupHandleIndex.destroy(m_alloc);
  23. }
  24. CString getLibraryName() const
  25. {
  26. return m_libraryName;
  27. }
  28. U32 getRayTypeCount() const
  29. {
  30. ANKI_ASSERT(m_rayTypeCount < MAX_U32);
  31. return m_rayTypeCount;
  32. }
  33. const ShaderProgramPtr& getShaderProgram() const
  34. {
  35. return m_program;
  36. }
  37. /// Given the filename of a program (that contains ray tracing shaders) and a specific mutation get the shader
  38. /// handle index.
  39. U32 getShaderGroupHandleIndex(CString resourceFilename, U64 mutationHash) const
  40. {
  41. return getIndex(generateShaderGroupGroupHash(resourceFilename, mutationHash, m_alloc));
  42. }
  43. private:
  44. GenericMemoryPoolAllocator<U8> m_alloc;
  45. String m_libraryName;
  46. U32 m_rayTypeCount = MAX_U32;
  47. ShaderProgramPtr m_program;
  48. HashMap<U64, U32> m_resourceHashToShaderGroupHandleIndex;
  49. /// Given the filename of a program (that contains ray tracing shaders) and a specific mutation get a hash back.
  50. static U64 generateShaderGroupGroupHash(CString resourceFilename, U64 mutationHash,
  51. GenericMemoryPoolAllocator<U8> alloc);
  52. /// The hash generated by generateShaderGroupGroupHash() can be used to retrieve the group position in the
  53. /// m_program.
  54. U32 getIndex(U64 groupHash) const
  55. {
  56. auto it = m_resourceHashToShaderGroupHandleIndex.find(groupHash);
  57. ANKI_ASSERT(it != m_resourceHashToShaderGroupHandleIndex.getEnd());
  58. return *it;
  59. }
  60. };
  61. /// A system that does some work on shader programs before resources start loading.
  62. class ShaderProgramResourceSystem
  63. {
  64. public:
  65. ShaderProgramResourceSystem(CString cacheDir, GrManager* gr, ResourceFilesystem* fs,
  66. const GenericMemoryPoolAllocator<U8>& alloc)
  67. : m_alloc(alloc)
  68. , m_gr(gr)
  69. , m_fs(fs)
  70. {
  71. m_cacheDir.create(alloc, cacheDir);
  72. }
  73. ~ShaderProgramResourceSystem();
  74. ANKI_USE_RESULT Error init();
  75. ConstWeakArray<ShaderProgramRaytracingLibrary> getRayTracingLibraries() const
  76. {
  77. return m_rtLibraries;
  78. }
  79. private:
  80. GenericMemoryPoolAllocator<U8> m_alloc;
  81. String m_cacheDir;
  82. GrManager* m_gr;
  83. ResourceFilesystem* m_fs;
  84. DynamicArray<ShaderProgramRaytracingLibrary> m_rtLibraries;
  85. /// Iterate all programs in the filesystem and compile them to AnKi's binary format.
  86. static Error compileAllShaders(CString cacheDir, GrManager& gr, ResourceFilesystem& fs,
  87. GenericMemoryPoolAllocator<U8>& alloc, StringListAuto& rtProgramFilenames);
  88. static Error createRayTracingPrograms(CString cacheDir, const StringListAuto& rtProgramFilenames, GrManager& gr,
  89. GenericMemoryPoolAllocator<U8>& alloc,
  90. DynamicArray<ShaderProgramRaytracingLibrary>& libs);
  91. };
  92. /// @}
  93. } // end namespace anki