sharpdx / SharpDX

SharpDX GitHub Repository
http://sharpdx.org
MIT License
1.68k stars 641 forks source link

Creating 2d rendertarget throwing exceptions sometimes. #1065

Closed Gavin-Williams closed 5 years ago

Gavin-Williams commented 5 years ago

I have this code ..

// create the 2d render target -------------------------------------------------------- using (var surface = backBuffer.QueryInterface<Surface>()) { renderTarget2dDefault = new RenderTarget( graphics.factory2d, surface, new RenderTargetProperties( new PixelFormat( SwapChainFormat, SharpDX.Direct2D1.AlphaMode.Premultiplied))) { AntialiasMode = AntialiasMode.PerPrimitive }; }

Sometimes it throws exceptions ..

SwapChain.Ctor() Exception thrown: 'System.Runtime.InteropServices.SEHException' in SharpDX.Direct2D1.dll An exception of type 'System.Runtime.InteropServices.SEHException' occurred in SharpDX.Direct2D1.dll but was not handled in user code External component has thrown an exception.

What could be causing this, is it a timing issue in my own code or perhaps something wrong with SharpDX.

Edit:

argument states:

Surface ◢ | surface | {SharpDX.DXGI.Surface} | SharpDX.DXGI.Surface   | DebugName | "BackBuffer" | string   | ◢ Description | {SharpDX.DXGI.SurfaceDescription} | SharpDX.DXGI.SurfaceDescription   | Format | R8G8B8A8_UNorm | SharpDX.DXGI.Format   | Height | 900 | int   | ▶ SampleDescription | {{1, 0}} | SharpDX.DXGI.SampleDescription   | Width | 1600 | int   | IsDisposed | false | bool   | ▶ NativePointer | {201916268} | System.IntPtr   | Tag | null | object   | _nativePointer | 0x0c08ff6c | void*

Factory2d ◢ | graphics.factory2d | {SharpDX.Direct2D1.Factory2} | SharpDX.Direct2D1.Factory2   | ▶ DesktopDpi | {(96,96)} | SharpDX.Size2F   | IsDisposed | false | bool   | ▶ NativePointer | {22082648} | System.IntPtr   | ▶ RegisteredEffects | {System.Guid[64]} | System.Guid[]   | Tag | null | object   | _nativePointer | 0x0150f458 | void*

SwapChainFormat   | SwapChainFormat | R8G8B8A8_UNorm | SharpDX.DXGI.Format

Other device states:

Device ◢ | graphics.Device3d | {SharpDX.Direct3D11.Device5} | SharpDX.Direct3D11.Device5   | CreationFlags | Debug | BgraSupport | SharpDX.Direct3D11.DeviceCreationFlags   | DebugName | "device3d" | string   | ▶ DeviceRemovedReason | {HRESULT = 0x0} | SharpDX.Result   | ExceptionMode | 0 | int   | FeatureLevel | Level_11_1 | SharpDX.Direct3D.FeatureLevel   | ▶ ImmediateContext | {SharpDX.Direct3D11.DeviceContext} | SharpDX.Direct3D11.DeviceContext   | ▶ ImmediateContext1 | {SharpDX.Direct3D11.DeviceContext1} | SharpDX.Direct3D11.DeviceContext1   | ▶ ImmediateContext1 | {SharpDX.Direct3D11.DeviceContext1} | SharpDX.Direct3D11.DeviceContext1   | ▶ ImmediateContext2 | {SharpDX.Direct3D11.DeviceContext2} | SharpDX.Direct3D11.DeviceContext2   | ▶ ImmediateContext2 | {SharpDX.Direct3D11.DeviceContext2} | SharpDX.Direct3D11.DeviceContext2   | ▶ ImmediateContext3 | {SharpDX.Direct3D11.DeviceContext3} | SharpDX.Direct3D11.DeviceContext3   | ▶ ImmediateContext3 | {SharpDX.Direct3D11.DeviceContext3} | SharpDX.Direct3D11.DeviceContext3   | ▶ ImmediateContext | {SharpDX.Direct3D11.DeviceContext} | SharpDX.Direct3D11.DeviceContext   | IsDisposed | false | bool   | ▶ NativePointer | {33341524} | System.IntPtr   | Tag | null | object   | _nativePointer | 0x01fcc054 | void*

BackBuffer ◢ | backBuffer | {SharpDX.Direct3D11.Texture2D} | SharpDX.Direct3D11.Texture2D   | DebugName | "BackBuffer" | string   | ◢ Description | {SharpDX.Direct3D11.Texture2DDescription} | SharpDX.Direct3D11.Texture2DDescription   | ArraySize | 1 | int   | BindFlags | RenderTarget | SharpDX.Direct3D11.BindFlags   | CpuAccessFlags | None | SharpDX.Direct3D11.CpuAccessFlags   | Format | R8G8B8A8_UNorm | SharpDX.DXGI.Format   | Height | 900 | int   | MipLevels | 1 | int   | OptionFlags | None | SharpDX.Direct3D11.ResourceOptionFlags   | ▶ SampleDescription | {{1, 0}} | SharpDX.DXGI.SampleDescription   | Usage | Default | SharpDX.Direct3D11.ResourceUsage   | Width | 1600 | int   | ▶ Device | {SharpDX.Direct3D11.Device} | SharpDX.Direct3D11.Device   | ▶ Device__ | {SharpDX.Direct3D11.Device} | SharpDX.Direct3D11.Device   | Dimension | Texture2D | SharpDX.Direct3D11.ResourceDimension   | EvictionPriority | 2013265920 | int   | IsDisposed | false | bool   | ▶ NativePointer | {201915988} | System.IntPtr   | Tag | null | object   | _nativePointer | 0x0c08fe54 | void*

ImmediateContext ◢ | ImmediateContext | {SharpDX.Direct3D11.DeviceContext} | SharpDX.Direct3D11.DeviceContext   | ▶ ComputeShader | {SharpDX.Direct3D11.ComputeShaderStage} | SharpDX.Direct3D11.ComputeShaderStage   | ▶ ContextFlags | 'graphics.Device3d.ImmediateContext.ContextFlags' threw an exception of type 'System.Runtime.InteropServices.SEHException' | int {System.Runtime.InteropServices.SEHException}   | DebugName | "context3d" | string   | ▶ Device | {SharpDX.Direct3D11.Device} | SharpDX.Direct3D11.Device   | ▶ Device__ | {SharpDX.Direct3D11.Device} | SharpDX.Direct3D11.Device   | ▶ DomainShader | {SharpDX.Direct3D11.DomainShaderStage} | SharpDX.Direct3D11.DomainShaderStage   | ▶ GeometryShader | {SharpDX.Direct3D11.GeometryShaderStage} | SharpDX.Direct3D11.GeometryShaderStage   | ▶ HullShader | {SharpDX.Direct3D11.HullShaderStage} | SharpDX.Direct3D11.HullShaderStage   | ▶ InputAssembler | {SharpDX.Direct3D11.InputAssemblerStage} | SharpDX.Direct3D11.InputAssemblerStage   | IsDisposed | false | bool   | ▶ NativePointer | {33263688} | System.IntPtr   | ▶ OutputMerger | {SharpDX.Direct3D11.OutputMergerStage} | SharpDX.Direct3D11.OutputMergerStage   | ▶ PixelShader | {SharpDX.Direct3D11.PixelShaderStage} | SharpDX.Direct3D11.PixelShaderStage   | ▶ Rasterizer | {SharpDX.Direct3D11.RasterizerStage} | SharpDX.Direct3D11.RasterizerStage   | ▶ StreamOutput | {SharpDX.Direct3D11.StreamOutputStage} | SharpDX.Direct3D11.StreamOutputStage   | Tag | null | object   | ▶ TypeInfo | 'graphics.Device3d.ImmediateContext.TypeInfo' threw an exception of type 'System.Runtime.InteropServices.SEHException' | SharpDX.Direct3D11.DeviceContextType {System.Runtime.InteropServices.SEHException}   | ▶ VertexShader | {SharpDX.Direct3D11.VertexShaderStage} | SharpDX.Direct3D11.VertexShaderStage   | _nativePointer | 0x01fb9048 | void*

ImmediateContext.ContextFlags ◢ | ContextFlags | 'graphics.Device3d.ImmediateContext.ContextFlags' threw an exception of type 'System.Runtime.InteropServices.SEHException' | int {System.Runtime.InteropServices.SEHException}   | ▶ Data | {System.Collections.ListDictionaryInternal} | System.Collections.IDictionary {System.Collections.ListDictionaryInternal}   | ErrorCode | -2147467259 | int   | HResult | -2147467259 | int   | HelpLink | null | string   | ◢ InnerException | null | System.Exception   | ▶ Static members |   |     | Message | "External component has thrown an exception." | string   | Source | "SharpDX.Direct3D11" | string   | StackTrace | " at SharpDX.Direct3D11.DeviceContext.GetContextFlags()\r\n at SharpDX.Direct3D11.DeviceContext.get_ContextFlags()" | string   | ◢ Static members |   |     | ◢ Non-Public members |   |     | _COMPlusExceptionCode | -532462766 | int   | s_EDILock | {object} | object   | ◢ Non-Public members |   |     | ▶ IPForWatsonBuckets | {123588342} | System.UIntPtr   | IsTransient | false | bool   | RemoteStackTrace | null | string   | WatsonBuckets | null | object   | _HResult | -2147467259 | int   | _className | null | string   | ▶ _data | {System.Collections.ListDictionaryInternal} | System.Collections.IDictionary {System.Collections.ListDictionaryInternal}   | _dynamicMethods | null | object   | _exceptionMethod | null | System.Reflection.MethodBase   | _exceptionMethodString | null | string   | _helpURL | null | string   | ▶ _innerException | null | System.Exception   | ▶ _ipForWatsonBuckets | {123588342} | System.UIntPtr   | _message | "External component has thrown an exception." | string   | _remoteStackIndex | 0 | int   | _remoteStackTraceString | null | string   | _source | "SharpDX.Direct3D11" | string   | ▶ _stackTrace | {sbyte[48]} | object {sbyte[]}   | _stackTraceString | null | string   | _watsonBuckets | null | object   | _xcode | 2173 | int   | ▶ _xptrs | {213813264} | System.IntPtr

Exception ◢ | $exception | {System.Runtime.InteropServices.SEHException: External component has thrown an exception. at SharpDX.Direct2D1.Factory.CreateDxgiSurfaceRenderTarget(Surface dxgiSurface, RenderTargetProperties& renderTargetProperties, RenderTarget renderTarget) at SharpDX.Direct2D1.RenderTarget..ctor(Factory factory, Surface dxgiSurface, RenderTargetProperties properties) at Nucleus.Graphics.Direct3d11.GpuSwapChain..ctor(Dx11Graphics gfx, CoreWindow coreWindow, Int32 width, Int32 height) at Nucleus.Graphics.Direct3d11.Dx11Graphics.SynchronizeSwapChainToCoreWindow(Int32 key, CoreWindow coreWindow, Int32 w, Int32 h) at Nucleus.Core.EngineCore.Render(FrameDescriptor frame) at Nucleus.Core.EngineCore.b__36_0(IAsyncAction action)} | System.Runtime.InteropServices.SEHException   | ▶ Data | {System.Collections.ListDictionaryInternal} | System.Collections.IDictionary {System.Collections.ListDictionaryInternal}   | ErrorCode | 0x80004005 | int   | HResult | 0x80004005 | int   | HelpLink | null | string   | ▶ InnerException | null | System.Exception   | Message | "External component has thrown an exception." | string   | Source | "SharpDX.Direct2D1" | string   | StackTrace | " at SharpDX.Direct2D1.Factory.CreateDxgiSurfaceRenderTarget(Surface dxgiSurface, RenderTargetProperties& renderTargetProperties, RenderTarget renderTarget)\r\n at SharpDX.Direct2D1.RenderTarget..ctor(Factory factory, Surface dxgiSurface, RenderTargetProperties properties)\r\n at Nucleus.Graphics.Direct3d11.GpuSwapChain..ctor(Dx11Graphics gfx, CoreWindow coreWindow, Int32 width, Int32 height)\r\n at Nucleus.Graphics.Direct3d11.Dx11Graphics.SynchronizeSwapChainToCoreWindow(Int32 key, CoreWindow coreWindow, Int32 w, Int32 h)\r\n at Nucleus.Core.EngineCore.Render(FrameDescriptor frame)\r\n at Nucleus.Core.EngineCore.b__36_0(IAsyncAction action)" | string

Gavin-Williams commented 5 years ago

After enabling mixed mode debugging I got this extra info ..

D3D11 CORRUPTION: ID3D11DeviceContext::ClearRenderTargetView: Two threads were found to be executing functions associated with the same Device[Context] at the same time. This will cause corruption of memory. Appropriate thread synchronization needs to occur external to the Direct3D API (or through the ID3D10Multithread interface). 18800 and 17524 are the implicated thread ids. [ MISCELLANEOUS CORRUPTION #28: CORRUPTED_MULTITHREADING] Exception thrown at 0x7702DDC2 (KernelBase.dll) in AppCore.exe: 0x0000087D (parameters: 0x00000000, 0x0F97DEB0, 0x0F97D2E8).

So I guess I've got my answer.