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 __GLSLLinkProgram_H__
29 #define __GLSLLinkProgram_H__
30 
31 #include "OgreGLPrerequisites.h"
32 #include "OgreGpuProgram.h"
33 #include "OgreHardwareVertexBuffer.h"
34 #include "OgreGLUniformCache.h"
35 
36 namespace Ogre {
37     namespace GLSL {
38 
39     /// Structure used to keep track of named uniforms in the linked program object
40 	struct GLUniformReference
41 	{
42 		/// GL location handle
43 		GLint  mLocation;
44 		/// Which type of program params will this value come from?
45 		GpuProgramType mSourceProgType;
46 		/// The constant definition it relates to
47 		const GpuConstantDefinition* mConstantDef;
48 	};
49 
50 	typedef vector<GLUniformReference>::type GLUniformReferenceList;
51 	typedef GLUniformReferenceList::iterator GLUniformReferenceIterator;
52 
53 	/** C++ encapsulation of GLSL Program Object
54 
55 	*/
56 
57 	class _OgreGLExport GLSLLinkProgram
58 	{
59 	private:
60 		/// Container of uniform references that are active in the program object
61 		GLUniformReferenceList mGLUniformReferences;
62 
63 		/// Linked vertex program
64 		GLSLGpuProgram* mVertexProgram;
65 		/// Linked geometry program
66 		GLSLGpuProgram* mGeometryProgram;
67 		/// Linked fragment program
68 		GLSLGpuProgram* mFragmentProgram;
69         GLUniformCache *mUniformCache;
70 
71 		/// Flag to indicate that uniform references have already been built
72 		bool		mUniformRefsBuilt;
73 		/// GL handle for the program object
74 		GLhandleARB mGLHandle;
75 		/// Flag indicating that the program object has been successfully linked
76 		GLint		mLinked;
77 		/// Flag indicating that the program object has tried to link and failed
78 		bool		mTriedToLinkAndFailed;
79 		/// Flag indicating skeletal animation is being performed
80 		bool mSkeletalAnimation;
81 
82 		/// Build uniform references from active named uniforms
83 		void buildGLUniformReferences(void);
84 		/// Extract attributes
85 		void extractAttributes(void);
86 
87 		typedef set<GLuint>::type AttributeSet;
88 		/// Custom attribute bindings
89 		AttributeSet mValidAttributes;
90 
91 		/// Name / attribute list
92 		struct CustomAttribute
93 		{
94 			String name;
95 			GLuint attrib;
CustomAttributeCustomAttribute96 			CustomAttribute(const String& _name, GLuint _attrib)
97 				:name(_name), attrib(_attrib) {}
98 		};
99 
100 		static CustomAttribute msCustomAttributes[];
101 
102 		String getCombinedName();
103 		/// Compiles and links the the vertex and fragment programs
104 		void compileAndLink();
105 		/// Get the the binary data of a program from the microcode cache
106 		void getMicrocodeFromCache();
107 	public:
108 		/// Constructor should only be used by GLSLLinkProgramManager
109 		GLSLLinkProgram(GLSLGpuProgram* vertexProgram, GLSLGpuProgram* geometryProgram, GLSLGpuProgram* fragmentProgram);
110 		~GLSLLinkProgram(void);
111 
112 		/** Makes a program object active by making sure it is linked and then putting it in use.
113 
114 		*/
115 		void activate(void);
116 
117 		/** Updates program object uniforms using data from GpuProgramParameters.
118 		normally called by GLSLGpuProgram::bindParameters() just before rendering occurs.
119 		*/
120 		void updateUniforms(GpuProgramParametersSharedPtr params, uint16 mask, GpuProgramType fromProgType);
121 		/** Updates program object uniforms using data from pass iteration GpuProgramParameters.
122 		normally called by GLSLGpuProgram::bindMultiPassParameters() just before multi pass rendering occurs.
123 		*/
124 		void updatePassIterationUniforms(GpuProgramParametersSharedPtr params);
125 		/// Get the GL Handle for the program object
getGLHandle(void)126 		GLhandleARB getGLHandle(void) const { return mGLHandle; }
127         /** Sets whether the linked program includes the required instructions
128         to perform skeletal animation.
129         @remarks
130         If this is set to true, OGRE will not blend the geometry according to
131         skeletal animation, it will expect the vertex program to do it.
132         */
setSkeletalAnimationIncluded(bool included)133         void setSkeletalAnimationIncluded(bool included)
134         { mSkeletalAnimation = included; }
135 
136         /** Returns whether the linked program includes the required instructions
137             to perform skeletal animation.
138         @remarks
139             If this returns true, OGRE will not blend the geometry according to
140             skeletal animation, it will expect the vertex program to do it.
141         */
isSkeletalAnimationIncluded(void)142         bool isSkeletalAnimationIncluded(void) const { return mSkeletalAnimation; }
143 
144 		/// Get the index of a non-standard attribute bound in the linked code
145 		GLuint getAttributeIndex(VertexElementSemantic semantic, uint index);
146 		/// Is a non-standard attribute bound in the linked code?
147 		bool isAttributeValid(VertexElementSemantic semantic, uint index);
148 
149 	};
150 
151     }
152 }
153 
154 #endif // __GLSLLinkProgram_H__
155