ProgramPrePreprocessor.h 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. // Copyright (C) 2009-2015, 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. /// Helper class used for shader program loading
  12. ///
  13. /// The class fills some of the GLSL spec deficiencies. It adds the include
  14. /// preprocessor directive and the support to have all the shaders in the same
  15. /// file. The file that includes all the shaders is called
  16. /// PrePreprocessor-compatible.
  17. ///
  18. /// The preprocessor pragmas are:
  19. ///
  20. /// - #pragma anki type <vert | tesc | tese | geom | frag | comp>
  21. /// - #pragma anki include "<filename>"
  22. class ProgramPrePreprocessor
  23. {
  24. public:
  25. /// It loads a file and parses it
  26. /// @param[in] filename The file to load
  27. ProgramPrePreprocessor(ResourceManager* manager)
  28. : m_alloc(manager->getTempAllocator())
  29. , m_manager(manager)
  30. {}
  31. ~ProgramPrePreprocessor()
  32. {
  33. m_shaderSource.destroy(m_alloc);
  34. m_sourceLines.destroy(m_alloc);
  35. }
  36. /// Parse a PrePreprocessor formated GLSL file. Use the accessors to get
  37. /// the output
  38. ///
  39. /// @param filename The file to parse
  40. ANKI_USE_RESULT Error parseFile(const ResourceFilename& filename);
  41. const String& getShaderSource() const
  42. {
  43. ANKI_ASSERT(!m_shaderSource.isEmpty());
  44. return m_shaderSource;
  45. }
  46. ShaderType getShaderType() const
  47. {
  48. ANKI_ASSERT(m_type != ShaderType::COUNT);
  49. return m_type;
  50. }
  51. protected:
  52. TempResourceAllocator<U8> m_alloc;
  53. /// The final program source
  54. String m_shaderSource;
  55. /// The parseFileForPragmas fills this
  56. StringList m_sourceLines;
  57. /// Shader type
  58. ShaderType m_type = ShaderType::COUNT;
  59. /// Keep the manager for some path conversions.
  60. ResourceManager* m_manager;
  61. /// A recursive function that parses a file for pragmas and updates the
  62. /// output
  63. ///
  64. /// @param filename The file to parse
  65. /// @param depth The #line in GLSL does not support filename so an
  66. /// depth it being used. It also tracks the includance depth
  67. ANKI_USE_RESULT Error parseFileForPragmas(
  68. ResourceFilename filename, U32 depth);
  69. /// Parse the type
  70. ANKI_USE_RESULT Error parseType(const String& line, Bool& found);
  71. void printSourceLines() const; ///< For debugging
  72. };
  73. } // end namespace anki