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-2013 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 #include "OgreGLDepthBuffer.h"
29 #include "OgreGLHardwarePixelBuffer.h"
30 #include "OgreGLRenderSystem.h"
31 #include "OgreGLFrameBufferObject.h"
32 
33 namespace Ogre
34 {
GLDepthBuffer(uint16 poolId,GLRenderSystem * renderSystem,GLContext * creatorContext,GLRenderBuffer * depth,GLRenderBuffer * stencil,uint32 width,uint32 height,uint32 fsaa,uint32 multiSampleQuality,bool manual)35 	GLDepthBuffer::GLDepthBuffer( uint16 poolId, GLRenderSystem *renderSystem, GLContext *creatorContext,
36 									GLRenderBuffer *depth, GLRenderBuffer *stencil,
37 									uint32 width, uint32 height, uint32 fsaa, uint32 multiSampleQuality,
38 									bool manual ) :
39 				DepthBuffer( poolId, 0, width, height, fsaa, "", manual ),
40 				mMultiSampleQuality( multiSampleQuality ),
41 				mCreatorContext( creatorContext ),
42 				mDepthBuffer( depth ),
43 				mStencilBuffer( stencil ),
44 				mRenderSystem( renderSystem )
45 	{
46 		if( mDepthBuffer )
47 		{
48 			switch( mDepthBuffer->getGLFormat() )
49 			{
50 			case GL_DEPTH_COMPONENT16:
51 				mBitDepth = 16;
52 				break;
53 			case GL_DEPTH_COMPONENT24:
54 			case GL_DEPTH_COMPONENT32:
55 			case GL_DEPTH24_STENCIL8_EXT:
56 				mBitDepth = 32;
57 				break;
58 			}
59 		}
60 	}
61 
~GLDepthBuffer()62 	GLDepthBuffer::~GLDepthBuffer()
63 	{
64 		if( mStencilBuffer && mStencilBuffer != mDepthBuffer )
65 		{
66 			delete mStencilBuffer;
67 			mStencilBuffer = 0;
68 		}
69 
70 		if( mDepthBuffer )
71 		{
72 			delete mDepthBuffer;
73 			mDepthBuffer = 0;
74 		}
75 	}
76 	//---------------------------------------------------------------------
isCompatible(RenderTarget * renderTarget) const77 	bool GLDepthBuffer::isCompatible( RenderTarget *renderTarget ) const
78 	{
79 		bool retVal = false;
80 
81 		//Check standard stuff first.
82 		if( mRenderSystem->getCapabilities()->hasCapability( RSC_RTT_DEPTHBUFFER_RESOLUTION_LESSEQUAL ) )
83 		{
84 			if( !DepthBuffer::isCompatible( renderTarget ) )
85 				return false;
86 		}
87 		else
88 		{
89 			if( this->getWidth() != renderTarget->getWidth() ||
90 				this->getHeight() != renderTarget->getHeight() ||
91 				this->getFsaa() != renderTarget->getFSAA() )
92 					return false;
93 		}
94 
95 		//Now check this is the appropriate format
96 		GLFrameBufferObject *fbo = 0;
97         renderTarget->getCustomAttribute(GLRenderTexture::CustomAttributeString_FBO, &fbo);
98 
99 		if( !fbo )
100 		{
101 			GLContext *windowContext = 0;
102 			renderTarget->getCustomAttribute( GLRenderTexture::CustomAttributeString_GLCONTEXT, &windowContext );
103 
104 			//Non-FBO targets and FBO depth surfaces don't play along, only dummies which match the same
105 			//context
106 			if( !mDepthBuffer && !mStencilBuffer && mCreatorContext == windowContext )
107 				retVal = true;
108 		}
109 		else
110 		{
111 			//Check this isn't a dummy non-FBO depth buffer with an FBO target, don't mix them.
112 			//If you don't want depth buffer, use a Null Depth Buffer, not a dummy one.
113 			if( mDepthBuffer || mStencilBuffer )
114 			{
115 				GLenum internalFormat = fbo->getFormat();
116 				GLenum depthFormat, stencilFormat;
117 				mRenderSystem->_getDepthStencilFormatFor( internalFormat, &depthFormat, &stencilFormat );
118 
119 				bool bSameDepth = false;
120 
121 				if( mDepthBuffer )
122 					bSameDepth |= mDepthBuffer->getGLFormat() == depthFormat;
123 
124 				bool bSameStencil = false;
125 
126                 if( !mStencilBuffer || mStencilBuffer == mDepthBuffer )
127                    bSameStencil = stencilFormat == GL_NONE;
128                 else
129                 {
130                     if( mStencilBuffer )
131                         bSameStencil = stencilFormat == mStencilBuffer->getGLFormat();
132                 }
133 
134 				retVal = bSameDepth && bSameStencil;
135 			}
136 		}
137 
138 		return retVal;
139 	}
140 }
141