MaterialProgramCreator.h 4.4 KB

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