First up the best place to start is the MSDN docs about GPU queries
The basic idea is to wrap your calls in a queries begin and end functions and then wait a few frames to read the info back from the GPU.
I haven't done much query work in DX9 so I cant help much more than showing you how I setup DX11 occlusion queries.
Best bet would be to do this in a none blocking way something like this every frame to check if the query has returned
//Setup Query
Dim qdesc As QueryDescription
qdesc.Flags = QueryFlags.None
qdesc.Type = QueryType.Occlusion
g_pPredicate = New Query(_device, qdesc)
//Running the Query
_context.ImmediateContext.Begin(g_pPredicate)
BoundingBoxRenderer.DrawEX(_device, Bounding, _camera, Color.Red, _context)
_context.ImmediateContext.End(g_pPredicate)
// Checking If The Query Has Returned
HasData = _context.IsDataAvailable(g_pPredicate)
If HasData then
return pPredicate
end if
//Using The Query Data
If HasData Then
Dim value As UInt64 = _context.ImmediateContext.GetData(Of UInt64)(g_pPredicate)
If value > 0 Then
IsVisible = True
Else
IsVisible = False
End If
HasData = False
End If
There are several types of queries which are designed to query the status of resources. The status of a given resource includes graphics processing unit (GPU) status, driver status, or runtime status. To understand the difference between the different query types, you need to understand the query states. The following state transition diagram explains each of the query states.
The diagram shows three states, each defined by circles. Each of the solid lines are application-driven events that cause a state transition. The dashed line is a resource-driven event that switches a query from the issued state to the signaled state. Each of these states has a different purpose:
•The signaled state is like an idle state. The query object has been generated and is waiting for the application to issue the query. Once a query has completed and transitioned back to the signaled state, the answer to the query can be retrieved.
•The building state is like a staging area for a query. From the building state, a query has been issued (by calling D3DISSUE_BEGIN) but has not yet transitioned to the issued state. When an application issues a query end (by calling D3DISSUE_END), the query transitions to the issued state.
•The issued state means that the resource being queried has control of the query. Once the resource finishes its work, the resource transitions the state machine to the signaled state. During the issued state, the application must poll to detect the transition to the signaled state. Once the transition to the signaled state occurs, GetData returns the query result (through an argument) to the application.