# Direct3D interoperability Error in cuD3D9CtxCreate(). Error description :CUDA\_ERROR\_UNKNOWN

**URL:** <https://forums.developer.nvidia.com/t/direct3d-interoperability-error-in-cud3d9ctxcreate-error-description-cuda-error-unknown/10014>\
**Category:** CUDA Programming and Performance\
**Created:** [June 9, 2009, 2:49pm UTC](https://forums.developer.nvidia.com/t/direct3d-interoperability-error-in-cud3d9ctxcreate-error-description-cuda-error-unknown/10014 "2009-06-09T14:49:26Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![ashuspeed](https://developer.download.nvidia.com/images/forums/profile-default-devtalk-84.png) [@ashuspeed](https://forums.developer.nvidia.com/u/ashuspeed)\
**Post date:** [June 9, 2009, 2:49pm UTC](https://forums.developer.nvidia.com/t/direct3d-interoperability-error-in-cud3d9ctxcreate-error-description-cuda-error-unknown/10014/1 "2009-06-09T14:49:26Z")

</div>

Hi, i am new to CUDA development and i am facing a problem in my code using Driver-API for D3D interoperability.

The Error is **CUDA\_ERROR\_UNKNOWN** (Runtime Error) while using the function **cuD3D9CtxCreate** (CUcontext \*pCtx, CUdevice \* pCuDevice, unsigned int flags,IDirect3DDevice9 \*pDxDevice);

Platform used : **Windows XP 64 bit**

Graphics Library used is : **Microsoft DirectX SDK (November 2008)**

CUDA version : **CUDA 2.2**

The values of flags used is =0.

ALL other functions like cuD3D9GetDevice(&dev, adapterId.DeviceName);

are returning CUDA\_SUCCESS.

a small part of my code is :-

int main()

{

```
CUresult resu;

CUdevice dev;

//////////////////////////////////////////////////////////////////////////

// Create a window

//////////////////////////////////////////////////////////////////////////

```

WNDCLASSEX winClass = {0};

```
MSG uMsg;

memset(&uMsg,0,sizeof(uMsg));

winClass.lpszClassName = "MY_WINDOWS_CLASS";

winClass.cbSize = sizeof(WNDCLASSEX);

winClass.style = CS_HREDRAW | CS_VREDRAW;

winClass.lpfnWndProc = WindowProc;

winClass.hIcon = NULL;

winClass.hIconSm = NULL;

winClass.hCursor = LoadCursor(NULL, IDC_ARROW);

winClass.hbrBackground = (HBRUSH)GetStockObject(BLACK_BRUSH);

winClass.lpszMenuName = NULL;

winClass.cbClsExtra = 0;

winClass.cbWndExtra = 0;

```

if( !RegisterClassEx(&winClass) )

```
    return E_FAIL;

```

HWND hWnd = CreateWindowEx( NULL, “MY\_WINDOWS\_CLASS”,

```
                         "D3D Interoperability ",

                         WS_OVERLAPPEDWINDOW | WS_VISIBLE,

                         0, 0, _fractalSize, _fractalSize, NULL, NULL, NULL, NULL );

```

if(hWnd == NULL)

{

```
std::cout<<"Failed to create a window here";

return(1);

```

}

```
ShowWindow(hWnd, SW_SHOWNORMAL );

UpdateWindow(hWnd );

```

resu = cuInit(0);

```
if(resu != CUDA_SUCCESS)

{

    std::cout<<"Error in Driver API Initialization \n";

}

```

D3D = Direct3DCreate9(D3D\_SDK\_VERSION);

// Initialize Direct3D

```
if(D3D == NULL)

std::cout<<"Error: Could not Create D3D device \n";

```

// Get a CUDA capable adapter

unsigned int adapter = 0;

unsigned int count = D3D-\>GetAdapterCount();

for (; adapter \< D3D-\>GetAdapterCount(); adapter++)

{

```
D3DADAPTER_IDENTIFIER9 adapterId;

D3D->GetAdapterIdentifier(adapter, 0, &adapterId);

char *name = adapterId.DeviceName;

resu = cuD3D9GetDevice(&dev, adapterId.DeviceName);

if(resu == CUDA_SUCCESS)

break;

```

}

ZeroMemory(&d3dpp ,sizeof(d3dpp));

```
// Set up the structure used to create the D3DDevice

```

d3dpp.Windowed = TRUE;

```
d3dpp.BackBufferCount = 1;

//d3dpp.hDeviceWindow = hWnd;

d3dpp.SwapEffect = D3DSWAPEFFECT_DISCARD;

d3dpp.BackBufferFormat = D3DFMT_UNKNOWN;

//d3dpp.FullScreen_RefreshRateInHz = 60;

d3dpp.PresentationInterval = D3DPRESENT_INTERVAL_IMMEDIATE;

d3dpp.EnableAutoDepthStencil = FALSE;

d3dpp.BackBufferWidth = g_WindowWidth;

d3dpp.BackBufferHeight = g_WindowHeight;

```

// Create device

result = D3D-\>CreateDevice(adapter, D3DDEVTYPE\_HAL, hWnd, D3DCREATE\_SOFTWARE\_VERTEXPROCESSING, &d3dpp, &device);

if(result != D3D\_OK)

{

```
std::cout<<"Failed to Create Device \n";

    return(1);

```

}

// Initialize driver API

// Create context

CUcontext cuContext;

resu = **cuD3D9CtxCreate(&cuContext, &dev, 0, device);** //Error at this point in the code resu returns **CUDA\_ERROR\_UNKNOWN**

if(resu != CUDA\_SUCCESS)

{

```
std::cout<<"Context not Created \n";

```

}

// Create module from binary file

CUmodule cuModule;

cuModuleLoad(&cuModule, “matrixMul\_kernel.cubin”);

// Get function handle from module

cuModuleGetFunction(&matrixMul\_kernel, cuModule, “matrixMul\_kernel”);

// Create vertex buffer and register it with CUDA

unsigned int size = width \* height \* sizeof(CUSTOMVERTEX);

device-\>CreateVertexBuffer(size, 0, D3DFVF\_XYZ|D3DFVF\_TEX1, D3DPOOL\_DEFAULT, &positionsVB, 0);

cuD3D9RegisterResource(positionsVB, CU\_D3D9\_REGISTER\_FLAGS\_NONE);

cuD3D9ResourceSetMapFlags(positionsVB, CU\_D3D9\_MAPRESOURCE\_FLAGS\_WRITEDISCARD);

// Launch rendering loop

//unsigned int temp =0;

```
while (1)

{

    Render();

    //temp++;

}

```

}

All initializations of Devices have been done correctly.

While Running the program a window is also created with an exception.

Thanks in Advance. :rolleyes:

---

<div class="post-metadata">

**Author:** ![ashuspeed](https://developer.download.nvidia.com/images/forums/profile-default-devtalk-84.png) [@ashuspeed](https://forums.developer.nvidia.com/u/ashuspeed)\
**Post date:** [June 9, 2009, 2:52pm UTC](https://forums.developer.nvidia.com/t/direct3d-interoperability-error-in-cud3d9ctxcreate-error-description-cuda-error-unknown/10014/2 "2009-06-09T14:52:24Z")

</div>

Sorry for the format of code attatched earlier.

> [@](#):
>
> Hi, i am new to CUDA development and i am facing a problem in my code using Driver-API for D3D interoperability.
> 
> The Error is **CUDA\_ERROR\_UNKNOWN** (Runtime Error) while using the function **cuD3D9CtxCreate** (CUcontext \*pCtx, CUdevice \* pCuDevice, unsigned int flags,IDirect3DDevice9 \*pDxDevice);
> 
> Platform used : **Windows XP 64 bit**
> 
> Graphics Library used is : **Microsoft DirectX SDK (November 2008)**
> 
> CUDA version : **CUDA 2.2**
> 
> The values of flags used is =0.
> 
> ALL other functions like cuD3D9GetDevice(&dev, adapterId.DeviceName);
> 
> are returning CUDA\_SUCCESS.
> 
> a small part of my code is :-
> 
> int main()
> 
> {
> 
> ```
> CUresult resu;
> 
> CUdevice dev;
> 
> //////////////////////////////////////////////////////////////////////////
> 
> // Create a window
> 
> //////////////////////////////////////////////////////////////////////////
> 
> ```
> 
> WNDCLASSEX winClass = {0};
> 
> ```
> MSG uMsg;
> 
> memset(&uMsg,0,sizeof(uMsg));
> 
> winClass.lpszClassName = "MY_WINDOWS_CLASS";
> 
> winClass.cbSize = sizeof(WNDCLASSEX);
> 
> winClass.style = CS_HREDRAW | CS_VREDRAW;
> 
> winClass.lpfnWndProc = WindowProc;
> 
> winClass.hIcon = NULL;
> 
> winClass.hIconSm = NULL;
> 
> winClass.hCursor = LoadCursor(NULL, IDC_ARROW);
> 
> winClass.hbrBackground = (HBRUSH)GetStockObject(BLACK_BRUSH);
> 
> winClass.lpszMenuName = NULL;
> 
> winClass.cbClsExtra = 0;
> 
> winClass.cbWndExtra = 0;
> 
> ```
> 
> if( !RegisterClassEx(&winClass) )
> 
> ```
> return E_FAIL;
> 
> ```
> 
> HWND hWnd = CreateWindowEx( NULL, “MY\_WINDOWS\_CLASS”,
> 
> ```
> "D3D Interoperability ",
> 
> WS_OVERLAPPEDWINDOW | WS_VISIBLE,
> 
> 0, 0, _fractalSize, _fractalSize, NULL, NULL, NULL, NULL );
> 
> ```
> 
> if(hWnd == NULL)
> 
> {
> 
> ```
> std::cout<<"Failed to create a window here";
> 
> return(1);
> 
> ```
> 
> }
> 
> ```
> ShowWindow(hWnd, SW_SHOWNORMAL );
> 
> UpdateWindow(hWnd );
> 
> ```
> 
> resu = cuInit(0);
> 
> ```
> if(resu != CUDA_SUCCESS)
> 
> {
> 
> std::cout<<"Error in Driver API Initialization \n";
> 
> }
> 
> ```
> 
> D3D = Direct3DCreate9(D3D\_SDK\_VERSION);
> 
> // Initialize Direct3D
> 
> ```
> if(D3D == NULL)
> 
> std::cout<<"Error: Could not Create D3D device \n";
> 
> ```
> 
> // Get a CUDA capable adapter
> 
> unsigned int adapter = 0;
> 
> unsigned int count = D3D-\>GetAdapterCount();
> 
> for (; adapter \< D3D-\>GetAdapterCount(); adapter++)
> 
> {
> 
> ```
> D3DADAPTER_IDENTIFIER9 adapterId;
> 
> D3D->GetAdapterIdentifier(adapter, 0, &adapterId);
> 
> char *name = adapterId.DeviceName;
> 
> resu = cuD3D9GetDevice(&dev, adapterId.DeviceName);
> 
> if(resu == CUDA_SUCCESS)
> 
> break;
> 
> ```
> 
> }
> 
> ZeroMemory(&d3dpp ,sizeof(d3dpp));
> 
> ```
> // Set up the structure used to create the D3DDevice
> 
> ```
> 
> d3dpp.Windowed = TRUE;
> 
> ```
> d3dpp.BackBufferCount = 1;
> 
> //d3dpp.hDeviceWindow = hWnd;
> 
> d3dpp.SwapEffect = D3DSWAPEFFECT_DISCARD;
> 
> d3dpp.BackBufferFormat = D3DFMT_UNKNOWN;
> 
> //d3dpp.FullScreen_RefreshRateInHz = 60;
> 
> d3dpp.PresentationInterval = D3DPRESENT_INTERVAL_IMMEDIATE;
> 
> d3dpp.EnableAutoDepthStencil = FALSE;
> 
> d3dpp.BackBufferWidth = g_WindowWidth;
> 
> d3dpp.BackBufferHeight = g_WindowHeight;
> 
> ```
> 
> // Create device
> 
> result = D3D-\>CreateDevice(adapter, D3DDEVTYPE\_HAL, hWnd, D3DCREATE\_SOFTWARE\_VERTEXPROCESSING, &d3dpp, &device);
> 
> if(result != D3D\_OK)
> 
> {
> 
> ```
> std::cout<<"Failed to Create Device \n";
> 
> return(1);
> 
> ```
> 
> }
> 
> // Initialize driver API
> 
> // Create context
> 
> CUcontext cuContext;
> 
> resu = **cuD3D9CtxCreate(&cuContext, &dev, 0, device);** //Error at this point in the code resu returns **CUDA\_ERROR\_UNKNOWN**
> 
> if(resu != CUDA\_SUCCESS)
> 
> {
> 
> ```
> std::cout<<"Context not Created \n";
> 
> ```
> 
> }
> 
> // Create module from binary file
> 
> CUmodule cuModule;
> 
> cuModuleLoad(&cuModule, “matrixMul\_kernel.cubin”);
> 
> // Get function handle from module
> 
> cuModuleGetFunction(&matrixMul\_kernel, cuModule, “matrixMul\_kernel”);
> 
> // Create vertex buffer and register it with CUDA
> 
> unsigned int size = width \* height \* sizeof(CUSTOMVERTEX);
> 
> device-\>CreateVertexBuffer(size, 0, D3DFVF\_XYZ|D3DFVF\_TEX1, D3DPOOL\_DEFAULT, &positionsVB, 0);
> 
> cuD3D9RegisterResource(positionsVB, CU\_D3D9\_REGISTER\_FLAGS\_NONE);
> 
> cuD3D9ResourceSetMapFlags(positionsVB, CU\_D3D9\_MAPRESOURCE\_FLAGS\_WRITEDISCARD);
> 
> // Launch rendering loop
> 
> //unsigned int temp =0;
> 
> ```
> while (1)
> 
> {
> 
> Render();
> 
> //temp++;
> 
> }
> 
> ```
> 
> }
> 
> All initializations of Devices have been done correctly.
> 
> While Running the program a window is also created with an exception.
> 
> Thanks in Advance. :rolleyes:

---

<div class="post-metadata">

**Author:** ![ashuspeed](https://developer.download.nvidia.com/images/forums/profile-default-devtalk-84.png) [@ashuspeed](https://forums.developer.nvidia.com/u/ashuspeed)\
**Post date:** [June 11, 2009, 9:40am UTC](https://forums.developer.nvidia.com/t/direct3d-interoperability-error-in-cud3d9ctxcreate-error-description-cuda-error-unknown/10014/3 "2009-06-11T09:40:30Z")

</div>

> [@](#):
>
> Sorry for the format of code attatched earlier.

I have solved this problem by changing the D3d vertex processing mode from software to hardware.

result = D3D-\>CreateDevice(adapter, D3DDEVTYPE\_HAL, hWnd, **D3DCREATE\_HARDWARE\_VERTEXPROCESSING** , &d3dpp, &device);

[External Media](http://hqnveipbwb20/public/style_emoticons/<#EMO_DIR#>/wave.gif "Media hosted on another site. Click to open in a new tab.")
