CmHighLevelGpuProgram.cpp 5.8 KB

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