CmGpuProgram.h 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  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 "CmGpuProgramParams.h"
  30. namespace CamelotEngine {
  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 CM_EXPORT GpuProgram
  70. {
  71. protected:
  72. /// The type of the program
  73. GpuProgramType mType;
  74. /// Does this (geometry) program require adjacency information?
  75. bool mNeedsAdjacencyInfo;
  76. /// Name of the shader entry method
  77. String mEntryPoint;
  78. /// Shader profiler that we are targeting (e.g. vs_1_1, etc.). Make sure profile matches the type.
  79. GpuProgramProfile mProfile;
  80. /// The assembler source of the program (may be blank until file loaded)
  81. String mSource;
  82. /// Syntax code e.g. arbvp1, vs_2_0 etc
  83. String mSyntaxCode;
  84. /// The default parameters for use with this object
  85. GpuProgramParametersSharedPtr mDefaultParams;
  86. /// Did we encounter a compilation error?
  87. bool mCompileError;
  88. /** Record of logical to physical buffer maps. Mandatory for low-level
  89. programs or high-level programs which set their params the same way.
  90. This is a shared pointer because if the program is recompiled and the parameters
  91. change, this definition will alter, but previous params may reference the old def. */
  92. mutable GpuLogicalBufferStructPtr mFloatLogicalToPhysical;
  93. /** Record of logical to physical buffer maps. Mandatory for low-level
  94. programs or high-level programs which set their params the same way.
  95. This is a shared pointer because if the program is recompiled and the parameters
  96. change, this definition will alter, but previous params may reference the old def.*/
  97. mutable GpuLogicalBufferStructPtr mIntLogicalToPhysical;
  98. /** Parameter name -> ConstantDefinition map, shared instance used by all parameter objects.
  99. This is a shared pointer because if the program is recompiled and the parameters
  100. change, this definition will alter, but previous params may reference the old def.
  101. */
  102. mutable GpuNamedConstantsPtr mConstantDefs;
  103. /** Internal method returns whether required capabilities for this program is supported.
  104. */
  105. bool isRequiredCapabilitiesSupported(void) const;
  106. /// @copydoc Resource::calculateSize
  107. size_t calculateSize(void) const { return 0; } // TODO
  108. /// Create the internal params logical & named mapping structures
  109. void createParameterMappingStructures(bool recreateIfExists = true) const;
  110. /// Create the internal params logical mapping structures
  111. void createLogicalParameterMappingStructures(bool recreateIfExists = true) const;
  112. /// Create the internal params named mapping structures
  113. void createNamedParameterMappingStructures(bool recreateIfExists = true) const;
  114. public:
  115. GpuProgram();
  116. virtual ~GpuProgram() {}
  117. virtual void load(void);
  118. virtual void unload() {}
  119. /** Sets the source assembly for this program from an in-memory string.
  120. @remarks
  121. Setting this will have no effect until you (re)load the program.
  122. */
  123. virtual void setSource(const String& source);
  124. /** Gets the syntax code for this program e.g. arbvp1, fp20, vs_1_1 etc */
  125. virtual const String& getSyntaxCode(void) const { return mSyntaxCode; }
  126. /** Sets the syntax code for this program e.g. arbvp1, fp20, vs_1_1 etc */
  127. virtual void setSyntaxCode(const String& syntax);
  128. /** Gets the assembler source for this program. */
  129. virtual const String& getSource(void) const { return mSource; }
  130. /// Set the program type (only valid before load)
  131. virtual void setType(GpuProgramType t);
  132. /// Get the program type
  133. virtual GpuProgramType getType(void) const { return mType; }
  134. /// Sets the gpu program profile (e.g. vs_1_1, etc.). Make sure it matches the program type.
  135. virtual void setProfile(GpuProgramProfile profile) { mProfile = profile; }
  136. virtual GpuProgramProfile getProfile() const { return mProfile; }
  137. /// Sets the name of the entry method for the program
  138. virtual void setEntryPoint(const String& entryPoint) { mEntryPoint = entryPoint; }
  139. virtual const String& getEntryPoint() const { return mEntryPoint; }
  140. /** Returns the GpuProgram which should be bound to the pipeline.
  141. @remarks
  142. This method is simply to allow some subclasses of GpuProgram to delegate
  143. the program which is bound to the pipeline to a delegate, if required. */
  144. virtual GpuProgram* _getBindingDelegate(void) { return this; }
  145. /** Returns whether this program can be supported on the current renderer and hardware. */
  146. virtual bool isSupported(void) const;
  147. /** Sets whether this geometry program requires adjacency information
  148. from the input primitives.
  149. */
  150. virtual void setAdjacencyInfoRequired(bool r) { mNeedsAdjacencyInfo = r; }
  151. /** Returns whether this geometry program requires adjacency information
  152. from the input primitives.
  153. */
  154. virtual bool isAdjacencyInfoRequired(void) const { return mNeedsAdjacencyInfo; }
  155. /** Creates a new parameters object compatible with this program definition.
  156. @remarks
  157. It is recommended that you use this method of creating parameters objects
  158. rather than going direct to GpuProgramManager, because this method will
  159. populate any implementation-specific extras (like named parameters) where
  160. they are appropriate.
  161. */
  162. virtual GpuProgramParametersSharedPtr createParameters(void);
  163. /** Get a reference to the default parameters which are to be used for all
  164. uses of this program.
  165. @remarks
  166. A program can be set up with a list of default parameters, which can save time when
  167. using a program many times in a material with roughly the same settings. By
  168. retrieving the default parameters and populating it with the most used options,
  169. any new parameter objects created from this program afterwards will automatically include
  170. the default parameters; thus users of the program need only change the parameters
  171. which are unique to their own usage of the program.
  172. */
  173. virtual GpuProgramParametersSharedPtr getDefaultParameters(void);
  174. /** Returns true if default parameters have been set up.
  175. */
  176. virtual bool hasDefaultParameters(void) const { return mDefaultParams != nullptr; }
  177. /** Returns a string that specifies the language of the gpu programs as specified
  178. in a material script. ie: asm, cg, hlsl, glsl
  179. */
  180. virtual const String& getLanguage(void) const;
  181. /** Did this program encounter a compile error when loading?
  182. */
  183. virtual bool hasCompileError(void) const { return mCompileError; }
  184. /** Reset a compile error if it occurred, allowing the load to be retried
  185. */
  186. virtual void resetCompileError(void) { mCompileError = false; }
  187. /// Get a read-only reference to the named constants registered for this program (manually or automatically)
  188. virtual const GpuNamedConstants& getNamedConstants() const { return *mConstantDefs.get(); }
  189. /** Get the full list of named constants.
  190. @note
  191. Only available if this parameters object has named parameters, which means either
  192. a high-level program which loads them, or a low-level program which has them
  193. specified manually.
  194. */
  195. virtual const GpuNamedConstants& getConstantDefinitions() const { return *mConstantDefs.get(); }
  196. protected:
  197. /// Virtual method which must be implemented by subclasses, load from mSource
  198. virtual void loadFromSource(void) = 0;
  199. };
  200. /** @} */
  201. }
  202. #endif