OgreGpuProgram.h 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388
  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. Copyright (c) 2000-2011 Torus Knot Software Ltd
  7. Permission is hereby granted, free of charge, to any person obtaining a copy
  8. of this software and associated documentation files (the "Software"), to deal
  9. in the Software without restriction, including without limitation the rights
  10. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  11. copies of the Software, and to permit persons to whom the Software is
  12. furnished to do so, subject to the following conditions:
  13. The above copyright notice and this permission notice shall be included in
  14. all copies or substantial portions of the Software.
  15. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  18. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  20. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  21. THE SOFTWARE.
  22. -----------------------------------------------------------------------------
  23. */
  24. #ifndef __GpuProgram_H_
  25. #define __GpuProgram_H_
  26. // Precompiler options
  27. #include "OgrePrerequisites.h"
  28. #include "OgreRenderOperation.h"
  29. #include "OgreGpuProgramParams.h"
  30. namespace Ogre {
  31. /** \addtogroup Core
  32. * @{
  33. */
  34. /** \addtogroup Resources
  35. * @{
  36. */
  37. /** Enumerates the types of programs which can run on the GPU. */
  38. enum GpuProgramType
  39. {
  40. GPT_VERTEX_PROGRAM,
  41. GPT_FRAGMENT_PROGRAM,
  42. GPT_GEOMETRY_PROGRAM
  43. };
  44. enum GpuProgramProfile
  45. {
  46. GPP_NONE,
  47. GPP_PS_1_1,
  48. GPP_PS_1_2,
  49. GPP_PS_1_3,
  50. GPP_PS_1_4,
  51. GPP_PS_2_0,
  52. GPP_PS_2_a,
  53. GPP_PS_2_b,
  54. GPP_PS_3_0,
  55. GPP_VS_1_1,
  56. GPP_VS_2_0,
  57. GPP_VS_2_a,
  58. GPP_VS_3_0
  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
  70. {
  71. protected:
  72. /// The type of the program
  73. GpuProgramType mType;
  74. /// Name of the shader entry method
  75. String mEntryPoint;
  76. /// Shader profiler that we are targeting (e.g. vs_1_1, etc.). Make sure profile matches the type.
  77. GpuProgramProfile mProfile;
  78. /// The name of the file to load source from (may be blank)
  79. String mFilename;
  80. /// The assembler source of the program (may be blank until file loaded)
  81. String mSource;
  82. /// Whether we need to load source from file or not
  83. bool mLoadFromFile;
  84. /// Syntax code e.g. arbvp1, vs_2_0 etc
  85. String mSyntaxCode;
  86. /// Does this (vertex) program include skeletal animation?
  87. bool mSkeletalAnimation;
  88. /// Does this (vertex) program include morph animation?
  89. bool mMorphAnimation;
  90. /// Does this (vertex) program include pose animation (count of number of poses supported)
  91. ushort mPoseAnimation;
  92. /// Does this (vertex) program require support for vertex texture fetch?
  93. bool mVertexTextureFetch;
  94. /// Does this (geometry) program require adjacency information?
  95. bool mNeedsAdjacencyInfo;
  96. /// The default parameters for use with this object
  97. GpuProgramParametersSharedPtr mDefaultParams;
  98. /// Did we encounter a compilation error?
  99. bool mCompileError;
  100. /** Record of logical to physical buffer maps. Mandatory for low-level
  101. programs or high-level programs which set their params the same way.
  102. This is a shared pointer because if the program is recompiled and the parameters
  103. change, this definition will alter, but previous params may reference the old def. */
  104. mutable GpuLogicalBufferStructPtr mFloatLogicalToPhysical;
  105. /** Record of logical to physical buffer maps. Mandatory for low-level
  106. programs or high-level programs which set their params the same way.
  107. This is a shared pointer because if the program is recompiled and the parameters
  108. change, this definition will alter, but previous params may reference the old def.*/
  109. mutable GpuLogicalBufferStructPtr mIntLogicalToPhysical;
  110. /** Parameter name -> ConstantDefinition map, shared instance used by all parameter objects.
  111. This is a shared pointer because if the program is recompiled and the parameters
  112. change, this definition will alter, but previous params may reference the old def.
  113. */
  114. mutable GpuNamedConstantsPtr mConstantDefs;
  115. /// File from which to load named constants manually
  116. String mManualNamedConstantsFile;
  117. bool mLoadedManualNamedConstants;
  118. /** Internal method for setting up the basic parameter definitions for a subclass.
  119. @remarks
  120. Because StringInterface holds a dictionary of parameters per class, subclasses need to
  121. call this to ask the base class to add it's parameters to their dictionary as well.
  122. Can't do this in the constructor because that runs in a non-virtual context.
  123. @par
  124. The subclass must have called it's own createParamDictionary before calling this method.
  125. */
  126. void setupBaseParamDictionary(void);
  127. /** Internal method returns whether required capabilities for this program is supported.
  128. */
  129. bool isRequiredCapabilitiesSupported(void) const;
  130. /// @copydoc Resource::calculateSize
  131. size_t calculateSize(void) const { return 0; } // TODO
  132. /// Create the internal params logical & named mapping structures
  133. void createParameterMappingStructures(bool recreateIfExists = true) const;
  134. /// Create the internal params logical mapping structures
  135. void createLogicalParameterMappingStructures(bool recreateIfExists = true) const;
  136. /// Create the internal params named mapping structures
  137. void createNamedParameterMappingStructures(bool recreateIfExists = true) const;
  138. public:
  139. GpuProgram();
  140. virtual ~GpuProgram() {}
  141. virtual void load(void);
  142. virtual void unload() {}
  143. /** Sets the filename of the source assembly for this program.
  144. @remarks
  145. Setting this will have no effect until you (re)load the program.
  146. */
  147. virtual void setSourceFile(const String& filename);
  148. /** Sets the source assembly for this program from an in-memory string.
  149. @remarks
  150. Setting this will have no effect until you (re)load the program.
  151. */
  152. virtual void setSource(const String& source);
  153. /** Gets the syntax code for this program e.g. arbvp1, fp20, vs_1_1 etc */
  154. virtual const String& getSyntaxCode(void) const { return mSyntaxCode; }
  155. /** Sets the syntax code for this program e.g. arbvp1, fp20, vs_1_1 etc */
  156. virtual void setSyntaxCode(const String& syntax);
  157. /** Gets the name of the file used as source for this program. */
  158. virtual const String& getSourceFile(void) const { return mFilename; }
  159. /** Gets the assembler source for this program. */
  160. virtual const String& getSource(void) const { return mSource; }
  161. /// Set the program type (only valid before load)
  162. virtual void setType(GpuProgramType t);
  163. /// Get the program type
  164. virtual GpuProgramType getType(void) const { return mType; }
  165. /// Sets the gpu program profile (e.g. vs_1_1, etc.). Make sure it matches the program type.
  166. virtual void setProfile(GpuProgramProfile profile) { mProfile = profile; }
  167. virtual GpuProgramProfile getProfile() const { return mProfile; }
  168. /// Sets the name of the entry method for the program
  169. virtual void setEntryPoint(const String& entryPoint) { mEntryPoint = entryPoint; }
  170. virtual const String& getEntryPoint() const { return mEntryPoint; }
  171. /** Returns the GpuProgram which should be bound to the pipeline.
  172. @remarks
  173. This method is simply to allow some subclasses of GpuProgram to delegate
  174. the program which is bound to the pipeline to a delegate, if required. */
  175. virtual GpuProgram* _getBindingDelegate(void) { return this; }
  176. /** Returns whether this program can be supported on the current renderer and hardware. */
  177. virtual bool isSupported(void) const;
  178. /** Creates a new parameters object compatible with this program definition.
  179. @remarks
  180. It is recommended that you use this method of creating parameters objects
  181. rather than going direct to GpuProgramManager, because this method will
  182. populate any implementation-specific extras (like named parameters) where
  183. they are appropriate.
  184. */
  185. virtual GpuProgramParametersSharedPtr createParameters(void);
  186. /** Sets whether a vertex program includes the required instructions
  187. to perform skeletal animation.
  188. @remarks
  189. If this is set to true, OGRE will not blend the geometry according to
  190. skeletal animation, it will expect the vertex program to do it.
  191. */
  192. virtual void setSkeletalAnimationIncluded(bool included)
  193. { mSkeletalAnimation = included; }
  194. /** Returns whether a vertex program includes the required instructions
  195. to perform skeletal animation.
  196. @remarks
  197. If this returns true, OGRE will not blend the geometry according to
  198. skeletal animation, it will expect the vertex program to do it.
  199. */
  200. virtual bool isSkeletalAnimationIncluded(void) const { return mSkeletalAnimation; }
  201. /** Sets whether a vertex program includes the required instructions
  202. to perform morph animation.
  203. @remarks
  204. If this is set to true, OGRE will not blend the geometry according to
  205. morph animation, it will expect the vertex program to do it.
  206. */
  207. virtual void setMorphAnimationIncluded(bool included)
  208. { mMorphAnimation = included; }
  209. /** Sets whether a vertex program includes the required instructions
  210. to perform pose animation.
  211. @remarks
  212. If this is set to true, OGRE will not blend the geometry according to
  213. pose animation, it will expect the vertex program to do it.
  214. @param poseCount The number of simultaneous poses the program can blend
  215. */
  216. virtual void setPoseAnimationIncluded(ushort poseCount)
  217. { mPoseAnimation = poseCount; }
  218. /** Returns whether a vertex program includes the required instructions
  219. to perform morph animation.
  220. @remarks
  221. If this returns true, OGRE will not blend the geometry according to
  222. morph animation, it will expect the vertex program to do it.
  223. */
  224. virtual bool isMorphAnimationIncluded(void) const { return mMorphAnimation; }
  225. /** Returns whether a vertex program includes the required instructions
  226. to perform pose animation.
  227. @remarks
  228. If this returns true, OGRE will not blend the geometry according to
  229. pose animation, it will expect the vertex program to do it.
  230. */
  231. virtual bool isPoseAnimationIncluded(void) const { return mPoseAnimation > 0; }
  232. /** Returns the number of simultaneous poses the vertex program can
  233. blend, for use in pose animation.
  234. */
  235. virtual ushort getNumberOfPosesIncluded(void) const { return mPoseAnimation; }
  236. /** Sets whether this vertex program requires support for vertex
  237. texture fetch from the hardware.
  238. */
  239. virtual void setVertexTextureFetchRequired(bool r) { mVertexTextureFetch = r; }
  240. /** Returns whether this vertex program requires support for vertex
  241. texture fetch from the hardware.
  242. */
  243. virtual bool isVertexTextureFetchRequired(void) const { return mVertexTextureFetch; }
  244. /** Sets whether this geometry program requires adjacency information
  245. from the input primitives.
  246. */
  247. virtual void setAdjacencyInfoRequired(bool r) { mNeedsAdjacencyInfo = r; }
  248. /** Returns whether this geometry program requires adjacency information
  249. from the input primitives.
  250. */
  251. virtual bool isAdjacencyInfoRequired(void) const { return mNeedsAdjacencyInfo; }
  252. /** Get a reference to the default parameters which are to be used for all
  253. uses of this program.
  254. @remarks
  255. A program can be set up with a list of default parameters, which can save time when
  256. using a program many times in a material with roughly the same settings. By
  257. retrieving the default parameters and populating it with the most used options,
  258. any new parameter objects created from this program afterwards will automatically include
  259. the default parameters; thus users of the program need only change the parameters
  260. which are unique to their own usage of the program.
  261. */
  262. virtual GpuProgramParametersSharedPtr getDefaultParameters(void);
  263. /** Returns true if default parameters have been set up.
  264. */
  265. virtual bool hasDefaultParameters(void) const { return mDefaultParams != nullptr; }
  266. /** Returns whether a vertex program wants light and material states to be passed
  267. through fixed pipeline low level API rendering calls (default false, subclasses can override)
  268. @remarks
  269. Most vertex programs do not need this material information, however GLSL
  270. shaders can refer to this material and lighting state so enable this option
  271. */
  272. virtual bool getPassSurfaceAndLightStates(void) const { return false; }
  273. /** Returns whether a fragment program wants fog state to be passed
  274. through fixed pipeline low level API rendering calls (default true, subclasses can override)
  275. @remarks
  276. On DirectX, shader model 2 and earlier continues to have fixed-function fog
  277. applied to it, so fog state is still passed (you should disable fog on the
  278. pass if you want to perform fog in the shader). In OpenGL it is also
  279. common to be able to access the fixed-function fog state inside the shader.
  280. */
  281. virtual bool getPassFogStates(void) const { return true; }
  282. /** Returns whether a vertex program wants transform state to be passed
  283. through fixed pipeline low level API rendering calls
  284. @remarks
  285. Most vertex programs do not need fixed-function transform information, however GLSL
  286. shaders can refer to this state so enable this option
  287. */
  288. virtual bool getPassTransformStates(void) const { return false; }
  289. /** Returns a string that specifies the language of the gpu programs as specified
  290. in a material script. ie: asm, cg, hlsl, glsl
  291. */
  292. virtual const String& getLanguage(void) const;
  293. /** Did this program encounter a compile error when loading?
  294. */
  295. virtual bool hasCompileError(void) const { return mCompileError; }
  296. /** Reset a compile error if it occurred, allowing the load to be retried
  297. */
  298. virtual void resetCompileError(void) { mCompileError = false; }
  299. /** Allows you to manually provide a set of named parameter mappings
  300. to a program which would not be able to derive named parameters itself.
  301. @remarks
  302. You may wish to use this if you have assembler programs that were compiled
  303. from a high-level source, and want the convenience of still being able
  304. to use the named parameters from the original high-level source.
  305. @see setManualNamedConstantsFile
  306. */
  307. virtual void setManualNamedConstants(const GpuNamedConstants& namedConstants);
  308. /// Get a read-only reference to the named constants registered for this program (manually or automatically)
  309. virtual const GpuNamedConstants& getNamedConstants() const { return *mConstantDefs.get(); }
  310. /** Specifies the name of a file from which to load named parameters mapping
  311. for a program which would not be able to derive named parameters itself.
  312. @remarks
  313. You may wish to use this if you have assembler programs that were compiled
  314. from a high-level source, and want the convenience of still being able
  315. to use the named parameters from the original high-level source. This
  316. method will make a low-level program search in the resource group of the
  317. program for the named file from which to load parameter names from.
  318. The file must be in the format produced by GpuNamedConstants::save.
  319. */
  320. virtual void setManualNamedConstantsFile(const String& paramDefFile);
  321. /** Gets the name of a file from which to load named parameters mapping
  322. for a program which would not be able to derive named parameters itself.
  323. */
  324. virtual const String& getManualNamedConstantsFile() const { return mManualNamedConstantsFile; }
  325. /** Get the full list of named constants.
  326. @note
  327. Only available if this parameters object has named parameters, which means either
  328. a high-level program which loads them, or a low-level program which has them
  329. specified manually.
  330. */
  331. virtual const GpuNamedConstants& getConstantDefinitions() const { return *mConstantDefs.get(); }
  332. protected:
  333. /// Virtual method which must be implemented by subclasses, load from mSource
  334. virtual void loadFromSource(void) = 0;
  335. };
  336. /** @} */
  337. }
  338. #endif