MaterialProgramCreator.h 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  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_MATERIAL_SHADER_PROGRAM_CREATOR_H
  6. #define ANKI_RESOURCE_MATERIAL_SHADER_PROGRAM_CREATOR_H
  7. #include "anki/util/StringList.h"
  8. #include "anki/Gl.h"
  9. #include "anki/resource/Common.h"
  10. namespace anki {
  11. // Forward
  12. class XmlElement;
  13. /// Material loader variable. It's the information on whatever is inside
  14. /// \<input\>
  15. class MaterialProgramCreatorInputVariable: public NonCopyable
  16. {
  17. public:
  18. using MPString = TempResourceString;
  19. using MPStringList = StringListBase<TempResourceAllocator<char>>;
  20. TempResourceAllocator<U8> m_alloc;
  21. MPString m_name;
  22. MPString m_typeStr;
  23. ShaderVariableDataType m_type = ShaderVariableDataType::NONE;
  24. MPStringList m_value;
  25. Bool8 m_constant = false;
  26. U16 m_arraySize = 0;
  27. Bool8 m_instanced = false;
  28. MPString m_line;
  29. GLbitfield m_shaderDefinedMask = 0; ///< Defined in
  30. GLbitfield m_shaderReferencedMask = 0; ///< Referenced by
  31. Bool8 m_inBlock = true;
  32. I16 m_binding = -1; ///< Texture unit
  33. I32 m_offset = -1; ///< Offset inside the UBO
  34. I32 m_arrayStride = -1;
  35. /// Identifying the stride between columns of a column-major matrix or
  36. /// rows of a row-major matrix
  37. I32 m_matrixStride = -1;
  38. MaterialProgramCreatorInputVariable()
  39. {}
  40. MaterialProgramCreatorInputVariable(MaterialProgramCreatorInputVariable&& b)
  41. {
  42. move(b);
  43. }
  44. ~MaterialProgramCreatorInputVariable()
  45. {
  46. m_name.destroy(m_alloc);
  47. m_typeStr.destroy(m_alloc);
  48. m_value.destroy(m_alloc);
  49. m_line.destroy(m_alloc);
  50. }
  51. MaterialProgramCreatorInputVariable& operator=(
  52. MaterialProgramCreatorInputVariable&& b)
  53. {
  54. move(b);
  55. return *this;
  56. }
  57. void move(MaterialProgramCreatorInputVariable& b)
  58. {
  59. m_alloc = std::move(b.m_alloc);
  60. m_name = std::move(b.m_name);
  61. m_type = b.m_type;
  62. m_typeStr = std::move(b.m_typeStr);
  63. m_value = std::move(b.m_value);
  64. m_constant = b.m_constant;
  65. m_arraySize = b.m_arraySize;
  66. m_instanced = b.m_instanced;
  67. m_line = std::move(b.m_line);
  68. m_shaderDefinedMask = b.m_shaderDefinedMask;
  69. m_shaderReferencedMask = b.m_shaderReferencedMask;
  70. m_inBlock = b.m_inBlock;
  71. m_binding = b.m_binding;
  72. m_offset = b.m_offset;
  73. m_arrayStride = b.m_arrayStride;
  74. m_matrixStride = b.m_matrixStride;
  75. }
  76. Bool duplicate(const MaterialProgramCreatorInputVariable& b) const
  77. {
  78. return b.m_name == m_name
  79. && b.m_type == m_type
  80. && b.m_value == m_value
  81. && b.m_constant == m_constant
  82. && b.m_arraySize == m_arraySize
  83. && b.m_instanced == m_instanced;
  84. }
  85. };
  86. /// Creator of shader programs. This class parses between
  87. /// @code <shaderProgams></shaderPrograms> @endcode located inside a
  88. /// @code <material></material> @endcode and creates the source of a custom
  89. /// program.
  90. ///
  91. /// @note Be carefull when you change the methods. Some change may create more
  92. /// unique shaders and this is never good.
  93. class MaterialProgramCreator
  94. {
  95. public:
  96. using MPString = TempResourceString;
  97. using MPStringList = StringListBase<TempResourceAllocator<char>>;
  98. using Input = MaterialProgramCreatorInputVariable;
  99. explicit MaterialProgramCreator(TempResourceAllocator<U8>& alloc);
  100. ~MaterialProgramCreator();
  101. /// Parse what is within the
  102. /// @code <programs></programs> @endcode
  103. ANKI_USE_RESULT Error parseProgramsTag(const XmlElement& el);
  104. /// Get the shader program source code
  105. const MPString& getProgramSource(ShaderType shaderType_) const
  106. {
  107. U shaderType = enumToType(shaderType_);
  108. ANKI_ASSERT(!m_sourceBaked[shaderType].isEmpty());
  109. return m_sourceBaked[shaderType];
  110. }
  111. const List<Input>& getInputVariables() const
  112. {
  113. return m_inputs;
  114. }
  115. Bool hasTessellation() const
  116. {
  117. return m_tessellation;
  118. }
  119. U32 getUniformBlockSize() const
  120. {
  121. return m_blockSize;
  122. }
  123. private:
  124. TempResourceAllocator<char> m_alloc;
  125. Array<MPStringList, 5> m_source; ///< Shader program final source
  126. Array<MPString, 5> m_sourceBaked; ///< Final source baked
  127. List<Input> m_inputs;
  128. MPStringList m_uniformBlock;
  129. GLbitfield m_uniformBlockReferencedMask = 0;
  130. U32 m_blockSize = 0;
  131. Bool8 m_instanced = false;
  132. U32 m_texBinding = 0;
  133. GLbitfield m_instanceIdMask = 0;
  134. Bool8 m_tessellation = false;
  135. /// Parse what is within the
  136. /// @code <program></program> @endcode
  137. ANKI_USE_RESULT Error parseProgramTag(const XmlElement& el);
  138. /// Parse what is within the @code <inputs></inputs> @endcode
  139. ANKI_USE_RESULT Error parseInputsTag(const XmlElement& programEl);
  140. /// Parse what is within the @code <operation></operation> @endcode
  141. ANKI_USE_RESULT Error parseOperationTag(
  142. const XmlElement& el, GLenum glshader,
  143. GLbitfield glshaderbit, MPString& out);
  144. };
  145. } // end namespace anki
  146. #endif