/ src / Ryujinx.Graphics.Shader / CodeGen / Glsl / NumberFormatter.cs
NumberFormatter.cs
  1  using Ryujinx.Graphics.Shader.Translation;
  2  using System;
  3  using System.Globalization;
  4  
  5  namespace Ryujinx.Graphics.Shader.CodeGen.Glsl
  6  {
  7      static class NumberFormatter
  8      {
  9          private const int MaxDecimal = 256;
 10  
 11          public static bool TryFormat(int value, AggregateType dstType, out string formatted)
 12          {
 13              if (dstType == AggregateType.FP32)
 14              {
 15                  return TryFormatFloat(BitConverter.Int32BitsToSingle(value), out formatted);
 16              }
 17              else if (dstType == AggregateType.S32)
 18              {
 19                  formatted = FormatInt(value);
 20              }
 21              else if (dstType == AggregateType.U32)
 22              {
 23                  formatted = FormatUint((uint)value);
 24              }
 25              else if (dstType == AggregateType.Bool)
 26              {
 27                  formatted = value != 0 ? "true" : "false";
 28              }
 29              else
 30              {
 31                  throw new ArgumentException($"Invalid variable type \"{dstType}\".");
 32              }
 33  
 34              return true;
 35          }
 36  
 37          public static string FormatFloat(float value)
 38          {
 39              if (!TryFormatFloat(value, out string formatted))
 40              {
 41                  throw new ArgumentException("Failed to convert float value to string.");
 42              }
 43  
 44              return formatted;
 45          }
 46  
 47          public static bool TryFormatFloat(float value, out string formatted)
 48          {
 49              if (float.IsNaN(value) || float.IsInfinity(value))
 50              {
 51                  formatted = null;
 52  
 53                  return false;
 54              }
 55  
 56              formatted = value.ToString("G9", CultureInfo.InvariantCulture);
 57  
 58              if (!(formatted.Contains('.') ||
 59                    formatted.Contains('e') ||
 60                    formatted.Contains('E')))
 61              {
 62                  formatted += ".0";
 63              }
 64  
 65              return true;
 66          }
 67  
 68          public static string FormatInt(int value, AggregateType dstType)
 69          {
 70              if (dstType == AggregateType.S32)
 71              {
 72                  return FormatInt(value);
 73              }
 74              else if (dstType == AggregateType.U32)
 75              {
 76                  return FormatUint((uint)value);
 77              }
 78              else
 79              {
 80                  throw new ArgumentException($"Invalid variable type \"{dstType}\".");
 81              }
 82          }
 83  
 84          public static string FormatInt(int value)
 85          {
 86              if (value <= MaxDecimal && value >= -MaxDecimal)
 87              {
 88                  return value.ToString(CultureInfo.InvariantCulture);
 89              }
 90  
 91              return "0x" + value.ToString("X", CultureInfo.InvariantCulture);
 92          }
 93  
 94          public static string FormatUint(uint value)
 95          {
 96              if (value <= MaxDecimal && value >= 0)
 97              {
 98                  return value.ToString(CultureInfo.InvariantCulture) + "u";
 99              }
100  
101              return "0x" + value.ToString("X", CultureInfo.InvariantCulture) + "u";
102          }
103      }
104  }