CmHighLevelGpuProgram.cpp 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  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 "CmHighLevelGpuProgram.h"
  25. #include "CmHighLevelGpuProgramManager.h"
  26. #include "CmRenderSystemManager.h"
  27. #include "CmRenderSystem.h"
  28. #include "CmException.h"
  29. #include "CmRenderSystem.h"
  30. #include "CmRenderSystemManager.h"
  31. #include "CmAsyncOp.h"
  32. #if CM_DEBUG_MODE
  33. #define THROW_IF_NOT_RENDER_THREAD throwIfNotRenderThread();
  34. #else
  35. #define THROW_IF_NOT_RENDER_THREAD
  36. #endif
  37. namespace CamelotEngine
  38. {
  39. //---------------------------------------------------------------------------
  40. HighLevelGpuProgram::HighLevelGpuProgram()
  41. : GpuProgram(),
  42. mHighLevelLoaded(false), mAssemblerProgram(0), mConstantDefsBuilt(false)
  43. {
  44. }
  45. //---------------------------------------------------------------------------
  46. void HighLevelGpuProgram::initialize()
  47. {
  48. RenderSystemManager::getActive()->queueResourceCommand(boost::bind(&HighLevelGpuProgram::initialize_internal, this));
  49. }
  50. //---------------------------------------------------------------------------
  51. void HighLevelGpuProgram::initialize_internal()
  52. {
  53. if (isSupported())
  54. {
  55. // load self
  56. loadHighLevel();
  57. // create low-level implementation
  58. createLowLevelImpl();
  59. // load constructed assembler program (if it exists)
  60. if (mAssemblerProgram != nullptr && mAssemblerProgram.get() != this)
  61. {
  62. mAssemblerProgram->initialize_internal();
  63. }
  64. Resource::initialize_internal();
  65. }
  66. }
  67. //---------------------------------------------------------------------------
  68. void HighLevelGpuProgram::unload()
  69. {
  70. if (mAssemblerProgram != nullptr && mAssemblerProgram.get() != this)
  71. {
  72. mAssemblerProgram = nullptr;
  73. }
  74. unloadHighLevel();
  75. resetCompileError();
  76. }
  77. //---------------------------------------------------------------------------
  78. HighLevelGpuProgram::~HighLevelGpuProgram()
  79. {
  80. // superclasses will trigger unload
  81. }
  82. //---------------------------------------------------------------------------
  83. void HighLevelGpuProgram::createParameters_internal(AsyncOp& op)
  84. {
  85. THROW_IF_NOT_RENDER_THREAD
  86. // Make sure param defs are loaded
  87. GpuProgramParametersSharedPtr params = GpuProgramParametersSharedPtr(new GpuProgramParameters());
  88. // Only populate named parameters if we can support this program
  89. if (this->isSupported())
  90. {
  91. populateParameterNames(params);
  92. }
  93. op.completeOperation(params);
  94. }
  95. //---------------------------------------------------------------------------
  96. void HighLevelGpuProgram::loadHighLevel(void)
  97. {
  98. if (!mHighLevelLoaded)
  99. {
  100. try
  101. {
  102. loadHighLevelImpl();
  103. mHighLevelLoaded = true;
  104. }
  105. catch (const Exception& e)
  106. {
  107. //// will already have been logged
  108. //LogManager::getSingleton().stream()
  109. // << "High-level program " << mName << " encountered an error "
  110. // << "during loading and is thus not supported.\n"
  111. // << e.getFullDescription();
  112. mCompileError = true;
  113. throw;
  114. }
  115. }
  116. }
  117. //---------------------------------------------------------------------------
  118. void HighLevelGpuProgram::unloadHighLevel(void)
  119. {
  120. if (mHighLevelLoaded)
  121. {
  122. unloadHighLevelImpl();
  123. // Clear saved constant defs
  124. mConstantDefsBuilt = false;
  125. createParameterMappingStructures(true);
  126. mHighLevelLoaded = false;
  127. }
  128. }
  129. //---------------------------------------------------------------------------
  130. void HighLevelGpuProgram::loadHighLevelImpl(void)
  131. {
  132. loadFromSource();
  133. }
  134. //---------------------------------------------------------------------
  135. const GpuNamedConstants& HighLevelGpuProgram::getConstantDefinitions() const
  136. {
  137. if (!mConstantDefsBuilt)
  138. {
  139. buildConstantDefinitions();
  140. mConstantDefsBuilt = true;
  141. }
  142. return *mConstantDefs.get();
  143. }
  144. //---------------------------------------------------------------------
  145. void HighLevelGpuProgram::populateParameterNames(GpuProgramParametersSharedPtr params)
  146. {
  147. getConstantDefinitions();
  148. params->_setNamedConstants(mConstantDefs);
  149. // also set logical / physical maps for programs which use this
  150. params->_setLogicalIndexes(mFloatLogicalToPhysical, mIntLogicalToPhysical, mSamplerLogicalToPhysical);
  151. }
  152. //---------------------------------------------------------------------
  153. HighLevelGpuProgramPtr HighLevelGpuProgram::create(const String& source, const String& entryPoint,
  154. const String& language, GpuProgramType gptype, GpuProgramProfile profile)
  155. {
  156. return HighLevelGpuProgramManager::instance().create(source, entryPoint, language, gptype, profile);
  157. }
  158. }
  159. #undef THROW_IF_NOT_RENDER_THREAD