From 43c356780364b094bc5e05662061d19bddfd5349 Mon Sep 17 00:00:00 2001 From: alexguirre Date: Sat, 3 Aug 2024 15:07:41 +0200 Subject: [PATCH] Replace Dec3N vertex format with RGBA8 SNorm The game uses DXGI_FORMAT_R8G8B8A8_SNORM when building the D3D11 input-layout for what CW called Dec3N. Updated XML import/export to correctly read these values. --- .../GameFiles/Resources/Drawable.cs | 51 +++++++------------ .../GameFiles/Resources/VertexType.cs | 6 +-- CodeWalker/Rendering/VertexTypes.cs | 2 +- 3 files changed, 22 insertions(+), 37 deletions(-) diff --git a/CodeWalker.Core/GameFiles/Resources/Drawable.cs b/CodeWalker.Core/GameFiles/Resources/Drawable.cs index 10b26b2..56ce4cd 100644 --- a/CodeWalker.Core/GameFiles/Resources/Drawable.cs +++ b/CodeWalker.Core/GameFiles/Resources/Drawable.cs @@ -3779,7 +3779,7 @@ namespace CodeWalker.GameFiles case VertexComponentType.Float2: SetVector2(v, c, new Vector2(f(0), f(1))); break; case VertexComponentType.Float3: SetVector3(v, c, new Vector3(f(0), f(1), f(2))); break; case VertexComponentType.Float4: SetVector4(v, c, new Vector4(f(0), f(1), f(2), f(3))); break; - case VertexComponentType.Dec3N: SetDec3N(v, c, new Vector4(f(0), f(1), f(2), f(3))); break; + case VertexComponentType.RGBA8SNorm: SetRGBA8SNorm(v, c, new Vector4(f(0), f(1), f(2), f(3))); break; case VertexComponentType.Half2: SetHalf2(v, c, new Half2(f(0), f(1))); break; case VertexComponentType.Half4: SetHalf4(v, c, new Half4(f(0), f(1), f(2), f(3))); break; case VertexComponentType.Colour: SetColour(v, c, new Color(b(0), b(1), b(2), b(3))); break; @@ -3862,30 +3862,22 @@ namespace CodeWalker.GameFiles } } } - public void SetDec3N(int v, int c, Vector4 val) + public void SetRGBA8SNorm(int v, int c, Vector4 val) { - //see https://docs.microsoft.com/en-us/previous-versions/windows/desktop/bb322868(v%3Dvs.85) + // Equivalent to DXGI_FORMAT_R8G8B8A8_SNORM if ((Info != null) && (VertexBytes != null)) { var s = Info.Stride; var co = Info.GetComponentOffset(c); var o = (v * s) + co; - var e = o + 4;//sizeof(Dec3N) + var e = o + 4;//sizeof(RGBA8SNorm) if (e <= VertexBytes.Length) { - var sx = (val.X >= 0.0f); - var sy = (val.Y >= 0.0f); - var sz = (val.Z >= 0.0f); - var sw = (val.W >= 0.0f); - var x = Math.Min((uint)(Math.Abs(val.X) * 511.0f), 511); - var y = Math.Min((uint)(Math.Abs(val.Y) * 511.0f), 511); - var z = Math.Min((uint)(Math.Abs(val.Z) * 511.0f), 511); - var w = Math.Min((uint)(val.W), 2); - var ux = ((sx ? x : ~x) & 0x1FF) + (sx ? 0x200 : 0); - var uy = ((sy ? y : ~y) & 0x1FF) + (sy ? 0x200 : 0); - var uz = ((sz ? z : ~z) & 0x1FF) + (sz ? 0x200 : 0); - var uw = sw ? w : 3;//(0,1,2)=>(0,1,2); (-1)=>(3) - var u = ux + (uy << 10) + (uz << 20) + (uw << 30); + var x = (byte)Math.Max(-127.0f, Math.Min(val.X * 127.0f, 127.0f)); + var y = (byte)Math.Max(-127.0f, Math.Min(val.Y * 127.0f, 127.0f)); + var z = (byte)Math.Max(-127.0f, Math.Min(val.Z * 127.0f, 127.0f)); + var w = (byte)Math.Max(-127.0f, Math.Min(val.W * 127.0f, 127.0f)); + var u = x | (y << 8) | (z << 16) | (w << 24); var b = BitConverter.GetBytes(u); Buffer.BlockCopy(b, 0, VertexBytes, o, 4); } @@ -3973,7 +3965,7 @@ namespace CodeWalker.GameFiles case VertexComponentType.Float2: return FloatUtil.GetVector2String(GetVector2(v, c), d); case VertexComponentType.Float3: return FloatUtil.GetVector3String(GetVector3(v, c), d); case VertexComponentType.Float4: return FloatUtil.GetVector4String(GetVector4(v, c), d); - case VertexComponentType.Dec3N: return FloatUtil.GetVector4String(GetDec3N(v, c), d); + case VertexComponentType.RGBA8SNorm: return FloatUtil.GetVector4String(GetRGBA8SNorm(v, c), d); case VertexComponentType.Half2: return FloatUtil.GetHalf2String(GetHalf2(v, c), d); case VertexComponentType.Half4: return FloatUtil.GetHalf4String(GetHalf4(v, c), d); case VertexComponentType.Colour: return FloatUtil.GetColourString(GetColour(v, c), d); @@ -4054,29 +4046,22 @@ namespace CodeWalker.GameFiles } return Vector4.Zero; } - public Vector4 GetDec3N(int v, int c) + public Vector4 GetRGBA8SNorm(int v, int c) { - //see https://docs.microsoft.com/en-us/previous-versions/windows/desktop/bb322868(v%3Dvs.85) + // Equivalent to DXGI_FORMAT_R8G8B8A8_SNORM if ((Info != null) && (VertexBytes != null)) { var s = Info.Stride; var co = Info.GetComponentOffset(c); var o = (v * s) + co; - var e = o + 4;//sizeof(Dec3N) + var e = o + 4;//sizeof(RGBA8SNorm) if (e <= VertexBytes.Length) { - var u = BitConverter.ToUInt32(VertexBytes, o); - var ux = (u >> 0) & 0x3FF; - var uy = (u >> 10) & 0x3FF; - var uz = (u >> 20) & 0x3FF; - var uw = (u >> 30) & 0x3; - var sx = (ux & 0x200) > 0; - var sy = (uy & 0x200) > 0; - var sz = (uz & 0x200) > 0; - var x = ((sx ? ux : ~ux) & 0x1FF) / (sx ? 511.0f : -511.0f); - var y = ((sy ? uy : ~uy) & 0x1FF) / (sy ? 511.0f : -511.0f); - var z = ((sz ? uz : ~uz) & 0x1FF) / (sz ? 511.0f : -511.0f); - var w = (uw == 3) ? -1.0f : uw;//(0,1,2)=>(0,1,2); (3)=>(-1) + var xyzw = BitConverter.ToUInt32(VertexBytes, o); + var x = (sbyte)(xyzw & 0xFF) / 127.0f; + var y = (sbyte)((xyzw >> 8) & 0xFF) / 127.0f; + var z = (sbyte)((xyzw >> 16) & 0xFF) / 127.0f; + var w = (sbyte)((xyzw >> 24) & 0xFF) / 127.0f; return new Vector4(x, y, z, w); } } diff --git a/CodeWalker.Core/GameFiles/Resources/VertexType.cs b/CodeWalker.Core/GameFiles/Resources/VertexType.cs index 1b922be..644b0c9 100644 --- a/CodeWalker.Core/GameFiles/Resources/VertexType.cs +++ b/CodeWalker.Core/GameFiles/Resources/VertexType.cs @@ -19,7 +19,7 @@ namespace CodeWalker.GameFiles Float4 = 7, UByte4 = 8, Colour = 9, - Dec3N = 10, + RGBA8SNorm = 10, Unk1 = 11, Unk2 = 12, Unk3 = 13, @@ -43,7 +43,7 @@ namespace CodeWalker.GameFiles case VertexComponentType.Float4: return 16; case VertexComponentType.UByte4: return 4; case VertexComponentType.Colour: return 4; - case VertexComponentType.Dec3N: return 4; + case VertexComponentType.RGBA8SNorm: return 4; default: return 0; } } @@ -62,7 +62,7 @@ namespace CodeWalker.GameFiles case VertexComponentType.Float4: return 4; case VertexComponentType.UByte4: return 4; case VertexComponentType.Colour: return 4; - case VertexComponentType.Dec3N: return 4; + case VertexComponentType.RGBA8SNorm: return 4; default: return 0; } } diff --git a/CodeWalker/Rendering/VertexTypes.cs b/CodeWalker/Rendering/VertexTypes.cs index 8460107..1932136 100644 --- a/CodeWalker/Rendering/VertexTypes.cs +++ b/CodeWalker/Rendering/VertexTypes.cs @@ -46,7 +46,7 @@ namespace CodeWalker.Rendering case VertexComponentType.Float4: return Format.R32G32B32A32_Float; case VertexComponentType.UByte4: return Format.R8G8B8A8_UInt; case VertexComponentType.Colour: return Format.R8G8B8A8_UNorm; - case VertexComponentType.Dec3N: return Format.R10G10B10A2_UNorm; + case VertexComponentType.RGBA8SNorm: return Format.R8G8B8A8_SNorm; default: return Format.Unknown; } }