BsGLSLParamParser.h 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  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 "RenderAPI/BsVertexDeclaration.h"
  6. #include "Debug/BsDebug.h"
  7. #include "Error/BsException.h"
  8. #include "RenderAPI/BsGpuParamDesc.h"
  9. namespace bs { namespace ct
  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] type Type of the GPU program we're parsing.
  43. * @param[out] returnParamDesc Output structure containing the parsed data.
  44. */
  45. void buildUniformDescriptions(GLuint glProgram, GpuProgramType type, GpuParamDesc& returnParamDesc);
  46. /**
  47. * Parses a compiled OpenGL program and outputs vertex element list that describes input attributes to the program.
  48. * Only valid for vertex programs.
  49. *
  50. * @param[in] glProgram OpenGL handle to the GPU program.
  51. */
  52. List<VertexElement> buildVertexDeclaration(GLuint glProgram);
  53. private:
  54. /** Types of HLSL parameters. */
  55. enum class ParamType
  56. {
  57. UniformBlock,
  58. Texture,
  59. Sampler,
  60. Image,
  61. StorageBlock,
  62. Count // Keep at end
  63. };
  64. /**
  65. * Populates information for uniform with the specified index into the provided structure.
  66. *
  67. * @param[in] desc Output structure containing the parsed data.
  68. * @param[in] paramName Name of the uniform.
  69. * @param[in] programHandle Internal OpenGL handle to the GPU program.
  70. * @param[in] 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. GLSL has no concept of semantics,
  75. * so we require all shaders to use specific names for attributes 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. /** Converts an OpenGL type to vertex element type. */
  93. VertexElementType glTypeToAttributeType(GLenum glType);
  94. /** Maps a parameter in a specific shader stage, of a specific type to a unique set index. */
  95. static UINT32 mapParameterToSet(GpuProgramType progType, ParamType paramType);
  96. };
  97. /** @} */
  98. }}