| 1 | /* | 
|---|
| 2 | ----------------------------------------------------------------------------- | 
|---|
| 3 | This source file is part of OGRE | 
|---|
| 4 |     (Object-oriented Graphics Rendering Engine) | 
|---|
| 5 | For the latest info, see http://www.ogre3d.org | 
|---|
| 6 |  | 
|---|
| 7 | Copyright (c) 2000-2006 Torus Knot Software Ltd | 
|---|
| 8 | Also see acknowledgements in Readme.html | 
|---|
| 9 |  | 
|---|
| 10 | This program is free software; you can redistribute it and/or modify it under | 
|---|
| 11 | the terms of the GNU Lesser General Public License as published by the Free Software | 
|---|
| 12 | Foundation; either version 2 of the License, or (at your option) any later | 
|---|
| 13 | version. | 
|---|
| 14 |  | 
|---|
| 15 | This program is distributed in the hope that it will be useful, but WITHOUT | 
|---|
| 16 | ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS | 
|---|
| 17 | FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. | 
|---|
| 18 |  | 
|---|
| 19 | You should have received a copy of the GNU Lesser General Public License along with | 
|---|
| 20 | this program; if not, write to the Free Software Foundation, Inc., 59 Temple | 
|---|
| 21 | Place - Suite 330, Boston, MA 02111-1307, USA, or go to | 
|---|
| 22 | http://www.gnu.org/copyleft/lesser.txt. | 
|---|
| 23 |  | 
|---|
| 24 | You may alternatively use this source under the terms of a specific version of | 
|---|
| 25 | the OGRE Unrestricted License provided you have obtained such a license from | 
|---|
| 26 | Torus Knot Software Ltd. | 
|---|
| 27 | ----------------------------------------------------------------------------- | 
|---|
| 28 | */ | 
|---|
| 29 | #include "OgreGLPBRenderTexture.h" | 
|---|
| 30 | #include "OgreGLContext.h" | 
|---|
| 31 | #include "OgreGLPixelFormat.h" | 
|---|
| 32 | #include "OgreLogManager.h" | 
|---|
| 33 | #include "OgreStringConverter.h" | 
|---|
| 34 | #include "OgreRoot.h" | 
|---|
| 35 | #include "OgreGLHardwarePixelBuffer.h" | 
|---|
| 36 |  | 
|---|
| 37 | namespace Ogre { | 
|---|
| 38 | //-----------------------------------------------------------------------------   | 
|---|
| 39 |     GLPBuffer::GLPBuffer(PixelComponentType format, size_t width, size_t height): | 
|---|
| 40 |         mFormat(format), | 
|---|
| 41 |         mWidth(width), | 
|---|
| 42 |         mHeight(height) | 
|---|
| 43 |     { | 
|---|
| 44 |     } | 
|---|
| 45 |     GLPBuffer::~GLPBuffer() | 
|---|
| 46 |     { | 
|---|
| 47 |     } | 
|---|
| 48 |  | 
|---|
| 49 | //-----------------------------------------------------------------------------   | 
|---|
| 50 |     GLPBRenderTexture::GLPBRenderTexture(GLPBRTTManager *manager, const String &name, const GLSurfaceDesc &target): | 
|---|
| 51 |         GLRenderTexture(name, target), | 
|---|
| 52 |         mManager(manager) | 
|---|
| 53 |     { | 
|---|
| 54 |         mPBFormat = PixelUtil::getComponentType(target.buffer->getFormat()); | 
|---|
| 55 |          | 
|---|
| 56 |         mManager->requestPBuffer(mPBFormat, mWidth, mHeight); | 
|---|
| 57 |     } | 
|---|
| 58 |     GLPBRenderTexture::~GLPBRenderTexture() | 
|---|
| 59 |     { | 
|---|
| 60 |         // Release PBuffer | 
|---|
| 61 |         mManager->releasePBuffer(mPBFormat); | 
|---|
| 62 |     } | 
|---|
| 63 |     void GLPBRenderTexture::getCustomAttribute(const String& name, void* pData) | 
|---|
| 64 |     { | 
|---|
| 65 |         if(name=="TARGET") | 
|---|
| 66 |         { | 
|---|
| 67 |                         GLSurfaceDesc &target = *static_cast<GLSurfaceDesc*>(pData); | 
|---|
| 68 |                         target.buffer = static_cast<GLHardwarePixelBuffer*>(mBuffer); | 
|---|
| 69 |                         target.zoffset = mZOffset; | 
|---|
| 70 |         } | 
|---|
| 71 |         else if(name=="GLCONTEXT") | 
|---|
| 72 |         { | 
|---|
| 73 |             // Get PBuffer for our internal format | 
|---|
| 74 |             *static_cast<GLContext**>(pData) = mManager->getContextFor(mPBFormat, mWidth, mHeight); | 
|---|
| 75 |         } | 
|---|
| 76 |     } | 
|---|
| 77 | //-----------------------------------------------------------------------------   | 
|---|
| 78 |     GLPBRTTManager::GLPBRTTManager(GLSupport *support, RenderTarget *mainwindow): | 
|---|
| 79 |         mSupport(support), | 
|---|
| 80 |                 mMainWindow(mainwindow), | 
|---|
| 81 |                 mMainContext(0) | 
|---|
| 82 |     { | 
|---|
| 83 |                 mMainWindow->getCustomAttribute("GLCONTEXT", &mMainContext); | 
|---|
| 84 |     }   | 
|---|
| 85 |     GLPBRTTManager::~GLPBRTTManager() | 
|---|
| 86 |     { | 
|---|
| 87 |         // Delete remaining PBuffers | 
|---|
| 88 |         for(size_t x=0; x<PCT_COUNT; ++x) | 
|---|
| 89 |         { | 
|---|
| 90 |             delete mPBuffers[x].pb; | 
|---|
| 91 |         } | 
|---|
| 92 |     } | 
|---|
| 93 |  | 
|---|
| 94 |     RenderTexture *GLPBRTTManager::createRenderTexture(const String &name, const GLSurfaceDesc &target) | 
|---|
| 95 |     { | 
|---|
| 96 |         return new GLPBRenderTexture(this, name, target); | 
|---|
| 97 |     } | 
|---|
| 98 |      | 
|---|
| 99 |     bool GLPBRTTManager::checkFormat(PixelFormat format)  | 
|---|
| 100 |     {  | 
|---|
| 101 |         return true;  | 
|---|
| 102 |     } | 
|---|
| 103 |  | 
|---|
| 104 |     void GLPBRTTManager::bind(RenderTarget *target) | 
|---|
| 105 |     { | 
|---|
| 106 |         // Nothing to do here | 
|---|
| 107 |         // Binding of context is done by GL subsystem, as contexts are also used for RenderWindows | 
|---|
| 108 |     } | 
|---|
| 109 |  | 
|---|
| 110 |     void GLPBRTTManager::unbind(RenderTarget *target) | 
|---|
| 111 |     {  | 
|---|
| 112 |                 // Copy on unbind | 
|---|
| 113 |         GLSurfaceDesc surface; | 
|---|
| 114 |                 surface.buffer = 0; | 
|---|
| 115 |         target->getCustomAttribute("TARGET", &surface); | 
|---|
| 116 |         if(surface.buffer) | 
|---|
| 117 |             static_cast<GLTextureBuffer*>(surface.buffer)->copyFromFramebuffer(surface.zoffset); | 
|---|
| 118 |     } | 
|---|
| 119 |      | 
|---|
| 120 |     void GLPBRTTManager::requestPBuffer(PixelComponentType ctype, size_t width, size_t height) | 
|---|
| 121 |     { | 
|---|
| 122 |         //Check size | 
|---|
| 123 |         if(mPBuffers[ctype].pb) | 
|---|
| 124 |         { | 
|---|
| 125 |             if(mPBuffers[ctype].pb->getWidth()<width || mPBuffers[ctype].pb->getHeight()<height) | 
|---|
| 126 |             { | 
|---|
| 127 |                 // If the current PBuffer is too small, destroy it and create a new one | 
|---|
| 128 |                 delete mPBuffers[ctype].pb; | 
|---|
| 129 |                 mPBuffers[ctype].pb = 0; | 
|---|
| 130 |             } | 
|---|
| 131 |         } | 
|---|
| 132 |         if(!mPBuffers[ctype].pb) | 
|---|
| 133 |         { | 
|---|
| 134 |             // Create pbuffer via rendersystem | 
|---|
| 135 |             mPBuffers[ctype].pb = mSupport->createPBuffer(ctype, width, height); | 
|---|
| 136 |         } | 
|---|
| 137 |          | 
|---|
| 138 |         ++mPBuffers[ctype].refcount; | 
|---|
| 139 |     } | 
|---|
| 140 |      | 
|---|
| 141 |     void GLPBRTTManager::releasePBuffer(PixelComponentType ctype) | 
|---|
| 142 |     { | 
|---|
| 143 |         --mPBuffers[ctype].refcount; | 
|---|
| 144 |         if(mPBuffers[ctype].refcount == 0) | 
|---|
| 145 |         { | 
|---|
| 146 |             delete mPBuffers[ctype].pb; | 
|---|
| 147 |             mPBuffers[ctype].pb = 0; | 
|---|
| 148 |         } | 
|---|
| 149 |     } | 
|---|
| 150 |      | 
|---|
| 151 |     GLContext *GLPBRTTManager::getContextFor(PixelComponentType ctype, size_t width, size_t height) | 
|---|
| 152 |     { | 
|---|
| 153 |         // Faster to return main context if the RTT is smaller than the window size | 
|---|
| 154 |         // and ctype is PCT_BYTE. This must be checked every time because the window might have been resized | 
|---|
| 155 |                 if(ctype == PCT_BYTE) | 
|---|
| 156 |                 { | 
|---|
| 157 |                         if(width <= mMainWindow->getWidth() && height <= mMainWindow->getHeight()) | 
|---|
| 158 |                                 return mMainContext; | 
|---|
| 159 |                 } | 
|---|
| 160 |         assert(mPBuffers[ctype].pb); | 
|---|
| 161 |         return mPBuffers[ctype].pb->getContext(); | 
|---|
| 162 |     } | 
|---|
| 163 | //--------------------------------------------------------------------------------------------- | 
|---|
| 164 |  | 
|---|
| 165 | } | 
|---|
| 166 |  | 
|---|