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 #include "OgreGpuProgram.h"
29 #include "OgreRoot.h"
30 #include "OgreRenderSystem.h"
31 #include "OgreRenderSystemCapabilities.h"
32 #include "OgreStringConverter.h"
33 #include "OgreGpuProgramManager.h"
34 #include "OgreHighLevelGpuProgramManager.h"
35 #include "OgreLogManager.h"
36 
37 #include "OgreGLSLProgram.h"
38 #include "OgreGLSLGpuProgram.h"
39 #include "OgreGLSLExtSupport.h"
40 #include "OgreGLSLLinkProgramManager.h"
41 #include "OgreGLSLPreprocessor.h"
42 
43 namespace Ogre {
44     namespace GLSL {
45     //---------------------------------------------------------------------------
~GLSLProgram()46     GLSLProgram::~GLSLProgram()
47     {
48         // Have to call this here rather than in Resource destructor
49         // since calling virtual methods in base destructors causes crash
50         if (isLoaded())
51         {
52             unload();
53         }
54         else
55         {
56             unloadHighLevel();
57         }
58     }
59     //---------------------------------------------------------------------------
compile(bool checkErrors)60     bool GLSLProgram::compile(bool checkErrors)
61     {
62         // only create a shader object if glsl is supported
63         if (isSupported())
64         {
65             // create shader object
66             GLenum shaderType = 0x0000;
67             switch (mType)
68             {
69             case GPT_VERTEX_PROGRAM:
70                 shaderType = GL_VERTEX_SHADER_ARB;
71                 break;
72             case GPT_FRAGMENT_PROGRAM:
73                 shaderType = GL_FRAGMENT_SHADER_ARB;
74                 break;
75             case GPT_GEOMETRY_PROGRAM:
76                 shaderType = GL_GEOMETRY_SHADER_EXT;
77                 break;
78             case GPT_COMPUTE_PROGRAM:
79             case GPT_DOMAIN_PROGRAM:
80             case GPT_HULL_PROGRAM:
81                 break;
82             }
83             mGLHandle = glCreateShaderObjectARB(shaderType);
84         }
85 
86         // Add preprocessor extras and main source
87         if (!mSource.empty())
88         {
89             const char *source = mSource.c_str();
90             glShaderSourceARB(mGLHandle, 1, &source, NULL);
91         }
92 
93         glCompileShaderARB(mGLHandle);
94         // check for compile errors
95         int compiled = 0;
96         glGetObjectParameterivARB(mGLHandle, GL_OBJECT_COMPILE_STATUS_ARB, &compiled);
97 
98         if(!checkErrors)
99             return compiled == 1;
100 
101         String compileInfo = getObjectInfo(mGLHandle);
102 
103         if (!compiled)
104             OGRE_EXCEPT(Exception::ERR_RENDERINGAPI_ERROR, getResourceLogName() + " " + compileInfo, "compile");
105 
106         // probably we have warnings
107         if (!compileInfo.empty())
108             LogManager::getSingleton().stream(LML_WARNING) << getResourceLogName() << " " << compileInfo;
109 
110         return (compiled == 1);
111     }
112 
113     //-----------------------------------------------------------------------
createLowLevelImpl(void)114     void GLSLProgram::createLowLevelImpl(void)
115     {
116         mAssemblerProgram = GpuProgramPtr(OGRE_NEW GLSLGpuProgram( this ));
117         // Shader params need to be forwarded to low level implementation
118         mAssemblerProgram->setAdjacencyInfoRequired(isAdjacencyInfoRequired());
119     }
120     //-----------------------------------------------------------------------
unloadHighLevelImpl(void)121     void GLSLProgram::unloadHighLevelImpl(void)
122     {
123         if (isSupported())
124         {
125             glDeleteObjectARB(mGLHandle);
126             mGLHandle = 0;
127 
128             // destroy all programs using this shader
129             GLSLLinkProgramManager::getSingletonPtr()->destroyAllByShader(this);
130         }
131     }
132 
133     //-----------------------------------------------------------------------
populateParameterNames(GpuProgramParametersSharedPtr params)134     void GLSLProgram::populateParameterNames(GpuProgramParametersSharedPtr params)
135     {
136         getConstantDefinitions();
137         params->_setNamedConstants(mConstantDefs);
138         // Don't set logical / physical maps here, as we can't access parameters by logical index in GLHL.
139     }
140     //-----------------------------------------------------------------------
buildConstantDefinitions() const141     void GLSLProgram::buildConstantDefinitions() const
142     {
143         // We need an accurate list of all the uniforms in the shader, but we
144         // can't get at them until we link all the shaders into a program object.
145 
146 
147         // Therefore instead, parse the source code manually and extract the uniforms
148         createParameterMappingStructures(true);
149         GLSLLinkProgramManager::getSingleton().extractUniformsFromGLSL(
150             mSource, *mConstantDefs, mName);
151 
152         // Also parse any attached sources
153         for (GLSLProgramContainer::const_iterator i = mAttachedGLSLPrograms.begin();
154             i != mAttachedGLSLPrograms.end(); ++i)
155         {
156             GLSLShaderCommon* childShader = *i;
157 
158             GLSLLinkProgramManager::getSingleton().extractUniformsFromGLSL(
159                 childShader->getSource(), *mConstantDefs, childShader->getName());
160 
161         }
162     }
163 
164     //-----------------------------------------------------------------------
GLSLProgram(ResourceManager * creator,const String & name,ResourceHandle handle,const String & group,bool isManual,ManualResourceLoader * loader)165     GLSLProgram::GLSLProgram(ResourceManager* creator,
166         const String& name, ResourceHandle handle,
167         const String& group, bool isManual, ManualResourceLoader* loader)
168         : GLSLShaderCommon(creator, name, handle, group, isManual, loader)
169         , mGLHandle(0)
170     {
171         // add parameter command "attach" to the material serializer dictionary
172         if (createParamDictionary("GLSLProgram"))
173         {
174             setupBaseParamDictionary();
175             ParamDictionary* dict = getParamDictionary();
176 
177             dict->addParameter(ParameterDef("preprocessor_defines",
178                 "Preprocessor defines use to compile the program.",
179                 PT_STRING),&msCmdPreprocessorDefines);
180             dict->addParameter(ParameterDef("attach",
181                 "name of another GLSL program needed by this program",
182                 PT_STRING),&msCmdAttach);
183             dict->addParameter(ParameterDef("column_major_matrices",
184                 "Whether matrix packing in column-major order.",
185                 PT_BOOL),&msCmdColumnMajorMatrices);
186             dict->addParameter(
187                 ParameterDef("input_operation_type",
188                 "The input operation type for this geometry program. \
189                 Can be 'point_list', 'line_list', 'line_strip', 'triangle_list', \
190                 'triangle_strip' or 'triangle_fan'", PT_STRING),
191                 &msInputOperationTypeCmd);
192             dict->addParameter(
193                 ParameterDef("output_operation_type",
194                 "The input operation type for this geometry program. \
195                 Can be 'point_list', 'line_strip' or 'triangle_strip'",
196                  PT_STRING),
197                  &msOutputOperationTypeCmd);
198             dict->addParameter(
199                 ParameterDef("max_output_vertices",
200                 "The maximum number of vertices a single run of this geometry program can output",
201                 PT_INT),&msMaxOutputVerticesCmd);
202         }
203         // Manually assign language now since we use it immediately
204         mSyntaxCode = "glsl";
205     }
206 
207     //-----------------------------------------------------------------------
attachToProgramObject(const GLhandleARB programObject)208     void GLSLProgram::attachToProgramObject( const GLhandleARB programObject )
209     {
210         // attach child objects
211         for (auto childShader : mAttachedGLSLPrograms)
212         {
213             childShader->attachToProgramObject(programObject);
214         }
215         glAttachObjectARB( programObject, mGLHandle );
216         GLenum glErr = glGetError();
217         if(glErr != GL_NO_ERROR)
218         {
219             reportGLSLError( glErr, "GLSLProgram::attachToProgramObject",
220                 "Error attaching " + mName + " shader object to GLSL Program Object", programObject );
221         }
222 
223     }
224     //-----------------------------------------------------------------------
detachFromProgramObject(const GLhandleARB programObject)225     void GLSLProgram::detachFromProgramObject( const GLhandleARB programObject )
226     {
227         glDetachObjectARB(programObject, mGLHandle);
228 
229         GLenum glErr = glGetError();
230         if(glErr != GL_NO_ERROR)
231         {
232             reportGLSLError( glErr, "GLSLProgram::detachFromProgramObject",
233                 "Error detaching " + mName + " shader object from GLSL Program Object", programObject );
234         }
235         // attach child objects
236         GLSLProgramContainerIterator childprogramcurrent = mAttachedGLSLPrograms.begin();
237         GLSLProgramContainerIterator childprogramend = mAttachedGLSLPrograms.end();
238 
239         while (childprogramcurrent != childprogramend)
240         {
241             GLSLShaderCommon* childShader = *childprogramcurrent;
242             childShader->detachFromProgramObject( programObject );
243             ++childprogramcurrent;
244         }
245 
246     }
247 
248     //-----------------------------------------------------------------------
getLanguage(void) const249     const String& GLSLProgram::getLanguage(void) const
250     {
251         static const String language = "glsl";
252 
253         return language;
254     }
255 
256 }
257 }
258