-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdisplay.hpp
64 lines (51 loc) · 1.99 KB
/
display.hpp
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
#pragma once
#include <Shader.hpp>
#include <GLFW/glfw3.h>
#include <cuda_gl_interop.h>
#include <data.hpp>
// Temporary texture that gets bound in ctor and unbound in dtor
struct TempTexture
{
TempTexture(int width, int height) : width(width), height(height), resource(nullptr)
{
glGenTextures(1, &textureID);
glBindTexture(GL_TEXTURE_2D, textureID);
// Set texture parameters
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA32F, width, height, 0, GL_RGBA, GL_FLOAT, nullptr);
}
~TempTexture() {
if (resource != nullptr) {
cudaGraphicsUnregisterResource(resource);
}
glBindTexture(GL_TEXTURE_2D, 0);
glDeleteTextures(1, &textureID);
}
void setDataCPU(Color* img)
{
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, GL_RGBA, GL_FLOAT, img);
}
void setDataGPUSlow(Color* img)
{
std::vector<Color> hostData;
hostData.resize(width * height);
CHECK_CUDA(cudaMemcpy(hostData.data(), img, sizeof(Color) * width * height, cudaMemcpyDefault));
setDataCPU(hostData.data());
}
void setDataGPUFast(Color* img) {
CHECK_CUDA(cudaGraphicsGLRegisterImage(&resource, textureID, GL_TEXTURE_2D, cudaGraphicsRegisterFlagsWriteDiscard));
cudaArray_t array;
CHECK_CUDA(cudaGraphicsMapResources(1, &resource));
CHECK_CUDA(cudaGraphicsSubResourceGetMappedArray(&array, resource, 0, 0));
CHECK_CUDA(cudaMemcpy2DToArray(array, 0, 0, img, width * sizeof(Color), width * sizeof(Color), height, cudaMemcpyDeviceToDevice));
CHECK_CUDA(cudaGraphicsUnmapResources(1, &resource));
}
private:
int width;
int height;
GLuint textureID;
cudaGraphicsResource_t resource;
};