Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: downloads/OgreMain/src/OgreHighLevelGpuProgramManager.cpp @ 3

Last change on this file since 3 was 3, checked in by anonymous, 17 years ago

=update

File size: 7.4 KB
Line 
1/*
2-----------------------------------------------------------------------------
3This source file is part of OGRE
4    (Object-oriented Graphics Rendering Engine)
5For the latest info, see http://www.ogre3d.org/
6
7Copyright (c) 2000-2006 Torus Knot Software Ltd
8Also see acknowledgements in Readme.html
9
10This program is free software; you can redistribute it and/or modify it under
11the terms of the GNU Lesser General Public License as published by the Free Software
12Foundation; either version 2 of the License, or (at your option) any later
13version.
14
15This program is distributed in the hope that it will be useful, but WITHOUT
16ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
17FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
18
19You should have received a copy of the GNU Lesser General Public License along with
20this program; if not, write to the Free Software Foundation, Inc., 59 Temple
21Place - Suite 330, Boston, MA 02111-1307, USA, or go to
22http://www.gnu.org/copyleft/lesser.txt.
23
24You may alternatively use this source under the terms of a specific version of
25the OGRE Unrestricted License provided you have obtained such a license from
26Torus Knot Software Ltd.
27-----------------------------------------------------------------------------
28*/
29#include "OgreStableHeaders.h"
30#include "OgreHighLevelGpuProgramManager.h"
31#include "OgreUnifiedHighLevelGpuProgram.h"
32
33namespace Ogre {
34
35        String sNullLang = "null";
36        class NullProgram : public HighLevelGpuProgram
37        {
38        protected:
39                /** Internal load implementation, must be implemented by subclasses.
40                */
41                void loadFromSource(void) {}
42                /** Internal method for creating an appropriate low-level program from this
43                high-level program, must be implemented by subclasses. */
44                void createLowLevelImpl(void) {}
45                /// Internal unload implementation, must be implemented by subclasses
46                void unloadHighLevelImpl(void) {}
47                /// Populate the passed parameters with name->index map, must be overridden
48                void populateParameterNames(GpuProgramParametersSharedPtr params)
49                {
50                        // Skip the normal implementation
51                        // Ensure we don't complain about missing parameter names
52                        params->setIgnoreMissingParams(true);
53
54                }
55                void buildConstantDefinitions() const
56                {
57                        // do nothing
58                }
59        public:
60                NullProgram(ResourceManager* creator, 
61                        const String& name, ResourceHandle handle, const String& group, 
62                        bool isManual, ManualResourceLoader* loader)
63                        : HighLevelGpuProgram(creator, name, handle, group, isManual, loader){}
64                ~NullProgram() {}
65                /// Overridden from GpuProgram - never supported
66                bool isSupported(void) const { return false; }
67                /// Overridden from GpuProgram
68                const String& getLanguage(void) const { return sNullLang; }
69
70                /// Overridden from StringInterface
71                bool setParameter(const String& name, const String& value)
72                {
73                        // always silently ignore all parameters so as not to report errors on
74                        // unsupported platforms
75                        return true;
76                }
77
78        };
79        class NullProgramFactory : public HighLevelGpuProgramFactory
80        {
81        public:
82                NullProgramFactory() {}
83                ~NullProgramFactory() {}
84                /// Get the name of the language this factory creates programs for
85                const String& getLanguage(void) const 
86                { 
87                        return sNullLang;
88                }
89                HighLevelGpuProgram* create(ResourceManager* creator, 
90                        const String& name, ResourceHandle handle,
91                        const String& group, bool isManual, ManualResourceLoader* loader)
92                {
93                        return new NullProgram(creator, name, handle, group, isManual, loader);
94                }
95                void destroy(HighLevelGpuProgram* prog)
96                {
97                        delete prog;
98                }
99
100        };
101        //-----------------------------------------------------------------------
102        template<> HighLevelGpuProgramManager* 
103        Singleton<HighLevelGpuProgramManager>::ms_Singleton = 0;
104    HighLevelGpuProgramManager* HighLevelGpuProgramManager::getSingletonPtr(void)
105    {
106        return ms_Singleton;
107    }
108    HighLevelGpuProgramManager& HighLevelGpuProgramManager::getSingleton(void)
109    { 
110        assert( ms_Singleton );  return ( *ms_Singleton ); 
111    }
112        //-----------------------------------------------------------------------
113        HighLevelGpuProgramManager::HighLevelGpuProgramManager()
114        {
115        // Loading order
116        mLoadOrder = 50.0f;
117        // Resource type
118        mResourceType = "HighLevelGpuProgram";
119
120        ResourceGroupManager::getSingleton()._registerResourceManager(mResourceType, this);   
121
122                mNullFactory = new NullProgramFactory();
123                addFactory(mNullFactory);
124                mUnifiedFactory = new UnifiedHighLevelGpuProgramFactory();
125                addFactory(mUnifiedFactory);
126        }
127        //-----------------------------------------------------------------------
128        HighLevelGpuProgramManager::~HighLevelGpuProgramManager()
129        {
130                delete mUnifiedFactory;
131                delete mNullFactory;
132        ResourceGroupManager::getSingleton()._unregisterResourceManager(mResourceType);   
133        }
134    //---------------------------------------------------------------------------
135        void HighLevelGpuProgramManager::addFactory(HighLevelGpuProgramFactory* factory)
136        {
137                // deliberately allow later plugins to override earlier ones
138                mFactories[factory->getLanguage()] = factory;
139        }
140    //---------------------------------------------------------------------------
141    void HighLevelGpuProgramManager::removeFactory(HighLevelGpuProgramFactory* factory)
142    {
143        // Remove only if equal to registered one, since it might overridden
144        // by other plugins
145        FactoryMap::iterator it = mFactories.find(factory->getLanguage());
146        if (it != mFactories.end() && it->second == factory)
147        {
148            mFactories.erase(it);
149        }
150    }
151    //---------------------------------------------------------------------------
152        HighLevelGpuProgramFactory* HighLevelGpuProgramManager::getFactory(const String& language)
153        {
154                FactoryMap::iterator i = mFactories.find(language);
155
156                if (i == mFactories.end())
157                {
158                        // use the null factory to create programs that will never be supported
159                        i = mFactories.find(sNullLang);
160                }
161                return i->second;
162        }
163    //---------------------------------------------------------------------------
164    Resource* HighLevelGpuProgramManager::createImpl(const String& name, ResourceHandle handle, 
165        const String& group, bool isManual, ManualResourceLoader* loader,
166        const NameValuePairList* params)
167    {
168        NameValuePairList::const_iterator paramIt;
169
170        if (!params || (paramIt = params->find("language")) == params->end())
171        {
172            OGRE_EXCEPT(Exception::ERR_INVALIDPARAMS, 
173                "You must supply a 'language' parameter",
174                "HighLevelGpuProgramManager::createImpl");
175        }
176
177        return getFactory(paramIt->second)->create(this, name, getNextHandle(), 
178            group, isManual, loader);
179    }
180    //---------------------------------------------------------------------------
181    HighLevelGpuProgramPtr HighLevelGpuProgramManager::createProgram(
182                        const String& name, const String& groupName, 
183            const String& language, GpuProgramType gptype)
184    {
185        ResourcePtr ret = ResourcePtr(
186            getFactory(language)->create(this, name, getNextHandle(), 
187            groupName, false, 0));
188
189        HighLevelGpuProgramPtr prg = ret;
190        prg->setType(gptype);
191        prg->setSyntaxCode(language);
192
193        addImpl(ret);
194        // Tell resource group manager
195        ResourceGroupManager::getSingleton()._notifyResourceCreated(ret);
196        return prg;
197    }
198    //---------------------------------------------------------------------------
199    HighLevelGpuProgramFactory::~HighLevelGpuProgramFactory() 
200    {
201    }
202}
Note: See TracBrowser for help on using the repository browser.