ProgramPrePreprocessor.h 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. // Copyright (C) 2014, 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/gl/GlCommon.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. private:
  26. using PPPStringList = StringListBase<TempResourceAllocator<char>>;
  27. using PPPString = TempResourceString;
  28. public:
  29. /// It loads a file and parses it
  30. /// @param[in] filename The file to load
  31. ProgramPrePreprocessor(ResourceManager* manager)
  32. : m_alloc(manager->_getTempAllocator()),
  33. m_manager(manager)
  34. {}
  35. ~ProgramPrePreprocessor()
  36. {
  37. m_shaderSource.destroy(m_alloc);
  38. m_sourceLines.destroy(m_alloc);
  39. }
  40. /// Parse a PrePreprocessor formated GLSL file. Use the accessors to get
  41. /// the output
  42. ///
  43. /// @param filename The file to parse
  44. ANKI_USE_RESULT Error parseFile(const CString& filename);
  45. const PPPString& getShaderSource() const
  46. {
  47. ANKI_ASSERT(!m_shaderSource.isEmpty());
  48. return m_shaderSource;
  49. }
  50. ShaderType getShaderType() const
  51. {
  52. ANKI_ASSERT(m_type != ShaderType::COUNT);
  53. return m_type;
  54. }
  55. protected:
  56. TempResourceAllocator<U8> m_alloc;
  57. /// The final program source
  58. PPPString m_shaderSource;
  59. /// The parseFileForPragmas fills this
  60. PPPStringList m_sourceLines;
  61. /// Shader type
  62. ShaderType m_type = ShaderType::COUNT;
  63. /// Keep the manager for some path conversions.
  64. ResourceManager* m_manager;
  65. /// A recursive function that parses a file for pragmas and updates the
  66. /// output
  67. ///
  68. /// @param filename The file to parse
  69. /// @param depth The #line in GLSL does not support filename so an
  70. /// depth it being used. It also tracks the includance depth
  71. ANKI_USE_RESULT Error parseFileForPragmas(
  72. CString filename, U32 depth);
  73. /// Parse the type
  74. ANKI_USE_RESULT Error parseType(const PPPString& line, Bool& found);
  75. void printSourceLines() const; ///< For debugging
  76. };
  77. } // end namespace anki
  78. #endif