/* ----------------------------------------------------------------------------- This source file is part of OGRE (Object-oriented Graphics Rendering Engine) For the latest info, see http://www.ogre3d.org Copyright (c) 2000-2011 Torus Knot Software Ltd 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 "CmGLPBRenderTexture.h" #include "CmGLContext.h" #include "CmGLPixelFormat.h" #include "CmGLHardwarePixelBuffer.h" namespace CamelotEngine { //----------------------------------------------------------------------------- GLPBuffer::GLPBuffer(PixelComponentType format, UINT32 width, UINT32 height): mFormat(format), mWidth(width), mHeight(height) { } GLPBuffer::~GLPBuffer() { } //----------------------------------------------------------------------------- GLPBRenderTexture::GLPBRenderTexture(GLPBRTTManager *manager, const String &name, const GLSurfaceDesc &target, bool writeGamma, UINT32 fsaa): GLRenderTexture(name, target, writeGamma, fsaa), mManager(manager) { mPBFormat = PixelUtil::getComponentType(target.buffer->getFormat()); mManager->requestPBuffer(mPBFormat, mWidth, mHeight); } GLPBRenderTexture::~GLPBRenderTexture() { // Release PBuffer mManager->releasePBuffer(mPBFormat); } void GLPBRenderTexture::getCustomAttribute_internal(const String& name, void* pData) { if(name=="TARGET") { GLSurfaceDesc &target = *static_cast(pData); target.buffer = static_cast(mBuffer); target.zoffset = mZOffset; } else if(name=="GLCONTEXT") { // Get PBuffer for our internal format *static_cast(pData) = mManager->getContextFor(mPBFormat, mWidth, mHeight); } } //----------------------------------------------------------------------------- GLPBRTTManager::GLPBRTTManager(GLSupport *support, RenderTarget *mainwindow): mSupport(support), mMainWindow(mainwindow), mMainContext(0) { mMainWindow->getCustomAttribute_internal("GLCONTEXT", &mMainContext); } GLPBRTTManager::~GLPBRTTManager() { // Delete remaining PBuffers for(size_t x=0; xgetCustomAttribute_internal("TARGET", &surface); if(surface.buffer) static_cast(surface.buffer)->copyFromFramebuffer(surface.zoffset); } void GLPBRTTManager::requestPBuffer(PixelComponentType ctype, UINT32 width, UINT32 height) { //Check size if(mPBuffers[ctype].pb) { if(mPBuffers[ctype].pb->getWidth()getHeight()createPBuffer(ctype, width, height); } ++mPBuffers[ctype].refcount; } void GLPBRTTManager::releasePBuffer(PixelComponentType ctype) { --mPBuffers[ctype].refcount; if(mPBuffers[ctype].refcount == 0) { delete mPBuffers[ctype].pb; mPBuffers[ctype].pb = 0; } } GLContext *GLPBRTTManager::getContextFor(PixelComponentType ctype, UINT32 width, UINT32 height) { // Faster to return main context if the RTT is smaller than the window size // and ctype is PCT_BYTE. This must be checked every time because the window might have been resized if(ctype == PCT_BYTE) { if(width <= mMainWindow->getWidth() && height <= mMainWindow->getHeight()) return mMainContext; } assert(mPBuffers[ctype].pb); return mPBuffers[ctype].pb->getContext(); } //--------------------------------------------------------------------------------------------- }