Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/145.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 无法创建DirectX设备和交换链_C++_Visual Studio_Visual C++ - Fatal编程技术网

C++ 无法创建DirectX设备和交换链

C++ 无法创建DirectX设备和交换链,c++,visual-studio,visual-c++,C++,Visual Studio,Visual C++,从directx检索交换链和设备时遇到问题。代码中有更多信息 void GXDX::StartUp(HWND* mainWindow,int w, int h) { //width and height are members of GXDX width = w; //contains the width height = h; //contains the height this->mainWindow = mainWindow; // Is a h

从directx检索交换链和设备时遇到问题。代码中有更多信息

void GXDX::StartUp(HWND* mainWindow,int w, int h)
{

    //width and height are members of GXDX

    width = w; //contains the width
    height = h; //contains the height

    this->mainWindow = mainWindow; // Is a handle to the main window. it is usually something 
                                   //like : unusual -735313406 

    ID3D10Texture2D *backBufferSurface;

    DXGI_SWAP_CHAIN_DESC swapChainDesc;
    swapChainDesc.BufferCount = 2;
    swapChainDesc.BufferDesc.Format = DXGI_FORMAT_R8G8B8A8_UNORM;
    swapChainDesc.BufferDesc.RefreshRate.Numerator = 60;
    swapChainDesc.BufferDesc.RefreshRate.Denominator = 1;
    swapChainDesc.BufferDesc.Width = width;
    swapChainDesc.BufferDesc.Height = height;
    swapChainDesc.SampleDesc.Count = 1;
    swapChainDesc.SampleDesc.Quality = 0;
    swapChainDesc.OutputWindow = *mainWindow;
    swapChainDesc.Windowed = TRUE;

    D3D10_DRIVER_TYPE driverType = D3D10_DRIVER_TYPE_HARDWARE;

    HRESULT hr = D3D10CreateDeviceAndSwapChain(NULL,driverType,NULL,0,
        D3D10_SDK_VERSION, &swapChainDesc,&swapChain,&dxDevice);

    if(FAILED(hr))//Error is here
        throw GXVideoException(L"Problems retrieving directX device");
}
以下是方法完成后给出的所有值

-       &dxDevice   0x00e74b04  ID3D10Device * *
-           0x00000000  ID3D10Device *
-       IUnknown    {...}   IUnknown
        __vfptr CXX0030: Error: expression cannot be evaluated  
-       &swapChain  0x00e74b08  IDXGISwapChain * *
-           0x00000000  IDXGISwapChain *
-       IDXGIDeviceSubObject    {...}   IDXGIDeviceSubObject
-       IDXGIObject {...}   IDXGIObject
-       IUnknown    {...}   IUnknown
        __vfptr CXX0030: Error: expression cannot be evaluated  
-       &swapChainDesc  0x002df90c {BufferDesc={...} SampleDesc={...} BufferUsage=0xcccccccc ...}   DXGI_SWAP_CHAIN_DESC *
-       BufferDesc  {Width=0x00000320 Height=0x00000258 RefreshRate={...} ...}  DXGI_MODE_DESC
        Width   800 unsigned int
        Height  600 unsigned int
-       RefreshRate {Numerator=60 Denominator=1 }   DXGI_RATIONAL
        Numerator   60  unsigned int
        Denominator 1   unsigned int
        Format  DXGI_FORMAT_R8G8B8A8_UNORM  DXGI_FORMAT
        ScanlineOrdering    -858993460  DXGI_MODE_SCANLINE_ORDER
        Scaling -858993460  DXGI_MODE_SCALING
-       SampleDesc  {Count=0x00000001 Quality=0x00000000 }  DXGI_SAMPLE_DESC
        Count   1   unsigned int
        Quality 0   unsigned int
        BufferUsage 3435973836  unsigned int
        BufferCount 2   unsigned int
-       OutputWindow    0x008b08ca {unused=-665779669 } HWND__ *
        unused  -665779669  int
        Windowed    1   int
        SwapEffect  -858993460  DXGI_SWAP_EFFECT
        Flags   3435973836  unsigned int
        driverType  D3D10_DRIVER_TYPE_HARDWARE  D3D10_DRIVER_TYPE
        hr  0x887a0001  HRESULT
-       this    0x00e74af0 {dxDevice=0x00000000 swapChain=0x00000000 }  GXDX * const
-       GXRenderer  {running=true width=0x00000320 height=0x00000258 ...}   GXRenderer
-       __vfptr 0x013277dc const GXDX::`vftable'    *
        [0] 0x0132110e GXDX::Render(void)   *
        [0x1]   0x013211d6 GXDX::StartUp(struct HWND__ * *,int,int) *
        [0x2]   0x01321041 GXDX::SetupScene(void)   *
        [0x3]   0x01321069 GXDX::DisplayScene(void) *
        running true    bool
        width   0x00000320  int
        height  0x00000258  int
-       mainWindow  0x0132a214 struct HWND__ * GXRenderManager::mainWindow {unused=0x008b08ca } HWND__ *
        unused  0x008b08ca  int
-       dxDevice    0x00000000  ID3D10Device *
+       IUnknown    {...}   IUnknown
-       swapChain   0x00000000  IDXGISwapChain *
-       IDXGIDeviceSubObject    {...}   IDXGIDeviceSubObject
-       IDXGIObject {...}   IDXGIObject
-       IUnknown    {...}   IUnknown
        __vfptr CXX0030: Error: expression cannot be evaluated  
[编辑]

在Goz响应之前,我检查了进一步的调试细节,这就是收到的

DXGI Error: IDXGIFactory::CreateSwapChain: SwapEffect is unknown.

我猜我没有添加swapeffect属性。我将这样做,并检查它。

尝试不通过传递指向HWND的指针。您应该将HWND作为非指针传递。除此之外,我认为没有什么大问题

以下是DXErrorLookup对您的错误的说明:

HRESULT: 0x887a0001 (2289696769)
Name: DXGI_ERROR_INVALID_CALL
Description: The application has made an erroneous API call that it had enough 
information to avoid. This error is intended to denote that the application should be 
altered to avoid the error. Use of the debug version of the DXGI.DLL will provide run-
time debug output with further information.
Severity code: Failed
Facility Code: FACILITY_DXGI (2170)
Error Code: 0x0001 (1)
那么,您是否考虑过使用DXGI的调试版本来查看错误是什么

顺便说一句,我的工作DX10初始化如下(警告大量代码!):

HRESULT hr=S_正常;
//传入了错误的init参数。
如果(pParams->paramSize!=sizeof(D3D10InitParams))
返回false;
//将initparams升级到正确的版本
mInitParams=*(D3D10InitParams*)pParams;
//创建工厂。
IDXGIFactory*pFactory=NULL;
if(失败(CreateDXGIFactory(uu uuidof(IDXGIFactory),(void**)和pFactory)))
{
返回false;
}
if(失败(pFactory->MakeWindowAssociation(mInitParams.hWnd,0)))
{
返回false;
}
HWND-hTemp;
pFactory->GetWindowAssociation(&hTemp);
//枚举适配器。
无符号整数计数=0;
IDXGIAdapter*pAdapter;
std::矢量可变适配器;
而(pFactory->EnumAdapters(计数和pAdapter)!=DXGI\u错误\u未找到)
{
vAdapters。推回(pAdapter);
计数++;
}
unsigned int selectedAdapter=minitgrams.display;
如果(vAdapters.size()>1)
{
//需要处理多个可用适配器。
}
//释放所有其他适配器。
计数=0;
unsigned int max=(unsigned int)vAdapters.size();
同时(计数<最大值)
{
如果(计数!=选择的适配器)
{
vAdapters[count]->Release();
}
计数++;
}
//设备应支持所有基本DX10功能。
//Caps不支持足够的基本功能。
//如果(!复选框(&caps))
//返回false;
//创建D3D10设备。
DXGI_MODE_DESC dxgiModeDesc;
dxgiModeDesc.Width=minitgrams.Width;
dxgiModeDesc.Height=迷你图.Height;
dxgiModeDesc.RefreshRate.Numerator=(mInitParams.RefreshRate==0)?60:迷你图。刷新率;
dxgiModeDesc.RefreshRate.deminator=1;
dxgiModeDesc.Format=DXGI_Format_R8G8B8A8_UNORM;
dxgiModeDesc.ScanlineOrdering=DXGI\u MODE\u SCANLINE\u ORDER\u PROGRESSIVE;
dxgiModeDesc.Scaling=DXGI\u MODE\u Scaling\u CENTERED;
DXGI_SAMPLE_DESC dxgiSampleDesc;
dxgiSampleDesc.Count=1;
dxgiSampleDesc.Quality=0;
//DXGI_用法DXGI用法;
//使用方法。
DXGI_SWAP_CHAIN_DESC dxgiSwapChainDesc;
dxgiSwapChainDesc.BufferDesc=dxgiModeDesc;
dxgiSwapChainDesc.SampleDesc=dxgiSampleDesc;
dxgiSwapChainDesc.BufferUsage=DXGI\u USAGE\u RENDER\u TARGET\u输出;
dxgiSwapChainDesc.BufferCount=2;
dxgiSwapChainDesc.OutputWindow=mInitParams.hWnd;
dxgiSwapChainDesc.Windowed=minitgrams.Windowed;
dxgiSwapChainDesc.SwapEffect=DXGI\u SWAP\u EFFECT\u DISCARD;
dxgiSwapChainDesc.Flags=0//DXGI交换链标志允许模式开关;
//为调试而编译时,将设备设置为调试设备。
#ifdef_调试
unsigned int flags=D3D10\u创建\u设备\u调试;
#否则
无符号整数标志=0;
#恩迪夫
mpAdapter=vAdapters[selectedAdapter];
//创建设备和交换链。
如果(失败(D3D10CreateDeviceAndSwapChain(mpAdapter、D3D10驱动程序类型、硬件、NULL、标志、D3D10\U SDK\U版本、dxgiSwapChainDesc、mpSwapChain和mpDevice)))
{
返回false;
}
//获取后缓冲区。
ID3D10Texture2D*pBuffer=NULL;
if(失败(mpSwapChain->GetBuffer(0,u uuidof(ID3D10Texture2D),(void**)和pBuffer)))
{
返回false;
}
//创建默认的渲染目标视图。
hr=mpDevice->CreateRenderTargetView(pBuffer、NULL和mDefaultRenderTarget);
pBuffer->Release();
如果(失败(小时))
{
返回false;
}
//创建深度模具纹理
D3D10_纹理2D_描述深度;
descDepth.Width=minitpams.Width;
descDepth.Height=minitpams.Height;
descDepth.MipLevels=1;
descDepth.ArraySize=1;
descDepth.Format=DXGI\u Format\u D24\u UNORM\u S8\u UINT;
descDepth.SampleDesc.Count=1;
descDepth.SampleDesc.Quality=0;
descDepth.Usage=D3D10\u Usage\u默认值;
descDepth.BindFlags=D3D10_BIND_DEPTH_模具;
descDepth.CPUAccessFlags=0;
descDepth.miscsflags=0;
if(失败(mpDevice->CreateTexture2D(&descDepth,NULL,&mpDepthStencilTex)))
{
返回false;
}
//创建深度模具视图
D3D10_深度_模板_视图_描述描述DSV;
descDSV.Format=descDepth.Format;
descDSV.ViewDimension=D3D10\u DSV\u DIMENSION\u TEXTURE2D;
descDSV.Texture2D.MipSlice=0;
if(失败(mpDevice->CreateDepthStencilView(mpDepthStencilTex、&descDSV、&mDefaultDepthStencilTarget)))
{
返回false;
}
//设置默认渲染目标。
mpDevice->OMSetRenderTargets(1,&mDefaultRenderTarget,mDefaultDepthStencilTarget);
mpEffectDevice=新的D3D10EffectStateDevice(GetDevice());
//设置默认渲染状态。
SetupRenderStates();
//设置默认视口。
D3D10_视口D3D10视口;
d3d10ViewPort.Width=minitpams.Width;
d3d10ViewPort.Height=迷你图.Height;
d3d10ViewPort.TopLeftX=0;
d3d10ViewPor
    HRESULT hr  = S_OK;

    // Wrong init params passed in.
    if ( pParams->paramSize != sizeof( D3D10InitParams ) )
        return false;

    // Upgrade the initparams to the correct version
    mInitParams = *(D3D10InitParams*)pParams;

    // Create factory.
    IDXGIFactory* pFactory  = NULL;
    if ( FAILED( CreateDXGIFactory( __uuidof( IDXGIFactory ), (void**)&pFactory ) ) )
    {
        return false;
    }

    if ( FAILED( pFactory->MakeWindowAssociation( mInitParams.hWnd, 0 ) ) )
    {
        return false;
    }

    HWND hTemp;
    pFactory->GetWindowAssociation( &hTemp );

    // Enumerate adapters.
    unsigned int count  = 0;
    IDXGIAdapter * pAdapter;
    std::vector<IDXGIAdapter*> vAdapters;
    while( pFactory->EnumAdapters( count, &pAdapter ) != DXGI_ERROR_NOT_FOUND )
    {
        vAdapters.push_back( pAdapter );
        count++;
    }

    unsigned int selectedAdapter    = mInitParams.display;
    if ( vAdapters.size() > 1 )
    {
        // Need to handle multiple available adapters.
    }

    // Release all other adapters.
    count   = 0;
    unsigned int max    = (unsigned int)vAdapters.size();
    while( count < max )
    {
        if ( count != selectedAdapter )
        {
            vAdapters[count]->Release();
        }
        count++;
    }

    // Device should support all basic DX10 features.
    // Caps does not support enough basic features.
    //if ( !CheckCaps( &caps ) )
    //  return false;

    // Create the D3D 10 device.
    DXGI_MODE_DESC dxgiModeDesc;
    dxgiModeDesc.Width                      = mInitParams.width;
    dxgiModeDesc.Height                     = mInitParams.height;
    dxgiModeDesc.RefreshRate.Numerator      = (mInitParams.refreshRate == 0) ? 60 : mInitParams.refreshRate;
    dxgiModeDesc.RefreshRate.Denominator    = 1;
    dxgiModeDesc.Format                     = DXGI_FORMAT_R8G8B8A8_UNORM;
    dxgiModeDesc.ScanlineOrdering           = DXGI_MODE_SCANLINE_ORDER_PROGRESSIVE;
    dxgiModeDesc.Scaling                    = DXGI_MODE_SCALING_CENTERED;

    DXGI_SAMPLE_DESC dxgiSampleDesc;
    dxgiSampleDesc.Count            = 1;
    dxgiSampleDesc.Quality          = 0;

    //DXGI_USAGE dxgiUsage;
    //dxgiUsage.

    DXGI_SWAP_CHAIN_DESC dxgiSwapChainDesc;
    dxgiSwapChainDesc.BufferDesc    = dxgiModeDesc;
    dxgiSwapChainDesc.SampleDesc    = dxgiSampleDesc;
    dxgiSwapChainDesc.BufferUsage   = DXGI_USAGE_RENDER_TARGET_OUTPUT;
    dxgiSwapChainDesc.BufferCount   = 2;
    dxgiSwapChainDesc.OutputWindow  = mInitParams.hWnd;
    dxgiSwapChainDesc.Windowed      = mInitParams.windowed;
    dxgiSwapChainDesc.SwapEffect    = DXGI_SWAP_EFFECT_DISCARD;
    dxgiSwapChainDesc.Flags         = 0;//DXGI_SWAP_CHAIN_FLAG_ALLOW_MODE_SWITCH;

    // Set the device as a debug device when compiled for debug.
#ifdef _DEBUG
    unsigned int flags  = D3D10_CREATE_DEVICE_DEBUG;
#else
    unsigned int flags  = 0;
#endif

    mpAdapter   = vAdapters[selectedAdapter];
    // Create the device and swap chain.
    if ( FAILED( D3D10CreateDeviceAndSwapChain( mpAdapter, D3D10_DRIVER_TYPE_HARDWARE, NULL, flags, D3D10_SDK_VERSION, &dxgiSwapChainDesc, &mpSwapChain, &mpDevice ) ) )
    {
        return false;
    }

    // Get the back buffer.
    ID3D10Texture2D* pBuffer    = NULL;
    if ( FAILED( mpSwapChain->GetBuffer( 0, __uuidof( ID3D10Texture2D ), (void**)&pBuffer ) ) )
    {
        return false;
    }

    // Create the default render target view.
    hr = mpDevice->CreateRenderTargetView( pBuffer, NULL, &mDefaultRenderTarget );
    pBuffer->Release();
    if ( FAILED( hr ) )
    {
        return false;
    }

      // Create depth stencil texture
    D3D10_TEXTURE2D_DESC descDepth;
    descDepth.Width                 = mInitParams.width;
    descDepth.Height                = mInitParams.height;
    descDepth.MipLevels             = 1;
    descDepth.ArraySize             = 1;
    descDepth.Format                = DXGI_FORMAT_D24_UNORM_S8_UINT;
    descDepth.SampleDesc.Count      = 1;
    descDepth.SampleDesc.Quality    = 0;
    descDepth.Usage                 = D3D10_USAGE_DEFAULT;
    descDepth.BindFlags             = D3D10_BIND_DEPTH_STENCIL;
    descDepth.CPUAccessFlags        = 0;
    descDepth.MiscFlags             = 0;
    if ( FAILED( mpDevice->CreateTexture2D( &descDepth, NULL, &mpDepthStencilTex ) ) )
    {
        return false;
    }

    // Create the depth stencil view
    D3D10_DEPTH_STENCIL_VIEW_DESC descDSV;
    descDSV.Format              = descDepth.Format;
    descDSV.ViewDimension       = D3D10_DSV_DIMENSION_TEXTURE2D;
    descDSV.Texture2D.MipSlice  = 0;
    if ( FAILED( mpDevice->CreateDepthStencilView( mpDepthStencilTex, &descDSV, &mDefaultDepthStencilTarget ) ) )
    {
        return false;
    }

    // Set the default render targets.
    mpDevice->OMSetRenderTargets( 1, &mDefaultRenderTarget, mDefaultDepthStencilTarget );

    mpEffectDevice  = new D3D10EffectStateDevice( GetDevice() );

    // Set the default render states.
    SetupRenderStates();

    // Set the default viewport.
    D3D10_VIEWPORT d3d10ViewPort;
    d3d10ViewPort.Width     = mInitParams.width;
    d3d10ViewPort.Height    = mInitParams.height;
    d3d10ViewPort.TopLeftX  = 0;
    d3d10ViewPort.TopLeftY  = 0;
    d3d10ViewPort.MinDepth  = 0.0f;
    d3d10ViewPort.MaxDepth  = 1.0f;

    GetDevice()->RSSetViewports( 1, &d3d10ViewPort );
SecureZeroMemory(&swapChainDesc, sizeof(swapChainDesc));
DXGI_SWAP_CHAIN_DESC swapChainDesc;
swapChainDesc.BufferDesc.Width = width;
swapChainDesc.BufferDesc.Height = height;
swapChainDesc.BufferDesc.Format = DXGI_FORMAT_R8G8B8A8_UNORM;
swapChainDesc.BufferDesc.RefreshRate.Numerator = 60;
swapChainDesc.BufferDesc.RefreshRate.Denominator = 1;

swapChainDesc.SampleDesc.Count = 1;
swapChainDesc.SampleDesc.Quality = 0;

swapChainDesc.BufferUsage = DXGI_USAGE_RENDER_TARGET_OUTPUT;
swapChainDesc.BufferCount = 1;
swapChainDesc.OutputWindow = hWnd;
swapChainDesc.Windowed = true;
swapChainDesc.SwapEffect = DXGI_SWAP_EFFECT_DISCARD;

swapChainDesc.Flags = 0; 

ID3D11Device *pDevice = 0;
ID3D11DeviceContext *pContext = 0;
IDXGISwapChain *pSwapChain = 0;
HRESULT result;

result = D3D11CreateDeviceAndSwapChain(NULL, driverType, NULL, D3D11_CREATE_DEVICE_SINGLETHREADED, featureLevel, totalFeatureLevel, D3D11_SDK_VERSION, &swapChainDesc, &pSwapChain, &pDevice, &currentFeatureLevel, &pContext);