Renderer.cs 5.0 KB

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