OpenGLRenderer.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336
  1. using OpenTK.Graphics.OpenGL;
  2. using Ryujinx.Common.Configuration;
  3. using Ryujinx.Common.Logging;
  4. using Ryujinx.Graphics.GAL;
  5. using Ryujinx.Graphics.OpenGL.Image;
  6. using Ryujinx.Graphics.OpenGL.Queries;
  7. using Ryujinx.Graphics.Shader.Translation;
  8. using System;
  9. namespace Ryujinx.Graphics.OpenGL
  10. {
  11. public sealed class OpenGLRenderer : IRenderer
  12. {
  13. private readonly Pipeline _pipeline;
  14. public IPipeline Pipeline => _pipeline;
  15. private readonly Counters _counters;
  16. private readonly Window _window;
  17. public IWindow Window => _window;
  18. private readonly TextureCopy _textureCopy;
  19. private readonly TextureCopy _backgroundTextureCopy;
  20. internal TextureCopy TextureCopy => BackgroundContextWorker.InBackground ? _backgroundTextureCopy : _textureCopy;
  21. internal TextureCopyIncompatible TextureCopyIncompatible { get; }
  22. internal TextureCopyMS TextureCopyMS { get; }
  23. private readonly Sync _sync;
  24. public event EventHandler<ScreenCaptureImageInfo> ScreenCaptured;
  25. internal PersistentBuffers PersistentBuffers { get; }
  26. internal ResourcePool ResourcePool { get; }
  27. internal int BufferCount { get; private set; }
  28. public string GpuVendor { get; private set; }
  29. public string GpuRenderer { get; private set; }
  30. public string GpuVersion { get; private set; }
  31. public bool PreferThreading => true;
  32. public OpenGLRenderer()
  33. {
  34. _pipeline = new Pipeline();
  35. _counters = new Counters();
  36. _window = new Window(this);
  37. _textureCopy = new TextureCopy(this);
  38. _backgroundTextureCopy = new TextureCopy(this);
  39. TextureCopyIncompatible = new TextureCopyIncompatible(this);
  40. TextureCopyMS = new TextureCopyMS(this);
  41. _sync = new Sync();
  42. PersistentBuffers = new PersistentBuffers();
  43. ResourcePool = new ResourcePool();
  44. }
  45. public BufferHandle CreateBuffer(int size, GAL.BufferAccess access)
  46. {
  47. BufferCount++;
  48. var memType = access & GAL.BufferAccess.MemoryTypeMask;
  49. if (memType == GAL.BufferAccess.HostMemory)
  50. {
  51. BufferHandle handle = Buffer.CreatePersistent(size);
  52. PersistentBuffers.Map(handle, size);
  53. return handle;
  54. }
  55. else
  56. {
  57. return Buffer.Create(size);
  58. }
  59. }
  60. public BufferHandle CreateBuffer(nint pointer, int size)
  61. {
  62. throw new NotSupportedException();
  63. }
  64. public BufferHandle CreateBufferSparse(ReadOnlySpan<BufferRange> storageBuffers)
  65. {
  66. throw new NotSupportedException();
  67. }
  68. public IImageArray CreateImageArray(int size, bool isBuffer)
  69. {
  70. return new ImageArray(size);
  71. }
  72. public IProgram CreateProgram(ShaderSource[] shaders, ShaderInfo info)
  73. {
  74. return new Program(shaders, info.FragmentOutputMap);
  75. }
  76. public ISampler CreateSampler(SamplerCreateInfo info)
  77. {
  78. return new Sampler(info);
  79. }
  80. public ITexture CreateTexture(TextureCreateInfo info)
  81. {
  82. if (info.Target == Target.TextureBuffer)
  83. {
  84. return new TextureBuffer(this, info);
  85. }
  86. else
  87. {
  88. return ResourcePool.GetTextureOrNull(info) ?? new TextureStorage(this, info).CreateDefaultView();
  89. }
  90. }
  91. public ITextureArray CreateTextureArray(int size, bool isBuffer)
  92. {
  93. return new TextureArray(size);
  94. }
  95. public void DeleteBuffer(BufferHandle buffer)
  96. {
  97. PersistentBuffers.Unmap(buffer);
  98. Buffer.Delete(buffer);
  99. }
  100. public HardwareInfo GetHardwareInfo()
  101. {
  102. return new HardwareInfo(GpuVendor, GpuRenderer, GpuVendor); // OpenGL does not provide a driver name, vendor name is closest analogue.
  103. }
  104. public PinnedSpan<byte> GetBufferData(BufferHandle buffer, int offset, int size)
  105. {
  106. return Buffer.GetData(this, buffer, offset, size);
  107. }
  108. public Capabilities GetCapabilities()
  109. {
  110. bool intelWindows = HwCapabilities.Vendor == HwCapabilities.GpuVendor.IntelWindows;
  111. bool intelUnix = HwCapabilities.Vendor == HwCapabilities.GpuVendor.IntelUnix;
  112. bool amdWindows = HwCapabilities.Vendor == HwCapabilities.GpuVendor.AmdWindows;
  113. return new Capabilities(
  114. api: TargetApi.OpenGL,
  115. vendorName: GpuVendor,
  116. memoryType: SystemMemoryType.BackendManaged,
  117. hasFrontFacingBug: intelWindows,
  118. hasVectorIndexingBug: amdWindows,
  119. needsFragmentOutputSpecialization: false,
  120. reduceShaderPrecision: false,
  121. supportsAstcCompression: HwCapabilities.SupportsAstcCompression,
  122. supportsBc123Compression: HwCapabilities.SupportsTextureCompressionS3tc,
  123. supportsBc45Compression: HwCapabilities.SupportsTextureCompressionRgtc,
  124. supportsBc67Compression: true, // Should check BPTC extension, but for some reason NVIDIA is not exposing the extension.
  125. supportsEtc2Compression: true,
  126. supports3DTextureCompression: false,
  127. supportsBgraFormat: false,
  128. supportsR4G4Format: false,
  129. supportsR4G4B4A4Format: true,
  130. supportsScaledVertexFormats: true,
  131. supportsSnormBufferTextureFormat: false,
  132. supports5BitComponentFormat: true,
  133. supportsSparseBuffer: false,
  134. supportsBlendEquationAdvanced: HwCapabilities.SupportsBlendEquationAdvanced,
  135. supportsFragmentShaderInterlock: HwCapabilities.SupportsFragmentShaderInterlock,
  136. supportsFragmentShaderOrderingIntel: HwCapabilities.SupportsFragmentShaderOrdering,
  137. supportsGeometryShader: true,
  138. supportsGeometryShaderPassthrough: HwCapabilities.SupportsGeometryShaderPassthrough,
  139. supportsTransformFeedback: true,
  140. supportsImageLoadFormatted: HwCapabilities.SupportsImageLoadFormatted,
  141. supportsLayerVertexTessellation: HwCapabilities.SupportsShaderViewportLayerArray,
  142. supportsMismatchingViewFormat: HwCapabilities.SupportsMismatchingViewFormat,
  143. supportsCubemapView: true,
  144. supportsNonConstantTextureOffset: HwCapabilities.SupportsNonConstantTextureOffset,
  145. supportsQuads: HwCapabilities.SupportsQuads,
  146. supportsSeparateSampler: false,
  147. supportsShaderBallot: HwCapabilities.SupportsShaderBallot,
  148. supportsShaderBarrierDivergence: !(intelWindows || intelUnix),
  149. supportsShaderFloat64: true,
  150. supportsTextureGatherOffsets: true,
  151. supportsTextureShadowLod: HwCapabilities.SupportsTextureShadowLod,
  152. supportsVertexStoreAndAtomics: true,
  153. supportsViewportIndexVertexTessellation: HwCapabilities.SupportsShaderViewportLayerArray,
  154. supportsViewportMask: HwCapabilities.SupportsViewportArray2,
  155. supportsViewportSwizzle: HwCapabilities.SupportsViewportSwizzle,
  156. supportsIndirectParameters: HwCapabilities.SupportsIndirectParameters,
  157. supportsDepthClipControl: true,
  158. uniformBufferSetIndex: 0,
  159. storageBufferSetIndex: 1,
  160. textureSetIndex: 2,
  161. imageSetIndex: 3,
  162. extraSetBaseIndex: 0,
  163. maximumExtraSets: 0,
  164. maximumUniformBuffersPerStage: 13, // TODO: Avoid hardcoding those limits here and get from driver?
  165. maximumStorageBuffersPerStage: 16,
  166. maximumTexturesPerStage: 32,
  167. maximumImagesPerStage: 8,
  168. maximumComputeSharedMemorySize: HwCapabilities.MaximumComputeSharedMemorySize,
  169. maximumSupportedAnisotropy: HwCapabilities.MaximumSupportedAnisotropy,
  170. shaderSubgroupSize: Constants.MaxSubgroupSize,
  171. storageBufferOffsetAlignment: HwCapabilities.StorageBufferOffsetAlignment,
  172. textureBufferOffsetAlignment: HwCapabilities.TextureBufferOffsetAlignment,
  173. gatherBiasPrecision: intelWindows || amdWindows ? 8 : 0, // Precision is 8 for these vendors on Vulkan.
  174. maximumGpuMemory: 0);
  175. }
  176. public void SetBufferData(BufferHandle buffer, int offset, ReadOnlySpan<byte> data)
  177. {
  178. Buffer.SetData(buffer, offset, data);
  179. }
  180. public void UpdateCounters()
  181. {
  182. _counters.Update();
  183. }
  184. public void PreFrame()
  185. {
  186. _sync.Cleanup();
  187. ResourcePool.Tick();
  188. }
  189. public ICounterEvent ReportCounter(CounterType type, EventHandler<ulong> resultHandler, float divisor, bool hostReserved)
  190. {
  191. return _counters.QueueReport(type, resultHandler, divisor, _pipeline.DrawCount, hostReserved);
  192. }
  193. public void Initialize(GraphicsDebugLevel glLogLevel)
  194. {
  195. Debugger.Initialize(glLogLevel);
  196. PrintGpuInformation();
  197. if (HwCapabilities.SupportsParallelShaderCompile)
  198. {
  199. GL.Arb.MaxShaderCompilerThreads(Math.Min(Environment.ProcessorCount, 8));
  200. }
  201. _counters.Initialize();
  202. // This is required to disable [0, 1] clamping for SNorm outputs on compatibility profiles.
  203. // This call is expected to fail if we're running with a core profile,
  204. // as this clamp target was deprecated, but that's fine as a core profile
  205. // should already have the desired behaviour were outputs are not clamped.
  206. GL.ClampColor(ClampColorTarget.ClampFragmentColor, ClampColorMode.False);
  207. }
  208. private void PrintGpuInformation()
  209. {
  210. GpuVendor = GL.GetString(StringName.Vendor);
  211. GpuRenderer = GL.GetString(StringName.Renderer);
  212. GpuVersion = GL.GetString(StringName.Version);
  213. Logger.Notice.Print(LogClass.Gpu, $"{GpuVendor} {GpuRenderer} ({GpuVersion})");
  214. }
  215. public void ResetCounter(CounterType type)
  216. {
  217. _counters.QueueReset(type);
  218. }
  219. public void BackgroundContextAction(Action action, bool alwaysBackground = false)
  220. {
  221. // alwaysBackground is ignored, since we cannot switch from the current context.
  222. if (_window.BackgroundContext.HasContext())
  223. {
  224. action(); // We have a context already - use that (assuming it is the main one).
  225. }
  226. else
  227. {
  228. _window.BackgroundContext.Invoke(action);
  229. }
  230. }
  231. public void InitializeBackgroundContext(IOpenGLContext baseContext)
  232. {
  233. _window.InitializeBackgroundContext(baseContext);
  234. }
  235. public void Dispose()
  236. {
  237. _textureCopy.Dispose();
  238. _backgroundTextureCopy.Dispose();
  239. TextureCopyMS.Dispose();
  240. PersistentBuffers.Dispose();
  241. ResourcePool.Dispose();
  242. _pipeline.Dispose();
  243. _window.Dispose();
  244. _counters.Dispose();
  245. _sync.Dispose();
  246. }
  247. public IProgram LoadProgramBinary(byte[] programBinary, bool hasFragmentShader, ShaderInfo info)
  248. {
  249. return new Program(programBinary, hasFragmentShader, info.FragmentOutputMap);
  250. }
  251. public void CreateSync(ulong id, bool strict)
  252. {
  253. _sync.Create(id);
  254. }
  255. public void WaitSync(ulong id)
  256. {
  257. _sync.Wait(id);
  258. }
  259. public ulong GetCurrentSync()
  260. {
  261. return _sync.GetCurrent();
  262. }
  263. public void SetInterruptAction(Action<Action> interruptAction)
  264. {
  265. // Currently no need for an interrupt action.
  266. }
  267. public void Screenshot()
  268. {
  269. _window.ScreenCaptureRequested = true;
  270. }
  271. public void OnScreenCaptured(ScreenCaptureImageInfo bitmap)
  272. {
  273. ScreenCaptured?.Invoke(this, bitmap);
  274. }
  275. public bool PrepareHostMapping(nint address, ulong size)
  276. {
  277. return false;
  278. }
  279. }
  280. }