原文链接

 /*
* Copyright 徐洪志(西北农林科技大学.信息工程学院). All rights reserved.
* Data: 2012-4-20
*/
//
// 此程序是演示了1D和2D纹理存储器的使用
#include <stdio.h>
#include <cutil_inline.h>
#include <iostream>
using namespace std; texture<float> texRef1D; // 1D texture
texture<float, > texRef2D; // 2D texture // 1D 纹理操作函数
__global__ void Texture1D(float *dst, int w, int h)
{
int x = threadIdx.x + blockIdx.x * blockDim.x;
int y = threadIdx.y + blockIdx.y * blockDim.y;
int offset = x + y * blockDim.x * gridDim.x; if(x<w && y<h)
dst[offset] = tex1Dfetch(texRef1D, offset);
}
// 2D 纹理操作函数
__global__ void Texture2D(float *dst, int w, int h)
{
int x = threadIdx.x + blockIdx.x * blockDim.x;
int y = threadIdx.y + blockIdx.y * blockDim.y;
int offset = x + y * blockDim.x * gridDim.x; dst[offset] = tex2D(texRef2D, x, y);
}
int main(int argc, char **argv)
{
CUT_DEVICE_INIT(argc, argv); // 启动 CUDA
/// 1D 纹理内存
cout << "1D texture" << endl;
float *host1D = (float*)calloc(, sizeof(float)); // 内存原数据
float *hostRet1D = (float*)calloc(, sizeof(float));// 内存保存返回数据 float *dev1D, *devRet1D; // 显存数据
int i;
cout << " host1D:" << endl;
for(i = ; i < ; ++i) // 初始化内存原数据
{
host1D[i] = i * ;
cout << " " << host1D[i] << " ";
}
cutilSafeCall( cudaMalloc((void**)&dev1D, sizeof(float)*)); // 申请显存空间
cutilSafeCall( cudaMalloc((void**)&devRet1D, sizeof(float)*));
cutilSafeCall( cudaMemcpy(dev1D, host1D, sizeof(float)*, cudaMemcpyHostToDevice)); // 将内存数据拷贝入显存
cutilSafeCall( cudaBindTexture(NULL, texRef1D, dev1D, sizeof(float)*)); // 将显存数据和纹理绑定 Texture1D<<<, >>>(devRet1D, , ); // 运行1D纹理操作函数 cutilSafeCall( cudaMemcpy(hostRet1D, devRet1D, sizeof(float)*, cudaMemcpyDeviceToHost)); // 将显存数据拷贝入内存
// 打印内存数据
cout << endl << " hostRet1D:" << endl;
for(i = ; i < ; ++i)
cout << " " << hostRet1D[i] << " "; cutilSafeCall( cudaUnbindTexture(texRef1D)); // 解绑定
cutilSafeCall( cudaFree(dev1D)); // 释放显存空间
cutilSafeCall( cudaFree(devRet1D));
free(host1D); // 释放内存空间
free(hostRet1D); /// 2D 纹理内存
cout << endl << "2D texture" << endl;
int width = , height = ;
float *host2D = (float*)calloc(width*height, sizeof(float)); // 内存原数据
float *hostRet2D = (float*)calloc(width*height, sizeof(float)); // 内存返回数据 cudaArray *cuArray; // CUDA数组
float *devRet2D; // 显存数据
int row, col;
cout << " host2D:" << endl;
for(row = ; row < height; ++row) // 初始化内存原数据
{
for(col = ; col < width; ++col)
{
host2D[row*width + col] = row + col;
cout << " " << host2D[row*width + col] << " ";
}
cout << endl;
}
cudaChannelFormatDesc channelDesc = cudaCreateChannelDesc<float>();
cutilSafeCall( cudaMallocArray(&cuArray, &channelDesc, width, height)); // 申请显存空间
cutilSafeCall( cudaMalloc((void**) &devRet2D, sizeof(float)*width*height));
cutilSafeCall( cudaBindTextureToArray(texRef2D, cuArray)); // 将显存数据和纹理绑定
cutilSafeCall( cudaMemcpyToArray(cuArray, , , host2D, sizeof(float)*width*height, cudaMemcpyHostToDevice)); // 将内存数据拷贝入CUDA数组 dim3 threads(width, height);
Texture2D<<<, threads>>>(devRet2D, width, height); // 运行2D纹理操作函数 cutilSafeCall( cudaMemcpy(hostRet2D, devRet2D, sizeof(float)*width*height, cudaMemcpyDeviceToHost)); // 将显存数据拷贝入内存
// 打印内存数据
cout << " hostRet2D:" << endl;
for(row = ; row < height; ++row)
{
for(col = ; col < width; ++col)
cout << " " << hostRet2D[row*width + col] << " ";
cout << endl;
} cutilSafeCall( cudaUnbindTexture(texRef2D)); // 解绑定
cutilSafeCall( cudaFreeArray(cuArray)); // 释放显存空间
cutilSafeCall( cudaFree(devRet2D));
free(host2D); // 释放内存空间
free(hostRet2D); CUT_EXIT(argc, argv); // 退出CUDA
}

CUDA Texture纹理存储器 示例程序的更多相关文章

  1. CUDA C 纹理提取Texture Fetching

    CUDA C 纹理提取Texture Fetching 一.参数曲面的纹理  使用纹理指定参数曲面属性. 二.CUDA C 纹理获取开发 用于计算纹理函数,根据纹理引用的各种属性返回的值的公式(请参见 ...

  2. OSG中的示例程序简介

    OSG中的示例程序简介 转自:http://www.cnblogs.com/indif/archive/2011/05/13/2045136.html 1.example_osganimate一)演示 ...

  3. OSG中的示例程序简介(转)

    OSG中的示例程序简介 1.example_osganimate一)演示了路径动画的使用 (AnimationPath.AnimationPathCallback),路径动画回调可以作用在Camera ...

  4. VS2012下基于Glut 矩阵变换示例程序2:

    在VS2012下基于Glut 矩阵变换示例程序:中我们在绘制甜圈或者圆柱时使用矩阵对相应的坐标进行变换后自己绘制甜圈或者圆柱.我们也可以使用glLoadMatrixf.glLoadMatrixd载入变 ...

  5. 通过Jexus 部署 dotnetcore版本MusicStore 示例程序

    ASPNET Music Store application 是一个展示最新的.NET 平台(包括.NET Core/Mono等)上使用MVC 和Entity Framework的示例程序,本文将展示 ...

  6. .NET跨平台:在Ubuntu上用自己编译的dnx运行ASP.NET 5示例程序

    在 Linux Ubuntu 上成功编译 dnx 之后,会在 artifacts/build/ 文件夹中生成 dnx-coreclr-linux-x64/ 与 dnx-mono/ 这2个文件夹,前者是 ...

  7. .NET跨平台:在CentOS上编译dnx并运行ASP.NET 5示例程序

    在之前的博文中我们在 Ubuntu 上成功编译出了 dnx ,并且用它成功运行了 ASP.NET 5 示例程序.在这篇博文中我们将 Ubuntu 换成 CentOS. 目前 dnx 的编译需要用到 m ...

  8. Salesforce Apex 使用JSON数据的示例程序

    本文介绍了一个在Salesforce Apex中使用JSON数据的示例程序, 该示例程序由以下几部分组成: 1) Album.cls, 定了了封装相关字段的数据Model类 2) RestClient ...

  9. D3D三层Texture纹理经像素着色器实现渲染YUV420P

    简单记录一下这两天用Texture实现渲染YUV420P的一些要点. 在视频播放的过程中,有的时候解码出来的数据是YUV420P的.表面(surface)通过设置参数是可以渲染YUV420P的,但Te ...

随机推荐

  1. 解决WebStorm/PyCharm/IDEA卡顿的问题

    问题 webstorm强大的功能就不多做介绍了.但是它的缺点也显而易见:吃内存. 电脑配置稍低一点,运行webstorm就特别容易卡顿,特别是项目比较大的时候,那卡顿得不要不要的. 在我的PC机32g ...

  2. OneDrive撸5T硬盘空间教程

    注意:要注册多个账户获取网盘的,用无痕模式打开临时教育邮箱网址.打开之后不要关闭,等会用来接收验证码. 1.需要office 365注册这时候需要教育邮箱: 临时教育邮箱:http://sysu.ed ...

  3. pat1033. To Fill or Not to Fill (25)

    1033. To Fill or Not to Fill (25) 时间限制 10 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 ZHANG, Gu ...

  4. HDU——Cover——————【技巧】

    Cover Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total Submi ...

  5. executenonquery只对insert,delete,update有效,查询select会默认返回-1

    问题:cmd.ExecuteNonQuery() 方法总是返回-1 原因:ExecuteNonQuery() 方法 select 返回-1 解释:执行Select子句,数据库并无变化,自然返回-1同样 ...

  6. Hibernate课程 初探一对多映射3-2 单向多对一的配置

    1 多方实体类中加入,一方类和getset方法 //多方定义一个一方的引用 private Grade grade; public Grade getGrade() { return grade; } ...

  7. MongoDB之mongodb.cnf配置

    # mongodb3.2.1 的主配置文件,将此文件放置于 mongodb3.2.1/bin 目录下 # hapday 2016-01-27-16:55 start # 数据文件存放目录 dbpath ...

  8. mint-ui popup自动关闭

    <template> <div class="hello"> <input type="text" v-model="n ...

  9. PHP underlying structure

    http://www.phpinternalsbook.com/classes_objects/magic_interfaces_comparable.html

  10. 常见O/R框架介绍

    1.hibernate(JPA的一个实现,同时也有自己的特色)2.toplink3.jdo4.ibatis 4.JPA a)意愿统一天下