-
Notifications
You must be signed in to change notification settings - Fork 5
/
volrenraycastcuda.cpp
212 lines (186 loc) · 8.14 KB
/
volrenraycastcuda.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
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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
#include "volrenraycastcuda.h"
#include "volrenraycastcuda.cuda.h"
#include <cassert>
#include <volumeglcuda.h>
#include <raycastfrustum.h>
#include "imagepbo.h"
//
//
// cuda error checking
//
//
#define cc(ans) { gpuAssert((ans), __FILE__, __LINE__); }
static void gpuAssert(cudaError_t code, const char *file, int line, bool abort=true)
{
if (code != cudaSuccess)
{
fprintf(stderr,"GPUassert: %s %s %d\n", cudaGetErrorString(code), file, line);
if (abort) exit(code);
}
}
//
//
// VolRenRaycastCuda
//
//
namespace yy {
namespace volren {
#define BASE VolRenRaycast
VolRenRaycastCuda::VolRenRaycastCuda()
: BASE(Method_Raycast_CUDA)
, outPBO(0)
, entryRes(NULL), exitRes(NULL), outRes(NULL)
, texWidth(_frustum->texWidth()), texHeight(_frustum->texHeight())
{
}
std::unique_ptr<VolRenRaycastCuda> VolRenRaycastCuda::create()
{
return std::unique_ptr<VolRenRaycastCuda>(new VolRenRaycastCuda());
}
VolRenRaycastCuda::~VolRenRaycastCuda()
{
if (entryRes) cc(cudaGraphicsUnregisterResource(entryRes));
if (exitRes) cc(cudaGraphicsUnregisterResource(exitRes));
if (outRes) cc(cudaGraphicsUnregisterResource(outRes));
// if (volRes) cc(cudaGraphicsUnregisterResource(volRes));
// if (tfFullRes) cc(cudaGraphicsUnregisterResource(tfFullRes));
// if (tfBackRes) cc(cudaGraphicsUnregisterResource(tfBackRes));
QOpenGLFunctions f(QOpenGLContext::currentContext());
if (0 != outPBO) f.glDeleteBuffers(1, &outPBO);
}
void VolRenRaycastCuda::initializeGL()
{
BASE::initializeGL();
updateCUDAResources();
}
void VolRenRaycastCuda::resize(int w, int h)
{
BASE::resize(w, h);
updateCUDAResources();
}
// TODO: SetVolumeCUDAed<BASE>
void VolRenRaycastCuda::setVolume(const std::shared_ptr<IVolume> &volume)
{
// check supported pixel type
static std::map<Volume::DataType, QOpenGLTexture::PixelType> dt2pt
= {{Volume::DT_Char, QOpenGLTexture::Int8},
{Volume::DT_Unsigned_Char, QOpenGLTexture::UInt8},
{Volume::DT_Float, QOpenGLTexture::Float32}};
if (0 == dt2pt.count(volume->pixelType()))
{
std::cout << "Unsupported pixel type..." << std::endl;
return;
}
// TODO: convert directly from Volume to VolumeCUDA without VolumeGL
// whether volume has the interface I need
std::shared_ptr<IVolumeCUDA> ptr = std::dynamic_pointer_cast<IVolumeCUDA>(volume);
if (!ptr)
ptr.reset(new VolumeGLCUDA(volume));
this->volume = ptr;
// set bounding box dimension
_frustum->setVolSize(
this->volume->w() * this->volume->sx(),
this->volume->h() * this->volume->sy(),
this->volume->d() * this->volume->sz());
}
//void VolRenRaycastCuda::setTF(const mslib::TF &tf, bool preinteg, float stepsize, Filter filter)
//{
// BASE::setTF(tf, preinteg, stepsize, filter);
// if (tfFullRes) cc(cudaGraphicsUnregisterResource(tfFullRes));
// cc(cudaGraphicsGLRegisterImage(&tfFullRes, tfInteg->getTexFull()->textureId(), GL_TEXTURE_2D, cudaGraphicsRegisterFlagsReadOnly));
// if (tfBackRes) cc(cudaGraphicsUnregisterResource(tfBackRes));
// cc(cudaGraphicsGLRegisterImage(&tfBackRes, tfInteg->getTexBack()->textureId(), GL_TEXTURE_2D, cudaGraphicsRegisterFlagsReadOnly));
//}
void VolRenRaycastCuda::setColormap(const std::shared_ptr<IColormap>& colormap)
{
auto ptr = std::dynamic_pointer_cast<IColormapCUDA>(colormap);
if (!ptr)
ptr = std::make_shared<ColormapGLCUDA>(colormap);
this->colormap = ptr;
}
std::shared_ptr<ImageAbstract> VolRenRaycastCuda::output() const
{
return std::make_shared<ImagePBO>(outPBO, _frustum->texWidth(), _frustum->texHeight());
}
void VolRenRaycastCuda::raycast()
{
// update entry and exit resources
if (entryRes) cc(cudaGraphicsUnregisterResource(entryRes));
cc(cudaGraphicsGLRegisterImage(&entryRes, *_frustum->texEntry(), GL_TEXTURE_2D, cudaGraphicsRegisterFlagsReadOnly));
if (exitRes) cc(cudaGraphicsUnregisterResource(exitRes));
cc(cudaGraphicsGLRegisterImage(&exitRes, *_frustum->texExit(), GL_TEXTURE_2D, cudaGraphicsRegisterFlagsReadOnly));
// TODO: getCUDAMappedArray();
cudaGraphicsResource_t volRes = this->volume->getCUDAResource();
cudaGraphicsResource_t tfFullRes = this->colormap->cudaResFull();
cudaGraphicsResource_t tfBackRes = this->colormap->cudaResBack();
cc(cudaGraphicsMapResources(1, &entryRes, 0));
cc(cudaGraphicsMapResources(1, &exitRes, 0));
cc(cudaGraphicsMapResources(1, &outRes, 0));
cc(cudaGraphicsMapResources(1, &volRes, 0));
cc(cudaGraphicsMapResources(1, &tfFullRes, 0));
cc(cudaGraphicsMapResources(1, &tfBackRes, 0));
cudaArray *entryArr, *exitArr, *volArr, *tfFullArr, *tfBackArr;
float *outPtr;
size_t nBytes;
cc(cudaGraphicsSubResourceGetMappedArray(&entryArr, entryRes, 0, 0));
cc(cudaGraphicsSubResourceGetMappedArray(&exitArr, exitRes, 0, 0));
cc(cudaGraphicsResourceGetMappedPointer((void**)&outPtr, &nBytes, outRes));
cc(cudaGraphicsSubResourceGetMappedArray(&volArr, volRes, 0, 0));
cc(cudaGraphicsSubResourceGetMappedArray(&tfFullArr, tfFullRes, 0, 0));
cc(cudaGraphicsSubResourceGetMappedArray(&tfBackArr, tfBackRes, 0, 0));
static std::map<IColormap::Filter, cudaTextureFilterMode> vr2cu
= { { IColormap::Filter_Linear, cudaFilterModeLinear }
, { IColormap::Filter_Nearest, cudaFilterModePoint } };
assert(vr2cu.count(colormap->filter()) > 0);
updateCUDALights(_frustum->matView());
cudacast(this->volume->w(), this->volume->h(), this->volume->d(), volArr,
colormap->nColors(), colormap->nColors(), colormap->stepsize(), vr2cu[colormap->filter()], tfFullArr, tfBackArr,
scalarMin, scalarMax,
_frustum->texWidth(), _frustum->texHeight(), entryArr, exitArr, outPtr);
cc(cudaGraphicsUnmapResources(1, &tfBackRes, 0));
cc(cudaGraphicsUnmapResources(1, &tfFullRes, 0));
cc(cudaGraphicsUnmapResources(1, &volRes, 0));
cc(cudaGraphicsUnmapResources(1, &entryRes, 0));
cc(cudaGraphicsUnmapResources(1, &exitRes, 0));
cc(cudaGraphicsUnmapResources(1, &outRes, 0));
}
void VolRenRaycastCuda::updateCUDAResources()
{
QOpenGLFunctions f(QOpenGLContext::currentContext());
// update output PBO and CUDA resource
// clean up
if (0 != outPBO) f.glDeleteBuffers(1, &outPBO);
// pbo
f.glGenBuffers(1, &outPBO);
f.glBindBuffer(GL_PIXEL_UNPACK_BUFFER, outPBO);
f.glBufferData(GL_PIXEL_UNPACK_BUFFER, 4 * _frustum->texWidth() * _frustum->texHeight() * sizeof(float), NULL, GL_STREAM_COPY);
f.glBindBuffer(GL_PIXEL_UNPACK_BUFFER, 0);
// register cuda resource
if (outRes) cc(cudaGraphicsUnregisterResource(outRes));
cc(cudaGraphicsGLRegisterBuffer(&outRes, outPBO, cudaGraphicsMapFlagsWriteDiscard));
}
void VolRenRaycastCuda::updateCUDALights(const QMatrix4x4& matView)
{
CudaLight cudaLights[10];
for (unsigned int i = 0; i < lights.size(); ++i)
{
QVector3D lightDir(matView.inverted() * QVector4D(lights[i].direction, 0.f));
lightDir.normalize();
cudaLights[i].direction.x = lightDir.x();
cudaLights[i].direction.y = lightDir.y();
cudaLights[i].direction.z = lightDir.z();
cudaLights[i].ambient.x = lights[i].color.x() * lights[i].ambient;
cudaLights[i].ambient.y = lights[i].color.y() * lights[i].ambient;
cudaLights[i].ambient.z = lights[i].color.z() * lights[i].ambient;
cudaLights[i].diffuse.x = lights[i].color.x() * lights[i].diffuse;
cudaLights[i].diffuse.y = lights[i].color.y() * lights[i].diffuse;
cudaLights[i].diffuse.z = lights[i].color.z() * lights[i].diffuse;
cudaLights[i].specular.x = lights[i].color.x() * lights[i].specular;
cudaLights[i].specular.y = lights[i].color.y() * lights[i].specular;
cudaLights[i].specular.z = lights[i].color.z() * lights[i].specular;
cudaLights[i].shininess = lights[i].shininess;
}
cudaSetLights(lights.size(), cudaLights);
}
} // namespace volren
} // namespace yy