Skip to content

Commit

Permalink
We now have alpha sorting for the render items.
Browse files Browse the repository at this point in the history
  • Loading branch information
Thomas Gravgaard committed Apr 11, 2012
1 parent b98848b commit 26672bd
Show file tree
Hide file tree
Showing 21 changed files with 358 additions and 162 deletions.
5 changes: 5 additions & 0 deletions Framework/PressPlay.FFWD/ApplicationSettings.cs
Expand Up @@ -52,6 +52,7 @@ public static class DefaultCapacities
public static int RenderQueues = 100;
public static int RenderCullingQueue = 20;
public static int ColliderContacts = 50;
public static int PriorityQueuePoolSize = 100;
#endregion
}

Expand All @@ -65,5 +66,9 @@ public static class DefaultValues
public static float StaticBatchTileSize = 200.0f;
}

public static class LogSettings
{
public static bool LogCulling = false;
}
}
}
13 changes: 2 additions & 11 deletions Framework/PressPlay.FFWD/Components/Camera.cs
Expand Up @@ -401,7 +401,6 @@ internal static void ChangeRenderQueue(Renderer renderer)

#if DEBUG
internal static bool logRenderCalls = false;
internal static bool logCulling = false;
#endif

internal static void Culling()
Expand All @@ -416,6 +415,7 @@ internal static void Culling()
{
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);
Expand Down Expand Up @@ -457,15 +457,7 @@ internal static void DoRender(GraphicsDevice device)
}
if (Input.GetMouseButtonUp(1))
{
if (Input.GetKey(Microsoft.Xna.Framework.Input.Keys.LeftShift) || Input.GetKey(Microsoft.Xna.Framework.Input.Keys.RightShift))
{
logCulling = true;
Debug.Log("----------- Render log with culling begin ---------------", Time.realtimeSinceStartup);
}
else
{
Debug.Log("----------- Render log begin ---------------", Time.realtimeSinceStartup);
}
Debug.Log("----------- Render log begin ---------------", Time.realtimeSinceStartup);
logRenderCalls = true;
}
#endif
Expand Down Expand Up @@ -531,7 +523,6 @@ internal static void DoRender(GraphicsDevice device)
#if DEBUG
Debug.Display("Draw stats", System.String.Format("Draw {0}, Batch draw {1}, Tris {2}, Verts {3}, RTs {4}", RenderStats.DrawCalls, RenderStats.BatchedDrawCalls, RenderStats.TrianglesDrawn, RenderStats.VerticesDrawn, renderTargets));
logRenderCalls = false;
logCulling = false;
#endif
}

Expand Down
163 changes: 92 additions & 71 deletions Framework/PressPlay.FFWD/Components/Renderers/RenderItem.cs
Expand Up @@ -30,7 +30,7 @@ protected RenderItem()
protected short[] indexData;

private List<int> Transforms = new List<int>(1);
private Dictionary<int, BitArray> CameraCullingInfo = new Dictionary<int, BitArray>(1);
private Dictionary<int, PooledPriorityQueue> CameraCullingInfo = new Dictionary<int, PooledPriorityQueue>(1);

protected const int MAX_INDEX_BUFFER_SIZE = Int16.MaxValue;

Expand All @@ -42,6 +42,8 @@ protected RenderItem()
private static int hitPool = 0;
private static int missPool = 0;

private Func<Camera, Transform, float> GetTransformPriority;

internal static RenderItem Create(Material material, Mesh mesh, int subMeshIndex, Transform t)
{
RenderItem item;
Expand Down Expand Up @@ -74,9 +76,16 @@ internal static RenderItem Create(Material material, Mesh mesh, int subMeshIndex
}
item.Priority = material.renderQueue;
item.AddMesh(mesh, t.world, subMeshIndex);
if (material.IsTransparent())
{
item.GetTransformPriority = GetTransparentPriority;
}
else
{
item.GetTransformPriority = GetOpaquePriority;
}
RenderItemPool[id] = item;
}

item.AddTransform(t);
return item;
}
Expand Down Expand Up @@ -106,85 +115,65 @@ public void Render(GraphicsDevice device, Camera cam)
int id = cam.GetInstanceID();
if (!CameraCullingInfo.ContainsKey(id))
{
#if DEBUG
if (Camera.logCulling)
{
Debug.LogFormat("Cull: {0} not shown on {1}", ToString(), cam);
}
#endif
return;
}
BitArray cullingInfo = CameraCullingInfo[id];
for (int i = Transforms.Count - 1; i >= 0; i--)
PooledPriorityQueue cullingInfo = CameraCullingInfo[id];
for (int i = 0; i < cullingInfo.Count; i++)
{
if (!cullingInfo[i])
Transform t = Application.Find<Transform>(cullingInfo[i]);
if (t == null)
{
#if DEBUG
if (Camera.logCulling)
{
Debug.LogFormat("Cull: {0} out of view on {1}", this, cam);
}
#endif
continue;
}
Transform t = Application.Find<Transform>(Transforms[i]);
if (t == null)
if (!t.renderer.enabled || !t.gameObject.active)
{
Transforms.RemoveAt(i);
continue;
}
else
{
if (!t.renderer.enabled || !t.gameObject.active)
{
continue;
}

#if DEBUG
if (Camera.logRenderCalls)
{
Debug.LogFormat("Render: {0} for {1} on {2}", this, t.gameObject, cam.gameObject);
}
if (Camera.logRenderCalls)
{
Debug.LogFormat("Render: {0} for {1} on {2}", this, t.gameObject, cam.gameObject);
}
#endif
Effect e = Material.shader.effect;
if (!devicePrepared)
{
devicePrepared = true;
device.SetVertexBuffer(VertexBuffer);
device.Indices = IndexBuffer;
Effect e = Material.shader.effect;
if (!devicePrepared)
{
devicePrepared = true;
device.SetVertexBuffer(VertexBuffer);
device.Indices = IndexBuffer;

Material.shader.ApplyPreRenderSettings(Material, UseVertexColor);
Material.SetBlendState(device);
Material.shader.ApplyPreRenderSettings(Material, UseVertexColor);
Material.SetBlendState(device);

IEffectMatrices ems = e as IEffectMatrices;
if (ems != null)
{
ems.World = t.world;
ems.View = cam.view;
ems.Projection = cam.projectionMatrix;
}
IEffectMatrices ems = e as IEffectMatrices;
if (ems != null)
{
ems.World = t.world;
ems.View = cam.view;
ems.Projection = cam.projectionMatrix;
}
else
{
IEffectMatrices ems = e as IEffectMatrices;
if (ems != null)
{
ems.World = t.world;
}
}
foreach (EffectPass pass in e.CurrentTechnique.Passes)
}
else
{
IEffectMatrices ems = e as IEffectMatrices;
if (ems != null)
{
pass.Apply();
device.DrawIndexedPrimitives(
PrimitiveType.TriangleList,
0,
0,
VertexBuffer.VertexCount,
0,
IndexBuffer.IndexCount / 3
);
ems.World = t.world;
}
RenderStats.AddDrawCall(batches, VertexBuffer.VertexCount, IndexBuffer.IndexCount / 3);
}
foreach (EffectPass pass in e.CurrentTechnique.Passes)
{
pass.Apply();
device.DrawIndexedPrimitives(
PrimitiveType.TriangleList,
0,
0,
VertexBuffer.VertexCount,
0,
IndexBuffer.IndexCount / 3
);
}
RenderStats.AddDrawCall(batches, VertexBuffer.VertexCount, IndexBuffer.IndexCount / 3);
}
}

Expand Down Expand Up @@ -215,12 +204,28 @@ public override string ToString()

internal bool UpdateCullingInfo(Camera cam)
{
BitArray array = new BitArray(Transforms.Count);
PooledPriorityQueue cullingInfo;
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]);
// Check the layer
// Check the layer and cull accordingly
if (t != null && (cam.cullingMask & (1 << t.gameObject.layer)) > 0)
{
// Check frustum culling
Expand All @@ -230,15 +235,21 @@ internal bool UpdateCullingInfo(Camera cam)
cam.frustum.Contains(ref sphere, out contain);
if (contain != ContainmentType.Disjoint)
{
array[i] = true;
float priority = GetTransformPriority(cam, t);
#if DEBUG
if (ApplicationSettings.LogSettings.LogCulling)
{
Debug.LogFormat("Put {0} in renderqueue with priority {1}", t.gameObject, priority);
}
#endif
cullingInfo.Add(t.GetInstanceID(), priority);
shouldBeRenderedOnCamera = true;
}
}
}
}
int id = cam.GetInstanceID();
if (shouldBeRenderedOnCamera)
{
CameraCullingInfo[id] = array;
CameraCullingInfo[id] = cullingInfo;
}
else
{
Expand All @@ -249,6 +260,16 @@ internal bool UpdateCullingInfo(Camera cam)
}
return shouldBeRenderedOnCamera;
}

private static float GetTransparentPriority(Camera cam, Transform t)
{
return -Vector3.DistanceSquared(t.position, cam.transform.position);
}

private static float GetOpaquePriority(Camera cam, Transform t)
{
return 0;
}
}

/// <summary>
Expand Down
9 changes: 0 additions & 9 deletions Framework/PressPlay.FFWD/Input/Input.cs
Expand Up @@ -495,15 +495,6 @@ public static bool GetKeyDown(Keys key)
#endif
}

public static bool GetKey(Keys key)
{
#if WINDOWS_PHONE
return false;
#else
return _currentKeyboardState.IsKeyDown(key);
#endif
}

public static string[] GetJoystickNames()
{
List<string> names = new List<string>(4);
Expand Down

0 comments on commit 26672bd

Please sign in to comment.