BsGLSLParamParser.h 3.7 KB

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