gfxFence.cpp
Engine/source/gfx/gfxFence.cpp
Detailed Description
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 "gfx/gfxFence.h" 26 27#include "gfx/primBuilder.h" 28#include "gfx/gfxTextureManager.h" 29 30 31GFXGeneralFence::~GFXGeneralFence() 32{ 33 // Release the ref pointers 34 mRenderTarget = NULL; 35 mRTTexHandle = NULL; 36 37 // Unregister texture callback 38 GFXTextureManager::removeEventDelegate( this, &GFXGeneralFence::_onTextureEvent ); 39} 40 41//------------------------------------------------------------------------------ 42 43void GFXGeneralFence::_init() 44{ 45 // Register texture callback to re-create resources 46 if ( !mInitialized ) 47 GFXTextureManager::addEventDelegate( this, &GFXGeneralFence::_onTextureEvent ); 48 49 // Set this to true for error checking 50 mInitialized = true; 51 52 // Allocate resources 53 mInitialized &= mRTTexHandle.set( 2, 2, GFXFormatR8G8B8X8, &GFXRenderTargetProfile, avar("%s() - mInitialized (line %d)", __FUNCTION__, __LINE__) ); 54 mRenderTarget = GFX->allocRenderToTextureTarget(); 55 mInitialized &= ( mRenderTarget != NULL ); 56 57 GFXStateBlockDesc d; 58 mRenderSB = GFX->createStateBlock(d); 59} 60 61//------------------------------------------------------------------------------ 62 63void GFXGeneralFence::issue() 64{ 65 PROFILE_SCOPE(GFXGeneralFence_Issue); 66 67 // Resource creation will be done on first call to issue() 68 if( !mInitialized ) 69 _init(); 70 71 // If we can't init, return. 72 if(!mInitialized) 73 return; 74 75 AssertFatal( mInitialized, "Error occured during GFXGeneralFence::_init, sorry I can't be more specific, break and debug." ); 76 77 RectI viewport = GFX->getViewport(); 78 79 GFX->pushActiveRenderTarget(); 80 mRenderTarget->attachTexture( GFXTextureTarget::Color0, mRTTexHandle ); 81 GFX->setActiveRenderTarget( mRenderTarget ); 82 83 // Set-up states 84 GFX->setStateBlock(mRenderSB); 85 GFX->pushWorldMatrix(); 86 GFX->setWorldMatrix( MatrixF::Identity ); 87 88 // CodeReview: We can re-do this with a static vertex buffer at some point. [5/9/2007 Pat] 89 PrimBuild::begin( GFXTriangleList, 3 ); 90 PrimBuild::vertex2f( 0.f, 0.f ); 91 PrimBuild::vertex2f( 0.f, 1.f ); 92 PrimBuild::vertex2f( 1.f, 0.f ); 93 PrimBuild::end(); 94 95 GFX->popWorldMatrix(); 96 GFX->popActiveRenderTarget(); 97 98 GFX->setViewport(viewport); 99} 100 101//------------------------------------------------------------------------------ 102 103void GFXGeneralFence::block() 104{ 105 PROFILE_SCOPE(GFXGeneralFence_block); 106 if( !mInitialized ) 107 return; 108 109 // We have to deal with the case where the lock fails (usually due to 110 // a device reset). 111 if(mRTTexHandle.lock()) 112 mRTTexHandle.unlock(); 113} 114 115void GFXGeneralFence::_onTextureEvent( GFXTexCallbackCode code ) 116{ 117 switch( code ) 118 { 119 case GFXZombify: 120 mRTTexHandle = NULL; 121 break; 122 123 case GFXResurrect: 124 mRTTexHandle.set( 2, 2, GFXFormatR8G8B8X8, &GFXRenderTargetProfile, avar("%s() - GFXGeneralFence->mRTTexHandle (line %d)", __FUNCTION__, __LINE__) ); 125 break; 126 } 127} 128 129void GFXGeneralFence::zombify() 130{ 131 mRTTexHandle = NULL; 132} 133 134void GFXGeneralFence::resurrect() 135{ 136 mRTTexHandle.set( 2, 2, GFXFormatR8G8B8X8, &GFXRenderTargetProfile, avar("%s() - mRTTexHandle (line %d)", __FUNCTION__, __LINE__) ); 137} 138 139const String GFXGeneralFence::describeSelf() const 140{ 141 // We've got nothing 142 return String(); 143} 144