Program.cs 5.7 KB

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