/src/ogre/OgreMain/include/OgreGpuProgramManager.h
Line | Count | Source |
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 | | #ifndef __GpuProgramManager_H_ |
29 | | #define __GpuProgramManager_H_ |
30 | | |
31 | | // Precompiler options |
32 | | #include "OgrePrerequisites.h" |
33 | | #include "OgreResourceManager.h" |
34 | | #include "OgreGpuProgram.h" |
35 | | #include "OgreSingleton.h" |
36 | | #include "OgreHeaderPrefix.h" |
37 | | |
38 | | namespace Ogre { |
39 | | |
40 | | /** \addtogroup Core |
41 | | * @{ |
42 | | */ |
43 | | /** \addtogroup Resources |
44 | | * @{ |
45 | | */ |
46 | | |
47 | | /** Interface definition for factories of GpuProgram. */ |
48 | | class _OgreExport GpuProgramFactory : public FactoryAlloc |
49 | | { |
50 | | public: |
51 | 2 | virtual ~GpuProgramFactory() {} |
52 | | /// Get the name of the language this factory creates programs for |
53 | | virtual const String& getLanguage(void) const = 0; |
54 | | virtual GpuProgram* create(ResourceManager* creator, const String& name, ResourceHandle handle, |
55 | | const String& group, bool isManual, ManualResourceLoader* loader) = 0; |
56 | 0 | virtual void destroy(GpuProgram* prog) { delete prog; } |
57 | | }; |
58 | | |
59 | | |
60 | | /** This ResourceManager manages GPU shader programs |
61 | | |
62 | | This class not only manages the programs themselves, it also manages the factory |
63 | | classes which allow the creation of programs using a variety of |
64 | | syntaxes. Plugins can be created which register themselves as program |
65 | | factories and as such the engine can be extended to accept virtually any kind of |
66 | | program provided a plugin is written. |
67 | | */ |
68 | | class _OgreExport GpuProgramManager : public ResourceManager, public Singleton<GpuProgramManager> |
69 | | { |
70 | | // silence warnings |
71 | | using ResourceManager::load; |
72 | | |
73 | | /// Factories capable of creating GpuProgram instances |
74 | | typedef std::map<String, GpuProgramFactory*> FactoryMap; |
75 | | FactoryMap mFactories; |
76 | | |
77 | | /// Factory for dealing with programs for languages we can't create |
78 | | std::unique_ptr<GpuProgramFactory> mNullFactory; |
79 | | /// Factory for unified high-level programs |
80 | | std::unique_ptr<GpuProgramFactory> mUnifiedFactory; |
81 | | |
82 | | GpuProgramFactory* getFactory(const String& language); |
83 | | |
84 | | public: |
85 | | |
86 | | typedef std::set<String> SyntaxCodes; |
87 | | typedef std::map<String, GpuSharedParametersPtr> SharedParametersMap; |
88 | | |
89 | | typedef MemoryDataStreamPtr Microcode; |
90 | | |
91 | | protected: |
92 | | |
93 | | SharedParametersMap mSharedParametersMap; |
94 | | std::map<uint32, Microcode> mMicrocodeCache; |
95 | | bool mSaveMicrocodesToCache; |
96 | | bool mCacheDirty; // When this is true the cache is 'dirty' and should be resaved to disk. |
97 | | |
98 | | static String addRenderSystemToName( const String & name ); |
99 | | |
100 | | /// Generic create method |
101 | | Resource* createImpl(const String& name, ResourceHandle handle, |
102 | | const String& group, bool isManual, ManualResourceLoader* loader, |
103 | | const NameValuePairList* createParams) override; |
104 | | public: |
105 | | GpuProgramManager(); |
106 | | virtual ~GpuProgramManager(); |
107 | | |
108 | | /// Get a GPU Program by name. For example, a GPU Program defined in some .program file. |
109 | | /// @see GpuProgramManager::getResourceByName |
110 | | GpuProgramPtr getByName(const String& name, const String& group OGRE_RESOURCE_GROUP_INIT) const; |
111 | | |
112 | | /// @deprecated preferHighLevelPrograms has no effect |
113 | | OGRE_DEPRECATED GpuProgramPtr getByName(const String& name, const String& group, |
114 | | bool preferHighLevelPrograms) const |
115 | 0 | { |
116 | 0 | return getByName(name, group); |
117 | 0 | } |
118 | | |
119 | | /** Loads a GPU program from a file |
120 | | |
121 | | This method creates a new program of the type specified as the second parameter. |
122 | | As with all types of ResourceManager, this class will search for the file in |
123 | | all resource locations it has been configured to look in. |
124 | | @param name The name of the GpuProgram |
125 | | @param groupName The name of the resource group |
126 | | @param filename The file to load |
127 | | @param gptype The type of program to create |
128 | | @param syntaxCode The name of the syntax to be used for this program e.g. arbvp1, vs_1_1 |
129 | | */ |
130 | | virtual GpuProgramPtr load(const String& name, const String& groupName, |
131 | | const String& filename, GpuProgramType gptype, |
132 | | const String& syntaxCode); |
133 | | |
134 | | /** Loads a GPU program from a string |
135 | | |
136 | | The assembly code must be compatible with this manager - call the |
137 | | getSupportedSyntax method for details of the supported syntaxes |
138 | | @param name The identifying name to give this program, which can be used to |
139 | | retrieve this program later with getByName. |
140 | | @param groupName The name of the resource group |
141 | | @param code A string of assembly code which will form the program to run |
142 | | @param gptype The type of program to create. |
143 | | @param syntaxCode The name of the syntax to be used for this program e.g. arbvp1, vs_1_1 |
144 | | */ |
145 | | virtual GpuProgramPtr loadFromString(const String& name, const String& groupName, |
146 | | const String& code, GpuProgramType gptype, |
147 | | const String& syntaxCode); |
148 | | |
149 | | /** Returns the syntaxes that the RenderSystem supports. */ |
150 | | static const SyntaxCodes& getSupportedSyntax(void); |
151 | | |
152 | | /** Returns whether a given syntax code (e.g. "glsl330", "vs_4_0", "arbvp1") is supported. */ |
153 | | static bool isSyntaxSupported(const String& syntaxCode); |
154 | | |
155 | | /** Returns whether a given high-level language (e.g. "glsl", "hlsl") is supported. */ |
156 | | bool isLanguageSupported(const String& lang) const; |
157 | | |
158 | | /** Creates a new GpuProgramParameters instance which can be used to bind |
159 | | parameters to your programs. |
160 | | |
161 | | Program parameters can be shared between multiple programs if you wish. |
162 | | */ |
163 | 0 | static GpuProgramParametersPtr createParameters(void) { return std::make_shared<GpuProgramParameters>(); } |
164 | | |
165 | | /** Create a new, unloaded GpuProgram from a file of assembly. |
166 | | |
167 | | Use this method in preference to the 'load' methods if you wish to define |
168 | | a GpuProgram, but not load it yet; useful for saving memory. |
169 | | @par |
170 | | This method creates a new program of the type specified as the second parameter. |
171 | | As with all types of ResourceManager, this class will search for the file in |
172 | | all resource locations it has been configured to look in. |
173 | | @param name The name of the program |
174 | | @param groupName The name of the resource group |
175 | | @param filename The file to load |
176 | | @param syntaxCode The name of the syntax to be used for this program e.g. arbvp1, vs_1_1 |
177 | | @param gptype The type of program to create |
178 | | */ |
179 | | virtual GpuProgramPtr createProgram(const String& name, |
180 | | const String& groupName, const String& filename, |
181 | | GpuProgramType gptype, const String& syntaxCode); |
182 | | |
183 | | /** Create a GPU program from a string of assembly code. |
184 | | |
185 | | Use this method in preference to the 'load' methods if you wish to define |
186 | | a GpuProgram, but not load it yet; useful for saving memory. |
187 | | @par |
188 | | The assembly code must be compatible with this manager - call the |
189 | | getSupportedSyntax method for details of the supported syntaxes |
190 | | @param name The identifying name to give this program, which can be used to |
191 | | retrieve this program later with getByName. |
192 | | @param groupName The name of the resource group |
193 | | @param code A string of assembly code which will form the program to run |
194 | | @param gptype The type of program to create. |
195 | | @param syntaxCode The name of the syntax to be used for this program e.g. arbvp1, vs_1_1 |
196 | | */ |
197 | | virtual GpuProgramPtr createProgramFromString(const String& name, |
198 | | const String& groupName, const String& code, |
199 | | GpuProgramType gptype, const String& syntaxCode); |
200 | | |
201 | | /** General create method, using specific create parameters |
202 | | instead of name / value pairs. |
203 | | */ |
204 | | GpuProgramPtr create(const String& name, const String& group, |
205 | | GpuProgramType gptype, const String& language, bool isManual = false, |
206 | | ManualResourceLoader* loader = 0); |
207 | | |
208 | | /** Create a new, unloaded GpuProgram. |
209 | | @par |
210 | | This method creates a new program of the type specified as the second and third parameters. |
211 | | You will have to call further methods on the returned program in order to |
212 | | define the program fully before you can load it. |
213 | | @param name The identifying name of the program |
214 | | @param groupName The name of the resource group which this program is |
215 | | to be a member of |
216 | | @param language Code of the language to use (e.g. "cg") |
217 | | @param gptype The type of program to create |
218 | | */ |
219 | | GpuProgramPtr createProgram(const String& name, const String& groupName, const String& language, |
220 | | GpuProgramType gptype) |
221 | 0 | { |
222 | 0 | return create(name, groupName, gptype, language); |
223 | 0 | } |
224 | | |
225 | | /** Create a new set of shared parameters, which can be used across many |
226 | | GpuProgramParameters objects of different structures. |
227 | | @param name The name to give the shared parameters so you can refer to them |
228 | | later. |
229 | | */ |
230 | | virtual GpuSharedParametersPtr createSharedParameters(const String& name); |
231 | | |
232 | | /** Retrieve a set of shared parameters, which can be used across many |
233 | | GpuProgramParameters objects of different structures. |
234 | | */ |
235 | | virtual GpuSharedParametersPtr getSharedParameters(const String& name) const; |
236 | | |
237 | | /** Get (const) access to the available shared parameter sets. |
238 | | */ |
239 | | virtual const SharedParametersMap& getAvailableSharedParameters() const; |
240 | | |
241 | | /** Get if the microcode of a shader should be saved to a cache |
242 | | */ |
243 | | bool getSaveMicrocodesToCache() const; |
244 | | /** Set if the microcode of a shader should be saved to a cache |
245 | | */ |
246 | | void setSaveMicrocodesToCache( bool val ); |
247 | | |
248 | | /** Returns true if the microcodecache changed during the run. |
249 | | */ |
250 | | bool isCacheDirty(void) const; |
251 | | |
252 | | static bool canGetCompiledShaderBuffer(); |
253 | | /** Check if a microcode is available for a program in the microcode cache. |
254 | | @param id The id of the program. |
255 | | */ |
256 | | bool isMicrocodeAvailableInCache(uint32 id) const; |
257 | | |
258 | | /** Returns a microcode for a program from the microcode cache. |
259 | | @param id The name of the program. |
260 | | */ |
261 | | const Microcode& getMicrocodeFromCache(uint32 id) const; |
262 | | |
263 | | /** Creates a microcode to be later added to the cache. |
264 | | @param size The size of the microcode in bytes |
265 | | */ |
266 | 0 | static Microcode createMicrocode(size_t size) { return std::make_shared<MemoryDataStream>(size); } |
267 | | |
268 | | /** Adds a microcode for a program to the microcode cache. |
269 | | @param id The id of the program |
270 | | @param microcode the program binary |
271 | | */ |
272 | | void addMicrocodeToCache(uint32 id, const Microcode& microcode); |
273 | | |
274 | | /** Removes a microcode for a program from the microcode cache. |
275 | | @param id The name of the program. |
276 | | */ |
277 | | void removeMicrocodeFromCache(uint32 id); |
278 | | |
279 | | /** Saves the microcode cache to disk. |
280 | | @param stream The destination stream |
281 | | */ |
282 | | void saveMicrocodeCache( const DataStreamPtr& stream ) const; |
283 | | /** Loads the microcode cache from disk. |
284 | | @param stream The source stream |
285 | | */ |
286 | | void loadMicrocodeCache( const DataStreamPtr& stream ); |
287 | | |
288 | | /** Add a new factory object for programs of a given language. */ |
289 | | void addFactory(GpuProgramFactory* factory); |
290 | | /** Remove a factory object for programs of a given language. */ |
291 | | void removeFactory(GpuProgramFactory* factory); |
292 | | |
293 | | /// @copydoc Singleton::getSingleton() |
294 | | static GpuProgramManager& getSingleton(void); |
295 | | /// @copydoc Singleton::getSingleton() |
296 | | static GpuProgramManager* getSingletonPtr(void); |
297 | | |
298 | | |
299 | | |
300 | | }; |
301 | | |
302 | | /** @} */ |
303 | | /** @} */ |
304 | | } |
305 | | |
306 | | #include "OgreHeaderSuffix.h" |
307 | | |
308 | | #endif |