▶ cpu_bitmap.h

 #ifndef __CPU_BITMAP_H__
#define __CPU_BITMAP_H__ #include "gl_helper.h" struct CPUBitmap
{
unsigned char *pixels;
int x, y;
void *dataBlock;
void (*bitmapExit)(void*); CPUBitmap( int width, int height, void *d = NULL )
{
pixels = new unsigned char[width * height * ];
x = width;
y = height;
dataBlock = d;
} ~CPUBitmap()
{
delete [] pixels;
} unsigned char* get_ptr( void ) const { return pixels; }
long image_size( void ) const { return x * y * ; } void display_and_exit( void(*e)(void*) = NULL )
{
CPUBitmap** bitmap = get_bitmap_ptr();
*bitmap = this;
bitmapExit = e;
// a bug in the Windows GLUT implementation prevents us from
// passing zero arguments to glutInit()
int c=;
char* dummy = "";
glutInit( &c, &dummy );
glutInitDisplayMode( GLUT_SINGLE | GLUT_RGBA );
glutInitWindowSize( x, y );
glutCreateWindow( "bitmap" );
glutKeyboardFunc(Key);
glutDisplayFunc(Draw);
glutMainLoop();
} // static method used for glut callbacks
static CPUBitmap** get_bitmap_ptr( void )
{
static CPUBitmap *gBitmap;
return &gBitmap;
} // static method used for glut callbacks
static void Key(unsigned char key, int x, int y)
{
switch (key)
{
case :
CPUBitmap* bitmap = *(get_bitmap_ptr());
if (bitmap->dataBlock != NULL && bitmap->bitmapExit != NULL)
bitmap->bitmapExit( bitmap->dataBlock );
exit();
}
} // static method used for glut callbacks
static void Draw( void )
{
CPUBitmap* bitmap = *(get_bitmap_ptr());
glClearColor( 0.0, 0.0, 0.0, 1.0 );
glClear( GL_COLOR_BUFFER_BIT );
glDrawPixels( bitmap->x, bitmap->y, GL_RGBA, GL_UNSIGNED_BYTE, bitmap->pixels );
glFlush();
}
}; #endif // __CPU_BITMAP_H__

▶ cpu_anim.h

 #ifndef __CPU_ANIM_H__
#define __CPU_ANIM_H__ #include "D:\Code\CUDA\book\common\gl_helper.h"
#include <iostream> struct CPUAnimBitmap
{
unsigned char *pixels;
int width, height;
void *dataBlock;
void (*fAnim)(void*,int);
void (*animExit)(void*);
void (*clickDrag)(void*,int,int,int,int);
int dragStartX, dragStartY; CPUAnimBitmap( int w, int h, void *d = NULL )
{
width = w;
height = h;
pixels = new unsigned char[width * height * ];
dataBlock = d;
clickDrag = NULL;
} ~CPUAnimBitmap()
{
delete [] pixels;
} unsigned char* get_ptr( void ) const { return pixels; }
long image_size( void ) const { return width * height * ; } void click_drag(void(*f)(void*, int, int, int, int))
{
clickDrag = f;
} void anim_and_exit(void(*f)(void*, int), void(*e)(void*))
{
CPUAnimBitmap** bitmap = get_bitmap_ptr();
*bitmap = this;
fAnim = f;
animExit = e;
// a bug in the Windows GLUT implementation prevents us from
// passing zero arguments to glutInit()
int c=;
char* dummy = "";
glutInit( &c, &dummy );
glutInitDisplayMode( GLUT_DOUBLE | GLUT_RGBA );
glutInitWindowSize( width, height );
glutCreateWindow( "bitmap" );
glutKeyboardFunc(Key);
glutDisplayFunc(Draw);
if (clickDrag != NULL)
glutMouseFunc( mouse_func );
glutIdleFunc( idle_func );
glutMainLoop();
} // static method used for glut callbacks
static CPUAnimBitmap** get_bitmap_ptr(void)
{
static CPUAnimBitmap* gBitmap;
return &gBitmap;
} // static method used for glut callbacks
static void mouse_func(int button, int state, int mx, int my)
{
if (button == GLUT_LEFT_BUTTON)
{
CPUAnimBitmap* bitmap = *(get_bitmap_ptr());
if (state == GLUT_DOWN)
{
bitmap->dragStartX = mx;
bitmap->dragStartY = my;
}
else if (state == GLUT_UP)
bitmap->clickDrag( bitmap->dataBlock,bitmap->dragStartX,bitmap->dragStartY,mx, my );
}
} // static method used for glut callbacks
static void idle_func( void )
{
static int ticks = ;
CPUAnimBitmap* bitmap = *(get_bitmap_ptr());
bitmap->fAnim( bitmap->dataBlock, ticks++ );
glutPostRedisplay();
} // static method used for glut callbacks
static void Key(unsigned char key, int x, int y)
{
switch (key)
{
case :
CPUAnimBitmap* bitmap = *(get_bitmap_ptr());
bitmap->animExit( bitmap->dataBlock );
//delete bitmap;
exit();
}
} // static method used for glut callbacks
static void Draw( void )
{
CPUAnimBitmap* bitmap = *(get_bitmap_ptr());
glClearColor( 0.0, 0.0, 0.0, 1.0 );
glClear( GL_COLOR_BUFFER_BIT );
glDrawPixels( bitmap->width, bitmap->height, GL_RGBA, GL_UNSIGNED_BYTE, bitmap->pixels );
glutSwapBuffers();
}
}; #endif // __CPU_ANIM_H__

▶ gpu_anim.h

 #ifndef __GPU_ANIM_H__
#define __GPU_ANIM_H__
#include "D:\Code\CUDA\book\common\gl_helper.h"
#include "cuda.h"
#include "cuda_gl_interop.h"
#include <iostream> PFNGLBINDBUFFERARBPROC glBindBuffer = NULL;
PFNGLDELETEBUFFERSARBPROC glDeleteBuffers = NULL;
PFNGLGENBUFFERSARBPROC glGenBuffers = NULL;
PFNGLBUFFERDATAARBPROC glBufferData = NULL; struct GPUAnimBitmap
{
GLuint bufferObj;
cudaGraphicsResource *resource;
int width, height;
void *dataBlock;
void (*fAnim)(uchar4*,void*,int);
void (*animExit)(void*);
void (*clickDrag)(void*,int,int,int,int);
int dragStartX, dragStartY; GPUAnimBitmap( int w, int h, void *d = NULL )
{
width = w;
height = h;
dataBlock = d;
clickDrag = NULL; // first, find a CUDA device and set it to graphic interop
cudaDeviceProp prop;
int dev;
memset( &prop, , sizeof( cudaDeviceProp ) );
prop.major = ;
prop.minor = ;
cudaChooseDevice( &dev, &prop ) );
cudaGLSetGLDevice( dev ); // a bug in the Windows GLUT implementation prevents us from
// passing zero arguments to glutInit()
int c=;
char* dummy = "";
glutInit( &c, &dummy );
glutInitDisplayMode( GLUT_DOUBLE | GLUT_RGBA );
glutInitWindowSize( width, height );
glutCreateWindow( "bitmap" ); glBindBuffer = (PFNGLBINDBUFFERARBPROC)GET_PROC_ADDRESS("glBindBuffer");
glDeleteBuffers = (PFNGLDELETEBUFFERSARBPROC)GET_PROC_ADDRESS("glDeleteBuffers");
glGenBuffers = (PFNGLGENBUFFERSARBPROC)GET_PROC_ADDRESS("glGenBuffers");
glBufferData = (PFNGLBUFFERDATAARBPROC)GET_PROC_ADDRESS("glBufferData"); glGenBuffers( , &bufferObj );
glBindBuffer( GL_PIXEL_UNPACK_BUFFER_ARB, bufferObj );
glBufferData(GL_PIXEL_UNPACK_BUFFER_ARB, width * height * , NULL, GL_DYNAMIC_DRAW_ARB); cudaGraphicsGLRegisterBuffer(&resource, bufferObj, cudaGraphicsMapFlagsNone);
} ~GPUAnimBitmap()
{
free_resources();
} void free_resources( void )
{
cudaGraphicsUnregisterResource( resource ) );
glBindBuffer( GL_PIXEL_UNPACK_BUFFER_ARB, );
glDeleteBuffers( , &bufferObj );
} long image_size( void ) const { return width * height * ; } void click_drag(void(*f)(void*, int, int, int, int))
{
clickDrag = f;
} void anim_and_exit(void(*f)(uchar4*, void*, int), void(*e)(void*))
{
GPUAnimBitmap** bitmap = get_bitmap_ptr();
*bitmap = this;
fAnim = f;
animExit = e; glutKeyboardFunc( Key );
glutDisplayFunc( Draw );
if (clickDrag != NULL)
glutMouseFunc( mouse_func );
glutIdleFunc( idle_func );
glutMainLoop();
} // static method used for glut callbacks
static GPUAnimBitmap** get_bitmap_ptr( void )
{
static GPUAnimBitmap* gBitmap;
return &gBitmap;
} // static method used for glut callbacks
static void mouse_func( int button, int state,int mx, int my )
{
if (button == GLUT_LEFT_BUTTON)
{
GPUAnimBitmap* bitmap = *(get_bitmap_ptr());
if (state == GLUT_DOWN)
{
bitmap->dragStartX = mx;
bitmap->dragStartY = my;
}
else if (state == GLUT_UP)
bitmap->clickDrag(bitmap->dataBlock, bitmap->dragStartX, bitmap->dragStartY, mx, my);
}
} // static method used for glut callbacks
static void idle_func( void )
{
static int ticks = ;
GPUAnimBitmap* bitmap = *(get_bitmap_ptr());
uchar4* devPtr;
size_t size; cudaGraphicsMapResources(, &(bitmap->resource), NULL);
cudaGraphicsResourceGetMappedPointer((void**)&devPtr, &size, bitmap->resource); bitmap->fAnim(devPtr, bitmap->dataBlock, ticks++); cudaGraphicsUnmapResources(, &(bitmap->resource), NULL); glutPostRedisplay();
} // static method used for glut callbacks
static void Key(unsigned char key, int x, int y)
{
switch (key)
{
case :
GPUAnimBitmap* bitmap = *(get_bitmap_ptr();
if (bitmap->animExit)
bitmap->animExit( bitmap->dataBlock );
bitmap->free_resources();
exit();
}
} // static method used for glut callbacks
static void Draw( void )
{
GPUAnimBitmap* bitmap = *(get_bitmap_ptr();
glClearColor( 0.0, 0.0, 0.0, 1.0 );
glClear( GL_COLOR_BUFFER_BIT );
glDrawPixels( bitmap->width, bitmap->height, GL_RGBA,
GL_UNSIGNED_BYTE, );
glutSwapBuffers();
}
}; #endif // __GPU_ANIM_H__

▶ gl_helper.h

 #ifndef __GL_HELPER_H__
#define __GL_HELPER_H__ /*
On 64-bit Windows, we need to prevent GLUT from automatically linking against
glut32. We do this by defining GLUT_NO_LIB_PRAGMA. This means that we need to
manually add opengl32.lib and glut64.lib back to the link using pragmas.
Alternatively, this could be done on the compilation/link command-line, but
we chose this so that compilation is identical between 32- and 64-bit Windows.
*/
#ifdef _WIN64
#define GLUT_NO_LIB_PRAGMA
#pragma comment (lib, "opengl32.lib") /* link with Microsoft OpenGL lib */
#pragma comment (lib, "glut64.lib") /* link with Win64 GLUT lib */
#endif //_WIN64 #ifdef _WIN32
/* On Windows, include the local copy of glut.h and glext.h */
#include "GL/glut.h"
#include "GL/glext.h" #define GET_PROC_ADDRESS( str ) wglGetProcAddress( str ) #else /* On Linux, include the system's copy of glut.h, glext.h, and glx.h */
#include <GL/glut.h>
#include <GL/glext.h>
#include <GL/glx.h> #define GET_PROC_ADDRESS( str ) glXGetProcAddress( (const GLubyte *)str ) #endif //_WIN32 #endif //__GL_HELPER_H__'

《GPU高性能编程CUDA实战》附录四 其他头文件的更多相关文章

  1. [问题解决]《GPU高性能编程CUDA实战》中第4章Julia实例“显示器驱动已停止响应,并且已恢复”问题的解决方法

    以下问题的出现及解决都基于"WIN7+CUDA7.5". 问题描述:当我编译运行<GPU高性能编程CUDA实战>中第4章所给Julia实例代码时,出现了显示器闪动的现象 ...

  2. 《GPU高性能编程CUDA实战》第四章 简单的线程块并行

    ▶ 本章介绍了线程块并行,并给出两个例子:长向量加法和绘制julia集. ● 长向量加法,中规中矩的GPU加法,包含申请内存和显存,赋值,显存传入,计算,显存传出,处理结果,清理内存和显存.用到了 t ...

  3. 《GPU高性能编程CUDA实战》附录二 散列表

    ▶ 使用CPU和GPU分别实现散列表 ● CPU方法 #include <stdio.h> #include <time.h> #include "cuda_runt ...

  4. 《GPU高性能编程CUDA实战》附录一 高级原子操作

    ▶ 本章介绍了手动实现原子操作.重构了第五章向量点积的过程.核心是通过定义结构Lock及其运算,实现锁定,读写,解锁的过程. ● 章节代码 #include <stdio.h> #incl ...

  5. 《GPU高性能编程CUDA实战中文》中第四章的julia实验

    在整个过程中出现了各种问题,我先将我调试好的真个项目打包,提供下载. /* * Copyright 1993-2010 NVIDIA Corporation. All rights reserved. ...

  6. 《GPU高性能编程CUDA实战》附录三 关于book.h

    ▶ 本书中用到的公用函数放到了头文件book.h中 #ifndef __BOOK_H__ #define __BOOK_H__ #include <stdio.h> #include &l ...

  7. 《GPU高性能编程CUDA实战》第五章 线程并行

    ▶ 本章介绍了线程并行,并给出四个例子.长向量加法.波纹效果.点积和显示位图. ● 长向量加法(线程块并行 + 线程并行) #include <stdio.h> #include &quo ...

  8. 《GPU高性能编程CUDA实战》第十一章 多GPU系统的CUDA C

    ▶ 本章介绍了多设备胸膛下的 CUDA 编程,以及一些特殊存储类型对计算速度的影响 ● 显存和零拷贝内存的拷贝与计算对比 #include <stdio.h> #include " ...

  9. 《GPU高性能编程CUDA实战》第八章 图形互操作性

    ▶ OpenGL与DirectX,等待填坑. ● basic_interop #include <stdio.h> #include "cuda_runtime.h" ...

随机推荐

  1. HDU 4548:美素数

    Problem Description 小明对数的研究比较热爱,一谈到数,脑子里就涌现出好多数的问题,今天,小明想考考你对素数的认识. 问题是这样的:一个十进制数,如果是素数,而且它的各位数字和也是素 ...

  2. 记录几个ubuntu环境下的php相关的路径

    php路径 /usr/bin/php phpize5 /usr/bin/phpize5 php5-fpm /usr/sbin/php5-fpm php所有的配置文件 /etc/php5/fpm 重启p ...

  3. Linux(CentOS)搭建SVN服务器

    1.安装命令 yum -y install subversion 查看SVN安装位置 rpm -ql subversion 查看SVN版本 svnserve --version 2.创建版本库根目录( ...

  4. .net 拆分字符串成数数组 包含使用空格 逗号 回车 换行符等

    简单的代码如下: public static string[] GetProductList(string inputstring)        {            char[] split ...

  5. AppBox下调用HighCharts画曲线

    例子见本博文件下载. 注意                xAxis: {                    categories: [<%= xAxisCategories %>], ...

  6. SiteMap Editor for Microsoft Dynamics CRM 2011 使用说明

    How to connect to CRM environments using this tool If you already connected to a CRM deployment usin ...

  7. ES(1): Creat linux VM on Azure

    本章记录在ES集群之前的环境准备工作,主要包含的内容如下: 目录: 创建linux虚拟机 启用root用户 创建linux虚拟机 首先创建一个云服务 按向导创建云服务名称,如下 创建虚拟机, 第二步: ...

  8. PHP常用函数总结(一):

    <?php echo "<pre>"; //===============================时间日期======================== ...

  9. Django的 admin管理工具

    admin组件使用 Django 提供了基于 web 的管理工具. Django 自动管理工具是 django.contrib 的一部分.你可以在项目的 settings.py 中的 INSTALLE ...

  10. 利用springMVC包装类上传多个文件

    前端JSP页面代码片段: <!-- springMVC包装类上传文件 --><form name="uploadFiles" id="uploadFil ...