Panda3D
 All Classes Functions Variables Enumerations
dxOcclusionQueryContext9.cxx
1 // Filename: dxOcclusionQueryContext9.cxx
2 // Created by: drose (04Jun07)
3 //
4 ////////////////////////////////////////////////////////////////////
5 //
6 // PANDA 3D SOFTWARE
7 // Copyright (c) Carnegie Mellon University. All rights reserved.
8 //
9 // All use of this software is subject to the terms of the revised BSD
10 // license. You should have received a copy of this license along
11 // with this source code in a file named "LICENSE."
12 //
13 ////////////////////////////////////////////////////////////////////
14 
15 #include "dxOcclusionQueryContext9.h"
16 #include "dxGraphicsStateGuardian9.h"
17 #include "pnotify.h"
18 #include "dcast.h"
19 #include "pStatTimer.h"
20 
21 TypeHandle DXOcclusionQueryContext9::_type_handle;
22 
23 ////////////////////////////////////////////////////////////////////
24 // Function: GLOcclusionQueryContext::Destructor
25 // Access: Public, Virtual
26 // Description:
27 ////////////////////////////////////////////////////////////////////
28 DXOcclusionQueryContext9::
29 ~DXOcclusionQueryContext9() {
30  _query->Release();
31  _query = NULL;
32 }
33 
34 ////////////////////////////////////////////////////////////////////
35 // Function: GLOcclusionQueryContext::is_answer_ready
36 // Access: Public, Virtual
37 // Description: Returns true if the query's answer is ready, false
38 // otherwise. If this returns false, the application
39 // must continue to poll until it returns true.
40 //
41 // It is only valid to call this from the draw thread.
42 ////////////////////////////////////////////////////////////////////
44 is_answer_ready() const {
45  DWORD result;
46  HRESULT hr = _query->GetData(&result, sizeof(result), 0);
47  return (hr != S_FALSE);
48 }
49 
50 ////////////////////////////////////////////////////////////////////
51 // Function: GLOcclusionQueryContext::waiting_for_answer
52 // Access: Public, Virtual
53 // Description: Requests the graphics engine to expedite the pending
54 // answer--the application is now waiting until the
55 // answer is ready.
56 //
57 // It is only valid to call this from the draw thread.
58 ////////////////////////////////////////////////////////////////////
62 }
63 
64 ////////////////////////////////////////////////////////////////////
65 // Function: GLOcclusionQueryContext::get_num_fragments
66 // Access: Public, Virtual
67 // Description: Returns the number of fragments (pixels) of the
68 // specified geometry that passed the depth test.
69 // If is_answer_ready() did not return true, this
70 // function may block before it returns.
71 //
72 // It is only valid to call this from the draw thread.
73 ////////////////////////////////////////////////////////////////////
76  DWORD result;
77  HRESULT hr = _query->GetData(&result, sizeof(result), 0);
78  if (hr == S_OK) {
79  // The answer is ready now.
80  return result;
81  }
82 
83  {
84  // The answer is not ready; this call will block.
85  PStatTimer timer(DXGraphicsStateGuardian9::_wait_occlusion_pcollector);
86  while (hr == S_FALSE) {
87  hr = _query->GetData(&result, sizeof(result), D3DGETDATA_FLUSH);
88  }
89  }
90 
91  if (FAILED(hr)) {
92  // Some failure, e.g. devicelost. Return a nonzero value as a
93  // worst-case answer.
94  dxgsg9_cat.info()
95  << "occlusion query failed " << D3DERRORSTRING(hr);
96  return 1;
97  }
98 
99  return result;
100 }
virtual void waiting_for_answer()
Requests the graphics engine to expedite the pending answer–the application is now waiting until the ...
A lightweight class that can be used to automatically start and stop a PStatCollector around a sectio...
Definition: pStatTimer.h:34
virtual bool is_answer_ready() const
Returns true if the query&#39;s answer is ready, false otherwise.
virtual int get_num_fragments() const
Returns the number of fragments (pixels) of the specified geometry that passed the depth test...
TypeHandle is the identifier used to differentiate C++ class types.
Definition: typeHandle.h:85