Skip to content

Commit

Permalink
Optimized BVH Building tasks
Browse files Browse the repository at this point in the history
Removed progressive building on each frame in favor of letting the application handle the threads on its own
  • Loading branch information
Kamzik123 committed Nov 28, 2024
1 parent ba2fafd commit dea4234
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 44 deletions.
4 changes: 2 additions & 2 deletions Mafia2Libs/Rendering/Core/InstanceGizmo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ namespace Rendering.Core
{
public class InstanceGizmo
{
private float ModelScale = 0.015f;
private Vector3 ModelScale = new Vector3(0.015f);
// Variable for rendering
public RenderModel InstanceModel;

Expand Down Expand Up @@ -45,7 +45,7 @@ public ID3D11ShaderResourceView LoadTexture(ID3D11Device d3d, ID3D11DeviceContex

public void UpdateInstanceBuffer(Instance instance, ID3D11Device d3d)
{
Matrix4x4 newtransform = MatrixUtils.SetMatrix(instance.Quaternion, new Vector3(ModelScale), instance.Position);
Matrix4x4 newtransform = MatrixUtils.SetMatrix(instance.Quaternion, ModelScale, instance.Position);

if (!InstanceModel.InstanceTransforms.ContainsKey(instance.RefID))
{
Expand Down
57 changes: 15 additions & 42 deletions Mafia2Libs/Rendering/Graphics/GraphicsClass.cs
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,7 @@ public class GraphicsClass
private PrimitiveBatch LineBatch = null;
private PrimitiveBatch BBoxBatch = null;
private int NumBVHToBuild = 0;
private int NumBVHToBuilt = 0;
private int MaxBVHBuildingTasks = 4; // We should detect how many threads the computer has on boot and assign the max task count based on that.
private int NumBVHBuilt = 0;
private List<Task> BVHBuildingTasks = new();
public PrimitiveManager OurPrimitiveManager { get; private set; }

Expand All @@ -73,8 +72,6 @@ public GraphicsClass()
navigationGrids = new SpatialGrid[0];
OurPrimitiveManager = new PrimitiveManager();

UpdateMaxBVHTasks();

OnSelectedObjectUpdated += OnSelectedObjectHasUpdated;

// Create bespoke batches for any lines or boxes passed in via the construct stack
Expand Down Expand Up @@ -149,12 +146,7 @@ public bool InitScene(int width, int height)
sky.DoRender = WorldSettings.RenderSky;
clouds.InitBuffers(D3D.Device, D3D.DeviceContext);
InstanceGizmo.InitBuffers(D3D.Device, D3D.DeviceContext);
var task = InstanceGizmo.InstanceModel.GetBVHBuildingTask(); // Maybe this function should be added to the IRenderer class instead? probably

if (task != null)
{
BVHBuildingTasks.Add(task);
}
InstanceGizmo.InstanceModel.GetBVHBuildingTask(); // Maybe this function should be added to the IRenderer class instead? probably

Input = new InputClass();
Input.Init();
Expand Down Expand Up @@ -393,8 +385,10 @@ public bool UpdateInput()

public bool Render()
{
// Clear completed BVH tasks
NumBVHToBuilt += BVHBuildingTasks.RemoveAll(t => t.IsCompleted);
if (NumBVHBuilt < NumBVHToBuild)
{
UpdateBVHQueue();
}

D3D.BeginScene(0.0f, 0f, 0f, 1.0f);
Camera.Render();
Expand All @@ -408,17 +402,6 @@ public bool Render()
{
RenderEntry.UpdateBuffers(D3D.Device, D3D.DeviceContext);
RenderEntry.Render(D3D.Device, D3D.DeviceContext, Camera);

// A status bar will be added to the map editor later to indicate when it is building BVH structures
if (RenderEntry is RenderModel mesh && BVHBuildingTasks.Count < MaxBVHBuildingTasks)
{
var task = mesh.GetBVHBuildingTask(); // Maybe this function should be added to the IRenderer class instead?

if (task != null)
{
BVHBuildingTasks.Add(task);
}
}
}

//navigationGrids[0].Render(D3D.Device, D3D.DeviceContext, Camera);
Expand Down Expand Up @@ -449,11 +432,6 @@ public bool Render()

private void ClearRenderStack()
{
if (InitObjectStack.Count > 0)
{
NumBVHToBuild = 0;
}

foreach (KeyValuePair<int, IRenderer> asset in InitObjectStack)
{
asset.Value.InitBuffers(D3D.Device, D3D.DeviceContext);
Expand All @@ -468,7 +446,9 @@ private void ClearRenderStack()
}
else if (asset.Value is RenderModel)
{
BVHBuildingTasks.Add(((RenderModel)asset.Value).GetBVHBuildingTask());
NumBVHToBuild++;

Assets.Add(asset.Key, asset.Value);
}
else
Expand All @@ -480,6 +460,12 @@ private void ClearRenderStack()
InitObjectStack.Clear();
}

private void UpdateBVHQueue()
{
// Clear completed BVH tasks
NumBVHBuilt += BVHBuildingTasks.RemoveAll(t => t.IsCompleted);
}

public void SelectEntry(int id)
{
IRenderer NewObject = GetAsset(id);
Expand Down Expand Up @@ -681,20 +667,7 @@ public string GetStatusBarText()
}

//return Utils.Language.Language.GetString("$BUILDING_BVH"); //Keeps printing missing text in debug build and slowing things down
return $"Building BVH: {NumBVHToBuilt}/{NumBVHToBuild}";
}

private void UpdateMaxBVHTasks()
{
int processorCount = Environment.ProcessorCount;

if (processorCount <= 3)
{
MaxBVHBuildingTasks = 1;
return;
}

MaxBVHBuildingTasks = processorCount - 2;
return $"Building BVH: {NumBVHBuilt}/{NumBVHToBuild}";
}

public ID3D11Device GetId3D11Device()
Expand Down

0 comments on commit dea4234

Please sign in to comment.