CmGpuProgram.cpp 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  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 "CmGpuProgram.h"
  25. #include "CmHighLevelGpuProgram.h"
  26. #include "CmVector3.h"
  27. #include "CmVector4.h"
  28. #include "CmRenderSystemCapabilities.h"
  29. #include "CmException.h"
  30. #include "CmRenderSystem.h"
  31. #include "CmRenderSystemManager.h"
  32. #include "CmAsyncOp.h"
  33. #include "CmGpuProgramRTTI.h"
  34. #if CM_DEBUG_MODE
  35. #define THROW_IF_NOT_RENDER_THREAD throwIfNotRenderThread();
  36. #else
  37. #define THROW_IF_NOT_RENDER_THREAD
  38. #endif
  39. namespace CamelotEngine
  40. {
  41. //-----------------------------------------------------------------------------
  42. GpuProgram::GpuProgram(const String& source, const String& entryPoint, const String& language,
  43. GpuProgramType gptype, GpuProgramProfile profile, bool isAdjacencyInfoRequired)
  44. :mSource(source), mEntryPoint(entryPoint), mSyntaxCode(language), mType(gptype),
  45. mProfile(profile), mNeedsAdjacencyInfo(isAdjacencyInfoRequired), mCompileError(false)
  46. {
  47. createParameterMappingStructures();
  48. }
  49. //----------------------------------------------------------------------------
  50. GpuProgram::~GpuProgram()
  51. {
  52. THROW_IF_NOT_RENDER_THREAD;
  53. }
  54. //-----------------------------------------------------------------------------
  55. void GpuProgram::initialize()
  56. {
  57. RenderSystemManager::getActive()->queueCommand(boost::bind(&GpuProgram::initialize_internal, this));
  58. }
  59. //-----------------------------------------------------------------------------
  60. void GpuProgram::initialize_internal(void)
  61. {
  62. // Call polymorphic load
  63. try
  64. {
  65. loadFromSource();
  66. Resource::initialize_internal();
  67. }
  68. catch (const Exception&)
  69. {
  70. // will already have been logged
  71. //LogManager::getSingleton().stream()
  72. // << "Gpu program " << mName << " encountered an error "
  73. // << "during loading and is thus not supported.";
  74. mCompileError = true;
  75. }
  76. }
  77. //-----------------------------------------------------------------------------
  78. void GpuProgram::unload_internal()
  79. {
  80. THROW_IF_NOT_RENDER_THREAD;
  81. }
  82. //-----------------------------------------------------------------------------
  83. bool GpuProgram::isSupported(void) const
  84. {
  85. if (mCompileError || !isRequiredCapabilitiesSupported())
  86. return false;
  87. RenderSystem* rs = CamelotEngine::RenderSystemManager::getActive();
  88. return rs->getCapabilities()->isShaderProfileSupported(mSyntaxCode);
  89. }
  90. //-----------------------------------------------------------------------------
  91. bool GpuProgram::isRequiredCapabilitiesSupported(void) const
  92. {
  93. return true;
  94. }
  95. //---------------------------------------------------------------------
  96. void GpuProgram::createParameterMappingStructures(bool recreateIfExists) const
  97. {
  98. createLogicalParameterMappingStructures(recreateIfExists);
  99. createNamedParameterMappingStructures(recreateIfExists);
  100. }
  101. //---------------------------------------------------------------------
  102. void GpuProgram::createLogicalParameterMappingStructures(bool recreateIfExists) const
  103. {
  104. if (recreateIfExists || (mFloatLogicalToPhysical == nullptr))
  105. mFloatLogicalToPhysical = GpuLogicalBufferStructPtr(new GpuLogicalBufferStruct());
  106. if (recreateIfExists || (mIntLogicalToPhysical == nullptr))
  107. mIntLogicalToPhysical = GpuLogicalBufferStructPtr(new GpuLogicalBufferStruct());
  108. if (recreateIfExists || (mSamplerLogicalToPhysical == nullptr))
  109. mSamplerLogicalToPhysical = GpuLogicalBufferStructPtr(new GpuLogicalBufferStruct());
  110. if(recreateIfExists || (mTextureLogicalToPhysical == nullptr))
  111. mTextureLogicalToPhysical = GpuLogicalBufferStructPtr(new GpuLogicalBufferStruct());
  112. }
  113. //---------------------------------------------------------------------
  114. void GpuProgram::createNamedParameterMappingStructures(bool recreateIfExists) const
  115. {
  116. if (recreateIfExists || (mConstantDefs == nullptr))
  117. mConstantDefs = GpuNamedConstantsPtr(new GpuNamedConstants());
  118. }
  119. //---------------------------------------------------------------------
  120. GpuProgramParametersSharedPtr GpuProgram::createParameters(void)
  121. {
  122. AsyncOp op = RenderSystemManager::getActive()->queueReturnCommand(boost::bind(&GpuProgram::createParameters_internal, this, _1), true);
  123. return op.getReturnValue<GpuProgramParametersSharedPtr>();
  124. }
  125. //-----------------------------------------------------------------------------
  126. void GpuProgram::createParameters_internal(AsyncOp& op)
  127. {
  128. THROW_IF_NOT_RENDER_THREAD
  129. // Default implementation simply returns standard parameters.
  130. GpuProgramParametersSharedPtr ret = GpuProgramParametersSharedPtr(new GpuProgramParameters());
  131. // set up named parameters, if any
  132. if ((mConstantDefs != nullptr) && !mConstantDefs->map.empty())
  133. {
  134. ret->_setNamedConstants(mConstantDefs);
  135. }
  136. // link shared logical / physical map for low-level use
  137. ret->_setLogicalIndexes(mFloatLogicalToPhysical, mIntLogicalToPhysical, mSamplerLogicalToPhysical, mTextureLogicalToPhysical);
  138. op.completeOperation(ret);
  139. }
  140. //-----------------------------------------------------------------------
  141. const String& GpuProgram::getLanguage(void) const
  142. {
  143. static const String language = "asm";
  144. return language;
  145. }
  146. const GpuNamedConstants& GpuProgram::getConstantDefinitions_internal() const
  147. {
  148. THROW_IF_NOT_RENDER_THREAD;
  149. return *mConstantDefs.get();
  150. }
  151. //-----------------------------------------------------------------------------
  152. void GpuProgram::throwIfNotRenderThread() const
  153. {
  154. if(CM_THREAD_CURRENT_ID != RenderSystemManager::getActive()->getRenderThreadId())
  155. CM_EXCEPT(InternalErrorException, "Calling an internal texture method from a non-render thread!");
  156. }
  157. /************************************************************************/
  158. /* SERIALIZATION */
  159. /************************************************************************/
  160. RTTITypeBase* GpuProgram::getRTTIStatic()
  161. {
  162. return GpuProgramRTTI::instance();
  163. }
  164. RTTITypeBase* GpuProgram::getRTTI() const
  165. {
  166. return GpuProgram::getRTTIStatic();
  167. }
  168. }
  169. #undef THROW_IF_NOT_RENDER_THREAD