Program.cs 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. using OpenTK.Graphics.OpenGL;
  2. using Ryujinx.Common.Logging;
  3. using Ryujinx.Graphics.GAL;
  4. using System;
  5. using System.Buffers.Binary;
  6. namespace Ryujinx.Graphics.OpenGL
  7. {
  8. class Program : IProgram
  9. {
  10. public int Handle { get; private set; }
  11. public bool IsLinked
  12. {
  13. get
  14. {
  15. if (_status == ProgramLinkStatus.Incomplete)
  16. {
  17. CheckProgramLink(true);
  18. }
  19. return _status == ProgramLinkStatus.Success;
  20. }
  21. }
  22. private ProgramLinkStatus _status = ProgramLinkStatus.Incomplete;
  23. private IShader[] _shaders;
  24. public bool HasFragmentShader;
  25. public int FragmentOutputMap { get; }
  26. public Program(IShader[] shaders, int fragmentOutputMap)
  27. {
  28. Handle = GL.CreateProgram();
  29. GL.ProgramParameter(Handle, ProgramParameterName.ProgramBinaryRetrievableHint, 1);
  30. for (int index = 0; index < shaders.Length; index++)
  31. {
  32. Shader shader = (Shader)shaders[index];
  33. if (shader.IsFragment)
  34. {
  35. HasFragmentShader = true;
  36. }
  37. GL.AttachShader(Handle, shader.Handle);
  38. }
  39. GL.LinkProgram(Handle);
  40. _shaders = shaders;
  41. FragmentOutputMap = fragmentOutputMap;
  42. }
  43. public Program(ReadOnlySpan<byte> code, bool hasFragmentShader, int fragmentOutputMap)
  44. {
  45. BinaryFormat binaryFormat = (BinaryFormat)BinaryPrimitives.ReadInt32LittleEndian(code.Slice(code.Length - 4, 4));
  46. Handle = GL.CreateProgram();
  47. unsafe
  48. {
  49. fixed (byte* ptr = code)
  50. {
  51. GL.ProgramBinary(Handle, binaryFormat, (IntPtr)ptr, code.Length - 4);
  52. }
  53. }
  54. HasFragmentShader = hasFragmentShader;
  55. FragmentOutputMap = fragmentOutputMap;
  56. }
  57. public void Bind()
  58. {
  59. GL.UseProgram(Handle);
  60. }
  61. public ProgramLinkStatus CheckProgramLink(bool blocking)
  62. {
  63. if (!blocking && HwCapabilities.SupportsParallelShaderCompile)
  64. {
  65. GL.GetProgram(Handle, (GetProgramParameterName)ArbParallelShaderCompile.CompletionStatusArb, out int completed);
  66. if (completed == 0)
  67. {
  68. return ProgramLinkStatus.Incomplete;
  69. }
  70. }
  71. GL.GetProgram(Handle, GetProgramParameterName.LinkStatus, out int status);
  72. if (_shaders != null)
  73. {
  74. for (int index = 0; index < _shaders.Length; index++)
  75. {
  76. int shaderHandle = ((Shader)_shaders[index]).Handle;
  77. GL.DetachShader(Handle, shaderHandle);
  78. }
  79. _shaders = null;
  80. }
  81. if (status == 0)
  82. {
  83. // Use GL.GetProgramInfoLog(Handle), it may be too long to print on the log.
  84. _status = ProgramLinkStatus.Failure;
  85. Logger.Debug?.Print(LogClass.Gpu, "Shader linking failed.");
  86. }
  87. else
  88. {
  89. _status = ProgramLinkStatus.Success;
  90. }
  91. return _status;
  92. }
  93. public byte[] GetBinary()
  94. {
  95. GL.GetProgram(Handle, (GetProgramParameterName)All.ProgramBinaryLength, out int size);
  96. byte[] data = new byte[size + 4];
  97. GL.GetProgramBinary(Handle, size, out _, out BinaryFormat binFormat, data);
  98. BinaryPrimitives.WriteInt32LittleEndian(data.AsSpan(size, 4), (int)binFormat);
  99. return data;
  100. }
  101. public void Dispose()
  102. {
  103. if (Handle != 0)
  104. {
  105. GL.DeleteProgram(Handle);
  106. Handle = 0;
  107. }
  108. }
  109. }
  110. }