BsGLSLParamParser.h 3.6 KB

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