我在做什么
我有一个WPF应用程序,我正在使用SlimDX。香港专业教育学院创建了一个用户控件,其中包含一个d3dImage。初始化时,我创建一个新任务,该任务在带有Dx11设备的Texture2D上渲染。重做之后,我使用IProgress对象通过Device9Ex对象将Texture设置为D3dImage的Backbuffer。我使用KeyedMutex来同步共享表面。
问题 问题发生在使用Device9Ex对象设置D3dImage的后备缓冲区的步骤中。 Texture2d构造函数会引发异常。
public void Dx9SetbackBuffer(Texture2D toSetAsBackBuffer)
{
_mutex.Acquire(1,int.MaxValue);
Format textureFormat = TranslateDx11ToDx9Format(toSetAsBackBuffer);
_sharedTexture?.Dispose();
IntPtr handle;
using (var resource = new SlimDX.DXGI.Resource(toSetAsBackBuffer))
{
handle = resource.SharedHandle;
}
// the next line throws Exception: SlimDX.Direct3D9.Direct3D9Exception: "D3DERR_INVALIDCALL: Invalid call (-2005530516)"
_sharedTexture = new Texture(_device,
toSetAsBackBuffer.Description.Width,
toSetAsBackBuffer.Description.Height,
1,
Usage.RenderTarget|Usage.WriteOnly,
textureFormat,
Pool.Default,
ref handle);
using (Surface surface = _sharedTexture.GetSurfaceLevel(0))
{
_renderTo.Lock();
_renderTo.SetBackBuffer(D3DResourceType.IDirect3DSurface9, surface.ComPointer);
_renderTo.AddDirtyRect(new Int32Rect(0, 0, _renderTo.PixelWidth, _renderTo.PixelHeight));
_renderTo.Unlock();
}
_mutex.Release(0);
}
用于初始化dx11设备并创建共享纹理的代码
_device = new Device(DriverType.Hardware, DeviceCreationFlags.Debug |
DeviceCreationFlags.BgraSupport,
FeatureLevel.Level_11_0);
TextureDescription = new Texture2DDescription()
{
BindFlags = BindFlags.RenderTarget|BindFlags.ShaderResource,
Format = Format.B8G8R8A8_UNorm,
ArraySize = 1,
Width = _width > 0 ? _width : 800,
Height = _height > 0 ? _height : 600,
MipLevels = 1,
Usage = ResourceUsage.Default,
SampleDescription = new SampleDescription(1, 0),
CpuAccessFlags = CpuAccessFlags.None,
OptionFlags = ResourceOptionFlags.Shared
};
var depthDescription = new Texture2DDescription()
{
...
};
var mutexDescription = new Texture2DDescription()
{
...
};
_mutexTexture = new Texture2D(_device,mutexDescription);
_mutex = new KeyedMutex(_mutexTexture);
//this is the texture, that is copied by the d9Device
SharedTexture = new Texture2D(_device, TextureDescription);
DepthTexture = new Texture2D(_device, depthDescription);
SampleRenderView = new RenderTargetView(_device, SharedTexture);
SampleDepthView = new DepthStencilView(_device, DepthTexture);
如您所见,即时消息仅使用专用纹理进行表面同步。 我不确定这是否正确,因为我在使用共享和keyedmutex标志初始化单个Texture时遇到问题。
如果您需要更多信息,请告诉我。
我将非常感谢您的帮助。