OGLShader.cs 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298
  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 const int ReservedCbufCount = 1;
  12. private const int ExtraDataSize = 4;
  13. public OGLShaderProgram Current;
  14. private ConcurrentDictionary<long, OGLShaderStage> Stages;
  15. private Dictionary<OGLShaderProgram, int> Programs;
  16. public int CurrentProgramHandle { get; private set; }
  17. private OGLConstBuffer Buffer;
  18. private int ExtraUboHandle;
  19. public OGLShader(OGLConstBuffer Buffer)
  20. {
  21. this.Buffer = Buffer;
  22. Stages = new ConcurrentDictionary<long, OGLShaderStage>();
  23. Programs = new Dictionary<OGLShaderProgram, int>();
  24. }
  25. public void Create(IGalMemory Memory, long Key, GalShaderType Type)
  26. {
  27. Stages.GetOrAdd(Key, (Stage) => ShaderStageFactory(Memory, Key, 0, false, Type));
  28. }
  29. public void Create(IGalMemory Memory, long VpAPos, long Key, GalShaderType Type)
  30. {
  31. Stages.GetOrAdd(Key, (Stage) => ShaderStageFactory(Memory, VpAPos, Key, true, Type));
  32. }
  33. private OGLShaderStage ShaderStageFactory(
  34. IGalMemory Memory,
  35. long Position,
  36. long PositionB,
  37. bool IsDualVp,
  38. GalShaderType Type)
  39. {
  40. GlslProgram Program;
  41. GlslDecompiler Decompiler = new GlslDecompiler();
  42. int ShaderDumpIndex = ShaderDumper.DumpIndex;
  43. if (IsDualVp)
  44. {
  45. ShaderDumper.Dump(Memory, Position, Type, "a");
  46. ShaderDumper.Dump(Memory, PositionB, Type, "b");
  47. Program = Decompiler.Decompile(Memory, Position, PositionB, Type);
  48. }
  49. else
  50. {
  51. ShaderDumper.Dump(Memory, Position, Type);
  52. Program = Decompiler.Decompile(Memory, Position, Type);
  53. }
  54. string Code = Program.Code;
  55. if (ShaderDumper.IsDumpEnabled())
  56. {
  57. Code = "//Shader " + ShaderDumpIndex + Environment.NewLine + Code;
  58. }
  59. return new OGLShaderStage(Type, Code, Program.Uniforms, 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 SetExtraData(float FlipX, float FlipY, int Instance)
  78. {
  79. BindProgram();
  80. EnsureExtraBlock();
  81. GL.BindBuffer(BufferTarget.UniformBuffer, ExtraUboHandle);
  82. float* Data = stackalloc float[ExtraDataSize];
  83. Data[0] = FlipX;
  84. Data[1] = FlipY;
  85. Data[2] = BitConverter.Int32BitsToSingle(Instance);
  86. //Invalidate buffer
  87. GL.BufferData(BufferTarget.UniformBuffer, ExtraDataSize * sizeof(float), IntPtr.Zero, BufferUsageHint.StreamDraw);
  88. GL.BufferSubData(BufferTarget.UniformBuffer, IntPtr.Zero, ExtraDataSize * sizeof(float), (IntPtr)Data);
  89. }
  90. public void Bind(long Key)
  91. {
  92. if (Stages.TryGetValue(Key, out OGLShaderStage Stage))
  93. {
  94. Bind(Stage);
  95. }
  96. }
  97. private void Bind(OGLShaderStage Stage)
  98. {
  99. if (Stage.Type == GalShaderType.Geometry)
  100. {
  101. //Enhanced layouts are required for Geometry shaders
  102. //skip this stage if current driver has no ARB_enhanced_layouts
  103. if (!OGLExtension.EnhancedLayouts)
  104. {
  105. return;
  106. }
  107. }
  108. switch (Stage.Type)
  109. {
  110. case GalShaderType.Vertex: Current.Vertex = Stage; break;
  111. case GalShaderType.TessControl: Current.TessControl = Stage; break;
  112. case GalShaderType.TessEvaluation: Current.TessEvaluation = Stage; break;
  113. case GalShaderType.Geometry: Current.Geometry = Stage; break;
  114. case GalShaderType.Fragment: Current.Fragment = Stage; break;
  115. }
  116. }
  117. public void Unbind(GalShaderType Type)
  118. {
  119. switch (Type)
  120. {
  121. case GalShaderType.Vertex: Current.Vertex = null; break;
  122. case GalShaderType.TessControl: Current.TessControl = null; break;
  123. case GalShaderType.TessEvaluation: Current.TessEvaluation = null; break;
  124. case GalShaderType.Geometry: Current.Geometry = null; break;
  125. case GalShaderType.Fragment: Current.Fragment = null; break;
  126. }
  127. }
  128. public void BindProgram()
  129. {
  130. if (Current.Vertex == null ||
  131. Current.Fragment == null)
  132. {
  133. return;
  134. }
  135. if (!Programs.TryGetValue(Current, out int Handle))
  136. {
  137. Handle = GL.CreateProgram();
  138. AttachIfNotNull(Handle, Current.Vertex);
  139. AttachIfNotNull(Handle, Current.TessControl);
  140. AttachIfNotNull(Handle, Current.TessEvaluation);
  141. AttachIfNotNull(Handle, Current.Geometry);
  142. AttachIfNotNull(Handle, Current.Fragment);
  143. GL.LinkProgram(Handle);
  144. CheckProgramLink(Handle);
  145. BindUniformBlocks(Handle);
  146. BindTextureLocations(Handle);
  147. Programs.Add(Current, Handle);
  148. }
  149. GL.UseProgram(Handle);
  150. CurrentProgramHandle = Handle;
  151. }
  152. private void EnsureExtraBlock()
  153. {
  154. if (ExtraUboHandle == 0)
  155. {
  156. ExtraUboHandle = GL.GenBuffer();
  157. GL.BindBuffer(BufferTarget.UniformBuffer, ExtraUboHandle);
  158. GL.BufferData(BufferTarget.UniformBuffer, ExtraDataSize * sizeof(float), IntPtr.Zero, BufferUsageHint.StreamDraw);
  159. GL.BindBufferBase(BufferRangeTarget.UniformBuffer, 0, ExtraUboHandle);
  160. }
  161. }
  162. private void AttachIfNotNull(int ProgramHandle, OGLShaderStage Stage)
  163. {
  164. if (Stage != null)
  165. {
  166. Stage.Compile();
  167. GL.AttachShader(ProgramHandle, Stage.Handle);
  168. }
  169. }
  170. private void BindUniformBlocks(int ProgramHandle)
  171. {
  172. int ExtraBlockindex = GL.GetUniformBlockIndex(ProgramHandle, GlslDecl.ExtraUniformBlockName);
  173. GL.UniformBlockBinding(ProgramHandle, ExtraBlockindex, 0);
  174. int FreeBinding = ReservedCbufCount;
  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. }