Torque3D Documentation / _generateds / renderFormatChanger.cpp

renderFormatChanger.cpp

Engine/source/renderInstance/renderFormatChanger.cpp

More...

Public Variables

Public Functions

ConsoleDocClass(RenderFormatToken , "@brief Used <a href="/coding/file/cmdgram_8cpp/#cmdgram_8cpp_1a5bafda9519252aa2d0fd038153f77dca">to</a> change the <a href="/coding/file/editortool_8cpp/#editortool_8cpp_1a4cb041169a32ea3d4cacadbb955e06b4">render</a> target format when rendering in <a href="/coding/file/cmdscan_8cpp/#cmdscan_8cpp_1aeab71244afb687f16d8c4f5ee9d6ef0e">AL.\n\n</a>" "<a href="/coding/class/classrenderformattoken/">RenderFormatToken</a> is an implementation which changes the format of the " "back buffer and/or the depth <a href="/coding/file/cmdscan_8cpp/#cmdscan_8cpp_1aeab71244afb687f16d8c4f5ee9d6ef0e">buffer.\n\n</a>" "The <a href="/coding/class/classrenderpassstatebin/">RenderPassStateBin</a> manager changes the rendering state associated with " "this token. In stock Torque 3D, <a href="/coding/file/pointer_8h/#pointer_8h_1aeeddce917cf130d62c370b8f216026dd">a</a> single example exists in the " "way of AL_FormatToken(found in renderManager." TORQUE_SCRIPT_EXTENSION "). In that script file, all the " "<a href="/coding/file/editortool_8cpp/#editortool_8cpp_1a4cb041169a32ea3d4cacadbb955e06b4">render</a> managers are intialized, and <a href="/coding/file/pointer_8h/#pointer_8h_1aeeddce917cf130d62c370b8f216026dd">a</a> single <a href="/coding/class/classrenderformattoken/">RenderFormatToken</a> is used. This " "implementation basically exists <a href="/coding/file/cmdgram_8cpp/#cmdgram_8cpp_1a5bafda9519252aa2d0fd038153f77dca">to</a> ensure Advanced Lighting works with <a href="/coding/file/cmdscan_8cpp/#cmdscan_8cpp_1aeab71244afb687f16d8c4f5ee9d6ef0e">MSAA.\n\n</a>" "The actions <a href="/coding/file/cmdscan_8cpp/#cmdscan_8cpp_1a2732ab74fa0237854c2ba0f75f88a624">for</a> this token toggle the format of the back/depth buffers " "and it lets you specify <a href="/coding/file/pointer_8h/#pointer_8h_1aeeddce917cf130d62c370b8f216026dd">a</a> custom shader <a href="/coding/file/cmdgram_8cpp/#cmdgram_8cpp_1a5bafda9519252aa2d0fd038153f77dca">to</a> \"copy\" the data so it can " "be reformatted or altered. This is done through the variables copyEffect and " "resolveEffect (which are post processes just like fog or glow)\<a href="/coding/file/cmdscan_8cpp/#cmdscan_8cpp_1aeab71244afb687f16d8c4f5ee9d6ef0e">n\n</a>" "@<a href="/coding/file/cmdscan_8cpp/#cmdscan_8cpp_1aeab71244afb687f16d8c4f5ee9d6ef0e">tsexample\n</a>" "// This token, and the associated <a href="/coding/file/editortool_8cpp/#editortool_8cpp_1a4cb041169a32ea3d4cacadbb955e06b4">render</a> managers, ensure " "that driver MSAA does not get used <a href="/coding/file/cmdscan_8cpp/#cmdscan_8cpp_1a2732ab74fa0237854c2ba0f75f88a624">for</a> Advanced Lighting <a href="/coding/file/cmdscan_8cpp/#cmdscan_8cpp_1aeab71244afb687f16d8c4f5ee9d6ef0e">renders.\n</a>" "//The 'AL_FormatResolve' <a href="/coding/class/classposteffect/">PostEffect</a> copies the result <a href="/coding/file/cmdgram_8cpp/#cmdgram_8cpp_1a5bafda9519252aa2d0fd038153f77dca">to</a> the <a href="/coding/file/cmdscan_8cpp/#cmdscan_8cpp_1aeab71244afb687f16d8c4f5ee9d6ef0e">backbuffer.\n</a>" "<a href="/coding/file/tmm__on_8h/#tmm__on_8h_1a1ac41480eb2e4aadd52252ee550b630a">new</a> <a href="/coding/class/classrenderformattoken/">RenderFormatToken</a>(AL_FormatToken)\<a href="/coding/file/cmdscan_8cpp/#cmdscan_8cpp_1aeab71244afb687f16d8c4f5ee9d6ef0e">n</a>" "{\<a href="/coding/file/cmdscan_8cpp/#cmdscan_8cpp_1aeab71244afb687f16d8c4f5ee9d6ef0e">n</a>" " enabled=\"false\";\n\n" "   format = \"GFXFormatR8G8B8A8\";\n" "   depthFormat = \"GFXFormatD24S8\";\n" "   aaLevel = 0; // -1 = match <a href="/coding/file/cmdscan_8cpp/#cmdscan_8cpp_1aeab71244afb687f16d8c4f5ee9d6ef0e">backbuffer\n\n</a>" "   // The contents of the back buffer before this format token is <a href="/coding/file/cmdscan_8cpp/#cmdscan_8cpp_1aeab71244afb687f16d8c4f5ee9d6ef0e">executed\n</a>" "   // is provided in $<a href="/coding/file/cmdscan_8cpp/#cmdscan_8cpp_1aeab71244afb687f16d8c4f5ee9d6ef0e">inTex\n</a>" "   copyEffect = \"AL_FormatCopy\";\n\n" "   // The contents of the <a href="/coding/file/editortool_8cpp/#editortool_8cpp_1a4cb041169a32ea3d4cacadbb955e06b4">render</a> target created by this format token <a href="/coding/file/cmdscan_8cpp/#cmdscan_8cpp_1aeab71244afb687f16d8c4f5ee9d6ef0e">is\n</a>" "   // provided in $<a href="/coding/file/cmdscan_8cpp/#cmdscan_8cpp_1aeab71244afb687f16d8c4f5ee9d6ef0e">inTex\n</a>" "   resolveEffect = \"AL_FormatCopy\";\n" "};\<a href="/coding/file/cmdscan_8cpp/#cmdscan_8cpp_1aeab71244afb687f16d8c4f5ee9d6ef0e">n</a>" "@<a href="/coding/file/cmdscan_8cpp/#cmdscan_8cpp_1aeab71244afb687f16d8c4f5ee9d6ef0e">endtsexample\n\n</a>" "@see <a href="/coding/file/cmdscan_8cpp/#cmdscan_8cpp_1aeab71244afb687f16d8c4f5ee9d6ef0e">RenderPassToken\n\n</a>" "@see <a href="/coding/file/cmdscan_8cpp/#cmdscan_8cpp_1aeab71244afb687f16d8c4f5ee9d6ef0e">RenderPassStateBin\n</a>" "@see game/core/scripts/client/renderManager." TORQUE_SCRIPT_EXTENSION "\n" "@ingroup <a href="/coding/file/cmdscan_8cpp/#cmdscan_8cpp_1aeab71244afb687f16d8c4f5ee9d6ef0e">GFX\n</a>" )

Detailed Description

Public Variables

ColorI gCanvasClearColor 

Public Functions

ConsoleDocClass(RenderFormatToken , "@brief Used <a href="/coding/file/cmdgram_8cpp/#cmdgram_8cpp_1a5bafda9519252aa2d0fd038153f77dca">to</a> change the <a href="/coding/file/editortool_8cpp/#editortool_8cpp_1a4cb041169a32ea3d4cacadbb955e06b4">render</a> target format when rendering in <a href="/coding/file/cmdscan_8cpp/#cmdscan_8cpp_1aeab71244afb687f16d8c4f5ee9d6ef0e">AL.\n\n</a>" "<a href="/coding/class/classrenderformattoken/">RenderFormatToken</a> is an implementation which changes the format of the " "back buffer and/or the depth <a href="/coding/file/cmdscan_8cpp/#cmdscan_8cpp_1aeab71244afb687f16d8c4f5ee9d6ef0e">buffer.\n\n</a>" "The <a href="/coding/class/classrenderpassstatebin/">RenderPassStateBin</a> manager changes the rendering state associated with " "this token. In stock Torque 3D, <a href="/coding/file/pointer_8h/#pointer_8h_1aeeddce917cf130d62c370b8f216026dd">a</a> single example exists in the " "way of AL_FormatToken(found in renderManager." TORQUE_SCRIPT_EXTENSION "). In that script file, all the " "<a href="/coding/file/editortool_8cpp/#editortool_8cpp_1a4cb041169a32ea3d4cacadbb955e06b4">render</a> managers are intialized, and <a href="/coding/file/pointer_8h/#pointer_8h_1aeeddce917cf130d62c370b8f216026dd">a</a> single <a href="/coding/class/classrenderformattoken/">RenderFormatToken</a> is used. This " "implementation basically exists <a href="/coding/file/cmdgram_8cpp/#cmdgram_8cpp_1a5bafda9519252aa2d0fd038153f77dca">to</a> ensure Advanced Lighting works with <a href="/coding/file/cmdscan_8cpp/#cmdscan_8cpp_1aeab71244afb687f16d8c4f5ee9d6ef0e">MSAA.\n\n</a>" "The actions <a href="/coding/file/cmdscan_8cpp/#cmdscan_8cpp_1a2732ab74fa0237854c2ba0f75f88a624">for</a> this token toggle the format of the back/depth buffers " "and it lets you specify <a href="/coding/file/pointer_8h/#pointer_8h_1aeeddce917cf130d62c370b8f216026dd">a</a> custom shader <a href="/coding/file/cmdgram_8cpp/#cmdgram_8cpp_1a5bafda9519252aa2d0fd038153f77dca">to</a> \"copy\" the data so it can " "be reformatted or altered. This is done through the variables copyEffect and " "resolveEffect (which are post processes just like fog or glow)\<a href="/coding/file/cmdscan_8cpp/#cmdscan_8cpp_1aeab71244afb687f16d8c4f5ee9d6ef0e">n\n</a>" "@<a href="/coding/file/cmdscan_8cpp/#cmdscan_8cpp_1aeab71244afb687f16d8c4f5ee9d6ef0e">tsexample\n</a>" "// This token, and the associated <a href="/coding/file/editortool_8cpp/#editortool_8cpp_1a4cb041169a32ea3d4cacadbb955e06b4">render</a> managers, ensure " "that driver MSAA does not get used <a href="/coding/file/cmdscan_8cpp/#cmdscan_8cpp_1a2732ab74fa0237854c2ba0f75f88a624">for</a> Advanced Lighting <a href="/coding/file/cmdscan_8cpp/#cmdscan_8cpp_1aeab71244afb687f16d8c4f5ee9d6ef0e">renders.\n</a>" "//The 'AL_FormatResolve' <a href="/coding/class/classposteffect/">PostEffect</a> copies the result <a href="/coding/file/cmdgram_8cpp/#cmdgram_8cpp_1a5bafda9519252aa2d0fd038153f77dca">to</a> the <a href="/coding/file/cmdscan_8cpp/#cmdscan_8cpp_1aeab71244afb687f16d8c4f5ee9d6ef0e">backbuffer.\n</a>" "<a href="/coding/file/tmm__on_8h/#tmm__on_8h_1a1ac41480eb2e4aadd52252ee550b630a">new</a> <a href="/coding/class/classrenderformattoken/">RenderFormatToken</a>(AL_FormatToken)\<a href="/coding/file/cmdscan_8cpp/#cmdscan_8cpp_1aeab71244afb687f16d8c4f5ee9d6ef0e">n</a>" "{\<a href="/coding/file/cmdscan_8cpp/#cmdscan_8cpp_1aeab71244afb687f16d8c4f5ee9d6ef0e">n</a>" " enabled=\"false\";\n\n" "   format = \"GFXFormatR8G8B8A8\";\n" "   depthFormat = \"GFXFormatD24S8\";\n" "   aaLevel = 0; // -1 = match <a href="/coding/file/cmdscan_8cpp/#cmdscan_8cpp_1aeab71244afb687f16d8c4f5ee9d6ef0e">backbuffer\n\n</a>" "   // The contents of the back buffer before this format token is <a href="/coding/file/cmdscan_8cpp/#cmdscan_8cpp_1aeab71244afb687f16d8c4f5ee9d6ef0e">executed\n</a>" "   // is provided in $<a href="/coding/file/cmdscan_8cpp/#cmdscan_8cpp_1aeab71244afb687f16d8c4f5ee9d6ef0e">inTex\n</a>" "   copyEffect = \"AL_FormatCopy\";\n\n" "   // The contents of the <a href="/coding/file/editortool_8cpp/#editortool_8cpp_1a4cb041169a32ea3d4cacadbb955e06b4">render</a> target created by this format token <a href="/coding/file/cmdscan_8cpp/#cmdscan_8cpp_1aeab71244afb687f16d8c4f5ee9d6ef0e">is\n</a>" "   // provided in $<a href="/coding/file/cmdscan_8cpp/#cmdscan_8cpp_1aeab71244afb687f16d8c4f5ee9d6ef0e">inTex\n</a>" "   resolveEffect = \"AL_FormatCopy\";\n" "};\<a href="/coding/file/cmdscan_8cpp/#cmdscan_8cpp_1aeab71244afb687f16d8c4f5ee9d6ef0e">n</a>" "@<a href="/coding/file/cmdscan_8cpp/#cmdscan_8cpp_1aeab71244afb687f16d8c4f5ee9d6ef0e">endtsexample\n\n</a>" "@see <a href="/coding/file/cmdscan_8cpp/#cmdscan_8cpp_1aeab71244afb687f16d8c4f5ee9d6ef0e">RenderPassToken\n\n</a>" "@see <a href="/coding/file/cmdscan_8cpp/#cmdscan_8cpp_1aeab71244afb687f16d8c4f5ee9d6ef0e">RenderPassStateBin\n</a>" "@see game/core/scripts/client/renderManager." TORQUE_SCRIPT_EXTENSION "\n" "@ingroup <a href="/coding/file/cmdscan_8cpp/#cmdscan_8cpp_1aeab71244afb687f16d8c4f5ee9d6ef0e">GFX\n</a>" )

IMPLEMENT_CONOBJECT(RenderFormatToken )

  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 "renderInstance/renderFormatChanger.h"
 26
 27#include "console/consoleTypes.h"
 28#include "gfx/gfxStringEnumTranslate.h"
 29#include "gfx/gfxTextureManager.h"
 30#include "gfx/gfxDebugEvent.h"
 31#include "postFx/postEffect.h"
 32#include "postFx/postEffectManager.h"
 33
 34extern ColorI gCanvasClearColor;
 35
 36IMPLEMENT_CONOBJECT(RenderFormatToken);
 37
 38ConsoleDocClass( RenderFormatToken,
 39   "@brief Used to change the render target format when rendering in AL.\n\n"
 40
 41   "RenderFormatToken is an implementation which changes the format of the "
 42   "back buffer and/or the depth buffer.\n\n"
 43   
 44   "The RenderPassStateBin manager changes the rendering state associated with "
 45   "this token. In stock Torque 3D, a single example exists in the "
 46   "way of AL_FormatToken (found in renderManager." TORQUE_SCRIPT_EXTENSION "). In that script file, all the "
 47   "render managers are intialized, and a single RenderFormatToken is used. This "
 48   "implementation basically exists to ensure Advanced Lighting works with MSAA.\n\n"
 49
 50   "The actions for this token toggle the format of the back/depth buffers "
 51   "and it lets you specify a custom shader to \"copy\" the data so it can "
 52   "be reformatted or altered. This is done through the variables copyEffect and "
 53   "resolveEffect (which are post processes just like fog or glow)\n\n"
 54
 55   "@tsexample\n"
 56   "// This token, and the associated render managers, ensure "
 57   "that driver MSAA does not get used for Advanced Lighting renders.\n"
 58   "// The 'AL_FormatResolve' PostEffect copies the result to the backbuffer.\n"
 59   "new RenderFormatToken(AL_FormatToken)\n"
 60   "{\n"
 61   "   enabled = \"false\";\n\n"
 62   "   format = \"GFXFormatR8G8B8A8\";\n"
 63   "   depthFormat = \"GFXFormatD24S8\";\n"
 64   "   aaLevel = 0; // -1 = match backbuffer\n\n"
 65   "   // The contents of the back buffer before this format token is executed\n"
 66   "   // is provided in $inTex\n"
 67   "   copyEffect = \"AL_FormatCopy\";\n\n"
 68   "   // The contents of the render target created by this format token is\n"
 69   "   // provided in $inTex\n"
 70   "   resolveEffect = \"AL_FormatCopy\";\n"
 71   "};\n"
 72   "@endtsexample\n\n"
 73
 74   "@see RenderPassToken\n\n"
 75   "@see RenderPassStateBin\n"
 76   "@see game/core/scripts/client/renderManager." TORQUE_SCRIPT_EXTENSION "\n"
 77
 78   "@ingroup GFX\n"
 79);
 80
 81RenderFormatToken::RenderFormatToken() 
 82   :  Parent(), 
 83      mFCState(FTSDisabled), 
 84      mColorFormat(GFXFormat_COUNT), 
 85      mDepthFormat(GFXFormat_COUNT),
 86      mTargetUpdatePending(true),
 87      mTargetChainIdx(0),
 88      mTargetSize(Point2I::Zero),
 89      mTargetAALevel(GFXTextureManager::AA_MATCH_BACKBUFFER),
 90      mCopyPostEffect(NULL),
 91      mResolvePostEffect(NULL)
 92{
 93   GFXDevice::getDeviceEventSignal().notify(this, &RenderFormatToken::_handleGFXEvent);
 94   GFXTextureManager::addEventDelegate(this, &RenderFormatToken::_onTextureEvent);
 95}
 96
 97RenderFormatToken::~RenderFormatToken()
 98{
 99   GFXTextureManager::removeEventDelegate(this, &RenderFormatToken::_onTextureEvent);
100   GFXDevice::getDeviceEventSignal().remove(this, &RenderFormatToken::_handleGFXEvent);
101
102   _teardownTargets();
103}
104
105void RenderFormatToken::process(SceneRenderState *state, RenderPassStateBin *callingBin)
106{
107   switch(mFCState)
108   {
109   case FTSWaiting:
110      {
111         GFXDEBUGEVENT_SCOPE_EX(RFT_Waiting, ColorI::BLUE, avar("[%s Activate] (%s)", getName(), GFXStringTextureFormat[mColorFormat]));
112         mFCState = FTSActive;
113
114         mTarget.setViewport( GFX->getViewport() );
115
116         // Update targets
117         _updateTargets();
118
119         // If we have a copy PostEffect then get the active backbuffer copy 
120         // now before we swap the render targets.
121         GFXTexHandle curBackBuffer;
122         if(mCopyPostEffect.isValid())
123            curBackBuffer = PFXMGR->getBackBufferTex();
124
125         // Push target
126         GFX->pushActiveRenderTarget();
127         GFX->setActiveRenderTarget(mTargetChain[mTargetChainIdx]);
128
129         // Set viewport
130         GFX->setViewport( mTarget.getViewport() );
131
132         // Clear
133         GFX->clear(GFXClearTarget | GFXClearZBuffer | GFXClearStencil, gCanvasClearColor, 1.0f, 0);
134
135         // Set active z target on render pass
136         if(mTargetDepthStencilTexture[mTargetChainIdx].isValid())
137         {
138            if(callingBin->getRenderPass()->getDepthTargetTexture() != GFXTextureTarget::sDefaultDepthStencil)
139               mStoredPassZTarget = callingBin->getRenderPass()->getDepthTargetTexture();
140            else
141               mStoredPassZTarget = NULL;
142
143            callingBin->getRenderPass()->setDepthTargetTexture(mTargetDepthStencilTexture[mTargetChainIdx]);
144         }
145
146         // Run the PostEffect which copies data into the new target.
147         if ( mCopyPostEffect.isValid() )
148            mCopyPostEffect->process( state, curBackBuffer, &mTarget.getViewport() );
149      }
150      break;
151
152   case FTSActive:
153      {
154         GFXDEBUGEVENT_SCOPE_EX(RFT_Active, ColorI::BLUE, avar("[%s Deactivate]", getName()));
155         mFCState = FTSComplete;
156
157         // Pop target
158         AssertFatal(GFX->getActiveRenderTarget() == mTargetChain[mTargetChainIdx], "Render target stack went wrong somewhere");
159         mTargetChain[mTargetChainIdx]->resolve();
160         GFX->popActiveRenderTarget();
161         mTarget.setTexture( mTargetColorTexture[mTargetChainIdx] ); 
162         
163         // This is the GFX viewport when we were first processed.
164         GFX->setViewport( mTarget.getViewport() );
165
166         // Restore active z-target
167         if(mTargetDepthStencilTexture[mTargetChainIdx].isValid())
168         {
169            callingBin->getRenderPass()->setDepthTargetTexture(mStoredPassZTarget.getPointer());
170            mStoredPassZTarget = NULL;
171         }
172
173         // Run the PostEffect which copies data to the backbuffer
174         if(mResolvePostEffect.isValid())
175         {
176            // Need to create a texhandle here, since inOutTex gets assigned during process()
177            GFXTexHandle inOutTex = mTargetColorTexture[mTargetChainIdx];
178            mResolvePostEffect->process( state, inOutTex, &mTarget.getViewport() );
179         }
180      }
181      break;
182
183   case FTSComplete:
184      AssertFatal(false, "process() called on a RenderFormatToken which was already complete.");
185      // fall through
186   case FTSDisabled:
187      break;
188   }
189}
190
191void RenderFormatToken::reset()
192{
193   AssertFatal(mFCState != FTSActive, "RenderFormatToken still active during reset()!");
194   if(mFCState != FTSDisabled)
195      mFCState = FTSWaiting;
196}
197
198void RenderFormatToken::_updateTargets()
199{
200   if ( GFX->getActiveRenderTarget() == NULL )
201      return;
202
203   const Point2I &rtSize = GFX->getActiveRenderTarget()->getSize();
204
205   if ( rtSize.x <= mTargetSize.x && 
206        rtSize.y <= mTargetSize.y && 
207        !mTargetUpdatePending )
208      return;   
209
210   mTargetSize = rtSize;
211   mTargetUpdatePending = false;   
212   mTargetChainIdx = 0;
213
214   for( U32 i = 0; i < TargetChainLength; i++ )
215   {
216      if( !mTargetChain[i] )
217         mTargetChain[i] = GFX->allocRenderToTextureTarget();
218
219      // Update color target
220      if(mColorFormat != GFXFormat_COUNT)
221      {
222         // try reuse of old color texture
223         if( !mTargetColorTexture[i] || mTargetColorTexture[i].getFormat() != mColorFormat 
224            || mTargetColorTexture[i].getWidthHeight() != rtSize)
225         {
226            mTargetColorTexture[i].set( rtSize.x, rtSize.y, mColorFormat, 
227                  &GFXRenderTargetSRGBProfile, avar( "%s() - (line %d)", __FUNCTION__, __LINE__ ),
228                  1, mTargetAALevel );
229            mTargetChain[i]->attachTexture( GFXTextureTarget::Color0, mTargetColorTexture[i] );
230         }
231      }      
232
233      // Update depth target
234      if(mDepthFormat != GFXFormat_COUNT)
235      {
236         // try reuse of old depth texture
237         if( !mTargetDepthStencilTexture[i] || mTargetDepthStencilTexture[i].getFormat() != mColorFormat 
238            || mTargetDepthStencilTexture[i].getWidthHeight() != rtSize)
239         {
240            mTargetDepthStencilTexture[i].set( rtSize.x, rtSize.y, mDepthFormat, 
241                  &GFXZTargetProfile, avar( "%s() - (line %d)", __FUNCTION__, __LINE__ ),
242                  1, mTargetAALevel );
243            mTargetChain[i]->attachTexture( GFXTextureTarget::DepthStencil, mTargetDepthStencilTexture[i] );
244         }
245      }     
246   }
247
248   //set the texture for now as the first color target texture
249   mTarget.setTexture(mTargetColorTexture[0]);
250}
251
252void RenderFormatToken::_teardownTargets()
253{
254   mTarget.release();
255
256   for(S32 i = 0; i < TargetChainLength; i++)
257   {
258      mTargetColorTexture[i] = NULL;
259      mTargetDepthStencilTexture[i] = NULL;
260      mTargetChain[i] = NULL;
261   }
262}
263
264bool RenderFormatToken::_setFmt( void *object, const char *index, const char *data )
265{
266   // Flag update pending
267   reinterpret_cast<RenderFormatToken *>( object )->mTargetUpdatePending = true;
268
269   // Allow console system to assign value
270   return true;
271}
272
273const char* RenderFormatToken::_getCopyPostEffect( void* object, const char* data )
274{
275   RenderFormatToken* token = reinterpret_cast< RenderFormatToken* >( object );
276   if( token->mCopyPostEffect.isValid() )
277      return token->mCopyPostEffect->getIdString();
278   return "0";
279}
280
281const char* RenderFormatToken::_getResolvePostEffect( void* object, const char* data )
282{
283   RenderFormatToken* token = reinterpret_cast< RenderFormatToken* >( object );
284   if( token->mResolvePostEffect.isValid() )
285      return token->mResolvePostEffect->getIdString();
286   return "0";
287}
288
289bool RenderFormatToken::_setCopyPostEffect( void* object, const char* index, const char* data )
290{
291   RenderFormatToken* token = reinterpret_cast< RenderFormatToken* >( object );
292   PostEffect* effect;
293   Sim::findObject( data, effect );
294   token->mCopyPostEffect = effect;
295   return false;
296}
297
298bool RenderFormatToken::_setResolvePostEffect( void* object, const char* index, const char* data )
299{
300   RenderFormatToken* token = reinterpret_cast< RenderFormatToken* >( object );
301   PostEffect* effect;
302   Sim::findObject( data, effect );
303   token->mResolvePostEffect = effect;
304   return false;
305}
306
307void RenderFormatToken::enable( bool enabled /*= true*/ )
308{
309   AssertFatal(mFCState != FTSActive, "RenderFormatToken is active, cannot change state now!");
310
311   if(enabled)
312      mFCState = FTSWaiting;
313   else
314      mFCState = FTSDisabled;
315}
316
317bool RenderFormatToken::isEnabled() const
318{
319   return (mFCState != FTSDisabled);
320}
321
322void RenderFormatToken::initPersistFields()
323{
324   addProtectedField("format", TypeGFXFormat, Offset(mColorFormat, RenderFormatToken), &_setFmt, &defaultProtectedGetFn, 
325      "Sets the color buffer format for this token.");
326
327   addProtectedField("depthFormat", TypeGFXFormat, Offset(mDepthFormat, RenderFormatToken), &_setFmt, &defaultProtectedGetFn, 
328      "Sets the depth/stencil buffer format for this token.");
329
330   addProtectedField("copyEffect", TYPEID<PostEffect>(), Offset(mCopyPostEffect, RenderFormatToken),
331      &_setCopyPostEffect, &_getCopyPostEffect,
332      "This PostEffect will be run when the render target is changed to the format specified "
333      "by this token. It is used to copy/format data into the token rendertarget");
334
335   addProtectedField("resolveEffect", TYPEID<PostEffect>(), Offset(mResolvePostEffect, RenderFormatToken),
336      &_setResolvePostEffect, &_getResolvePostEffect,
337      "This PostEffect will be run when the render target is changed back to the format "
338      "active prior to this token. It is used to copy/format data from the token rendertarget to the backbuffer.");
339
340   addField("aaLevel", TypeS32, Offset(mTargetAALevel, RenderFormatToken), 
341      "Anti-ailiasing level for the this token. 0 disables, -1 uses adapter default.");
342
343   Parent::initPersistFields();
344}
345
346
347bool RenderFormatToken::_handleGFXEvent(GFXDevice::GFXDeviceEventType event_)
348{
349   if ( event_ == GFXDevice::deStartOfFrame )
350   {
351      mTargetChainIdx++;
352      if ( mTargetChainIdx >= TargetChainLength )
353         mTargetChainIdx = 0;
354   }
355
356   return true;
357}
358
359void RenderFormatToken::_onTextureEvent( GFXTexCallbackCode code )
360{
361   if(code == GFXZombify)
362   {
363      _teardownTargets();
364      mTargetUpdatePending = true;
365   }
366}
367
368bool RenderFormatToken::onAdd()
369{
370   if(!Parent::onAdd())
371      return false;
372
373   mTarget.registerWithName( getName() );
374   mTarget.setSamplerState( GFXSamplerStateDesc::getClampPoint() );
375
376   return true;
377}
378
379void RenderFormatToken::onRemove()
380{
381   mTarget.unregister();
382   mTarget.release();
383
384   Parent::onRemove();
385}
386