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-2014 Torus Knot Software Ltd 8 9 Permission is hereby granted, free of charge, to any person obtaining a copy 10 of this software and associated documentation files (the "Software"), to deal 11 in the Software without restriction, including without limitation the rights 12 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 13 copies of the Software, and to permit persons to whom the Software is 14 furnished to do so, subject to the following conditions: 15 16 The above copyright notice and this permission notice shall be included in 17 all copies or substantial portions of the Software. 18 19 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 20 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 21 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 22 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 23 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 24 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 25 THE SOFTWARE. 26 ----------------------------------------------------------------------------- 27 */ 28 29 #include "OgreGLRenderTexture.h" 30 #include "OgreGLHardwarePixelBufferCommon.h" 31 #include "OgreGLRenderSystemCommon.h" 32 #include "OgreRoot.h" 33 34 namespace Ogre { 35 36 const String GLRenderTexture::CustomAttributeString_FBO = "FBO"; 37 const String GLRenderTexture::CustomAttributeString_TARGET = "TARGET"; 38 const String GLRenderTexture::CustomAttributeString_GLCONTEXT = "GLCONTEXT"; 39 40 template<> GLRTTManager* Singleton<GLRTTManager>::msSingleton = NULL; 41 GLFrameBufferObjectCommon(int32 fsaa)42 GLFrameBufferObjectCommon::GLFrameBufferObjectCommon(int32 fsaa) 43 : mFB(0), mMultisampleFB(0), mNumSamples(fsaa) 44 { 45 auto* rs = static_cast<GLRenderSystemCommon*>( 46 Root::getSingleton().getRenderSystem()); 47 mContext = rs->_getCurrentContext(); 48 49 // Initialise state 50 mDepth.buffer = 0; 51 mStencil.buffer = 0; 52 for(size_t x = 0; x < OGRE_MAX_MULTIPLE_RENDER_TARGETS; ++x) 53 { 54 mColour[x].buffer=0; 55 } 56 } 57 bindSurface(size_t attachment,const GLSurfaceDesc & target)58 void GLFrameBufferObjectCommon::bindSurface(size_t attachment, const GLSurfaceDesc &target) 59 { 60 assert(attachment < OGRE_MAX_MULTIPLE_RENDER_TARGETS); 61 mColour[attachment] = target; 62 // Re-initialise 63 if(mColour[0].buffer) 64 initialise(); 65 } 66 unbindSurface(size_t attachment)67 void GLFrameBufferObjectCommon::unbindSurface(size_t attachment) 68 { 69 assert(attachment < OGRE_MAX_MULTIPLE_RENDER_TARGETS); 70 mColour[attachment].buffer = 0; 71 // Re-initialise if buffer 0 still bound 72 if(mColour[0].buffer) 73 initialise(); 74 } 75 getWidth() const76 uint32 GLFrameBufferObjectCommon::getWidth() const 77 { 78 assert(mColour[0].buffer); 79 return mColour[0].buffer->getWidth(); 80 } getHeight() const81 uint32 GLFrameBufferObjectCommon::getHeight() const 82 { 83 assert(mColour[0].buffer); 84 return mColour[0].buffer->getHeight(); 85 } getFormat() const86 PixelFormat GLFrameBufferObjectCommon::getFormat() const 87 { 88 assert(mColour[0].buffer); 89 return mColour[0].buffer->getFormat(); 90 } 91 getSingletonPtr(void)92 GLRTTManager* GLRTTManager::getSingletonPtr(void) 93 { 94 return msSingleton; 95 } getSingleton(void)96 GLRTTManager& GLRTTManager::getSingleton(void) 97 { 98 assert( msSingleton ); return ( *msSingleton ); 99 } 100 GLRTTManager()101 GLRTTManager::GLRTTManager() {} 102 // need to implement in cpp due to how Ogre::Singleton works ~GLRTTManager()103 GLRTTManager::~GLRTTManager() {} 104 createMultiRenderTarget(const String & name)105 MultiRenderTarget* GLRTTManager::createMultiRenderTarget(const String & name) 106 { 107 // TODO: Check rendersystem capabilities before throwing the exception 108 OGRE_EXCEPT(Exception::ERR_NOT_IMPLEMENTED, 109 "MultiRenderTarget is not supported", 110 "GLRTTManager::createMultiRenderTarget"); 111 } 112 getSupportedAlternative(PixelFormat format)113 PixelFormat GLRTTManager::getSupportedAlternative(PixelFormat format) 114 { 115 if (checkFormat(format)) 116 { 117 return format; 118 } 119 120 /// Find first alternative 121 PixelComponentType pct = PixelUtil::getComponentType(format); 122 123 switch (pct) 124 { 125 case PCT_BYTE: 126 format = PF_BYTE_RGBA; // native endian 127 break; 128 case PCT_SHORT: 129 format = PF_SHORT_RGBA; 130 break; 131 case PCT_FLOAT16: 132 format = PF_FLOAT16_RGBA; 133 break; 134 case PCT_FLOAT32: 135 format = PF_FLOAT32_RGBA; 136 break; 137 case PCT_COUNT: 138 default: 139 break; 140 } 141 142 if (checkFormat(format)) 143 return format; 144 145 /// If none at all, return to default 146 return PF_BYTE_RGBA; // native endian 147 } 148 releaseRenderBuffer(const GLSurfaceDesc & surface)149 void GLRTTManager::releaseRenderBuffer(const GLSurfaceDesc &surface) 150 { 151 if(surface.buffer == 0) 152 return; 153 RBFormat key(surface.buffer->getGLFormat(), surface.buffer->getWidth(), surface.buffer->getHeight(), surface.numSamples); 154 RenderBufferMap::iterator it = mRenderBufferMap.find(key); 155 if(it != mRenderBufferMap.end()) 156 { 157 // Decrease refcount 158 --it->second.refcount; 159 if(it->second.refcount==0) 160 { 161 // If refcount reaches zero, delete buffer and remove from map 162 delete it->second.buffer; 163 mRenderBufferMap.erase(it); 164 // std::cerr << "Destroyed renderbuffer of format " << std::hex << key.format << std::dec 165 // << " of " << key.width << "x" << key.height << std::endl; 166 } 167 } 168 } 169 GLRenderTexture(const String & name,const GLSurfaceDesc & target,bool writeGamma,uint fsaa)170 GLRenderTexture::GLRenderTexture(const String &name, 171 const GLSurfaceDesc &target, 172 bool writeGamma, 173 uint fsaa) 174 : RenderTexture(target.buffer, target.zoffset) 175 { 176 mName = name; 177 mHwGamma = writeGamma; 178 mFSAA = fsaa; 179 } 180 } 181