glbind.h
2.29 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
#ifndef RTS_GL_BIND_H
#define RTS_GL_BIND_H
//#include <GL/glew.h>
#ifdef WIN32
#include <GL/GL.h>
#include <GL/GLU.h>
#else
#include <GL/gl.h>
#include <GL/glu.h>
#endif
#include <stdio.h>
#include <cstring>
//#include <cudaHandleError.h>
#include "cuda_gl_interop.h"
#include <stim/gl/error.h>
namespace stim
{
static void InitGLEW()
{
//Initialize the GLEW toolkit
/*GLenum err = glewInit();
if(GLEW_OK != err)
{
printf("Error starting GLEW.");
}
fprintf(stdout, "Status: Using GLEW %s\n", glewGetString(GLEW_VERSION));*/
}
static void cudaSetDevice(int major = 1, int minor = 3)
{
cudaDeviceProp prop;
int dev;
//find a CUDA device that can handle an offscreen buffer
int num_gpu;
HANDLE_ERROR(cudaGetDeviceCount(&num_gpu));
printf("Number of CUDA devices detected: %d\n", num_gpu);
memset(&prop, 0, sizeof(cudaDeviceProp));
prop.major=major;
prop.minor=minor;
HANDLE_ERROR(cudaChooseDevice(&dev, &prop));
HANDLE_ERROR(cudaGetDeviceProperties(&prop, dev));
HANDLE_ERROR(cudaGLSetGLDevice(dev));
}
static void* cudaMapResource(cudaGraphicsResource* cudaBufferResource)
{
//this function takes a predefined CUDA resource and maps it to a pointer
void* buffer;
HANDLE_ERROR(cudaGraphicsMapResources(1, &cudaBufferResource, NULL));
size_t size;
HANDLE_ERROR(cudaGraphicsResourceGetMappedPointer( (void**)&buffer, &size, cudaBufferResource));
return buffer;
}
static void cudaUnmapResource(cudaGraphicsResource* resource)
{
//this function unmaps the CUDA resource so it can be used by OpenGL
HANDLE_ERROR(cudaGraphicsUnmapResources(1, &resource, NULL));
}
static void cudaCreateRenderBuffer(GLuint &glBufferName, cudaGraphicsResource* &cudaBufferResource, int resX, int resY)
{
//delete the previous buffer name and resource
if(cudaBufferResource != 0)
HANDLE_ERROR(cudaGraphicsUnregisterResource(cudaBufferResource));
if(glBufferName != 0)
glDeleteBuffers(1, &glBufferName);
//generate an OpenGL offscreen buffer
glGenBuffers(1, &glBufferName);
//bind the buffer - directs all calls to this buffer
glBindBuffer(GL_PIXEL_UNPACK_BUFFER, glBufferName);
glBufferData(GL_PIXEL_UNPACK_BUFFER, resX * resY * sizeof(uchar3), NULL, GL_DYNAMIC_DRAW_ARB);
CHECK_OPENGL_ERROR
HANDLE_ERROR(cudaGraphicsGLRegisterBuffer(&cudaBufferResource, glBufferName, cudaGraphicsMapFlagsNone));
}
}
#endif