/* ----------------------------------------------------------------------------- This source file is part of OGRE (Object-oriented Graphics Rendering Engine) For the latest info, see http://www.ogre3d.org/ Copyright (c) 2000-2011 Torus Knot Software Ltd Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. ----------------------------------------------------------------------------- */ #include "OgreGpuProgram.h" #include "OgreHighLevelGpuProgram.h" #include "OgreVector3.h" #include "OgreVector4.h" #include "OgreRenderSystemCapabilities.h" #include "OgreStringConverter.h" #include "OgreException.h" #include "OgreRenderSystem.h" #include "CmRenderSystemManager.h" namespace Ogre { //----------------------------------------------------------------------------- GpuProgram::GpuProgram() :mType(GPT_VERTEX_PROGRAM), mLoadFromFile(true), mSkeletalAnimation(false), mMorphAnimation(false), mPoseAnimation(0), mVertexTextureFetch(false), mNeedsAdjacencyInfo(false), mCompileError(false), mLoadedManualNamedConstants(false), mProfile(GPP_NONE) { createParameterMappingStructures(); } //----------------------------------------------------------------------------- void GpuProgram::setType(GpuProgramType t) { mType = t; } //----------------------------------------------------------------------------- void GpuProgram::setSyntaxCode(const String& syntax) { mSyntaxCode = syntax; } //----------------------------------------------------------------------------- void GpuProgram::setSourceFile(const String& filename) { mFilename = filename; mSource.clear(); mLoadFromFile = true; mCompileError = false; } //----------------------------------------------------------------------------- void GpuProgram::setSource(const String& source) { mSource = source; mFilename.clear(); mLoadFromFile = false; mCompileError = false; } //----------------------------------------------------------------------------- void GpuProgram::load(void) { // Call polymorphic load try { loadFromSource(); assert(mDefaultParams == nullptr); mDefaultParams = createParameters(); } catch (const Exception&) { // will already have been logged //LogManager::getSingleton().stream() // << "Gpu program " << mName << " encountered an error " // << "during loading and is thus not supported."; mCompileError = true; } } //----------------------------------------------------------------------------- bool GpuProgram::isRequiredCapabilitiesSupported(void) const { // TODO PORT- I dont think I'll be using these capabilities return true; //const RenderSystemCapabilities* caps = // Root::getSingleton().getRenderSystem()->getCapabilities(); // // If skeletal animation is being done, we need support for UBYTE4 // if (isSkeletalAnimationIncluded() && // !caps->hasCapability(RSC_VERTEX_FORMAT_UBYTE4)) // { // return false; // } //// Vertex texture fetch required? //if (isVertexTextureFetchRequired() && // !caps->hasCapability(RSC_VERTEX_TEXTURE_FETCH)) //{ // return false; //} // return true; } //----------------------------------------------------------------------------- bool GpuProgram::isSupported(void) const { if (mCompileError || !isRequiredCapabilitiesSupported()) return false; RenderSystem* rs = CamelotEngine::RenderSystemManager::getActive(); return rs->getCapabilities()->isShaderProfileSupported(mSyntaxCode); } //--------------------------------------------------------------------- void GpuProgram::createParameterMappingStructures(bool recreateIfExists) const { createLogicalParameterMappingStructures(recreateIfExists); createNamedParameterMappingStructures(recreateIfExists); } //--------------------------------------------------------------------- void GpuProgram::createLogicalParameterMappingStructures(bool recreateIfExists) const { if (recreateIfExists || (mFloatLogicalToPhysical == nullptr)) mFloatLogicalToPhysical = GpuLogicalBufferStructPtr(OGRE_NEW GpuLogicalBufferStruct()); if (recreateIfExists || (mIntLogicalToPhysical == nullptr)) mIntLogicalToPhysical = GpuLogicalBufferStructPtr(OGRE_NEW GpuLogicalBufferStruct()); } //--------------------------------------------------------------------- void GpuProgram::createNamedParameterMappingStructures(bool recreateIfExists) const { if (recreateIfExists || (mConstantDefs == nullptr)) mConstantDefs = GpuNamedConstantsPtr(OGRE_NEW GpuNamedConstants()); } //--------------------------------------------------------------------- void GpuProgram::setManualNamedConstantsFile(const String& paramDefFile) { mManualNamedConstantsFile = paramDefFile; mLoadedManualNamedConstants = false; } //--------------------------------------------------------------------- void GpuProgram::setManualNamedConstants(const GpuNamedConstants& namedConstants) { createParameterMappingStructures(); *mConstantDefs.get() = namedConstants; mFloatLogicalToPhysical->bufferSize = mConstantDefs->floatBufferSize; mIntLogicalToPhysical->bufferSize = mConstantDefs->intBufferSize; mFloatLogicalToPhysical->map.clear(); mIntLogicalToPhysical->map.clear(); // need to set up logical mappings too for some rendersystems for (GpuConstantDefinitionMap::const_iterator i = mConstantDefs->map.begin(); i != mConstantDefs->map.end(); ++i) { const String& name = i->first; const GpuConstantDefinition& def = i->second; // only consider non-array entries if (name.find("[") == String::npos) { GpuLogicalIndexUseMap::value_type val(def.logicalIndex, GpuLogicalIndexUse(def.physicalIndex, def.arraySize * def.elementSize, def.variability)); if (def.isFloat()) { mFloatLogicalToPhysical->map.insert(val); } else { mIntLogicalToPhysical->map.insert(val); } } } } //----------------------------------------------------------------------------- GpuProgramParametersSharedPtr GpuProgram::createParameters(void) { // Default implementation simply returns standard parameters. GpuProgramParametersSharedPtr ret = GpuProgramParametersSharedPtr(OGRE_NEW GpuProgramParameters()); // set up named parameters, if any if ((mConstantDefs != nullptr) && !mConstantDefs->map.empty()) { ret->_setNamedConstants(mConstantDefs); } // link shared logical / physical map for low-level use ret->_setLogicalIndexes(mFloatLogicalToPhysical, mIntLogicalToPhysical); // Copy in default parameters if present if (mDefaultParams != nullptr) ret->copyConstantsFrom(*(mDefaultParams.get())); return ret; } //----------------------------------------------------------------------------- GpuProgramParametersSharedPtr GpuProgram::getDefaultParameters(void) { if (mDefaultParams == nullptr) { mDefaultParams = createParameters(); } return mDefaultParams; } //----------------------------------------------------------------------------- void GpuProgram::setupBaseParamDictionary(void) { } //----------------------------------------------------------------------- const String& GpuProgram::getLanguage(void) const { static const String language = "asm"; return language; } }