CreateSwapChain Fails

Hi, I’ve been investigating why nSight doesn’t work for us anymore.

It appears that if I call D3D11CreateDevice and then IDXGIFactory::CreateSwapChain the CreateSwapChain call fails.

However with the same swapchain parameters, if I call D3D11CreateDeviceAndSwapChain, it succeeds !

( We can’t use D3D11CreateDeviceAndSwapChain in our engine, because we might have multiple swap chains for edition tools )

My specs:

GTX Titan
Window 7 64 bit
Visual Studio 2012
nSight 4.5 (64 bit)


Here’s a simple repro, try, with and without the line ‘#define CREATE_DEVICE_AND_SWAP_CHAIN_IN_A_SINGLE_CALL’

#include "stdafx.h"
#include "nsightCrashRepro.h"

#include <d3d11.h>

#define MAX_LOADSTRING 100

// Global Variables:
HINSTANCE hInst;								// current instance
HWND g_hWnd;
TCHAR szTitle[MAX_LOADSTRING];					// The title bar text
TCHAR szWindowClass[MAX_LOADSTRING];			// the main window class name

// Forward declarations of functions included in this code module:
ATOM				MyRegisterClass(HINSTANCE hInstance);
BOOL				InitInstance(HINSTANCE, int);
LRESULT CALLBACK	WndProc(HWND, UINT, WPARAM, LPARAM);
INT_PTR CALLBACK	About(HWND, UINT, WPARAM, LPARAM);

//#define CREATE_DEVICE_AND_SWAP_CHAIN_IN_A_SINGLE_CALL

bool InitD3D()
{
	UINT flags = 0;
	D3D_FEATURE_LEVEL featureLevels[] = {
		D3D_FEATURE_LEVEL_11_0,	
	};

	D3D_FEATURE_LEVEL d3dFeatureLevel;
	ID3D11Device * device = NULL;
	ID3D11DeviceContext * immDeviceContext = NULL;
	IDXGISwapChain* swapChain;

	DXGI_SWAP_CHAIN_DESC d3dsd;
	ZeroMemory( &d3dsd, sizeof( d3dsd ) );
	d3dsd.SampleDesc.Count = 1;
	d3dsd.SampleDesc.Quality = 0;//D3D11_STANDARD_MULTISAMPLE_PATTERN;
	d3dsd.BufferUsage = DXGI_USAGE_BACK_BUFFER | DXGI_USAGE_RENDER_TARGET_OUTPUT;
	d3dsd.BufferCount = 2;
	d3dsd.SwapEffect = DXGI_SWAP_EFFECT_DISCARD;
	d3dsd.Flags = 0;
	d3dsd.OutputWindow = g_hWnd;
	d3dsd.Windowed = TRUE;
	d3dsd.BufferDesc.Width = 512;
	d3dsd.BufferDesc.Height = 512;
	d3dsd.BufferDesc.Format = DXGI_FORMAT_R8G8B8A8_UNORM_SRGB;
	d3dsd.BufferDesc.RefreshRate.Numerator = 60;
	d3dsd.BufferDesc.RefreshRate.Denominator = 1;
	d3dsd.BufferDesc.Scaling = DXGI_MODE_SCALING_STRETCHED;

#ifdef CREATE_DEVICE_AND_SWAP_CHAIN_IN_A_SINGLE_CALL

	HRESULT hr = D3D11CreateDeviceAndSwapChain(	NULL, D3D_DRIVER_TYPE_HARDWARE, NULL,
		flags, NULL, 0, D3D11_SDK_VERSION, &d3dsd, &swapChain, &device, &d3dFeatureLevel, &immDeviceContext );

	if( FAILED( hr ) )
	{
		MessageBox( g_hWnd, _T("D3D11CreateDeviceAndSwapChain failed"), _T("Error"), MB_OK );
		return false;
	}

#else

	HRESULT hr = D3D11CreateDevice( NULL, D3D_DRIVER_TYPE_HARDWARE, NULL, 
		flags, NULL, 0, D3D11_SDK_VERSION, &device, &d3dFeatureLevel, &immDeviceContext );

	if( FAILED( hr ) )
	{
		MessageBox( g_hWnd, _T("D3D11CreateDevice failed"), _T("Error"), MB_OK );
		return false;
	}

	//Create swap chain

	IDXGIDevice * dxgiDevice;
	hr = device->QueryInterface( __uuidof( dxgiDevice ), reinterpret_cast< void** >( &dxgiDevice ) );
	if( FAILED( hr ) ) {
		return false;
	}

	IDXGIAdapter * dxgiAdapter;
	hr = dxgiDevice->GetAdapter( &dxgiAdapter );
	if( FAILED( hr ) ) {
		return false;
	}

	IDXGIFactory * dxgiFactory;
	hr = dxgiAdapter->GetParent( __uuidof( dxgiFactory ), reinterpret_cast< void** >( &dxgiFactory ) );
	if( FAILED( hr ) ) {
		return false;
	}

	hr = dxgiFactory->CreateSwapChain( device, &d3dsd, &swapChain );
	if( FAILED( hr ) ) {
		MessageBox( g_hWnd, _T("CreateSwapChain failed"), _T("Error"), MB_OK );
		return false;
	}

#endif

	MessageBox( g_hWnd, _T("D3D Init successful"), _T("Success"), MB_OK );

	return true;
}


int APIENTRY _tWinMain(_In_ HINSTANCE hInstance,
                     _In_opt_ HINSTANCE hPrevInstance,
                     _In_ LPTSTR    lpCmdLine,
                     _In_ int       nCmdShow)
{
	UNREFERENCED_PARAMETER(hPrevInstance);
	UNREFERENCED_PARAMETER(lpCmdLine);

 	// TODO: Place code here.
	MSG msg;
	HACCEL hAccelTable;

	// Initialize global strings
	LoadString(hInstance, IDS_APP_TITLE, szTitle, MAX_LOADSTRING);
	LoadString(hInstance, IDC_NSIGHTCRASHREPRO, szWindowClass, MAX_LOADSTRING);
	MyRegisterClass(hInstance);

	// Perform application initialization:
	if (!InitInstance (hInstance, nCmdShow))
	{
		return FALSE;
	}

	if( !InitD3D() )
	{
		return FALSE;
	}

	hAccelTable = LoadAccelerators(hInstance, MAKEINTRESOURCE(IDC_NSIGHTCRASHREPRO));

	// Main message loop:
	while (GetMessage(&msg, NULL, 0, 0))
	{
		if (!TranslateAccelerator(msg.hwnd, hAccelTable, &msg))
		{
			TranslateMessage(&msg);
			DispatchMessage(&msg);
		}
	}

	return (int) msg.wParam;
}

ATOM MyRegisterClass(HINSTANCE hInstance)
{
	WNDCLASSEX wcex;

	wcex.cbSize = sizeof(WNDCLASSEX);

	wcex.style			= CS_HREDRAW | CS_VREDRAW;
	wcex.lpfnWndProc	= WndProc;
	wcex.cbClsExtra		= 0;
	wcex.cbWndExtra		= 0;
	wcex.hInstance		= hInstance;
	wcex.hIcon			= LoadIcon(hInstance, MAKEINTRESOURCE(IDI_NSIGHTCRASHREPRO));
	wcex.hCursor		= LoadCursor(NULL, IDC_ARROW);
	wcex.hbrBackground	= (HBRUSH)(COLOR_WINDOW+1);
	wcex.lpszMenuName	= MAKEINTRESOURCE(IDC_NSIGHTCRASHREPRO);
	wcex.lpszClassName	= szWindowClass;
	wcex.hIconSm		= LoadIcon(wcex.hInstance, MAKEINTRESOURCE(IDI_SMALL));

	return RegisterClassEx(&wcex);
}

BOOL InitInstance(HINSTANCE hInstance, int nCmdShow)
{
   hInst = hInstance; // Store instance handle in our global variable
	
   g_hWnd = CreateWindow(szWindowClass, szTitle, WS_OVERLAPPEDWINDOW,
      CW_USEDEFAULT, 0, CW_USEDEFAULT, 0, NULL, NULL, hInstance, NULL);

   if (!g_hWnd)
   {
      return FALSE;
   }

   ShowWindow(g_hWnd, nCmdShow);
   UpdateWindow(g_hWnd);

   return TRUE;
}


LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
	int wmId, wmEvent;
	PAINTSTRUCT ps;
	HDC hdc;

	switch (message)
	{
	case WM_COMMAND:
		wmId    = LOWORD(wParam);
		wmEvent = HIWORD(wParam);
		// Parse the menu selections:
		switch (wmId)
		{
		case IDM_ABOUT:
			break;
		case IDM_EXIT:
			DestroyWindow(hWnd);
			break;
		default:
			return DefWindowProc(hWnd, message, wParam, lParam);
		}
		break;
	case WM_PAINT:
		hdc = BeginPaint(hWnd, &ps);
		// TODO: Add any drawing code here...
		EndPaint(hWnd, &ps);
		break;
	case WM_DESTROY:
		PostQuitMessage(0);
		break;
	default:
		return DefWindowProc(hWnd, message, wParam, lParam);
	}
	return 0;
}

Florent Tournade
Graphics engineer at Arkane Studios

By the way:

  • My driver version is 347.88
  • And I just tested with the latest nSight (4.6.0.15071)

The problem is still there
nsightCrashRepro.zip (11 KB)

Hi FlorentTournade,

We already noticed this issue some time ago, that’s due to a bug of Nsight and we already fixed it. Next update version of Nsight should take this. ;)

Thanks
An

Thanks ! Good news !

Is there an approximate ETA for the next version ?

Cheers

Hi FlorentTournade,

We are working on it right now, but I can’t show you some ETA.

Thanks
An

Hi, guys. Did you come up with a solution for that trouble? Looks like i have something very similar to that stuff. Can’t create swap chain using CreateSwapChain function but CreateDeviceAndSwapChain works ok. Could you give me some pointers?

Hi all,

We just released Nsight 4.7 which should have a fix for the issue described in this thread.

Thanks
An