Skip to content

Commit

Permalink
Initial translokator spatial grid rendering code
Browse files Browse the repository at this point in the history
  • Loading branch information
Kamzik123 committed Dec 1, 2024
1 parent 7d73efc commit e729b5d
Show file tree
Hide file tree
Showing 8 changed files with 341 additions and 15 deletions.
2 changes: 1 addition & 1 deletion Mafia2Libs/Forms/MapEditor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1138,7 +1138,7 @@ private void BuildRenderObjects()
translokatorRoot.Nodes.Add(gridNode);

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

Expand Down
10 changes: 5 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,11 @@ public bool InitScene(int width, int height)
return true;
}

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

public TreeNode SetNavigationGrid(ResourceTypes.Navigation.OBJData[] data)
Expand Down
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

0 comments on commit e729b5d

Please sign in to comment.