matTextureTarget.cpp
Engine/source/materials/matTextureTarget.cpp
Public Functions
DefineEngineFunction(getNamedTargetList , String , () , "" )
Detailed Description
Public Functions
DefineEngineFunction(getNamedTargetList , String , () , "" )
1 2//----------------------------------------------------------------------------- 3// Copyright (c) 2012 GarageGames, LLC 4// 5// Permission is hereby granted, free of charge, to any person obtaining a copy 6// of this software and associated documentation files (the "Software"), to 7// deal in the Software without restriction, including without limitation the 8// rights to use, copy, modify, merge, publish, distribute, sublicense, and/or 9// sell copies of the Software, and to permit persons to whom the Software is 10// furnished to do so, subject to the following conditions: 11// 12// The above copyright notice and this permission notice shall be included in 13// all copies or substantial portions of the Software. 14// 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 20// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS 21// IN THE SOFTWARE. 22//----------------------------------------------------------------------------- 23 24#include "platform/platform.h" 25#include "materials/matTextureTarget.h" 26 27#include "console/console.h" 28#include "platform/profiler.h" 29#include "shaderGen/conditionerFeature.h" 30#include "gfx/gfxTextureObject.h" 31#include "gfx/gfxStructs.h" 32 33 34NamedTexTarget::TargetMap NamedTexTarget::smTargets; 35 36 37bool NamedTexTarget::registerWithName( const String &name ) 38{ 39 if ( mIsRegistered ) 40 { 41 // If we're already registered with 42 // this name then do nothing. 43 if ( mName == name ) 44 return true; 45 46 // Else unregister ourselves first. 47 unregister(); 48 } 49 50 // Make sure the target name isn't empty or already taken. 51 if ( name.isEmpty()) 52 { 53 Con::errorf("NamedTexTarget::registerWithName( const String &name ) No name given!"); 54 return false; 55 } 56 if (smTargets.contains( name ) ) 57 { 58 Con::errorf("NamedTexTarget::registerWithName( %s ) Already used!", name.c_str()); 59 return false; 60 } 61 mName = name; 62 mIsRegistered = true; 63 smTargets.insert( mName, this ); 64 return true; 65} 66 67void NamedTexTarget::unregister() 68{ 69 if ( !mIsRegistered ) 70 return; 71 72 TargetMap::Iterator iter = smTargets.find( mName ); 73 74 AssertFatal( iter != smTargets.end() && 75 iter->value == this, 76 "NamedTexTarget::unregister - Bad registration!" ); 77 78 mIsRegistered = false; 79 mName = String::EmptyString; 80 smTargets.erase( iter ); 81} 82 83NamedTexTarget* NamedTexTarget::find( const String &name ) 84{ 85 PROFILE_SCOPE( NamedTexTarget_find ); 86 87 TargetMap::Iterator iter = smTargets.find( name ); 88 if ( iter != smTargets.end() ) 89 return iter->value; 90 else 91 return NULL; 92} 93 94NamedTexTarget::NamedTexTarget() 95 : mIsRegistered( false ), 96 mViewport( RectI::One ), 97 mConditioner( NULL ) 98{ 99} 100 101NamedTexTarget::~NamedTexTarget() 102{ 103 unregister(); 104} 105 106void NamedTexTarget::setTexture( U32 index, GFXTextureObject *tex ) 107{ 108 AssertFatal( index < 4, "NamedTexTarget::setTexture - Got invalid index!" ); 109 mTex[index] = tex; 110} 111 112void NamedTexTarget::release() 113{ 114 mTex[0] = NULL; 115 mTex[1] = NULL; 116 mTex[2] = NULL; 117 mTex[3] = NULL; 118} 119 120void NamedTexTarget::getShaderMacros( Vector<GFXShaderMacro> *outMacros ) 121{ 122 ConditionerFeature *cond = getConditioner(); 123 if ( !cond ) 124 return; 125 126 // TODO: No check for duplicates is 127 // going on here which might be a problem? 128 129 String targetName = String::ToLower( mName ); 130 131 // Add both the condition and uncondition macros. 132 const String &condMethod = cond->getShaderMethodName( ConditionerFeature::ConditionMethod ); 133 if ( condMethod.isNotEmpty() ) 134 { 135 GFXShaderMacro macro; 136 macro.name = targetName + "Condition"; 137 macro.value = condMethod; 138 outMacros->push_back( macro ); 139 } 140 141 const String &uncondMethod = cond->getShaderMethodName( ConditionerFeature::UnconditionMethod ); 142 if ( uncondMethod.isNotEmpty() ) 143 { 144 GFXShaderMacro macro; 145 macro.name = targetName + "Uncondition"; 146 macro.value = uncondMethod; 147 outMacros->push_back( macro ); 148 } 149} 150 151DefineEngineFunction(getNamedTargetList, String, (), , "") 152{ 153 String targetList = ""; 154 NamedTexTarget::TargetMap targets = NamedTexTarget::getTargetMap(); 155 for (NamedTexTarget::TargetMap::Iterator iter = targets.begin(); iter != targets.end(); iter++) 156 { 157 targetList += iter->value->getName() + " "; 158 } 159 160 return targetList; 161} 162