gfxGLWindowTarget.cpp 6.0 KB

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