Skip to content

Commit

Permalink
Small cleanup
Browse files Browse the repository at this point in the history
Cleaned up usings
Removed LightPS normal scale which was only used for debugging
Reduced LightVS input size to 64 bytes to fit into a single 64 byte cache line
Removed BVH debugging code
  • Loading branch information
Kamzik123 committed Nov 25, 2024
1 parent 1fdff45 commit a126a40
Show file tree
Hide file tree
Showing 6 changed files with 15 additions and 40 deletions.
8 changes: 3 additions & 5 deletions Mafia2Libs/Rendering/Core/InstanceGizmo.cs
Original file line number Diff line number Diff line change
@@ -1,19 +1,17 @@
using Rendering.Graphics;
using System;
using ResourceTypes.Translokator;
using System.Numerics;
using System.Runtime.CompilerServices;
using ResourceTypes.Translokator;
using Utils.Logging;
using Utils.VorticeUtils;
using Vortice.Direct3D;
using Vortice.Direct3D11;
using Vortice.Mathematics;

namespace Rendering.Core
{
public class InstanceGizmo
{

private float ModelScale = 0.015f;
// Variable for rendering
public RenderModel InstanceModel;

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

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

if (!InstanceModel.InstanceTransforms.ContainsKey(instance.RefID))
{
Expand Down
6 changes: 0 additions & 6 deletions Mafia2Libs/Rendering/Graphics/BoundingVolumeHierarchy.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ public class BVHNode
public int FirstTriangleIndex { get; set; }
public int TriangleCount { get; set; }
public bool IsLeaf { get => TriangleCount > 0; }
public RenderBoundingBox RenderObject { get; set; } = new(); //TODO: Remove this debugging code
}

public class BVH
Expand Down Expand Up @@ -76,8 +75,6 @@ public void Build(VertexLayouts.NormalLayout.Vertex[] vertices, uint[] indices)
Nodes[RootNodeID].TriangleCount = Triangles.Count;
UpdateNodeBounds(Nodes[RootNodeID]);

Nodes[RootNodeID].RenderObject.Init(Nodes[RootNodeID].Bounds);

// Start recursive splitting
SubdivideNode(Nodes[RootNodeID]);

Expand Down Expand Up @@ -208,9 +205,6 @@ public void SubdivideNode(BVHNode node)
UpdateNodeBounds(Nodes[LeftChildID]);
UpdateNodeBounds(Nodes[RightChildID]);

Nodes[LeftChildID].RenderObject.Init(Nodes[LeftChildID].Bounds);
Nodes[RightChildID].RenderObject.Init(Nodes[RightChildID].Bounds);

// Recursively split the child nodes
SubdivideNode(Nodes[LeftChildID]);
SubdivideNode(Nodes[RightChildID]);
Expand Down
2 changes: 1 addition & 1 deletion Mafia2Libs/Rendering/Graphics/GraphicsClass.cs
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
using Rendering.Core;
using Rendering.Input;
using ResourceTypes.FrameResource;
using ResourceTypes.Translokator;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Numerics;
using System.Threading.Tasks;
using System.Windows.Forms;
using ResourceTypes.FrameResource;
using Toolkit.Core;
using Utils.Models;
using Utils.Settings;
Expand Down
20 changes: 0 additions & 20 deletions Mafia2Libs/Rendering/Graphics/RenderTypes/RenderModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -181,11 +181,6 @@ public bool ConvertFrameToRenderModel(FrameObjectSingleMesh mesh, FrameGeometry
LODs[i] = lod;
}

//if (LODs.Length > 0)
//{
// BVH.Build(LODs[0].Vertices, LODs[0].Indices);
//}

SetupShaders();
return true;
}
Expand Down Expand Up @@ -265,11 +260,6 @@ public override void InitBuffers(ID3D11Device d3d, ID3D11DeviceContext d3dContex
vertexBuffer = d3d.CreateBuffer(BindFlags.VertexBuffer, LODs[0].Vertices, 0, ResourceUsage.Default, CpuAccessFlags.None);
indexBuffer = d3d.CreateBuffer(BindFlags.IndexBuffer, LODs[0].Indices, 0, ResourceUsage.Default, CpuAccessFlags.None);

//foreach (var node in BVH.Nodes)
//{
// node.RenderObject.InitBuffers(d3d, d3dContext); //For debugging, will be deleted later
//}

InitInstanceBuffer(d3d);

InitTextures(d3d, d3dContext);
Expand Down Expand Up @@ -343,11 +333,6 @@ public void ReloadInstanceBuffer(ID3D11Device d3d)
public override void SetTransform(Matrix4x4 matrix)
{
Transform = matrix;

//foreach (var node in BVH.Nodes)
//{
// node.RenderObject.SetTransform(matrix); //For debugging, will be deleted later
//}
}

public override void Render(ID3D11Device device, ID3D11DeviceContext deviceContext, Camera camera)
Expand Down Expand Up @@ -398,11 +383,6 @@ public override void Render(ID3D11Device device, ID3D11DeviceContext deviceConte
Segment.Shader.SetSceneVariables(deviceContext, Transform, camera);
Segment.Shader.Render(deviceContext, PrimitiveTopology.TriangleList, (int)(Segment.NumFaces * 3), Segment.StartIndex);
}

//foreach (BVHNode node in BVH.Nodes)
//{
// node.RenderObject.Render(device, deviceContext, camera); //For debugging, will be deleted later
//}
}

private float colorTransitionTime = 0.0f; // timer for distinguishing translokators
Expand Down
3 changes: 1 addition & 2 deletions Mafia2Libs/Shaders/LightPS.hlsl
Original file line number Diff line number Diff line change
Expand Up @@ -94,11 +94,10 @@ float4 CalculateColor(VS_OUTPUT input, float4 color)
float3 reflection;
float4 specular;
float3 normal = input.Normal;//float3(1.0f, 1.0f, 1.0f);
float normalMapScale = 5.0f;

if(hasTangentSpace == 1)
{
normal = CalculateFromNormalMap(input) * normalMapScale;
normal = CalculateFromNormalMap(input);
}

// Set the default output color to the ambient light value for all pixels.
Expand Down
16 changes: 10 additions & 6 deletions Mafia2Libs/Shaders/LightVS.hlsl
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,9 @@ cbuffer HighlightBuffer : register(b2)
// Instance transformation matrix buffer
StructuredBuffer<matrix> InstanceBuffer : register(t0);

struct VS_INPUT
struct VS_INPUT // Reduced size to fit into a single 64 byte cache line
{
float4 Position : POSITION;
float3 Position : POSITION;
float3 Normal : NORMAL;
float3 Tangent : TANGENT;
float3 Binormal : BINORMAL;
Expand All @@ -49,12 +49,14 @@ VS_OUTPUT LightVertexShader(VS_INPUT input)
float4 worldPosition;

// Change the position vector to be 4 units for proper matrix calculations.
input.Position.w = 1.0f;
float4 position;
position.xyz = input.Position.xyz;
position.w = 1.0f;
input.TexCoord0.y = -input.TexCoord0.y;
input.TexCoord7.y = -input.TexCoord7.y;

// Calculate the position of the vertex against the world, view, and projection matrices.
worldPosition = mul(input.Position, worldMatrix);
worldPosition = mul(position, worldMatrix);
output.Position = mul(worldPosition, viewProjectionMatrix);

// Store the texture coordinates for the pixel shader.
Expand Down Expand Up @@ -86,15 +88,17 @@ VS_OUTPUT LightInstanceVertexShader(VS_INPUT input, uint InstanceId : SV_Instanc
{
VS_OUTPUT output;

input.Position.w = 1.0f;
float4 position;
position.xyz = input.Position.xyz;
position.w = 1.0f;
input.TexCoord0.y = -input.TexCoord0.y;
input.TexCoord7.y = -input.TexCoord7.y;

// Fetch the instance transformation matrix using InstanceID
matrix instanceMatrix = InstanceBuffer[InstanceId];

// Transform position by instance matrix
float4 worldPosition = mul(input.Position, instanceMatrix);
float4 worldPosition = mul(position, instanceMatrix);
output.Position = mul(worldPosition, viewProjectionMatrix); // Apply view matrix

// Store the texture coordinates for the pixel shader
Expand Down

0 comments on commit a126a40

Please sign in to comment.