Program.cs 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275
  1. using OpenTK.Graphics.OpenGL;
  2. using Ryujinx.Common.Logging;
  3. using Ryujinx.Graphics.GAL;
  4. using Ryujinx.Graphics.Shader;
  5. using Ryujinx.Graphics.Shader.CodeGen.Glsl;
  6. using System;
  7. using System.Collections.Generic;
  8. using System.Linq;
  9. namespace Ryujinx.Graphics.OpenGL
  10. {
  11. class Program : IProgram
  12. {
  13. private const int ShaderStages = 6;
  14. private const int UbStageShift = 5;
  15. private const int SbStageShift = 4;
  16. private const int TexStageShift = 5;
  17. private const int ImgStageShift = 3;
  18. private const int UbsPerStage = 1 << UbStageShift;
  19. private const int SbsPerStage = 1 << SbStageShift;
  20. private const int TexsPerStage = 1 << TexStageShift;
  21. private const int ImgsPerStage = 1 << ImgStageShift;
  22. public int Handle { get; private set; }
  23. public int FragmentIsBgraUniform { get; }
  24. public int FragmentRenderScaleUniform { get; }
  25. public int ComputeRenderScaleUniform { get; }
  26. public bool IsLinked { get; private set; }
  27. private int[] _ubBindingPoints;
  28. private int[] _sbBindingPoints;
  29. private int[] _textureUnits;
  30. private int[] _imageUnits;
  31. public Program(IShader[] shaders, TransformFeedbackDescriptor[] transformFeedbackDescriptors)
  32. {
  33. _ubBindingPoints = new int[UbsPerStage * ShaderStages];
  34. _sbBindingPoints = new int[SbsPerStage * ShaderStages];
  35. _textureUnits = new int[TexsPerStage * ShaderStages];
  36. _imageUnits = new int[ImgsPerStage * ShaderStages];
  37. for (int index = 0; index < _ubBindingPoints.Length; index++)
  38. {
  39. _ubBindingPoints[index] = -1;
  40. }
  41. for (int index = 0; index < _sbBindingPoints.Length; index++)
  42. {
  43. _sbBindingPoints[index] = -1;
  44. }
  45. for (int index = 0; index < _textureUnits.Length; index++)
  46. {
  47. _textureUnits[index] = -1;
  48. }
  49. for (int index = 0; index < _imageUnits.Length; index++)
  50. {
  51. _imageUnits[index] = -1;
  52. }
  53. Handle = GL.CreateProgram();
  54. for (int index = 0; index < shaders.Length; index++)
  55. {
  56. int shaderHandle = ((Shader)shaders[index]).Handle;
  57. GL.AttachShader(Handle, shaderHandle);
  58. }
  59. if (transformFeedbackDescriptors != null)
  60. {
  61. List<string> varyings = new List<string>();
  62. int cbi = 0;
  63. foreach (var tfd in transformFeedbackDescriptors.OrderBy(x => x.BufferIndex))
  64. {
  65. if (tfd.VaryingLocations.Length == 0)
  66. {
  67. continue;
  68. }
  69. while (cbi < tfd.BufferIndex)
  70. {
  71. varyings.Add("gl_NextBuffer");
  72. cbi++;
  73. }
  74. int stride = Math.Min(128 * 4, (tfd.Stride + 3) & ~3);
  75. int j = 0;
  76. for (; j < tfd.VaryingLocations.Length && j * 4 < stride; j++)
  77. {
  78. byte location = tfd.VaryingLocations[j];
  79. varyings.Add(Varying.GetName(location) ?? "gl_SkipComponents1");
  80. j += Varying.GetSize(location) - 1;
  81. }
  82. int feedbackBytes = j * 4;
  83. while (feedbackBytes < stride)
  84. {
  85. int bytes = Math.Min(16, stride - feedbackBytes);
  86. varyings.Add($"gl_SkipComponents{(bytes / 4)}");
  87. feedbackBytes += bytes;
  88. }
  89. }
  90. GL.TransformFeedbackVaryings(Handle, varyings.Count, varyings.ToArray(), TransformFeedbackMode.InterleavedAttribs);
  91. }
  92. GL.LinkProgram(Handle);
  93. for (int index = 0; index < shaders.Length; index++)
  94. {
  95. int shaderHandle = ((Shader)shaders[index]).Handle;
  96. GL.DetachShader(Handle, shaderHandle);
  97. }
  98. CheckProgramLink();
  99. int ubBindingPoint = 0;
  100. int sbBindingPoint = 0;
  101. int textureUnit = 0;
  102. int imageUnit = 0;
  103. for (int index = 0; index < shaders.Length; index++)
  104. {
  105. Shader shader = (Shader)shaders[index];
  106. foreach (BufferDescriptor descriptor in shader.Info.CBuffers)
  107. {
  108. int location = GL.GetUniformBlockIndex(Handle, descriptor.Name);
  109. if (location < 0)
  110. {
  111. continue;
  112. }
  113. GL.UniformBlockBinding(Handle, location, ubBindingPoint);
  114. int bpIndex = (int)shader.Stage << UbStageShift | descriptor.Slot;
  115. _ubBindingPoints[bpIndex] = ubBindingPoint;
  116. ubBindingPoint++;
  117. }
  118. foreach (BufferDescriptor descriptor in shader.Info.SBuffers)
  119. {
  120. int location = GL.GetProgramResourceIndex(Handle, ProgramInterface.ShaderStorageBlock, descriptor.Name);
  121. if (location < 0)
  122. {
  123. continue;
  124. }
  125. GL.ShaderStorageBlockBinding(Handle, location, sbBindingPoint);
  126. int bpIndex = (int)shader.Stage << SbStageShift | descriptor.Slot;
  127. _sbBindingPoints[bpIndex] = sbBindingPoint;
  128. sbBindingPoint++;
  129. }
  130. int samplerIndex = 0;
  131. foreach (TextureDescriptor descriptor in shader.Info.Textures)
  132. {
  133. int location = GL.GetUniformLocation(Handle, descriptor.Name);
  134. if (location < 0)
  135. {
  136. continue;
  137. }
  138. GL.ProgramUniform1(Handle, location, textureUnit);
  139. int uIndex = (int)shader.Stage << TexStageShift | samplerIndex++;
  140. _textureUnits[uIndex] = textureUnit;
  141. textureUnit++;
  142. }
  143. int imageIndex = 0;
  144. foreach (TextureDescriptor descriptor in shader.Info.Images)
  145. {
  146. int location = GL.GetUniformLocation(Handle, descriptor.Name);
  147. if (location < 0)
  148. {
  149. continue;
  150. }
  151. GL.ProgramUniform1(Handle, location, imageUnit);
  152. int uIndex = (int)shader.Stage << ImgStageShift | imageIndex++;
  153. _imageUnits[uIndex] = imageUnit;
  154. imageUnit++;
  155. }
  156. }
  157. FragmentIsBgraUniform = GL.GetUniformLocation(Handle, "is_bgra");
  158. FragmentRenderScaleUniform = GL.GetUniformLocation(Handle, "fp_renderScale");
  159. ComputeRenderScaleUniform = GL.GetUniformLocation(Handle, "cp_renderScale");
  160. }
  161. public void Bind()
  162. {
  163. GL.UseProgram(Handle);
  164. }
  165. public int GetUniformBufferBindingPoint(ShaderStage stage, int index)
  166. {
  167. return _ubBindingPoints[(int)stage << UbStageShift | index];
  168. }
  169. public int GetStorageBufferBindingPoint(ShaderStage stage, int index)
  170. {
  171. return _sbBindingPoints[(int)stage << SbStageShift | index];
  172. }
  173. public int GetTextureUnit(ShaderStage stage, int index)
  174. {
  175. return _textureUnits[(int)stage << TexStageShift | index];
  176. }
  177. public int GetImageUnit(ShaderStage stage, int index)
  178. {
  179. return _imageUnits[(int)stage << ImgStageShift | index];
  180. }
  181. private void CheckProgramLink()
  182. {
  183. GL.GetProgram(Handle, GetProgramParameterName.LinkStatus, out int status);
  184. if (status == 0)
  185. {
  186. // Use GL.GetProgramInfoLog(Handle), it may be too long to print on the log.
  187. Logger.Debug?.Print(LogClass.Gpu, "Shader linking failed.");
  188. }
  189. else
  190. {
  191. IsLinked = true;
  192. }
  193. }
  194. public void Dispose()
  195. {
  196. if (Handle != 0)
  197. {
  198. GL.DeleteProgram(Handle);
  199. Handle = 0;
  200. }
  201. }
  202. }
  203. }