#if XBOX
Thread.CurrentThread.SetProcessorAffinity(
new int[] { 4 });
#endif
private void StartUpdateThread()
{
// Avoid duplicate threads running.
if (!isUpdateThreadActive)
{
isUpdateThreadActive = true;
killThreads = false;
Thread threadUpdate =
new Thread(ProcessUpdateThread);
// Set to background so that when the foreground
// thread dies the background one dies as well
threadUpdate.IsBackground = true;
threadUpdate.Start();
}
}
// Runs on another hardware thread on the Xbox.
private void ProcessUpdateThread()
{
#if XBOX
Thread.CurrentThread.SetProcessorAffinity(
new int[] { 4 });
#endif
updateTime = new GameSelfTimer();
updateRandom = new Random();
// Wait a moment for everything to catch up
Thread.Sleep(5);
while (!killThreads)
{
if (readyToUpdate && !readyToDraw)
{
readyToUpdate = false;
updateTime.Update();
ProcessUpdate(updateTime);
readyToDraw = true;
}
}
isUpdateThreadActive = false;
}
- View matrix calculated from the player camera position
- Projection matrix calculated occasionally when the weapon sight zooms in
- World position of every model
- Skin transform matrix array for animations
I simply buffer those and use a boolean variable to indicate when changes are ready to update the buffers. There may be a couple of others I find in time but the above have smoothed the display.
// From the main thread
public virtual void UpdateThreadBuffers()
{
lock (lockPosition)
{
WorldPositionDraw = worldPositionBuffer;
}
}
I use a base class for all models which mean that I only had to add the Update buffers method in a very few places.
The tricky bit was making sure I called it in the correct place for all instances. That did not take as long as I expected. Especially as static models don't need any changes after they are positioned.
// From the main thread.
protected void UpdateThreadBuffers()
{
if (readyToDraw)
{
gameManager.Shading.UpdateThreadBuffers();
for (int i = 0; i < Controllers.Count; i++)
{
Controllers[i].UpdateThreadBuffers();
}
PortableItems.UpdateDroppedThreadBuffers();
// Always the last thing so update will run again
readyToDraw = false;
}
}
I also use 3D moving and animated models on some menus and option screens so I had to make changes outside of the main game to keep those screens compatible.
Job done. Loads more processing capacity for the extra collision calculations I need, good frame rate and it should make future changes much easier.
No comments:
Post a Comment