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 
29 #include "OgreGLSLESGpuProgram.h"
30 #include "OgreGLSLESProgram.h"
31 #include "OgreGLSLESLinkProgramManager.h"
32 #include "OgreGLSLESProgramPipelineManager.h"
33 #include "OgreRoot.h"
34 
35 namespace Ogre {
36 
37 	GLuint GLSLESGpuProgram::mVertexShaderCount = 0;
38 	GLuint GLSLESGpuProgram::mFragmentShaderCount = 0;
39     //-----------------------------------------------------------------------------
GLSLESGpuProgram(GLSLESProgram * parent)40 	GLSLESGpuProgram::GLSLESGpuProgram(GLSLESProgram* parent) :
41         GLES2GpuProgram(parent->getCreator(), parent->getName(), parent->getHandle(),
42             parent->getGroup(), false, 0), mGLSLProgram(parent)
43     {
44         mType = parent->getType();
45         mSyntaxCode = "glsles";
46 
47         mLinked = 0;
48 
49 		if (parent->getType() == GPT_VERTEX_PROGRAM)
50 		{
51 			mProgramID = ++mVertexShaderCount;
52 		}
53 		else if (parent->getType() == GPT_FRAGMENT_PROGRAM)
54 		{
55 			mProgramID = ++mFragmentShaderCount;
56 		}
57 
58         // Transfer skeletal animation status from parent
59         mSkeletalAnimation = mGLSLProgram->isSkeletalAnimationIncluded();
60 		// There is nothing to load
61 		mLoadFromFile = false;
62     }
63     //-----------------------------------------------------------------------
~GLSLESGpuProgram()64     GLSLESGpuProgram::~GLSLESGpuProgram()
65     {
66         // Have to call this here rather than in Resource destructor
67         // since calling virtual methods in base destructors causes crash
68         unload();
69     }
70 	//-----------------------------------------------------------------------------
loadImpl(void)71     void GLSLESGpuProgram::loadImpl(void)
72     {
73 		// nothing to load
74     }
75 
76 	//-----------------------------------------------------------------------------
unloadImpl(void)77 	void GLSLESGpuProgram::unloadImpl(void)
78 	{
79 		// nothing to unload
80 	}
81 
82 	//-----------------------------------------------------------------------------
loadFromSource(void)83     void GLSLESGpuProgram::loadFromSource(void)
84     {
85 		// nothing to load
86 	}
87 
88 	//-----------------------------------------------------------------------------
bindProgram(void)89 	void GLSLESGpuProgram::bindProgram(void)
90 	{
91         if(Root::getSingleton().getRenderSystem()->getCapabilities()->hasCapability(RSC_SEPARATE_SHADER_OBJECTS))
92         {
93             // Tell the Program Pipeline Manager what pipeline is to become active
94             switch (mType)
95             {
96                 case GPT_VERTEX_PROGRAM:
97                     GLSLESProgramPipelineManager::getSingleton().setActiveVertexLinkProgram( this );
98                     break;
99                 case GPT_FRAGMENT_PROGRAM:
100                     GLSLESProgramPipelineManager::getSingleton().setActiveFragmentLinkProgram( this );
101                     break;
102                 case GPT_GEOMETRY_PROGRAM:
103                 default:
104                     break;
105             }
106         }
107         else
108         {
109             // Tell the Link Program Manager what shader is to become active
110             switch (mType)
111             {
112                 case GPT_VERTEX_PROGRAM:
113                     GLSLESLinkProgramManager::getSingleton().setActiveVertexShader( this );
114                     break;
115                 case GPT_FRAGMENT_PROGRAM:
116                     GLSLESLinkProgramManager::getSingleton().setActiveFragmentShader( this );
117                     break;
118                 case GPT_GEOMETRY_PROGRAM:
119                 default:
120                     break;
121             }
122         }
123 	}
124 
125 	//-----------------------------------------------------------------------------
unbindProgram(void)126 	void GLSLESGpuProgram::unbindProgram(void)
127 	{
128         if(Root::getSingleton().getRenderSystem()->getCapabilities()->hasCapability(RSC_SEPARATE_SHADER_OBJECTS))
129         {
130             // Tell the Program Pipeline Manager what pipeline is to become inactive
131             if (mType == GPT_VERTEX_PROGRAM)
132             {
133                 GLSLESProgramPipelineManager::getSingleton().setActiveVertexLinkProgram(NULL);
134             }
135             else if (mType == GPT_FRAGMENT_PROGRAM)
136             {
137                 GLSLESProgramPipelineManager::getSingleton().setActiveFragmentLinkProgram(NULL);
138             }
139         }
140         else
141         {
142             // Tell the Link Program Manager what shader is to become inactive
143             if (mType == GPT_VERTEX_PROGRAM)
144             {
145                 GLSLESLinkProgramManager::getSingleton().setActiveVertexShader( NULL );
146             }
147             else if (mType == GPT_FRAGMENT_PROGRAM)
148             {
149                 GLSLESLinkProgramManager::getSingleton().setActiveFragmentShader( NULL );
150             }
151         }
152 	}
153 
154 	//-----------------------------------------------------------------------------
bindProgramParameters(GpuProgramParametersSharedPtr params,uint16 mask)155 	void GLSLESGpuProgram::bindProgramParameters(GpuProgramParametersSharedPtr params, uint16 mask)
156 	{
157 		// Link can throw exceptions, ignore them at this point
158 		try
159 		{
160             if(Root::getSingleton().getRenderSystem()->getCapabilities()->hasCapability(RSC_SEPARATE_SHADER_OBJECTS))
161             {
162                 // Activate the program pipeline object
163                 GLSLESProgramPipeline* programPipeline = GLSLESProgramPipelineManager::getSingleton().getActiveProgramPipeline();
164                 // Pass on parameters from params to program object uniforms
165                 programPipeline->updateUniforms(params, mask, mType);
166             }
167             else
168             {
169                 // Activate the link program object
170                 GLSLESLinkProgram* linkProgram = GLSLESLinkProgramManager::getSingleton().getActiveLinkProgram();
171                 // Pass on parameters from params to program object uniforms
172                 linkProgram->updateUniforms(params, mask, mType);
173             }
174 		}
175 		catch (Exception& e) {}
176 	}
177 
178     //-----------------------------------------------------------------------------
bindProgramSharedParameters(GpuProgramParametersSharedPtr params,uint16 mask)179 	void GLSLESGpuProgram::bindProgramSharedParameters(GpuProgramParametersSharedPtr params, uint16 mask)
180 	{
181 		// Link can throw exceptions, ignore them at this point
182 		try
183 		{
184             if(Root::getSingleton().getRenderSystem()->getCapabilities()->hasCapability(RSC_SEPARATE_SHADER_OBJECTS))
185             {
186                 // Activate the program pipeline object
187                 GLSLESProgramPipeline* programPipeline = GLSLESProgramPipelineManager::getSingleton().getActiveProgramPipeline();
188                 // Pass on parameters from params to program object uniforms
189                 programPipeline->updateUniformBlocks(params, mask, mType);
190             }
191             else
192             {
193                 // Activate the link program object
194                 GLSLESLinkProgram* linkProgram = GLSLESLinkProgramManager::getSingleton().getActiveLinkProgram();
195                 // Pass on parameters from params to program object uniforms
196                 linkProgram->updateUniformBlocks(params, mask, mType);
197             }
198 		}
199 		catch (Exception& e) {}
200 	}
201 
202 	//-----------------------------------------------------------------------------
bindProgramPassIterationParameters(GpuProgramParametersSharedPtr params)203 	void GLSLESGpuProgram::bindProgramPassIterationParameters(GpuProgramParametersSharedPtr params)
204 	{
205         if(Root::getSingleton().getRenderSystem()->getCapabilities()->hasCapability(RSC_SEPARATE_SHADER_OBJECTS))
206         {
207             // Activate the program pipeline object
208             GLSLESProgramPipeline* programPipeline = GLSLESProgramPipelineManager::getSingleton().getActiveProgramPipeline();
209             // Pass on parameters from params to program object uniforms
210             programPipeline->updatePassIterationUniforms( params );
211         }
212         else
213         {
214             // Activate the link program object
215             GLSLESLinkProgram* linkProgram = GLSLESLinkProgramManager::getSingleton().getActiveLinkProgram();
216             // Pass on parameters from params to program object uniforms
217             linkProgram->updatePassIterationUniforms( params );
218         }
219 	}
220 }
221