Renderer.cs 5.6 KB

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