I'm learning DirectX12 and I'm trying to create a simple application that clears the screen with a solid color, but I'm stuck in Direct3D initialization. I can't create the swap chain and the DXGI factory returns a nullptr. This is my initialization code:
bool Game::InitializeD3D() { ID3D12Debug *debug; D3D12GetDebugInterface(IID_PPV_ARGS(&debug)); debug->EnableDebugLayer(); // hardware result returned by functions HRESULT hr; // create device hr = D3D12CreateDevice(nullptr, D3D_FEATURE_LEVEL_11_0, IID_PPV_ARGS(&mDevice)); if (FAILED(hr)) MessageBox(0, L"device creation failed", L"error", MB_OK); // create fence mDevice->CreateFence(0, D3D12_FENCE_FLAG_NONE, IID_PPV_ARGS(&mFence)); // query descriptor size mRTVDescriptorSize = mDevice->GetDescriptorHandleIncrementSize(D3D12_DESCRIPTOR_HEAP_TYPE_RTV); mDSVDescriptorSize = mDevice->GetDescriptorHandleIncrementSize(D3D12_DESCRIPTOR_HEAP_TYPE_DSV); mCBVSRVDescriptorSize = mDevice->GetDescriptorHandleIncrementSize(D3D12_DESCRIPTOR_HEAP_TYPE_CBV_SRV_UAV); // check for 4X MSAA quality level support D3D12_FEATURE_DATA_MULTISAMPLE_QUALITY_LEVELS MSAAQuality; MSAAQuality.Format = DXGI_FORMAT_R8G8B8A8_UNORM; MSAAQuality.Flags = D3D12_MULTISAMPLE_QUALITY_LEVELS_FLAG_NONE; MSAAQuality.NumQualityLevels = 0; MSAAQuality.SampleCount = 4; mDevice->CheckFeatureSupport(D3D12_FEATURE_MULTISAMPLE_QUALITY_LEVELS, &MSAAQuality, sizeof MSAAQuality); mMSAAQualityLevels = MSAAQuality.NumQualityLevels; // create command queue D3D12_COMMAND_QUEUE_DESC commandQueueDesc = {}; commandQueueDesc.Type = D3D12_COMMAND_LIST_TYPE_DIRECT; commandQueueDesc.Flags = D3D12_COMMAND_QUEUE_FLAG_NONE; hr = mDevice->CreateCommandQueue(&commandQueueDesc, IID_PPV_ARGS(&mCommandQueue)); if (FAILED(hr)) MessageBox(0, L"command queue creation failed", L"Error", MB_OK); // create command allocator mDevice->CreateCommandAllocator(D3D12_COMMAND_LIST_TYPE_DIRECT, IID_PPV_ARGS(&mCommandAllocator)); // create main command list mDevice->CreateCommandList(0, D3D12_COMMAND_LIST_TYPE_DIRECT, mCommandAllocator, nullptr, IID_PPV_ARGS(&mCommandList)); static_cast<ID3D12GraphicsCommandList*>(mCommandList)->Close(); // create the swap chain DXGI_MODE_DESC mode = {}; mode.Width = mWindowWidth; mode.Height = mWindowHeight; mode.Format = mBackBufferFormat; mode.RefreshRate.Numerator = 60; // 60 Hz refresh rate mode.RefreshRate.Denominator = 1; mode.Scaling = DXGI_MODE_SCALING_UNSPECIFIED; mode.ScanlineOrdering = DXGI_MODE_SCANLINE_ORDER_UNSPECIFIED; DXGI_SWAP_CHAIN_DESC swapChainDesc = {}; swapChainDesc.BufferCount = bufferCount; // double buffering swapChainDesc.BufferUsage = DXGI_USAGE_RENDER_TARGET_OUTPUT; swapChainDesc.BufferDesc = mode; swapChainDesc.OutputWindow = mWindow; swapChainDesc.SampleDesc.Count = 4; // 4X MSAA swapChainDesc.SampleDesc.Quality = mMSAAQualityLevels - 1; swapChainDesc.SwapEffect = DXGI_SWAP_EFFECT_FLIP_DISCARD; swapChainDesc.Windowed = true; swapChainDesc.Flags = DXGI_SWAP_CHAIN_FLAG_ALLOW_MODE_SWITCH; hr = CreateDXGIFactory1(IID_PPV_ARGS(&mFactory)); if (FAILED(hr)) MessageBox(0, L"factory creation failed", L"Error", MB_OK); hr = mFactory->CreateSwapChain(mCommandQueue, &swapChainDesc, &mSwapChain); if (FAILED(hr)) MessageBox(0, L"swap chain creation failed", L"Error", MB_OK); // more init code ...... My main window is created and the message box appears saying that the swap chain creation failed and Visual Studio raises an access violation exception. What am I doing wrong?