ProgramPrePreprocessor.h 2.4 KB

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