Skip to content

Commit

Permalink
Only update a single render transform and not the entire queue when a…
Browse files Browse the repository at this point in the history
… render item has been changed.
  • Loading branch information
Thomas Gravgaard committed Apr 11, 2012
1 parent 26672bd commit 0695a2a
Show file tree
Hide file tree
Showing 5 changed files with 77 additions and 27 deletions.
11 changes: 4 additions & 7 deletions Framework/PressPlay.FFWD/Components/Camera.cs
Expand Up @@ -408,21 +408,18 @@ internal static void Culling()
int camCount = _allCameras.Count;

// Go through the moved renderers and update culling
RenderItem movedItem = RenderQueue.GetMovedItem();
while (movedItem != null)
Renderer updated = RenderQueue.GetUpdatedRenderer();
while (updated != null)
{
for (int i = 0; i < camCount; i++)
{
if (!_allCameras[i].doFullCullingScan)
{
// TODO: We should have a method of updating only the cull info of the moved item and not the entrire camera queue
if (movedItem.UpdateCullingInfo(_allCameras[i]))
{
_allCameras[i].CulledRenderQueue.Add(movedItem);
}
updated.UpdateCullingInfo(_allCameras[i]);
}
}
movedItem = RenderQueue.GetMovedItem();
updated = RenderQueue.GetUpdatedRenderer();
}

for (int i = 0; i < camCount; i++)
Expand Down
59 changes: 46 additions & 13 deletions Framework/PressPlay.FFWD/Components/Renderers/RenderItem.cs
Expand Up @@ -204,24 +204,19 @@ public override string ToString()

internal bool UpdateCullingInfo(Camera cam)
{
PooledPriorityQueue cullingInfo;
PooledPriorityQueue cullingInfo = null;
int id = cam.GetInstanceID();
if (CameraCullingInfo.ContainsKey(id))
{
cullingInfo = CameraCullingInfo[id];
cullingInfo.Clear();
}
else
{
cullingInfo = new PooledPriorityQueue(Transforms.Count);
}
#if DEBUG
if (ApplicationSettings.LogSettings.LogCulling)
{
Debug.LogFormat("Update culling for {0}", cam.gameObject);
}
#endif
bool shouldBeRenderedOnCamera = false;
for (int i = 0; i < Transforms.Count; i++)
{
Transform t = Application.Find<Transform>(Transforms[i]);
Expand All @@ -242,23 +237,61 @@ internal bool UpdateCullingInfo(Camera cam)
Debug.LogFormat("Put {0} in renderqueue with priority {1}", t.gameObject, priority);
}
#endif
if (cullingInfo == null)
{
cullingInfo = new PooledPriorityQueue(Transforms.Count);
CameraCullingInfo[id] = cullingInfo;
}
cullingInfo.Add(t.GetInstanceID(), priority);
shouldBeRenderedOnCamera = true;
}
}
}
if (shouldBeRenderedOnCamera)
return cullingInfo.Count > 0;
}

internal bool UpdateCullingInfo(Camera cam, Transform t)
{
PooledPriorityQueue cullingInfo = null;
int id = cam.GetInstanceID();
int transformId = t.GetInstanceID();
if (CameraCullingInfo.ContainsKey(id))
{
CameraCullingInfo[id] = cullingInfo;
cullingInfo = CameraCullingInfo[id];
cullingInfo.Remove(transformId);
}
else
#if DEBUG
if (ApplicationSettings.LogSettings.LogCulling)
{
Debug.LogFormat("Update culling for {0} on {1}", t, cam.gameObject);
}
#endif
// Check the layer and cull accordingly
if (t != null && (cam.cullingMask & (1 << t.gameObject.layer)) > 0)
{
if (CameraCullingInfo.ContainsKey(id))
// Check frustum culling
// TODO: Here we should use something like an octtree to make full scanning faster
BoundingSphere sphere = new BoundingSphere(t.TransformPoint(Bounds.Center), Bounds.Radius * Math.Max(Math.Abs(t.lossyScale.x), Math.Max(Math.Abs(t.lossyScale.y), Math.Abs(t.lossyScale.z))));
ContainmentType contain;
cam.frustum.Contains(ref sphere, out contain);
if (contain != ContainmentType.Disjoint)
{
CameraCullingInfo.Remove(id);
float priority = GetTransformPriority(cam, t);
#if DEBUG
if (ApplicationSettings.LogSettings.LogCulling)
{
Debug.LogFormat("Put {0} in renderqueue with priority {1}", t.gameObject, priority);
}
#endif
if (cullingInfo == null)
{
cullingInfo = new PooledPriorityQueue(Transforms.Count);
CameraCullingInfo[id] = cullingInfo;
}
cullingInfo.Add(t.GetInstanceID(), priority);
return true;
}
}
return shouldBeRenderedOnCamera;
return false;
}

private static float GetTransparentPriority(Camera cam, Transform t)
Expand Down
12 changes: 6 additions & 6 deletions Framework/PressPlay.FFWD/Components/Renderers/RenderQueue.cs
Expand Up @@ -13,7 +13,7 @@ public RenderQueue(int capacity)
}

private List<RenderItem> list;
private static Queue<RenderItem> reconsiderForCulling = new Queue<RenderItem>(ApplicationSettings.DefaultCapacities.RenderCullingQueue);
private static Queue<Renderer> updatedRenderers = new Queue<Renderer>(ApplicationSettings.DefaultCapacities.RenderCullingQueue);

public void Add(RenderItem item)
{
Expand Down Expand Up @@ -66,18 +66,18 @@ public int Compare(RenderItem x, RenderItem y)
return idx;
}

internal static void ReconsiderForCulling(RenderItem r)
internal static void ReconsiderForCulling(Renderer r)
{
reconsiderForCulling.Enqueue(r);
updatedRenderers.Enqueue(r);
}

internal static RenderItem GetMovedItem()
internal static Renderer GetUpdatedRenderer()
{
if (reconsiderForCulling.Count == 0)
if (updatedRenderers.Count == 0)
{
return null;
}
return reconsiderForCulling.Dequeue();
return updatedRenderers.Dequeue();
}

internal void Clear()
Expand Down
10 changes: 9 additions & 1 deletion Framework/PressPlay.FFWD/Components/Renderers/Renderer.cs
Expand Up @@ -164,12 +164,20 @@ public virtual bool ShouldPrefabsBeInitialized()
/// Flag that the renderer has changed in some way so we need to reconsider it for culling.
/// </summary>
internal void ReconsiderForCulling()
{
RenderQueue.ReconsiderForCulling(this);
}

internal void UpdateCullingInfo(Camera cam)
{
if (renderItems.HasElements())
{
for (int i = 0; i < renderItems.Length; i++)
{
RenderQueue.ReconsiderForCulling(renderItems[i]);
if (renderItems[i].UpdateCullingInfo(cam, transform))
{
cam.CulledRenderQueue.Add(renderItems[i]);
}
}
}
}
Expand Down
12 changes: 12 additions & 0 deletions Framework/PressPlay.FFWD/PooledPriorityQueue.cs
Expand Up @@ -89,5 +89,17 @@ public int Compare(PooledPriorityQueue.PooledPriorityQueueItem x, PooledPriority
{
return x.priority.CompareTo(y.priority);
}

internal void Remove(int transformId)
{
for (int i = internalList.Count - 1; i >= 0; i--)
{
if (internalList[i].id == transformId)
{
internalList.RemoveAt(i);
break;
}
}
}
}
}

0 comments on commit 0695a2a

Please sign in to comment.