CmGpuProgram.cpp 7.3 KB

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