BsGLSLParamParser.h 3.6 KB

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