2
0

CmGpuProgram.h 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  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 "CmPrerequisites.h"
  28. #include "CmRenderOperation.h"
  29. #include "CmGpuProgramParams.h"
  30. #include "CmRenderSystemCapabilities.h"
  31. #include "CmResource.h"
  32. namespace CamelotEngine {
  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. GPT_DOMAIN_PROGRAM,
  46. GPT_HULL_PROGRAM,
  47. GPT_COMPUTE_PROGRAM
  48. };
  49. /** Defines a program which runs on the GPU such as a vertex or fragment program.
  50. @remarks
  51. This class defines the low-level program in assembler code, the sort used to
  52. directly assemble into machine instructions for the GPU to execute. By nature,
  53. this means that the assembler source is rendersystem specific, which is why this
  54. is an abstract class - real instances are created through the RenderSystem.
  55. If you wish to use higher level shading languages like HLSL and Cg, you need to
  56. use the HighLevelGpuProgram class instead.
  57. */
  58. class CM_EXPORT GpuProgram : public Resource
  59. {
  60. protected:
  61. /// The type of the program
  62. GpuProgramType mType;
  63. /// Does this (geometry) program require adjacency information?
  64. bool mNeedsAdjacencyInfo;
  65. /// Name of the shader entry method
  66. String mEntryPoint;
  67. /// Shader profiler that we are targeting (e.g. vs_1_1, etc.). Make sure profile matches the type.
  68. GpuProgramProfile mProfile;
  69. /// The assembler source of the program (may be blank until file loaded)
  70. String mSource;
  71. /// Syntax code e.g. arbvp1, vs_2_0 etc
  72. String mSyntaxCode;
  73. /// Did we encounter a compilation error?
  74. bool mCompileError;
  75. /** Record of logical to physical buffer maps. Mandatory for low-level
  76. programs or high-level programs which set their params the same way.
  77. This is a shared pointer because if the program is recompiled and the parameters
  78. change, this definition will alter, but previous params may reference the old def. */
  79. mutable GpuLogicalBufferStructPtr mFloatLogicalToPhysical;
  80. /** Record of logical to physical buffer maps. Mandatory for low-level
  81. programs or high-level programs which set their params the same way.
  82. This is a shared pointer because if the program is recompiled and the parameters
  83. change, this definition will alter, but previous params may reference the old def.*/
  84. mutable GpuLogicalBufferStructPtr mIntLogicalToPhysical;
  85. /** Record of logical to physical buffer maps. Mandatory for low-level
  86. programs or high-level programs which set their params the same way.
  87. This is a shared pointer because if the program is recompiled and the parameters
  88. change, this definition will alter, but previous params may reference the old def.*/
  89. mutable GpuLogicalBufferStructPtr mSamplerLogicalToPhysical;
  90. /** Parameter name -> ConstantDefinition map, shared instance used by all parameter objects.
  91. This is a shared pointer because if the program is recompiled and the parameters
  92. change, this definition will alter, but previous params may reference the old def.
  93. */
  94. mutable GpuNamedConstantsPtr mConstantDefs;
  95. /** Internal method returns whether required capabilities for this program is supported.
  96. */
  97. bool isRequiredCapabilitiesSupported(void) const;
  98. /// @copydoc Resource::calculateSize
  99. size_t calculateSize(void) const { return 0; } // TODO
  100. /// Create the internal params logical & named mapping structures
  101. void createParameterMappingStructures(bool recreateIfExists = true) const;
  102. /// Create the internal params logical mapping structures
  103. void createLogicalParameterMappingStructures(bool recreateIfExists = true) const;
  104. /// Create the internal params named mapping structures
  105. void createNamedParameterMappingStructures(bool recreateIfExists = true) const;
  106. void throwIfNotRenderThread() const;
  107. public:
  108. virtual ~GpuProgram();
  109. /**
  110. * @brief Initializes the gpu program. This must be called right after the program is constructed.
  111. * Called by GpuProgramManager upon creation, so usually you don't want to call this manually.
  112. *
  113. * @note Initialization is not done immediately, and is instead just scheduled on the render thread.
  114. */
  115. void initialize();
  116. /**
  117. * @brief Performs GpuProgram initialization. Only callable from the render thread.
  118. */
  119. virtual void initialize_internal();
  120. virtual void unload() {}
  121. /** Gets the syntax code for this program e.g. arbvp1, fp20, vs_1_1 etc */
  122. virtual const String& getSyntaxCode(void) const { return mSyntaxCode; }
  123. /** Gets the assembler source for this program. */
  124. virtual const String& getSource(void) const { return mSource; }
  125. /// Get the program type
  126. virtual GpuProgramType getType(void) const { return mType; }
  127. virtual GpuProgramProfile getProfile() const { return mProfile; }
  128. virtual const String& getEntryPoint() const { return mEntryPoint; }
  129. /** Returns the GpuProgram which should be bound to the pipeline.
  130. @remarks
  131. This method is simply to allow some subclasses of GpuProgram to delegate
  132. the program which is bound to the pipeline to a delegate, if required. */
  133. virtual GpuProgram* getBindingDelegate_internal(void) { return this; }
  134. /** Returns whether this program can be supported on the current renderer and hardware. */
  135. virtual bool isSupported(void) const;
  136. /** Sets whether this geometry program requires adjacency information
  137. from the input primitives.
  138. */
  139. virtual void setAdjacencyInfoRequired(bool r) { mNeedsAdjacencyInfo = r; }
  140. /** Returns whether this geometry program requires adjacency information
  141. from the input primitives.
  142. */
  143. virtual bool isAdjacencyInfoRequired(void) const { return mNeedsAdjacencyInfo; }
  144. /** Creates a new parameters object compatible with this program definition.
  145. @remarks
  146. It is recommended that you use this method of creating parameters objects
  147. rather than going direct to GpuProgramManager, because this method will
  148. populate any implementation-specific extras (like named parameters) where
  149. they are appropriate.
  150. */
  151. GpuProgramParametersSharedPtr createParameters(void);
  152. virtual void createParameters_internal(AsyncOp& op);
  153. /** Returns a string that specifies the language of the gpu programs as specified
  154. in a material script. ie: asm, cg, hlsl, glsl
  155. */
  156. virtual const String& getLanguage(void) const;
  157. /** Did this program encounter a compile error when loading?
  158. */
  159. virtual bool hasCompileError(void) const { return mCompileError; }
  160. /** Reset a compile error if it occurred, allowing the load to be retried
  161. */
  162. virtual void resetCompileError(void) { mCompileError = false; }
  163. /** Get the full list of named constants.
  164. @note
  165. Only available if this parameters object has named parameters, which means either
  166. a high-level program which loads them, or a low-level program which has them
  167. specified manually.
  168. */
  169. virtual const GpuNamedConstants& getConstantDefinitions_internal() const;
  170. protected:
  171. friend class GpuProgramManager;
  172. GpuProgram(const String& source, const String& entryPoint, const String& language,
  173. GpuProgramType gptype, GpuProgramProfile profile, bool isAdjacencyInfoRequired = false);
  174. /// Virtual method which must be implemented by subclasses, load from mSource
  175. virtual void loadFromSource(void) = 0;
  176. /************************************************************************/
  177. /* SERIALIZATION */
  178. /************************************************************************/
  179. public:
  180. friend class GpuProgramRTTI;
  181. static RTTITypeBase* getRTTIStatic();
  182. virtual RTTITypeBase* getRTTI() const;
  183. };
  184. /** @} */
  185. }
  186. #endif