CmGLSLLinkProgramManager.h 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. /*
  2. -----------------------------------------------------------------------------
  3. This source file is part of OGRE
  4. (Object-oriented Graphics Rendering Engine)
  5. For the latest info, see http://www.ogre3d.org/
  6. Copyright (c) 2000-2011 Torus Knot Software Ltd
  7. Permission is hereby granted, free of charge, to any person obtaining a copy
  8. of this software and associated documentation files (the "Software"), to deal
  9. in the Software without restriction, including without limitation the rights
  10. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  11. copies of the Software, and to permit persons to whom the Software is
  12. furnished to do so, subject to the following conditions:
  13. The above copyright notice and this permission notice shall be included in
  14. all copies or substantial portions of the Software.
  15. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  18. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  20. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  21. THE SOFTWARE.
  22. -----------------------------------------------------------------------------
  23. */
  24. #ifndef __GLSLLinkProgramManager_H__
  25. #define __GLSLLinkProgramManager_H__
  26. #include "CmGLPrerequisites.h"
  27. #include "OgreSingleton.h"
  28. #include "CmGLSLExtSupport.h"
  29. #include "CmGLSLLinkProgram.h"
  30. namespace CamelotEngine {
  31. /** Ogre assumes that there are seperate vertex and fragment programs to deal with but
  32. GLSL has one program object that represents the active vertex and fragment shader objects
  33. during a rendering state. GLSL Vertex and fragment
  34. shader objects are compiled seperately and then attached to a program object and then the
  35. program object is linked. Since Ogre can only handle one vertex program and one fragment
  36. program being active in a pass, the GLSL Link Program Manager does the same. The GLSL Link
  37. program manager acts as a state machine and activates a program object based on the active
  38. vertex and fragment program. Previously created program objects are stored along with a unique
  39. key in a hash_map for quick retrieval the next time the program object is required.
  40. */
  41. class _OgreGLExport GLSLLinkProgramManager : public Singleton<GLSLLinkProgramManager>
  42. {
  43. private:
  44. typedef map<UINT64, GLSLLinkProgram*>::type LinkProgramMap;
  45. typedef LinkProgramMap::iterator LinkProgramIterator;
  46. /// container holding previously created program objects
  47. LinkProgramMap mLinkPrograms;
  48. /// active objects defining the active rendering gpu state
  49. GLSLGpuProgram* mActiveVertexGpuProgram;
  50. GLSLGpuProgram* mActiveGeometryGpuProgram;
  51. GLSLGpuProgram* mActiveFragmentGpuProgram;
  52. GLSLLinkProgram* mActiveLinkProgram;
  53. typedef map<String, GLenum>::type StringToEnumMap;
  54. StringToEnumMap mTypeEnumMap;
  55. /// Use type to complete other information
  56. void completeDefInfo(GLenum gltype, GpuConstantDefinition& defToUpdate);
  57. /// Find where the data for a specific uniform should come from, populate
  58. bool completeParamSource(const String& paramName,
  59. const GpuConstantDefinitionMap* vertexConstantDefs,
  60. const GpuConstantDefinitionMap* geometryConstantDefs,
  61. const GpuConstantDefinitionMap* fragmentConstantDefs,
  62. GLUniformReference& refToUpdate);
  63. public:
  64. GLSLLinkProgramManager(void);
  65. ~GLSLLinkProgramManager(void);
  66. /**
  67. Get the program object that links the two active shader objects together
  68. if a program object was not already created and linked a new one is created and linked
  69. */
  70. GLSLLinkProgram* getActiveLinkProgram(void);
  71. /** Set the active fragment shader for the next rendering state.
  72. The active program object will be cleared.
  73. Normally called from the GLSLGpuProgram::bindProgram and unbindProgram methods
  74. */
  75. void setActiveFragmentShader(GLSLGpuProgram* fragmentGpuProgram);
  76. /** Set the active geometry shader for the next rendering state.
  77. The active program object will be cleared.
  78. Normally called from the GLSLGpuProgram::bindProgram and unbindProgram methods
  79. */
  80. void setActiveGeometryShader(GLSLGpuProgram* geometryGpuProgram);
  81. /** Set the active vertex shader for the next rendering state.
  82. The active program object will be cleared.
  83. Normally called from the GLSLGpuProgram::bindProgram and unbindProgram methods
  84. */
  85. void setActiveVertexShader(GLSLGpuProgram* vertexGpuProgram);
  86. /** Populate a list of uniforms based on a program object.
  87. @param programObject Handle to the program object to query
  88. @param vertexConstantDefs Definition of the constants extracted from the
  89. vertex program, used to match up physical buffer indexes with program
  90. uniforms. May be null if there is no vertex program.
  91. @param geometryConstantDefs Definition of the constants extracted from the
  92. geometry program, used to match up physical buffer indexes with program
  93. uniforms. May be null if there is no geometry program.
  94. @param fragmentConstantDefs Definition of the constants extracted from the
  95. fragment program, used to match up physical buffer indexes with program
  96. uniforms. May be null if there is no fragment program.
  97. @param list The list to populate (will not be cleared before adding, clear
  98. it yourself before calling this if that's what you want).
  99. */
  100. void extractUniforms(GLhandleARB programObject,
  101. const GpuConstantDefinitionMap* vertexConstantDefs,
  102. const GpuConstantDefinitionMap* geometryConstantDefs,
  103. const GpuConstantDefinitionMap* fragmentConstantDefs,
  104. GLUniformReferenceList& list);
  105. /** Populate a list of uniforms based on GLSL source.
  106. @param src Reference to the source code
  107. @param list The defs to populate (will not be cleared before adding, clear
  108. it yourself before calling this if that's what you want).
  109. @param filename The file name this came from, for logging errors.
  110. */
  111. void extractConstantDefs(const String& src, GpuNamedConstants& constantDefs,
  112. const String& filename);
  113. static GLSLLinkProgramManager& getSingleton(void);
  114. static GLSLLinkProgramManager* getSingletonPtr(void);
  115. };
  116. }
  117. #endif // __GLSLLinkProgramManager_H__