技术开发 频道

CUDA 3.0 编程接口

  Direct3D 11 版本:

ID3D11Device* device;
struct CUSTOMVERTEX {
FLOAT x, y, z;
DWORD color;
};
ID3D11Buffer
* positionsVB;
struct cudaGraphicsResource* positionsVB_CUDA;

int main() {
// Get a CUDA-enabled adapter
IDXGIFactory* factory;
CreateDXGIFactory(__uuidof(IDXGIFactory), (
void**)&factory);
IDXGIAdapter
* adapter = 0;
for (unsigned int i = 0; !adapter; ++i) {
if (FAILED(factory->EnumAdapters(i, &adapter))
break;
int dev;
if (cudaD3D11GetDevice(&dev, adapter) == cudaSuccess)
break;
adapter
->Release();
}
factory
->Release();
// Create swap chain and device

sFnPtr_D3D11CreateDeviceAndSwapChain(adapter, D3D11_DRIVER_TYPE_HARDWARE,
0, D3D11_CREATE_DEVICE_DEBUG, featureLevels, 3, D3D11_SDK_VERSION, &swapChainDesc, &swapChain, &device, &featureLevel, &deviceContext);
adapter
->Release();
// Register device with CUDA
cudaD3D11SetDirect3DDevice(device);
// Create vertex buffer and register it with CUDA
unsigned int size = width * height * sizeof(CUSTOMVERTEX);
D3D11_BUFFER_DESC bufferDesc;
bufferDesc.Usage
= D3D11_USAGE_DEFAULT;
bufferDesc.ByteWidth
= size;
bufferDesc.BindFlags
= D3D11_BIND_VERTEX_BUFFER;
bufferDesc.CPUAccessFlags
= 0;
bufferDesc.MiscFlags
= 0;
device
->CreateBuffer(&bufferDesc, 0, &positionsVB);
cudaGraphicsD3D11RegisterResource(
&positionsVB_CUDA,
positionsVB,
cudaGraphicsRegisterFlagsNone);
cudaGraphicsResourceSetMapFlags(positionsVB_CUDA,
// Execute kernel
dim3 dimBlock(16, 16, 1);
dim3 dimGrid(width
/ dimBlock.x, height / dimBlock.y, 1);
createVertices
<<<dimGrid, dimBlock>>>(positions, time, width, height);
// Unmap vertex buffer
cudaGraphicsUnmapResources(1, &positionsVB_CUDA, 0);
// Draw and present
...
}

void releaseVB() {
cudaGraphicsUnregisterResource(positionsVB_CUDA);
positionsVB
->Release();
}
__global__
void createVertices(float4* positions, float time, unsigned int width, unsigned int height){
unsigned
int x = blockIdx.x * blockDim.x + threadIdx.x;
unsigned
int y = blockIdx.y * blockDim.y + threadIdx.y;
// Calculate uv coordinates
float u = x / (float)width;
float v = y / (float)height;
u
= u * 2.0f - 1.0f;
v
= v * 2.0f - 1.0f;
0
相关文章