OgreGpuProgram.h 19 KB

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