BsGLSLParamParser.h 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  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. /**
  54. * Calculates the size and alignment of a single element within a shader interface block using the std140 layout.
  55. *
  56. * @param[in] type Type of the element. Structs are not supported.
  57. * @param[in] arraySize Number of array elements of the element (1 if it's not an array).
  58. * @param[in, out] offset Current location in some parent buffer at which the element should be placed at. If the
  59. * location doesn't match the element's alignment, the value will be modified to a valid
  60. * alignment. In multiples of 4 bytes.
  61. * @return Size of the element, in multiples of 4 bytes.
  62. */
  63. static UINT32 calcInterfaceBlockElementSizeAndOffset(GpuParamDataType type, UINT32 arraySize, UINT32& offset);
  64. private:
  65. /** Types of HLSL parameters. */
  66. enum class ParamType
  67. {
  68. UniformBlock,
  69. Texture,
  70. Sampler,
  71. Image,
  72. StorageBlock,
  73. Count // Keep at end
  74. };
  75. /**
  76. * Populates information for uniform with the specified index into the provided structure.
  77. *
  78. * @param[in] desc Output structure containing the parsed data.
  79. * @param[in] paramName Name of the uniform.
  80. * @param[in] programHandle Internal OpenGL handle to the GPU program.
  81. * @param[in] uniformIndex Unique uniform index to retrieve data from. Obtained from OpenGL parsing methods.
  82. */
  83. void determineParamInfo(GpuParamDataDesc& desc, const String& paramName, GLuint programHandle, GLuint uniformIndex);
  84. /**
  85. * Attempts to find out a vertex element semantic based on input parameter name. GLSL has no concept of semantics,
  86. * so we require all shaders to use specific names for attributes so that we know what they are used for.
  87. *
  88. * Valid names and semantics:
  89. * bs_position - VES_POSITION
  90. * bs_normal - VES_NORMAL
  91. * bs_tangent - VES_TANGENT
  92. * bs_bitangent - VES_BITANGENT
  93. * bs_texcoord - VES_TEXCOORD
  94. * bs_color - VES_COLOR
  95. * bs_blendweights - VES_BLEND_WEIGHTS
  96. * bs_blendindices - VES_BLEND_INDICES
  97. *
  98. * You may append a number to the end of the name to specify semantic index.
  99. *
  100. * @return True if it succeeds, false if it fails.
  101. */
  102. bool attribNameToElementSemantic(const String& name, VertexElementSemantic& semantic, UINT16& index);
  103. /** Converts an OpenGL type to vertex element type. */
  104. VertexElementType glTypeToAttributeType(GLenum glType);
  105. /** Maps a parameter in a specific shader stage, of a specific type to a unique set index. */
  106. static UINT32 mapParameterToSet(GpuProgramType progType, ParamType paramType);
  107. };
  108. /** @} */
  109. }}