11 CudaArray(
void) : m_length(0), m_host(0), m_read_buffer(0), m_write_buffer(1), setmemdev(0)
17 m_vbo =
new unsigned int[2];
30 std::cout <<
"Delete CudaArray begin" << std::endl;
34 std::cout <<
"Delete Buffer" << std::endl;
37 cutilSafeCall(cudaGraphicsUnregisterResource(m_cuda_vbo[0]));
38 glDeleteBuffers(1, &m_vbo[0]);
43 if(setmemdev) cutilSafeCall(cudaFree(m_dev[0]));;
46 std::cout <<
"Delete PingPong Buffer" << std::endl;
51 cutilSafeCall(cudaGraphicsUnregisterResource(m_cuda_vbo[1]));
52 glDeleteBuffers(1, &m_vbo[1]);
57 if(setmemdev) cutilSafeCall(cudaFree(m_dev[1]));
71 std::cout <<
"Delete CudaArray end" << std::endl;
80 void CreateBuffer(
size_t size,
bool vbo=
false,
bool pingpong=
false)
84 m_pingpong = pingpong;
86 if(pingpong) m_write_buffer = 1;
98 unsigned int GetLength()
110 glGenBuffers(1, &tmp_vbo);
111 glBindBuffer(GL_ARRAY_BUFFER, tmp_vbo);
112 glBufferData(GL_ARRAY_BUFFER, size, m_host, GL_DYNAMIC_DRAW);
113 glBindBuffer(GL_ARRAY_BUFFER, 0);
125 cutilSafeCall(cudaGraphicsMapResources(1, &m_cuda_vbo[0], 0));
127 cutilSafeCall(cudaGraphicsResourceGetMappedPointer((
void **)&m_dev[0], &num_bytes, m_cuda_vbo[0]));
129 if(m_pingpong && m_vbo[1])
131 cutilSafeCall(cudaGraphicsMapResources(1, &m_cuda_vbo[1], 0));
133 cutilSafeCall(cudaGraphicsResourceGetMappedPointer((
void **)&m_dev[1], &num_bytes, m_cuda_vbo[1]));
144 cutilSafeCall(cudaGraphicsUnmapResources(1, &m_cuda_vbo[0], 0));
148 if(m_pingpong && m_vbo[1])
150 cutilSafeCall(cudaGraphicsUnmapResources(1, &m_cuda_vbo[1], 0));
183 void SetHost(T* tmp,
unsigned int length)
192 void Swap() { std::swap(m_read_buffer, m_write_buffer); }
199 m_vbo[0] =
CreateVBO(m_length*
sizeof(T));
200 cutilSafeCall(cudaGraphicsGLRegisterBuffer(&m_cuda_vbo[0], m_vbo[0], cudaGraphicsMapFlagsNone));
204 m_vbo[1] =
CreateVBO(m_length*
sizeof(T));
205 cutilSafeCall(cudaGraphicsGLRegisterBuffer(&m_cuda_vbo[1], m_vbo[1], cudaGraphicsMapFlagsNone));
214 m_host = (T *)
new T[m_length];
223 size_t size = m_length*
sizeof(T);
225 cutilSafeCall(cudaMalloc((
void **) &m_dev[0], size));
226 if (m_pingpong) cutilSafeCall(cudaMalloc((
void **) &m_dev[1], size));
244 void SetVBO(
unsigned int id1,
unsigned int id2)
257 void Copy(
bool tohost,
unsigned int start,
unsigned int count,
bool use_write_buffer =
false)
262 if(tohost) cutilSafeCall(cudaMemcpy((
void *) (m_host + start), (
void *) (m_dev[m_write_buffer] + start), count*
sizeof(T), cudaMemcpyDeviceToHost));
263 else cutilSafeCall(cudaMemcpy((
void *) (m_dev[m_write_buffer] + start), (
void *) (m_host + start), count*
sizeof(T), cudaMemcpyHostToDevice));
269 if(tohost) cutilSafeCall(cudaMemcpy((
void *) (m_host + start), (
void *) (m_dev[m_read_buffer] + start), count*
sizeof(T), cudaMemcpyDeviceToHost));
270 else cutilSafeCall(cudaMemcpy((
void *) (m_dev[m_read_buffer] + start), (
void *) (m_host + start), count*
sizeof(T), cudaMemcpyHostToDevice));
280 void Debug(
unsigned int start,
unsigned int count)
282 if(count == 0) count = m_length;
284 Copy(
true, 0, count);
287 for(
unsigned int i=start; i<(start+count); i++)
291 unsigned int tmp = m_host[i];
292 if(tmp < 0xffffffff)printf(
"%d \n", m_host[i]);
298 unsigned int m_length;
303 unsigned int m_read_buffer;
304 unsigned int m_write_buffer;
310 struct cudaGraphicsResource *m_cuda_vbo[2];