FormatConverter.cs 1.5 KB

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