OGLShader.cs 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  1. using OpenTK.Graphics.OpenGL;
  2. using Ryujinx.Graphics.Gal.Shader;
  3. using System;
  4. using System.Collections.Concurrent;
  5. using System.Collections.Generic;
  6. using System.Linq;
  7. namespace Ryujinx.Graphics.Gal.OpenGL
  8. {
  9. class OGLShader : IGalShader
  10. {
  11. public OGLShaderProgram Current;
  12. private ConcurrentDictionary<long, OGLShaderStage> Stages;
  13. private Dictionary<OGLShaderProgram, int> Programs;
  14. public int CurrentProgramHandle { get; private set; }
  15. private OGLConstBuffer Buffer;
  16. private int ExtraUboHandle;
  17. public OGLShader(OGLConstBuffer Buffer)
  18. {
  19. this.Buffer = Buffer;
  20. Stages = new ConcurrentDictionary<long, OGLShaderStage>();
  21. Programs = new Dictionary<OGLShaderProgram, int>();
  22. }
  23. public void Create(IGalMemory Memory, long Key, GalShaderType Type)
  24. {
  25. Stages.GetOrAdd(Key, (Stage) => ShaderStageFactory(Memory, Key, 0, false, Type));
  26. }
  27. public void Create(IGalMemory Memory, long VpAPos, long Key, GalShaderType Type)
  28. {
  29. Stages.GetOrAdd(Key, (Stage) => ShaderStageFactory(Memory, VpAPos, Key, true, Type));
  30. }
  31. private OGLShaderStage ShaderStageFactory(
  32. IGalMemory Memory,
  33. long Position,
  34. long PositionB,
  35. bool IsDualVp,
  36. GalShaderType Type)
  37. {
  38. GlslProgram Program;
  39. GlslDecompiler Decompiler = new GlslDecompiler();
  40. if (IsDualVp)
  41. {
  42. ShaderDumper.Dump(Memory, Position, Type, "a");
  43. ShaderDumper.Dump(Memory, PositionB, Type, "b");
  44. Program = Decompiler.Decompile(
  45. Memory,
  46. Position,
  47. PositionB,
  48. Type);
  49. }
  50. else
  51. {
  52. ShaderDumper.Dump(Memory, Position, Type);
  53. Program = Decompiler.Decompile(Memory, Position, Type);
  54. }
  55. return new OGLShaderStage(
  56. Type,
  57. Program.Code,
  58. Program.Textures,
  59. Program.Uniforms);
  60. }
  61. public IEnumerable<ShaderDeclInfo> GetTextureUsage(long Key)
  62. {
  63. if (Stages.TryGetValue(Key, out OGLShaderStage Stage))
  64. {
  65. return Stage.TextureUsage;
  66. }
  67. return Enumerable.Empty<ShaderDeclInfo>();
  68. }
  69. public void EnsureTextureBinding(string UniformName, int Value)
  70. {
  71. BindProgram();
  72. int Location = GL.GetUniformLocation(CurrentProgramHandle, UniformName);
  73. GL.Uniform1(Location, Value);
  74. }
  75. public unsafe void SetFlip(float X, float Y)
  76. {
  77. BindProgram();
  78. EnsureExtraBlock();
  79. GL.BindBuffer(BufferTarget.UniformBuffer, ExtraUboHandle);
  80. float* Data = stackalloc float[4];
  81. Data[0] = X;
  82. Data[1] = Y;
  83. //Invalidate buffer
  84. GL.BufferData(BufferTarget.UniformBuffer, 4 * sizeof(float), IntPtr.Zero, BufferUsageHint.StreamDraw);
  85. GL.BufferSubData(BufferTarget.UniformBuffer, IntPtr.Zero, 4 * sizeof(float), (IntPtr)Data);
  86. }
  87. public void Bind(long Key)
  88. {
  89. if (Stages.TryGetValue(Key, out OGLShaderStage Stage))
  90. {
  91. Bind(Stage);
  92. }
  93. }
  94. private void Bind(OGLShaderStage Stage)
  95. {
  96. if (Stage.Type == GalShaderType.Geometry)
  97. {
  98. //Enhanced layouts are required for Geometry shaders
  99. //skip this stage if current driver has no ARB_enhanced_layouts
  100. if (!OGLExtension.HasEnhancedLayouts())
  101. {
  102. return;
  103. }
  104. }
  105. switch (Stage.Type)
  106. {
  107. case GalShaderType.Vertex: Current.Vertex = Stage; break;
  108. case GalShaderType.TessControl: Current.TessControl = Stage; break;
  109. case GalShaderType.TessEvaluation: Current.TessEvaluation = Stage; break;
  110. case GalShaderType.Geometry: Current.Geometry = Stage; break;
  111. case GalShaderType.Fragment: Current.Fragment = Stage; break;
  112. }
  113. }
  114. public void Unbind(GalShaderType Type)
  115. {
  116. switch (Type)
  117. {
  118. case GalShaderType.Vertex: Current.Vertex = null; break;
  119. case GalShaderType.TessControl: Current.TessControl = null; break;
  120. case GalShaderType.TessEvaluation: Current.TessEvaluation = null; break;
  121. case GalShaderType.Geometry: Current.Geometry = null; break;
  122. case GalShaderType.Fragment: Current.Fragment = null; break;
  123. }
  124. }
  125. public void BindProgram()
  126. {
  127. if (Current.Vertex == null ||
  128. Current.Fragment == null)
  129. {
  130. return;
  131. }
  132. if (!Programs.TryGetValue(Current, out int Handle))
  133. {
  134. Handle = GL.CreateProgram();
  135. AttachIfNotNull(Handle, Current.Vertex);
  136. AttachIfNotNull(Handle, Current.TessControl);
  137. AttachIfNotNull(Handle, Current.TessEvaluation);
  138. AttachIfNotNull(Handle, Current.Geometry);
  139. AttachIfNotNull(Handle, Current.Fragment);
  140. GL.LinkProgram(Handle);
  141. CheckProgramLink(Handle);
  142. BindUniformBlocks(Handle);
  143. Programs.Add(Current, Handle);
  144. }
  145. GL.UseProgram(Handle);
  146. CurrentProgramHandle = Handle;
  147. }
  148. private void EnsureExtraBlock()
  149. {
  150. if (ExtraUboHandle == 0)
  151. {
  152. ExtraUboHandle = GL.GenBuffer();
  153. GL.BindBuffer(BufferTarget.UniformBuffer, ExtraUboHandle);
  154. GL.BufferData(BufferTarget.UniformBuffer, 4 * sizeof(float), IntPtr.Zero, BufferUsageHint.StreamDraw);
  155. GL.BindBufferBase(BufferRangeTarget.UniformBuffer, 0, ExtraUboHandle);
  156. }
  157. }
  158. private void AttachIfNotNull(int ProgramHandle, OGLShaderStage Stage)
  159. {
  160. if (Stage != null)
  161. {
  162. Stage.Compile();
  163. GL.AttachShader(ProgramHandle, Stage.Handle);
  164. }
  165. }
  166. private void BindUniformBlocks(int ProgramHandle)
  167. {
  168. int ExtraBlockindex = GL.GetUniformBlockIndex(ProgramHandle, GlslDecl.ExtraUniformBlockName);
  169. GL.UniformBlockBinding(ProgramHandle, ExtraBlockindex, 0);
  170. //First index is reserved
  171. int FreeBinding = 1;
  172. void BindUniformBlocksIfNotNull(OGLShaderStage Stage)
  173. {
  174. if (Stage != null)
  175. {
  176. foreach (ShaderDeclInfo DeclInfo in Stage.UniformUsage)
  177. {
  178. int BlockIndex = GL.GetUniformBlockIndex(ProgramHandle, DeclInfo.Name);
  179. if (BlockIndex < 0)
  180. {
  181. //It is expected that its found, if it's not then driver might be in a malfunction
  182. throw new InvalidOperationException();
  183. }
  184. GL.UniformBlockBinding(ProgramHandle, BlockIndex, FreeBinding);
  185. FreeBinding++;
  186. }
  187. }
  188. }
  189. BindUniformBlocksIfNotNull(Current.Vertex);
  190. BindUniformBlocksIfNotNull(Current.TessControl);
  191. BindUniformBlocksIfNotNull(Current.TessEvaluation);
  192. BindUniformBlocksIfNotNull(Current.Geometry);
  193. BindUniformBlocksIfNotNull(Current.Fragment);
  194. }
  195. private static void CheckProgramLink(int Handle)
  196. {
  197. int Status = 0;
  198. GL.GetProgram(Handle, GetProgramParameterName.LinkStatus, out Status);
  199. if (Status == 0)
  200. {
  201. throw new ShaderException(GL.GetProgramInfoLog(Handle));
  202. }
  203. }
  204. }
  205. }