Renderer.cs 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  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 Renderer : 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 Renderer()
  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 ReadOnlySpan<byte> GetBufferData(BufferHandle buffer, int offset, int size)
  70. {
  71. return Buffer.GetData(this, buffer, offset, size);
  72. }
  73. public Capabilities GetCapabilities()
  74. {
  75. return new Capabilities(
  76. api: TargetApi.OpenGL,
  77. vendorName: GpuVendor,
  78. hasFrontFacingBug: HwCapabilities.Vendor == HwCapabilities.GpuVendor.IntelWindows,
  79. hasVectorIndexingBug: HwCapabilities.Vendor == HwCapabilities.GpuVendor.AmdWindows,
  80. supportsAstcCompression: HwCapabilities.SupportsAstcCompression,
  81. supports3DTextureCompression: false,
  82. supportsBgraFormat: false,
  83. supportsR4G4Format: false,
  84. supportsFragmentShaderInterlock: HwCapabilities.SupportsFragmentShaderInterlock,
  85. supportsFragmentShaderOrderingIntel: HwCapabilities.SupportsFragmentShaderOrdering,
  86. supportsImageLoadFormatted: HwCapabilities.SupportsImageLoadFormatted,
  87. supportsMismatchingViewFormat: HwCapabilities.SupportsMismatchingViewFormat,
  88. supportsNonConstantTextureOffset: HwCapabilities.SupportsNonConstantTextureOffset,
  89. supportsShaderBallot: HwCapabilities.SupportsShaderBallot,
  90. supportsTextureShadowLod: HwCapabilities.SupportsTextureShadowLod,
  91. supportsViewportSwizzle: HwCapabilities.SupportsViewportSwizzle,
  92. supportsIndirectParameters: HwCapabilities.SupportsIndirectParameters,
  93. maximumComputeSharedMemorySize: HwCapabilities.MaximumComputeSharedMemorySize,
  94. maximumSupportedAnisotropy: HwCapabilities.MaximumSupportedAnisotropy,
  95. storageBufferOffsetAlignment: HwCapabilities.StorageBufferOffsetAlignment);
  96. }
  97. public void SetBufferData(BufferHandle buffer, int offset, ReadOnlySpan<byte> data)
  98. {
  99. Buffer.SetData(buffer, offset, data);
  100. }
  101. public void UpdateCounters()
  102. {
  103. _counters.Update();
  104. }
  105. public void PreFrame()
  106. {
  107. _sync.Cleanup();
  108. ResourcePool.Tick();
  109. }
  110. public ICounterEvent ReportCounter(CounterType type, EventHandler<ulong> resultHandler, bool hostReserved)
  111. {
  112. return _counters.QueueReport(type, resultHandler, _pipeline.DrawCount, hostReserved);
  113. }
  114. public void Initialize(GraphicsDebugLevel glLogLevel)
  115. {
  116. Debugger.Initialize(glLogLevel);
  117. PrintGpuInformation();
  118. if (HwCapabilities.SupportsParallelShaderCompile)
  119. {
  120. GL.Arb.MaxShaderCompilerThreads(Math.Min(Environment.ProcessorCount, 8));
  121. }
  122. _pipeline.Initialize(this);
  123. _counters.Initialize();
  124. // This is required to disable [0, 1] clamping for SNorm outputs on compatibility profiles.
  125. // This call is expected to fail if we're running with a core profile,
  126. // as this clamp target was deprecated, but that's fine as a core profile
  127. // should already have the desired behaviour were outputs are not clamped.
  128. GL.ClampColor(ClampColorTarget.ClampFragmentColor, ClampColorMode.False);
  129. }
  130. private void PrintGpuInformation()
  131. {
  132. GpuVendor = GL.GetString(StringName.Vendor);
  133. GpuRenderer = GL.GetString(StringName.Renderer);
  134. GpuVersion = GL.GetString(StringName.Version);
  135. Logger.Notice.Print(LogClass.Gpu, $"{GpuVendor} {GpuRenderer} ({GpuVersion})");
  136. }
  137. public void ResetCounter(CounterType type)
  138. {
  139. _counters.QueueReset(type);
  140. }
  141. public void BackgroundContextAction(Action action, bool alwaysBackground = false)
  142. {
  143. // alwaysBackground is ignored, since we cannot switch from the current context.
  144. if (IOpenGLContext.HasContext())
  145. {
  146. action(); // We have a context already - use that (assuming it is the main one).
  147. }
  148. else
  149. {
  150. _window.BackgroundContext.Invoke(action);
  151. }
  152. }
  153. public void InitializeBackgroundContext(IOpenGLContext baseContext)
  154. {
  155. _window.InitializeBackgroundContext(baseContext);
  156. }
  157. public void Dispose()
  158. {
  159. _textureCopy.Dispose();
  160. _backgroundTextureCopy.Dispose();
  161. PersistentBuffers.Dispose();
  162. ResourcePool.Dispose();
  163. _pipeline.Dispose();
  164. _window.Dispose();
  165. _counters.Dispose();
  166. _sync.Dispose();
  167. }
  168. public IProgram LoadProgramBinary(byte[] programBinary, bool hasFragmentShader, ShaderInfo info)
  169. {
  170. return new Program(programBinary, hasFragmentShader, info.FragmentOutputMap);
  171. }
  172. public void CreateSync(ulong id)
  173. {
  174. _sync.Create(id);
  175. }
  176. public void WaitSync(ulong id)
  177. {
  178. _sync.Wait(id);
  179. }
  180. public void Screenshot()
  181. {
  182. _window.ScreenCaptureRequested = true;
  183. }
  184. public void OnScreenCaptured(ScreenCaptureImageInfo bitmap)
  185. {
  186. ScreenCaptured?.Invoke(this, bitmap);
  187. }
  188. }
  189. }