1 | /*
|
---|
2 | -----------------------------------------------------------------------------
|
---|
3 | This source file is part of OGRE
|
---|
4 | (Object-oriented Graphics Rendering Engine)
|
---|
5 | For the latest info, see http://ogre.sourceforge.net/
|
---|
6 |
|
---|
7 | Copyright (c) 2000-2005 The OGRE Team
|
---|
8 | Also see acknowledgements in Readme.html
|
---|
9 |
|
---|
10 | This program is free software; you can redistribute it and/or modify it under
|
---|
11 | the terms of the GNU Lesser General Public License as published by the Free Software
|
---|
12 | Foundation; either version 2 of the License, or (at your option) any later
|
---|
13 | version.
|
---|
14 |
|
---|
15 | This program is distributed in the hope that it will be useful, but WITHOUT
|
---|
16 | ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
|
---|
17 | FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
|
---|
18 |
|
---|
19 | You should have received a copy of the GNU Lesser General Public License along with
|
---|
20 | this program; if not, write to the Free Software Foundation, Inc., 59 Temple
|
---|
21 | Place - Suite 330, Boston, MA 02111-1307, USA, or go to
|
---|
22 | http://www.gnu.org/copyleft/lesser.txt.
|
---|
23 | -----------------------------------------------------------------------------
|
---|
24 | */
|
---|
25 | #include "OgreD3D9HardwareOcclusionQuery.h"
|
---|
26 | #include "OgreRenderSystemCapabilities.h"
|
---|
27 | #include "OgreException.h"
|
---|
28 |
|
---|
29 | namespace 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 | * Updated on 4/8/2005 by Tuan Kuranes email: tuan.kuranes@free.fr
|
---|
39 | */
|
---|
40 |
|
---|
41 | /**
|
---|
42 | * Default object constructor
|
---|
43 | */
|
---|
44 | D3D9HardwareOcclusionQuery::D3D9HardwareOcclusionQuery( IDirect3DDevice9* pD3DDevice ) :
|
---|
45 | mpDevice(pD3DDevice)
|
---|
46 | {
|
---|
47 | // create the occlusion query
|
---|
48 | const HRESULT hr = mpDevice->CreateQuery(D3DQUERYTYPE_OCCLUSION, &mpQuery);
|
---|
49 |
|
---|
50 | if ( hr != D3D_OK )
|
---|
51 | {
|
---|
52 | if( D3DERR_NOTAVAILABLE == hr)
|
---|
53 | {
|
---|
54 | OGRE_EXCEPT( Exception::ERR_INTERNAL_ERROR,
|
---|
55 | "Cannot allocate a Hardware query. This video card doesn't supports it, sorry.",
|
---|
56 | "D3D9HardwareOcclusionQuery::D3D9HardwareOcclusionQuery" );
|
---|
57 | }
|
---|
58 | }
|
---|
59 | }
|
---|
60 |
|
---|
61 | /**
|
---|
62 | * Object destructor
|
---|
63 | */
|
---|
64 | D3D9HardwareOcclusionQuery::~D3D9HardwareOcclusionQuery()
|
---|
65 | {
|
---|
66 | SAFE_RELEASE(mpQuery);
|
---|
67 | }
|
---|
68 |
|
---|
69 | //------------------------------------------------------------------
|
---|
70 | // Occlusion query functions (see base class documentation for this)
|
---|
71 | //--
|
---|
72 | void D3D9HardwareOcclusionQuery::beginOcclusionQuery()
|
---|
73 | {
|
---|
74 | mpQuery->Issue(D3DISSUE_BEGIN);
|
---|
75 | mIsQueryResultStillOutstanding = true;
|
---|
76 | mPixelCount = 0;
|
---|
77 | }
|
---|
78 |
|
---|
79 | void D3D9HardwareOcclusionQuery::endOcclusionQuery()
|
---|
80 | {
|
---|
81 | mpQuery->Issue(D3DISSUE_END);
|
---|
82 | }
|
---|
83 |
|
---|
84 | //------------------------------------------------------------------
|
---|
85 | bool D3D9HardwareOcclusionQuery::pullOcclusionQuery( unsigned int* NumOfFragments )
|
---|
86 | {
|
---|
87 | // in case you didn't check if query arrived and want the result now.
|
---|
88 | if (mIsQueryResultStillOutstanding)
|
---|
89 | {
|
---|
90 | // Loop until the data becomes available
|
---|
91 | DWORD pixels;
|
---|
92 | const size_t dataSize = sizeof( DWORD );
|
---|
93 | while (1)
|
---|
94 | {
|
---|
95 | const HRESULT hr = mpQuery->GetData((void *)&pixels, dataSize, D3DGETDATA_FLUSH);
|
---|
96 |
|
---|
97 | if (hr == S_FALSE)
|
---|
98 | continue;
|
---|
99 | if (hr == S_OK)
|
---|
100 | {
|
---|
101 | mPixelCount = pixels;
|
---|
102 | *NumOfFragments = pixels;
|
---|
103 | break;
|
---|
104 | }
|
---|
105 | if (hr == D3DERR_DEVICELOST)
|
---|
106 | {
|
---|
107 | *NumOfFragments = 100000;
|
---|
108 | mPixelCount = 100000;
|
---|
109 | mpDevice->CreateQuery(D3DQUERYTYPE_OCCLUSION, &mpQuery);
|
---|
110 | break;
|
---|
111 | }
|
---|
112 | }
|
---|
113 | mIsQueryResultStillOutstanding = false;
|
---|
114 | }
|
---|
115 | else
|
---|
116 | {
|
---|
117 | // we already stored result from last frames.
|
---|
118 | *NumOfFragments = mPixelCount;
|
---|
119 | }
|
---|
120 | return true;
|
---|
121 | }
|
---|
122 | //------------------------------------------------------------------
|
---|
123 | bool D3D9HardwareOcclusionQuery::isStillOutstanding(void)
|
---|
124 | {
|
---|
125 | // in case you already asked for this query
|
---|
126 | if (!mIsQueryResultStillOutstanding)
|
---|
127 | return false;
|
---|
128 |
|
---|
129 | DWORD pixels;
|
---|
130 | const HRESULT hr = mpQuery->GetData( (void *) &pixels, sizeof( DWORD ), D3DGETDATA_FLUSH);
|
---|
131 |
|
---|
132 | if (hr == S_FALSE)
|
---|
133 | return true;
|
---|
134 |
|
---|
135 | if (hr == D3DERR_DEVICELOST)
|
---|
136 | {
|
---|
137 | mPixelCount = 100000;
|
---|
138 | mpDevice->CreateQuery(D3DQUERYTYPE_OCCLUSION, &mpQuery);
|
---|
139 | }
|
---|
140 | mPixelCount = pixels;
|
---|
141 | mIsQueryResultStillOutstanding = false;
|
---|
142 | return false;
|
---|
143 |
|
---|
144 | }
|
---|
145 | }
|
---|