Skip to content

Commit

Permalink
Adding abstraction to the scene buffer manager, separating the BuiltI…
Browse files Browse the repository at this point in the history
…n from the engine common classes

Visual improvements added to the Virtual World scene
  • Loading branch information
Selinux24 committed Aug 11, 2024
1 parent 78249fd commit 2d135d6
Show file tree
Hide file tree
Showing 67 changed files with 1,260 additions and 1,227 deletions.
30 changes: 3 additions & 27 deletions Engine/BuiltIn/Components/Ground/Scenery.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
using Engine.BuiltIn.Drawers;
using Engine.BuiltIn.Drawers.Deferred;
using Engine.BuiltIn.Drawers.Forward;
using Engine.BuiltIn.Primitives;
using Engine.Collections.Generic;
using Engine.Common;
using Engine.Content;
Expand Down Expand Up @@ -138,7 +135,7 @@ public bool DrawSceneryShadows(DrawContextShadows context)
var meshMaterial = matData.MeshMaterial;
var mesh = matData.Mesh;

var sceneryDrawer = context.ShadowMap.GetDrawer(mesh.VertextType, false, meshMaterial.Material.IsTransparent);
var sceneryDrawer = context.ShadowMap.GetDrawer(mesh, false, meshMaterial.Material.IsTransparent);
if (sceneryDrawer == null)
{
continue;
Expand Down Expand Up @@ -172,7 +169,7 @@ public bool DrawScenery(DrawContext context, BlendModes blendMode)
continue;
}

var sceneryDrawer = GetDrawer(context.DrawerMode, mesh.VertextType);
var sceneryDrawer = BuiltInDrawer.GetDrawer(context.DrawerMode, mesh, false);
if (sceneryDrawer == null)
{
continue;
Expand All @@ -193,7 +190,7 @@ public bool DrawScenery(DrawContext context, BlendModes blendMode)
/// <param name="sceneryDrawer">Drawer</param>
/// <param name="mesh">Mesh</param>
/// <param name="material">Material</param>
private static bool DrawWithDrawer(IEngineDeviceContext dc, IDrawer sceneryDrawer, Mesh mesh, IMeshMaterial material)
private static bool DrawWithDrawer(IEngineDeviceContext dc, IDrawer sceneryDrawer, IMesh mesh, IMeshMaterial material)
{
sceneryDrawer.UpdateMesh(dc, BuiltInDrawerMeshState.Default());

Expand All @@ -209,27 +206,6 @@ private static bool DrawWithDrawer(IEngineDeviceContext dc, IDrawer sceneryDrawe
return sceneryDrawer.Draw(dc, [mesh]);
}

/// <summary>
/// Gets the drawing effect for the current instance
/// </summary>
/// <param name="mode">Drawing mode</param>
/// <param name="vertexType">Vertex type</param>
/// <returns>Returns the drawing effect</returns>
private static IDrawer GetDrawer(DrawerModes mode, VertexTypes vertexType)
{
if (mode.HasFlag(DrawerModes.Forward))
{
return ForwardDrawerManager.GetDrawer(vertexType, false);
}

if (mode.HasFlag(DrawerModes.Deferred))
{
return DeferredDrawerManager.GetDrawer(vertexType, false);
}

return null;
}

/// <summary>
/// Gets all the used materials
/// </summary>
Expand Down
2 changes: 1 addition & 1 deletion Engine/BuiltIn/Components/Ground/Terrain.cs
Original file line number Diff line number Diff line change
Expand Up @@ -194,7 +194,7 @@ public override bool DrawShadows(DrawContextShadows context)
return false;
}

var shadowDrawer = context.ShadowMap?.GetDrawer(VertexTypes.Terrain, false, false);
var shadowDrawer = context.ShadowMap?.GetDrawer<VertexTerrain>(false, false);
if (shadowDrawer == null)
{
return false;
Expand Down
2 changes: 1 addition & 1 deletion Engine/BuiltIn/Components/Ground/TerrainGrid.cs
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ public static async Task<TerrainGrid> Create(Game game, string mapName, IEnumera
var nodes = res.drawingQuadTree.GetLeafNodes();
foreach (var node in nodes)
{
var data = await VertexTypesHelper.Convert(VertexTypes.Terrain, node.Items, null, null);
var data = await VertexTerrain.Convert(node.Items);

res.dictVB.Add(node.Id, game.BufferManager.AddVertexData(mapName, false, data));
}
Expand Down
4 changes: 2 additions & 2 deletions Engine/BuiltIn/Components/Model/Model.cs
Original file line number Diff line number Diff line change
Expand Up @@ -209,7 +209,7 @@ public override bool DrawShadows(DrawContextShadows context)

var localTransform = GetPartTransformByName(meshName);

var drawer = context.ShadowMap?.GetDrawer(mesh.VertextType, false, meshMaterial.Material.IsTransparent);
var drawer = context.ShadowMap?.GetDrawer(mesh, false, meshMaterial.Material.IsTransparent);
if (drawer == null)
{
continue;
Expand Down Expand Up @@ -280,7 +280,7 @@ public override bool Draw(DrawContext context)
continue;
}

var drawer = BuiltInDrawer.GetDrawer(context.DrawerMode, mesh.VertextType, false);
var drawer = BuiltInDrawer.GetDrawer(context.DrawerMode, mesh, false);
if (drawer == null)
{
continue;
Expand Down
6 changes: 3 additions & 3 deletions Engine/BuiltIn/Components/Model/ModelInstanced.cs
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ protected override void Dispose(bool disposing)
{
if (disposing)
{
BufferManager.RemoveInstancingData<VertexInstancingData>(InstancingBuffer);
BufferManager.RemoveInstancingData(InstancingBuffer);
InstancingBuffer = null;
}

Expand Down Expand Up @@ -356,7 +356,7 @@ private int DrawShadowMesh(DrawContextShadows context, DrawingData drawingData,

Logger.WriteTrace(this, $"{nameof(ModelInstanced)}.{Name} - {nameof(DrawShadowMesh)}: {meshName}. Index {startInstanceLocation} Length {instancesToDraw}.");

var drawer = context.ShadowMap?.GetDrawer(mesh.VertextType, true, meshMaterial.Material.IsTransparent);
var drawer = context.ShadowMap?.GetDrawer(mesh, true, meshMaterial.Material.IsTransparent);
if (drawer == null)
{
continue;
Expand Down Expand Up @@ -493,7 +493,7 @@ private int DrawMesh(DrawContext context, DrawingData drawingData, ModelInstance
continue;
}

var drawer = BuiltInDrawer.GetDrawer(context.DrawerMode, mesh.VertextType, true);
var drawer = BuiltInDrawer.GetDrawer(context.DrawerMode, mesh, true);
if (drawer == null)
{
continue;
Expand Down
101 changes: 71 additions & 30 deletions Engine/BuiltIn/Components/Primitives/GeometryDrawer.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using Engine.BuiltIn.Drawers;
using Engine.BuiltIn.Primitives;
using Engine.Common;
using Engine.Content;
using SharpDX;
Expand Down Expand Up @@ -28,10 +27,6 @@ public sealed class GeometryDrawer<T>(Scene scene, string id, string name) : Dra
/// Triangle dictionary by color
/// </summary>
private readonly ConcurrentBag<T> bag = [];
/// <summary>
/// Vertex type
/// </summary>
private readonly VertexTypes vertexType = default(T).VertexType;

/// <summary>
/// Topology
Expand Down Expand Up @@ -264,60 +259,71 @@ public void SetManipulator(IManipulator3D manipulator)
/// <inheritdoc/>
public override bool Draw(DrawContext context)
{
if (!Visible)
if (!BuffersReady)
{
return false;
}

if (!BuffersReady)
WriteDataInBuffer();

if (vertexCount <= 0)
{
return false;
}

bool draw = context.ValidateDraw(BlendMode);
if (!draw)
if (!Visible)
{
return false;
}

WriteDataInBuffer();

if (vertexCount <= 0)
bool draw = context.ValidateDraw(BlendMode);
if (!draw)
{
return false;
}

var drawer = BuiltInDrawer.GetDrawer(context.DrawerMode, vertexType, false);
var drawer = BuiltInDrawer.GetDrawer<T>(context.DrawerMode, false);
if (drawer == null)
{
return false;
}

var dc = context.DeviceContext;
drawer.UpdateMesh(dc, PrepareMeshState());
drawer.UpdateMaterial(dc, PrepareMaterialState());

var meshState = new BuiltInDrawerMeshState
return drawer.Draw(dc, PrepareDrawOptions());
}
/// <inheritdoc/>
public override bool DrawShadows(DrawContextShadows context)
{
if (!BuffersReady)
{
Local = Manipulator.GlobalTransform
};
drawer.UpdateMesh(dc, meshState);
return false;
}

var materialState = new BuiltInDrawerMaterialState
if (vertexCount <= 0)
{
TintColor = TintColor,
Material = material,
TextureIndex = TextureIndex,
UseAnisotropic = UseAnisotropic,
};
drawer.UpdateMaterial(dc, materialState);
return false;
}

bool drawn = drawer.Draw(dc, new DrawOptions
if (!Visible)
{
VertexBuffer = vertexBuffer,
VertexDrawCount = vertexCount,
Topology = topology,
});
return false;
}

var drawer = context.ShadowMap?.GetDrawer<T>(false, material.Material.IsTransparent);
if (drawer == null)
{
return false;
}

var dc = context.DeviceContext;
drawer.UpdateCastingLight(context);
drawer.UpdateMesh(dc, PrepareMeshState());
drawer.UpdateMaterial(dc, PrepareMaterialState());

return drawn;
return drawer.Draw(dc, PrepareDrawOptions());
}
/// <summary>
/// Writes dictionary data in buffer
Expand Down Expand Up @@ -345,6 +351,41 @@ public void WriteDataInBuffer()
bagChanged = false;
}
}
/// <summary>
/// Prepares the drawer mesh state
/// </summary>
private BuiltInDrawerMeshState PrepareMeshState()
{
return new()
{
Local = Manipulator.GlobalTransform
};
}
/// <summary>
/// Prepares the drawer material state
/// </summary>
private BuiltInDrawerMaterialState PrepareMaterialState()
{
return new()
{
TintColor = TintColor,
Material = material,
TextureIndex = TextureIndex,
UseAnisotropic = UseAnisotropic,
};
}
/// <summary>
/// Prepares the draw options
/// </summary>
private DrawOptions PrepareDrawOptions()
{
return new()
{
VertexBuffer = vertexBuffer,
VertexDrawCount = vertexCount,
Topology = topology,
};
}

/// <inheritdoc/>
public IEnumerable<IMeshMaterial> GetMaterials()
Expand Down
Loading

0 comments on commit 2d135d6

Please sign in to comment.