【发布时间】:2021-09-01 20:19:01
【问题描述】:
我在 .NET 5(更好的跨平台)中使用 SkiaSharp 进行屏幕外渲染,并且需要 GPU 加速。 注意:C++ 版本的 Skia 也可以使用。
开发是在 Windows 上进行的。我没有使用 SkiaSharp.View,因为该程序是一个命令行工具。我刚刚创建了一个隐藏窗口。该窗口是使用 OpenTk 创建的,因为我在 GitHub 上看到了 Matthew 的示例,并且我将窗口的上下文设置为默认上下文。然后,我创建了一个帧缓冲区和渲染缓冲区,如SO solution 中所述。最后,我尝试创建表面,但只从SKSurface.Create 得到一个空值。
// GL below is all OpenTK.Graphics.ES30.GL!
var window = new GameWindow(
GameWindowSettings.Default,
new NativeWindowSettings {
Size = new Vector2i(1, 1),
StartVisible = false,
Flags = OpenTK.Windowing.Common.ContextFlags.Offscreen,
APIVersion = new Version(3, 2) });
window.Context.MakeCurrent();
var gpuInterface = GRGlInterface.Create(); // Q: Is this interface correctly linked to the one OpenTk uses?
var grContext = GRContext.CreateGl(gpuInterface);
uint fbo = (uint)GL.GenFramebuffer();
uint rbo = (uint)GL.GenRenderbuffer();
GL.BindRenderbuffer(OpenTK.Graphics.ES30.RenderbufferTarget.Renderbuffer, rbo);
GL.RenderbufferStorage(OpenTK.Graphics.ES30.RenderbufferTarget.Renderbuffer, OpenTK.Graphics.ES30.RenderbufferInternalFormat.Rgba8, 800, 700);
GL.BindFramebuffer(OpenTK.Graphics.ES30.FramebufferTarget.DrawFramebuffer, fbo);
GL.FramebufferRenderbuffer(
OpenTK.Graphics.ES30.FramebufferTarget.DrawFramebuffer,
OpenTK.Graphics.ES30.FramebufferAttachment.ColorAttachment0,
OpenTK.Graphics.ES30.RenderbufferTarget.Renderbuffer,
rbo);
var sampleCounts = GL.GetInteger(OpenTK.Graphics.ES30.GetPName.Samples);
int stencilBits;
GL.GetFramebufferAttachmentParameter(
OpenTK.Graphics.ES30.FramebufferTarget.DrawFramebuffer,
OpenTK.Graphics.ES30.FramebufferAttachment.ColorAttachment0,
OpenTK.Graphics.ES30.FramebufferParameterName.FramebufferAttachmentStencilSize,
out stencilBits);
var target = new GRBackendRenderTarget(800, 700, sampleCounts, stencilBits, new GRGlFramebufferInfo(fbo, SKColorType.Bgra8888.ToGlSizedFormat()));
var surface = SKSurface.Create(grContext, target, GRSurfaceOrigin.TopLeft, SKColorType.Bgra8888);
// surface == null ?
此外,使用GL.GetError 也不会出错。
任何帮助将不胜感激。
【问题讨论】:
-
你做 window.Context.MakeCurrent(); 然后你再次创建上下文: var grContext = GRContext.CreateGl(gpuInterface); .我对 Skia API 不是很熟悉,但首先要确保你最后创建的上下文是有效的和最新的。你也检查过你的 FBO 句柄吗?
-
Michael GRContext.createGl() 只为 Skia 创建一个到 OpenGL API 的链接,skia 的 OpenGL 支持旨在在创建 GrContext 之前创建一个 OpenGL 上下文。
标签: opengl opentk skiasharp skia