2012-09-28 24 views
1

我的D3D11CreateDeviceAndSwapChain()有问题。我以为我在prev线程中得到了一个解决方案,所以我已经将它标记为已解决。 [Create Swap Chain Failed创建交换链失败2.0

貌似我骗了自己,当我不小心返回HRESULT为bool ...

我一直在争取这个问题&整天还没有计算出来。这里有大量关于输入输出&调试信息...

1]以建议使用未知与非空vAdapter: Debug Pic http://content.wuala.com/contents/RandomClown/Public/RandomCrap/Debug%201.png

2]继离开DX样本它空&使用类型的硬件: Debug Pic http://content.wuala.com/contents/RandomClown/Public/RandomCrap/Debug%202.png

的图片可能足以有人发现了这个问题,但如果是别的东西,代码:

012从该代码
//  This is some relevant stuff [anything referenced] in the class. 

Graphics(){ 
    selectedVAdapter=NULL; 
    deviceInterface=NULL; 
    deviceContext=NULL; 
    swapChain=NULL; 
} 

bool initDevice(HWND &hWnd){ 
    HRESULT success=S_OK; 

    D3D_FEATURE_LEVEL featureLevels[]={ 
     D3D_FEATURE_LEVEL_11_0, 
     D3D_FEATURE_LEVEL_10_1, 
     D3D_FEATURE_LEVEL_10_0, 
     D3D_FEATURE_LEVEL_9_3, 
    }; 
    uint featuresSize=ARRAYSIZE(featureLevels); 

    D3D_DRIVER_TYPE driverTypes[]={ 
     D3D_DRIVER_TYPE_UNKNOWN, // Needed for manual vid adapter setting 
     D3D_DRIVER_TYPE_HARDWARE, 
     D3D_DRIVER_TYPE_WARP, 
     D3D_DRIVER_TYPE_REFERENCE, 
    }; 
    uint driversSize=ARRAYSIZE(driverTypes); 

    refreshVideoAdapters(); 
    setVideoAdapter(); 

    //setSampleQuality(); 

    DXGI_SWAP_CHAIN_DESC sd; 
    ZeroMemory(&sd, sizeof(sd)); 
    sd.BufferCount = settings.bufferCount; 
    sd.BufferDesc.Width = settings.width; 
    sd.BufferDesc.Height = settings.height; 
    sd.BufferDesc.Format = settings.colorDepth; 
    sd.BufferDesc.RefreshRate.Numerator = settings.rateNumerator; 
    sd.BufferDesc.RefreshRate.Denominator = settings.rateDenominator; 
    sd.BufferUsage = DXGI_USAGE_RENDER_TARGET_OUTPUT; 
    sd.OutputWindow = hWnd; 
    sd.SampleDesc.Count = settings.sampleCount; 
    sd.SampleDesc.Quality = settings.sampleQuality; 
    sd.Windowed = !settings.fullScreen; 

    uint flag=0; 
    #ifdef _DEBUG 
     flag|=D3D11_CREATE_DEVICE_DEBUG; 
    #endif 

    for(uint i=0; i<driversSize; i++){ // SwapChain: http://msdn.microsoft.com/en-us/library/ff476083%28v=vs.85%29.aspx 
     D3D_DRIVER_TYPE driver=driverTypes[i]; 
     success=D3D11CreateDeviceAndSwapChain(
      //NULL, 
      selectedVAdapter, driver, NULL, flag, 
      featureLevels, featuresSize, D3D11_SDK_VERSION, &sd, 
      &swapChain, &deviceInterface, &selectedFeatureLevel, &deviceContext); 
     if(SUCCEEDED(success)) break; 
    } 

    return SUCCEEDED(success); 
} 

//  Methods to manage video adapters 
void refreshVideoAdapters(){ 
    IDXGIAdapter1* pAdapter; 
    IDXGIFactory1* pFactory=NULL; 

    uint lastID=0; 
    if(selectedVAdapter){ 
     DXGI_ADAPTER_DESC1* desc=NULL; 
     selectedVAdapter->GetDesc1(desc); 
     lastID=desc->DeviceId; 
     releaseVideoAdapters(); 
    } 

    if(FAILED(CreateDXGIFactory1(__uuidof(IDXGIFactory1), (void**)&pFactory))) return; 

    for(uint i=0; pFactory->EnumAdapters1(i, &pAdapter)!=DXGI_ERROR_NOT_FOUND; i++){ 
     vAdapters.push_back(pAdapter); 

     if(lastID){ 
      DXGI_ADAPTER_DESC1* desc=NULL; 
      pAdapter->GetDesc1(desc); 
      if(lastID==desc->DeviceId){ 
       selectedVAdapter=pAdapter; 
       lastID=0; 
      } 
     } 
    } 

    if(pFactory) pFactory->Release(); 
} 
void releaseVideoAdapters(){ 
    for(uint i=0; i<vAdapters.size(); i++){ 
     vAdapters[i]->Release(); 
     vAdapters[i]=NULL; 
    } 
    vAdapters.clear(); 
    selectedVAdapter=NULL; 
} 
IDXGIAdapter1* getVideoAdapter(){return selectedVAdapter;} 
bool setVideoAdapter(uint num=0){ 
    if(num<vAdapters.size()){ 
     selectedVAdapter=vAdapters[num]; 
     return 1; 
    } 
    return 0; 
} 

// Member vars 
private: 
SettingsGraphicsDevice settings; 

D3D_FEATURE_LEVEL selectedFeatureLevel; 

vector<IDXGIAdapter1*> vAdapters; 
IDXGIAdapter1* selectedVAdapter; 

ID3D11Device* deviceInterface; 
ID3D11DeviceContext* deviceContext; 
IDXGISwapChain* swapChain; 

设置结构:

struct SettingsGraphicsDevice{ 
    uint width, height; 
    bool fullScreen, vsync; 
    uint rateNumerator; 
    uint rateDenominator; 
    uint bufferCount; 
    uint sampleCount, sampleQuality; 
    DXGI_FORMAT colorDepth; 
    float minDist, maxDist; 

    SettingsGraphicsDevice(){ 
     width=height=0; 
     fullScreen=0; 
     vsync=0; 

     rateNumerator=0; 
     rateDenominator=1; 
     bufferCount=1; 
     sampleCount=1, sampleQuality=0; 
     colorDepth=DXGI_FORMAT_R8G8B8A8_UINT; 

     minDist=0.1f; 
     maxDist=1000.0f; 
    } 

}; 

感谢阅读。希望这次能找到解决方案。

+0

很抱歉听到。您忽略了MSDN中某处提供的另一条建议:当您使用全屏时,您应该创建一个窗口化交换链并在之后将其设置为全屏。无论哪种方式,因为你没有做全屏:你是否尝试将刷新率值保持为0?此外,快速浏览我的代码表明我使用DXGI_FORMAT_R8G8B8A8_UNORM,但我目前不知道这是否是正确的模式。正在进行中:-) –

+0

是的,我处于窗口模式,并且我的刷新率为0.我将尝试该颜色模式。 – RandomClown

回答

1

从我的评论复制和粘贴:“另外,快速浏览我的代码表明我使用DXGI_FORMAT_R8G8B8A8_UNORM,但我目前还不知道这是否是正确的模式。”好吧,现在是在回答:-)

是的,格式... DXGI_FORMAT是一个相当大的枚举,但在许多情况下只允许某些格式。无论是全屏还是非全屏,只允许显示某些格式,这应该不会太令人惊讶。

我从文档中获得了我使用的值(与往常一样),特别是DXGI_MODE_DESC说明中的列表。我实际上并不知道自己是否创建了BLT块转移交换链,但确定这些值与任何开始时一样好,即使我不关心功能级别9,它似乎很安全: - )

我不知道为什么你的代码似乎适用于一个空适配器;我认为这很令人困惑。也许调试运行时会抓住它?

+0

示例显示它使用空适配器进行初始化。我认为它默认为第一个适配器。另外,说“适配器=视频卡”是否正确? – RandomClown

0

切换到该颜色模式[DXGI_FORMAT_R8G8B8A8_UNORM]工作。我现在得到S_OK。为什么其他颜色模式存在,如果他们不工作?

+1

DXGI格式取决于资源(纹理/缓冲区)的使用情况,因此并不是所有这些格式都取决于您想要对它们执行的操作,请查看http://msdn.microsoft.com/en-us/library/windows/desktop /ff476498(v=vs.85).aspx格式支持 – catflier

+0

有趣。谢谢,这是很好的知道。 – RandomClown