Shader.cs 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. using Ryujinx.Common.Logging;
  2. using Ryujinx.Graphics.GAL;
  3. using Ryujinx.Graphics.Shader;
  4. using shaderc;
  5. using Silk.NET.Vulkan;
  6. using System;
  7. using System.Runtime.InteropServices;
  8. using System.Threading;
  9. using System.Threading.Tasks;
  10. namespace Ryujinx.Graphics.Vulkan
  11. {
  12. class Shader : IDisposable
  13. {
  14. // The shaderc.net dependency's Options constructor and dispose are not thread safe.
  15. // Take this lock when using them.
  16. private static readonly Lock _shaderOptionsLock = new();
  17. private static readonly nint _ptrMainEntryPointName = Marshal.StringToHGlobalAnsi("main");
  18. private readonly Vk _api;
  19. private readonly Device _device;
  20. private readonly ShaderStageFlags _stage;
  21. private bool _disposed;
  22. private ShaderModule _module;
  23. public ShaderStageFlags StageFlags => _stage;
  24. public ProgramLinkStatus CompileStatus { private set; get; }
  25. public readonly Task CompileTask;
  26. public unsafe Shader(Vk api, Device device, ShaderSource shaderSource)
  27. {
  28. _api = api;
  29. _device = device;
  30. CompileStatus = ProgramLinkStatus.Incomplete;
  31. _stage = shaderSource.Stage.Convert();
  32. CompileTask = Task.Run(() =>
  33. {
  34. byte[] spirv = shaderSource.BinaryCode;
  35. if (spirv == null)
  36. {
  37. spirv = GlslToSpirv(shaderSource.Code, shaderSource.Stage);
  38. if (spirv == null)
  39. {
  40. CompileStatus = ProgramLinkStatus.Failure;
  41. return;
  42. }
  43. }
  44. fixed (byte* pCode = spirv)
  45. {
  46. var shaderModuleCreateInfo = new ShaderModuleCreateInfo
  47. {
  48. SType = StructureType.ShaderModuleCreateInfo,
  49. CodeSize = (uint)spirv.Length,
  50. PCode = (uint*)pCode,
  51. };
  52. api.CreateShaderModule(device, in shaderModuleCreateInfo, null, out _module).ThrowOnError();
  53. }
  54. CompileStatus = ProgramLinkStatus.Success;
  55. });
  56. }
  57. private unsafe static byte[] GlslToSpirv(string glsl, ShaderStage stage)
  58. {
  59. Options options;
  60. lock (_shaderOptionsLock)
  61. {
  62. options = new Options(false)
  63. {
  64. SourceLanguage = SourceLanguage.Glsl,
  65. TargetSpirVVersion = new SpirVVersion(1, 5),
  66. };
  67. }
  68. options.SetTargetEnvironment(TargetEnvironment.Vulkan, EnvironmentVersion.Vulkan_1_2);
  69. Compiler compiler = new(options);
  70. var scr = compiler.Compile(glsl, "Ryu", GetShaderCShaderStage(stage));
  71. lock (_shaderOptionsLock)
  72. {
  73. options.Dispose();
  74. }
  75. if (scr.Status != Status.Success)
  76. {
  77. Logger.Error?.Print(LogClass.Gpu, $"Shader compilation error: {scr.Status} {scr.ErrorMessage}");
  78. return null;
  79. }
  80. var spirvBytes = new Span<byte>((void*)scr.CodePointer, (int)scr.CodeLength);
  81. byte[] code = new byte[(scr.CodeLength + 3) & ~3];
  82. spirvBytes.CopyTo(code.AsSpan()[..(int)scr.CodeLength]);
  83. return code;
  84. }
  85. private static ShaderKind GetShaderCShaderStage(ShaderStage stage)
  86. {
  87. switch (stage)
  88. {
  89. case ShaderStage.Vertex:
  90. return ShaderKind.GlslVertexShader;
  91. case ShaderStage.Geometry:
  92. return ShaderKind.GlslGeometryShader;
  93. case ShaderStage.TessellationControl:
  94. return ShaderKind.GlslTessControlShader;
  95. case ShaderStage.TessellationEvaluation:
  96. return ShaderKind.GlslTessEvaluationShader;
  97. case ShaderStage.Fragment:
  98. return ShaderKind.GlslFragmentShader;
  99. case ShaderStage.Compute:
  100. return ShaderKind.GlslComputeShader;
  101. }
  102. Logger.Debug?.Print(LogClass.Gpu, $"Invalid {nameof(ShaderStage)} enum value: {stage}.");
  103. return ShaderKind.GlslVertexShader;
  104. }
  105. public unsafe PipelineShaderStageCreateInfo GetInfo()
  106. {
  107. return new PipelineShaderStageCreateInfo
  108. {
  109. SType = StructureType.PipelineShaderStageCreateInfo,
  110. Stage = _stage,
  111. Module = _module,
  112. PName = (byte*)_ptrMainEntryPointName,
  113. };
  114. }
  115. public void WaitForCompile()
  116. {
  117. CompileTask.Wait();
  118. }
  119. public unsafe void Dispose()
  120. {
  121. if (!_disposed)
  122. {
  123. _api.DestroyShaderModule(_device, _module, null);
  124. _disposed = true;
  125. }
  126. }
  127. }
  128. }