• Advertisement
Sign in to follow this  

[FIXED] C++ DirectX rendering of a C# Canvas

Recommended Posts

Hey folks,

So as the title suggests I'm trying to handle the rendering of a C# Panel instance (in this case a Canvas) using C++ and DirectX - but I'm having trouble creating the device and swap chain instance on the C++ side of things.

The C# application has 'Canvas' instance in the XAML (I've also tried a Windows.Forms panel, as shown below):

<Grid Grid.Row="0">
    <!-- <WindowsFormsHost Width="800" Height="600" x:Name="myEngineWindow">
        <wf:Panel Width="800" Height="600" x:Name="myDXControl"/>
    </WindowsFormsHost> -->
    <Canvas Width="800" Height="600" x:Name="myDXControl" />
</Grid>

And in my application's 'Loaded' event callback I grad the window handle from the Canvas and send it through to my C++ renderer (SoundSynthesisInterface):

HwndSource hwndSource = (HwndSource)HwndSource.FromVisual(myDXControl);
SoundSynthesisInterface.Instance.InitializeEngine(hwndSource.Handle, (int)myDXControl.Width, (int)myDXControl.Height); 

The SoundSynthesisInterface is a managed C++ class, and converts the IntPtr supplied by C# to a HWND instance, and then uses that window handle to try and initialize the device context and swap chain:

bool SoundSynthesisInterface::InitializeEngine( IntPtr aWindowHandle, int aWindowWidth, int aWindowHeight )
{
  ...
  HWND windowHandle = (HWND)aWindowHandle.ToPointer();
  
  ...

  InitializeRenderContext(windowHandle, aWindowWidth, aWindowHeight);
} 

The HWND instance that gets passed through to InitializeRenderContext is non-null, but when I call the following code to create the device & swap chain:

 // create a structure to hold information about the swap chain
 DXGI_SWAP_CHAIN_DESC swapChainDescription;
 ZeroMemory( &swapChainDescription, sizeof(DXGI_SWAP_CHAIN_DESC) );

 HRESULT result = S_OK;

 // fill in the swap chain structure
 swapChainDescription.BufferCount        = 2;
 swapChainDescription.BufferDesc.Format  = DXGI_FORMAT_R8G8B8A8_UNORM;
 swapChainDescription.BufferUsage        = DXGI_USAGE_RENDER_TARGET_OUTPUT;
 swapChainDescription.OutputWindow       = windowHandle;
 swapChainDescription.SampleDesc.Count   = 8;
 swapChainDescription.SampleDesc.Quality = 1;
 swapChainDescription.Windowed           = TRUE;
 swapChainDescription.Flags              = DXGI_SWAP_CHAIN_FLAG_ALLOW_MODE_SWITCH;

 // create the context
 result = D3D11CreateDeviceAndSwapChain(
     NULL,
     D3D_DRIVER_TYPE_HARDWARE,
     NULL,
     NULL,
     NULL,
     NULL,
     D3D11_SDK_VERSION,
     &swapChainDescription,
     &m_swapChain,
     &m_device,
     NULL,
     &m_deviceContext
 ); 

I get the following error code:

    0x80070057

Which I think means that one of the parameters I'm passing in to CreateDeviceAndSwapChain is invalid... but I'm not sure what I'm doing wrong here. Anyone have any bright ideas? m_swapChain, m_device & m_deviceContext are all initialized to 'nullptr' by the class constructor.

I'm currently running on Windows 7 with VS2015 and I'm linking to the 'Windows Kit v8.1' version of the DXSDK. Both the C# and C++ projects are complied using an x64 configuration, as are all DirectX libs I'm linking to.

Thanks for the help!

Edited by Orangeatang

Share this post


Link to post
Share on other sites
Advertisement

Try the D3D11_CREATE_DEVICE_DEBUG flag. Then you should get a debug message telling you what you did wrong.

Just tried that :) 

Apparently my graphics device doesn't support the SampleDesc.Quality I was supplying.

Thanks!

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this  

  • Advertisement