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 
7  Copyright (c) 2000-2013 Torus Knot Software Ltd
8 
9  Permission is hereby granted, free of charge, to any person obtaining a copy
10  of this software and associated documentation files (the "Software"), to deal
11  in the Software without restriction, including without limitation the rights
12  to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
13  copies of the Software, and to permit persons to whom the Software is
14  furnished to do so, subject to the following conditions:
15 
16  The above copyright notice and this permission notice shall be included in
17  all copies or substantial portions of the Software.
18 
19  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
22  AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
23  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
24  OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
25  THE SOFTWARE.
26  -----------------------------------------------------------------------------
27  */
28 #ifndef __GLSLProgramCommon_H__
29 #define __GLSLProgramCommon_H__
30 
31 #include "OgreGL3PlusPrerequisites.h"
32 #include "OgreGpuProgram.h"
33 #include "OgreHardwareVertexBuffer.h"
34 #include "OgreGL3PlusHardwareUniformBuffer.h"
35 #include "OgreGL3PlusVertexArrayObject.h"
36 
37 namespace Ogre {
38 
39     class GLSLGpuProgram;
40 
41 	/// Structure used to keep track of named uniforms in the linked program object
42 	struct GLUniformReference
43 	{
44 		/// GL location handle
45 		GLint mLocation;
46 		/// Which type of program params will this value come from?
47 		GpuProgramType mSourceProgType;
48 		/// The constant definition it relates to
49 		const GpuConstantDefinition* mConstantDef;
50 	};
51 
52 	typedef vector<GLUniformReference>::type GLUniformReferenceList;
53 	typedef GLUniformReferenceList::iterator GLUniformReferenceIterator;
54     typedef vector<HardwareUniformBufferSharedPtr>::type GLUniformBufferList;
55 	typedef GLUniformBufferList::iterator GLUniformBufferIterator;
56 
57 	/** C++ encapsulation of GLSL Program Object
58 
59      */
60 
61 	class _OgreGL3PlusExport GLSLProgramCommon
62 	{
63 	protected:
64 		/// Container of uniform references that are active in the program object
65 		GLUniformReferenceList mGLUniformReferences;
66 
67 		/// Container of uniform buffer references that are active in the program object
68 		GLUniformBufferList mGLUniformBufferReferences;
69 
70 		/// Linked vertex program
71 		GLSLGpuProgram* mVertexProgram;
72 		/// Linked fragment program
73 		GLSLGpuProgram* mFragmentProgram;
74 		/// Linked geometry program
75 		GLSLGpuProgram* mGeometryProgram;
76 		/// Linked hull(control) program
77 		GLSLGpuProgram* mHullProgram;
78 		/// Linked domain(evaluation) program
79 		GLSLGpuProgram* mDomainProgram;
80 		/// Linked compute program
81 		GLSLGpuProgram* mComputeProgram;
82         /// GL handle for the vertex array object
83         GL3PlusVertexArrayObject *mVertexArrayObject;
84 
85 		/// Flag to indicate that uniform references have already been built
86 		bool mUniformRefsBuilt;
87 		/// GL handle for the program object
88 		GLuint mGLProgramHandle;
89 		/// Flag indicating that the program or pipeline object has been successfully linked
90 		GLint mLinked;
91 		/// Flag indicating that the program or pipeline object has tried to link and failed
92 		bool mTriedToLinkAndFailed;
93 		/// Flag indicating skeletal animation is being performed
94 		bool mSkeletalAnimation;
95 
96 		/// Build uniform references from active named uniforms
97 		void buildGLUniformReferences(void);
98 		typedef set<GLuint>::type AttributeSet;
99 
100 		/// An array to hold the attributes indexes
101 		GLint mCustomAttributesIndexes[VES_COUNT][OGRE_MAX_TEXTURE_COORD_SETS];
102         /// A value to define the case we didn't look for the attributes since the contractor
103 #define NULL_CUSTOM_ATTRIBUTES_INDEX -2
104         /// A value to define the attribute has not been found (this is also the result when glGetAttribLocation fails)
105 #define NOT_FOUND_CUSTOM_ATTRIBUTES_INDEX -1
106 
107 		Ogre::String getCombinedName(void);
108 		/// Get the the binary data of a program from the microcode cache
109 		void getMicrocodeFromCache(void);
110 		/// Compiles and links the vertex and fragment programs
111 		virtual void compileAndLink(void) = 0;
112         /// Put a program in use
113         virtual void _useProgram(void) = 0;
114 
115         typedef map<String, VertexElementSemantic>::type SemanticToStringMap;
116         SemanticToStringMap mSemanticTypeMap;
117 
118         VertexElementSemantic getAttributeSemanticEnum(String type);
119         const char * getAttributeSemanticString(VertexElementSemantic semantic);
120 
121 	public:
122 		/// Constructor should only be used by GLSLLinkProgramManager and GLSLProgramPipelineManager
123 		GLSLProgramCommon(GLSLGpuProgram* vertexProgram, GLSLGpuProgram* geometryProgram, GLSLGpuProgram* fragmentProgram, GLSLGpuProgram* hullProgram, GLSLGpuProgram* domainProgram, GLSLGpuProgram* computeProgram);
124 		virtual ~GLSLProgramCommon(void);
125 
126 		/** Makes a program object active by making sure it is linked and then putting it in use.
127          */
128 		virtual void activate(void) = 0;
129 
130 		/** Updates program object uniforms using data from GpuProgramParameters.
131          normally called by GLSLGpuProgram::bindParameters() just before rendering occurs.
132          */
133 		virtual void updateUniforms(GpuProgramParametersSharedPtr params, uint16 mask, GpuProgramType fromProgType) = 0;
134 		/** Updates program object uniform blocks using data from GpuProgramParameters.
135          normally called by GLSLGpuProgram::bindParameters() just before rendering occurs.
136          */
137 		virtual void updateUniformBlocks(GpuProgramParametersSharedPtr params, uint16 mask, GpuProgramType fromProgType) = 0;
138 		/** Updates program object uniforms using data from pass iteration GpuProgramParameters.
139          normally called by GLSLGpuProgram::bindMultiPassParameters() just before multi pass rendering occurs.
140          */
141 		virtual void updatePassIterationUniforms(GpuProgramParametersSharedPtr params) = 0;
142         /// Finds layout qualifiers in the shader source and sets attribute indices appropriately
143         virtual void extractLayoutQualifiers(void);
144 		/// Get the GL Handle for the program object
getGLProgramHandle(void)145 		GLuint getGLProgramHandle(void) const { return mGLProgramHandle; }
146         /** Sets whether the linked program includes the required instructions
147          to perform skeletal animation.
148          @remarks
149          If this is set to true, OGRE will not blend the geometry according to
150          skeletal animation, it will expect the vertex program to do it.
151          */
setSkeletalAnimationIncluded(bool included)152         void setSkeletalAnimationIncluded(bool included) { mSkeletalAnimation = included; }
153 
154         /** Returns whether the linked program includes the required instructions
155          to perform skeletal animation.
156          @remarks
157          If this returns true, OGRE will not blend the geometry according to
158          skeletal animation, it will expect the vertex program to do it.
159          */
isSkeletalAnimationIncluded(void)160         bool isSkeletalAnimationIncluded(void) const { return mSkeletalAnimation; }
161 
162 		/// Get the index of a non-standard attribute bound in the linked code
163 		virtual GLint getAttributeIndex(VertexElementSemantic semantic, uint index);
164 		/// Is a non-standard attribute bound in the linked code?
165 		bool isAttributeValid(VertexElementSemantic semantic, uint index);
166 
getVertexProgram()167 		GLSLGpuProgram* getVertexProgram() const { return mVertexProgram; }
getFragmentProgram()168 		GLSLGpuProgram* getFragmentProgram() const { return mFragmentProgram; }
getGeometryProgram()169 		GLSLGpuProgram* getGeometryProgram() const { return mGeometryProgram; }
getHullProgram()170 		GLSLGpuProgram* getHullProgram() const { return mHullProgram; }
getDomainProgram()171 		GLSLGpuProgram* getDomainProgram() const { return mDomainProgram; }
getComputeProgram()172 		GLSLGpuProgram* getComputeProgram() const { return mComputeProgram; }
getVertexArrayObject()173         GL3PlusVertexArrayObject* getVertexArrayObject() { return mVertexArrayObject; }
174 	};
175 }
176 
177 #endif // __GLSLProgramCommon_H__
178