ShaderProgramResourceSystem.h 3.2 KB

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