source: OGRE/trunk/ogrenew/RenderSystems/Direct3D9/src/OgreD3D9HardwareOcclusionQuery.cpp @ 657

Revision 657, 5.0 KB checked in by mattausch, 18 years ago (diff)

added ogre dependencies and patched ogre sources

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
31        /**
32        * This is a class that is the DirectX9 implementation of
33        * hardware occlusion testing.
34        *
35        * @author Lee Sandberg
36        *
37        * Updated on 12/7/2004 by Chris McGuirk
38        */
39
40        /**
41        * Default object constructor
42        */
43        D3D9HardwareOcclusionQuery::D3D9HardwareOcclusionQuery( IDirect3DDevice9* pD3DDevice )
44        {
45                mpDevice = pD3DDevice;
46                mPixelCount = 0;
47                mSkipCounter = 0;
48                mSkipInterval = 0;
49                mHasOcclusionSupport = false;
50
51                // create the occlusion query
52                HRESULT hr = mpDevice->CreateQuery(D3DQUERYTYPE_OCCLUSION, &mpQuery);
53
54                if ( hr != D3D_OK )
55                {
56                        mHasOcclusionSupport = false;
57                }
58                else
59                {
60                        mHasOcclusionSupport = true;
61                }
62        }
63
64        /**
65        * Object destructor
66        */
67        D3D9HardwareOcclusionQuery::~D3D9HardwareOcclusionQuery()
68        {
69                SAFE_RELEASE(mpQuery);
70        }
71
72        //------------------------------------------------------------------
73        // Occlusion query functions (see base class documentation for this)
74        //--
75        void D3D9HardwareOcclusionQuery::beginOcclusionQuery()
76        {
77                // Make it fail silently if hardware occlusion isn't supported
78                if(mHasOcclusionSupport)
79                {
80#ifndef GTP_VISIBILITY_MODIFIED_OGRE
81                        // Counter starts at 0 again at mSkipInterval
82                        if(mSkipCounter == mSkipInterval)
83#else
84                        if (mSkipCounter >= mSkipInterval)  // otherwise no query ever issued if mSkipInterval = 0 !
85#endif // GTP_VISIBILITY_MODIFIED_OGRE
86                        {
87                                mSkipCounter = 0;
88                        }
89                        if (mSkipCounter == 0)
90                        {
91                                mpQuery->Issue(D3DISSUE_BEGIN);
92                        }
93                }
94       
95        }
96
97        void D3D9HardwareOcclusionQuery::endOcclusionQuery()
98        {
99                // Make it fail silently if hardware occlusion isn't supported
100                if(mHasOcclusionSupport)
101                {
102                        if(mSkipCounter == 0)
103                        {
104                                mpQuery->Issue(D3DISSUE_END);
105                        }
106
107                        // The skip counter is increased
108                        mSkipCounter++;
109                }
110        }
111
112        //------------------------------------------------------------------
113        // This version of pullOcclusionQuery causes the DX9 API/Driver to not flush all commands to the 3D card
114        // to allow a fast result from the query, but the batching of API calls to the card will be normal.
115        // But the query wont be processed until the card receives the query in the next batch.
116        // Note: OpenGL dosn't use this flag at all so the application running OpenGL won't display any different behaviour.
117        //--
118        bool D3D9HardwareOcclusionQuery::pullOcclusionQuery( unsigned int* NumOfFragments, const HW_OCCLUSIONQUERY flag  )
119        {
120        HRESULT hr;
121
122                // Make it fail silently if hardware occlusion isn't supported
123                if(mHasOcclusionSupport)
124                {
125                            DWORD d3dFlags = (flag == HWOCCLUSIONQUERY_FLUSH) ? D3DGETDATA_FLUSH : 0;
126
127                        // run until success (http://www.gamedev.net/reference/programming/features/occlusionculling/page2.asp)
128                        while(hr = mpQuery->GetData( NumOfFragments, sizeof( NumOfFragments ), d3dFlags) == S_FALSE);
129
130                            mPixelCount = *NumOfFragments;
131            }
132            else
133            {
134                        // fail silently if not supported, assume visible i suppose
135                        mPixelCount = 100000;
136                }
137
138                return true;
139        }
140
141#ifdef GTP_VISIBILITY_MODIFIED_OGRE
142    //------------------------------------------------------------------
143        bool D3D9HardwareOcclusionQuery::pullOcclusionQuery( unsigned int* NumOfFragments, const bool waitForResult,
144                                                                                                             const HW_OCCLUSIONQUERY flag )
145        {
146                HRESULT hr = S_OK;
147                DWORD queryFlag = (flag == HWOCCLUSIONQUERY_FLUSH) ? D3DGETDATA_FLUSH : 0;
148
149                if (mHasOcclusionSupport)       // In case hardware occlusion isn't supported
150    {
151                        do
152    {
153                                hr = mpQuery->GetData(NumOfFragments, sizeof(NumOfFragments), queryFlag);
154                }
155                        while(waitForResult && (hr == S_FALSE));
156             
157                        mPixelCount = *NumOfFragments;
158    }
159    else
160    {
161                        mPixelCount = 100000; // every object is visible
162    }
163       
164                return hr == S_OK;
165    }
166
167
168#endif // GTP_VISIBILITY_MODIFIED_OGRE
169
170
171}
Note: See TracBrowser for help on using the repository browser.