Code Monkey home page Code Monkey logo

wpfdxinterop's Introduction

WPF DirectX Extensions

WPF DirectX Extensions allow you to easily host DirectX 10 and DirectX 11 content in WPF applications.

Getting Started

Where to get it

Resources

More Info

Code Example

XAML

<Window x:Class="WpfApplication5.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:WpfApplication5"
        mc:Ignorable="d"
        xmlns:DXExtensions="clr-namespace:Microsoft.Wpf.Interop.DirectX;assembly=Microsoft.Wpf.Interop.DirectX"
        Title="MainWindow" Height="350" Width="525">
    <Grid>
        <Image>
            <Image.Source>
                <DXExtensions:D3D11Image  x:Name="InteropImage"/>
            </Image.Source>
        </Image>
    </Grid>
</Window>

C#

The C# portions of interfacing with a native component that generates the DX visualization is not concise enough to host as an example. We would recommend that you look at sample code here to get a detailed understanding of the code required

Using WPF DirectX Extensions

The documentation explains how to install Visual Studio, add the WPF DirectX Extension NuGet package to your project, and get started using the API.

Building WPF DirectX Extensions from Source

What You Need

Build and Create WPF DirectX Extensions NuGet

  • Clone the Repository
  • Open Microsoft.Wpf.Interop.DirectX_winsdk or Microsoft.Wpf.Interop.DirectX_dxsdk solution from Source in Visual Studio
  • Change Build Configuration to Release and build for x86 and x64
  • Run BuildNuGetPackage in scripts to create nuget packages

wpfdxinterop's People

Contributors

microsoft-github-policy-service[bot] avatar msftgits avatar rrelyea avatar singhashish-wpf avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

wpfdxinterop's Issues

Combining D3D11Image with SharpDX

Hi, I want to combine D3D11Image with SharpDX. I think it should be possible but I can not figure out how to properly implement OnRender handler.
In Render event handler I receive IntPtr. Can you please describe what this argument represents and how should I use it to create render target view?

this is my implementation of Render event handler.

void DoRender(IntPtr pointer)
{
var dxgiResource = SharpDX.DXGI.Resource.FromPointer<SharpDX.DXGI.Resource>(pointer);
var dx10Resource = Device.OpenSharedResource<SharpDX.Direct3D10.Resource>(dxgiResource.SharedHandle);
var dx10Texture = dx10Resource.QueryInterface<SharpDX.Direct3D10.Texture2D>();
var renderTarget = new SharpDX.Direct3D10.RenderTargetView(Device, dx10Texture);

    }

Is there anything obviously wrong? I tried to mimic implementation from D3DVisualization.cpp

Thank you
Petr

While Windows is locked, DoRender constantly making new surfaces

Any time I lock my computer and come back a few minutes later my application is frozen with an OutOfMemoryException. Nearly every call to DoRender(surface,isNewSurface) has a new surface and isNewSurface = true. I might watch for windows being locked and unsubscribe to CompositionTarget_Rendering, or just not call InteropImage.RequestRender while locked, but I'm hoping there might be a better solution for this.

Stereoscopic 3D WPF apps possible?

Will the new DirectX 11 capabilities of D3DImage enable us to create stereoscopic 3D WPF apps?

If yes, will there be samples available to look at?

We are interested in 3D images i.e. swapping a stereopair of images at 120Hz.

[originally posted as a comment to WPF Team blog announcement post: http://blogs.msdn.com/b/wpf/archive/2015/10/29/wpf-in-net-4-6-1.aspx by LKeene]

Sample not working on Remote Desktop

When I ran the sample (using winsdk build) using a remote desktop connection (client Win8.1, server Win8.1), the cube is not shown. Also the magnifier is lagging from the cursor very much.

image

WPFDXInterop with DirectX 12 (it throws exception on m_d3dDevice11on12->OpenSharedResource call

## I can use DirectX 12 to write to a D3D11Image on a number of Windows 10 machines using a D3D11On12CreateDevice dx11 device.

{
..
IUnknown pUnk = (IUnknown)pResource;
IDXGIResource * pDXGIResource;
ID3D11Resource * pDx11Resource;
HANDLE sharedHandle;
ID3D11Resource * tempResource11;
ID3D11Texture2D * pOutputResource;
D3D11_RENDER_TARGET_VIEW_DESC rtDesc;
rtDesc.Format = DXGI_FORMAT_B8G8R8A8_UNORM;
rtDesc.ViewDimension = D3D11_RTV_DIMENSION_TEXTURE2D;
rtDesc.Texture2D.MipSlice = 0;

pUnk->QueryInterface(__uuidof(IDXGIResource), (void**)&pDXGIResource);
pUnk->QueryInterface(__uuidof(ID3D11Resource), (void**)&pDx11Resource);
pDXGIResource->GetSharedHandle(&sharedHandle)))return hr;
m_d3dDevice11on12->OpenSharedResource(sharedHandle, __uuidof(ID3D11Resource), (void**)(&tempResource11));
tempResource11->QueryInterface(__uuidof(ID3D11Texture2D), (void**)(&pOutputResource));
m_d3dDevice11on12->CreateRenderTargetView(pOutputResource, &rtDesc, &m_pRenderTargetView1);

pDXGIResource->Release();
tempResource11->Release();
...
}

## However as soon as I enable debugging

ComPtr debugController;
D3D12GetDebugInterface(IID_PPV_ARGS(debugController.GetAddressOf());
debugController->EnableDebugLayer();

I get the following error (exception thrown)

D3D12 ERROR: ID3D12CompatibilityDevice::ReflectSharedProperties: Resource provided was not shared by D3D11, or with a D3D11 desc. [ MISCELLANEOUS ERROR #916: REFLECTSHAREDPROPERTIES_INVALIDOBJECT]

Anyone have any idea what I am doing wrong?

Sample is sometimes freezing.

When I run the sample, it works fine.
But sometimes the cube is freezing, I mean it doesn't rotate for a few seconds and rotate again.

You can refer to the following link.

https://youtu.be/RjIjBBgknyw

You can see the issue at playing time 1:17.

My system information is
Graphic Card : Nvidia P2000
CPU : Intel Core i7-4770K
Mem : 16GB

D3DImage possible issues

Hi,
As far as I understand, in d3dImage rendering occurs only when OnRender arrives, but what if I have rendering in another thread in while loop and I want to keep rendering without UI thread, but I just need IntPtr to tell swapchain where to render?

Could I render to D3DImage independent from OnRender?
Also, I see that on size changed IntPtr of the surface is changed, which will lead to recreation of the whole swapchain instead of simple swapchain.Resize(). I think this is caused by the fact that WPF internally is still working on DirectX 9 and DirectX 9 has no way to resize swapchain, but DirectX 10 and higher - does.

From the sample I see that you recreate rendertarget each DoRender() call, which is extremely not efficient in highe performance rendering scenarios. Rendertargets should be recreated only when one of the swapchain parameters changed. Other time DX should reuse existing one.

Now I can render in the separate thread to the WinForm surface without any problems with any FPS my PC can reach, but I have only one issue - well known airspace problem. D3DImage could relieve me from it, but it creates a lot of other issues.

All this issues, obviously not that I expect when creating request on visualstudio.uservoice.com for Native DX 11 support for WPF. The goal was high performance immediate mode rendering, but here we still have the same 60 fps.

And my question is: could this be fixed or this will leave forever?

Surface Not Automatically Validated When Display is Closed and Reopened

I have pulled the code from your repository, and everything renders correctly in DirectX. The cube is also displayed properly. However, when I close the display for a few seconds and then reopen it, the application stops rendering anything. Simply resizing the application window restores normal rendering. Would you be able to kindly investigate and fix this issue?
Normal status:
image
worng status:
image

can't get this to work with DepthStencilBuffer

I got D3D11Image working with SharpDX, following some of the advice in the other thread and digging through the source code, but it only works with a single RenderTarget (the RenderTargetView that's wrapping the shard handle from D3D11Image). When I try to create a DepthStencilView and call OutputMerger.SetTargets(DepthStencilView, RenderTargetView) my image just winds up being blank. As I am working with 3d, I need depth, or else the layering in the image resorts to whatever was last drawn. Please advise. Thanks and have a great day.

Get rid of shared surface of DX 9

I see that now you create a shared surface handle to render from 11 dx to 9 (in which WPF is hosted), but shared surfaces has limitations such as only A8R8G8B8 format instaead of A10R10G10B10 or even A16R16G16B16. So, to fix this you need to get rid of sharing surface and create native DX 11 surface.

Do we really need Synchronization Helper inside D3D11Image ?

Looking at the source code, I think you have implemented the synchronization helper as described here: https://msdn.microsoft.com/en-us/library/windows/desktop/ee913554(v=vs.85).aspx#interoperability_between_direct3d_9ex_and_dxgi_based_apis

You created one Dx9 device (Producer on A side, Consumer on B side) and one Dx10 device (Producer on B side, Consumer on A side) inside the helper class. However these two devices do not do any rendering, and are not exposed to the external world. Thus synchronizing between the two seems not necessary to me.

I think we only need to create a shareable Dx10/Dx11 surface, set it as backbuffer to the base class D3DImage, then expose the surface to user. Then user needs to flush/sync their device after rendering to the surface, before adding DirtRect to the D3DImage.

FYI, In my use case, I need to render 4~10 D3DImages in one refresh. The mentioned synchronization helper code consumed a lot of UI thread's time and reduced my frame rate.

I can't find this package in the NuGet package Manager

Today, I use this package , I open the NuGet Package Manager and type Microsoft.Wpf.Interop.DirectX. I find I can't search this package, But the NuGet package Manager give me another package: TqkLibrary.Wpf.Interop.DirectX, Why? The Microsoft.Wpf.Interop.DirectX has been discarded?

Screen flickering on lower end system. Where does SwapChain.Present happen?

Before I started using WPFDXInterop image I would end my rendering with a call to SwapChain.Present, but it seems like that's not needed with this, and if I do call it, my image flickers like crazy, even on my development system. I'm thinking maybe there's somewhere I should be locking on, but I've tried locking outside of the call to InteropImage.RequestRender, which is where I assume the swap happens, and it hasn't fixed the problem. Any input would be appreciated. Thank you.

How to render yuv data?

In D3DVisualization_dxsdk.vcxproj, I edit D3DVisualization.cpp,

HRESULT CCube::Render(void * pResource, bool isNewSurface, unsigned char * yuvData) // add yuvData parameter
{
...
m_pImmediateDeviceContext->UpdateSubresource(pBackBuffer, 0, NULL, yuvData, 1920, 0);
if (NULL != m_pImmediateDeviceContext)
{
m_pImmediateDeviceContext->Flush();
}
}

The display is error, could you help me ?

The source code is D3D11Image.zip

Thank you very much!

Unlisted requirement: Visual C++ 2013 Runtime (x86/x64)

I discovered that the current NuGet package (v0.9.0-beta-22856) depends on the Microsoft Visual C++ 2013 Runtime being installed (architecture to match the .NET runtime, obviously). It would be nice if this prerequisite was either baked in or listed somewhere.

Non-default adapter support.

I cannot use D3DImage with a non-default D3D11 display adapter. When attempting to do so, creating the shared handle for the surface fails. Am I missing something? Is this not supported, or is there something extra I need to do?

Multisampling support

Hello,
can I somehow force D3D11Image to create its internal Texture2D with my sample description?

D3D12 support advise

I'm planning to add support for D3D12 support. Do you have any piece of advise to provide ?
Regards

DX11 object leaks

In CCube::InitDevice, I OR-ed in D3D11_CREATE_DEVICE_DEBUG so that I could tell the DirectX Control Panel to monitor the application. There are some object leaks. The CCube destructor needs to safe-release m_pConstantBuffer and m_pRenderTargetView. Also, when the window is resized, InitRenderTarget creates another render target view and reuses m_pRenderTargetView. The previous target view needs to be released. I added a safe-release of m_pRenderTargetView just before the CreateRenderTargetView function call.

RequestRender not firing the DoRender callback

When I use the D3D11Image control inside a Window directly, everything works fine. Calling the RequestRender method causes the DoRender callback to fire. However, if I use the D3D11Image control in a UserControl instead of a Window (the UserControl is in the Window), calling the RequestRender method will not cause the DoRender callback to fire.

Aside from moving to using a UserControl, all the code is the same between the two cases. Has anyone run into this before or gotten it to work from within a UserControl? I thought maybe the window handle for the D3D11Image was wrong but I verified it is correct.

Cannot use "D3D_DRIVER_TYPE_WARP" drive with "D3D_FEATURE_LEVEL_11_0"

When initializing d3d device with "D3D_DRIVER_TYPE_WARP" and "D3D_FEATURE_LEVEL_11_0", the sample D3DVisualization program stops at " hr = m_pd3dDevice->OpenSharedResource(sharedHandle, __uuidof(ID3D11Resource), (void**)(&tempResource11));" at the method "InitRenderTarget(void * pResource)" in class CCube.

Anybody knows why?

Flicker on rendering large datasets

I changed the example code with the rotating cube into a set of routines for fast GPU-bound rendering of polygonal paths with the stencil method. It worked out quite well but now, with big datasets and especially when a certain proportion of rendered foreground/path pixels is surpassed (so the image must also be big enough), flickering occurs.

From another issue posted here it was proposed to solve this by introducing locking. Now my questions are:

Where is this locking code to be placed? In the C# code? Around the render code in my C++ module?

It seems to me that whereas D3DImage contains "lock" memberfunctions D3D11Image has no such "lock" memberfunctions any more. Is that true? So shall I sandwich the render C++ code with mutexes? Shall I use Windows specific ones for that or can I use such from C++ natively? Or are there even further other kinds of locks to be used here?

Any guidance for solving this flicker problem would be much appreciated!

Component improvement: Microsoft.Wpf.Media.SurfaceQueueInteropHelper::SetPixelSize: can swapchain.Resize() be used instead of recreate on size changed?

I see that on size changed IntPtr of the surface is changed, which will lead to recreation of the whole swapchain instead of simple swapchain.Resize(). I think this is caused by the fact that WPF internally is still working on DirectX 9 and DirectX 9 has no way to resize swapchain, but DirectX 10 and higher - does.

[Originally filed by QuantumDeveloper as part of "D3DImage possible issues" https://github.com//issues/2]

I can not add D3DVisualization.dll to references.

I built D3DVisualization.dll (D3DVisualization_winsdk) without any error (debug and release , 86 and 64bit ), But I can not add it to references.

I got this error :

D3DVisualization.dll could not be added. Please make sure that the file is accessible , and it is a valid assembly or COM component.

Unified platform/architecture nuget

Hi,

It's possible to create a single unified nuget package by using the LoadLibrary trick (name both x86/x64 dll's the same name, ensure all pinvoke is correct and then simply load the correct file manually).
Lots of nugets already use this trick, e.g. System.Data.SQLite.
This would be much easier for users, since most people use AnyCPU.

WPF improvement: Want DirectX hosted in WPF to support higher than 60fps

[Originally filed by QuantumDeveloper as part of "D3DImage possible issues" https://github.com//issues/2]

As far as I understand, in d3dImage rendering occurs only when OnRender arrives, but what if I have rendering in another thread in while loop and I want to keep rendering without UI thread, but I just need IntPtr to tell swapchain where to render?
Could I render to D3DImage independent from OnRender?

Now I can render in the separate thread to the WinForm surface without any problems with any FPS my PC can reach, but I have only one issue - well known airspace problem. D3DImage could relieve me from it, but it creates a lot of other issues.

All this issues, obviously not that I expect when creating request on visualstudio.uservoice.com for Native DX 11 support for WPF. The goal was high performance immediate mode rendering, but here we still have the same 60 fps.

And my question is: could this be fixed or this will leave forever?

Displaying higher bit depth images on specialized higher bit depth monitors

Hi!
I am hoping the developers still check this forum.
I am concerned that the images I am displaying (16 bit grayscale) through WPF will ultimately not show as 16 bit on a display monitor specifically designed to display in 16 bit grayscale.
This is because there is a lot of information talking about how images in WPF are automatically converted to bgra32.

Is there something that can be done for this, perhaps through a greater investment in my understanding of Direct X with WPF?

I spent a great deal of time using this WPFDXInterop code as a guide to read, process, and display images. I use DirectX 11, CUDA, even OpenCV at times and display it in WPF. But if images become compressed into 8 bit channels due to how legacy some parts of WPF are, I need to know.

Thank you!

Cannot execute Release x64 version successfully

I try to compile the sample project(D3D11Image) with visual studio 2015, in Debug-x64 version, the WpfD3D11Interop.exe would be executed successfully but in Release-x64 version, the WpfD3D11Interop.exe always popup an error window
"""
This sample requires:
Manual build of the D3DVisualization project, which requires
installation of Windows 10 SDK or DirectXSDK.
Installation of the DirectX runtime on non-build machines.

Detailed exception message: Unable to load Dll 'D3DVisualization.dll':
The specified module could not be found.(Exception from HRESULT:0x8007007E)
"""
I'm 100% for sure that I've installed
DirectX SDK(https://www.microsoft.com/en-us/download/details.aspx?id=6812),
Windows 10 SDK(https://dev.windows.com/en-us/downloads/windows-10-sdk), and
compiled D3DVisualization.dll successfully.

BTW, I cannot compile .dll via WpfD3D11Interop_winsdk.sln, but it is okay via WpfD3D11Interop_dxsdk.sln. The error msessage is
"""
Cannot open include file: 'windows.h': No such file or directory
"""
It seems that there is no $(WindowsSDK_IncludePath) macro in D3DVisualization_winsdk.

Is there any library/dll should be installed I missed?
I think that it is weird the Debug can be executed but Release cannnot.

I've tried two pc.
OS:Windows 7 Embedded Standard SP1
CPU: i3-3220

OS:Windows 7 Professional SP1
CPU: i3-4150

DX - WPF interop in windows 10

I discovered that d3dx11.h couldn't be found in the windows 10 sdk, and that the DX10/11-WPF interop was maybe deprecated. Is there any update possible for WPF-DX12/W10 interop ? In particular something that allows display of DX12 content in a WPF window ?

Best

UCEERR_RENDERTHREADFAILURE when switching presentation modes

The application WpfD3D11Interop crashes when switching the Windows presentation display modes in a multi-monitor setup.

Steps to reproduce the problem (requires multi-monitor setup):

  1. Run WpfD3D11Interop.
  2. Press Win+P and select another presentation display mode (such as "Second screen only"). (Sometimes you have to repeat step 2 a few times.)

Exception:

An unhandled exception of type 'System.Runtime.InteropServices.COMException' occurred in PresentationCore.dll

Additional information: UCEERR_RENDERTHREADFAILURE (Exception from HRESULT: 0x88980406)

Stack trace:

   at System.Windows.Media.Composition.DUCE.Channel.SyncFlush()
   at System.Windows.Interop.HwndTarget.UpdateWindowSettings(Boolean enableRenderTarget, Nullable`1 channelSet)
   at System.Windows.Interop.HwndTarget.UpdateWindowPos(IntPtr lParam)
   at System.Windows.Interop.HwndTarget.HandleMessage(WindowMessage msg, IntPtr wparam, IntPtr lparam)
   at System.Windows.Interop.HwndSource.HwndTargetFilterMessage(IntPtr hwnd, Int32 msg, IntPtr wParam, IntPtr lParam, Boolean& handled)
   at MS.Win32.HwndWrapper.WndProc(IntPtr hwnd, Int32 msg, IntPtr wParam, IntPtr lParam, Boolean& handled)
   at MS.Win32.HwndSubclass.DispatcherCallbackOperation(Object o)
   at System.Windows.Threading.ExceptionWrapper.InternalRealCall(Delegate callback, Object args, Int32 numArgs)
   at System.Windows.Threading.ExceptionWrapper.TryCatchWhen(Object source, Delegate callback, Object args, Int32 numArgs, Delegate catchHandler)
   at System.Windows.Threading.Dispatcher.LegacyInvokeImpl(DispatcherPriority priority, TimeSpan timeout, Delegate method, Object args, Int32 numArgs)
   at MS.Win32.HwndSubclass.SubclassWndProc(IntPtr hwnd, Int32 msg, IntPtr wParam, IntPtr lParam)

The Call Stack window in Visual Studio tells me that the application hangs in
PresentationCore.dll!System.Windows.Interop.D3DImage.Lock().

isNewSurface is insufficient to detect screensaver/lock/Ctrl-Alt-Delete losses

The OnRender callback's Boolean/isNewSurface flag isn't set as true after the desktop is locked / Ctrl-Alt-Delete pressed / screensaver / etc., so while rendering is happening, the visible image isn't being updated. I'm not a DX expert, so I'm not sure what goes on under the hood, but I did notice that the device's NativePointer changes in these circumstances.

To detect these situations, my code now notices that the device's NativePointer changes:
if (isNewSurface || device.NativePointer != _currentDevice) {
_currentDevice = device.NativePointer;
...set up as new...
{

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.