Program.cs 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
  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. Bind();
  100. int ubBindingPoint = 0;
  101. int sbBindingPoint = 0;
  102. int textureUnit = 0;
  103. int imageUnit = 0;
  104. for (int index = 0; index < shaders.Length; index++)
  105. {
  106. Shader shader = (Shader)shaders[index];
  107. foreach (BufferDescriptor descriptor in shader.Info.CBuffers)
  108. {
  109. int location = GL.GetUniformBlockIndex(Handle, descriptor.Name);
  110. if (location < 0)
  111. {
  112. continue;
  113. }
  114. GL.UniformBlockBinding(Handle, location, ubBindingPoint);
  115. int bpIndex = (int)shader.Stage << UbStageShift | descriptor.Slot;
  116. _ubBindingPoints[bpIndex] = ubBindingPoint;
  117. ubBindingPoint++;
  118. }
  119. foreach (BufferDescriptor descriptor in shader.Info.SBuffers)
  120. {
  121. int location = GL.GetProgramResourceIndex(Handle, ProgramInterface.ShaderStorageBlock, descriptor.Name);
  122. if (location < 0)
  123. {
  124. continue;
  125. }
  126. GL.ShaderStorageBlockBinding(Handle, location, sbBindingPoint);
  127. int bpIndex = (int)shader.Stage << SbStageShift | descriptor.Slot;
  128. _sbBindingPoints[bpIndex] = sbBindingPoint;
  129. sbBindingPoint++;
  130. }
  131. int samplerIndex = 0;
  132. foreach (TextureDescriptor descriptor in shader.Info.Textures)
  133. {
  134. int location = GL.GetUniformLocation(Handle, descriptor.Name);
  135. if (location < 0)
  136. {
  137. continue;
  138. }
  139. GL.Uniform1(location, textureUnit);
  140. int uIndex = (int)shader.Stage << TexStageShift | samplerIndex++;
  141. _textureUnits[uIndex] = textureUnit;
  142. textureUnit++;
  143. }
  144. int imageIndex = 0;
  145. foreach (TextureDescriptor descriptor in shader.Info.Images)
  146. {
  147. int location = GL.GetUniformLocation(Handle, descriptor.Name);
  148. if (location < 0)
  149. {
  150. continue;
  151. }
  152. GL.Uniform1(location, imageUnit);
  153. int uIndex = (int)shader.Stage << ImgStageShift | imageIndex++;
  154. _imageUnits[uIndex] = imageUnit;
  155. imageUnit++;
  156. }
  157. }
  158. FragmentIsBgraUniform = GL.GetUniformLocation(Handle, "is_bgra");
  159. FragmentRenderScaleUniform = GL.GetUniformLocation(Handle, "fp_renderScale");
  160. ComputeRenderScaleUniform = GL.GetUniformLocation(Handle, "cp_renderScale");
  161. }
  162. public void Bind()
  163. {
  164. GL.UseProgram(Handle);
  165. }
  166. public int GetUniformBufferBindingPoint(ShaderStage stage, int index)
  167. {
  168. return _ubBindingPoints[(int)stage << UbStageShift | index];
  169. }
  170. public int GetStorageBufferBindingPoint(ShaderStage stage, int index)
  171. {
  172. return _sbBindingPoints[(int)stage << SbStageShift | index];
  173. }
  174. public int GetTextureUnit(ShaderStage stage, int index)
  175. {
  176. return _textureUnits[(int)stage << TexStageShift | index];
  177. }
  178. public int GetImageUnit(ShaderStage stage, int index)
  179. {
  180. return _imageUnits[(int)stage << ImgStageShift | index];
  181. }
  182. private void CheckProgramLink()
  183. {
  184. GL.GetProgram(Handle, GetProgramParameterName.LinkStatus, out int status);
  185. if (status == 0)
  186. {
  187. // Use GL.GetProgramInfoLog(Handle), it may be too long to print on the log.
  188. Logger.PrintDebug(LogClass.Gpu, "Shader linking failed.");
  189. }
  190. else
  191. {
  192. IsLinked = true;
  193. }
  194. }
  195. public void Dispose()
  196. {
  197. if (Handle != 0)
  198. {
  199. GL.DeleteProgram(Handle);
  200. Handle = 0;
  201. }
  202. }
  203. }
  204. }