Torque3D Documentation / _generateds / gfxGLWindowTarget.cpp

gfxGLWindowTarget.cpp

Engine/source/gfx/gl/gfxGLWindowTarget.cpp

More...

Detailed Description

Public Functions

GFX_ImplementTextureProfile(BackBufferDepthProfile , GFXTextureProfile::DiffuseMap , GFXTextureProfile::PreserveSize</a>|<a href="/coding/class/classgfxtextureprofile/#classgfxtextureprofile_1a09105f0bf717a1f2ae49ceda21b8e82daf55b13e32bd1a1746406b68ead73ba05">GFXTextureProfile::NoMipmap</a>|<a href="/coding/class/classgfxtextureprofile/#classgfxtextureprofile_1a09105f0bf717a1f2ae49ceda21b8e82daa3a2027753aec009ced4f0818a231296">GFXTextureProfile::ZTarget</a>|<a href="/coding/class/classgfxtextureprofile/#classgfxtextureprofile_1a09105f0bf717a1f2ae49ceda21b8e82da8e64fa44eaa9b6444614dac7bdb1dcf8">GFXTextureProfile::Pooled , GFXTextureProfile::NONE )

  1
  2//-----------------------------------------------------------------------------
  3// Copyright (c) 2012 GarageGames, LLC
  4//
  5// Permission is hereby granted, free of charge, to any person obtaining a copy
  6// of this software and associated documentation files (the "Software"), to
  7// deal in the Software without restriction, including without limitation the
  8// rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
  9// sell copies of the Software, and to permit persons to whom the Software is
 10// furnished to do so, subject to the following conditions:
 11//
 12// The above copyright notice and this permission notice shall be included in
 13// all copies or substantial portions of the Software.
 14//
 15// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 16// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 17// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 18// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 19// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
 20// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
 21// IN THE SOFTWARE.
 22//-----------------------------------------------------------------------------
 23
 24#include "windowManager/platformWindow.h"
 25#include "gfx/gl/gfxGLDevice.h"
 26#include "gfx/gl/gfxGLWindowTarget.h"
 27#include "gfx/gl/gfxGLTextureObject.h"
 28#include "gfx/gl/gfxGLUtils.h"
 29#include "postFx/postEffect.h"
 30
 31GFX_ImplementTextureProfile( BackBufferDepthProfile,
 32                             GFXTextureProfile::DiffuseMap,
 33                             GFXTextureProfile::PreserveSize |
 34                             GFXTextureProfile::NoMipmap |
 35                             GFXTextureProfile::ZTarget |
 36                             GFXTextureProfile::Pooled,
 37                             GFXTextureProfile::NONE );
 38
 39GFXGLWindowTarget::GFXGLWindowTarget(PlatformWindow *win, GFXDevice *d)
 40      : GFXWindowTarget(win), mDevice(d), mContext(NULL), mFullscreenContext(NULL)
 41      , mCopyFBO(0), mBackBufferFBO(0), mSecondaryWindow(false)
 42{      
 43   win->appEvent.notify(this, &GFXGLWindowTarget::_onAppSignal);
 44}
 45
 46GFXGLWindowTarget::~GFXGLWindowTarget()
 47{
 48   if(glIsFramebuffer(mCopyFBO))
 49   {
 50      glDeleteFramebuffers(1, &mCopyFBO);
 51   }
 52}
 53
 54void GFXGLWindowTarget::resetMode()
 55{
 56   // Do some validation...
 57   bool fullscreen = mWindow->getVideoMode().fullScreen;
 58   if (fullscreen && mSecondaryWindow)
 59   {
 60      AssertFatal(false, "GFXGLWindowTarget::resetMode - Cannot go fullscreen with secondary window!");
 61   }
 62
 63   if (fullscreen != mWindow->isFullscreen())
 64   {
 65      _teardownCurrentMode();
 66      _setupNewMode();
 67   }
 68   GFX->beginReset();
 69}
 70
 71void GFXGLWindowTarget::_onAppSignal(WindowId wnd, S32 event)
 72{
 73   if(event != WindowHidden)
 74      return;
 75      
 76   // TODO: Investigate this further.
 77   // Opening and then closing the console results in framerate dropping at an alarming rate down to 3-4 FPS and then
 78   // rebounding to it's usual level.  Clearing all the volatile VBs prevents this behavior, but I can't explain why.
 79   // My fear is there is something fundamentally wrong with how we share objects between contexts and this is simply 
 80   // masking the issue for the most common case.
 81   static_cast<GFXGLDevice*>(mDevice)->mVolatileVBs.clear();
 82}
 83
 84void GFXGLWindowTarget::resolveTo(GFXTextureObject* obj)
 85{
 86   AssertFatal(dynamic_cast<GFXGLTextureObject*>(obj), "GFXGLTextureTarget::resolveTo - Incorrect type of texture, expected a GFXGLTextureObject");
 87   GFXGLTextureObject* glTexture = static_cast<GFXGLTextureObject*>(obj);
 88
 89   if( GFXGL->mCapabilities.copyImage )
 90   {
 91      if(mBackBufferColorTex.getWidth() == glTexture->getWidth()
 92         && mBackBufferColorTex.getHeight() == glTexture->getHeight()
 93         && mBackBufferColorTex.getFormat() == glTexture->getFormat())
 94      {
 95         glCopyImageSubData(
 96           static_cast<GFXGLTextureObject*>(mBackBufferColorTex.getPointer())->getHandle(), GL_TEXTURE_2D, 0, 0, 0, 0,
 97           glTexture->getHandle(), GL_TEXTURE_2D, 0, 0, 0, 0,
 98           getSize().x, getSize().y, 1);
 99         return;
100      }
101   }
102
103   PRESERVE_FRAMEBUFFER();
104
105   if(!mCopyFBO)
106   {
107      glGenFramebuffers(1, &mCopyFBO);
108   }
109   
110   glBindFramebuffer(GL_DRAW_FRAMEBUFFER, mCopyFBO);
111   glFramebufferTexture2D(GL_DRAW_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, glTexture->getHandle(), 0);
112   
113   glBindFramebuffer(GL_READ_FRAMEBUFFER, mBackBufferFBO);
114   
115   glBlitFramebuffer(0, 0, getSize().x, getSize().y,
116      0, 0, glTexture->getWidth(), glTexture->getHeight(), GL_COLOR_BUFFER_BIT, GL_NEAREST);
117}
118
119inline void GFXGLWindowTarget::_setupAttachments()
120{
121   glBindFramebuffer( GL_FRAMEBUFFER, mBackBufferFBO);
122   glEnable(GL_FRAMEBUFFER_SRGB);
123   GFXGL->getOpenglCache()->setCacheBinded(GL_FRAMEBUFFER, mBackBufferFBO);
124   const Point2I dstSize = getSize();
125   mBackBufferColorTex.set(dstSize.x, dstSize.y, getFormat(), &GFXRenderTargetSRGBProfile, "backBuffer");
126   GFXGLTextureObject *color = static_cast<GFXGLTextureObject*>(mBackBufferColorTex.getPointer());
127   glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, color->getHandle(), 0);
128   mBackBufferDepthTex.set(dstSize.x, dstSize.y, GFXFormatD24S8, &BackBufferDepthProfile, "backBuffer");
129   GFXGLTextureObject *depth = static_cast<GFXGLTextureObject*>(mBackBufferDepthTex.getPointer());
130   glFramebufferTexture2D(GL_FRAMEBUFFER, GL_DEPTH_STENCIL_ATTACHMENT, GL_TEXTURE_2D, depth->getHandle(), 0);
131}
132
133void GFXGLWindowTarget::makeActive()
134{
135   //make the rendering context active on this window
136   _makeContextCurrent();
137
138   if(mBackBufferFBO)
139   {
140      glBindFramebuffer( GL_FRAMEBUFFER, mBackBufferFBO);
141      GFXGL->getOpenglCache()->setCacheBinded(GL_FRAMEBUFFER, mBackBufferFBO);
142   }
143   else
144   {
145      glGenFramebuffers(1, &mBackBufferFBO);
146      _setupAttachments();
147      CHECK_FRAMEBUFFER_STATUS();
148   }
149}
150
151bool GFXGLWindowTarget::present()
152{
153    PRESERVE_FRAMEBUFFER();
154
155   const Point2I srcSize = mBackBufferColorTex.getWidthHeight();
156   const Point2I dstSize = getSize();
157
158   glBindFramebuffer(GL_DRAW_FRAMEBUFFER, 0);
159   glBindFramebuffer(GL_READ_FRAMEBUFFER, mBackBufferFBO);
160
161   // OpenGL render upside down for make render more similar to DX.
162   // Final screen are corrected here
163   glBlitFramebuffer(
164      0, 0, srcSize.x, srcSize.y,
165      0, dstSize.y, dstSize.x, 0, // Y inverted
166      GL_COLOR_BUFFER_BIT, GL_NEAREST);
167
168   _WindowPresent();
169
170   if(srcSize != dstSize || mBackBufferDepthTex.getWidthHeight() != dstSize)
171      _setupAttachments();
172
173   return true;
174}
175