2

我目前正在尝试创建一个互操作层来将我的渲染目标纹理渲染到 Skia SkImage 中。这样做是为了便于从我的图形 API 渲染到 Avalonia。

我已经设法拼凑出足够多的代码,让一切运行起来没有任何错误(至少,我看不到任何错误),但是当我绘制 SkImage 时,我只看到一个黑色图像。

当然,这些东西用代码更容易描述:

private EglPlatformOpenGlInterface _platform;
private AngleWin32EglDisplay _angleDisplay;
private readonly int[] _glTexHandle = new int[1];

IDrawingContextImpl context // <-- From Avalonia

_platform = (EglPlatformOpenGlInterface)platform;
_angleDisplay = (AngleWin32EglDisplay)_platform.Display;

IntPtr d3dDevicePtr = _angleDisplay.GetDirect3DDevice();

// Device5 is from SharpDX.
_d3dDevice = new Device5(d3dDevicePtr);  

// Texture.GetSharedHandle() is the shared handle of my render target.
_eglTarget = _d3dDevice.OpenSharedResource<Texture2D>(_target.Texture.GetSharedHandle());

// WrapDirect3D11Texture calls eglCreatePbufferFromClientBuffer.
_glSurface = _angleDisplay.WrapDirect3D11Texture(_platform, _eglTarget.NativePointer);

using (_platform.PrimaryEglContext.MakeCurrent())
{                
   _platform.PrimaryEglContext.GlInterface.GenTextures(1, _glTexHandle);
}

var fbInfo = new GRGlTextureInfo(GlConsts.GL_TEXTURE_2D, (uint)_glTexHandle[0], GlConsts.GL_RGBA8);
_backendTarget = new GRBackendTexture(_target.Width, _target.Height, false, fbInfo);            

using (_platform.PrimaryEglContext.MakeCurrent())
{                
   // Here's where we find the gl surface to our texture object apparently.
   _platform.PrimaryEglContext.GlInterface.BindTexture(GlConsts.GL_TEXTURE_2D, _glTexHandle[0]);

   EglBindTexImage(_angleDisplay.Handle, _glSurface.DangerousGetHandle(), EglConsts.EGL_BACK_BUFFER);

   _platform.PrimaryEglContext.GlInterface.BindTexture(GlConsts.GL_TEXTURE_2D, 0);
}

// context is a GRContext
_skiaSurface = SKImage.FromTexture(context, _backendTarget, GRSurfaceOrigin.BottomLeft, SKColorType.Rgba8888, SKAlphaType.Premul);

// This clears my render target (obviously). I should be seeing this when I draw the image right?
_target.Clear(GorgonColor.CornFlowerBlue);

canvas.DrawImage(_skiaSurface, new SKPoint(320, 240));

所以,据我所知,这应该有效。但正如我之前所说,它只向我显示黑色图像。应该是矢车菊蓝。我试过打电话FlushID3D11DeviceContext但我仍然得到黑色图像。

有人知道我做错了什么吗?

4

0 回答 0