OgreGpuProgram.h 19 KB

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