OpenGLRenderer.cs 8.7 KB

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