gfxFence.cpp 4.4 KB

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