Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Translokator spatial grid rendering #114

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
56 changes: 53 additions & 3 deletions Mafia2Libs/Forms/MapEditor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -362,9 +362,27 @@ private void UpdateAssetVisualisation(TreeNode node, TreeNode parent)

// Update rendered counterpart
int refID = (bIsFrame) ? (node.Tag as FrameEntry).RefID : result;
if (!bIsFrame && node.Tag is Instance && node.Parent.Tag is Object trObject)

if (!bIsFrame)
{
UpdateInstanceVisualisation(node,trObject,node.Checked && node.CheckIfParentsAreValid());
if (node.Tag is Instance && node.Parent.Tag is Object trObject)
{
UpdateInstanceVisualisation(node, trObject, node.Checked && node.CheckIfParentsAreValid());
}
else if (node.Tag is Grid trGrid)
{
bool enabled = node.Checked && node.CheckIfParentsAreValid();

if (enabled)
{
RebuildTranslokatorGrids();
}
else
{
int trGridIndex = Array.IndexOf(SceneData.Translokator.Grids, trGrid);
Graphics.SetTranslokatorGridEnabled(trGridIndex, enabled);
}
}
}
else
{
Expand Down Expand Up @@ -1132,13 +1150,14 @@ private void BuildRenderObjects()
Grid grid = SceneData.Translokator.Grids[i];
TreeNode child = new TreeNode("Grid " + i);
child.Tag = grid;
child.Checked = false;
gridNode.Nodes.Add(child);
}

translokatorRoot.Nodes.Add(gridNode);

dSceneTree.AddToTree(translokatorRoot);
//Graphics.SetTranslokatorGrid(SceneData.Translokator);
Graphics.BuildTranslokatorGrid(SceneData.Translokator);
}
}

Expand Down Expand Up @@ -1851,6 +1870,10 @@ private void OnPropertyValueChanged(object s, PropertyValueChangedEventArgs e)
Graphics.InstanceGizmo.UpdateInstanceBuffer(instance, Graphics.GetId3D11Device());
}
}
if (pGrid.SelectedObject is Grid trGrid)
{
RebuildTranslokatorGrids();
}

pGrid.Refresh();
}
Expand Down Expand Up @@ -2800,6 +2823,33 @@ private void DeleteTRObject(TreeNode objectNode)
}
dSceneTree.RemoveNode(objectNode);
}

private void RebuildTranslokatorGrids()
{
SceneData.Translokator.RebuildGridData();
Graphics.BuildTranslokatorGrid(SceneData.Translokator);

TreeNode gridsNode = null;

foreach (TreeNode node in translokatorRoot.Nodes)
{
if (node.Text.Equals("Grids", StringComparison.InvariantCultureIgnoreCase))
{
gridsNode = node;
break;
}
}

for (int i = 0; i < gridsNode.Nodes.Count; i++)
{
TreeNode child = gridsNode.Nodes[i];

if (child.Tag is Grid)
{
Graphics.SetTranslokatorGridEnabled(i, child.Checked && child.CheckIfParentsAreValid());
}
}
}
}
}

14 changes: 9 additions & 5 deletions Mafia2Libs/Rendering/Graphics/GraphicsClass.cs
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ public class GraphicsClass

private DirectX11Class D3D;

private SpatialGrid translokatorGrid;
private TranslokatorSpatialGrid translokatorGrid;
private SpatialGrid[] navigationGrids;

// Local batches for objects passed through
Expand All @@ -68,7 +68,7 @@ public GraphicsClass()
Profile = new Profiler();
Assets = new Dictionary<int, IRenderer>();
selectionBox = new RenderBoundingBox();
translokatorGrid = new SpatialGrid();
translokatorGrid = new TranslokatorSpatialGrid();
navigationGrids = new SpatialGrid[0];
OurPrimitiveManager = new PrimitiveManager();

Expand Down Expand Up @@ -153,11 +153,10 @@ public bool InitScene(int width, int height)
return true;
}

public TreeNode SetTranslokatorGrid(TranslokatorLoader translokator)
public void BuildTranslokatorGrid(TranslokatorLoader translokator)
{
translokatorGrid = new SpatialGrid(this, translokator);
translokatorGrid.Build(translokator);
translokatorGrid.Initialise(D3D.Device, D3D.DeviceContext);
return translokatorGrid.GetTreeNodes();
}

public TreeNode SetNavigationGrid(ResourceTypes.Navigation.OBJData[] data)
Expand Down Expand Up @@ -741,5 +740,10 @@ public void DeleteInstance(int InstanceRefID)
{
InstanceGizmo.InstanceModel.RemoveInstance(InstanceRefID,D3D.Device);
}

public void SetTranslokatorGridEnabled(int index, bool enabled)
{
translokatorGrid.SetGridEnabled(index, enabled);
}
}
}
1 change: 1 addition & 0 deletions Mafia2Libs/Rendering/Graphics/RenderTypes/IRenderer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ public abstract class IRenderer
protected ID3D11ShaderResourceView instanceBufferView;

public bool DoRender { get; set; }
public bool DoRenderInstancesOnly { get; set; }
public Matrix4x4 Transform { get; protected set; }
public BoundingBox BoundingBox { get; protected set; }

Expand Down
120 changes: 115 additions & 5 deletions Mafia2Libs/Rendering/Graphics/RenderTypes/RenderBoundingBox.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
using System.Numerics;
using Rendering.Core;
using System;
using System.Collections.Generic;
using System.Numerics;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using Vortice;
using Vortice.Direct3D;
using Vortice.Direct3D11;
Expand All @@ -22,6 +26,7 @@ public class RenderBoundingBox : IRenderer
private VertexLayouts.BasicLayout.Vertex[] vertices;
private Color CurrentColour;
private Color UnselectedColour;
private List<Matrix4x4> InstanceTransforms = new();

public RenderBoundingBox()
{
Expand Down Expand Up @@ -73,6 +78,73 @@ public override void InitBuffers(ID3D11Device d3d, ID3D11DeviceContext deviceCon
{
vertexBuffer = d3d.CreateBuffer(BindFlags.VertexBuffer, vertices, 0, ResourceUsage.Dynamic, CpuAccessFlags.Write);
indexBuffer = d3d.CreateBuffer(BindFlags.IndexBuffer, Indices, 0, ResourceUsage.Dynamic, CpuAccessFlags.Write);

InitInstanceBuffer(d3d);
}

public void InitInstanceBuffer(ID3D11Device d3d)
{
int newSize = InstanceTransforms.Count * Marshal.SizeOf<Matrix4x4>();

if (InstanceTransforms.Count == 0)
{
return;
}

// Create or update buffer only if necessary
if (instanceBuffer == null || instanceBuffer.Description.SizeInBytes < newSize)
{
// Buffer description for instance buffer
var bufferDescription = new BufferDescription
{
SizeInBytes = newSize,
Usage = ResourceUsage.Dynamic,
BindFlags = BindFlags.ShaderResource,
OptionFlags = ResourceOptionFlags.BufferStructured,
CpuAccessFlags = CpuAccessFlags.Write,
StructureByteStride = Marshal.SizeOf<Matrix4x4>(),
};

var viewDescription = new ShaderResourceViewDescription()
{
Format = Vortice.DXGI.Format.Unknown,
ViewDimension = ShaderResourceViewDimension.Buffer,
};

viewDescription.Buffer.FirstElement = 0;
viewDescription.Buffer.NumElements = InstanceTransforms.Count;

// Dispose old buffer if necessary
instanceBuffer?.Dispose();

// Convert list to array
Matrix4x4[] transformsArray = InstanceTransforms.ToArray();

// Pin the array in memory
GCHandle handle = GCHandle.Alloc(transformsArray, GCHandleType.Pinned);
try
{
IntPtr pointer = handle.AddrOfPinnedObject();
// Update the instance buffer
instanceBuffer = d3d.CreateBuffer(bufferDescription, pointer);

instanceBufferView = d3d.CreateShaderResourceView(instanceBuffer, viewDescription);
}
finally
{
handle.Free();
}
}
}

public void ReloadInstanceBuffer(ID3D11Device d3d)
{
instanceBuffer?.Dispose();
instanceBuffer = null;
instanceBufferView?.Dispose();
instanceBufferView = null;

InitInstanceBuffer(d3d);
}

public void SetColour(Color newColour, bool update = false)
Expand All @@ -95,18 +167,51 @@ public override void Render(ID3D11Device device, ID3D11DeviceContext deviceConte
return;
}

bool BuffersSet = false;

if (InstanceTransforms.Count > 0)
{
VertexBufferView VertexBufferView = new VertexBufferView(vertexBuffer, Unsafe.SizeOf<VertexLayouts.BasicLayout.Vertex>(), 0);
deviceContext.IASetVertexBuffers(0, VertexBufferView);
deviceContext.IASetIndexBuffer(indexBuffer, Vortice.DXGI.Format.R32_UInt, 0);
deviceContext.IASetPrimitiveTopology(PrimitiveTopology.LineList);

BuffersSet = true;

RenderInstances(deviceContext, camera, device);
}

if (DoRenderInstancesOnly)
{
return;
}

if (!camera.CheckBBoxFrustum(Transform, BoundingBox))
return;

VertexBufferView VertexBufferView = new VertexBufferView(vertexBuffer, Unsafe.SizeOf<VertexLayouts.BasicLayout.Vertex>(), 0);
deviceContext.IASetVertexBuffers(0, VertexBufferView);
deviceContext.IASetIndexBuffer(indexBuffer, Vortice.DXGI.Format.R32_UInt, 0);
deviceContext.IASetPrimitiveTopology(PrimitiveTopology.LineList);
if (!BuffersSet)
{
VertexBufferView VertexBufferView = new VertexBufferView(vertexBuffer, Unsafe.SizeOf<VertexLayouts.BasicLayout.Vertex>(), 0);
deviceContext.IASetVertexBuffers(0, VertexBufferView);
deviceContext.IASetIndexBuffer(indexBuffer, Vortice.DXGI.Format.R32_UInt, 0);
deviceContext.IASetPrimitiveTopology(PrimitiveTopology.LineList);
BuffersSet = true;
}

shader.SetSceneVariables(deviceContext, Transform, camera);
shader.Render(deviceContext, PrimitiveTopology.LineList, ReadOnlyIndices.Length, 0);
}

public void RenderInstances(ID3D11DeviceContext deviceContext, Camera camera, ID3D11Device device)
{
deviceContext.VSSetShaderResource(0, instanceBufferView);

shader.SetSceneVariables(deviceContext, Transform, camera);

shader.RenderInstanced(deviceContext, PrimitiveTopology.LineList, Indices.Length, 0, InstanceTransforms.Count);
Profiler.NumDrawCallsThisFrame++;
}

public override void Shutdown()
{
vertices = null;
Expand Down Expand Up @@ -165,5 +270,10 @@ public VertexLayouts.BasicLayout.Vertex[] GetTransformVertices()

return NewVertices;
}

public void SetInstanceTransforms(List<Matrix4x4> Transforms)
{
InstanceTransforms = Transforms;
}
}
}
8 changes: 4 additions & 4 deletions Mafia2Libs/Rendering/Graphics/ShaderManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public bool Init(ID3D11Device device)
DebugInitParams.Elements = VertexLayouts.BasicLayout.GetLayout();
DebugInitParams.PixelShaderFile = new ShaderInitParams.ShaderFileEntryPoint("DebugPS.hlsl", "DebugPixelShader", "ps_5_0");
DebugInitParams.VertexShaderFile = new ShaderInitParams.ShaderFileEntryPoint("DebugVS.hlsl", "DebugVertexShader", "vs_5_0");
DefaultInitParams.InstancedVertexShaderFile = new ShaderInitParams.ShaderFileEntryPoint("LightVS.hlsl", "LightInstanceVertexShader", "vs_5_0");
DebugInitParams.InstancedVertexShaderFile = new ShaderInitParams.ShaderFileEntryPoint("DebugVS.hlsl", "DebugInstanceVertexShader", "vs_5_0");

DebugShader OurDebugShader = new DebugShader(device, DebugInitParams);

Expand All @@ -33,7 +33,7 @@ public bool Init(ID3D11Device device)
CollisionInitParams.Elements = VertexLayouts.CollisionLayout.GetLayout();
CollisionInitParams.PixelShaderFile = new ShaderInitParams.ShaderFileEntryPoint("CollisionPS.hlsl", "CollisionShader", "ps_5_0");
CollisionInitParams.VertexShaderFile = new ShaderInitParams.ShaderFileEntryPoint("CollisionVS.hlsl", "CollisionShader", "vs_5_0");
DefaultInitParams.InstancedVertexShaderFile = new ShaderInitParams.ShaderFileEntryPoint("LightVS.hlsl", "LightInstanceVertexShader", "vs_5_0");
CollisionInitParams.InstancedVertexShaderFile = new ShaderInitParams.ShaderFileEntryPoint("LightVS.hlsl", "LightInstanceVertexShader", "vs_5_0");

CollisionShader OurCollisionShader = new CollisionShader(device, CollisionInitParams);

Expand All @@ -42,7 +42,7 @@ public bool Init(ID3D11Device device)
Shader601151254_InitParams.Elements = VertexLayouts.NormalLayout.GetLayout();
Shader601151254_InitParams.PixelShaderFile = new ShaderInitParams.ShaderFileEntryPoint("LightPS.hlsl", "PS_601151254", "ps_5_0");
Shader601151254_InitParams.VertexShaderFile = new ShaderInitParams.ShaderFileEntryPoint("LightVS.hlsl", "LightVertexShader", "vs_5_0");
DefaultInitParams.InstancedVertexShaderFile = new ShaderInitParams.ShaderFileEntryPoint("LightVS.hlsl", "LightInstanceVertexShader", "vs_5_0");
Shader601151254_InitParams.InstancedVertexShaderFile = new ShaderInitParams.ShaderFileEntryPoint("LightVS.hlsl", "LightInstanceVertexShader", "vs_5_0");

Shader_601151254 OurShader601151254 = new Shader_601151254(device, Shader601151254_InitParams);

Expand All @@ -51,7 +51,7 @@ public bool Init(ID3D11Device device)
Shader_50760736_InitParams.Elements = VertexLayouts.NormalLayout.GetLayout();
Shader_50760736_InitParams.PixelShaderFile = new ShaderInitParams.ShaderFileEntryPoint("LightPS.hlsl", "PS_50760736", "ps_5_0");
Shader_50760736_InitParams.VertexShaderFile = new ShaderInitParams.ShaderFileEntryPoint("LightVS.hlsl", "LightVertexShader", "vs_5_0");
DefaultInitParams.InstancedVertexShaderFile = new ShaderInitParams.ShaderFileEntryPoint("LightVS.hlsl", "LightInstanceVertexShader", "vs_5_0");
Shader_50760736_InitParams.InstancedVertexShaderFile = new ShaderInitParams.ShaderFileEntryPoint("LightVS.hlsl", "LightInstanceVertexShader", "vs_5_0");

Shader_50760736 OurShader50760736 = new Shader_50760736(device, Shader_50760736_InitParams);

Expand Down
31 changes: 31 additions & 0 deletions Mafia2Libs/Rendering/Graphics/Shaders/DebugShader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,37 @@ public override void Render(ID3D11DeviceContext deviceContext, PrimitiveTopology
Profiler.NumDrawCallsThisFrame++;
}

public override void RenderInstanced(ID3D11DeviceContext context, PrimitiveTopology type, int size, int offset, int count)
{
context.IASetInputLayout(Layout);

// set shaders only if available
if (OurInstanceVertexShader != null)
{
context.VSSetShader(OurInstanceVertexShader);
}

if (OurInstanceVertexShader != null)
{
context.PSSetShader(OurPixelShader);
}

switch (type)
{
case PrimitiveTopology.LineList:
case PrimitiveTopology.TriangleList:
context.DrawIndexedInstanced(size, count, offset, 0, 0);
break;
case PrimitiveTopology.LineStrip:
//context.Draw(size, 0);
break;
default:
break;
}

Profiler.NumDrawCallsThisFrame++;
}

public override void SetShaderParameters(ID3D11Device device, ID3D11DeviceContext context, MaterialParameters material)
{
//empty
Expand Down
Loading
Loading