Commit 69dc661b authored by Chuck Walbourn's avatar Chuck Walbourn
Browse files

SimpleCompute updated for better suspend handling

parent 193176cd
Loading
Loading
Loading
Loading
+1 −1
Original line number Diff line number Diff line
@@ -228,7 +228,7 @@ void DeviceResources::CreateDeviceResources()

    m_fence->SetName(L"DeviceResources");

    m_fenceEvent.Attach(CreateEvent(nullptr, FALSE, FALSE, nullptr));
    m_fenceEvent.Attach(CreateEventEx(nullptr, nullptr, 0, EVENT_MODIFY_STATE | SYNCHRONIZE));
    if (!m_fenceEvent.IsValid())
    {
        throw std::exception("CreateEvent");
+7 −0
Original line number Diff line number Diff line
@@ -68,6 +68,12 @@ namespace

        pCmdList->ResourceBarrier(1, &barrierDesc);
    }

    struct CB_FractalCS
    {
        DirectX::XMFLOAT4 MaxThreadIter;
        DirectX::XMFLOAT4 Window;
    };
}

Sample::Sample() noexcept(false) :
@@ -158,6 +164,7 @@ void Sample::Update(DX::StepTimer const& timer)
            if (pad.IsViewPressed())
            {
                ExitSample();
                m_terminateThread = true;
            }

            if ((m_gamePadButtons.a == DirectX::GamePad::ButtonStateTracker::PRESSED))
+0 −12
Original line number Diff line number Diff line
@@ -80,17 +80,6 @@ public:
    void GetDefaultSize( int& width, int& height ) const;

private:
    struct CB_FractalCS
    {
        DirectX::XMFLOAT4 MaxThreadIter;
        DirectX::XMFLOAT4 Window;
    };
    struct Vertex
    {
        DirectX::XMFLOAT4 position;
        DirectX::XMFLOAT2 texcoord;
    };

    void Update(DX::StepTimer const& timer);
    void Render();

@@ -143,7 +132,6 @@ private:

    std::atomic<uint32_t>                       m_renderIndex;          // which bank of fractal data the renderer is using, the value is either 0 or 1, compute is using the inverse

    CB_FractalCS*                               m_fractalData;          // data for compute shader constant buffer on how many iterations on the Mandelbrot set and the bounds
    DirectX::XMFLOAT4                           m_window;               // the bounds for the Mandelbrot set being calculated on the CPU
    uint32_t                                    m_fractalMaxIterations; // number of iterations when calculating the Mandelbrot set on the CPU
    std::atomic<bool>							m_windowUpdated;
+1 −1
Original line number Diff line number Diff line
@@ -266,7 +266,7 @@ void DeviceResources::CreateDeviceResources()

    m_fence->SetName(L"DeviceResources");

    m_fenceEvent.Attach(CreateEvent(nullptr, FALSE, FALSE, nullptr));
    m_fenceEvent.Attach(CreateEventEx(nullptr, nullptr, 0, EVENT_MODIFY_STATE | SYNCHRONIZE));
    if (!m_fenceEvent.IsValid())
    {
        throw std::exception("CreateEvent");
+27 −0
Original line number Diff line number Diff line
@@ -67,6 +67,12 @@ namespace

        pCmdList->ResourceBarrier(1, &barrierDesc);
    }

    struct CB_FractalCS
    {
        DirectX::XMFLOAT4 MaxThreadIter;
        DirectX::XMFLOAT4 Window;
    };
}

Sample::Sample() :
@@ -75,6 +81,8 @@ Sample::Sample() :
    m_usingAsyncCompute(false),
    m_renderIndex(0),
    m_terminateThread(false),
    m_suspendThread(false),
    m_computeThread(nullptr),
    m_fractalMaxIterations(300)
{
    // Renders only 2D, so no need for a depth buffer.
@@ -105,6 +113,10 @@ void Sample::Initialize(::IUnknown* window, int width, int height, DXGI_MODE_ROT
    m_deviceResources->CreateWindowSizeDependentResources();
    CreateWindowSizeDependentResources();

    m_computeResumeSignal.Attach(CreateEventEx(nullptr, nullptr, 0, EVENT_MODIFY_STATE | SYNCHRONIZE));
    if (!m_computeResumeSignal.IsValid())
        throw std::exception("CreateEvent");

    m_computeThread = new std::thread(&Sample::AsyncComputeThreadProc, this);

}
@@ -413,6 +425,8 @@ void Sample::OnDeactivated()

void Sample::OnSuspending()
{
    ResetEvent(m_computeResumeSignal.Get());
    m_suspendThread = true; 
}

void Sample::OnResuming()
@@ -420,6 +434,9 @@ void Sample::OnResuming()
    m_timer.ResetElapsedTime();
    m_gamePadButtons.Reset();
    m_keyboardButtons.Reset();

    m_suspendThread = false;
    SetEvent(m_computeResumeSignal.Get());
}

void Sample::OnWindowSizeChanged(int width, int height, DXGI_MODE_ROTATION rotation)
@@ -799,6 +816,11 @@ void Sample::AsyncComputeThreadProc()

    while (!m_terminateThread)
    {
        if (m_suspendThread)
        {
            (void)WaitForSingleObject(m_computeResumeSignal.Get(), INFINITE);
        }

        LARGE_INTEGER CurrentFrameTime;
        QueryPerformanceCounter(&CurrentFrameTime);
        double DeltaTime = (double)(CurrentFrameTime.QuadPart - LastFrameTime.QuadPart) / (double)PerfFreq.QuadPart;
@@ -832,6 +854,11 @@ void Sample::AsyncComputeThreadProc()
                    continue;
                }

                if (m_suspendThread)
                {
                    (void)WaitForSingleObject(m_computeResumeSignal.Get(), INFINITE);
                }

                m_computeFPS.Tick(static_cast<FLOAT>(DeltaTime));

                UpdateFractalData();
Loading