Panda3D
|
00001 // Filename: dxOcclusionQueryContext9.cxx 00002 // Created by: drose (04Jun07) 00003 // 00004 //////////////////////////////////////////////////////////////////// 00005 // 00006 // PANDA 3D SOFTWARE 00007 // Copyright (c) Carnegie Mellon University. All rights reserved. 00008 // 00009 // All use of this software is subject to the terms of the revised BSD 00010 // license. You should have received a copy of this license along 00011 // with this source code in a file named "LICENSE." 00012 // 00013 //////////////////////////////////////////////////////////////////// 00014 00015 #include "dxOcclusionQueryContext9.h" 00016 #include "dxGraphicsStateGuardian9.h" 00017 #include "pnotify.h" 00018 #include "dcast.h" 00019 #include "pStatTimer.h" 00020 00021 TypeHandle DXOcclusionQueryContext9::_type_handle; 00022 00023 //////////////////////////////////////////////////////////////////// 00024 // Function: GLOcclusionQueryContext::Destructor 00025 // Access: Public, Virtual 00026 // Description: 00027 //////////////////////////////////////////////////////////////////// 00028 DXOcclusionQueryContext9:: 00029 ~DXOcclusionQueryContext9() { 00030 _query->Release(); 00031 _query = NULL; 00032 } 00033 00034 //////////////////////////////////////////////////////////////////// 00035 // Function: GLOcclusionQueryContext::is_answer_ready 00036 // Access: Public, Virtual 00037 // Description: Returns true if the query's answer is ready, false 00038 // otherwise. If this returns false, the application 00039 // must continue to poll until it returns true. 00040 // 00041 // It is only valid to call this from the draw thread. 00042 //////////////////////////////////////////////////////////////////// 00043 bool DXOcclusionQueryContext9:: 00044 is_answer_ready() const { 00045 DWORD result; 00046 HRESULT hr = _query->GetData(&result, sizeof(result), 0); 00047 return (hr != S_FALSE); 00048 } 00049 00050 //////////////////////////////////////////////////////////////////// 00051 // Function: GLOcclusionQueryContext::waiting_for_answer 00052 // Access: Public, Virtual 00053 // Description: Requests the graphics engine to expedite the pending 00054 // answer--the application is now waiting until the 00055 // answer is ready. 00056 // 00057 // It is only valid to call this from the draw thread. 00058 //////////////////////////////////////////////////////////////////// 00059 void DXOcclusionQueryContext9:: 00060 waiting_for_answer() { 00061 get_num_fragments(); 00062 } 00063 00064 //////////////////////////////////////////////////////////////////// 00065 // Function: GLOcclusionQueryContext::get_num_fragments 00066 // Access: Public, Virtual 00067 // Description: Returns the number of fragments (pixels) of the 00068 // specified geometry that passed the depth test. 00069 // If is_answer_ready() did not return true, this 00070 // function may block before it returns. 00071 // 00072 // It is only valid to call this from the draw thread. 00073 //////////////////////////////////////////////////////////////////// 00074 int DXOcclusionQueryContext9:: 00075 get_num_fragments() const { 00076 DWORD result; 00077 HRESULT hr = _query->GetData(&result, sizeof(result), 0); 00078 if (hr == S_OK) { 00079 // The answer is ready now. 00080 return result; 00081 } 00082 00083 { 00084 // The answer is not ready; this call will block. 00085 PStatTimer timer(DXGraphicsStateGuardian9::_wait_occlusion_pcollector); 00086 while (hr == S_FALSE) { 00087 hr = _query->GetData(&result, sizeof(result), D3DGETDATA_FLUSH); 00088 } 00089 } 00090 00091 if (FAILED(hr)) { 00092 // Some failure, e.g. devicelost. Return a nonzero value as a 00093 // worst-case answer. 00094 dxgsg9_cat.info() 00095 << "occlusion query failed " << D3DERRORSTRING(hr); 00096 return 1; 00097 } 00098 00099 return result; 00100 }