Program.cs 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. using CommandLine;
  2. using Ryujinx.Graphics.Shader;
  3. using Ryujinx.Graphics.Shader.Translation;
  4. using System;
  5. using System.IO;
  6. using System.Runtime.InteropServices;
  7. namespace Ryujinx.ShaderTools
  8. {
  9. class Program
  10. {
  11. private class GpuAccessor : IGpuAccessor
  12. {
  13. private const int DefaultArrayLength = 32;
  14. private readonly byte[] _data;
  15. private int _texturesCount;
  16. private int _imagesCount;
  17. public GpuAccessor(byte[] data)
  18. {
  19. _data = data;
  20. _texturesCount = 0;
  21. _imagesCount = 0;
  22. }
  23. public SetBindingPair CreateConstantBufferBinding(int index)
  24. {
  25. return new SetBindingPair(0, index + 1);
  26. }
  27. public SetBindingPair CreateImageBinding(int count, bool isBuffer)
  28. {
  29. int binding = _imagesCount;
  30. _imagesCount += count;
  31. return new SetBindingPair(3, binding);
  32. }
  33. public SetBindingPair CreateStorageBufferBinding(int index)
  34. {
  35. return new SetBindingPair(1, index);
  36. }
  37. public SetBindingPair CreateTextureBinding(int count, bool isBuffer)
  38. {
  39. int binding = _texturesCount;
  40. _texturesCount += count;
  41. return new SetBindingPair(2, binding);
  42. }
  43. public ReadOnlySpan<ulong> GetCode(ulong address, int minimumSize)
  44. {
  45. return MemoryMarshal.Cast<byte, ulong>(new ReadOnlySpan<byte>(_data)[(int)address..]);
  46. }
  47. public int QuerySamplerArrayLengthFromPool()
  48. {
  49. return DefaultArrayLength;
  50. }
  51. public int QueryTextureArrayLengthFromBuffer(int slot)
  52. {
  53. return DefaultArrayLength;
  54. }
  55. public int QueryTextureArrayLengthFromPool()
  56. {
  57. return DefaultArrayLength;
  58. }
  59. }
  60. private class Options
  61. {
  62. [Option("compute", Required = false, Default = false, HelpText = "Indicate that the shader is a compute shader.")]
  63. public bool Compute { get; set; }
  64. [Option("vertex-as-compute", Required = false, Default = false, HelpText = "Indicate that the shader is a vertex shader and should be converted to compute.")]
  65. public bool VertexAsCompute { get; set; }
  66. [Option("vertex-passthrough", Required = false, Default = false, HelpText = "Indicate that the shader is a vertex passthrough shader for compute output.")]
  67. public bool VertexPassthrough { get; set; }
  68. [Option("target-language", Required = false, Default = TargetLanguage.Glsl, HelpText = "Indicate the target shader language to use.")]
  69. public TargetLanguage TargetLanguage { get; set; }
  70. [Option("target-api", Required = false, Default = TargetApi.OpenGL, HelpText = "Indicate the target graphics api to use.")]
  71. public TargetApi TargetApi { get; set; }
  72. [Value(0, MetaName = "input", HelpText = "Binary Maxwell shader input path.", Required = true)]
  73. public string InputPath { get; set; }
  74. [Value(1, MetaName = "output", HelpText = "Decompiled shader output path.", Required = false)]
  75. public string OutputPath { get; set; }
  76. }
  77. static void HandleArguments(Options options)
  78. {
  79. TranslationFlags flags = TranslationFlags.DebugMode;
  80. if (options.Compute)
  81. {
  82. flags |= TranslationFlags.Compute;
  83. }
  84. byte[] data = File.ReadAllBytes(options.InputPath);
  85. TranslationOptions translationOptions = new(options.TargetLanguage, options.TargetApi, flags);
  86. TranslatorContext translatorContext = Translator.CreateContext(0, new GpuAccessor(data), translationOptions);
  87. ShaderProgram program;
  88. if (options.VertexPassthrough)
  89. {
  90. (program, _) = translatorContext.GenerateVertexPassthroughForCompute();
  91. }
  92. else
  93. {
  94. program = translatorContext.Translate(options.VertexAsCompute);
  95. }
  96. if (options.OutputPath == null)
  97. {
  98. if (program.BinaryCode != null)
  99. {
  100. using Stream outputStream = Console.OpenStandardOutput();
  101. outputStream.Write(program.BinaryCode);
  102. }
  103. else
  104. {
  105. Console.WriteLine(program.Code);
  106. }
  107. }
  108. else
  109. {
  110. if (program.BinaryCode != null)
  111. {
  112. File.WriteAllBytes(options.OutputPath, program.BinaryCode);
  113. }
  114. else
  115. {
  116. File.WriteAllText(options.OutputPath, program.Code);
  117. }
  118. }
  119. }
  120. static void Main(string[] args)
  121. {
  122. Parser.Default.ParseArguments<Options>(args)
  123. .WithParsed(options => HandleArguments(options))
  124. .WithNotParsed(errors => errors.Output());
  125. }
  126. }
  127. }