Shader.cs 5.0 KB

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