OpenGLRenderer.cs 9.6 KB

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