7 #include <cutil_inline.h>
8 #include <cutil_gl_inline.h>
9 #include <channel_descriptor.h>
11 #include <vector_types.h>
12 #include <vector_functions.h>
13 #include <driver_functions.h>
14 #include <cuda_gl_interop.h>
17 template <
class T,
unsigned int N>
25 CTT_CUDA_READ_OGL_TEXTURE,
26 CTT_READ_WRITE_CUDA_AND_OGL
44 CudaTexture(
unsigned int width,
unsigned int height,
unsigned int depth) : m_type(CTT_CUDA)
46 res = make_cudaExtent(width, height, depth);
57 CudaTexture(
unsigned int width,
unsigned int height,
unsigned int depth,
bool write) : m_type(CTT_CUDA_RW)
59 res = make_cudaExtent(width, height, depth);
67 CudaTexture(Texture::TexturePtr tex) : m_type(CTT_CUDA_READ_OGL_TEXTURE)
70 res.width = tex->m_width;
71 res.height = tex->m_height;
72 res.depth = tex->m_depth;
82 CudaTexture(Texture::TexturePtr tex,
bool write) : m_type(CTT_READ_WRITE_CUDA_AND_OGL)
85 res.width = tex->m_width;
86 if(N > 1) res.height = tex->m_height;
87 if(N > 2) res.depth = tex->m_depth;
98 cudaFreeArray( m_volumeArray );
106 if(m_type == CTT_CUDA_READ_OGL_TEXTURE || m_type == CTT_READ_WRITE_CUDA_AND_OGL)
111 cutilSafeCall(cudaGraphicsGLRegisterImage(&cudaRes, m_tex->GetTexName(), GL_TEXTURE_2D, cudaGraphicsMapFlagsReadOnly));
112 cudaGLRegisterBufferObject(m_tex->GetTexName());
115 cutilSafeCall(cudaGraphicsGLRegisterImage(&cudaRes, m_tex->GetTexName(), GL_TEXTURE_3D, cudaGraphicsMapFlagsReadOnly));
116 cudaGLRegisterBufferObject(m_tex->GetTexName());
121 if(m_type == CTT_CUDA || m_type == CTT_CUDA_RW || m_type == CTT_READ_WRITE_CUDA_AND_OGL)
132 cutilSafeCall(cudaGraphicsMapResources(1, &cudaRes, 0));
133 cutilSafeCall(cudaGraphicsSubResourceGetMappedArray(&m_volumeArray, cudaRes, 0, 0));
141 cutilSafeCall(cudaGraphicsUnmapResources(1, &cudaRes, 0));
149 cutilSafeCall(cudaMemset(m_device,0,
sizeof(T) * dim[0] * max(1, dim[1]) * max(1, dim[2])));
157 return m_volumeArray;
179 desc = cudaCreateChannelDesc<T>();
184 std::cout <<
"Create Array" << std::endl;
185 if(m_type == CTT_CUDA_RW) cutilSafeCall(cudaMalloc((
void**)&m_device,
sizeof(T)*res.width));
186 cutilSafeCall(cudaMallocArray(&m_volumeArray, &desc, res.width,1));
189 cutilSafeCall(cudaMalloc((
void**)&m_device,
sizeof(T)*res.width * res.height));
190 cutilSafeCall(cudaMallocArray(&m_volumeArray, &desc, res.width, res.height));
193 cutilSafeCall(cudaMalloc((
void**)&m_device,
sizeof(T)*res.width * res.height * res.depth));
194 cutilSafeCall(cudaMalloc3DArray(&m_volumeArray, &desc, res));
206 ArrayCopy((
void*)(m_device), m_volumeArray, (
size_t)(
sizeof(T)), res);
215 cudaArray* m_volumeArray;
216 struct cudaGraphicsResource* cudaRes;
217 Texture::TexturePtr m_tex;
219 cudaChannelFormatDesc desc;