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 "OgreGLSLProgramPipelineManager.h"
30 #include "OgreGLSLGpuProgram.h"
31 #include "OgreGLSLProgram.h"
32 
33 namespace Ogre
34 {
35     //-----------------------------------------------------------------------
36 	template<> GLSLProgramPipelineManager* Singleton<GLSLProgramPipelineManager>::msSingleton = 0;
37 
38 	//-----------------------------------------------------------------------
getSingletonPtr(void)39     GLSLProgramPipelineManager* GLSLProgramPipelineManager::getSingletonPtr(void)
40     {
41         return msSingleton;
42     }
43 
44 	//-----------------------------------------------------------------------
getSingleton(void)45     GLSLProgramPipelineManager& GLSLProgramPipelineManager::getSingleton(void)
46     {
47         assert( msSingleton );  return ( *msSingleton );
48     }
49 
GLSLProgramPipelineManager(void)50     GLSLProgramPipelineManager::GLSLProgramPipelineManager(void) :
51         GLSLProgramManagerCommon(), mActiveProgramPipeline(NULL) { }
52 
~GLSLProgramPipelineManager(void)53     GLSLProgramPipelineManager::~GLSLProgramPipelineManager(void)
54     {
55 		// Iterate through map container and delete program pipelines
56 		for (ProgramPipelineIterator currentProgram = mProgramPipelines.begin();
57              currentProgram != mProgramPipelines.end(); ++currentProgram)
58 		{
59 			delete currentProgram->second;
60 		}
61     }
62 
63     //-----------------------------------------------------------------------
setActiveFragmentLinkProgram(GLSLGpuProgram * fragmentGpuProgram)64 	void GLSLProgramPipelineManager::setActiveFragmentLinkProgram(GLSLGpuProgram* fragmentGpuProgram)
65 	{
66 		if (fragmentGpuProgram != mActiveFragmentGpuProgram)
67 		{
68 			mActiveFragmentGpuProgram = fragmentGpuProgram;
69 			// ActiveProgramPipeline is no longer valid
70 			mActiveProgramPipeline = NULL;
71 		}
72 	}
73 
74 	//-----------------------------------------------------------------------
setActiveVertexLinkProgram(GLSLGpuProgram * vertexGpuProgram)75 	void GLSLProgramPipelineManager::setActiveVertexLinkProgram(GLSLGpuProgram* vertexGpuProgram)
76 	{
77 		if (vertexGpuProgram != mActiveVertexGpuProgram)
78 		{
79 			mActiveVertexGpuProgram = vertexGpuProgram;
80 			// ActiveProgramPipeline is no longer valid
81 			mActiveProgramPipeline = NULL;
82 		}
83 	}
84 
setActiveGeometryLinkProgram(GLSLGpuProgram * geometryGpuProgram)85     void GLSLProgramPipelineManager::setActiveGeometryLinkProgram(GLSLGpuProgram* geometryGpuProgram)
86 	{
87 		if (geometryGpuProgram != mActiveGeometryGpuProgram)
88 		{
89 			mActiveGeometryGpuProgram = geometryGpuProgram;
90 			// ActiveProgramPipeline is no longer valid
91 			mActiveProgramPipeline = NULL;
92 		}
93 	}
94 
setActiveTessDomainLinkProgram(GLSLGpuProgram * domainGpuProgram)95     void GLSLProgramPipelineManager::setActiveTessDomainLinkProgram(GLSLGpuProgram* domainGpuProgram)
96 	{
97 		if (domainGpuProgram != mActiveDomainGpuProgram)
98 		{
99 			mActiveDomainGpuProgram = domainGpuProgram;
100 			// ActiveProgramPipeline is no longer valid
101 			mActiveProgramPipeline = NULL;
102 		}
103 	}
104 
setActiveTessHullLinkProgram(GLSLGpuProgram * hullGpuProgram)105     void GLSLProgramPipelineManager::setActiveTessHullLinkProgram(GLSLGpuProgram* hullGpuProgram)
106 	{
107 		if (hullGpuProgram != mActiveHullGpuProgram)
108 		{
109 			mActiveHullGpuProgram = hullGpuProgram;
110 			// ActiveProgramPipeline is no longer valid
111 			mActiveProgramPipeline = NULL;
112 		}
113 	}
114 
setActiveComputeLinkProgram(GLSLGpuProgram * computeGpuProgram)115     void GLSLProgramPipelineManager::setActiveComputeLinkProgram(GLSLGpuProgram* computeGpuProgram)
116 	{
117 		if (computeGpuProgram != mActiveComputeGpuProgram)
118 		{
119 			mActiveComputeGpuProgram = computeGpuProgram;
120 			// ActiveProgramPipeline is no longer valid
121 			mActiveProgramPipeline = NULL;
122 		}
123 	}
124 
125     //-----------------------------------------------------------------------
getActiveProgramPipeline(void)126 	GLSLProgramPipeline* GLSLProgramPipelineManager::getActiveProgramPipeline(void)
127 	{
128 		// If there is an active link program then return it
129 		if (mActiveProgramPipeline)
130 			return mActiveProgramPipeline;
131 
132 		// No active link program so find one or make a new one
133 		// Is there an active key?
134 		uint32 activeKey = 0;
135         GLuint progID = 0;
136 		if (mActiveVertexGpuProgram)
137 		{
138             progID = mActiveVertexGpuProgram->getProgramID();
139             activeKey = FastHash((const char *)(&progID), sizeof(GLuint), activeKey);
140 		}
141 		if (mActiveFragmentGpuProgram)
142 		{
143             progID = mActiveFragmentGpuProgram->getProgramID();
144             activeKey = FastHash((const char *)(&progID), sizeof(GLuint), activeKey);
145 		}
146 		if (mActiveGeometryGpuProgram)
147 		{
148             progID = mActiveGeometryGpuProgram->getProgramID();
149             activeKey = FastHash((const char *)(&progID), sizeof(GLuint), activeKey);
150 		}
151 		if (mActiveDomainGpuProgram)
152 		{
153             progID = mActiveDomainGpuProgram->getProgramID();
154             activeKey = FastHash((const char *)(&progID), sizeof(GLuint), activeKey);
155 		}
156 		if (mActiveHullGpuProgram)
157 		{
158             progID = mActiveHullGpuProgram->getProgramID();
159             activeKey = FastHash((const char *)(&progID), sizeof(GLuint), activeKey);
160 		}
161 		if (mActiveComputeGpuProgram)
162 		{
163             progID = mActiveComputeGpuProgram->getProgramID();
164             activeKey = FastHash((const char *)(&progID), sizeof(GLuint), activeKey);
165 		}
166 
167 		// Only return a program pipeline object if a vertex or fragment stage exist
168 		if (activeKey > 0)
169 		{
170 			// Find the key in the hash map
171 			ProgramPipelineIterator programFound = mProgramPipelines.find(activeKey);
172 			// Program object not found for key so need to create it
173 			if (programFound == mProgramPipelines.end())
174 			{
175 				mActiveProgramPipeline = new GLSLProgramPipeline(mActiveVertexGpuProgram, mActiveGeometryGpuProgram,
176                                                                  mActiveFragmentGpuProgram, mActiveHullGpuProgram,
177                                                                  mActiveDomainGpuProgram, mActiveComputeGpuProgram);
178 				mProgramPipelines[activeKey] = mActiveProgramPipeline;
179 			}
180 			else
181 			{
182 				// Found a link program in map container so make it active
183 				mActiveProgramPipeline = programFound->second;
184 			}
185 		}
186 		// Make the program object active
187 		if (mActiveProgramPipeline)
188             mActiveProgramPipeline->activate();
189 
190 		return mActiveProgramPipeline;
191 	}
192 
193 }
194