30.04.2014 Views

NVIDIA CUDA 计算统一设备架构

NVIDIA CUDA 计算统一设备架构

NVIDIA CUDA 计算统一设备架构

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

创<br />

填<br />

程 指 南 , 版<br />

上 上<br />

将<br />

资<br />

设<br />

用<br />

为<br />

4.5.3.11 Direct3D 互 操 作 性<br />

Direct3D 互<br />

LPDIRECT3DVERTEXBUFFER9 buffer;<br />

cuD3D9RegisterResource(buffer, CU_D3D9_REGISTER_FLAGS_NONE);<br />

LPDIRECT3DSURFACE9 surface;<br />

cuD3D9RegisterResource(surface, CU_D3D9_REGISTER_FLAGS_NONE);<br />

cuD3D9RegisterResource()<br />

cuD3D9UnregisterVertexBuffer() 可 可<br />

<strong>CUDA</strong> 性 要 求 在 创 建 作 操<br />

Direct3D 时 指 定 文 下<br />

cuD3D9CtxCreate() 通 过 使 用 。 备<br />

cuCtxCreate() 非 而<br />

建 <strong>CUDA</strong><br />

下 文 即 可 实 现 此 目 标 。。<br />

cuD3D9UnmapResources()<br />

有 较 高 的 开 销 , 通 常 仅 为 每 个 资 源 调 用 一 次 。 使 取 消 注 册 。 之 和 任 意 多 次 地 映 射 和 解 除 映 射 。 内 核 可 使 用 具 能<br />

cuD3D9ResourceGetMappedPointer()<br />

cuD3D9ResourceGetMappedSize()、cuD3D9ResourceGetMappedPitch()<br />

cuD3D9ResourceGetMappedPitchSlice() 返 访 及 返<br />

cuD3D9RegisterResource() 即 可 使 用 后 随<br />

Direct3D<br />

注 册 到 <strong>CUDA</strong>: 源<br />

0<br />

CUdeviceptr devPtr;<br />

cuD3D9ResourceGetMappedPointer(&devPtr, buffer);<br />

size_t size;<br />

cuD3D9ResourceGetMappedSize(&size, buffer);<br />

cuMemset(devPtr, 0, size);<br />

用<br />

height) 的<br />

设 备 存 储 器 地 址 和 回 的 大 小 和 间 距 信 息 来 读 取 和 写 入 已 映 射 的 资 源 。 通 问 已 映 射 的 资 源 将 导 致 不 确 定 的 结 果 。 回<br />

面 的 代 码 示 例 使 充 了 一 个 缓 冲 区 : 下<br />

<strong>CUDA</strong> 源 注 册 到 资 将<br />

cuD3D9MapResources() 即 可 在 需 要 时 分 别 使 用 , 后<br />

// host code<br />

代 码 示 例 中 , 每 个 线 程 都 访 问 大 小 的 面 下 在<br />

CUdeviceptr devPtr;<br />

cuD3D9ResourceGetMappedPointer(&devPtr, surface);<br />

size_t pitch;<br />

cuD3D9ResourceGetMappedPitch(&pitch, surface);<br />

cuModuleGetFunction(&cuFunction, cuModule, “myKernel”);<br />

cuFuncSetBlockShape(cuFunction, 16, 16, 1);<br />

int offset = 0;<br />

cuParamSeti(cuFunction, offset, devPtr);<br />

offset += sizeof(devPtr);<br />

cuParamSeti(cuFunction, 0, width);<br />

offset += sizeof(width);<br />

cuParamSeti(cuFunction, 0, height);<br />

offset += sizeof(height);<br />

cuParamSeti(cuFunction, 0, pitch);<br />

offset += sizeof(pitch);<br />

cuParamSetSize(cuFunction, offset);<br />

cuLaunchGrid(cuFunction,<br />

(width+Db.x–1)/Db.x, (height+Db.y–1)/Db.y);<br />

// device code<br />

__global__ void myKernel(unsigned char* surface,<br />

int width, int height, size_t pitch)<br />

{<br />

int x = blockIdx.x * blockDim.x + threadIdx.x;<br />

int y = blockIdx.y * blockDim.y + threadIdx.y;<br />

if (x >= width || y >= height) return;<br />

float* pixel = (float*)(surface + y * pitch) + 4 * x;<br />

}<br />

二 维 表 面 的 一 个 像 素 , 像 素 格 式<br />

过 Direct3D<br />

为 (width,<br />

float4:<br />

<strong>CUDA</strong> 编<br />

35<br />

本 2.0

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!