Torque3D/Engine/source/gfx/gl/gfxGLWindowTarget.cpp

167 lines
6.2 KiB
C++
Raw Permalink Normal View History

2012-09-19 15:15:01 +00:00
//-----------------------------------------------------------------------------
// Copyright (c) 2012 GarageGames, LLC
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to
// deal in the Software without restriction, including without limitation the
// rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
// sell copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
// IN THE SOFTWARE.
//-----------------------------------------------------------------------------
#include "windowManager/platformWindow.h"
#include "gfx/gl/gfxGLDevice.h"
#include "gfx/gl/gfxGLWindowTarget.h"
#include "gfx/gl/gfxGLTextureObject.h"
#include "gfx/gl/gfxGLUtils.h"
2014-11-08 16:41:17 +00:00
#include "postFx/postEffect.h"
GFX_ImplementTextureProfile( BackBufferDepthProfile,
GFXTextureProfile::DiffuseMap,
GFXTextureProfile::PreserveSize |
GFXTextureProfile::NoMipmap |
GFXTextureProfile::ZTarget |
GFXTextureProfile::Pooled,
GFXTextureProfile::NONE );
2012-09-19 15:15:01 +00:00
GFXGLWindowTarget::GFXGLWindowTarget(PlatformWindow *win, GFXDevice *d)
: GFXWindowTarget(win), mDevice(d), mContext(NULL), mFullscreenContext(NULL)
, mCopyFBO(0), mBackBufferFBO(0)
2012-09-19 15:15:01 +00:00
{
win->appEvent.notify(this, &GFXGLWindowTarget::_onAppSignal);
}
2016-01-18 04:55:48 +00:00
GFXGLWindowTarget::~GFXGLWindowTarget()
{
if(glIsFramebuffer(mCopyFBO))
{
glDeleteFramebuffers(1, &mCopyFBO);
}
}
2012-09-19 15:15:01 +00:00
void GFXGLWindowTarget::resetMode()
{
if(mWindow->getVideoMode().fullScreen != mWindow->isFullscreen())
2012-09-19 15:15:01 +00:00
{
_teardownCurrentMode();
_setupNewMode();
}
The final step (barring any overlooked missing bits, requested refactors, and of course, rolling in dependencies already submitted as PRs) consists of: renderPrePassMgr.cpp related: A) shifting .addFeature( MFT_XYZ); calls from ProcessedShaderMaterial::_determineFeatures to ProcessedPrePassMaterial::_determineFeatures B) mimicking the "// set the XXX if different" entries from RenderMeshMgr::render in RenderPrePassMgr::render C) fleshing out ProcessedPrePassMaterial::getNumStages() so that it shares a 1:1 correlation with ProcessedShaderMaterial::getNumStages() D) causing inline void Swizzle<T, mapLength>::ToBuffer( void *destination, const void *source, const dsize_t size ) to silently fail rather than fatally assert if a source or destination buffer is not yet ready to be filled. (support for #customTarget scripted render targets) Reflections: A) removing reflectRenderState.disableAdvancedLightingBins(true); entries. this would otherwise early out from prepass and provide no color data whatsoever. B) removing the fd.features.addFeature( MFT_ForwardShading ); entry forcing all materials to be forward lit when reflected. C) 2 things best described bluntly as working hacks: C1) when reflected, a scattersky is rotated PI along it's z then x axis in order to draw properly. C2) along similar lines, in terraincellmaterial, we shut off culling if it's a prepass material. Skies: scattersky is given a pair of rotations for reflection purposes, all sky objects are given a z value for depth testing.
2016-02-16 08:50:49 +00:00
GFX->beginReset();
2012-09-19 15:15:01 +00:00
}
void GFXGLWindowTarget::_onAppSignal(WindowId wnd, S32 event)
{
if(event != WindowHidden)
return;
// TODO: Investigate this further.
// Opening and then closing the console results in framerate dropping at an alarming rate down to 3-4 FPS and then
// rebounding to it's usual level. Clearing all the volatile VBs prevents this behavior, but I can't explain why.
// My fear is there is something fundamentally wrong with how we share objects between contexts and this is simply
// masking the issue for the most common case.
static_cast<GFXGLDevice*>(mDevice)->mVolatileVBs.clear();
}
void GFXGLWindowTarget::resolveTo(GFXTextureObject* obj)
{
AssertFatal(dynamic_cast<GFXGLTextureObject*>(obj), "GFXGLTextureTarget::resolveTo - Incorrect type of texture, expected a GFXGLTextureObject");
GFXGLTextureObject* glTexture = static_cast<GFXGLTextureObject*>(obj);
if( GFXGL->mCapabilities.copyImage )
2014-11-08 16:41:17 +00:00
{
if(mBackBufferColorTex.getWidth() == glTexture->getWidth()
&& mBackBufferColorTex.getHeight() == glTexture->getHeight()
&& mBackBufferColorTex.getFormat() == glTexture->getFormat())
{
glCopyImageSubData(
static_cast<GFXGLTextureObject*>(mBackBufferColorTex.getPointer())->getHandle(), GL_TEXTURE_2D, 0, 0, 0, 0,
glTexture->getHandle(), GL_TEXTURE_2D, 0, 0, 0, 0,
getSize().x, getSize().y, 1);
return;
}
}
2012-09-19 15:15:01 +00:00
PRESERVE_FRAMEBUFFER();
2014-11-08 16:41:17 +00:00
if(!mCopyFBO)
{
glGenFramebuffers(1, &mCopyFBO);
}
2012-09-19 15:15:01 +00:00
2014-11-08 16:41:17 +00:00
glBindFramebuffer(GL_DRAW_FRAMEBUFFER, mCopyFBO);
glFramebufferTexture2D(GL_DRAW_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, glTexture->getHandle(), 0);
2012-09-19 15:15:01 +00:00
2014-11-08 16:41:17 +00:00
glBindFramebuffer(GL_READ_FRAMEBUFFER, mBackBufferFBO);
2012-09-19 15:15:01 +00:00
2014-11-08 16:41:17 +00:00
glBlitFramebuffer(0, 0, getSize().x, getSize().y,
2012-09-19 15:15:01 +00:00
0, 0, glTexture->getWidth(), glTexture->getHeight(), GL_COLOR_BUFFER_BIT, GL_NEAREST);
2014-11-08 16:41:17 +00:00
}
inline void GFXGLWindowTarget::_setupAttachments()
{
glBindFramebuffer( GL_FRAMEBUFFER, mBackBufferFBO);
glEnable(GL_FRAMEBUFFER_SRGB);
2014-11-08 16:41:17 +00:00
GFXGL->getOpenglCache()->setCacheBinded(GL_FRAMEBUFFER, mBackBufferFBO);
const Point2I dstSize = getSize();
mBackBufferColorTex.set(dstSize.x, dstSize.y, getFormat(), &GFXRenderTargetSRGBProfile, "backBuffer");
2014-11-08 16:41:17 +00:00
GFXGLTextureObject *color = static_cast<GFXGLTextureObject*>(mBackBufferColorTex.getPointer());
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, color->getHandle(), 0);
mBackBufferDepthTex.set(dstSize.x, dstSize.y, GFXFormatD24S8, &BackBufferDepthProfile, "backBuffer");
GFXGLTextureObject *depth = static_cast<GFXGLTextureObject*>(mBackBufferDepthTex.getPointer());
2016-09-28 01:09:48 +00:00
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_DEPTH_STENCIL_ATTACHMENT, GL_TEXTURE_2D, depth->getHandle(), 0);
2014-11-08 16:41:17 +00:00
}
void GFXGLWindowTarget::makeActive()
{
2016-09-28 01:09:48 +00:00
//make the rendering context active on this window
_makeContextCurrent();
2014-11-08 16:41:17 +00:00
if(mBackBufferFBO)
{
glBindFramebuffer( GL_FRAMEBUFFER, mBackBufferFBO);
GFXGL->getOpenglCache()->setCacheBinded(GL_FRAMEBUFFER, mBackBufferFBO);
}
else
{
glGenFramebuffers(1, &mBackBufferFBO);
_setupAttachments();
CHECK_FRAMEBUFFER_STATUS();
}
}
bool GFXGLWindowTarget::present()
{
PRESERVE_FRAMEBUFFER();
const Point2I srcSize = mBackBufferColorTex.getWidthHeight();
const Point2I dstSize = getSize();
glBindFramebuffer(GL_DRAW_FRAMEBUFFER, 0);
glBindFramebuffer(GL_READ_FRAMEBUFFER, mBackBufferFBO);
// OpenGL render upside down for make render more similar to DX.
// Final screen are corrected here
glBlitFramebuffer(
0, 0, srcSize.x, srcSize.y,
0, dstSize.y, dstSize.x, 0, // Y inverted
GL_COLOR_BUFFER_BIT, GL_NEAREST);
_WindowPresent();
if(srcSize != dstSize || mBackBufferDepthTex.getWidthHeight() != dstSize)
_setupAttachments();
return true;
2012-09-19 15:15:01 +00:00
}