matTextureTarget.cpp 4.6 KB

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