Big frame time spike when moving camera

Started by
2 comments, last by RobM 8 years, 6 months ago

I've previously been accused of early optimisation in the game development cycle. It might feel like I'm doing something like that here, but this is really confusing me and perhaps I'm missing something obvious...

As a test, my engine is running with no visible objects, just a keyboard-controller controlling a camera entity. My camera system update code essentially checks if buttons are down (using KeyDown() at the moment) and moves the camera accordingly, it's all a bit basic at the moment - just so I can move around the world using a keyboard.

When the engine starts, my frame time eventually (after a few seconds) settles on around 0.39-0.41ms. This is fine, it's not rendering anything, it's just clearing and presenting. However, when I move the camera position, the frame time shoots up to over 2ms sometimes even higher and stays there until I stop moving. I'm doing some matrix manipulation to move the camera in the current direction and all that stuff, but that takes a negligible amount of time. After going through my method commenting out chunks of code until the frame rate stays steady, it turns out that the issue is on one line.

LVec3 position = entity->GetPosition();

// ... matrix manipulation code

entity->SetPosition(position);

I don't want to flood this post with code because I think most of it is irrelevant, but it's the SetPosition line that appears to be causing the frame time spikes. Take it out and the frame time stays steady, whether moving the camera or not.

I usually use the Control key with AWSD to move, but I removed the Control check so this update method gets called every update cycle (every physics step) and when I'm not moving the camera (i.e. changing the original value passed to SetPosition), the frame rate stays steady even though all the rest of the code is still being called. It's only when the position value actually changes that the spikes occur. The position vector is used again after the SetPosition call to determine the lookAt position, so the compiler is not optimising it out.

There is nothing in my entity code to do anything when the position changes:

void SetPosition(LVec3 vec3) { position = vec3; }

Any thoughts on where I can go next with my investigation? I can't understand why removing that SetPosition line stops the spikes (even though, of course, the camera doesn't physically move with it not there - but the view does change slightly as I'm changing the LookAt).

(Originally, my GetPosition() entity method returned a reference to the position vector to save a copy, but I removed that to simplify.)

Frustratingly, this error is sporadic, after a good 20 seconds or so, I can move around with no spikes but the spikes come back randomly. It sounds like they might not be linked to the moving code, but after dozens of tests, they definitely are.

Advertisement

After a bit more tinkering, I think this might have something to do with running the engine from within Visual Studio. Built in Release mode and running the exe as a standalone app (in full screen mode), there are no spikes, everything is smooth. Running the same exe within Visual Studio causes the spikes.

Make sure you clear any breakpoints, trace points, data breakpoints, etc. you might have set.

A single tracepoint can have observable delays of over 1ms.

Sean Middleditch – Game Systems Engineer – Join my team!

Make sure you clear any breakpoints, trace points, data breakpoints, etc. you might have set.

A single tracepoint can have observable delays of over 1ms.

Thanks - I don't have any points set anywhere in the code. I'm happy with the instability of frame time as long as it works fine standalone, as long as it's not my code! :)

This topic is closed to new replies.

Advertisement