@@ -4172,13 +4172,13 @@ class AlignedDataPtr2D
4172
4172
size_t step_;
4173
4173
4174
4174
public:
4175
- AlignedDataPtr2D (uchar* ptr, size_t rows, size_t cols, size_t step, size_t alignment)
4175
+ AlignedDataPtr2D (uchar* ptr, size_t rows, size_t cols, size_t step, size_t alignment, size_t extrabytes= 0 )
4176
4176
: size_(rows*step), originPtr_(ptr), alignment_(alignment), ptr_(ptr), allocatedPtr_(NULL ), rows_(rows), cols_(cols), step_(step)
4177
4177
{
4178
4178
CV_DbgAssert ((alignment & (alignment - 1 )) == 0 ); // check for 2^n
4179
- if (((size_t )ptr_ & (alignment - 1 )) != 0 )
4179
+ if (ptr == 0 || ((size_t )ptr_ & (alignment - 1 )) != 0 )
4180
4180
{
4181
- allocatedPtr_ = new uchar[size_ + alignment - 1 ];
4181
+ allocatedPtr_ = new uchar[size_ + extrabytes + alignment - 1 ];
4182
4182
ptr_ = (uchar*)(((uintptr_t )allocatedPtr_ + (alignment - 1 )) & ~(alignment - 1 ));
4183
4183
if (readAccess)
4184
4184
{
@@ -4978,6 +4978,25 @@ class OpenCLAllocator : public MatAllocator
4978
4978
CV_OCL_CHECK (clEnqueueReadBuffer (q, (cl_mem)u->handle , CL_TRUE,
4979
4979
srcrawofs, total, alignedPtr.getAlignedPtr (), 0 , 0 , 0 ));
4980
4980
}
4981
+ #ifdef __APPLE__
4982
+ else
4983
+ {
4984
+ const size_t padding = CV_OPENCL_DATA_PTR_ALIGNMENT;
4985
+ size_t new_srcrawofs = srcrawofs & ~(padding-1 );
4986
+ size_t membuf_ofs = srcrawofs - new_srcrawofs;
4987
+ AlignedDataPtr2D<false , false > alignedPtr (0 , new_sz[1 ], new_srcstep[0 ], new_srcstep[0 ],
4988
+ CV_OPENCL_DATA_PTR_ALIGNMENT, padding*2 );
4989
+ uchar* ptr = alignedPtr.getAlignedPtr ();
4990
+
4991
+ CV_Assert (new_srcstep[0 ] >= new_sz[0 ]);
4992
+ total = alignSize (new_srcstep[0 ]*new_sz[1 ] + membuf_ofs, padding);
4993
+ total = std::min (total, u->size - new_srcrawofs);
4994
+ CV_OCL_CHECK (clEnqueueReadBuffer (q, (cl_mem)u->handle , CL_TRUE,
4995
+ new_srcrawofs, total, ptr, 0 , 0 , 0 ));
4996
+ for ( size_t i = 0 ; i < new_sz[1 ]; i++ )
4997
+ memcpy ( (uchar*)dstptr + i*new_dststep[0 ], ptr + i*new_srcstep[0 ] + membuf_ofs, new_sz[0 ]);
4998
+ }
4999
+ #else
4981
5000
else
4982
5001
{
4983
5002
AlignedDataPtr2D<false , true > alignedPtr ((uchar*)dstptr, new_sz[1 ], new_sz[0 ], new_dststep[0 ], CV_OPENCL_DATA_PTR_ALIGNMENT);
@@ -4989,6 +5008,7 @@ class OpenCLAllocator : public MatAllocator
4989
5008
new_dststep[0 ], 0 ,
4990
5009
ptr, 0 , 0 , 0 ));
4991
5010
}
5011
+ #endif
4992
5012
}
4993
5013
}
4994
5014
@@ -5095,6 +5115,30 @@ class OpenCLAllocator : public MatAllocator
5095
5115
CV_OCL_CHECK (clEnqueueWriteBuffer (q, (cl_mem)u->handle , CL_TRUE,
5096
5116
dstrawofs, total, alignedPtr.getAlignedPtr (), 0 , 0 , 0 ));
5097
5117
}
5118
+ #ifdef __APPLE__
5119
+ else
5120
+ {
5121
+ const size_t padding = CV_OPENCL_DATA_PTR_ALIGNMENT;
5122
+ size_t new_dstrawofs = dstrawofs & ~(padding-1 );
5123
+ size_t membuf_ofs = dstrawofs - new_dstrawofs;
5124
+ AlignedDataPtr2D<false , false > alignedPtr (0 , new_sz[1 ], new_dststep[0 ], new_dststep[0 ],
5125
+ CV_OPENCL_DATA_PTR_ALIGNMENT, padding*2 );
5126
+ uchar* ptr = alignedPtr.getAlignedPtr ();
5127
+
5128
+ CV_Assert (new_dststep[0 ] >= new_sz[0 ] && new_srcstep[0 ] >= new_sz[0 ]);
5129
+ total = alignSize (new_dststep[0 ]*new_sz[1 ] + membuf_ofs, padding);
5130
+ total = std::min (total, u->size - new_dstrawofs);
5131
+ /* printf("new_sz0=%d, new_sz1=%d, membuf_ofs=%d, total=%d (%08x), new_dstrawofs=%d (%08x)\n",
5132
+ (int)new_sz[0], (int)new_sz[1], (int)membuf_ofs,
5133
+ (int)total, (int)total, (int)new_dstrawofs, (int)new_dstrawofs);*/
5134
+ CV_OCL_CHECK (clEnqueueReadBuffer (q, (cl_mem)u->handle , CL_TRUE,
5135
+ new_dstrawofs, total, ptr, 0 , 0 , 0 ));
5136
+ for ( size_t i = 0 ; i < new_sz[1 ]; i++ )
5137
+ memcpy ( ptr + i*new_dststep[0 ] + membuf_ofs, (uchar*)srcptr + i*new_srcstep[0 ], new_sz[0 ]);
5138
+ CV_OCL_CHECK (clEnqueueWriteBuffer (q, (cl_mem)u->handle , CL_TRUE,
5139
+ new_dstrawofs, total, ptr, 0 , 0 , 0 ));
5140
+ }
5141
+ #else
5098
5142
else
5099
5143
{
5100
5144
AlignedDataPtr2D<true , false > alignedPtr ((uchar*)srcptr, new_sz[1 ], new_sz[0 ], new_srcstep[0 ], CV_OPENCL_DATA_PTR_ALIGNMENT);
@@ -5106,6 +5150,7 @@ class OpenCLAllocator : public MatAllocator
5106
5150
new_srcstep[0 ], 0 ,
5107
5151
ptr, 0 , 0 , 0 ));
5108
5152
}
5153
+ #endif
5109
5154
}
5110
5155
u->markHostCopyObsolete (true );
5111
5156
#ifdef HAVE_OPENCL_SVM
@@ -5247,6 +5292,41 @@ class OpenCLAllocator : public MatAllocator
5247
5292
CV_OCL_CHECK (retval = clEnqueueCopyBuffer (q, (cl_mem)src->handle , (cl_mem)dst->handle ,
5248
5293
srcrawofs, dstrawofs, total, 0 , 0 , 0 ));
5249
5294
}
5295
+ #ifdef __APPLE__
5296
+ else
5297
+ {
5298
+ const size_t padding = CV_OPENCL_DATA_PTR_ALIGNMENT;
5299
+ size_t new_srcrawofs = srcrawofs & ~(padding-1 );
5300
+ size_t srcmembuf_ofs = srcrawofs - new_srcrawofs;
5301
+ size_t new_dstrawofs = dstrawofs & ~(padding-1 );
5302
+ size_t dstmembuf_ofs = dstrawofs - new_dstrawofs;
5303
+
5304
+ AlignedDataPtr2D<false , false > srcBuf (0 , new_sz[1 ], new_srcstep[0 ], new_srcstep[0 ],
5305
+ CV_OPENCL_DATA_PTR_ALIGNMENT, padding*2 );
5306
+ AlignedDataPtr2D<false , false > dstBuf (0 , new_sz[1 ], new_dststep[0 ], new_dststep[0 ],
5307
+ CV_OPENCL_DATA_PTR_ALIGNMENT, padding*2 );
5308
+ uchar* srcptr = srcBuf.getAlignedPtr ();
5309
+ uchar* dstptr = dstBuf.getAlignedPtr ();
5310
+
5311
+ CV_Assert (new_dststep[0 ] >= new_sz[0 ] && new_srcstep[0 ] >= new_sz[0 ]);
5312
+
5313
+ size_t src_total = alignSize (new_srcstep[0 ]*new_sz[1 ] + srcmembuf_ofs, padding);
5314
+ src_total = std::min (src_total, src->size - new_srcrawofs);
5315
+ size_t dst_total = alignSize (new_dststep[0 ]*new_sz[1 ] + dstmembuf_ofs, padding);
5316
+ dst_total = std::min (dst_total, dst->size - new_dstrawofs);
5317
+
5318
+ CV_OCL_CHECK (clEnqueueReadBuffer (q, (cl_mem)src->handle , CL_TRUE,
5319
+ new_srcrawofs, src_total, srcptr, 0 , 0 , 0 ));
5320
+ CV_OCL_CHECK (clEnqueueReadBuffer (q, (cl_mem)dst->handle , CL_TRUE,
5321
+ new_dstrawofs, dst_total, dstptr, 0 , 0 , 0 ));
5322
+
5323
+ for ( size_t i = 0 ; i < new_sz[1 ]; i++ )
5324
+ memcpy ( dstptr + dstmembuf_ofs + i*new_dststep[0 ],
5325
+ srcptr + srcmembuf_ofs + i*new_srcstep[0 ], new_sz[0 ]);
5326
+ CV_OCL_CHECK (clEnqueueWriteBuffer (q, (cl_mem)dst->handle , CL_TRUE,
5327
+ new_dstrawofs, dst_total, dstptr, 0 , 0 , 0 ));
5328
+ }
5329
+ #else
5250
5330
else
5251
5331
{
5252
5332
CV_OCL_CHECK (retval = clEnqueueCopyBufferRect (q, (cl_mem)src->handle , (cl_mem)dst->handle ,
@@ -5255,6 +5335,7 @@ class OpenCLAllocator : public MatAllocator
5255
5335
new_dststep[0 ], 0 ,
5256
5336
0 , 0 , 0 ));
5257
5337
}
5338
+ #endif
5258
5339
}
5259
5340
if (retval == CL_SUCCESS)
5260
5341
{
0 commit comments