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-2014 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 
29 #ifndef RENDERSYSTEMS_GLSUPPORT_INCLUDE_GLSL_OGREGLSLPROGRAMCOMMON_H_
30 #define RENDERSYSTEMS_GLSUPPORT_INCLUDE_GLSL_OGREGLSLPROGRAMCOMMON_H_
31 
32 #include "OgreGLSupportPrerequisites.h"
33 #include "OgreConfig.h"
34 #include "OgreHardwareVertexBuffer.h"
35 #include "OgreGLSLShaderCommon.h"
36 #include "OgreHardwareUniformBuffer.h"
37 
38 namespace Ogre
39 {
40 /// Structure used to keep track of named uniforms in the linked program object
41 struct GLUniformReference
42 {
43     /// GL location handle
44     int mLocation;
45     /// Which type of program params will this value come from?
46     GpuProgramType mSourceProgType;
47     /// The constant definition it relates to
48     const GpuConstantDefinition* mConstantDef;
49 };
50 typedef std::vector<GLUniformReference> GLUniformReferenceList;
51 typedef GLUniformReferenceList::iterator GLUniformReferenceIterator;
52 
53 typedef std::vector<HardwareUniformBufferSharedPtr> GLUniformBufferList;
54 typedef GLUniformBufferList::iterator GLUniformBufferIterator;
55 
56 class GLSLProgramCommon
57 {
58 public:
59     GLSLProgramCommon(GLSLShaderCommon* vertexShader);
~GLSLProgramCommon()60     virtual ~GLSLProgramCommon() {}
61 
62     void extractLayoutQualifiers(void);
63 
64     /** Sets whether the linked program includes the required instructions
65         to perform skeletal animation.
66         @remarks
67         If this is set to true, OGRE will not blend the geometry according to
68         skeletal animation, it will expect the vertex program to do it.
69     */
setSkeletalAnimationIncluded(bool included)70     void setSkeletalAnimationIncluded(bool included) { mSkeletalAnimation = included; }
71 
72     /** Returns whether the linked program includes the required instructions
73         to perform skeletal animation.
74         @remarks
75          If this returns true, OGRE will not blend the geometry according to
76          skeletal animation, it will expect the vertex program to do it.
77                                                                          */
isSkeletalAnimationIncluded(void)78     bool isSkeletalAnimationIncluded(void) const { return mSkeletalAnimation; }
79 
80     /// Get the GL Handle for the program object
getGLProgramHandle(void)81     uint getGLProgramHandle(void) const { return mGLProgramHandle; }
82 
83     /** Makes a program object active by making sure it is linked and then putting it in use.
84      */
85     virtual void activate(void) = 0;
86 
87     /// query if the program is using the given shader
88     virtual bool isUsingShader(GLSLShaderCommon* shader) const = 0;
89 
90     /** Updates program object uniforms using data from GpuProgramParameters.
91         Normally called by GLSLShader::bindParameters() just before rendering occurs.
92     */
93     virtual void updateUniforms(GpuProgramParametersSharedPtr params, uint16 mask, GpuProgramType fromProgType) = 0;
94 
95     /** Updates program object uniform blocks using data from GpuProgramParameters.
96         Normally called by GLSLShader::bindParameters() just before rendering occurs.
97     */
98     virtual void updateUniformBlocks(GpuProgramParametersSharedPtr params, uint16 mask, GpuProgramType fromProgType) = 0;
99 
100     /** Updates program object uniforms using data from pass iteration GpuProgramParameters.
101         Normally called by GLSLShader::bindMultiPassParameters() just before multi pass rendering occurs.
102     */
103     virtual void updatePassIterationUniforms(GpuProgramParametersSharedPtr params) = 0;
104 
105     /** Get the fixed attribute bindings normally used by GL for a semantic. */
106     static int32 getFixedAttributeIndex(VertexElementSemantic semantic, uint index);
107 
108     /**
109      * use alternate vertex attribute layout using only 8 vertex attributes
110      *
111      * For "Vivante GC1000" and "VideoCore IV" (notably in Raspberry Pi) on GLES2
112      */
113     static void useTightAttributeLayout();
114 protected:
115     /// Container of uniform references that are active in the program object
116     GLUniformReferenceList mGLUniformReferences;
117     /// Container of uniform buffer references that are active in the program object
118     GLUniformBufferList mGLUniformBufferReferences;
119 
120     /// Linked vertex shader.
121     GLSLShaderCommon* mVertexShader;
122 
123     /// Flag to indicate that uniform references have already been built
124     bool mUniformRefsBuilt;
125     /// GL handle for the program object
126     uint mGLProgramHandle;
127     /// Flag indicating that the program or pipeline object has been successfully linked
128     int mLinked;
129     /// Flag indicating skeletal animation is being performed
130     bool mSkeletalAnimation;
131     /// A value to define the case we didn't look for the attributes since the contractor
132     static const int NULL_CUSTOM_ATTRIBUTES_INDEX = -2;
133     /// A value to define the attribute has not been found (this is also the result when glGetAttribLocation fails)
134     static const int NOT_FOUND_CUSTOM_ATTRIBUTES_INDEX = -1;
135 
136     /// An array to hold the attributes indexes
137     int mCustomAttributesIndexes[VES_COUNT][OGRE_MAX_TEXTURE_COORD_SETS];
138 
139     /// Compiles and links the vertex and fragment programs
140     virtual void compileAndLink(void) = 0;
141 
142     static VertexElementSemantic getAttributeSemanticEnum(const String& type);
143     static const char * getAttributeSemanticString(VertexElementSemantic semantic);
144 
145     /// Name / attribute list
146     struct CustomAttribute
147     {
148         const char* name;
149         int32 attrib;
150         VertexElementSemantic semantic;
151     };
152     static CustomAttribute msCustomAttributes[17];
153 };
154 
155 } /* namespace Ogre */
156 
157 #endif /* RENDERSYSTEMS_GLSUPPORT_INCLUDE_GLSL_OGREGLSLPROGRAMCOMMON_H_ */
158