Renderer.cs 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  1. using OpenTK.Graphics;
  2. using OpenTK.Graphics.OpenGL;
  3. using Ryujinx.Common.Configuration;
  4. using Ryujinx.Common.Logging;
  5. using Ryujinx.Graphics.GAL;
  6. using Ryujinx.Graphics.OpenGL.Image;
  7. using Ryujinx.Graphics.OpenGL.Queries;
  8. using Ryujinx.Graphics.Shader;
  9. using System;
  10. namespace Ryujinx.Graphics.OpenGL
  11. {
  12. public sealed class Renderer : IRenderer
  13. {
  14. private readonly Pipeline _pipeline;
  15. public IPipeline Pipeline => _pipeline;
  16. private readonly Counters _counters;
  17. private readonly Window _window;
  18. public IWindow Window => _window;
  19. private TextureCopy _textureCopy;
  20. private TextureCopy _backgroundTextureCopy;
  21. internal TextureCopy TextureCopy => BackgroundContextWorker.InBackground ? _backgroundTextureCopy : _textureCopy;
  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 Renderer()
  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. _sync = new Sync();
  39. PersistentBuffers = new PersistentBuffers();
  40. ResourcePool = new ResourcePool();
  41. }
  42. public IShader CompileShader(ShaderStage stage, string code)
  43. {
  44. return new Shader(stage, code);
  45. }
  46. public BufferHandle CreateBuffer(int size)
  47. {
  48. BufferCount++;
  49. return Buffer.Create(size);
  50. }
  51. public IProgram CreateProgram(IShader[] shaders, ShaderInfo info)
  52. {
  53. return new Program(shaders, info.FragmentOutputMap);
  54. }
  55. public ISampler CreateSampler(SamplerCreateInfo info)
  56. {
  57. return new Sampler(info);
  58. }
  59. public ITexture CreateTexture(TextureCreateInfo info, float scaleFactor)
  60. {
  61. if (info.Target == Target.TextureBuffer)
  62. {
  63. return new TextureBuffer(this, info);
  64. }
  65. else
  66. {
  67. return ResourcePool.GetTextureOrNull(info, scaleFactor) ?? new TextureStorage(this, info, scaleFactor).CreateDefaultView();
  68. }
  69. }
  70. public void DeleteBuffer(BufferHandle buffer)
  71. {
  72. Buffer.Delete(buffer);
  73. }
  74. public ReadOnlySpan<byte> GetBufferData(BufferHandle buffer, int offset, int size)
  75. {
  76. return Buffer.GetData(this, buffer, offset, size);
  77. }
  78. public Capabilities GetCapabilities()
  79. {
  80. return new Capabilities(
  81. hasFrontFacingBug: HwCapabilities.Vendor == HwCapabilities.GpuVendor.IntelWindows,
  82. hasVectorIndexingBug: HwCapabilities.Vendor == HwCapabilities.GpuVendor.AmdWindows,
  83. supportsAstcCompression: HwCapabilities.SupportsAstcCompression,
  84. supports3DTextureCompression: false,
  85. supportsBgraFormat: false,
  86. supportsR4G4Format: false,
  87. supportsFragmentShaderInterlock: HwCapabilities.SupportsFragmentShaderInterlock,
  88. supportsFragmentShaderOrderingIntel: HwCapabilities.SupportsFragmentShaderOrdering,
  89. supportsImageLoadFormatted: HwCapabilities.SupportsImageLoadFormatted,
  90. supportsMismatchingViewFormat: HwCapabilities.SupportsMismatchingViewFormat,
  91. supportsNonConstantTextureOffset: HwCapabilities.SupportsNonConstantTextureOffset,
  92. supportsShaderBallot: HwCapabilities.SupportsShaderBallot,
  93. supportsTextureShadowLod: HwCapabilities.SupportsTextureShadowLod,
  94. supportsViewportSwizzle: HwCapabilities.SupportsViewportSwizzle,
  95. supportsIndirectParameters: HwCapabilities.SupportsIndirectParameters,
  96. maximumComputeSharedMemorySize: HwCapabilities.MaximumComputeSharedMemorySize,
  97. maximumSupportedAnisotropy: HwCapabilities.MaximumSupportedAnisotropy,
  98. storageBufferOffsetAlignment: HwCapabilities.StorageBufferOffsetAlignment);
  99. }
  100. public void SetBufferData(BufferHandle buffer, int offset, ReadOnlySpan<byte> data)
  101. {
  102. Buffer.SetData(buffer, offset, data);
  103. }
  104. public void UpdateCounters()
  105. {
  106. _counters.Update();
  107. }
  108. public void PreFrame()
  109. {
  110. _sync.Cleanup();
  111. ResourcePool.Tick();
  112. }
  113. public ICounterEvent ReportCounter(CounterType type, EventHandler<ulong> resultHandler, bool hostReserved)
  114. {
  115. return _counters.QueueReport(type, resultHandler, _pipeline.DrawCount, hostReserved);
  116. }
  117. public void Initialize(GraphicsDebugLevel glLogLevel)
  118. {
  119. Debugger.Initialize(glLogLevel);
  120. PrintGpuInformation();
  121. if (HwCapabilities.SupportsParallelShaderCompile)
  122. {
  123. GL.Arb.MaxShaderCompilerThreads(Math.Min(Environment.ProcessorCount, 8));
  124. }
  125. _pipeline.Initialize(this);
  126. _counters.Initialize();
  127. // This is required to disable [0, 1] clamping for SNorm outputs on compatibility profiles.
  128. // This call is expected to fail if we're running with a core profile,
  129. // as this clamp target was deprecated, but that's fine as a core profile
  130. // should already have the desired behaviour were outputs are not clamped.
  131. GL.ClampColor(ClampColorTarget.ClampFragmentColor, ClampColorMode.False);
  132. }
  133. private void PrintGpuInformation()
  134. {
  135. GpuVendor = GL.GetString(StringName.Vendor);
  136. GpuRenderer = GL.GetString(StringName.Renderer);
  137. GpuVersion = GL.GetString(StringName.Version);
  138. Logger.Notice.Print(LogClass.Gpu, $"{GpuVendor} {GpuRenderer} ({GpuVersion})");
  139. }
  140. public void ResetCounter(CounterType type)
  141. {
  142. _counters.QueueReset(type);
  143. }
  144. public void BackgroundContextAction(Action action, bool alwaysBackground = false)
  145. {
  146. // alwaysBackground is ignored, since we cannot switch from the current context.
  147. if (IOpenGLContext.HasContext())
  148. {
  149. action(); // We have a context already - use that (assuming it is the main one).
  150. }
  151. else
  152. {
  153. _window.BackgroundContext.Invoke(action);
  154. }
  155. }
  156. public void InitializeBackgroundContext(IOpenGLContext baseContext)
  157. {
  158. _window.InitializeBackgroundContext(baseContext);
  159. }
  160. public void Dispose()
  161. {
  162. _textureCopy.Dispose();
  163. _backgroundTextureCopy.Dispose();
  164. PersistentBuffers.Dispose();
  165. ResourcePool.Dispose();
  166. _pipeline.Dispose();
  167. _window.Dispose();
  168. _counters.Dispose();
  169. _sync.Dispose();
  170. }
  171. public IProgram LoadProgramBinary(byte[] programBinary, bool hasFragmentShader, ShaderInfo info)
  172. {
  173. return new Program(programBinary, hasFragmentShader, info.FragmentOutputMap);
  174. }
  175. public void CreateSync(ulong id)
  176. {
  177. _sync.Create(id);
  178. }
  179. public void WaitSync(ulong id)
  180. {
  181. _sync.Wait(id);
  182. }
  183. public void Screenshot()
  184. {
  185. _window.ScreenCaptureRequested = true;
  186. }
  187. public void OnScreenCaptured(ScreenCaptureImageInfo bitmap)
  188. {
  189. ScreenCaptured?.Invoke(this, bitmap);
  190. }
  191. }
  192. }