FormatConverter.cs
1 using System; 2 using System.Runtime.InteropServices; 3 4 namespace Ryujinx.Graphics.Vulkan 5 { 6 class FormatConverter 7 { 8 public static void ConvertD24S8ToD32FS8(Span<byte> output, ReadOnlySpan<byte> input) 9 { 10 const float UnormToFloat = 1f / 0xffffff; 11 12 Span<uint> outputUint = MemoryMarshal.Cast<byte, uint>(output); 13 ReadOnlySpan<uint> inputUint = MemoryMarshal.Cast<byte, uint>(input); 14 15 int i = 0; 16 17 for (; i < inputUint.Length; i++) 18 { 19 uint depthStencil = inputUint[i]; 20 uint depth = depthStencil >> 8; 21 uint stencil = depthStencil & 0xff; 22 23 int j = i * 2; 24 25 outputUint[j] = (uint)BitConverter.SingleToInt32Bits(depth * UnormToFloat); 26 outputUint[j + 1] = stencil; 27 } 28 } 29 30 public static void ConvertD32FS8ToD24S8(Span<byte> output, ReadOnlySpan<byte> input) 31 { 32 Span<uint> outputUint = MemoryMarshal.Cast<byte, uint>(output); 33 ReadOnlySpan<uint> inputUint = MemoryMarshal.Cast<byte, uint>(input); 34 35 int i = 0; 36 37 for (; i < inputUint.Length; i += 2) 38 { 39 float depth = BitConverter.Int32BitsToSingle((int)inputUint[i]); 40 uint stencil = inputUint[i + 1]; 41 uint depthStencil = (Math.Clamp((uint)(depth * 0xffffff), 0, 0xffffff) << 8) | (stencil & 0xff); 42 43 int j = i >> 1; 44 45 outputUint[j] = depthStencil; 46 } 47 } 48 } 49 }