▶ 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. Eclipse使用前准备(转)

    Eclipse的发布流程 M1  08/19/2009      M2     09/30/2009     M3     11/11/2009     M4     12/16/2009     M ...

  2. 磁盘 -> 硬盘 -> c盘 && 内存

    磁盘是计算机的外部存储器,分为两类,一类是硬盘,一类是软盘. (附:计算机的存储器有两类,一类是内部存储器(内存条是用半导体材料做成的),断电不会保存当前工作:一类是外部存储器,断了电也能可以保存.) ...

  3. leetcode:Single Number【Python版】

    1.用双重循环逐个遍历(超时) 2.用list B的append和remove函数(超时) 3.用dict B(AC) class Solution: # @param A, a list of in ...

  4. 在 ASP.NET 网页中不经过回发而以编程方式实现客户端回调

    在 ASP.NET 网页的默认模型中,用户会与页交互,单击按钮或执行导致回发的一些其他操作.此时将重新创建页及其控件,并在服务器上运行页代码,且新版本的页被呈现到浏览器.但是,在有些情况下,需要从客户 ...

  5. VisualSVN安装配置与使用

    VisualSVN安装配置与使用 1.  所选服务器安装包:VisualSVN-Server-2.1.3.msi. 2.  客户端安装包:TortoiseSVN-1.6.2.16344-win32-s ...

  6. 移植RTL8188CUS USB-WIFI(移植失败)

    1.主makefile CONFIG_POWER_SAVING = n CONFIG_PLATFORM_I386_PC = n CONFIG_PLATFORM_HI3518E = y ##swann ...

  7. Apache关闭VirtualHost的Log日志记录

    有时我们的apache产生的日志是超大的并且 没什么用处,这时我们就可以关闭了,关闭apache日志很简单,直接ErrorLog off或 # CustomLog即可. Web server(ex: ...

  8. 二分法查找 (Binary Search)

    二分法查找适用于排列有序的数据.java实现方法如下: // Find the location of a value in array a // Array a must be sorted // ...

  9. JS判断IP的正则表达式

    <html> <head> <title>最简洁的IP判断正则表达式</title> <meta http-equiv="Content ...

  10. 【appium】根据xpath定位元素

    1. 背景 本文尝试使用的试验对象是SDK自带的NotePad应用实例,假设已经有两个Notes分别是“note1”和“note2”添加到Notepad上面,我们要做的就是尝试用xpath的方法来定位 ...