OGLShader.cs 9.1 KB

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