int
D3DOverdrawWindow::
Loop(void)
{
    int nOverdraw = 0;
    IDirect3DQuery9* pOcclusionQuery;
    DWORD numberOfPixelsDrawn;
    d3d->CreateQuery(D3DQUERYTYPE_OCCLUSION, &pOcclusionQuery);

    for (unsigned int iViewpoint = 0; iViewpoint < m_nViewpointCount;
         iViewpoint++)
    {
        // Add an end marker to the command buffer queue.
        pOcclusionQuery->Issue(D3DISSUE_BEGIN);
        // Draw scene
        SetupDraw(iViewpoint);
        DrawModel();
        d3d->EndScene();
        d3d->Present(NULL, NULL, NULL, NULL);
        // Add an end marker to the command buffer queue.
        pOcclusionQuery->Issue(D3DISSUE_END);

        // Force the driver to execute the commands from the command buffer.
        // Empty the command buffer and wait until the GPU is idle.
        while (S_FALSE == pOcclusionQuery->GetData(&numberOfPixelsDrawn,
                                                   sizeof(DWORD), D3DGETDATA_FLUSH))
            ;

        nOverdraw += numberOfPixelsDrawn;
    }

    pOcclusionQuery->Release();
    return nOverdraw;
}
GraphicsManager::GraphicsManager()
{ // Initialises PSGL for to use for drawing and translating, sets up the draw settings
  // then creates the light and HUD.
  InitialisePSGL();
  SetupDraw();
  CreateLight();
  pHUD = new HUD();
} // Constructor()
bool
D3DOverdrawWindow::
NormalizedLoop(float& fOverdraw, float& fOverdrawMax)
{
    int nOverdraw = 0;
    int nOverdrawPix = 0;
    fOverdrawMax = 0;
    DWORD numberOfPixelsDrawn;
    DWORD numberOfPixelsDrawnPix;
    SetupDraw(0);

    for (unsigned int iViewpoint = 0; iViewpoint < m_nViewpointCount; iViewpoint += NUM_QUERIES)
    {
        for (unsigned int j = 0; j < NUM_QUERIES && iViewpoint + j < m_nViewpointCount; j++)
        {
            SetupView(iViewpoint + j);
            SetupTransforms();
            d3d->Clear(0, NULL,  D3DCLEAR_ZBUFFER,
                       D3DCOLOR_XRGB(0, 0, 0), 1.0f, 0);
            // Add an end marker to the command buffer queue.
            m_pOcclusionQuery[j]->Issue(D3DISSUE_BEGIN);

            // Draw scene
            if (FAILED(DrawModel()))
            {
                return false;
            }

            // Add an end marker to the command buffer queue.
            m_pOcclusionQuery[j]->Issue(D3DISSUE_END);

            // Add an end marker to the command buffer queue.
            d3d->SetRenderState(D3DRS_ZFUNC, D3DCMP_EQUAL);
            m_pOcclusionQueryPix[j]->Issue(D3DISSUE_BEGIN);

            // Draw scene
            if (FAILED(DrawModel()))
            {
                return false;
            }

            // Add an end marker to the command buffer queue.
            m_pOcclusionQueryPix[j]->Issue(D3DISSUE_END);
            d3d->SetRenderState(D3DRS_ZFUNC, D3DCMP_LESS);
        }

        for (unsigned int j = 0; j < NUM_QUERIES && iViewpoint + j < m_nViewpointCount; j++)
        {
            // Force the driver to execute the commands from the command buffer.
            // Empty the command buffer and wait until the GPU is idle.
            while (S_FALSE == m_pOcclusionQuery[j]->GetData(&numberOfPixelsDrawn,
                                                            sizeof(DWORD), D3DGETDATA_FLUSH))
                ;

            nOverdraw += numberOfPixelsDrawn;

            while (S_FALSE == m_pOcclusionQueryPix[j]->GetData(&numberOfPixelsDrawnPix,
                                                               sizeof(DWORD), D3DGETDATA_FLUSH))
                ;

            nOverdrawPix += numberOfPixelsDrawnPix;

            if (numberOfPixelsDrawnPix > 0)
            {
                fOverdrawMax = max(fOverdrawMax, numberOfPixelsDrawn / (float)numberOfPixelsDrawnPix);
            }
        }
    }

    if (nOverdrawPix > 0)
    {
        fOverdraw = nOverdraw / (float)nOverdrawPix;
    }
    else
    {
        fOverdraw = 0;
    }

    return true;
}
int
D3DOverdrawWindow::
Loop(int iClusterA, int iClusterB)
{
    int nOverdraw = 0;
    DWORD numberOfPixelsDrawn;
    int j = 0;
    SetupDraw(0);

    for (unsigned int iViewpoint = 0; iViewpoint < m_nViewpointCount; iViewpoint++)
    {
        SetupView(iViewpoint);
        m_iTested++;

        if (!CheckIsect(iClusterA, iClusterB))
        {
            continue;
        }

        m_iRendered++;
        SetupTransforms();
        d3d->Clear(0, NULL,  D3DCLEAR_ZBUFFER,
                   D3DCOLOR_XRGB(0, 0, 0), 1.0f, 0);

        //Add this to remove triple dependencies
        //DrawComplement(iClusterA, iClusterB);

        // Add an end marker to the command buffer queue.
        m_pOcclusionQuery[j]->Issue(D3DISSUE_BEGIN);
        // Draw scene
        DrawCluster(iClusterA);
        DrawCluster(iClusterB);
        // Add an end marker to the command buffer queue.
        m_pOcclusionQuery[j]->Issue(D3DISSUE_END);
        // Force the driver to execute the commands from the command buffer.
        // Empty the command buffer and wait until the GPU is idle.
        j++;

        if (j == NUM_QUERIES)
        {
            for (j = 0; j < NUM_QUERIES; j++)
            {
                while (S_FALSE == m_pOcclusionQuery[j]->GetData(&numberOfPixelsDrawn,
                                                                sizeof(DWORD), D3DGETDATA_FLUSH))
                    ;

                nOverdraw += numberOfPixelsDrawn;
            }

            j = 0;
        }
    }

    int jnum = j;

    for (int j = 0; j < jnum; j++)
    {
        while (S_FALSE == m_pOcclusionQuery[j]->GetData(&numberOfPixelsDrawn,
                                                        sizeof(DWORD), D3DGETDATA_FLUSH))
            ;

        nOverdraw += numberOfPixelsDrawn;
    }

    return nOverdraw;
}