OgreGpuProgram.cpp 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  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. #include "OgreGpuProgram.h"
  25. #include "OgreHighLevelGpuProgram.h"
  26. #include "OgreVector3.h"
  27. #include "OgreVector4.h"
  28. #include "OgreRenderSystemCapabilities.h"
  29. #include "OgreStringConverter.h"
  30. #include "OgreException.h"
  31. #include "OgreRenderSystem.h"
  32. #include "CmRenderSystemManager.h"
  33. namespace Ogre
  34. {
  35. //-----------------------------------------------------------------------------
  36. GpuProgram::GpuProgram()
  37. :mType(GPT_VERTEX_PROGRAM), mLoadFromFile(true), mSkeletalAnimation(false),
  38. mMorphAnimation(false), mPoseAnimation(0),
  39. mVertexTextureFetch(false), mNeedsAdjacencyInfo(false),
  40. mCompileError(false), mLoadedManualNamedConstants(false), mProfile(GPP_NONE)
  41. {
  42. createParameterMappingStructures();
  43. }
  44. //-----------------------------------------------------------------------------
  45. void GpuProgram::setType(GpuProgramType t)
  46. {
  47. mType = t;
  48. }
  49. //-----------------------------------------------------------------------------
  50. void GpuProgram::setSyntaxCode(const String& syntax)
  51. {
  52. mSyntaxCode = syntax;
  53. }
  54. //-----------------------------------------------------------------------------
  55. void GpuProgram::setSourceFile(const String& filename)
  56. {
  57. mFilename = filename;
  58. mSource.clear();
  59. mLoadFromFile = true;
  60. mCompileError = false;
  61. }
  62. //-----------------------------------------------------------------------------
  63. void GpuProgram::setSource(const String& source)
  64. {
  65. mSource = source;
  66. mFilename.clear();
  67. mLoadFromFile = false;
  68. mCompileError = false;
  69. }
  70. //-----------------------------------------------------------------------------
  71. void GpuProgram::load(void)
  72. {
  73. // Call polymorphic load
  74. try
  75. {
  76. loadFromSource();
  77. assert(mDefaultParams == nullptr);
  78. mDefaultParams = createParameters();
  79. }
  80. catch (const Exception&)
  81. {
  82. // will already have been logged
  83. //LogManager::getSingleton().stream()
  84. // << "Gpu program " << mName << " encountered an error "
  85. // << "during loading and is thus not supported.";
  86. mCompileError = true;
  87. }
  88. }
  89. //-----------------------------------------------------------------------------
  90. bool GpuProgram::isRequiredCapabilitiesSupported(void) const
  91. {
  92. // TODO PORT- I dont think I'll be using these capabilities
  93. return true;
  94. //const RenderSystemCapabilities* caps =
  95. // Root::getSingleton().getRenderSystem()->getCapabilities();
  96. // // If skeletal animation is being done, we need support for UBYTE4
  97. // if (isSkeletalAnimationIncluded() &&
  98. // !caps->hasCapability(RSC_VERTEX_FORMAT_UBYTE4))
  99. // {
  100. // return false;
  101. // }
  102. //// Vertex texture fetch required?
  103. //if (isVertexTextureFetchRequired() &&
  104. // !caps->hasCapability(RSC_VERTEX_TEXTURE_FETCH))
  105. //{
  106. // return false;
  107. //}
  108. // return true;
  109. }
  110. //-----------------------------------------------------------------------------
  111. bool GpuProgram::isSupported(void) const
  112. {
  113. if (mCompileError || !isRequiredCapabilitiesSupported())
  114. return false;
  115. RenderSystem* rs = CamelotEngine::RenderSystemManager::getActive();
  116. return rs->getCapabilities()->isShaderProfileSupported(mSyntaxCode);
  117. }
  118. //---------------------------------------------------------------------
  119. void GpuProgram::createParameterMappingStructures(bool recreateIfExists) const
  120. {
  121. createLogicalParameterMappingStructures(recreateIfExists);
  122. createNamedParameterMappingStructures(recreateIfExists);
  123. }
  124. //---------------------------------------------------------------------
  125. void GpuProgram::createLogicalParameterMappingStructures(bool recreateIfExists) const
  126. {
  127. if (recreateIfExists || (mFloatLogicalToPhysical == nullptr))
  128. mFloatLogicalToPhysical = GpuLogicalBufferStructPtr(OGRE_NEW GpuLogicalBufferStruct());
  129. if (recreateIfExists || (mIntLogicalToPhysical == nullptr))
  130. mIntLogicalToPhysical = GpuLogicalBufferStructPtr(OGRE_NEW GpuLogicalBufferStruct());
  131. }
  132. //---------------------------------------------------------------------
  133. void GpuProgram::createNamedParameterMappingStructures(bool recreateIfExists) const
  134. {
  135. if (recreateIfExists || (mConstantDefs == nullptr))
  136. mConstantDefs = GpuNamedConstantsPtr(OGRE_NEW GpuNamedConstants());
  137. }
  138. //---------------------------------------------------------------------
  139. void GpuProgram::setManualNamedConstantsFile(const String& paramDefFile)
  140. {
  141. mManualNamedConstantsFile = paramDefFile;
  142. mLoadedManualNamedConstants = false;
  143. }
  144. //---------------------------------------------------------------------
  145. void GpuProgram::setManualNamedConstants(const GpuNamedConstants& namedConstants)
  146. {
  147. createParameterMappingStructures();
  148. *mConstantDefs.get() = namedConstants;
  149. mFloatLogicalToPhysical->bufferSize = mConstantDefs->floatBufferSize;
  150. mIntLogicalToPhysical->bufferSize = mConstantDefs->intBufferSize;
  151. mFloatLogicalToPhysical->map.clear();
  152. mIntLogicalToPhysical->map.clear();
  153. // need to set up logical mappings too for some rendersystems
  154. for (GpuConstantDefinitionMap::const_iterator i = mConstantDefs->map.begin();
  155. i != mConstantDefs->map.end(); ++i)
  156. {
  157. const String& name = i->first;
  158. const GpuConstantDefinition& def = i->second;
  159. // only consider non-array entries
  160. if (name.find("[") == String::npos)
  161. {
  162. GpuLogicalIndexUseMap::value_type val(def.logicalIndex,
  163. GpuLogicalIndexUse(def.physicalIndex, def.arraySize * def.elementSize, def.variability));
  164. if (def.isFloat())
  165. {
  166. mFloatLogicalToPhysical->map.insert(val);
  167. }
  168. else
  169. {
  170. mIntLogicalToPhysical->map.insert(val);
  171. }
  172. }
  173. }
  174. }
  175. //-----------------------------------------------------------------------------
  176. GpuProgramParametersSharedPtr GpuProgram::createParameters(void)
  177. {
  178. // Default implementation simply returns standard parameters.
  179. GpuProgramParametersSharedPtr ret = GpuProgramParametersSharedPtr(OGRE_NEW GpuProgramParameters());
  180. // set up named parameters, if any
  181. if ((mConstantDefs != nullptr) && !mConstantDefs->map.empty())
  182. {
  183. ret->_setNamedConstants(mConstantDefs);
  184. }
  185. // link shared logical / physical map for low-level use
  186. ret->_setLogicalIndexes(mFloatLogicalToPhysical, mIntLogicalToPhysical);
  187. // Copy in default parameters if present
  188. if (mDefaultParams != nullptr)
  189. ret->copyConstantsFrom(*(mDefaultParams.get()));
  190. return ret;
  191. }
  192. //-----------------------------------------------------------------------------
  193. GpuProgramParametersSharedPtr GpuProgram::getDefaultParameters(void)
  194. {
  195. if (mDefaultParams == nullptr)
  196. {
  197. mDefaultParams = createParameters();
  198. }
  199. return mDefaultParams;
  200. }
  201. //-----------------------------------------------------------------------------
  202. void GpuProgram::setupBaseParamDictionary(void)
  203. {
  204. }
  205. //-----------------------------------------------------------------------
  206. const String& GpuProgram::getLanguage(void) const
  207. {
  208. static const String language = "asm";
  209. return language;
  210. }
  211. }