ShaderLoader.h 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. // Copyright (C) 2009-2016, Panagiotis Christopoulos Charitos.
  2. // All rights reserved.
  3. // Code licensed under the BSD License.
  4. // http://www.anki3d.org/LICENSE
  5. #pragma once
  6. #include <anki/resource/ResourceManager.h>
  7. #include <anki/util/StdTypes.h>
  8. #include <anki/util/StringList.h>
  9. #include <anki/Gr.h>
  10. namespace anki
  11. {
  12. /// Helper class used for shader program loading. The class adds the include
  13. /// preprocessor directive.
  14. class ShaderLoader
  15. {
  16. public:
  17. /// It loads a file and parses it
  18. /// @param[in] filename The file to load
  19. ShaderLoader(ResourceManager* manager)
  20. : m_alloc(manager->getTempAllocator())
  21. , m_manager(manager)
  22. {
  23. }
  24. ~ShaderLoader()
  25. {
  26. m_shaderSource.destroy(m_alloc);
  27. m_sourceLines.destroy(m_alloc);
  28. }
  29. /// Parse a PrePreprocessor formated GLSL file. Use the accessors to get
  30. /// the output
  31. ///
  32. /// @param filename The file to parse
  33. ANKI_USE_RESULT Error parseFile(const ResourceFilename& filename);
  34. const String& getShaderSource() const
  35. {
  36. ANKI_ASSERT(!m_shaderSource.isEmpty());
  37. return m_shaderSource;
  38. }
  39. ShaderType getShaderType() const
  40. {
  41. ANKI_ASSERT(m_type != ShaderType::COUNT);
  42. return m_type;
  43. }
  44. protected:
  45. TempResourceAllocator<U8> m_alloc;
  46. /// The final program source
  47. String m_shaderSource;
  48. /// The parseFileForPragmas fills this
  49. StringList m_sourceLines;
  50. /// Shader type
  51. ShaderType m_type = ShaderType::COUNT;
  52. /// Keep the manager for some path conversions.
  53. ResourceManager* m_manager;
  54. /// A recursive function that parses a file for #include and updates the
  55. /// output
  56. ///
  57. /// @param filename The file to parse
  58. /// @param depth The #line in GLSL does not support filename so an
  59. /// depth it being used. It also tracks the includance depth
  60. ANKI_USE_RESULT Error parseFileIncludes(
  61. ResourceFilename filename, U32 depth);
  62. void printSourceLines() const; ///< For debugging
  63. };
  64. } // end namespace anki