BsGLSLParamParser.h 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. #pragma once
  2. #include "BsGLPrerequisites.h"
  3. #include "BsVertexDeclaration.h"
  4. #include "BsDebug.h"
  5. #include "BsException.h"
  6. #include "BsGpuParamDesc.h"
  7. namespace BansheeEngine
  8. {
  9. /**
  10. * @brief Holds a GLSL program input attribute used in vertex programs.
  11. */
  12. struct GLSLAttribute
  13. {
  14. /**
  15. * @brief Constructs a new attribute from a name and a semantic that represents
  16. * in which way is the attribute used.
  17. */
  18. GLSLAttribute(const String& name, VertexElementSemantic semantic)
  19. :mName(name), mSemantic(semantic)
  20. { }
  21. /**
  22. * @brief Return true if attribute name matches the specified name
  23. * and returns optional semantic index if it exists. Start of the
  24. * two compared strings must match, and the remaining non-matching
  25. * bit will be assumed to be the semantic index. Returns -1 if no
  26. * match is made.
  27. */
  28. INT32 matchesName(const String& name);
  29. /**
  30. * @brief Returns the semantic of this attribute.
  31. */
  32. VertexElementSemantic getSemantic() const { return mSemantic; }
  33. private:
  34. String mName;
  35. VertexElementSemantic mSemantic;
  36. };
  37. /**
  38. * @brief Helper class that is able to parse a GLSL GPU program and retrieve
  39. * used uniforms and input attributes.
  40. */
  41. class GLSLParamParser
  42. {
  43. public:
  44. /**
  45. * @brief Parses a compiled OpenGL program and outputs a parameter description
  46. * that contains information about used uniforms.
  47. *
  48. * @param glProgram OpenGL handle to the GPU program.
  49. * @param returnParamDesc Output structure containing the parsed data.
  50. */
  51. void buildUniformDescriptions(GLuint glProgram, GpuParamDesc& returnParamDesc);
  52. /**
  53. * @brief Parses a compiled OpenGL program and outputs vertex element list that
  54. * describes input attributes to the program. Only valid for vertex programs.
  55. *
  56. * @param glProgram OpenGL handle to the GPU program.
  57. */
  58. List<VertexElement> buildVertexDeclaration(GLuint glProgram);
  59. private:
  60. /**
  61. * @brief Populates information for uniform with the specified index into the
  62. * provided structure.
  63. *
  64. * @param desc Output structure containing the parsed data.
  65. * @param paramName Name of the uniform.
  66. * @param programHandle Internal OpenGL handle to the GPU program.
  67. * @param uniformIndex Unique uniform index to retrieve data from. Obtained from OpenGL parsing methods.
  68. */
  69. void determineParamInfo(GpuParamDataDesc& desc, const String& paramName, GLuint programHandle, GLuint uniformIndex);
  70. /**
  71. * Attempts to find out a vertex element semantic based on input parameter name.
  72. * GLSL has no concept of semantics, so we require all shaders to use specific names for attributes
  73. * so that we know what they are used for.
  74. *
  75. * Valid names and semantics:
  76. * bs_position - VES_POSITION
  77. * bs_normal - VES_NORMAL
  78. * bs_tangent - VES_TANGENT
  79. * bs_bitangent - VES_BITANGENT
  80. * bs_texcoord - VES_TEXCOORD
  81. * bs_color - VES_COLOR
  82. * bs_blendweights - VES_BLEND_WEIGHTS
  83. * bs_blendindices - VES_BLEND_INDICES
  84. *
  85. * You may append a number to the end of the name to specify semantic index.
  86. *
  87. * @return True if it succeeds, false if it fails.
  88. */
  89. bool attribNameToElementSemantic(const String& name, VertexElementSemantic& semantic, UINT16& index);
  90. /**
  91. * @brief Converts an OpenGL type to vertex element type.
  92. */
  93. VertexElementType glTypeToAttributeType(GLenum glType);
  94. };
  95. }