1
\$\begingroup\$

I've followed https://gafferongames.com/post/fix_your_timestep/ to do interpolated physics. I have a separate thread entirely from my graphics thread. I use vulkan which allows me to do asynchronous GPU transfers, initiated from completely different threads. However I'm having trouble understanding how to effectively broadcast the associated alpha constant over to may main graphics thread.

Background

I my physics thread updates at 60hz, my framerate can be anything. There are a set of double buffered host visible, host coherent (but not device visible) buffers, there also a set of double buffered device resident buffers. Each buffer contains both previous and current data for physics for simplicity sake.

Every time my physics thread finishes a real update, it sets a flag that the data is ready to be sent off. Then outside the accumulated delta time check, my code checks if new data can be sent, and if all swapchain frames are using the old data in the device side double buffer using a timeline semaphore. It then executes a copy command on a transfer queue with an thread owned command pool a long with the relevant queue transfer buffer memory barrier, copying the data to the un-used device memory.

On the display side, each new prepared frame checks if a new physics data has been done transferring, via a separate timeline semaphore signaled via the copy command. The next command uses prepares a command buffer that tells the frame to use the new finished uploaded data if it's ready, and signals it's own timeline semaphore incrementing a value which signals how many swapchain frames are using said data. This much works without interpolation.

Problem

I was going to use an atomic variable from my physics thread to send to the drawing thread the current alpha value, as seen in the linked article. However, I realized that it is possible that the alpha value could be seen and updated before the associated sent data would be ready to consume via the draw thread, and less of a concern, after either.

Question

How do I broadcast the alpha variable properly to my physics thread? The best I could come up with was somehow using an associated counter value, which counted the number of frames, and should line up with the associated timeline semaphore value, so I add the difference between frames (the one used with the frame, and the one associated with the alpha value) and then add that to the alpha value when I finally use it in my actual shader. This may occasionally make the value for t go above 1.0 (though likely for only a very short amount of time), but it should look smooth enough to the player. Eg:

struct AlphaCount{
    double alpha;  
    std::uint64_t count; 
}

std::atomic<AlphaCount> alpha_count; 
\$\endgroup\$

0

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.