Program.cs 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  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. int imageUnit = 0;
  63. for (int index = 0; index < shaders.Length; index++)
  64. {
  65. Shader shader = (Shader)shaders[index];
  66. foreach (BufferDescriptor descriptor in shader.Info.CBuffers)
  67. {
  68. int location = GL.GetUniformBlockIndex(Handle, descriptor.Name);
  69. if (location < 0)
  70. {
  71. continue;
  72. }
  73. GL.UniformBlockBinding(Handle, location, ubBindingPoint);
  74. int bpIndex = (int)shader.Stage << UbStageShift | descriptor.Slot;
  75. _ubBindingPoints[bpIndex] = ubBindingPoint;
  76. ubBindingPoint++;
  77. }
  78. foreach (BufferDescriptor descriptor in shader.Info.SBuffers)
  79. {
  80. int location = GL.GetProgramResourceIndex(Handle, ProgramInterface.ShaderStorageBlock, descriptor.Name);
  81. if (location < 0)
  82. {
  83. continue;
  84. }
  85. GL.ShaderStorageBlockBinding(Handle, location, sbBindingPoint);
  86. int bpIndex = (int)shader.Stage << SbStageShift | descriptor.Slot;
  87. _sbBindingPoints[bpIndex] = sbBindingPoint;
  88. sbBindingPoint++;
  89. }
  90. int samplerIndex = 0;
  91. foreach (TextureDescriptor descriptor in shader.Info.Textures)
  92. {
  93. int location = GL.GetUniformLocation(Handle, descriptor.Name);
  94. if (location < 0)
  95. {
  96. continue;
  97. }
  98. GL.Uniform1(location, textureUnit);
  99. int uIndex = (int)shader.Stage << TexStageShift | samplerIndex++;
  100. _textureUnits[uIndex] = textureUnit;
  101. textureUnit++;
  102. }
  103. int imageIndex = 0;
  104. foreach (TextureDescriptor descriptor in shader.Info.Images)
  105. {
  106. int location = GL.GetUniformLocation(Handle, descriptor.Name);
  107. if (location < 0)
  108. {
  109. continue;
  110. }
  111. GL.Uniform1(location, imageUnit);
  112. int uIndex = (int)shader.Stage << ImgStageShift | imageIndex++;
  113. _imageUnits[uIndex] = imageUnit;
  114. imageUnit++;
  115. }
  116. }
  117. }
  118. public void Bind()
  119. {
  120. GL.UseProgram(Handle);
  121. }
  122. public int GetUniformBufferBindingPoint(ShaderStage stage, int index)
  123. {
  124. return _ubBindingPoints[(int)stage << UbStageShift | index];
  125. }
  126. public int GetStorageBufferBindingPoint(ShaderStage stage, int index)
  127. {
  128. return _sbBindingPoints[(int)stage << SbStageShift | index];
  129. }
  130. public int GetTextureUnit(ShaderStage stage, int index)
  131. {
  132. return _textureUnits[(int)stage << TexStageShift | index];
  133. }
  134. public int GetImageUnit(ShaderStage stage, int index)
  135. {
  136. return _imageUnits[(int)stage << ImgStageShift | index];
  137. }
  138. private void CheckProgramLink()
  139. {
  140. int status = 0;
  141. GL.GetProgram(Handle, GetProgramParameterName.LinkStatus, out status);
  142. if (status == 0)
  143. {
  144. // throw new System.Exception(GL.GetProgramInfoLog(Handle));
  145. }
  146. else
  147. {
  148. IsLinked = true;
  149. }
  150. }
  151. public void Dispose()
  152. {
  153. if (Handle != 0)
  154. {
  155. GL.DeleteProgram(Handle);
  156. Handle = 0;
  157. }
  158. }
  159. }
  160. }