Renderer.cs 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  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 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 IShader CompileShader(ShaderStage stage, string code)
  42. {
  43. return new Shader(stage, code);
  44. }
  45. public BufferHandle CreateBuffer(int size)
  46. {
  47. BufferCount++;
  48. return Buffer.Create(size);
  49. }
  50. public IProgram CreateProgram(IShader[] shaders, TransformFeedbackDescriptor[] transformFeedbackDescriptors)
  51. {
  52. return new Program(shaders, transformFeedbackDescriptors);
  53. }
  54. public ISampler CreateSampler(SamplerCreateInfo info)
  55. {
  56. return new Sampler(info);
  57. }
  58. public ITexture CreateTexture(TextureCreateInfo info, float scaleFactor)
  59. {
  60. if (info.Target == Target.TextureBuffer)
  61. {
  62. return new TextureBuffer(this, info);
  63. }
  64. else
  65. {
  66. return ResourcePool.GetTextureOrNull(info, scaleFactor) ?? new TextureStorage(this, info, scaleFactor).CreateDefaultView();
  67. }
  68. }
  69. public void DeleteBuffer(BufferHandle buffer)
  70. {
  71. Buffer.Delete(buffer);
  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. HwCapabilities.Vendor == HwCapabilities.GpuVendor.IntelWindows,
  81. HwCapabilities.Vendor == HwCapabilities.GpuVendor.AmdWindows,
  82. HwCapabilities.SupportsAstcCompression,
  83. HwCapabilities.SupportsImageLoadFormatted,
  84. HwCapabilities.SupportsMismatchingViewFormat,
  85. HwCapabilities.SupportsNonConstantTextureOffset,
  86. HwCapabilities.SupportsTextureShadowLod,
  87. HwCapabilities.SupportsViewportSwizzle,
  88. HwCapabilities.SupportsIndirectParameters,
  89. HwCapabilities.MaximumComputeSharedMemorySize,
  90. HwCapabilities.MaximumSupportedAnisotropy,
  91. HwCapabilities.StorageBufferOffsetAlignment);
  92. }
  93. public void SetBufferData(BufferHandle buffer, int offset, ReadOnlySpan<byte> data)
  94. {
  95. Buffer.SetData(buffer, offset, data);
  96. }
  97. public void UpdateCounters()
  98. {
  99. _counters.Update();
  100. }
  101. public void PreFrame()
  102. {
  103. _sync.Cleanup();
  104. ResourcePool.Tick();
  105. }
  106. public ICounterEvent ReportCounter(CounterType type, EventHandler<ulong> resultHandler)
  107. {
  108. return _counters.QueueReport(type, resultHandler, _pipeline.DrawCount);
  109. }
  110. public void Initialize(GraphicsDebugLevel glLogLevel)
  111. {
  112. Debugger.Initialize(glLogLevel);
  113. PrintGpuInformation();
  114. if (HwCapabilities.SupportsParallelShaderCompile)
  115. {
  116. GL.Arb.MaxShaderCompilerThreads(Math.Min(Environment.ProcessorCount, 8));
  117. }
  118. _pipeline.Initialize();
  119. _counters.Initialize();
  120. }
  121. private void PrintGpuInformation()
  122. {
  123. GpuVendor = GL.GetString(StringName.Vendor);
  124. GpuRenderer = GL.GetString(StringName.Renderer);
  125. GpuVersion = GL.GetString(StringName.Version);
  126. Logger.Notice.Print(LogClass.Gpu, $"{GpuVendor} {GpuRenderer} ({GpuVersion})");
  127. }
  128. public void ResetCounter(CounterType type)
  129. {
  130. _counters.QueueReset(type);
  131. }
  132. public void BackgroundContextAction(Action action)
  133. {
  134. if (IOpenGLContext.HasContext())
  135. {
  136. action(); // We have a context already - use that (assuming it is the main one).
  137. }
  138. else
  139. {
  140. _window.BackgroundContext.Invoke(action);
  141. }
  142. }
  143. public void InitializeBackgroundContext(IOpenGLContext baseContext)
  144. {
  145. _window.InitializeBackgroundContext(baseContext);
  146. }
  147. public void Dispose()
  148. {
  149. _textureCopy.Dispose();
  150. _backgroundTextureCopy.Dispose();
  151. PersistentBuffers.Dispose();
  152. ResourcePool.Dispose();
  153. _pipeline.Dispose();
  154. _window.Dispose();
  155. _counters.Dispose();
  156. _sync.Dispose();
  157. }
  158. public IProgram LoadProgramBinary(byte[] programBinary)
  159. {
  160. return new Program(programBinary);
  161. }
  162. public void CreateSync(ulong id)
  163. {
  164. _sync.Create(id);
  165. }
  166. public void WaitSync(ulong id)
  167. {
  168. _sync.Wait(id);
  169. }
  170. public void Screenshot()
  171. {
  172. _window.ScreenCaptureRequested = true;
  173. }
  174. public void OnScreenCaptured(ScreenCaptureImageInfo bitmap)
  175. {
  176. ScreenCaptured?.Invoke(this, bitmap);
  177. }
  178. }
  179. }