Shader.cs 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  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. // TODO: We should generate the correct code on the shader translator instead of doing this compensation.
  61. glsl = glsl.Replace("gl_VertexID", "(gl_VertexIndex - gl_BaseVertex)");
  62. glsl = glsl.Replace("gl_InstanceID", "(gl_InstanceIndex - gl_BaseInstance)");
  63. Options options;
  64. lock (_shaderOptionsLock)
  65. {
  66. options = new Options(false)
  67. {
  68. SourceLanguage = SourceLanguage.Glsl,
  69. TargetSpirVVersion = new SpirVVersion(1, 5)
  70. };
  71. }
  72. options.SetTargetEnvironment(TargetEnvironment.Vulkan, EnvironmentVersion.Vulkan_1_2);
  73. Compiler compiler = new Compiler(options);
  74. var scr = compiler.Compile(glsl, "Ryu", GetShaderCShaderStage(stage));
  75. lock (_shaderOptionsLock)
  76. {
  77. options.Dispose();
  78. }
  79. if (scr.Status != Status.Success)
  80. {
  81. Logger.Error?.Print(LogClass.Gpu, $"Shader compilation error: {scr.Status} {scr.ErrorMessage}");
  82. return null;
  83. }
  84. var spirvBytes = new Span<byte>((void*)scr.CodePointer, (int)scr.CodeLength);
  85. byte[] code = new byte[(scr.CodeLength + 3) & ~3];
  86. spirvBytes.CopyTo(code.AsSpan().Slice(0, (int)scr.CodeLength));
  87. return code;
  88. }
  89. private static ShaderKind GetShaderCShaderStage(ShaderStage stage)
  90. {
  91. switch (stage)
  92. {
  93. case ShaderStage.Vertex:
  94. return ShaderKind.GlslVertexShader;
  95. case ShaderStage.Geometry:
  96. return ShaderKind.GlslGeometryShader;
  97. case ShaderStage.TessellationControl:
  98. return ShaderKind.GlslTessControlShader;
  99. case ShaderStage.TessellationEvaluation:
  100. return ShaderKind.GlslTessEvaluationShader;
  101. case ShaderStage.Fragment:
  102. return ShaderKind.GlslFragmentShader;
  103. case ShaderStage.Compute:
  104. return ShaderKind.GlslComputeShader;
  105. }
  106. Logger.Debug?.Print(LogClass.Gpu, $"Invalid {nameof(ShaderStage)} enum value: {stage}.");
  107. return ShaderKind.GlslVertexShader;
  108. }
  109. public unsafe PipelineShaderStageCreateInfo GetInfo()
  110. {
  111. return new PipelineShaderStageCreateInfo()
  112. {
  113. SType = StructureType.PipelineShaderStageCreateInfo,
  114. Stage = _stage,
  115. Module = _module,
  116. PName = (byte*)_ptrMainEntryPointName
  117. };
  118. }
  119. public void WaitForCompile()
  120. {
  121. CompileTask.Wait();
  122. }
  123. public unsafe void Dispose()
  124. {
  125. if (!_disposed)
  126. {
  127. _api.DestroyShaderModule(_device, _module, null);
  128. _disposed = true;
  129. }
  130. }
  131. }
  132. }