matTextureTarget.cpp 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  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() || smTargets.contains( name ) )
  43. return false;
  44. mName = name;
  45. mIsRegistered = true;
  46. smTargets.insert( mName, this );
  47. return true;
  48. }
  49. void NamedTexTarget::unregister()
  50. {
  51. if ( !mIsRegistered )
  52. return;
  53. TargetMap::Iterator iter = smTargets.find( mName );
  54. AssertFatal( iter != smTargets.end() &&
  55. iter->value == this,
  56. "NamedTexTarget::unregister - Bad registration!" );
  57. mIsRegistered = false;
  58. mName = String::EmptyString;
  59. smTargets.erase( iter );
  60. }
  61. NamedTexTarget* NamedTexTarget::find( const String &name )
  62. {
  63. PROFILE_SCOPE( NamedTexTarget_find );
  64. TargetMap::Iterator iter = smTargets.find( name );
  65. if ( iter != smTargets.end() )
  66. return iter->value;
  67. else
  68. return NULL;
  69. }
  70. NamedTexTarget::NamedTexTarget()
  71. : mViewport( RectI::One ),
  72. mIsRegistered( false ),
  73. mConditioner( NULL )
  74. {
  75. }
  76. NamedTexTarget::~NamedTexTarget()
  77. {
  78. unregister();
  79. }
  80. void NamedTexTarget::setTexture( U32 index, GFXTextureObject *tex )
  81. {
  82. AssertFatal( index < 4, "NamedTexTarget::setTexture - Got invalid index!" );
  83. mTex[index] = tex;
  84. }
  85. void NamedTexTarget::release()
  86. {
  87. mTex[0] = NULL;
  88. mTex[1] = NULL;
  89. mTex[2] = NULL;
  90. mTex[3] = NULL;
  91. }
  92. void NamedTexTarget::getShaderMacros( Vector<GFXShaderMacro> *outMacros )
  93. {
  94. ConditionerFeature *cond = getConditioner();
  95. if ( !cond )
  96. return;
  97. // TODO: No check for duplicates is
  98. // going on here which might be a problem?
  99. String targetName = String::ToLower( mName );
  100. // Add both the condition and uncondition macros.
  101. const String &condMethod = cond->getShaderMethodName( ConditionerFeature::ConditionMethod );
  102. if ( condMethod.isNotEmpty() )
  103. {
  104. GFXShaderMacro macro;
  105. macro.name = targetName + "Condition";
  106. macro.value = condMethod;
  107. outMacros->push_back( macro );
  108. }
  109. const String &uncondMethod = cond->getShaderMethodName( ConditionerFeature::UnconditionMethod );
  110. if ( uncondMethod.isNotEmpty() )
  111. {
  112. GFXShaderMacro macro;
  113. macro.name = targetName + "Uncondition";
  114. macro.value = uncondMethod;
  115. outMacros->push_back( macro );
  116. }
  117. }