gfxGLWindowTarget.cpp 6.5 KB

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