OglShader.cs 9.5 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. _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(OglLimit.MaxUboSize, OglExtension.NvidiaDriver);
  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. }