| 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-2006 Torus Knot Software Ltd |
|---|
| 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 | You may alternatively use this source under the terms of a specific version of |
|---|
| 25 | the OGRE Unrestricted License provided you have obtained such a license from |
|---|
| 26 | Torus Knot Software Ltd. |
|---|
| 27 | ----------------------------------------------------------------------------- |
|---|
| 28 | */ |
|---|
| 29 | #include "OgreD3D9HardwareOcclusionQuery.h" |
|---|
| 30 | #include "OgreRenderSystemCapabilities.h" |
|---|
| 31 | #include "OgreException.h" |
|---|
| 32 | |
|---|
| 33 | namespace Ogre { |
|---|
| 34 | |
|---|
| 35 | /** |
|---|
| 36 | * This is a class that is the DirectX9 implementation of |
|---|
| 37 | * hardware occlusion testing. |
|---|
| 38 | * |
|---|
| 39 | * @author Lee Sandberg |
|---|
| 40 | * |
|---|
| 41 | * Updated on 12/7/2004 by Chris McGuirk |
|---|
| 42 | * Updated on 4/8/2005 by Tuan Kuranes email: tuan.kuranes@free.fr |
|---|
| 43 | */ |
|---|
| 44 | |
|---|
| 45 | /** |
|---|
| 46 | * Default object constructor |
|---|
| 47 | */ |
|---|
| 48 | D3D9HardwareOcclusionQuery::D3D9HardwareOcclusionQuery( IDirect3DDevice9* pD3DDevice ) : |
|---|
| 49 | mpDevice(pD3DDevice) |
|---|
| 50 | { |
|---|
| 51 | // create the occlusion query |
|---|
| 52 | const HRESULT hr = mpDevice->CreateQuery(D3DQUERYTYPE_OCCLUSION, &mpQuery); |
|---|
| 53 | |
|---|
| 54 | if ( hr != D3D_OK ) |
|---|
| 55 | { |
|---|
| 56 | if( D3DERR_NOTAVAILABLE == hr) |
|---|
| 57 | { |
|---|
| 58 | OGRE_EXCEPT( Exception::ERR_INTERNAL_ERROR, |
|---|
| 59 | "Cannot allocate a Hardware query. This video card doesn't supports it, sorry.", |
|---|
| 60 | "D3D9HardwareOcclusionQuery::D3D9HardwareOcclusionQuery" ); |
|---|
| 61 | } |
|---|
| 62 | } |
|---|
| 63 | } |
|---|
| 64 | |
|---|
| 65 | /** |
|---|
| 66 | * Object destructor |
|---|
| 67 | */ |
|---|
| 68 | D3D9HardwareOcclusionQuery::~D3D9HardwareOcclusionQuery() |
|---|
| 69 | { |
|---|
| 70 | SAFE_RELEASE(mpQuery); |
|---|
| 71 | } |
|---|
| 72 | |
|---|
| 73 | //------------------------------------------------------------------ |
|---|
| 74 | // Occlusion query functions (see base class documentation for this) |
|---|
| 75 | //-- |
|---|
| 76 | void D3D9HardwareOcclusionQuery::beginOcclusionQuery() |
|---|
| 77 | { |
|---|
| 78 | mpQuery->Issue(D3DISSUE_BEGIN); |
|---|
| 79 | mIsQueryResultStillOutstanding = true; |
|---|
| 80 | mPixelCount = 0; |
|---|
| 81 | } |
|---|
| 82 | |
|---|
| 83 | void D3D9HardwareOcclusionQuery::endOcclusionQuery() |
|---|
| 84 | { |
|---|
| 85 | mpQuery->Issue(D3DISSUE_END); |
|---|
| 86 | } |
|---|
| 87 | |
|---|
| 88 | //------------------------------------------------------------------ |
|---|
| 89 | bool D3D9HardwareOcclusionQuery::pullOcclusionQuery( unsigned int* NumOfFragments ) |
|---|
| 90 | { |
|---|
| 91 | // in case you didn't check if query arrived and want the result now. |
|---|
| 92 | if (mIsQueryResultStillOutstanding) |
|---|
| 93 | { |
|---|
| 94 | // Loop until the data becomes available |
|---|
| 95 | DWORD pixels; |
|---|
| 96 | const size_t dataSize = sizeof( DWORD ); |
|---|
| 97 | while (1) |
|---|
| 98 | { |
|---|
| 99 | const HRESULT hr = mpQuery->GetData((void *)&pixels, dataSize, D3DGETDATA_FLUSH); |
|---|
| 100 | |
|---|
| 101 | if (hr == S_FALSE) |
|---|
| 102 | continue; |
|---|
| 103 | if (hr == S_OK) |
|---|
| 104 | { |
|---|
| 105 | mPixelCount = pixels; |
|---|
| 106 | *NumOfFragments = pixels; |
|---|
| 107 | break; |
|---|
| 108 | } |
|---|
| 109 | if (hr == D3DERR_DEVICELOST) |
|---|
| 110 | { |
|---|
| 111 | *NumOfFragments = 100000; |
|---|
| 112 | mPixelCount = 100000; |
|---|
| 113 | mpDevice->CreateQuery(D3DQUERYTYPE_OCCLUSION, &mpQuery); |
|---|
| 114 | break; |
|---|
| 115 | } |
|---|
| 116 | } |
|---|
| 117 | mIsQueryResultStillOutstanding = false; |
|---|
| 118 | } |
|---|
| 119 | else |
|---|
| 120 | { |
|---|
| 121 | // we already stored result from last frames. |
|---|
| 122 | *NumOfFragments = mPixelCount; |
|---|
| 123 | } |
|---|
| 124 | return true; |
|---|
| 125 | } |
|---|
| 126 | //------------------------------------------------------------------ |
|---|
| 127 | bool D3D9HardwareOcclusionQuery::isStillOutstanding(void) |
|---|
| 128 | { |
|---|
| 129 | // in case you already asked for this query |
|---|
| 130 | if (!mIsQueryResultStillOutstanding) |
|---|
| 131 | return false; |
|---|
| 132 | |
|---|
| 133 | DWORD pixels; |
|---|
| 134 | const HRESULT hr = mpQuery->GetData( (void *) &pixels, sizeof( DWORD ), 0); |
|---|
| 135 | |
|---|
| 136 | if (hr == S_FALSE) |
|---|
| 137 | return true; |
|---|
| 138 | |
|---|
| 139 | if (hr == D3DERR_DEVICELOST) |
|---|
| 140 | { |
|---|
| 141 | mPixelCount = 100000; |
|---|
| 142 | mpDevice->CreateQuery(D3DQUERYTYPE_OCCLUSION, &mpQuery); |
|---|
| 143 | } |
|---|
| 144 | mPixelCount = pixels; |
|---|
| 145 | mIsQueryResultStillOutstanding = false; |
|---|
| 146 | return false; |
|---|
| 147 | |
|---|
| 148 | } |
|---|
| 149 | } |
|---|