Program.cs 8.3 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 FragmentRenderScaleUniform { get; }
  24. public int ComputeRenderScaleUniform { get; }
  25. public bool IsLinked { get; private set; }
  26. private int[] _ubBindingPoints;
  27. private int[] _sbBindingPoints;
  28. private int[] _textureUnits;
  29. private int[] _imageUnits;
  30. public Program(IShader[] shaders, TransformFeedbackDescriptor[] transformFeedbackDescriptors)
  31. {
  32. _ubBindingPoints = new int[UbsPerStage * ShaderStages];
  33. _sbBindingPoints = new int[SbsPerStage * ShaderStages];
  34. _textureUnits = new int[TexsPerStage * ShaderStages];
  35. _imageUnits = new int[ImgsPerStage * ShaderStages];
  36. for (int index = 0; index < _ubBindingPoints.Length; index++)
  37. {
  38. _ubBindingPoints[index] = -1;
  39. }
  40. for (int index = 0; index < _sbBindingPoints.Length; index++)
  41. {
  42. _sbBindingPoints[index] = -1;
  43. }
  44. for (int index = 0; index < _textureUnits.Length; index++)
  45. {
  46. _textureUnits[index] = -1;
  47. }
  48. for (int index = 0; index < _imageUnits.Length; index++)
  49. {
  50. _imageUnits[index] = -1;
  51. }
  52. Handle = GL.CreateProgram();
  53. for (int index = 0; index < shaders.Length; index++)
  54. {
  55. int shaderHandle = ((Shader)shaders[index]).Handle;
  56. GL.AttachShader(Handle, shaderHandle);
  57. }
  58. if (transformFeedbackDescriptors != null)
  59. {
  60. List<string> varyings = new List<string>();
  61. int cbi = 0;
  62. foreach (var tfd in transformFeedbackDescriptors.OrderBy(x => x.BufferIndex))
  63. {
  64. if (tfd.VaryingLocations.Length == 0)
  65. {
  66. continue;
  67. }
  68. while (cbi < tfd.BufferIndex)
  69. {
  70. varyings.Add("gl_NextBuffer");
  71. cbi++;
  72. }
  73. int stride = Math.Min(128 * 4, (tfd.Stride + 3) & ~3);
  74. int j = 0;
  75. for (; j < tfd.VaryingLocations.Length && j * 4 < stride; j++)
  76. {
  77. byte location = tfd.VaryingLocations[j];
  78. varyings.Add(Varying.GetName(location) ?? "gl_SkipComponents1");
  79. j += Varying.GetSize(location) - 1;
  80. }
  81. int feedbackBytes = j * 4;
  82. while (feedbackBytes < stride)
  83. {
  84. int bytes = Math.Min(16, stride - feedbackBytes);
  85. varyings.Add($"gl_SkipComponents{(bytes / 4)}");
  86. feedbackBytes += bytes;
  87. }
  88. }
  89. GL.TransformFeedbackVaryings(Handle, varyings.Count, varyings.ToArray(), TransformFeedbackMode.InterleavedAttribs);
  90. }
  91. GL.LinkProgram(Handle);
  92. for (int index = 0; index < shaders.Length; index++)
  93. {
  94. int shaderHandle = ((Shader)shaders[index]).Handle;
  95. GL.DetachShader(Handle, shaderHandle);
  96. }
  97. CheckProgramLink();
  98. Bind();
  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.Uniform1(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.Uniform1(location, imageUnit);
  152. int uIndex = (int)shader.Stage << ImgStageShift | imageIndex++;
  153. _imageUnits[uIndex] = imageUnit;
  154. imageUnit++;
  155. }
  156. }
  157. FragmentRenderScaleUniform = GL.GetUniformLocation(Handle, "fp_renderScale");
  158. ComputeRenderScaleUniform = GL.GetUniformLocation(Handle, "cp_renderScale");
  159. }
  160. public void Bind()
  161. {
  162. GL.UseProgram(Handle);
  163. }
  164. public int GetUniformBufferBindingPoint(ShaderStage stage, int index)
  165. {
  166. return _ubBindingPoints[(int)stage << UbStageShift | index];
  167. }
  168. public int GetStorageBufferBindingPoint(ShaderStage stage, int index)
  169. {
  170. return _sbBindingPoints[(int)stage << SbStageShift | index];
  171. }
  172. public int GetTextureUnit(ShaderStage stage, int index)
  173. {
  174. return _textureUnits[(int)stage << TexStageShift | index];
  175. }
  176. public int GetImageUnit(ShaderStage stage, int index)
  177. {
  178. return _imageUnits[(int)stage << ImgStageShift | index];
  179. }
  180. private void CheckProgramLink()
  181. {
  182. GL.GetProgram(Handle, GetProgramParameterName.LinkStatus, out int status);
  183. if (status == 0)
  184. {
  185. // Use GL.GetProgramInfoLog(Handle), it may be too long to print on the log.
  186. Logger.PrintDebug(LogClass.Gpu, "Shader linking failed.");
  187. }
  188. else
  189. {
  190. IsLinked = true;
  191. }
  192. }
  193. public void Dispose()
  194. {
  195. if (Handle != 0)
  196. {
  197. GL.DeleteProgram(Handle);
  198. Handle = 0;
  199. }
  200. }
  201. }
  202. }