Renderer.cs 6.1 KB

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