Skip to content

Commit

Permalink
using SSBOs for unlimited bones
Browse files Browse the repository at this point in the history
  • Loading branch information
4sval committed Feb 8, 2023
1 parent 1ca18c3 commit 9da407e
Show file tree
Hide file tree
Showing 8 changed files with 48 additions and 20 deletions.
1 change: 0 additions & 1 deletion FModel/Constants.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ public static class Constants

public const float SCALE_DOWN_RATIO = 0.01F;
public const int SAMPLES_COUNT = 4;
public const int MAX_BONE_UNIFORM = 250;

public const string WHITE = "#DAE5F2";
public const string GRAY = "#BBBBBB";
Expand Down
5 changes: 4 additions & 1 deletion FModel/Resources/default.vert
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,10 @@ layout (location = 14) in vec3 vMorphTargetTangent;
uniform mat4 uView;
uniform mat4 uProjection;
uniform float uMorphTime;
uniform mat4 uFinalBonesMatrix[250];
layout(std430, binding = 1) buffer layoutName
{
mat4 uFinalBonesMatrix[];
};

out vec3 fPos;
out vec3 fNormal;
Expand Down
5 changes: 4 additions & 1 deletion FModel/Resources/outline.vert
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,10 @@ uniform mat4 uView;
uniform vec3 uViewPos;
uniform mat4 uProjection;
uniform float uMorphTime;
uniform mat4 uFinalBonesMatrix[250];
layout(std430, binding = 1) buffer layoutName
{
mat4 uFinalBonesMatrix[];
};

void main()
{
Expand Down
5 changes: 4 additions & 1 deletion FModel/Resources/picking.vert
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,10 @@ layout (location = 13) in vec3 vMorphTargetPos;
uniform mat4 uView;
uniform mat4 uProjection;
uniform float uMorphTime;
uniform mat4 uFinalBonesMatrix[250];
layout(std430, binding = 1) buffer layoutName
{
mat4 uFinalBonesMatrix[];
};

void main()
{
Expand Down
7 changes: 7 additions & 0 deletions FModel/Views/Snooper/Buffers/BufferObject.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,13 @@ public void Bind()
GL.BindBuffer(_bufferTarget, _handle);
}

public void BindBufferBase(int index)
{
if (_bufferTarget != BufferTarget.ShaderStorageBuffer)
throw new ArgumentException("BindBufferBase is not allowed for anything but Shader Storage Buffers");
GL.BindBufferBase(BufferRangeTarget.ShaderStorageBuffer, index, _handle);
}

public void Unbind()
{
GL.BindBuffer(_bufferTarget, 0);
Expand Down
38 changes: 25 additions & 13 deletions FModel/Views/Snooper/Models/Animations/Skeleton.cs
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Numerics;
using CUE4Parse_Conversion.Animations;
using CUE4Parse.UE4.Assets.Exports.Animation;
using CUE4Parse.UE4.Objects.UObject;
using FModel.Views.Snooper.Buffers;
using FModel.Views.Snooper.Shading;
using OpenTK.Graphics.OpenGL4;
using Serilog;

namespace FModel.Views.Snooper.Models.Animations;
Expand All @@ -17,12 +20,15 @@ public struct BoneIndice

public class Skeleton : IDisposable
{
private int _handle;
private BufferObject<Matrix4x4> _ssbo;

public readonly USkeleton UnrealSkeleton;
public readonly bool IsLoaded;

public readonly Dictionary<string, BoneIndice> BonesIndicesByLoweredName;
public readonly Dictionary<int, Transform> BonesTransformByIndex;
public readonly Dictionary<int, Matrix4x4> InvertedBonesMatrixByIndex;
public readonly Matrix4x4[] InvertedBonesMatrixByIndex;

public Animation Anim;
public bool HasAnim => Anim != null;
Expand All @@ -31,7 +37,7 @@ public Skeleton()
{
BonesIndicesByLoweredName = new Dictionary<string, BoneIndice>();
BonesTransformByIndex = new Dictionary<int, Transform>();
InvertedBonesMatrixByIndex = new Dictionary<int, Matrix4x4>();
InvertedBonesMatrixByIndex = Array.Empty<Matrix4x4>();
}

public Skeleton(FPackageIndex package, FReferenceSkeleton referenceSkeleton) : this()
Expand All @@ -55,6 +61,7 @@ public Skeleton(FPackageIndex package, FReferenceSkeleton referenceSkeleton) : t
}
#endif

InvertedBonesMatrixByIndex = new Matrix4x4[BonesIndicesByLoweredName.Count];
foreach (var boneIndices in BonesIndicesByLoweredName.Values)
{
var bone = referenceSkeleton.FinalRefBonePose[boneIndices.Index];
Expand Down Expand Up @@ -84,39 +91,44 @@ public void SetAnimation(CAnimSet anim, bool rotationOnly)
Anim = new Animation(this, anim, rotationOnly);
}

public void SetUniform(Shader shader, float deltaSeconds = 0f, bool update = false)
public void Setup()
{
_handle = GL.CreateProgram();
_ssbo = new BufferObject<Matrix4x4>(InvertedBonesMatrixByIndex, BufferTarget.ShaderStorageBuffer);
}

public void Render(float deltaSeconds = 0f, bool update = false)
{
if (!IsLoaded) return;

_ssbo.BindBufferBase(1);

if (!HasAnim)
{
foreach (var boneIndex in BonesTransformByIndex.Keys)
{
if (boneIndex >= Constants.MAX_BONE_UNIFORM)
break;
shader.SetUniform($"uFinalBonesMatrix[{boneIndex}]", Matrix4x4.Identity);
_ssbo.Update(boneIndex, Matrix4x4.Identity);
}
}
else
{
if (update) Anim.Update(deltaSeconds);
foreach (var boneIndex in BonesTransformByIndex.Keys)
{
if (boneIndex >= Constants.MAX_BONE_UNIFORM)
break;
if (!InvertedBonesMatrixByIndex.TryGetValue(boneIndex, out var invertMatrix))
throw new ArgumentNullException($"no inverse matrix for bone '{boneIndex}'");

shader.SetUniform($"uFinalBonesMatrix[{boneIndex}]", invertMatrix * Anim.InterpolateBoneTransform(boneIndex));
_ssbo.Update(boneIndex, InvertedBonesMatrixByIndex[boneIndex] * Anim.InterpolateBoneTransform(boneIndex));
}
if (update) Anim.CheckForNextSequence();
}

_ssbo.Unbind();
}

public void Dispose()
{
BonesIndicesByLoweredName.Clear();
BonesTransformByIndex.Clear();
InvertedBonesMatrixByIndex.Clear();
Anim?.Dispose();

GL.DeleteProgram(_handle);
}
}
5 changes: 3 additions & 2 deletions FModel/Views/Snooper/Models/Model.cs
Original file line number Diff line number Diff line change
Expand Up @@ -348,6 +348,7 @@ public void Setup(Options options)
Materials[i].Setup(options, broken ? 1 : UvCount);
}

if (HasSkeleton) Skeleton.Setup();
if (HasMorphTargets)
{
for (uint morph = 0; morph < Morphs.Length; morph++)
Expand Down Expand Up @@ -382,7 +383,7 @@ public void Render(float deltaSeconds, Shader shader, bool outline = false)

_vao.Bind();
shader.SetUniform("uMorphTime", MorphTime);
if (HasSkeleton) Skeleton.SetUniform(shader, deltaSeconds, !outline);
if (HasSkeleton) Skeleton.Render(deltaSeconds, !outline);
if (!outline)
{
shader.SetUniform("uUvCount", UvCount);
Expand Down Expand Up @@ -413,7 +414,7 @@ public void PickingRender(Shader shader)

_vao.Bind();
shader.SetUniform("uMorphTime", MorphTime);
if (HasSkeleton) Skeleton.SetUniform(shader);
if (HasSkeleton) Skeleton.Render();

foreach (var section in Sections)
{
Expand Down
2 changes: 1 addition & 1 deletion FModel/Views/Snooper/SnimGui.cs
Original file line number Diff line number Diff line change
Expand Up @@ -452,7 +452,7 @@ private void DrawDetails(Snooper s)
if (model.HasSkeleton)
{
Layout("Skeleton");ImGui.Text($" : {model.Skeleton.UnrealSkeleton.Name}");
Layout("Bones");ImGui.Text($" : x{model.Skeleton.InvertedBonesMatrixByIndex.Count}");
Layout("Bones");ImGui.Text($" : x{model.Skeleton.InvertedBonesMatrixByIndex.Length}");
}
else
{
Expand Down

0 comments on commit 9da407e

Please sign in to comment.