source: trunk/VUT/work/ogre_changes/RenderSystems/Direct3D9/src/OgreD3D9HardwareOcclusionQuery.cpp @ 115

Revision 115, 5.1 KB checked in by mattausch, 19 years ago (diff)

added depth pass algorithm + delayed transparent object rendering (so depth ordering is right)

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  */
43D3D9HardwareOcclusionQuery::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  */
67D3D9HardwareOcclusionQuery::~D3D9HardwareOcclusionQuery()
68{
69                SAFE_RELEASE(mpQuery);
70}
71
72//------------------------------------------------------------------
73// Occlusion query functions (see base class documentation for this)
74//--
75void 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                        //std::stringstream d; d << "count: " << mSkipCounter;
90                        //LogManager::getSingleton().logMessage(d.str());
91                        if (mSkipCounter == 0)
92                {
93                                mpQuery->Issue(D3DISSUE_BEGIN);
94                }
95        }
96       
97}
98
99        void D3D9HardwareOcclusionQuery::endOcclusionQuery()
100        {
101                // Make it fail silently if hardware occlusion isn't supported
102                if(mHasOcclusionSupport)
103                        {
104                        if(mSkipCounter == 0)
105                        {
106                                mpQuery->Issue(D3DISSUE_END);
107        }
108
109                        // The skip counter is increased
110                        mSkipCounter++;
111        }
112}
113
114//------------------------------------------------------------------
115        // This version of pullOcclusionQuery causes the DX9 API/Driver to not flush all commands to the 3D card
116// to allow a fast result from the query, but the batching of API calls to the card will be normal.
117        // But the query wont be processed until the card recives the query in the next batch.
118// Note: OpenGL dosn't use this flag at all so the application running OpenGL won't display any different behaviour.
119//--
120bool D3D9HardwareOcclusionQuery::pullOcclusionQuery( unsigned int* NumOfFragments, const HW_OCCLUSIONQUERY flag  )
121{
122        HRESULT hr;
123
124                // Make it fail silently if hardware occlusion isn't supported
125                if(mHasOcclusionSupport)
126        {
127                        DWORD d3dFlags = (flag == HWOCCLUSIONQUERY_FLUSH) ? D3DGETDATA_FLUSH : 0;
128       
129                        // run until success (http://www.gamedev.net/reference/programming/features/occlusionculling/page2.asp)
130                        while(hr = mpQuery->GetData( NumOfFragments, sizeof( NumOfFragments ), d3dFlags) == S_FALSE);
131
132                        mPixelCount = *NumOfFragments;
133                        }
134                        else
135                        {
136                        // fail silently if not supported, assume visible i suppose
137                        mPixelCount = 100000;
138                        }
139
140                return true;
141        }
142
143#ifdef GTP_VISIBILITY_MODIFIED_OGRE
144//------------------------------------------------------------------
145        bool D3D9HardwareOcclusionQuery::pullOcclusionQuery( unsigned int* NumOfFragments, const bool waitForResult,
146                                                                                                             const HW_OCCLUSIONQUERY flag )
147        {
148                HRESULT hr = S_OK;
149                DWORD queryFlag = (flag == HWOCCLUSIONQUERY_FLUSH) ? D3DGETDATA_FLUSH : 0;
150
151                if (mHasOcclusionSupport)       // In case hardware occlusion isn't supported
152                {       
153                        do
154                        {
155                                hr = mpQuery->GetData(NumOfFragments, sizeof(NumOfFragments), queryFlag);
156                }
157                        while(waitForResult && (hr == S_FALSE));
158               
159                        mPixelCount = *NumOfFragments;
160                }
161                else
162                {
163                        mPixelCount = 100000; // every object is visible
164                }
165       
166                return hr == S_OK;
167        }
168
169
170#endif // GTP_VISIBILITY_MODIFIED_OGRE
171
172
173}
Note: See TracBrowser for help on using the repository browser.