ShaderProgramResourceSystem.h 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. // Copyright (C) 2009-2022, 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(const GenericMemoryPoolAllocator<U8>& alloc)
  66. : m_alloc(alloc)
  67. {
  68. }
  69. ~ShaderProgramResourceSystem();
  70. ANKI_USE_RESULT Error init(ResourceFilesystem& fs, GrManager& gr);
  71. ConstWeakArray<ShaderProgramRaytracingLibrary> getRayTracingLibraries() const
  72. {
  73. return m_rtLibraries;
  74. }
  75. private:
  76. GenericMemoryPoolAllocator<U8> m_alloc;
  77. DynamicArray<ShaderProgramRaytracingLibrary> m_rtLibraries;
  78. static Error createRayTracingPrograms(ResourceFilesystem& fs, GrManager& gr, GenericMemoryPoolAllocator<U8>& alloc,
  79. DynamicArray<ShaderProgramRaytracingLibrary>& outLibs);
  80. };
  81. /// @}
  82. } // end namespace anki