gfxGLWindowTarget.cpp 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  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 "windowManager/platformWindow.h"
  23. #include "gfx/gl/gfxGLDevice.h"
  24. #include "gfx/gl/gfxGLWindowTarget.h"
  25. #include "gfx/gl/gfxGLTextureObject.h"
  26. #include "gfx/gl/gfxGLUtils.h"
  27. #include "postFx/postEffect.h"
  28. GFX_ImplementTextureProfile( BackBufferDepthProfile,
  29. GFXTextureProfile::DiffuseMap,
  30. GFXTextureProfile::PreserveSize |
  31. GFXTextureProfile::NoMipmap |
  32. GFXTextureProfile::ZTarget |
  33. GFXTextureProfile::Pooled,
  34. GFXTextureProfile::NONE );
  35. GFXGLWindowTarget::GFXGLWindowTarget(PlatformWindow *win, GFXDevice *d)
  36. : GFXWindowTarget(win), mDevice(d), mContext(NULL), mFullscreenContext(NULL)
  37. , mCopyFBO(0), mBackBufferFBO(0)
  38. {
  39. win->appEvent.notify(this, &GFXGLWindowTarget::_onAppSignal);
  40. }
  41. GFXGLWindowTarget::~GFXGLWindowTarget()
  42. {
  43. if(glIsFramebuffer(mCopyFBO))
  44. {
  45. glDeleteFramebuffers(1, &mCopyFBO);
  46. }
  47. }
  48. void GFXGLWindowTarget::resetMode()
  49. {
  50. if(mWindow->getVideoMode().fullScreen != mWindow->isFullscreen())
  51. {
  52. _teardownCurrentMode();
  53. _setupNewMode();
  54. }
  55. GFX->beginReset();
  56. }
  57. void GFXGLWindowTarget::_onAppSignal(WindowId wnd, S32 event)
  58. {
  59. if(event != WindowHidden)
  60. return;
  61. // TODO: Investigate this further.
  62. // Opening and then closing the console results in framerate dropping at an alarming rate down to 3-4 FPS and then
  63. // rebounding to it's usual level. Clearing all the volatile VBs prevents this behavior, but I can't explain why.
  64. // My fear is there is something fundamentally wrong with how we share objects between contexts and this is simply
  65. // masking the issue for the most common case.
  66. static_cast<GFXGLDevice*>(mDevice)->mVolatileVBs.clear();
  67. }
  68. void GFXGLWindowTarget::resolveTo(GFXTextureObject* obj)
  69. {
  70. AssertFatal(dynamic_cast<GFXGLTextureObject*>(obj), "GFXGLTextureTarget::resolveTo - Incorrect type of texture, expected a GFXGLTextureObject");
  71. GFXGLTextureObject* glTexture = static_cast<GFXGLTextureObject*>(obj);
  72. if( gglHasExtension(ARB_copy_image) )
  73. {
  74. if(mBackBufferColorTex.getWidth() == glTexture->getWidth()
  75. && mBackBufferColorTex.getHeight() == glTexture->getHeight()
  76. && mBackBufferColorTex.getFormat() == glTexture->getFormat())
  77. {
  78. glCopyImageSubData(
  79. static_cast<GFXGLTextureObject*>(mBackBufferColorTex.getPointer())->getHandle(), GL_TEXTURE_2D, 0, 0, 0, 0,
  80. glTexture->getHandle(), GL_TEXTURE_2D, 0, 0, 0, 0,
  81. getSize().x, getSize().y, 1);
  82. return;
  83. }
  84. }
  85. PRESERVE_FRAMEBUFFER();
  86. if(!mCopyFBO)
  87. {
  88. glGenFramebuffers(1, &mCopyFBO);
  89. }
  90. glBindFramebuffer(GL_DRAW_FRAMEBUFFER, mCopyFBO);
  91. glFramebufferTexture2D(GL_DRAW_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, glTexture->getHandle(), 0);
  92. glBindFramebuffer(GL_READ_FRAMEBUFFER, mBackBufferFBO);
  93. glBlitFramebuffer(0, 0, getSize().x, getSize().y,
  94. 0, 0, glTexture->getWidth(), glTexture->getHeight(), GL_COLOR_BUFFER_BIT, GL_NEAREST);
  95. }
  96. inline void GFXGLWindowTarget::_setupAttachments()
  97. {
  98. glBindFramebuffer( GL_FRAMEBUFFER, mBackBufferFBO);
  99. GFXGL->getOpenglCache()->setCacheBinded(GL_FRAMEBUFFER, mBackBufferFBO);
  100. const Point2I dstSize = getSize();
  101. mBackBufferColorTex.set(dstSize.x, dstSize.y, getFormat(), &PostFxTargetProfile, "backBuffer");
  102. GFXGLTextureObject *color = static_cast<GFXGLTextureObject*>(mBackBufferColorTex.getPointer());
  103. glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, color->getHandle(), 0);
  104. mBackBufferDepthTex.set(dstSize.x, dstSize.y, GFXFormatD24S8, &BackBufferDepthProfile, "backBuffer");
  105. GFXGLTextureObject *depth = static_cast<GFXGLTextureObject*>(mBackBufferDepthTex.getPointer());
  106. glFramebufferTexture2D(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_TEXTURE_2D, depth->getHandle(), 0);
  107. }
  108. void GFXGLWindowTarget::makeActive()
  109. {
  110. if(mBackBufferFBO)
  111. {
  112. glBindFramebuffer( GL_FRAMEBUFFER, mBackBufferFBO);
  113. GFXGL->getOpenglCache()->setCacheBinded(GL_FRAMEBUFFER, mBackBufferFBO);
  114. }
  115. else
  116. {
  117. glGenFramebuffers(1, &mBackBufferFBO);
  118. _setupAttachments();
  119. CHECK_FRAMEBUFFER_STATUS();
  120. }
  121. }
  122. bool GFXGLWindowTarget::present()
  123. {
  124. PRESERVE_FRAMEBUFFER();
  125. const Point2I srcSize = mBackBufferColorTex.getWidthHeight();
  126. const Point2I dstSize = getSize();
  127. glBindFramebuffer(GL_DRAW_FRAMEBUFFER, 0);
  128. glBindFramebuffer(GL_READ_FRAMEBUFFER, mBackBufferFBO);
  129. // OpenGL render upside down for make render more similar to DX.
  130. // Final screen are corrected here
  131. glBlitFramebuffer(
  132. 0, 0, srcSize.x, srcSize.y,
  133. 0, dstSize.y, dstSize.x, 0, // Y inverted
  134. GL_COLOR_BUFFER_BIT, GL_NEAREST);
  135. _WindowPresent();
  136. if(srcSize != dstSize || mBackBufferDepthTex.getWidthHeight() != dstSize)
  137. _setupAttachments();
  138. return true;
  139. }