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 | #ifndef __GpuProgram_H_ |
---|
29 | #define __GpuProgram_H_ |
---|
30 | |
---|
31 | // Precompiler options |
---|
32 | #include "OgrePrerequisites.h" |
---|
33 | #include "OgreResource.h" |
---|
34 | #include "OgreSharedPtr.h" |
---|
35 | #include "OgreIteratorWrappers.h" |
---|
36 | #include "OgreSerializer.h" |
---|
37 | #include "OgreRenderOperation.h" |
---|
38 | #include "OgreGpuProgramParams.h" |
---|
39 | #include "OgreHeaderPrefix.h" |
---|
40 | |
---|
41 | namespace Ogre { |
---|
42 | |
---|
43 | /** \addtogroup Core |
---|
44 | * @{ |
---|
45 | */ |
---|
46 | /** \addtogroup Resources |
---|
47 | * @{ |
---|
48 | */ |
---|
49 | /** Enumerates the types of programs which can run on the GPU. */ |
---|
50 | enum GpuProgramType |
---|
51 | { |
---|
52 | GPT_VERTEX_PROGRAM, |
---|
53 | GPT_FRAGMENT_PROGRAM, |
---|
54 | GPT_GEOMETRY_PROGRAM, |
---|
55 | GPT_DOMAIN_PROGRAM, |
---|
56 | GPT_HULL_PROGRAM, |
---|
57 | GPT_COMPUTE_PROGRAM |
---|
58 | }; |
---|
59 | |
---|
60 | /** Defines a program which runs on the GPU such as a vertex or fragment program. |
---|
61 | @remarks |
---|
62 | This class defines the low-level program in assembler code, the sort used to |
---|
63 | directly assemble into machine instructions for the GPU to execute. By nature, |
---|
64 | this means that the assembler source is rendersystem specific, which is why this |
---|
65 | is an abstract class - real instances are created through the RenderSystem. |
---|
66 | If you wish to use higher level shading languages like HLSL and Cg, you need to |
---|
67 | use the HighLevelGpuProgram class instead. |
---|
68 | */ |
---|
69 | class _OgreExport GpuProgram : public Resource |
---|
70 | { |
---|
71 | protected: |
---|
72 | /// Command object - see ParamCommand |
---|
73 | class _OgreExport CmdType : public ParamCommand |
---|
74 | { |
---|
75 | public: |
---|
76 | String doGet(const void* target) const; |
---|
77 | void doSet(void* target, const String& val); |
---|
78 | }; |
---|
79 | class _OgreExport CmdSyntax : public ParamCommand |
---|
80 | { |
---|
81 | public: |
---|
82 | String doGet(const void* target) const; |
---|
83 | void doSet(void* target, const String& val); |
---|
84 | }; |
---|
85 | class _OgreExport CmdSkeletal : public ParamCommand |
---|
86 | { |
---|
87 | public: |
---|
88 | String doGet(const void* target) const; |
---|
89 | void doSet(void* target, const String& val); |
---|
90 | }; |
---|
91 | class _OgreExport CmdMorph : public ParamCommand |
---|
92 | { |
---|
93 | public: |
---|
94 | String doGet(const void* target) const; |
---|
95 | void doSet(void* target, const String& val); |
---|
96 | }; |
---|
97 | class _OgreExport CmdPose : public ParamCommand |
---|
98 | { |
---|
99 | public: |
---|
100 | String doGet(const void* target) const; |
---|
101 | void doSet(void* target, const String& val); |
---|
102 | }; |
---|
103 | class _OgreExport CmdVTF : public ParamCommand |
---|
104 | { |
---|
105 | public: |
---|
106 | String doGet(const void* target) const; |
---|
107 | void doSet(void* target, const String& val); |
---|
108 | }; |
---|
109 | class _OgreExport CmdManualNamedConstsFile : public ParamCommand |
---|
110 | { |
---|
111 | public: |
---|
112 | String doGet(const void* target) const; |
---|
113 | void doSet(void* target, const String& val); |
---|
114 | }; |
---|
115 | class _OgreExport CmdAdjacency : public ParamCommand |
---|
116 | { |
---|
117 | public: |
---|
118 | String doGet(const void* target) const; |
---|
119 | void doSet(void* target, const String& val); |
---|
120 | }; |
---|
121 | // Command object for setting / getting parameters |
---|
122 | static CmdType msTypeCmd; |
---|
123 | static CmdSyntax msSyntaxCmd; |
---|
124 | static CmdSkeletal msSkeletalCmd; |
---|
125 | static CmdMorph msMorphCmd; |
---|
126 | static CmdPose msPoseCmd; |
---|
127 | static CmdVTF msVTFCmd; |
---|
128 | static CmdManualNamedConstsFile msManNamedConstsFileCmd; |
---|
129 | static CmdAdjacency msAdjacencyCmd; |
---|
130 | /// The type of the program |
---|
131 | GpuProgramType mType; |
---|
132 | /// The name of the file to load source from (may be blank) |
---|
133 | String mFilename; |
---|
134 | /// The assembler source of the program (may be blank until file loaded) |
---|
135 | String mSource; |
---|
136 | /// Whether we need to load source from file or not |
---|
137 | bool mLoadFromFile; |
---|
138 | /// Syntax code e.g. arbvp1, vs_2_0 etc |
---|
139 | String mSyntaxCode; |
---|
140 | /// Does this (vertex) program include skeletal animation? |
---|
141 | bool mSkeletalAnimation; |
---|
142 | /// Does this (vertex) program include morph animation? |
---|
143 | bool mMorphAnimation; |
---|
144 | /// Does this (vertex) program include pose animation (count of number of poses supported) |
---|
145 | ushort mPoseAnimation; |
---|
146 | /// Does this (vertex) program require support for vertex texture fetch? |
---|
147 | bool mVertexTextureFetch; |
---|
148 | /// Does this (geometry) program require adjacency information? |
---|
149 | bool mNeedsAdjacencyInfo; |
---|
150 | /// The default parameters for use with this object |
---|
151 | GpuProgramParametersSharedPtr mDefaultParams; |
---|
152 | /// Did we encounter a compilation error? |
---|
153 | bool mCompileError; |
---|
154 | /** Record of logical to physical buffer maps. Mandatory for low-level |
---|
155 | programs or high-level programs which set their params the same way. |
---|
156 | This is a shared pointer because if the program is recompiled and the parameters |
---|
157 | change, this definition will alter, but previous params may reference the old def. */ |
---|
158 | mutable GpuLogicalBufferStructPtr mFloatLogicalToPhysical; |
---|
159 | /** Record of logical to physical buffer maps. Mandatory for low-level |
---|
160 | programs or high-level programs which set their params the same way. |
---|
161 | This is a shared pointer because if the program is recompiled and the parameters |
---|
162 | change, this definition will alter, but previous params may reference the old def. */ |
---|
163 | mutable GpuLogicalBufferStructPtr mDoubleLogicalToPhysical; |
---|
164 | /** Record of logical to physical buffer maps. Mandatory for low-level |
---|
165 | programs or high-level programs which set their params the same way. |
---|
166 | This is a shared pointer because if the program is recompiled and the parameters |
---|
167 | change, this definition will alter, but previous params may reference the old def.*/ |
---|
168 | mutable GpuLogicalBufferStructPtr mIntLogicalToPhysical; |
---|
169 | /** Parameter name -> ConstantDefinition map, shared instance used by all parameter objects. |
---|
170 | This is a shared pointer because if the program is recompiled and the parameters |
---|
171 | change, this definition will alter, but previous params may reference the old def. |
---|
172 | */ |
---|
173 | mutable GpuNamedConstantsPtr mConstantDefs; |
---|
174 | /// File from which to load named constants manually |
---|
175 | String mManualNamedConstantsFile; |
---|
176 | bool mLoadedManualNamedConstants; |
---|
177 | |
---|
178 | |
---|
179 | /** Internal method for setting up the basic parameter definitions for a subclass. |
---|
180 | @remarks |
---|
181 | Because StringInterface holds a dictionary of parameters per class, subclasses need to |
---|
182 | call this to ask the base class to add it's parameters to their dictionary as well. |
---|
183 | Can't do this in the constructor because that runs in a non-virtual context. |
---|
184 | @par |
---|
185 | The subclass must have called it's own createParamDictionary before calling this method. |
---|
186 | */ |
---|
187 | void setupBaseParamDictionary(void); |
---|
188 | |
---|
189 | /** Internal method returns whether required capabilities for this program is supported. |
---|
190 | */ |
---|
191 | bool isRequiredCapabilitiesSupported(void) const; |
---|
192 | |
---|
193 | /// @copydoc Resource::loadImpl |
---|
194 | void loadImpl(void); |
---|
195 | |
---|
196 | /// Create the internal params logical & named mapping structures |
---|
197 | void createParameterMappingStructures(bool recreateIfExists = true) const; |
---|
198 | /// Create the internal params logical mapping structures |
---|
199 | void createLogicalParameterMappingStructures(bool recreateIfExists = true) const; |
---|
200 | /// Create the internal params named mapping structures |
---|
201 | void createNamedParameterMappingStructures(bool recreateIfExists = true) const; |
---|
202 | |
---|
203 | public: |
---|
204 | |
---|
205 | GpuProgram(ResourceManager* creator, const String& name, ResourceHandle handle, |
---|
206 | const String& group, bool isManual = false, ManualResourceLoader* loader = 0); |
---|
207 | |
---|
208 | virtual ~GpuProgram() {} |
---|
209 | |
---|
210 | /** Sets the filename of the source assembly for this program. |
---|
211 | @remarks |
---|
212 | Setting this will have no effect until you (re)load the program. |
---|
213 | */ |
---|
214 | virtual void setSourceFile(const String& filename); |
---|
215 | |
---|
216 | /** Sets the source assembly for this program from an in-memory string. |
---|
217 | @remarks |
---|
218 | Setting this will have no effect until you (re)load the program. |
---|
219 | */ |
---|
220 | virtual void setSource(const String& source); |
---|
221 | |
---|
222 | /** Gets the syntax code for this program e.g. arbvp1, fp20, vs_1_1 etc */ |
---|
223 | virtual const String& getSyntaxCode(void) const { return mSyntaxCode; } |
---|
224 | |
---|
225 | /** Sets the syntax code for this program e.g. arbvp1, fp20, vs_1_1 etc */ |
---|
226 | virtual void setSyntaxCode(const String& syntax); |
---|
227 | |
---|
228 | /** Gets the name of the file used as source for this program. */ |
---|
229 | virtual const String& getSourceFile(void) const { return mFilename; } |
---|
230 | /** Gets the assembler source for this program. */ |
---|
231 | virtual const String& getSource(void) const { return mSource; } |
---|
232 | /// Set the program type (only valid before load) |
---|
233 | virtual void setType(GpuProgramType t); |
---|
234 | /// Get the program type |
---|
235 | virtual GpuProgramType getType(void) const { return mType; } |
---|
236 | |
---|
237 | /** Returns the GpuProgram which should be bound to the pipeline. |
---|
238 | @remarks |
---|
239 | This method is simply to allow some subclasses of GpuProgram to delegate |
---|
240 | the program which is bound to the pipeline to a delegate, if required. */ |
---|
241 | virtual GpuProgram* _getBindingDelegate(void) { return this; } |
---|
242 | |
---|
243 | /** Returns whether this program can be supported on the current renderer and hardware. */ |
---|
244 | virtual bool isSupported(void) const; |
---|
245 | |
---|
246 | /** Creates a new parameters object compatible with this program definition. |
---|
247 | @remarks |
---|
248 | It is recommended that you use this method of creating parameters objects |
---|
249 | rather than going direct to GpuProgramManager, because this method will |
---|
250 | populate any implementation-specific extras (like named parameters) where |
---|
251 | they are appropriate. |
---|
252 | */ |
---|
253 | virtual GpuProgramParametersSharedPtr createParameters(void); |
---|
254 | |
---|
255 | /** Sets whether a vertex program includes the required instructions |
---|
256 | to perform skeletal animation. |
---|
257 | @remarks |
---|
258 | If this is set to true, OGRE will not blend the geometry according to |
---|
259 | skeletal animation, it will expect the vertex program to do it. |
---|
260 | */ |
---|
261 | virtual void setSkeletalAnimationIncluded(bool included) |
---|
262 | { mSkeletalAnimation = included; } |
---|
263 | |
---|
264 | /** Returns whether a vertex program includes the required instructions |
---|
265 | to perform skeletal animation. |
---|
266 | @remarks |
---|
267 | If this returns true, OGRE will not blend the geometry according to |
---|
268 | skeletal animation, it will expect the vertex program to do it. |
---|
269 | */ |
---|
270 | virtual bool isSkeletalAnimationIncluded(void) const { return mSkeletalAnimation; } |
---|
271 | |
---|
272 | /** Sets whether a vertex program includes the required instructions |
---|
273 | to perform morph animation. |
---|
274 | @remarks |
---|
275 | If this is set to true, OGRE will not blend the geometry according to |
---|
276 | morph animation, it will expect the vertex program to do it. |
---|
277 | */ |
---|
278 | virtual void setMorphAnimationIncluded(bool included) |
---|
279 | { mMorphAnimation = included; } |
---|
280 | |
---|
281 | /** Sets whether a vertex program includes the required instructions |
---|
282 | to perform pose animation. |
---|
283 | @remarks |
---|
284 | If this is set to true, OGRE will not blend the geometry according to |
---|
285 | pose animation, it will expect the vertex program to do it. |
---|
286 | @param poseCount The number of simultaneous poses the program can blend |
---|
287 | */ |
---|
288 | virtual void setPoseAnimationIncluded(ushort poseCount) |
---|
289 | { mPoseAnimation = poseCount; } |
---|
290 | |
---|
291 | /** Returns whether a vertex program includes the required instructions |
---|
292 | to perform morph animation. |
---|
293 | @remarks |
---|
294 | If this returns true, OGRE will not blend the geometry according to |
---|
295 | morph animation, it will expect the vertex program to do it. |
---|
296 | */ |
---|
297 | virtual bool isMorphAnimationIncluded(void) const { return mMorphAnimation; } |
---|
298 | |
---|
299 | /** Returns whether a vertex program includes the required instructions |
---|
300 | to perform pose animation. |
---|
301 | @remarks |
---|
302 | If this returns true, OGRE will not blend the geometry according to |
---|
303 | pose animation, it will expect the vertex program to do it. |
---|
304 | */ |
---|
305 | virtual bool isPoseAnimationIncluded(void) const { return mPoseAnimation > 0; } |
---|
306 | /** Returns the number of simultaneous poses the vertex program can |
---|
307 | blend, for use in pose animation. |
---|
308 | */ |
---|
309 | virtual ushort getNumberOfPosesIncluded(void) const { return mPoseAnimation; } |
---|
310 | /** Sets whether this vertex program requires support for vertex |
---|
311 | texture fetch from the hardware. |
---|
312 | */ |
---|
313 | virtual void setVertexTextureFetchRequired(bool r) { mVertexTextureFetch = r; } |
---|
314 | /** Returns whether this vertex program requires support for vertex |
---|
315 | texture fetch from the hardware. |
---|
316 | */ |
---|
317 | virtual bool isVertexTextureFetchRequired(void) const { return mVertexTextureFetch; } |
---|
318 | |
---|
319 | /** Sets whether this geometry program requires adjacency information |
---|
320 | from the input primitives. |
---|
321 | */ |
---|
322 | virtual void setAdjacencyInfoRequired(bool r) { mNeedsAdjacencyInfo = r; } |
---|
323 | /** Returns whether this geometry program requires adjacency information |
---|
324 | from the input primitives. |
---|
325 | */ |
---|
326 | virtual bool isAdjacencyInfoRequired(void) const { return mNeedsAdjacencyInfo; } |
---|
327 | |
---|
328 | /** Get a reference to the default parameters which are to be used for all |
---|
329 | uses of this program. |
---|
330 | @remarks |
---|
331 | A program can be set up with a list of default parameters, which can save time when |
---|
332 | using a program many times in a material with roughly the same settings. By |
---|
333 | retrieving the default parameters and populating it with the most used options, |
---|
334 | any new parameter objects created from this program afterwards will automatically include |
---|
335 | the default parameters; thus users of the program need only change the parameters |
---|
336 | which are unique to their own usage of the program. |
---|
337 | */ |
---|
338 | virtual GpuProgramParametersSharedPtr getDefaultParameters(void); |
---|
339 | |
---|
340 | /** Returns true if default parameters have been set up. |
---|
341 | */ |
---|
342 | virtual bool hasDefaultParameters(void) const { return !mDefaultParams.isNull(); } |
---|
343 | |
---|
344 | /** Returns whether a vertex program wants light and material states to be passed |
---|
345 | through fixed pipeline low level API rendering calls (default false, subclasses can override) |
---|
346 | @remarks |
---|
347 | Most vertex programs do not need this material information, however GLSL |
---|
348 | shaders can refer to this material and lighting state so enable this option |
---|
349 | */ |
---|
350 | virtual bool getPassSurfaceAndLightStates(void) const { return false; } |
---|
351 | |
---|
352 | /** Returns whether a fragment program wants fog state to be passed |
---|
353 | through fixed pipeline low level API rendering calls (default true, subclasses can override) |
---|
354 | @remarks |
---|
355 | On DirectX, shader model 2 and earlier continues to have fixed-function fog |
---|
356 | applied to it, so fog state is still passed (you should disable fog on the |
---|
357 | pass if you want to perform fog in the shader). In OpenGL it is also |
---|
358 | common to be able to access the fixed-function fog state inside the shader. |
---|
359 | */ |
---|
360 | virtual bool getPassFogStates(void) const { return true; } |
---|
361 | |
---|
362 | /** Returns whether a vertex program wants transform state to be passed |
---|
363 | through fixed pipeline low level API rendering calls |
---|
364 | @remarks |
---|
365 | Most vertex programs do not need fixed-function transform information, however GLSL |
---|
366 | shaders can refer to this state so enable this option |
---|
367 | */ |
---|
368 | virtual bool getPassTransformStates(void) const { return false; } |
---|
369 | |
---|
370 | /** Returns a string that specifies the language of the gpu programs as specified |
---|
371 | in a material script. ie: asm, cg, hlsl, glsl |
---|
372 | */ |
---|
373 | virtual const String& getLanguage(void) const; |
---|
374 | |
---|
375 | /** Did this program encounter a compile error when loading? |
---|
376 | */ |
---|
377 | virtual bool hasCompileError(void) const { return mCompileError; } |
---|
378 | |
---|
379 | /** Reset a compile error if it occurred, allowing the load to be retried |
---|
380 | */ |
---|
381 | virtual void resetCompileError(void) { mCompileError = false; } |
---|
382 | |
---|
383 | /** Allows you to manually provide a set of named parameter mappings |
---|
384 | to a program which would not be able to derive named parameters itself. |
---|
385 | @remarks |
---|
386 | You may wish to use this if you have assembler programs that were compiled |
---|
387 | from a high-level source, and want the convenience of still being able |
---|
388 | to use the named parameters from the original high-level source. |
---|
389 | @see setManualNamedConstantsFile |
---|
390 | */ |
---|
391 | virtual void setManualNamedConstants(const GpuNamedConstants& namedConstants); |
---|
392 | |
---|
393 | /// Get a read-only reference to the named constants registered for this program (manually or automatically) |
---|
394 | virtual const GpuNamedConstants& getNamedConstants() const { return *mConstantDefs.get(); } |
---|
395 | |
---|
396 | /** Specifies the name of a file from which to load named parameters mapping |
---|
397 | for a program which would not be able to derive named parameters itself. |
---|
398 | @remarks |
---|
399 | You may wish to use this if you have assembler programs that were compiled |
---|
400 | from a high-level source, and want the convenience of still being able |
---|
401 | to use the named parameters from the original high-level source. This |
---|
402 | method will make a low-level program search in the resource group of the |
---|
403 | program for the named file from which to load parameter names from. |
---|
404 | The file must be in the format produced by GpuNamedConstants::save. |
---|
405 | */ |
---|
406 | virtual void setManualNamedConstantsFile(const String& paramDefFile); |
---|
407 | |
---|
408 | /** Gets the name of a file from which to load named parameters mapping |
---|
409 | for a program which would not be able to derive named parameters itself. |
---|
410 | */ |
---|
411 | virtual const String& getManualNamedConstantsFile() const { return mManualNamedConstantsFile; } |
---|
412 | /** Get the full list of named constants. |
---|
413 | @note |
---|
414 | Only available if this parameters object has named parameters, which means either |
---|
415 | a high-level program which loads them, or a low-level program which has them |
---|
416 | specified manually. |
---|
417 | */ |
---|
418 | virtual const GpuNamedConstants& getConstantDefinitions() const { return *mConstantDefs.get(); } |
---|
419 | |
---|
420 | /// @copydoc Resource::calculateSize |
---|
421 | virtual size_t calculateSize(void) const; |
---|
422 | |
---|
423 | protected: |
---|
424 | /// Virtual method which must be implemented by subclasses, load from mSource |
---|
425 | virtual void loadFromSource(void) = 0; |
---|
426 | |
---|
427 | }; |
---|
428 | /** @} */ |
---|
429 | /** @} */ |
---|
430 | } |
---|
431 | |
---|
432 | #include "OgreHeaderSuffix.h" |
---|
433 | |
---|
434 | #endif |
---|