| 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 | |
|---|
| 7 | Copyright (c) 2000-2006 Torus Knot Software Ltd |
|---|
| 8 | Also see acknowledgements in Readme.html |
|---|
| 9 | |
|---|
| 10 | This program is free software you can redistribute it and/or modify it under |
|---|
| 11 | the terms of the GNU Lesser General Public License as published by the Free Software |
|---|
| 12 | Foundation either version 2 of the License, or (at your option) any later |
|---|
| 13 | version. |
|---|
| 14 | |
|---|
| 15 | This program is distributed in the hope that it will be useful, but WITHOUT |
|---|
| 16 | ANY WARRANTY without even the implied warranty of MERCHANTABILITY or FITNESS |
|---|
| 17 | FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. |
|---|
| 18 | |
|---|
| 19 | You should have received a copy of the GNU Lesser General Public License along with |
|---|
| 20 | this program if not, write to the Free Software Foundation, Inc., 59 Temple |
|---|
| 21 | Place - Suite 330, Boston, MA 02111-1307, USA, or go to |
|---|
| 22 | http://www.gnu.org/copyleft/lesser.txt. |
|---|
| 23 | |
|---|
| 24 | You may alternatively use this source under the terms of a specific version of |
|---|
| 25 | the OGRE Unrestricted License provided you have obtained such a license from |
|---|
| 26 | Torus Knot Software Ltd. |
|---|
| 27 | ----------------------------------------------------------------------------- |
|---|
| 28 | */ |
|---|
| 29 | #include "OgreStableHeaders.h" |
|---|
| 30 | #include "OgreHighLevelGpuProgram.h" |
|---|
| 31 | #include "OgreException.h" |
|---|
| 32 | #include "OgreGpuProgramManager.h" |
|---|
| 33 | #include "OgreLogManager.h" |
|---|
| 34 | |
|---|
| 35 | namespace Ogre |
|---|
| 36 | { |
|---|
| 37 | //--------------------------------------------------------------------------- |
|---|
| 38 | HighLevelGpuProgram::HighLevelGpuProgram(ResourceManager* creator, |
|---|
| 39 | const String& name, ResourceHandle handle, const String& group, |
|---|
| 40 | bool isManual, ManualResourceLoader* loader) |
|---|
| 41 | : GpuProgram(creator, name, handle, group, isManual, loader), |
|---|
| 42 | mHighLevelLoaded(false), mAssemblerProgram(0), mConstantDefsBuilt(false) |
|---|
| 43 | { |
|---|
| 44 | } |
|---|
| 45 | //--------------------------------------------------------------------------- |
|---|
| 46 | void HighLevelGpuProgram::loadImpl() |
|---|
| 47 | { |
|---|
| 48 | if (isSupported()) |
|---|
| 49 | { |
|---|
| 50 | // load self |
|---|
| 51 | loadHighLevel(); |
|---|
| 52 | |
|---|
| 53 | // create low-level implementation |
|---|
| 54 | createLowLevelImpl(); |
|---|
| 55 | // load constructed assembler program (if it exists) |
|---|
| 56 | if (!mAssemblerProgram.isNull()) |
|---|
| 57 | { |
|---|
| 58 | mAssemblerProgram->load(); |
|---|
| 59 | } |
|---|
| 60 | } |
|---|
| 61 | } |
|---|
| 62 | //--------------------------------------------------------------------------- |
|---|
| 63 | void HighLevelGpuProgram::unloadImpl() |
|---|
| 64 | { |
|---|
| 65 | if (!mAssemblerProgram.isNull()) |
|---|
| 66 | { |
|---|
| 67 | mAssemblerProgram->getCreator()->remove(mAssemblerProgram->getHandle()); |
|---|
| 68 | mAssemblerProgram.setNull(); |
|---|
| 69 | } |
|---|
| 70 | |
|---|
| 71 | unloadHighLevel(); |
|---|
| 72 | } |
|---|
| 73 | //--------------------------------------------------------------------------- |
|---|
| 74 | HighLevelGpuProgram::~HighLevelGpuProgram() |
|---|
| 75 | { |
|---|
| 76 | // superclasses will trigger unload |
|---|
| 77 | } |
|---|
| 78 | //--------------------------------------------------------------------------- |
|---|
| 79 | GpuProgramParametersSharedPtr HighLevelGpuProgram::createParameters(void) |
|---|
| 80 | { |
|---|
| 81 | // Lock mutex before allowing this since this is a top-level method |
|---|
| 82 | // called outside of the load() |
|---|
| 83 | OGRE_LOCK_AUTO_MUTEX |
|---|
| 84 | |
|---|
| 85 | // Make sure param defs are loaded |
|---|
| 86 | GpuProgramParametersSharedPtr params = GpuProgramManager::getSingleton().createParameters(); |
|---|
| 87 | // Only populate named parameters if we can support this program |
|---|
| 88 | if (this->isSupported()) |
|---|
| 89 | { |
|---|
| 90 | loadHighLevel(); |
|---|
| 91 | // Errors during load may have prevented compile |
|---|
| 92 | if (this->isSupported()) |
|---|
| 93 | { |
|---|
| 94 | populateParameterNames(params); |
|---|
| 95 | } |
|---|
| 96 | } |
|---|
| 97 | // Copy in default parameters if present |
|---|
| 98 | if (!mDefaultParams.isNull()) |
|---|
| 99 | params->copyConstantsFrom(*(mDefaultParams.get())); |
|---|
| 100 | return params; |
|---|
| 101 | } |
|---|
| 102 | //--------------------------------------------------------------------------- |
|---|
| 103 | void HighLevelGpuProgram::loadHighLevel(void) |
|---|
| 104 | { |
|---|
| 105 | if (!mHighLevelLoaded) |
|---|
| 106 | { |
|---|
| 107 | try |
|---|
| 108 | { |
|---|
| 109 | loadHighLevelImpl(); |
|---|
| 110 | mHighLevelLoaded = true; |
|---|
| 111 | } |
|---|
| 112 | catch (const Exception& e) |
|---|
| 113 | { |
|---|
| 114 | // will already have been logged |
|---|
| 115 | StringUtil::StrStreamType str; |
|---|
| 116 | str << "High-level program " << mName << " encountered an error " |
|---|
| 117 | << "during loading and is thus not supported." |
|---|
| 118 | << "\n" |
|---|
| 119 | << e.getFullDescription(); |
|---|
| 120 | LogManager::getSingleton().logMessage(str.str()); |
|---|
| 121 | |
|---|
| 122 | mCompileError = true; |
|---|
| 123 | } |
|---|
| 124 | } |
|---|
| 125 | } |
|---|
| 126 | //--------------------------------------------------------------------------- |
|---|
| 127 | void HighLevelGpuProgram::unloadHighLevel(void) |
|---|
| 128 | { |
|---|
| 129 | if (mHighLevelLoaded) |
|---|
| 130 | { |
|---|
| 131 | unloadHighLevelImpl(); |
|---|
| 132 | // Clear saved constant defs |
|---|
| 133 | mConstantDefs.map.clear(); |
|---|
| 134 | mConstantDefs.floatBufferSize = 0; |
|---|
| 135 | mConstantDefs.intBufferSize = 0; |
|---|
| 136 | mConstantDefsBuilt = false; |
|---|
| 137 | mFloatLogicalToPhysical.map.clear(); |
|---|
| 138 | mFloatLogicalToPhysical.bufferSize = 0; |
|---|
| 139 | mIntLogicalToPhysical.map.clear(); |
|---|
| 140 | mIntLogicalToPhysical.bufferSize = 0; |
|---|
| 141 | |
|---|
| 142 | mHighLevelLoaded = false; |
|---|
| 143 | } |
|---|
| 144 | } |
|---|
| 145 | //--------------------------------------------------------------------------- |
|---|
| 146 | void HighLevelGpuProgram::loadHighLevelImpl(void) |
|---|
| 147 | { |
|---|
| 148 | if (mLoadFromFile) |
|---|
| 149 | { |
|---|
| 150 | // find & load source code |
|---|
| 151 | DataStreamPtr stream = |
|---|
| 152 | ResourceGroupManager::getSingleton().openResource( |
|---|
| 153 | mFilename, mGroup, true, this); |
|---|
| 154 | |
|---|
| 155 | mSource = stream->getAsString(); |
|---|
| 156 | } |
|---|
| 157 | |
|---|
| 158 | loadFromSource(); |
|---|
| 159 | } |
|---|
| 160 | //--------------------------------------------------------------------- |
|---|
| 161 | const GpuNamedConstants& HighLevelGpuProgram::getConstantDefinitions() const |
|---|
| 162 | { |
|---|
| 163 | if (!mConstantDefsBuilt) |
|---|
| 164 | { |
|---|
| 165 | buildConstantDefinitions(); |
|---|
| 166 | mConstantDefsBuilt = true; |
|---|
| 167 | } |
|---|
| 168 | return mConstantDefs; |
|---|
| 169 | |
|---|
| 170 | } |
|---|
| 171 | //--------------------------------------------------------------------- |
|---|
| 172 | void HighLevelGpuProgram::populateParameterNames(GpuProgramParametersSharedPtr params) |
|---|
| 173 | { |
|---|
| 174 | params->_setNamedConstants(&getConstantDefinitions()); |
|---|
| 175 | // also set logical / physical maps for programs which use this |
|---|
| 176 | params->_setLogicalIndexes(&mFloatLogicalToPhysical, &mIntLogicalToPhysical); |
|---|
| 177 | } |
|---|
| 178 | //----------------------------------------------------------------------- |
|---|
| 179 | //----------------------------------------------------------------------- |
|---|
| 180 | HighLevelGpuProgramPtr& HighLevelGpuProgramPtr::operator=(const GpuProgramPtr& r) |
|---|
| 181 | { |
|---|
| 182 | // Can assign direct |
|---|
| 183 | if (pRep == static_cast<HighLevelGpuProgram*>(r.getPointer())) |
|---|
| 184 | return *this; |
|---|
| 185 | release(); |
|---|
| 186 | // lock & copy other mutex pointer |
|---|
| 187 | OGRE_MUTEX_CONDITIONAL(r.OGRE_AUTO_MUTEX_NAME) |
|---|
| 188 | { |
|---|
| 189 | OGRE_LOCK_MUTEX(*r.OGRE_AUTO_MUTEX_NAME) |
|---|
| 190 | OGRE_COPY_AUTO_SHARED_MUTEX(r.OGRE_AUTO_MUTEX_NAME) |
|---|
| 191 | pRep = static_cast<HighLevelGpuProgram*>(r.getPointer()); |
|---|
| 192 | pUseCount = r.useCountPointer(); |
|---|
| 193 | if (pUseCount) |
|---|
| 194 | { |
|---|
| 195 | ++(*pUseCount); |
|---|
| 196 | } |
|---|
| 197 | } |
|---|
| 198 | else |
|---|
| 199 | { |
|---|
| 200 | // RHS must be a null pointer |
|---|
| 201 | assert(r.isNull() && "RHS must be null if it has no mutex!"); |
|---|
| 202 | setNull(); |
|---|
| 203 | } |
|---|
| 204 | return *this; |
|---|
| 205 | } |
|---|
| 206 | |
|---|
| 207 | |
|---|
| 208 | } |
|---|