CmGLSLProgram.h 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  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 __GLSLProgram_H__
  25. #define __GLSLProgram_H__
  26. #include "CmGLPrerequisites.h"
  27. #include "CmHighLevelGpuProgram.h"
  28. namespace CamelotEngine {
  29. /** Specialisation of HighLevelGpuProgram to provide support for OpenGL
  30. Shader Language (GLSL).
  31. @remarks
  32. GLSL has no target assembler or entry point specification like DirectX 9 HLSL.
  33. Vertex and Fragment shaders only have one entry point called "main".
  34. When a shader is compiled, microcode is generated but can not be accessed by
  35. the application.
  36. GLSL also does not provide assembler low level output after compiling. The GL Render
  37. system assumes that the Gpu program is a GL Gpu program so GLSLProgram will create a
  38. GLSLGpuProgram that is subclassed from GLGpuProgram for the low level implementation.
  39. The GLSLProgram class will create a shader object and compile the source but will
  40. not create a program object. It's up to GLSLGpuProgram class to request a program object
  41. to link the shader object to.
  42. @note
  43. GLSL supports multiple modular shader objects that can be attached to one program
  44. object to form a single shader. This is supported through the "attach" material script
  45. command. All the modules to be attached are listed on the same line as the attach command
  46. seperated by white space.
  47. */
  48. class _OgreGLExport GLSLProgram : public HighLevelGpuProgram
  49. {
  50. public:
  51. GLSLProgram();
  52. ~GLSLProgram();
  53. const GLhandleARB getGLHandle() const { return mGLHandle; }
  54. void attachToProgramObject( const GLhandleARB programObject );
  55. void detachFromProgramObject( const GLhandleARB programObject );
  56. String getAttachedShaderNames() const { return mAttachedShaderNames; }
  57. /// Overridden
  58. bool getPassTransformStates(void) const;
  59. bool getPassSurfaceAndLightStates(void) const;
  60. /** Attach another GLSL Shader to this one. */
  61. void attachChildShader(const String& name);
  62. /** Sets the preprocessor defines use to compile the program. */
  63. void setPreprocessorDefines(const String& defines) { mPreprocessorDefines = defines; }
  64. /** Sets the preprocessor defines use to compile the program. */
  65. const String& getPreprocessorDefines(void) const { return mPreprocessorDefines; }
  66. /// Overridden from GpuProgram
  67. const String& getLanguage(void) const;
  68. /** Returns the operation type that this geometry program expects to
  69. receive as input
  70. */
  71. virtual RenderOperation::OperationType getInputOperationType(void) const
  72. { return mInputOperationType; }
  73. /** Returns the operation type that this geometry program will emit
  74. */
  75. virtual RenderOperation::OperationType getOutputOperationType(void) const
  76. { return mOutputOperationType; }
  77. /** Returns the maximum number of vertices that this geometry program can
  78. output in a single run
  79. */
  80. virtual int getMaxOutputVertices(void) const { return mMaxOutputVertices; }
  81. /** Sets the operation type that this geometry program expects to receive
  82. */
  83. virtual void setInputOperationType(RenderOperation::OperationType operationType)
  84. { mInputOperationType = operationType; }
  85. /** Set the operation type that this geometry program will emit
  86. */
  87. virtual void setOutputOperationType(RenderOperation::OperationType operationType)
  88. { mOutputOperationType = operationType; }
  89. /** Set the maximum number of vertices that a single run of this geometry program
  90. can emit.
  91. */
  92. virtual void setMaxOutputVertices(int maxOutputVertices)
  93. { mMaxOutputVertices = maxOutputVertices; }
  94. protected:
  95. /** Internal load implementation, must be implemented by subclasses.
  96. */
  97. void loadFromSource(void);
  98. /** Internal method for creating a dummy low-level program for this
  99. high-level program. GLSL does not give access to the low level implementation of the
  100. shader so this method creates an object sub-classed from GLGpuProgram just to be
  101. compatible with GLRenderSystem.
  102. */
  103. void createLowLevelImpl(void);
  104. /// Internal unload implementation, must be implemented by subclasses
  105. void unloadHighLevelImpl(void);
  106. /// Overridden from HighLevelGpuProgram
  107. void unloadImpl(void);
  108. /// Populate the passed parameters with name->index map
  109. void populateParameterNames(GpuProgramParametersSharedPtr params);
  110. /// Populate the passed parameters with name->index map, must be overridden
  111. void buildConstantDefinitions() const;
  112. /// compile source into shader object
  113. bool compile( const bool checkErrors = true);
  114. private:
  115. /// GL handle for shader object
  116. GLhandleARB mGLHandle;
  117. /// flag indicating if shader object successfully compiled
  118. GLint mCompiled;
  119. /// The input operation type for this (geometry) program
  120. RenderOperation::OperationType mInputOperationType;
  121. /// The output operation type for this (geometry) program
  122. RenderOperation::OperationType mOutputOperationType;
  123. /// The maximum amount of vertices that this (geometry) program can output
  124. int mMaxOutputVertices;
  125. /// attached Shader names
  126. String mAttachedShaderNames;
  127. /// Preprocessor options
  128. String mPreprocessorDefines;
  129. /// container of attached programs
  130. typedef vector< GLSLProgram* >::type GLSLProgramContainer;
  131. typedef GLSLProgramContainer::iterator GLSLProgramContainerIterator;
  132. GLSLProgramContainer mAttachedGLSLPrograms;
  133. };
  134. }
  135. #endif // __GLSLProgram_H__