gfxGLWindowTarget.cpp 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  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. }
  56. void GFXGLWindowTarget::_onAppSignal(WindowId wnd, S32 event)
  57. {
  58. if(event != WindowHidden)
  59. return;
  60. // TODO: Investigate this further.
  61. // Opening and then closing the console results in framerate dropping at an alarming rate down to 3-4 FPS and then
  62. // rebounding to it's usual level. Clearing all the volatile VBs prevents this behavior, but I can't explain why.
  63. // My fear is there is something fundamentally wrong with how we share objects between contexts and this is simply
  64. // masking the issue for the most common case.
  65. static_cast<GFXGLDevice*>(mDevice)->mVolatileVBs.clear();
  66. }
  67. void GFXGLWindowTarget::resolveTo(GFXTextureObject* obj)
  68. {
  69. AssertFatal(dynamic_cast<GFXGLTextureObject*>(obj), "GFXGLTextureTarget::resolveTo - Incorrect type of texture, expected a GFXGLTextureObject");
  70. GFXGLTextureObject* glTexture = static_cast<GFXGLTextureObject*>(obj);
  71. if( gglHasExtension(ARB_copy_image) )
  72. {
  73. if(mBackBufferColorTex.getWidth() == glTexture->getWidth()
  74. && mBackBufferColorTex.getHeight() == glTexture->getHeight()
  75. && mBackBufferColorTex.getFormat() == glTexture->getFormat())
  76. {
  77. glCopyImageSubData(
  78. static_cast<GFXGLTextureObject*>(mBackBufferColorTex.getPointer())->getHandle(), GL_TEXTURE_2D, 0, 0, 0, 0,
  79. glTexture->getHandle(), GL_TEXTURE_2D, 0, 0, 0, 0,
  80. getSize().x, getSize().y, 1);
  81. return;
  82. }
  83. }
  84. PRESERVE_FRAMEBUFFER();
  85. if(!mCopyFBO)
  86. {
  87. glGenFramebuffers(1, &mCopyFBO);
  88. }
  89. glBindFramebuffer(GL_DRAW_FRAMEBUFFER, mCopyFBO);
  90. glFramebufferTexture2D(GL_DRAW_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, glTexture->getHandle(), 0);
  91. glBindFramebuffer(GL_READ_FRAMEBUFFER, mBackBufferFBO);
  92. glBlitFramebuffer(0, 0, getSize().x, getSize().y,
  93. 0, 0, glTexture->getWidth(), glTexture->getHeight(), GL_COLOR_BUFFER_BIT, GL_NEAREST);
  94. }
  95. inline void GFXGLWindowTarget::_setupAttachments()
  96. {
  97. glBindFramebuffer( GL_FRAMEBUFFER, mBackBufferFBO);
  98. GFXGL->getOpenglCache()->setCacheBinded(GL_FRAMEBUFFER, mBackBufferFBO);
  99. const Point2I dstSize = getSize();
  100. mBackBufferColorTex.set(dstSize.x, dstSize.y, getFormat(), &PostFxTargetProfile, "backBuffer");
  101. GFXGLTextureObject *color = static_cast<GFXGLTextureObject*>(mBackBufferColorTex.getPointer());
  102. glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, color->getHandle(), 0);
  103. mBackBufferDepthTex.set(dstSize.x, dstSize.y, GFXFormatD24S8, &BackBufferDepthProfile, "backBuffer");
  104. GFXGLTextureObject *depth = static_cast<GFXGLTextureObject*>(mBackBufferDepthTex.getPointer());
  105. glFramebufferTexture2D(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_TEXTURE_2D, depth->getHandle(), 0);
  106. }
  107. void GFXGLWindowTarget::makeActive()
  108. {
  109. if(mBackBufferFBO)
  110. {
  111. glBindFramebuffer( GL_FRAMEBUFFER, mBackBufferFBO);
  112. GFXGL->getOpenglCache()->setCacheBinded(GL_FRAMEBUFFER, mBackBufferFBO);
  113. }
  114. else
  115. {
  116. glGenFramebuffers(1, &mBackBufferFBO);
  117. _setupAttachments();
  118. CHECK_FRAMEBUFFER_STATUS();
  119. }
  120. }
  121. bool GFXGLWindowTarget::present()
  122. {
  123. PRESERVE_FRAMEBUFFER();
  124. const Point2I srcSize = mBackBufferColorTex.getWidthHeight();
  125. const Point2I dstSize = getSize();
  126. glBindFramebuffer(GL_DRAW_FRAMEBUFFER, 0);
  127. glBindFramebuffer(GL_READ_FRAMEBUFFER, mBackBufferFBO);
  128. // OpenGL render upside down for make render more similar to DX.
  129. // Final screen are corrected here
  130. glBlitFramebuffer(
  131. 0, 0, srcSize.x, srcSize.y,
  132. 0, dstSize.y, dstSize.x, 0, // Y inverted
  133. GL_COLOR_BUFFER_BIT, GL_NEAREST);
  134. _WindowPresent();
  135. if(srcSize != dstSize || mBackBufferDepthTex.getWidthHeight() != dstSize)
  136. _setupAttachments();
  137. return true;
  138. }