• Advertisement
Sign in to follow this  

DX11 SharpDX createswapchain failed

This topic is 2296 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi, all<br />I am using sharpdx now, and CreateSwapChainAndDevice success for me but if I only want to create swap chain with existing device, then it's failed, can some one help me?<br /><br /> SharpDX.Direct3D11.Device CreateOnlyDevice()<br />        {<br />            int adapterNo = 0; // TODO: Initialize to an appropriate value<br />            DX11AdapterList adaList = new DX11AdapterList();<br />            Factory1 factory = new Factory1();<br />            adaList.Enumerate(factory);<br />            DX11Adapter adapter = adaList.Adapter(adapterNo);<br />            FeatureLevel[] feature_levels = <br />                {<br />                    //FeatureLevel.Level_11_0,<br />                    FeatureLevel.Level_10_1,<br />                    FeatureLevel.Level_10_0,<br />                    FeatureLevel.Level_9_3<br />                };<br />            SharpDX.Direct3D11.Device actual =<br />                new SharpDX.Direct3D11.Device(adapter.Adapter, DeviceCreationFlags.Debug, feature_levels);<br />            return actual;<br />        }<br /><br />        public SwapChain CreateDeviceAndSwapChain()<br />        {<br />            System.Windows.Forms.Form form = new System.Windows.Forms.Form();<br />            <br />            SharpDX.Direct3D11.Device device = CreateOnlyDevice();<br />            int quality = device.CheckMultisampleQualityLevels(Format.R8G8B8A8_UNorm, 8);<br />            device.Dispose();<br /><br />            DriverType driverType = DriverType.Hardware; // TODO: Initialize to an appropriate value<br />            DeviceCreationFlags flags = DeviceCreationFlags.Debug; // TODO: Initialize to an appropriate value<br />            FeatureLevel[] feature_levels = <br />                {<br />                    FeatureLevel.Level_11_0,<br />                    FeatureLevel.Level_10_1,<br />                    FeatureLevel.Level_10_0,<br />                    FeatureLevel.Level_9_3<br />                };<br />            SwapChainDescription desc = new SwapChainDescription();<br />            desc.BufferCount = 1;<br />            desc.ModeDescription = new ModeDescription(form.ClientSize.Width, form.ClientSize.Height, new Rational(60, 1), Format.R8G8B8A8_UNorm);<br />            desc.ModeDescription.ScanlineOrdering = DisplayModeScanlineOrder.Unspecified;<br />            desc.ModeDescription.Scaling = DisplayModeScaling.Unspecified;<br />            desc.Usage = Usage.RenderTargetOutput;<br />            desc.OutputHandle = form.Handle;<br />            desc.SampleDescription.Count = 8;<br />            desc.SampleDescription.Quality = quality -1;<br />            desc.IsWindowed = true;<br />            desc.SwapEffect = SwapEffect.Discard;<br />            desc.Flags = SwapChainFlags.AllowModeSwitch;<br /><br />            SwapChain swapChain = null; // TODO: Initialize to an appropriate value<br />            DX11RenderFactory.Instance.CreateSwapChain(driverType, flags, feature_levels, desc, out swapChain);<br />            return swapChain;<br />        }<br /><br />I am running my program in .net unit test project<br />Here are the two tests I did using two functions above.<br /><br />  /// <summary><br />        ///A test for CreateSwapChain<br />        ///</summary><br />        [TestMethod()]<br />        public void CreateSwapChainTest()<br />        {<br />            SwapChain tempSwapChain = CreateDeviceAndSwapChain();<br />            try<br />            {<br />                SwapChain swapChain = new SwapChain(DX11RenderFactory.Instance.DxgiFactory, <br />                    DX11RenderFactory.Instance.D3DDevice, tempSwapChain.Description);<br />            }<br />            catch (System.Exception ex)<br />            {<br />                System.Console.Write(ex.Message);<br />            }<br />            Assert.Inconclusive("A method that does not return a value cannot be verified.");<br />        }<br /><br />        /// <summary><br />        ///A test for CreateSwapChain<br />        ///</summary>s<br />        [TestMethod()]<br />        public void CreateSwapChainTest1()<br />        {<br />            SwapChain swapChain = CreateDeviceAndSwapChain();<br />            Assert.AreNotEqual(null, DX11RenderFactory.Instance.D3DDevice);<br />            Assert.AreNotEqual(null, swapChain);<br />            Assert.Inconclusive("A method that does not return a value cannot be verified.");<br />        }<br /><br />and the first test failed, the second test pass.<br />But I checked in the first test, d3ddevice ,dxgiFactory is successfully created, however  <br />SwapChain swapChain = new SwapChain(DX11RenderFactory.Instance.DxgiFactory, <br />                    DX11RenderFactory.Instance.D3DDevice, tempSwapChain.Description);<br />throw a exception {"Unknown error (HRESULT = 0x887A0001)"}<br />

Share this post


Link to post
Share on other sites
Advertisement
Woo boy, this is why I always preview my posts before I submit. You're going to have to reformat that whole post (and code) before people can help you.

Share this post


Link to post
Share on other sites
If you run in debug mode with the option "Enable unmanaged code debugging" in your project, you would have seen this error:

IDXGIFactory::CreateSwapChain: This function is being called with a device from a different IDXGIFactory.

When you create a swap chain from a device, they need to share the same factory but in your code you are creating D3D11Device with one factory (Factory1 factory = new Factory1();) and the swap chain with another (DX11RenderFactory.Instance.DxgiFactory)

If you run this code, It is working with no exceptions:

Factory factory = new Factory();
var adapter = factory.GetAdapter(0);
Device device = new Device(adapter, DeviceCreationFlags.Debug);
SwapChain swapChain = new SwapChain(factory, device, desc);

(And yes, please, preview and reformat your code next time)

Share this post


Link to post
Share on other sites

If you run in debug mode with the option "Enable unmanaged code debugging" in your project, you would have seen this error:

IDXGIFactory::CreateSwapChain: This function is being called with a device from a different IDXGIFactory.

When you create a swap chain from a device, they need to share the same factory but in your code you are creating D3D11Device with one factory (Factory1 factory = new Factory1();) and the swap chain with another (DX11RenderFactory.Instance.DxgiFactory)

If you run this code, It is working with no exceptions:

Factory factory = new Factory();
var adapter = factory.GetAdapter(0);
Device device = new Device(adapter, DeviceCreationFlags.Debug);
SwapChain swapChain = new SwapChain(factory, device, desc);

(And yes, please, preview and reformat your code next time)


Sorry for the layout problem, in the office gamedev is affected by blocking policy, the css is totally wrong, so when i edit my post, everything messed up. I will post at home next time.
Thank u , Alexandre, I figured out the problem, I did use the same Factory, the Factory1 factory = new Factory1(); you saw is a temporary one I created just to check multisampleing quality,nothing else,
but i notice if i use the constructor Device.CreateSwapChainAndDevice(drivertype,....) , every time it will reallocate a new factory for my device and swap chain implicitly,
because i have a singleton which holds Factory1 and D3D Device, so if i want to create a new swapchain using same device
(created using Device.CreateWithSwapChain(driverType, flags, featureLevels, desc,out d3dDevice_, out swapChain);
I have to reset my factory. Here is the code I fixed:
I have 2 remaining questions:
1.do I need to call dispose for dxgiFacotry_, d3dDevice_, dxContext_ before Device.CreateWithSwapChain?? in first function below?
2. original API CreateDeviceAndSwapChain can take Adapter and DriverType together as parameters , in SharpDx, seems constructor can only take either Adapter or DriverType one of them,
so if I use constructor which takes DriverType as parameter to create device and swapchain, is it only bind to the first Adapter? Any reason for these two can not passed in together?

// Create a new device and swapchain with a new DXGI factory1
public SwapChain CreateSwapChainAndDevice(DriverType driverType, DeviceCreationFlags flags,
FeatureLevel[] featureLevels, SwapChainDescription desc)
{
SwapChain swapChain;
Device.CreateWithSwapChain(driverType, flags, featureLevels, desc,out d3dDevice_, out swapChain);
dxgiFacotry_ = swapChain.GetParent<Factory1>();
dxContext_ = d3dDevice_.ImmediateContext;
return swapChain;
}

// in this case, device is already created, create additional swapchain with existed DXGI factory1
public SwapChain CreateSwapChain(SwapChainDescription swdesc)
{
return new SwapChain(dxgiFacotry_, d3dDevice_, swdesc);
}

Share this post


Link to post
Share on other sites

I have 2 remaining questions:
1.do I need to call dispose for dxgiFacotry_, d3dDevice_, dxContext_ before Device.CreateWithSwapChain?? in first function below?

If you are overriding previous values, you should probably dispose previous dxgiFactory and d3dDevice (context is released by device).

Also you should be able to create Device/SwapChain with their respective constructors, using the same DXGI factory/adapter for all calls, without having to use static Device.CreateWithSwapChain method.


2. original API CreateDeviceAndSwapChain can take Adapter and DriverType together as parameters , in SharpDx, seems constructor can only take either Adapter or DriverType one of them,
so if I use constructor which takes DriverType as parameter to create device and swapchain, is it only bind to the first Adapter? Any reason for these two can not passed in together?

If you look at D3D11CreateDeviceAndSwapChain documentation, in remarks, it is stated:
"If you set the pAdapter parameter to a non-NULL value, you must also set the DriverType parameter to the D3D_DRIVER_TYPE_UNKNOWN value. If you set the pAdapter parameter to a non-NULL value and the DriverType parameter to the D3D_DRIVER_TYPE_HARDWARE value, D3D11CreateDeviceAndSwapChain returns an HRESULT of E_INVALIDARG."

That's why SharpDX doesn't expose Adapter and DriverType together, as when you are passing an Adapter, the DriverType mush always be set to Unknown.

Share this post


Link to post
Share on other sites

[quote name='skidrow1982' timestamp='1320493715' post='4880745']
I have 2 remaining questions:
1.do I need to call dispose for dxgiFacotry_, d3dDevice_, dxContext_ before Device.CreateWithSwapChain?? in first function below?

If you are overriding previous values, you should probably dispose previous dxgiFactory and d3dDevice (context is released by device).

Also you should be able to create Device/SwapChain with their respective constructors, using the same DXGI factory/adapter for all calls, without having to use static Device.CreateWithSwapChain method.


2. original API CreateDeviceAndSwapChain can take Adapter and DriverType together as parameters , in SharpDx, seems constructor can only take either Adapter or DriverType one of them,
so if I use constructor which takes DriverType as parameter to create device and swapchain, is it only bind to the first Adapter? Any reason for these two can not passed in together?

If you look at D3D11CreateDeviceAndSwapChain documentation, in remarks, it is stated:
"If you set the pAdapter parameter to a non-NULL value, you must also set the DriverType parameter to the D3D_DRIVER_TYPE_UNKNOWN value. If you set the pAdapter parameter to a non-NULL value and the DriverType parameter to the D3D_DRIVER_TYPE_HARDWARE value, D3D11CreateDeviceAndSwapChain returns an HRESULT of E_INVALIDARG."

That's why SharpDX doesn't expose Adapter and DriverType together, as when you are passing an Adapter, the DriverType mush always be set to Unknown.
[/quote]

thanks again,
I ran into a new problem, the same unit test code passed test on office machine, but when i ran on my home's pc, it failed, I opened debug unmanaged code in debug mode and caught sharpdxexception only said "Unknown error (HRESULT = 0x80004005)" at SharpDX.Result.CheckError()
at SharpDX.Direct3D11.D3D11.CreateDevice(Adapter adapterRef, DriverType driverType, IntPtr software, DeviceCreationFlags flags, FeatureLevel[] featureLevelsRef, Int32 featureLevels, Int32 sDKVersion, Device deviceOut, FeatureLevel& featureLevelRef, DeviceContext& immediateContextOut)
my pc has 2x GTX260 card ,
I attached the unit test failure screen shot and also if i change DeviceCreationFlags in the following line to DeviceCreationFlags.None then it will create device succssfully.
actual = new SharpDX.Direct3D11.Device(adapter.Adapter, DeviceCreationFlags.Debug, feature_levels);
where can be the problem of this?

Share this post


Link to post
Share on other sites

I attached the unit test failure screen shot and also if i change DeviceCreationFlags in the following line to DeviceCreationFlags.None then it will create device succssfully.
actual = new SharpDX.Direct3D11.Device(adapter.Adapter, DeviceCreationFlags.Debug, feature_levels);
where can be the problem of this?

Creating a D3D10/D3D11 device in Debug mode is looking for DLLs (for example, D3D11SDKLayers.dll) that are only installed by the DirectX SDK (They are not part of the redistributable DirectX runtime and thus must be only used in development phase. To solve your problem, you need to install DirectX June 2010 SDK

Share this post


Link to post
Share on other sites

[quote name='skidrow1982' timestamp='1320624440' post='4881205']
I attached the unit test failure screen shot and also if i change DeviceCreationFlags in the following line to DeviceCreationFlags.None then it will create device succssfully.
actual = new SharpDX.Direct3D11.Device(adapter.Adapter, DeviceCreationFlags.Debug, feature_levels);
where can be the problem of this?

Creating a D3D10/D3D11 device in Debug mode is looking for DLLs (for example, D3D11SDKLayers.dll) that are only installed by the DirectX SDK (They are not part of the redistributable DirectX runtime and thus must be only used in development phase. To solve your problem, you need to install DirectX June 2010 SDK
[/quote]

thanks, i got it.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
  • Advertisement
  • Popular Tags

  • Advertisement
  • Popular Now

  • Similar Content

    • By turanszkij
      Hi, right now building my engine in visual studio involves a shader compiling step to build hlsl 5.0 shaders. I have a separate project which only includes shader sources and the compiler is the visual studio integrated fxc compiler. I like this method because on any PC that has visual studio installed, I can just download the solution from GitHub and everything just builds without additional dependencies and using the latest version of the compiler. I also like it because the shaders are included in the solution explorer and easy to browse, and double-click to open (opening files can be really a pain in the ass in visual studio run in admin mode). Also it's nice that VS displays the build output/errors in the output window.
      But now I have the HLSL 6 compiler and want to build hlsl 6 shaders as well (and as I understand I can also compile vulkan compatible shaders with it later). Any idea how to do this nicely? I want only a single project containing shader sources, like it is now, but build them for different targets. I guess adding different building projects would be the way to go that reference the shader source project? But how would they differentiate from shader type of the sources (eg. pixel shader, compute shader,etc.)? Now the shader building project contains for each shader the shader type, how can other building projects reference that?
      Anyone with some experience in this?
    • By osiris_dev
      Hello!
      Have a problem with reflection shader for D3D11:
      1>engine_render_d3d11_system.obj : error LNK2001: unresolved external symbol IID_ID3D11ShaderReflection
      I tried to add this:
      #include <D3D11Shader.h>
      #include <D3Dcompiler.h>
      #include <D3DCompiler.inl>
      #pragma comment(lib, "D3DCompiler.lib")
      //#pragma comment(lib, "D3DCompiler_47.lib")
      As MSDN tells me but still no fortune. I think lot of people did that already, what I missing?
      I also find this article http://mattfife.com/?p=470
      where recommend to use SDK headers and libs before Wind SDK, but I am not using DirectX SDK for this project at all, should I?
    • By trojanfoe
      Hi there, this is my first post in what looks to be a very interesting forum.
      I am using DirectXTK to put together my 2D game engine but would like to use the GPU depth buffer in order to avoid sorting back-to-front on the CPU and I think I also want to use GPU instancing, so can I do that with SpriteBatch or am I looking at implementing my own sprite rendering?
      Thanks in advance!
    • By Matt_Aufderheide
      I am trying to draw a screen-aligned quad with arbitrary sizes.
       
      currently I just send 4 vertices to the vertex shader like so:
      pDevCon->IASetPrimitiveTopology(D3D_PRIMITIVE_TOPOLOGY_TRIANGLESTRIP);
      pDevCon->Draw(4, 0);
       
      then in the vertex shader I am doing this:
      float4 main(uint vI : SV_VERTEXID) : SV_POSITION
      {
       float2 texcoord = float2(vI & 1, vI >> 1);
      return float4((texcoord.x - 0.5f) * 2, -(texcoord.y - 0.5f) * 2, 0, 1);
      }
      that gets me a screen-sized quad...ok .. what's the correct way to get arbitrary sizes?...I have messed around with various numbers, but I think I don't quite get something in these relationships.
      one thing I tried is: 
       
      float4 quad = float4((texcoord.x - (xpos/screensizex)) * (width/screensizex), -(texcoord.y - (ypos/screensizey)) * (height/screensizey), 0, 1);
       
      .. where xpos and ypos is number of pixels from upper right corner..width and height is the desired size of the quad in pixels
      this gets me somewhat close, but not right.. a bit too small..so I'm missing something ..any ideas?
       
      .
    • By Stewie.G
      Hi,
      I've been trying to implement a gaussian blur recently, it would seem the best way to achieve this is by running a bur on one axis, then another blur on the other axis.
      I think I have successfully implemented the blur part per axis, but now I have to blend both calls with a proper BlendState, at least I think this is where my problem is.
      Here are my passes:
      RasterizerState DisableCulling { CullMode = BACK; }; BlendState AdditiveBlend { BlendEnable[0] = TRUE; BlendEnable[1] = TRUE; SrcBlend[0] = SRC_COLOR; BlendOp[0] = ADD; BlendOp[1] = ADD; SrcBlend[1] = SRC_COLOR; }; technique11 BlockTech { pass P0 { SetVertexShader(CompileShader(vs_5_0, VS())); SetGeometryShader(NULL); SetPixelShader(CompileShader(ps_5_0, PS_BlurV())); SetRasterizerState(DisableCulling); SetBlendState(AdditiveBlend, float4(0.0, 0.0, 0.0, 0.0), 0xffffffff); } pass P1 { SetVertexShader(CompileShader(vs_5_0, VS())); SetGeometryShader(NULL); SetPixelShader(CompileShader(ps_5_0, PS_BlurH())); SetRasterizerState(DisableCulling); } }  
      D3DX11_TECHNIQUE_DESC techDesc; mBlockEffect->mTech->GetDesc( &techDesc ); for(UINT p = 0; p < techDesc.Passes; ++p) { deviceContext->IASetVertexBuffers(0, 2, bufferPointers, stride, offset); deviceContext->IASetIndexBuffer(mIB, DXGI_FORMAT_R32_UINT, 0); mBlockEffect->mTech->GetPassByIndex(p)->Apply(0, deviceContext); deviceContext->DrawIndexedInstanced(36, mNumberOfActiveCubes, 0, 0, 0); } No blur

       
      PS_BlurV

      PS_BlurH

      P0 + P1

      As you can see, it does not work at all.
      I think the issue is in my BlendState, but I am not sure.
      I've seen many articles going with the render to texture approach, but I've also seen articles where both shaders were called in succession, and it worked just fine, I'd like to go with that second approach. Unfortunately, the code was in OpenGL where the syntax for running multiple passes is quite different (http://rastergrid.com/blog/2010/09/efficient-gaussian-blur-with-linear-sampling/). So I need some help doing the same in HLSL :-)
       
      Thanks!
  • Advertisement