22 #if defined(CL_SIFTGPU_ENABLED)
33 #include <CL/OpenCL.h>
39 CLTexImage::CLTexImage()
44 _numChannel = _bufferLen = _fromGL = 0;
45 _imgWidth = _imgHeight = _texWidth = _texHeight = 0;
48 CLTexImage::CLTexImage(cl_context
context, cl_command_queue queue)
53 _numChannel = _bufferLen = _fromGL = 0;
54 _imgWidth = _imgHeight = _texWidth = _texHeight = 0;
57 void CLTexImage::SetContext(cl_context
context, cl_command_queue queue)
64 CLTexImage::~CLTexImage()
69 void CLTexImage::ReleaseTexture()
71 if(_fromGL) clEnqueueReleaseGLObjects(_queue, 1, &_clData, 0,
NULL,
NULL);
72 if(_clData) clReleaseMemObject(_clData);
75 void CLTexImage::SetImageSize(
int width,
int height)
81 void CLTexImage::InitBufferTex(
int width,
int height,
int nchannel)
83 if(
width == 0 ||
height == 0 || nchannel <= 0 || _fromGL)
return;
86 _texWidth = _texHeight = _fromGL = 0;
87 _numChannel =
min(nchannel, 4);
90 if (
size <= _bufferLen)
return;
94 if(_clData) status = clReleaseMemObject(_clData);
96 _clData = clCreateBuffer(_context, CL_MEM_READ_WRITE,
99 ProgramBagCL::CheckErrorCL(status,
"CLTexImage::InitBufferTex");
103 void CLTexImage::InitTexture(
int width,
int height,
int nchannel)
105 if(
width == 0 ||
height == 0 || nchannel <= 0 || _fromGL)
return;
106 if(_clData &&
width == _texWidth &&
height == _texHeight && _numChannel == nchannel)
return;
107 if(_clData) clReleaseMemObject(_clData);
109 _texWidth = _imgWidth =
width;
110 _texHeight = _imgHeight =
height;
111 _numChannel = nchannel;
112 _bufferLen = _fromGL = 0;
114 cl_int status; cl_image_format
format;
116 if(nchannel == 1)
format.image_channel_order = CL_R;
117 else if(nchannel == 2)
format.image_channel_order = CL_RG;
118 else if(nchannel == 3)
format.image_channel_order = CL_RGB;
119 else format.image_channel_order = CL_RGBA;
121 format.image_channel_data_type = CL_FLOAT;
122 _clData = clCreateImage2D(_context, CL_MEM_READ_WRITE, &
format,
123 _texWidth, _texHeight, 0, 0, &status);
124 ProgramBagCL::CheckErrorCL(status,
"CLTexImage::InitTexture");
127 void CLTexImage::InitPackedTex(
int width,
int height,
int packed)
129 if(packed) InitTexture((
width + 1) >> 1, (
height + 1) >> 1, 4);
133 void CLTexImage::SetPackedSize(
int width,
int height,
int packed)
135 if(packed) SetImageSize((
width + 1) >> 1, (
height + 1) >> 1);
139 void CLTexImage::InitTextureGL(GLuint tex,
int width,
int height,
int nchannel)
142 if(_clData) clReleaseMemObject(_clData);
146 _clData = clCreateFromGLTexture2D(_context, CL_MEM_WRITE_ONLY,
148 ProgramBagCL::CheckErrorCL(status,
"CLTexImage::InitTextureGL->clCreateFromGLTexture2D");
149 if(status != CL_SUCCESS)
return;
151 _texWidth = _imgWidth =
width;
152 _texHeight = _imgHeight =
height;
153 _numChannel = nchannel;
154 _bufferLen = 0; _fromGL = 1;
157 status = clEnqueueAcquireGLObjects(_queue, 1, &_clData, 0,
NULL,
NULL);
158 ProgramBagCL::CheckErrorCL(status,
"CLTexImage::InitTextureGL->clEnqueueAcquireGLObjects");
162 void CLTexImage::CopyFromHost(
const void * buf)
164 if(_clData ==
NULL)
return;
168 status = clEnqueueWriteBuffer(_queue, _clData,
false, 0,
169 _imgWidth * _imgHeight * _numChannel *
sizeof(
float), buf, 0,
NULL,
NULL);
172 size_t origin[3] = {0, 0, 0}, region[3] = {_imgWidth, _imgHeight, 1};
173 size_t row_pitch = _imgWidth * _numChannel *
sizeof(float);
174 status = clEnqueueWriteImage(_queue, _clData,
false, origin,
175 region, row_pitch, 0, buf, 0, 0, 0);
177 ProgramBagCL::CheckErrorCL(status,
"CLTexImage::CopyFromHost");
180 int CLTexImage::GetImageDataSize()
182 return _imgWidth * _imgHeight * _numChannel *
sizeof(float);
185 int CLTexImage::CopyToPBO(GLuint pbo)
187 glBindBufferARB(GL_PIXEL_UNPACK_BUFFER_ARB, pbo);
189 int esize = GetImageDataSize(), bsize;
190 glGetBufferParameteriv(GL_PIXEL_UNPACK_BUFFER_ARB, GL_BUFFER_SIZE, &bsize);
193 glBufferData(GL_PIXEL_UNPACK_BUFFER_ARB, esize,
NULL, GL_STATIC_DRAW_ARB);
194 glGetBufferParameteriv(GL_PIXEL_UNPACK_BUFFER_ARB, GL_BUFFER_SIZE, &bsize);
199 void* ptr = glMapBufferARB(GL_PIXEL_UNPACK_BUFFER_ARB, GL_WRITE_ONLY_ARB);
202 glUnmapBufferARB(GL_PIXEL_UNPACK_BUFFER_ARB);
204 glBindBufferARB(GL_PIXEL_UNPACK_BUFFER_ARB, 0);
206 return esize >= bsize;
209 void CLTexImage::CopyToHost(
void * buf)
211 if(_clData ==
NULL)
return;
215 status = clEnqueueReadBuffer(_queue, _clData,
true, 0,
216 _imgWidth * _imgHeight * _numChannel *
sizeof(
float), buf, 0,
NULL,
NULL);
219 size_t origin[3] = {0, 0, 0}, region[3] = {_imgWidth, _imgHeight, 1};
220 size_t row_pitch = _imgWidth * _numChannel *
sizeof(float);
221 status = clEnqueueReadImage(_queue, _clData,
true, origin,
222 region, row_pitch, 0, buf, 0, 0, 0);
225 ProgramBagCL::CheckErrorCL(status,
"CLTexImage::CopyToHost");
filament::Texture::InternalFormat format
static void CheckErrorsGL(const char *location=NULL)