OpenGLRenderer.cs 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  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. supportsViewportSwizzle: HwCapabilities.SupportsViewportSwizzle,
  101. supportsIndirectParameters: HwCapabilities.SupportsIndirectParameters,
  102. maximumUniformBuffersPerStage: 13, // TODO: Avoid hardcoding those limits here and get from driver?
  103. maximumStorageBuffersPerStage: 16,
  104. maximumTexturesPerStage: 32,
  105. maximumImagesPerStage: 8,
  106. maximumComputeSharedMemorySize: HwCapabilities.MaximumComputeSharedMemorySize,
  107. maximumSupportedAnisotropy: HwCapabilities.MaximumSupportedAnisotropy,
  108. storageBufferOffsetAlignment: HwCapabilities.StorageBufferOffsetAlignment);
  109. }
  110. public void SetBufferData(BufferHandle buffer, int offset, ReadOnlySpan<byte> data)
  111. {
  112. Buffer.SetData(buffer, offset, data);
  113. }
  114. public void UpdateCounters()
  115. {
  116. _counters.Update();
  117. }
  118. public void PreFrame()
  119. {
  120. _sync.Cleanup();
  121. ResourcePool.Tick();
  122. }
  123. public ICounterEvent ReportCounter(CounterType type, EventHandler<ulong> resultHandler, bool hostReserved)
  124. {
  125. return _counters.QueueReport(type, resultHandler, _pipeline.DrawCount, hostReserved);
  126. }
  127. public void Initialize(GraphicsDebugLevel glLogLevel)
  128. {
  129. Debugger.Initialize(glLogLevel);
  130. PrintGpuInformation();
  131. if (HwCapabilities.SupportsParallelShaderCompile)
  132. {
  133. GL.Arb.MaxShaderCompilerThreads(Math.Min(Environment.ProcessorCount, 8));
  134. }
  135. _pipeline.Initialize(this);
  136. _counters.Initialize();
  137. // This is required to disable [0, 1] clamping for SNorm outputs on compatibility profiles.
  138. // This call is expected to fail if we're running with a core profile,
  139. // as this clamp target was deprecated, but that's fine as a core profile
  140. // should already have the desired behaviour were outputs are not clamped.
  141. GL.ClampColor(ClampColorTarget.ClampFragmentColor, ClampColorMode.False);
  142. }
  143. private void PrintGpuInformation()
  144. {
  145. GpuVendor = GL.GetString(StringName.Vendor);
  146. GpuRenderer = GL.GetString(StringName.Renderer);
  147. GpuVersion = GL.GetString(StringName.Version);
  148. Logger.Notice.Print(LogClass.Gpu, $"{GpuVendor} {GpuRenderer} ({GpuVersion})");
  149. }
  150. public void ResetCounter(CounterType type)
  151. {
  152. _counters.QueueReset(type);
  153. }
  154. public void BackgroundContextAction(Action action, bool alwaysBackground = false)
  155. {
  156. // alwaysBackground is ignored, since we cannot switch from the current context.
  157. if (IOpenGLContext.HasContext())
  158. {
  159. action(); // We have a context already - use that (assuming it is the main one).
  160. }
  161. else
  162. {
  163. _window.BackgroundContext.Invoke(action);
  164. }
  165. }
  166. public void InitializeBackgroundContext(IOpenGLContext baseContext)
  167. {
  168. _window.InitializeBackgroundContext(baseContext);
  169. }
  170. public void Dispose()
  171. {
  172. _textureCopy.Dispose();
  173. _backgroundTextureCopy.Dispose();
  174. PersistentBuffers.Dispose();
  175. ResourcePool.Dispose();
  176. _pipeline.Dispose();
  177. _window.Dispose();
  178. _counters.Dispose();
  179. _sync.Dispose();
  180. }
  181. public IProgram LoadProgramBinary(byte[] programBinary, bool hasFragmentShader, ShaderInfo info)
  182. {
  183. return new Program(programBinary, hasFragmentShader, info.FragmentOutputMap);
  184. }
  185. public void CreateSync(ulong id)
  186. {
  187. _sync.Create(id);
  188. }
  189. public void WaitSync(ulong id)
  190. {
  191. _sync.Wait(id);
  192. }
  193. public void Screenshot()
  194. {
  195. _window.ScreenCaptureRequested = true;
  196. }
  197. public void OnScreenCaptured(ScreenCaptureImageInfo bitmap)
  198. {
  199. ScreenCaptured?.Invoke(this, bitmap);
  200. }
  201. }
  202. }