-
Notifications
You must be signed in to change notification settings - Fork 3
/
cuda_utils.cpp
58 lines (53 loc) · 1.73 KB
/
cuda_utils.cpp
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
#include <stdio.h>
#include <cstring>
#include <cuda_runtime_api.h>
#include <cuda_gl_interop.h>
#include "cuda_utils.h"
// cuda needs to be initialized after opengl
void cuda_init()
{
cudaDeviceProp prop;
int dev;
memset(&prop, 0, sizeof(cudaDeviceProp));
prop.major = 6;
prop.minor = 0;
if (cudaChooseDevice(&dev, &prop) != cudaSuccess)
puts("failed to choose device");
printf("cuda chose device %d\n",dev);
if (cudaGLSetGLDevice(dev) != cudaSuccess)
puts("failed to set gl device");
}
// register buffer GLuint and return as a cudaGraphicsResource
void *cuda_register_buffer(GLuint buf)
{
cudaGraphicsResource *res = nullptr;
if (cudaGraphicsGLRegisterBuffer(&res, buf, cudaGraphicsRegisterFlagsNone) != cudaSuccess)
printf("Failed to register buffer %u\n", buf);
return res;
}
void cuda_unregister_resource(void *res)
{
if (cudaGraphicsUnregisterResource(static_cast<cudaGraphicsResource *>(res)) != cudaSuccess)
puts("Failed to unregister resource for buffer");
}
void *cuda_map_resource(void *res)
{
cudaGraphicsResource *ptr = static_cast<cudaGraphicsResource *>(res);
if (cudaGraphicsMapResources(1, &ptr) != cudaSuccess) {
puts("Failed to map resource");
return nullptr;
}
void *devPtr = nullptr;
size_t size;
if (cudaGraphicsResourceGetMappedPointer(&devPtr, &size, static_cast<cudaGraphicsResource *>(res)) != cudaSuccess) {
puts("Failed to get device pointer");
return nullptr;
}
return devPtr;
}
void cuda_unmap_resource(void *res)
{
cudaGraphicsResource *ptr = static_cast<cudaGraphicsResource *>(res);
if (cudaGraphicsUnmapResources(1, &ptr) != cudaSuccess)
puts("Failed to unmap resource");
}