OglShader.cs 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291
  1. using OpenTK.Graphics.OpenGL;
  2. using Ryujinx.Graphics.Shader;
  3. using Ryujinx.Graphics.Shader.Translation;
  4. using System;
  5. using System.Collections.Concurrent;
  6. using System.Collections.Generic;
  7. using System.Linq;
  8. namespace Ryujinx.Graphics.Gal.OpenGL
  9. {
  10. class OglShader : IGalShader
  11. {
  12. public const int ReservedCbufCount = 1;
  13. private const int ExtraDataSize = 4;
  14. public OglShaderProgram Current;
  15. private ConcurrentDictionary<long, OglShaderStage> _stages;
  16. private Dictionary<OglShaderProgram, int> _programs;
  17. public int CurrentProgramHandle { get; private set; }
  18. private OglConstBuffer _buffer;
  19. private int _extraUboHandle;
  20. public OglShader(OglConstBuffer buffer)
  21. {
  22. _buffer = buffer;
  23. _stages = new ConcurrentDictionary<long, OglShaderStage>();
  24. _programs = new Dictionary<OglShaderProgram, int>();
  25. }
  26. public void Create(IGalMemory memory, long key, GalShaderType type)
  27. {
  28. _stages.GetOrAdd(key, (stage) => ShaderStageFactory(memory, key, 0, false, type));
  29. }
  30. public void Create(IGalMemory memory, long vpAPos, long key, GalShaderType type)
  31. {
  32. _stages.GetOrAdd(key, (stage) => ShaderStageFactory(memory, vpAPos, key, true, type));
  33. }
  34. private OglShaderStage ShaderStageFactory(
  35. IGalMemory memory,
  36. long position,
  37. long positionB,
  38. bool isDualVp,
  39. GalShaderType type)
  40. {
  41. ShaderConfig config = new ShaderConfig(type, OglLimit.MaxUboSize);
  42. ShaderProgram program;
  43. if (isDualVp)
  44. {
  45. ShaderDumper.Dump(memory, position, type, "a");
  46. ShaderDumper.Dump(memory, positionB, type, "b");
  47. program = Translator.Translate(memory, (ulong)position, (ulong)positionB, config);
  48. }
  49. else
  50. {
  51. ShaderDumper.Dump(memory, position, type);
  52. program = Translator.Translate(memory, (ulong)position, config);
  53. }
  54. string code = program.Code;
  55. if (ShaderDumper.IsDumpEnabled())
  56. {
  57. int shaderDumpIndex = ShaderDumper.DumpIndex;
  58. code = "//Shader " + shaderDumpIndex + Environment.NewLine + code;
  59. }
  60. return new OglShaderStage(type, code, program.Info.CBuffers, program.Info.Textures);
  61. }
  62. public IEnumerable<CBufferDescriptor> GetConstBufferUsage(long key)
  63. {
  64. if (_stages.TryGetValue(key, out OglShaderStage stage))
  65. {
  66. return stage.ConstBufferUsage;
  67. }
  68. return Enumerable.Empty<CBufferDescriptor>();
  69. }
  70. public IEnumerable<TextureDescriptor> GetTextureUsage(long key)
  71. {
  72. if (_stages.TryGetValue(key, out OglShaderStage stage))
  73. {
  74. return stage.TextureUsage;
  75. }
  76. return Enumerable.Empty<TextureDescriptor>();
  77. }
  78. public unsafe void SetExtraData(float flipX, float flipY, int instance)
  79. {
  80. BindProgram();
  81. EnsureExtraBlock();
  82. GL.BindBuffer(BufferTarget.UniformBuffer, _extraUboHandle);
  83. float* data = stackalloc float[ExtraDataSize];
  84. data[0] = flipX;
  85. data[1] = flipY;
  86. data[2] = BitConverter.Int32BitsToSingle(instance);
  87. // Invalidate buffer
  88. GL.BufferData(BufferTarget.UniformBuffer, ExtraDataSize * sizeof(float), IntPtr.Zero, BufferUsageHint.StreamDraw);
  89. GL.BufferSubData(BufferTarget.UniformBuffer, IntPtr.Zero, ExtraDataSize * sizeof(float), (IntPtr)data);
  90. }
  91. public void Bind(long key)
  92. {
  93. if (_stages.TryGetValue(key, out OglShaderStage stage))
  94. {
  95. Bind(stage);
  96. }
  97. }
  98. private void Bind(OglShaderStage stage)
  99. {
  100. switch (stage.Type)
  101. {
  102. case GalShaderType.Vertex: Current.Vertex = stage; break;
  103. case GalShaderType.TessControl: Current.TessControl = stage; break;
  104. case GalShaderType.TessEvaluation: Current.TessEvaluation = stage; break;
  105. case GalShaderType.Geometry: Current.Geometry = stage; break;
  106. case GalShaderType.Fragment: Current.Fragment = stage; break;
  107. }
  108. }
  109. public void Unbind(GalShaderType type)
  110. {
  111. switch (type)
  112. {
  113. case GalShaderType.Vertex: Current.Vertex = null; break;
  114. case GalShaderType.TessControl: Current.TessControl = null; break;
  115. case GalShaderType.TessEvaluation: Current.TessEvaluation = null; break;
  116. case GalShaderType.Geometry: Current.Geometry = null; break;
  117. case GalShaderType.Fragment: Current.Fragment = null; break;
  118. }
  119. }
  120. public void BindProgram()
  121. {
  122. if (Current.Vertex == null ||
  123. Current.Fragment == null)
  124. {
  125. return;
  126. }
  127. if (!_programs.TryGetValue(Current, out int handle))
  128. {
  129. handle = GL.CreateProgram();
  130. AttachIfNotNull(handle, Current.Vertex);
  131. AttachIfNotNull(handle, Current.TessControl);
  132. AttachIfNotNull(handle, Current.TessEvaluation);
  133. AttachIfNotNull(handle, Current.Geometry);
  134. AttachIfNotNull(handle, Current.Fragment);
  135. GL.LinkProgram(handle);
  136. CheckProgramLink(handle);
  137. BindUniformBlocks(handle);
  138. BindTextureLocations(handle);
  139. _programs.Add(Current, handle);
  140. }
  141. GL.UseProgram(handle);
  142. CurrentProgramHandle = handle;
  143. }
  144. private void EnsureExtraBlock()
  145. {
  146. if (_extraUboHandle == 0)
  147. {
  148. _extraUboHandle = GL.GenBuffer();
  149. GL.BindBuffer(BufferTarget.UniformBuffer, _extraUboHandle);
  150. GL.BufferData(BufferTarget.UniformBuffer, ExtraDataSize * sizeof(float), IntPtr.Zero, BufferUsageHint.StreamDraw);
  151. GL.BindBufferBase(BufferRangeTarget.UniformBuffer, 0, _extraUboHandle);
  152. }
  153. }
  154. private void AttachIfNotNull(int programHandle, OglShaderStage stage)
  155. {
  156. if (stage != null)
  157. {
  158. stage.Compile();
  159. GL.AttachShader(programHandle, stage.Handle);
  160. }
  161. }
  162. private void BindUniformBlocks(int programHandle)
  163. {
  164. int extraBlockindex = GL.GetUniformBlockIndex(programHandle, "Extra");
  165. GL.UniformBlockBinding(programHandle, extraBlockindex, 0);
  166. int freeBinding = ReservedCbufCount;
  167. void BindUniformBlocksIfNotNull(OglShaderStage stage)
  168. {
  169. if (stage != null)
  170. {
  171. foreach (CBufferDescriptor desc in stage.ConstBufferUsage)
  172. {
  173. int blockIndex = GL.GetUniformBlockIndex(programHandle, desc.Name);
  174. if (blockIndex < 0)
  175. {
  176. // This may be fine, the compiler may optimize away unused uniform buffers,
  177. // and in this case the above call would return -1 as the buffer has been
  178. // optimized away.
  179. continue;
  180. }
  181. GL.UniformBlockBinding(programHandle, blockIndex, freeBinding);
  182. freeBinding++;
  183. }
  184. }
  185. }
  186. BindUniformBlocksIfNotNull(Current.Vertex);
  187. BindUniformBlocksIfNotNull(Current.TessControl);
  188. BindUniformBlocksIfNotNull(Current.TessEvaluation);
  189. BindUniformBlocksIfNotNull(Current.Geometry);
  190. BindUniformBlocksIfNotNull(Current.Fragment);
  191. }
  192. private void BindTextureLocations(int programHandle)
  193. {
  194. int index = 0;
  195. void BindTexturesIfNotNull(OglShaderStage stage)
  196. {
  197. if (stage != null)
  198. {
  199. foreach (TextureDescriptor desc in stage.TextureUsage)
  200. {
  201. int location = GL.GetUniformLocation(programHandle, desc.Name);
  202. GL.Uniform1(location, index);
  203. index++;
  204. }
  205. }
  206. }
  207. GL.UseProgram(programHandle);
  208. BindTexturesIfNotNull(Current.Vertex);
  209. BindTexturesIfNotNull(Current.TessControl);
  210. BindTexturesIfNotNull(Current.TessEvaluation);
  211. BindTexturesIfNotNull(Current.Geometry);
  212. BindTexturesIfNotNull(Current.Fragment);
  213. }
  214. private static void CheckProgramLink(int handle)
  215. {
  216. int status = 0;
  217. GL.GetProgram(handle, GetProgramParameterName.LinkStatus, out status);
  218. if (status == 0)
  219. {
  220. throw new ShaderException(GL.GetProgramInfoLog(handle));
  221. }
  222. }
  223. }
  224. }