OGLShader.cs 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273
  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.Uniforms,
  59. Program.Textures);
  60. }
  61. public IEnumerable<ShaderDeclInfo> GetConstBufferUsage(long Key)
  62. {
  63. if (Stages.TryGetValue(Key, out OGLShaderStage Stage))
  64. {
  65. return Stage.ConstBufferUsage;
  66. }
  67. return Enumerable.Empty<ShaderDeclInfo>();
  68. }
  69. public IEnumerable<ShaderDeclInfo> GetTextureUsage(long Key)
  70. {
  71. if (Stages.TryGetValue(Key, out OGLShaderStage Stage))
  72. {
  73. return Stage.TextureUsage;
  74. }
  75. return Enumerable.Empty<ShaderDeclInfo>();
  76. }
  77. public void EnsureTextureBinding(string UniformName, int Value)
  78. {
  79. BindProgram();
  80. int Location = GL.GetUniformLocation(CurrentProgramHandle, UniformName);
  81. GL.Uniform1(Location, Value);
  82. }
  83. public unsafe void SetFlip(float X, float Y)
  84. {
  85. BindProgram();
  86. EnsureExtraBlock();
  87. GL.BindBuffer(BufferTarget.UniformBuffer, ExtraUboHandle);
  88. float* Data = stackalloc float[4];
  89. Data[0] = X;
  90. Data[1] = Y;
  91. //Invalidate buffer
  92. GL.BufferData(BufferTarget.UniformBuffer, 4 * sizeof(float), IntPtr.Zero, BufferUsageHint.StreamDraw);
  93. GL.BufferSubData(BufferTarget.UniformBuffer, IntPtr.Zero, 4 * sizeof(float), (IntPtr)Data);
  94. }
  95. public void Bind(long Key)
  96. {
  97. if (Stages.TryGetValue(Key, out OGLShaderStage Stage))
  98. {
  99. Bind(Stage);
  100. }
  101. }
  102. private void Bind(OGLShaderStage Stage)
  103. {
  104. if (Stage.Type == GalShaderType.Geometry)
  105. {
  106. //Enhanced layouts are required for Geometry shaders
  107. //skip this stage if current driver has no ARB_enhanced_layouts
  108. if (!OGLExtension.HasEnhancedLayouts())
  109. {
  110. return;
  111. }
  112. }
  113. switch (Stage.Type)
  114. {
  115. case GalShaderType.Vertex: Current.Vertex = Stage; break;
  116. case GalShaderType.TessControl: Current.TessControl = Stage; break;
  117. case GalShaderType.TessEvaluation: Current.TessEvaluation = Stage; break;
  118. case GalShaderType.Geometry: Current.Geometry = Stage; break;
  119. case GalShaderType.Fragment: Current.Fragment = Stage; break;
  120. }
  121. }
  122. public void Unbind(GalShaderType Type)
  123. {
  124. switch (Type)
  125. {
  126. case GalShaderType.Vertex: Current.Vertex = null; break;
  127. case GalShaderType.TessControl: Current.TessControl = null; break;
  128. case GalShaderType.TessEvaluation: Current.TessEvaluation = null; break;
  129. case GalShaderType.Geometry: Current.Geometry = null; break;
  130. case GalShaderType.Fragment: Current.Fragment = null; break;
  131. }
  132. }
  133. public void BindProgram()
  134. {
  135. if (Current.Vertex == null ||
  136. Current.Fragment == null)
  137. {
  138. return;
  139. }
  140. if (!Programs.TryGetValue(Current, out int Handle))
  141. {
  142. Handle = GL.CreateProgram();
  143. AttachIfNotNull(Handle, Current.Vertex);
  144. AttachIfNotNull(Handle, Current.TessControl);
  145. AttachIfNotNull(Handle, Current.TessEvaluation);
  146. AttachIfNotNull(Handle, Current.Geometry);
  147. AttachIfNotNull(Handle, Current.Fragment);
  148. GL.LinkProgram(Handle);
  149. CheckProgramLink(Handle);
  150. BindUniformBlocks(Handle);
  151. Programs.Add(Current, Handle);
  152. }
  153. GL.UseProgram(Handle);
  154. CurrentProgramHandle = Handle;
  155. }
  156. private void EnsureExtraBlock()
  157. {
  158. if (ExtraUboHandle == 0)
  159. {
  160. ExtraUboHandle = GL.GenBuffer();
  161. GL.BindBuffer(BufferTarget.UniformBuffer, ExtraUboHandle);
  162. GL.BufferData(BufferTarget.UniformBuffer, 4 * sizeof(float), IntPtr.Zero, BufferUsageHint.StreamDraw);
  163. GL.BindBufferBase(BufferRangeTarget.UniformBuffer, 0, ExtraUboHandle);
  164. }
  165. }
  166. private void AttachIfNotNull(int ProgramHandle, OGLShaderStage Stage)
  167. {
  168. if (Stage != null)
  169. {
  170. Stage.Compile();
  171. GL.AttachShader(ProgramHandle, Stage.Handle);
  172. }
  173. }
  174. private void BindUniformBlocks(int ProgramHandle)
  175. {
  176. int ExtraBlockindex = GL.GetUniformBlockIndex(ProgramHandle, GlslDecl.ExtraUniformBlockName);
  177. GL.UniformBlockBinding(ProgramHandle, ExtraBlockindex, 0);
  178. //First index is reserved
  179. int FreeBinding = 1;
  180. void BindUniformBlocksIfNotNull(OGLShaderStage Stage)
  181. {
  182. if (Stage != null)
  183. {
  184. foreach (ShaderDeclInfo DeclInfo in Stage.ConstBufferUsage)
  185. {
  186. int BlockIndex = GL.GetUniformBlockIndex(ProgramHandle, DeclInfo.Name);
  187. if (BlockIndex < 0)
  188. {
  189. //It is expected that its found, if it's not then driver might be in a malfunction
  190. throw new InvalidOperationException();
  191. }
  192. GL.UniformBlockBinding(ProgramHandle, BlockIndex, FreeBinding);
  193. FreeBinding++;
  194. }
  195. }
  196. }
  197. BindUniformBlocksIfNotNull(Current.Vertex);
  198. BindUniformBlocksIfNotNull(Current.TessControl);
  199. BindUniformBlocksIfNotNull(Current.TessEvaluation);
  200. BindUniformBlocksIfNotNull(Current.Geometry);
  201. BindUniformBlocksIfNotNull(Current.Fragment);
  202. }
  203. private static void CheckProgramLink(int Handle)
  204. {
  205. int Status = 0;
  206. GL.GetProgram(Handle, GetProgramParameterName.LinkStatus, out Status);
  207. if (Status == 0)
  208. {
  209. throw new ShaderException(GL.GetProgramInfoLog(Handle));
  210. }
  211. }
  212. }
  213. }