highfidelity/hifi — pull request #1901 by ZappoMan (2014-02-06 10:28:38+00:00)
Title: fix stutter while spinning

I am 99.9% certain this fill fix Philip's "stutter while spinning" bug (closes #1897)

Here's what I've found, the stutter is caused by waiting on a mutex for the VoxelTree embedded in the VoxelSystem. The update operation will attempt to do ray intersections on the voxels as well as sphere and capsule penetrations on the voxels. These operations were sometimes getting blocked while they waited to get the mutex that was busy because of the hideShow thread.

So, to get the stutter you needed to have a local tree that was complex enough that the recursing of it for the hide/show voxel operations would be somewhat expensive (more than 1 frame) and since this operation was locking the entire tree, the other operations (like ray intersect or capsule penetration) had to wait.

The fix is two fold...

1) Switch to using Read/Write Locks, which allow multiple readers to access the tree at the same time - and only really use a write lock when there's the potential for the tree structure to be changed.

2) Switch the intersect and penetration operations to use tryLocks() and to bail with "false" if they are unable to obtain a lock. This works for our use case because we can be lazy about these particular operations... it's ok if we can't ray pick a voxel for a frame or two... it's ok if we allow our avatar to potentially penetrate a voxel while we're updating the scene with correct voxels.

Tested with the repro case of moving to the center of the city in HQ (3500, 450, 2500) and spinning around until I got at least 400k local voxels, then moving to near origin (70, 23, 73) and spinning around. This case would repro this stutter 100% of the time on my Mavericks mac. With this change, this test case will result in no stutter and solid 59fps rendering for the entire test case.