locked
Rendering Direct3D content to a XAML metro Control

    Question

  • Hi.  I’m trying to render a cube to a XAML control.  I’ve been trying to use the SurfaceImageSource route, as suggested at “DirectX and XAML interop” (http://msdn.microsoft.com/library/windows/apps/hh825871.aspx), and the cube rendering code in the default directx application template generated by Visual Studio.  However, I can’t figure out or find through web searching how to get from the IDXGISurface provided by the ISurfaceImageSourceNative::BeginDraw call to a ID3D11RenderTargetView for use by the ID3D11DeviceContext1 in rendering the 3D content.   The tone of the article suggests that this step is straightforward, but I can’t find it.  Thanks for any help!

    Thursday, March 15, 2012 1:20 AM

Answers

  • Here's pseudocode for one approach that should work:

    // get D3D texture from surface returned by BeginDraw
    ComPtr<ID3D11Texture2D> d3DTexture;
    surface.As(&d3DTexture);
    
    // create render target view
    ComPtr<ID3D11RenderTargetView> renderTargetView;
    myD3DDevice->CreateRenderTargetView(d3DTexture.Get(), nullptr, &renderTargetView);
    
    ...
    
    // clear 
    myD3DContext->ClearRenderTargetView(renderTargetView.Get(), ...)
    
    ...
    
    // set render target
    myD3DContext->OMSetRenderTargets(1, renderTargetView.GetAddressOf(), ...)


    • Marked as answer by osilrfjxvch Tuesday, March 20, 2012 5:30 AM
    Monday, March 19, 2012 5:21 PM

All replies

  • I believe you just render to the device you passed into BeginDraw. The IDXGISurface is provided so you can share it for other interop. For example, if you want to use Direct2D you would pass the IDXGISurface to CreateDxgiSurfaceRenderTarget to create an ID2D1RenderTarget.

    --Rob

    Friday, March 16, 2012 12:32 AM
    Owner
  • Thanks for the response.  It was helpful as it suggests that the problem lies in my direct3d code (which currently is unintentionally generating a transparent image brush rather than 3D content - although, I’m not getting any errors while running).  I’ve successfully adapted the Direct2D magazine sample (from the  Windows 8 Consumer Preview Metro style app samples pack) to set the background color, which suggests to me that I have the SurfaceImageSource framework set up properly.  However, I still can’t get 3D content rendered.  I’m still stuck thinking that it’s because I haven’t properly set the RenderTargetView, which I still don’t know how to do (and the way that it’s created in the normal 3D samples doesn't work).  As you suggested and as presented in the D2D sample, CreateBitmapFromDxgiSurface will create the proper target for rendering D2D content (ID2D1Bitmap1 in the magazine example), but what is the proper call to get the render target for Direct3D?  I’ve tried things like ID3D11DeviceContext::OMGetRenderTargets, but this just returns nullptrs.   Can someone please share a simple, even pseudo code, example that would show how to take the example in “DirectX and XAML interop” (which creates and sets the device to the SurfaceImageSouce) and , say, clear the back buffer/brush to a color?  That would be extremely helpful.  Thanks!

    Friday, March 16, 2012 9:36 PM
  • Here's pseudocode for one approach that should work:

    // get D3D texture from surface returned by BeginDraw
    ComPtr<ID3D11Texture2D> d3DTexture;
    surface.As(&d3DTexture);
    
    // create render target view
    ComPtr<ID3D11RenderTargetView> renderTargetView;
    myD3DDevice->CreateRenderTargetView(d3DTexture.Get(), nullptr, &renderTargetView);
    
    ...
    
    // clear 
    myD3DContext->ClearRenderTargetView(renderTargetView.Get(), ...)
    
    ...
    
    // set render target
    myD3DContext->OMSetRenderTargets(1, renderTargetView.GetAddressOf(), ...)


    • Marked as answer by osilrfjxvch Tuesday, March 20, 2012 5:30 AM
    Monday, March 19, 2012 5:21 PM
  • Aha!  That was just what I was looking for.  Now I have 3D rendering.  Thanks very much for taking the time!

    Tuesday, March 20, 2012 5:30 AM