Program.cs 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  1. using Ryujinx.Graphics.GAL;
  2. using Ryujinx.Graphics.Shader;
  3. using OpenTK.Graphics.OpenGL;
  4. namespace Ryujinx.Graphics.OpenGL
  5. {
  6. class Program : IProgram
  7. {
  8. private const int ShaderStages = 6;
  9. private const int UbStageShift = 5;
  10. private const int SbStageShift = 4;
  11. private const int TexStageShift = 5;
  12. private const int ImgStageShift = 3;
  13. private const int UbsPerStage = 1 << UbStageShift;
  14. private const int SbsPerStage = 1 << SbStageShift;
  15. private const int TexsPerStage = 1 << TexStageShift;
  16. private const int ImgsPerStage = 1 << ImgStageShift;
  17. public int Handle { get; private set; }
  18. public bool IsLinked { get; private set; }
  19. private int[] _ubBindingPoints;
  20. private int[] _sbBindingPoints;
  21. private int[] _textureUnits;
  22. private int[] _imageUnits;
  23. public Program(IShader[] shaders)
  24. {
  25. _ubBindingPoints = new int[UbsPerStage * ShaderStages];
  26. _sbBindingPoints = new int[SbsPerStage * ShaderStages];
  27. _textureUnits = new int[TexsPerStage * ShaderStages];
  28. _imageUnits = new int[ImgsPerStage * ShaderStages];
  29. for (int index = 0; index < _ubBindingPoints.Length; index++)
  30. {
  31. _ubBindingPoints[index] = -1;
  32. }
  33. for (int index = 0; index < _sbBindingPoints.Length; index++)
  34. {
  35. _sbBindingPoints[index] = -1;
  36. }
  37. for (int index = 0; index < _textureUnits.Length; index++)
  38. {
  39. _textureUnits[index] = -1;
  40. }
  41. for (int index = 0; index < _imageUnits.Length; index++)
  42. {
  43. _imageUnits[index] = -1;
  44. }
  45. Handle = GL.CreateProgram();
  46. for (int index = 0; index < shaders.Length; index++)
  47. {
  48. int shaderHandle = ((Shader)shaders[index]).Handle;
  49. GL.AttachShader(Handle, shaderHandle);
  50. }
  51. GL.LinkProgram(Handle);
  52. CheckProgramLink();
  53. Bind();
  54. int extraBlockindex = GL.GetUniformBlockIndex(Handle, "Extra");
  55. if (extraBlockindex >= 0)
  56. {
  57. GL.UniformBlockBinding(Handle, extraBlockindex, 0);
  58. }
  59. int ubBindingPoint = 1;
  60. int sbBindingPoint = 0;
  61. int textureUnit = 0;
  62. for (int index = 0; index < shaders.Length; index++)
  63. {
  64. Shader shader = (Shader)shaders[index];
  65. foreach (BufferDescriptor descriptor in shader.Info.CBuffers)
  66. {
  67. int location = GL.GetUniformBlockIndex(Handle, descriptor.Name);
  68. if (location < 0)
  69. {
  70. continue;
  71. }
  72. GL.UniformBlockBinding(Handle, location, ubBindingPoint);
  73. int bpIndex = (int)shader.Stage << UbStageShift | descriptor.Slot;
  74. _ubBindingPoints[bpIndex] = ubBindingPoint;
  75. ubBindingPoint++;
  76. }
  77. foreach (BufferDescriptor descriptor in shader.Info.SBuffers)
  78. {
  79. int location = GL.GetProgramResourceIndex(Handle, ProgramInterface.ShaderStorageBlock, descriptor.Name);
  80. if (location < 0)
  81. {
  82. continue;
  83. }
  84. GL.ShaderStorageBlockBinding(Handle, location, sbBindingPoint);
  85. int bpIndex = (int)shader.Stage << SbStageShift | descriptor.Slot;
  86. _sbBindingPoints[bpIndex] = sbBindingPoint;
  87. sbBindingPoint++;
  88. }
  89. int samplerIndex = 0;
  90. foreach (TextureDescriptor descriptor in shader.Info.Textures)
  91. {
  92. int location = GL.GetUniformLocation(Handle, descriptor.Name);
  93. if (location < 0)
  94. {
  95. continue;
  96. }
  97. GL.Uniform1(location, textureUnit);
  98. int uIndex = (int)shader.Stage << TexStageShift | samplerIndex++;
  99. _textureUnits[uIndex] = textureUnit;
  100. textureUnit++;
  101. }
  102. int imageIndex = 0;
  103. foreach (TextureDescriptor descriptor in shader.Info.Images)
  104. {
  105. int location = GL.GetUniformLocation(Handle, descriptor.Name);
  106. if (location < 0)
  107. {
  108. continue;
  109. }
  110. GL.Uniform1(location, textureUnit);
  111. int uIndex = (int)shader.Stage << ImgStageShift | imageIndex++;
  112. _textureUnits[uIndex] = textureUnit;
  113. textureUnit++;
  114. }
  115. }
  116. }
  117. public void Bind()
  118. {
  119. GL.UseProgram(Handle);
  120. }
  121. public int GetUniformBufferBindingPoint(ShaderStage stage, int index)
  122. {
  123. return _ubBindingPoints[(int)stage << UbStageShift | index];
  124. }
  125. public int GetStorageBufferBindingPoint(ShaderStage stage, int index)
  126. {
  127. return _sbBindingPoints[(int)stage << SbStageShift | index];
  128. }
  129. public int GetTextureUnit(ShaderStage stage, int index)
  130. {
  131. return _textureUnits[(int)stage << TexStageShift | index];
  132. }
  133. public int GetImageUnit(ShaderStage stage, int index)
  134. {
  135. return _textureUnits[(int)stage << ImgStageShift | index];
  136. }
  137. private void CheckProgramLink()
  138. {
  139. int status = 0;
  140. GL.GetProgram(Handle, GetProgramParameterName.LinkStatus, out status);
  141. if (status == 0)
  142. {
  143. // throw new System.Exception(GL.GetProgramInfoLog(Handle));
  144. }
  145. else
  146. {
  147. IsLinked = true;
  148. }
  149. }
  150. public void Dispose()
  151. {
  152. if (Handle != 0)
  153. {
  154. GL.DeleteProgram(Handle);
  155. Handle = 0;
  156. }
  157. }
  158. }
  159. }