Program.cs 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. using OpenTK.Graphics.OpenGL;
  2. using Ryujinx.Common.Logging;
  3. using Ryujinx.Graphics.GAL;
  4. using Ryujinx.Graphics.Shader.CodeGen.Glsl;
  5. using System;
  6. using System.Buffers.Binary;
  7. using System.Collections.Generic;
  8. using System.Linq;
  9. namespace Ryujinx.Graphics.OpenGL
  10. {
  11. class Program : IProgram
  12. {
  13. public int Handle { get; private set; }
  14. public bool IsLinked
  15. {
  16. get
  17. {
  18. if (_status == ProgramLinkStatus.Incomplete)
  19. {
  20. CheckProgramLink(true);
  21. }
  22. return _status == ProgramLinkStatus.Success;
  23. }
  24. }
  25. private ProgramLinkStatus _status = ProgramLinkStatus.Incomplete;
  26. private IShader[] _shaders;
  27. public Program(IShader[] shaders, TransformFeedbackDescriptor[] transformFeedbackDescriptors)
  28. {
  29. Handle = GL.CreateProgram();
  30. GL.ProgramParameter(Handle, ProgramParameterName.ProgramBinaryRetrievableHint, 1);
  31. for (int index = 0; index < shaders.Length; index++)
  32. {
  33. int shaderHandle = ((Shader)shaders[index]).Handle;
  34. GL.AttachShader(Handle, shaderHandle);
  35. }
  36. if (transformFeedbackDescriptors != null)
  37. {
  38. List<string> varyings = new List<string>();
  39. int cbi = 0;
  40. foreach (var tfd in transformFeedbackDescriptors.OrderBy(x => x.BufferIndex))
  41. {
  42. if (tfd.VaryingLocations.Length == 0)
  43. {
  44. continue;
  45. }
  46. while (cbi < tfd.BufferIndex)
  47. {
  48. varyings.Add("gl_NextBuffer");
  49. cbi++;
  50. }
  51. int stride = Math.Min(128 * 4, (tfd.Stride + 3) & ~3);
  52. int j = 0;
  53. for (; j < tfd.VaryingLocations.Length && j * 4 < stride; j++)
  54. {
  55. byte location = tfd.VaryingLocations[j];
  56. varyings.Add(Varying.GetName(location) ?? "gl_SkipComponents1");
  57. j += Varying.GetSize(location) - 1;
  58. }
  59. int feedbackBytes = j * 4;
  60. while (feedbackBytes < stride)
  61. {
  62. int bytes = Math.Min(16, stride - feedbackBytes);
  63. varyings.Add($"gl_SkipComponents{(bytes / 4)}");
  64. feedbackBytes += bytes;
  65. }
  66. }
  67. GL.TransformFeedbackVaryings(Handle, varyings.Count, varyings.ToArray(), TransformFeedbackMode.InterleavedAttribs);
  68. }
  69. GL.LinkProgram(Handle);
  70. _shaders = shaders;
  71. }
  72. public Program(ReadOnlySpan<byte> code)
  73. {
  74. BinaryFormat binaryFormat = (BinaryFormat)BinaryPrimitives.ReadInt32LittleEndian(code.Slice(code.Length - 4, 4));
  75. Handle = GL.CreateProgram();
  76. unsafe
  77. {
  78. fixed (byte* ptr = code)
  79. {
  80. GL.ProgramBinary(Handle, binaryFormat, (IntPtr)ptr, code.Length - 4);
  81. }
  82. }
  83. }
  84. public void Bind()
  85. {
  86. GL.UseProgram(Handle);
  87. }
  88. public ProgramLinkStatus CheckProgramLink(bool blocking)
  89. {
  90. if (!blocking && HwCapabilities.SupportsParallelShaderCompile)
  91. {
  92. GL.GetProgram(Handle, (GetProgramParameterName)ArbParallelShaderCompile.CompletionStatusArb, out int completed);
  93. if (completed == 0)
  94. {
  95. return ProgramLinkStatus.Incomplete;
  96. }
  97. }
  98. GL.GetProgram(Handle, GetProgramParameterName.LinkStatus, out int status);
  99. if (_shaders != null)
  100. {
  101. for (int index = 0; index < _shaders.Length; index++)
  102. {
  103. int shaderHandle = ((Shader)_shaders[index]).Handle;
  104. GL.DetachShader(Handle, shaderHandle);
  105. }
  106. _shaders = null;
  107. }
  108. if (status == 0)
  109. {
  110. // Use GL.GetProgramInfoLog(Handle), it may be too long to print on the log.
  111. _status = ProgramLinkStatus.Failure;
  112. Logger.Debug?.Print(LogClass.Gpu, "Shader linking failed.");
  113. }
  114. else
  115. {
  116. _status = ProgramLinkStatus.Success;
  117. }
  118. return _status;
  119. }
  120. public byte[] GetBinary()
  121. {
  122. GL.GetProgram(Handle, (GetProgramParameterName)All.ProgramBinaryLength, out int size);
  123. byte[] data = new byte[size + 4];
  124. GL.GetProgramBinary(Handle, size, out _, out BinaryFormat binFormat, data);
  125. BinaryPrimitives.WriteInt32LittleEndian(data.AsSpan().Slice(size, 4), (int)binFormat);
  126. return data;
  127. }
  128. public void Dispose()
  129. {
  130. if (Handle != 0)
  131. {
  132. GL.DeleteProgram(Handle);
  133. Handle = 0;
  134. }
  135. }
  136. }
  137. }