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 "OgreGLESDepthBuffer.h"
29 #include "OgreGLESHardwarePixelBuffer.h"
30 #include "OgreGLESRenderSystem.h"
31 #include "OgreGLESFrameBufferObject.h"
32 
33 namespace Ogre
34 {
GLESDepthBuffer(uint16 poolId,GLESRenderSystem * renderSystem,GLESContext * creatorContext,GLESRenderBuffer * depth,GLESRenderBuffer * stencil,uint32 width,uint32 height,uint32 fsaa,uint32 multiSampleQuality,bool isManual)35 	GLESDepthBuffer::GLESDepthBuffer( uint16 poolId, GLESRenderSystem *renderSystem, GLESContext *creatorContext,
36 									GLESRenderBuffer *depth, GLESRenderBuffer *stencil,
37 									uint32 width, uint32 height, uint32 fsaa, uint32 multiSampleQuality,
38 									bool isManual ) :
39 				DepthBuffer( poolId, 0, width, height, fsaa, "", isManual ),
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 #if GL_OES_framebuffer_object
51 			case GL_DEPTH_COMPONENT16_OES:
52 				mBitDepth = 16;
53 				break;
54 #endif
55 
56 #if GL_OES_packed_depth_stencil
57             case GL_DEPTH24_STENCIL8_OES:  // Packed depth / stencil
58 #endif
59 #if GL_OES_depth24
60 			case GL_DEPTH_COMPONENT24_OES:
61 				mBitDepth = 32;
62 				break;
63 #endif
64 			}
65 		}
66 	}
67 
~GLESDepthBuffer()68 	GLESDepthBuffer::~GLESDepthBuffer()
69 	{
70 		if( mStencilBuffer && mStencilBuffer != mDepthBuffer )
71 		{
72 			delete mStencilBuffer;
73 			mStencilBuffer = 0;
74 		}
75 
76 		if( mDepthBuffer )
77 		{
78 			delete mDepthBuffer;
79 			mDepthBuffer = 0;
80 		}
81 	}
82 	//---------------------------------------------------------------------
isCompatible(RenderTarget * renderTarget) const83 	bool GLESDepthBuffer::isCompatible( RenderTarget *renderTarget ) const
84 	{
85 		bool retVal = false;
86 
87 		//Check standard stuff first.
88 		if( mRenderSystem->getCapabilities()->hasCapability( RSC_RTT_DEPTHBUFFER_RESOLUTION_LESSEQUAL ) )
89 		{
90 			if( !DepthBuffer::isCompatible( renderTarget ) )
91 				return false;
92 		}
93 		else
94 		{
95 			if( this->getWidth() != renderTarget->getWidth() ||
96 				this->getHeight() != renderTarget->getHeight() ||
97 				this->getFsaa() != renderTarget->getFSAA() )
98 					return false;
99 		}
100 
101 		//Now check this is the appropriate format
102 		GLESFrameBufferObject *fbo = 0;
103         renderTarget->getCustomAttribute("FBO", &fbo);
104 
105 		if( !fbo )
106 		{
107 			GLESContext *windowContext = 0;
108 			renderTarget->getCustomAttribute( "GLCONTEXT", &windowContext );
109 
110 			//Non-FBO targets and FBO depth surfaces don't play along, only dummies which match the same
111 			//context
112 			if( !mDepthBuffer && !mStencilBuffer && mCreatorContext == windowContext )
113 				retVal = true;
114 		}
115 		else
116 		{
117 			//Check this isn't a dummy non-FBO depth buffer with an FBO target, don't mix them.
118 			//If you don't want depth buffer, use a Null Depth Buffer, not a dummy one.
119 			if( mDepthBuffer || mStencilBuffer )
120 			{
121 				GLenum internalFormat = fbo->getFormat();
122 				GLenum depthFormat, stencilFormat;
123 				mRenderSystem->_getDepthStencilFormatFor( internalFormat, &depthFormat, &stencilFormat );
124 
125 				bool bSameDepth = false;
126 
127 				if( mDepthBuffer )
128 					bSameDepth |= mDepthBuffer->getGLFormat() == depthFormat;
129 
130 				bool bSameStencil = false;
131 
132                 if( !mStencilBuffer || mStencilBuffer == mDepthBuffer )
133                    bSameStencil = stencilFormat == GL_NONE;
134                 else
135                 {
136                     if( mStencilBuffer )
137                         bSameStencil = stencilFormat == mStencilBuffer->getGLFormat();
138                 }
139 
140 				retVal = bSameDepth && bSameStencil;
141 			}
142 		}
143 
144 		return retVal;
145 	}
146 }
147