OpenGLRenderer.cs 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  1. using OpenTK.Graphics.OpenGL;
  2. using Ryujinx.Common.Configuration;
  3. using Ryujinx.Common.Logging;
  4. using Ryujinx.Graphics.GAL;
  5. using Ryujinx.Graphics.OpenGL.Image;
  6. using Ryujinx.Graphics.OpenGL.Queries;
  7. using Ryujinx.Graphics.Shader.Translation;
  8. using System;
  9. namespace Ryujinx.Graphics.OpenGL
  10. {
  11. public sealed class OpenGLRenderer : IRenderer
  12. {
  13. private readonly Pipeline _pipeline;
  14. public IPipeline Pipeline => _pipeline;
  15. private readonly Counters _counters;
  16. private readonly Window _window;
  17. public IWindow Window => _window;
  18. private TextureCopy _textureCopy;
  19. private TextureCopy _backgroundTextureCopy;
  20. internal TextureCopy TextureCopy => BackgroundContextWorker.InBackground ? _backgroundTextureCopy : _textureCopy;
  21. internal TextureCopyIncompatible TextureCopyIncompatible { get; }
  22. internal TextureCopyMS TextureCopyMS { get; }
  23. private Sync _sync;
  24. public event EventHandler<ScreenCaptureImageInfo> ScreenCaptured;
  25. internal PersistentBuffers PersistentBuffers { get; }
  26. internal ResourcePool ResourcePool { get; }
  27. internal int BufferCount { get; private set; }
  28. public string GpuVendor { get; private set; }
  29. public string GpuRenderer { get; private set; }
  30. public string GpuVersion { get; private set; }
  31. public bool PreferThreading => true;
  32. public OpenGLRenderer()
  33. {
  34. _pipeline = new Pipeline();
  35. _counters = new Counters();
  36. _window = new Window(this);
  37. _textureCopy = new TextureCopy(this);
  38. _backgroundTextureCopy = new TextureCopy(this);
  39. TextureCopyIncompatible = new TextureCopyIncompatible(this);
  40. TextureCopyMS = new TextureCopyMS(this);
  41. _sync = new Sync();
  42. PersistentBuffers = new PersistentBuffers();
  43. ResourcePool = new ResourcePool();
  44. }
  45. public BufferHandle CreateBuffer(int size, BufferHandle storageHint)
  46. {
  47. BufferCount++;
  48. return Buffer.Create(size);
  49. }
  50. public IProgram CreateProgram(ShaderSource[] shaders, ShaderInfo info)
  51. {
  52. return new Program(shaders, info.FragmentOutputMap);
  53. }
  54. public ISampler CreateSampler(SamplerCreateInfo info)
  55. {
  56. return new Sampler(info);
  57. }
  58. public ITexture CreateTexture(TextureCreateInfo info, float scaleFactor)
  59. {
  60. if (info.Target == Target.TextureBuffer)
  61. {
  62. return new TextureBuffer(this, info);
  63. }
  64. else
  65. {
  66. return ResourcePool.GetTextureOrNull(info, scaleFactor) ?? new TextureStorage(this, info, scaleFactor).CreateDefaultView();
  67. }
  68. }
  69. public void DeleteBuffer(BufferHandle buffer)
  70. {
  71. Buffer.Delete(buffer);
  72. }
  73. public HardwareInfo GetHardwareInfo()
  74. {
  75. return new HardwareInfo(GpuVendor, GpuRenderer);
  76. }
  77. public PinnedSpan<byte> GetBufferData(BufferHandle buffer, int offset, int size)
  78. {
  79. return Buffer.GetData(this, buffer, offset, size);
  80. }
  81. public Capabilities GetCapabilities()
  82. {
  83. return new Capabilities(
  84. api: TargetApi.OpenGL,
  85. vendorName: GpuVendor,
  86. hasFrontFacingBug: HwCapabilities.Vendor == HwCapabilities.GpuVendor.IntelWindows,
  87. hasVectorIndexingBug: HwCapabilities.Vendor == HwCapabilities.GpuVendor.AmdWindows,
  88. needsFragmentOutputSpecialization: false,
  89. reduceShaderPrecision: false,
  90. supportsAstcCompression: HwCapabilities.SupportsAstcCompression,
  91. supportsBc123Compression: HwCapabilities.SupportsTextureCompressionS3tc,
  92. supportsBc45Compression: HwCapabilities.SupportsTextureCompressionRgtc,
  93. supportsBc67Compression: true, // Should check BPTC extension, but for some reason NVIDIA is not exposing the extension.
  94. supportsEtc2Compression: true,
  95. supports3DTextureCompression: false,
  96. supportsBgraFormat: false,
  97. supportsR4G4Format: false,
  98. supportsR4G4B4A4Format: true,
  99. supportsSnormBufferTextureFormat: false,
  100. supports5BitComponentFormat: true,
  101. supportsBlendEquationAdvanced: HwCapabilities.SupportsBlendEquationAdvanced,
  102. supportsFragmentShaderInterlock: HwCapabilities.SupportsFragmentShaderInterlock,
  103. supportsFragmentShaderOrderingIntel: HwCapabilities.SupportsFragmentShaderOrdering,
  104. supportsGeometryShader: true,
  105. supportsGeometryShaderPassthrough: HwCapabilities.SupportsGeometryShaderPassthrough,
  106. supportsImageLoadFormatted: HwCapabilities.SupportsImageLoadFormatted,
  107. supportsLayerVertexTessellation: HwCapabilities.SupportsShaderViewportLayerArray,
  108. supportsMismatchingViewFormat: HwCapabilities.SupportsMismatchingViewFormat,
  109. supportsCubemapView: true,
  110. supportsNonConstantTextureOffset: HwCapabilities.SupportsNonConstantTextureOffset,
  111. supportsShaderBallot: HwCapabilities.SupportsShaderBallot,
  112. supportsTextureShadowLod: HwCapabilities.SupportsTextureShadowLod,
  113. supportsViewportIndex: HwCapabilities.SupportsShaderViewportLayerArray,
  114. supportsViewportSwizzle: HwCapabilities.SupportsViewportSwizzle,
  115. supportsIndirectParameters: HwCapabilities.SupportsIndirectParameters,
  116. maximumUniformBuffersPerStage: 13, // TODO: Avoid hardcoding those limits here and get from driver?
  117. maximumStorageBuffersPerStage: 16,
  118. maximumTexturesPerStage: 32,
  119. maximumImagesPerStage: 8,
  120. maximumComputeSharedMemorySize: HwCapabilities.MaximumComputeSharedMemorySize,
  121. maximumSupportedAnisotropy: HwCapabilities.MaximumSupportedAnisotropy,
  122. storageBufferOffsetAlignment: HwCapabilities.StorageBufferOffsetAlignment);
  123. }
  124. public void SetBufferData(BufferHandle buffer, int offset, ReadOnlySpan<byte> data)
  125. {
  126. Buffer.SetData(buffer, offset, data);
  127. }
  128. public void UpdateCounters()
  129. {
  130. _counters.Update();
  131. }
  132. public void PreFrame()
  133. {
  134. _sync.Cleanup();
  135. ResourcePool.Tick();
  136. }
  137. public ICounterEvent ReportCounter(CounterType type, EventHandler<ulong> resultHandler, bool hostReserved)
  138. {
  139. return _counters.QueueReport(type, resultHandler, _pipeline.DrawCount, hostReserved);
  140. }
  141. public void Initialize(GraphicsDebugLevel glLogLevel)
  142. {
  143. Debugger.Initialize(glLogLevel);
  144. PrintGpuInformation();
  145. if (HwCapabilities.SupportsParallelShaderCompile)
  146. {
  147. GL.Arb.MaxShaderCompilerThreads(Math.Min(Environment.ProcessorCount, 8));
  148. }
  149. _pipeline.Initialize(this);
  150. _counters.Initialize(_pipeline);
  151. // This is required to disable [0, 1] clamping for SNorm outputs on compatibility profiles.
  152. // This call is expected to fail if we're running with a core profile,
  153. // as this clamp target was deprecated, but that's fine as a core profile
  154. // should already have the desired behaviour were outputs are not clamped.
  155. GL.ClampColor(ClampColorTarget.ClampFragmentColor, ClampColorMode.False);
  156. }
  157. private void PrintGpuInformation()
  158. {
  159. GpuVendor = GL.GetString(StringName.Vendor);
  160. GpuRenderer = GL.GetString(StringName.Renderer);
  161. GpuVersion = GL.GetString(StringName.Version);
  162. Logger.Notice.Print(LogClass.Gpu, $"{GpuVendor} {GpuRenderer} ({GpuVersion})");
  163. }
  164. public void ResetCounter(CounterType type)
  165. {
  166. _counters.QueueReset(type);
  167. }
  168. public void BackgroundContextAction(Action action, bool alwaysBackground = false)
  169. {
  170. // alwaysBackground is ignored, since we cannot switch from the current context.
  171. if (IOpenGLContext.HasContext())
  172. {
  173. action(); // We have a context already - use that (assuming it is the main one).
  174. }
  175. else
  176. {
  177. _window.BackgroundContext.Invoke(action);
  178. }
  179. }
  180. public void InitializeBackgroundContext(IOpenGLContext baseContext)
  181. {
  182. _window.InitializeBackgroundContext(baseContext);
  183. }
  184. public void Dispose()
  185. {
  186. _textureCopy.Dispose();
  187. _backgroundTextureCopy.Dispose();
  188. TextureCopyMS.Dispose();
  189. PersistentBuffers.Dispose();
  190. ResourcePool.Dispose();
  191. _pipeline.Dispose();
  192. _window.Dispose();
  193. _counters.Dispose();
  194. _sync.Dispose();
  195. }
  196. public IProgram LoadProgramBinary(byte[] programBinary, bool hasFragmentShader, ShaderInfo info)
  197. {
  198. return new Program(programBinary, hasFragmentShader, info.FragmentOutputMap);
  199. }
  200. public void CreateSync(ulong id, bool strict)
  201. {
  202. _sync.Create(id);
  203. }
  204. public void WaitSync(ulong id)
  205. {
  206. _sync.Wait(id);
  207. }
  208. public ulong GetCurrentSync()
  209. {
  210. return _sync.GetCurrent();
  211. }
  212. public void SetInterruptAction(Action<Action> interruptAction)
  213. {
  214. // Currently no need for an interrupt action.
  215. }
  216. public void Screenshot()
  217. {
  218. _window.ScreenCaptureRequested = true;
  219. }
  220. public void OnScreenCaptured(ScreenCaptureImageInfo bitmap)
  221. {
  222. ScreenCaptured?.Invoke(this, bitmap);
  223. }
  224. }
  225. }