source: trunk/VUT/work/ogre_changes/RenderSystem/Direct3D9/src/OgreD3D9HardwareOcclusionQuery.cpp @ 61

Revision 61, 5.9 KB checked in by mattausch, 19 years ago (diff)
Line 
1/*
2-----------------------------------------------------------------------------
3This source file is part of OGRE
4    (Object-oriented Graphics Rendering Engine)
5For the latest info, see http://ogre.sourceforge.net/
6
7Copyright (c) 2000-2005 The OGRE Team
8Also see acknowledgements in Readme.html
9
10This program is free software; you can redistribute it and/or modify it under
11the terms of the GNU Lesser General Public License as published by the Free Software
12Foundation; either version 2 of the License, or (at your option) any later
13version.
14
15This program is distributed in the hope that it will be useful, but WITHOUT
16ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
17FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
18
19You should have received a copy of the GNU Lesser General Public License along with
20this program; if not, write to the Free Software Foundation, Inc., 59 Temple
21Place - Suite 330, Boston, MA 02111-1307, USA, or go to
22http://www.gnu.org/copyleft/lesser.txt.
23-----------------------------------------------------------------------------
24*/
25#include "OgreD3D9HardwareOcclusionQuery.h"
26#include "OgreRenderSystemCapabilities.h"
27#include "OgreException.h"
28
29namespace Ogre {
30
31int D3D9HardwareOcclusionQuery::m_Skip = 0;
32
33/**
34  * This is a class that is the DirectX9 implementation of
35  * hardware occlusion testing.
36  *
37  * @author Lee Sandberg
38  */
39
40/**
41  * Default object constructor
42  *
43  */
44D3D9HardwareOcclusionQuery::D3D9HardwareOcclusionQuery( IDirect3DDevice9* pD3DDevice )
45{
46        m_pD3DDevice = pD3DDevice;
47        m_uintPixelCount = 0;
48        m_SkipCounter = 0;
49        m_bHWOcclusionSupport = false;
50
51        HRESULT hr = m_pD3DDevice->CreateQuery(D3DQUERYTYPE_OCCLUSION, &m_pD3DQuery);
52        if ( hr != D3D_OK )
53        {
54                //OGRE_EXCEPT(hr, "D3D9HardwareOcclusionQuery couldn't create hardware occlusion query object.",
55        //        "D3D9HardwareOcclusionQuery::D3D9HardwareOcclusionQuery");
56                m_bHWOcclusionSupport = false;
57        }
58        else
59        {
60                m_bHWOcclusionSupport = true;
61        }
62}
63
64
65/**
66  * Object destructor
67  */
68D3D9HardwareOcclusionQuery::~D3D9HardwareOcclusionQuery()
69{
70        SAFE_RELEASE( m_pD3DQuery );
71}
72
73//------------------------------------------------------------------
74// Occlusion query functions (see base class documentation for this)
75//--
76void D3D9HardwareOcclusionQuery::beginOcclusionQuery()
77{
78        if( m_bHWOcclusionSupport )             // Make it fail silently if hardware occlusion isn't supported
79        {
80                if  ( m_SkipCounter ==  m_Skip ) { m_SkipCounter = 0; };                // Counter starts at 0 again at m_Skip
81               
82                if ( m_SkipCounter == 0 && m_uintPixelCount != 0 )      // New or none visable objects must allways be tested but visable objects can be skiped
83                {
84                        m_pD3DQuery->Issue(D3DISSUE_BEGIN);
85                }
86        }
87}
88       
89void D3D9HardwareOcclusionQuery::endOcclusionQuery()
90{
91        if( m_bHWOcclusionSupport )     // Make it fail silently if hardware occlusion isn't supported
92        {
93                if (m_SkipCounter == 0 && m_uintPixelCount != 0 ) // New or none visable objects must allways be tested but visable objects can be skiped
94                {
95                        m_pD3DQuery->Issue(D3DISSUE_END);
96                }
97                m_SkipCounter++;                                                                 // The skip counter is increased
98        }
99}
100
101//------------------------------------------------------------------
102// This version of pullOcclusionQuery cases the DX9 API/Driver to flush all commands to the 3D card
103// to allow a fast result from the query, but at the cost of poorer batching of API calls to the card.
104// Note: OpenGL dosn't use this flag at all so the application running OpenGL won't display any different behaviour.
105//--
106bool D3D9HardwareOcclusionQuery::pullOcclusionQuery( unsigned int* NumOfFragments)
107{
108        HRESULT hr;
109       
110        if( m_bHWOcclusionSupport )     // Make it fail silently if hardware occlusion isn't supported
111        {
112                hr = m_pD3DQuery->GetData( NumOfFragments, sizeof( NumOfFragments ), D3DGETDATA_FLUSH );
113
114                if ( hr != S_OK )
115                        {
116                                return false;
117                        }
118                        else
119                        {
120                                m_uintPixelCount = *NumOfFragments;
121                                return true;
122                        }
123        }
124        else
125        {
126                m_uintPixelCount = 100000; // Fails quitlly if hardware occlusion is not supported - every object is visable
127                return true;
128        }
129}
130
131//------------------------------------------------------------------
132// This version of pullOcclusionQuery cases the DX9 API/Driver to not flush all commands to the 3D card
133// to allow a fast result from the query, but the batching of API calls to the card will be normal.
134// But the query wont be processed until the card recives the query in the nexr batch.
135// Note: OpenGL dosn't use this flag at all so the application running OpenGL won't display any different behaviour.
136//--
137bool D3D9HardwareOcclusionQuery::pullOcclusionQuery( unsigned int* NumOfFragments, const HW_OCCLUSIONQUERY flag  )
138{
139        HRESULT hr;
140
141        // TO DO: USE lockOpts= D3D9Mappings::get(options); instead of RS_OCCLUSIONQUERY enum
142
143        if( m_bHWOcclusionSupport )     // Make it fail silently if hardware occlusion isn't supported
144        {
145       
146                switch( flag )
147                {
148                        case HWOCCLUSIONQUERY_FLUSH :   
149                                hr = m_pD3DQuery->GetData( NumOfFragments, sizeof( NumOfFragments ), D3DGETDATA_FLUSH );
150                        break;
151
152                        case HWOCCLUSIONQUERY_NOFLUSH :
153                                hr = m_pD3DQuery->GetData( NumOfFragments, sizeof( NumOfFragments ), 0 );
154                        break;
155                };
156
157
158
159                if ( hr != S_OK )
160                        {
161                                return false;
162                        }
163                        else
164                        {
165                                m_uintPixelCount = *NumOfFragments;
166                                return true;
167                        }
168        }
169        else
170        {
171                m_uintPixelCount = 100000; // Fails quitlly if hardware occlusion is not supported - every object is visable
172                return true;
173        }
174}
175
176
177#ifdef GTP_VISIBILITY_MODIFIED_OGRE
178
179bool D3D9HardwareOcclusionQuery::resultAvailable()
180{
181        bool result = true;
182
183        if( m_bHWOcclusionSupport )     // Make it fail silently if hardware occlusion isn't supported
184        {       //TODO: implement
185                //glGetOcclusionQueryuivNV_ptr( m_uintQuery[0], GL_PIXEL_COUNT_NV, &result );
186        }
187
188        return result;
189}
190
191#endif // GTP_VISIBILITY_MODIFIED_OGRE
192
193}
Note: See TracBrowser for help on using the repository browser.