| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186 |
- /*
- -----------------------------------------------------------------------------
- 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 "CmCgProgram.h"
- #include "CmGpuProgramManager.h"
- #include "CmHighLevelGpuProgramManager.h"
- #include "CmRenderSystem.h"
- #include "CmDebug.h"
- #include "CmException.h"
- #include "CmCgProgramRTTI.h"
- namespace BansheeEngine {
- void checkForCgError(const String& ogreMethod, const String& errorTextPrefix, CGcontext context)
- {
- CGerror error = cgGetError();
- if (error != CG_NO_ERROR)
- {
- String msg = errorTextPrefix + cgGetErrorString(error);
- if (error == CG_COMPILER_ERROR)
- {
- // Get listing with full compile errors
- msg = msg + "\n" + cgGetLastListing(context);
- }
- CM_EXCEPT(InternalErrorException, msg);
- }
- }
- //-----------------------------------------------------------------------
- void CgProgram::selectProfile(void)
- {
- mSelectedProfile.clear();
- mSelectedCgProfile = CG_PROFILE_UNKNOWN;
- RenderSystem* rs = RenderSystem::instancePtr();
- const String& rsName = rs->getName();
- mSelectedProfile = GpuProgramManager::instance().gpuProgProfileToRSSpecificProfile(mProfile);
- GpuProgramManager& gpuMgr = GpuProgramManager::instance();
- if (gpuMgr.isSyntaxSupported(mSelectedProfile))
- {
- mSelectedCgProfile = cgGetProfile(mSelectedProfile.c_str());
- // Check for errors
- checkForCgError("CgProgram::selectProfile",
- "Unable to find CG profile enum for program.", mCgContext);
- }
- else
- mSelectedProfile.clear();
- }
- //-----------------------------------------------------------------------
- void CgProgram::loadFromSource(void)
- {
- // Create Cg Program
- selectProfile();
- if (mSelectedCgProfile == CG_PROFILE_UNKNOWN)
- {
- LOGWRN("Attempted to load Cg program but no supported profile was found.");
- return;
- }
- // TODO PORT - This doesn't load includes
- // deal with includes
- String sourceToUse = mSource;
- //String sourceToUse = resolveCgIncludes(mSource, this, mFilename);
- mCgProgram = cgCreateProgram(mCgContext, CG_SOURCE, sourceToUse.c_str(),
- mSelectedCgProfile, mEntryPoint.c_str(), nullptr);
- // Check for errors
- checkForCgError("CgProgram::loadFromSource",
- "Unable to compile Cg program", mCgContext);
- // ignore any previous error
- if (mSelectedCgProfile != CG_PROFILE_UNKNOWN)
- {
- String sourceFromCg = cgGetProgramString(mCgProgram, CG_COMPILED_PROGRAM);
- // Create a low-level program, give it the same name as us
- mAssemblerProgram =
- HighLevelGpuProgramManager::instance().create(
- sourceFromCg, mEntryPoint, RenderSystem::instance().getShadingLanguageName(), mType, mProfile, nullptr);
- // Shader params need to be forwarded to low level implementation
- mAssemblerProgram->setAdjacencyInfoRequired(isAdjacencyInfoRequired());
- }
- cgDestroyProgram(mCgProgram);
- checkForCgError("CgProgram::unloadImpl", "Error while unloading Cg program", mCgContext);
- mCgProgram = nullptr;
- }
- void CgProgram::unload_internal(void)
- {
- HighLevelGpuProgram::destroy_internal();
- }
- GpuParamsPtr CgProgram::createParameters()
- {
- return getWrappedProgram()->createParameters();
- }
- GpuProgramPtr CgProgram::getBindingDelegate(void)
- {
- if(mAssemblerProgram)
- return getWrappedProgram()->getBindingDelegate();
- return nullptr;
- }
- HighLevelGpuProgramPtr CgProgram::getWrappedProgram() const
- {
- HighLevelGpuProgramPtr wrappedProgram = std::static_pointer_cast<HighLevelGpuProgram>(mAssemblerProgram);
- return wrappedProgram;
- }
- CgProgram::CgProgram(CGcontext context, const String& source, const String& entryPoint, const String& language,
- GpuProgramType gptype, GpuProgramProfile profile, const Vector<HGpuProgInclude>::type* includes, bool isAdjacencyInfoRequired)
- : HighLevelGpuProgram(source, entryPoint, language, gptype, profile, includes, isAdjacencyInfoRequired),
- mCgContext(context), mCgProgram(0),
- mSelectedCgProfile(CG_PROFILE_UNKNOWN)
- {
- }
- CgProgram::~CgProgram()
- {
- unload_internal();
- }
- bool CgProgram::isSupported(void) const
- {
- if (!isRequiredCapabilitiesSupported())
- return false;
- String selectedProfile = GpuProgramManager::instance().gpuProgProfileToRSSpecificProfile(mProfile);
- if (GpuProgramManager::instance().isSyntaxSupported(selectedProfile))
- return true;
- return false;
- }
- const String& CgProgram::getLanguage(void) const
- {
- static const String language = "cg";
- return language;
- }
- /************************************************************************/
- /* SERIALIZATION */
- /************************************************************************/
- RTTITypeBase* CgProgram::getRTTIStatic()
- {
- return CgProgramRTTI::instance();
- }
- RTTITypeBase* CgProgram::getRTTI() const
- {
- return CgProgram::getRTTIStatic();
- }
- }
|