Renderer.cs 5.8 KB

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