matTextureTarget.h 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  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. #ifndef _MATTEXTURETARGET_H_
  23. #define _MATTEXTURETARGET_H_
  24. #ifndef _TDICTIONARY_H_
  25. #include "core/util/tDictionary.h"
  26. #endif
  27. #ifndef _REFBASE_H_
  28. #include "core/util/refBase.h"
  29. #endif
  30. #ifndef _GFXTEXTUREHANDLE_H_
  31. #include "gfx/gfxTextureHandle.h"
  32. #endif
  33. #ifndef _MRECT_H_
  34. #include "math/mRect.h"
  35. #endif
  36. #ifndef _GFXSTATEBLOCK_H_
  37. #include "gfx/gfxStateBlock.h"
  38. #endif
  39. #ifndef _UTIL_DELEGATE_H_
  40. #include "core/util/delegate.h"
  41. #endif
  42. struct GFXShaderMacro;
  43. class ConditionerFeature;
  44. ///
  45. class NamedTexTarget : public WeakRefBase
  46. {
  47. public:
  48. ///
  49. static NamedTexTarget* find( const String &name );
  50. ///
  51. NamedTexTarget();
  52. ///
  53. virtual ~NamedTexTarget();
  54. ///
  55. bool registerWithName( const String &name );
  56. ///
  57. void unregister();
  58. ///
  59. bool isRegistered() const { return mIsRegistered; }
  60. /// Returns the target name we were registered with.
  61. const String& getName() const { return mName; }
  62. // Register the passed texture with our name, unregistering "anyone"
  63. // priorly registered with that name.
  64. // Pass NULL to only unregister.
  65. void setTexture( GFXTextureObject *tex ) { setTexture( 0, tex ); }
  66. ///
  67. void setTexture( U32 index, GFXTextureObject *tex );
  68. ///
  69. GFXTextureObject* getTexture( U32 index = 0 ) const;
  70. /// The delegate used to override the getTexture method.
  71. /// @see getTexture
  72. typedef Delegate<GFXTextureObject*(U32)> TexDelegate;
  73. ///
  74. /// @see getTexture
  75. TexDelegate& getTextureDelegate() { return mTexDelegate; }
  76. const TexDelegate& getTextureDelegate() const { return mTexDelegate; }
  77. /// Release all the textures.
  78. void release();
  79. // NOTE:
  80. //
  81. // The following members are here to support the existing conditioner
  82. // and target system used for the deferred gbuffer and lighting.
  83. //
  84. // We will refactor that system as part of material2 removing the concept
  85. // of conditioners from C++ (moving them to HLSL/GLSL) and make the shader
  86. // features which use the texture responsible for setting the correct sampler
  87. // states.
  88. //
  89. // It could be that at this time this class could completely
  90. // be removed and instead these textures can be registered
  91. // with the TEXMGR and looked up there exclusively.
  92. //
  93. void setViewport( const RectI &viewport ) { mViewport = viewport; }
  94. const RectI& getViewport() const { return mViewport; }
  95. void setSamplerState( const GFXSamplerStateDesc &desc ) { mSamplerDesc = desc; }
  96. void setupSamplerState( GFXSamplerStateDesc *desc ) const { *desc = mSamplerDesc; }
  97. void setConditioner( ConditionerFeature *cond ) { mConditioner = cond; }
  98. ConditionerFeature* getConditioner() const { return mConditioner; }
  99. void getShaderMacros( Vector<GFXShaderMacro> *outMacros );
  100. typedef Map<String, NamedTexTarget*> TargetMap;
  101. static TargetMap getTargetMap() {
  102. return smTargets;
  103. }
  104. protected:
  105. ///
  106. static TargetMap smTargets;
  107. ///
  108. bool mIsRegistered;
  109. /// The target name we were registered with.
  110. String mName;
  111. /// The held textures.
  112. GFXTexHandle mTex[4];
  113. ///
  114. TexDelegate mTexDelegate;
  115. ///
  116. RectI mViewport;
  117. ///
  118. GFXSamplerStateDesc mSamplerDesc;
  119. ///
  120. ConditionerFeature *mConditioner;
  121. };
  122. inline GFXTextureObject* NamedTexTarget::getTexture( U32 index ) const
  123. {
  124. AssertFatal( index < 4, "NamedTexTarget::getTexture - Got invalid index!" );
  125. if ( mTexDelegate.empty() )
  126. return mTex[index];
  127. return mTexDelegate( index );
  128. }
  129. /// A weak reference to a texture target.
  130. typedef WeakRefPtr<NamedTexTarget> NamedTexTargetRef;
  131. #endif // _MATTEXTURETARGET_H_