opencl_kernels_objdetect.cpp 38 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048
  1. // This file is auto-generated. Do not edit!
  2. #include "opencv2/core.hpp"
  3. #include "cvconfig.h"
  4. #include "opencl_kernels_objdetect.hpp"
  5. #ifdef HAVE_OPENCL
  6. namespace cv
  7. {
  8. namespace ocl
  9. {
  10. namespace objdetect
  11. {
  12. static const char* const moduleName = "objdetect";
  13. struct cv::ocl::internal::ProgramEntry cascadedetect_oclsrc={moduleName, "cascadedetect",
  14. "#ifdef HAAR\n"
  15. "typedef struct __attribute__((aligned(4))) OptHaarFeature\n"
  16. "{\n"
  17. "int4 ofs[3] __attribute__((aligned (4)));\n"
  18. "float4 weight __attribute__((aligned (4)));\n"
  19. "}\n"
  20. "OptHaarFeature;\n"
  21. "#endif\n"
  22. "#ifdef LBP\n"
  23. "typedef struct __attribute__((aligned(4))) OptLBPFeature\n"
  24. "{\n"
  25. "int16 ofs __attribute__((aligned (4)));\n"
  26. "}\n"
  27. "OptLBPFeature;\n"
  28. "#endif\n"
  29. "typedef struct __attribute__((aligned(4))) Stump\n"
  30. "{\n"
  31. "float4 st __attribute__((aligned (4)));\n"
  32. "}\n"
  33. "Stump;\n"
  34. "typedef struct __attribute__((aligned(4))) Node\n"
  35. "{\n"
  36. "int4 n __attribute__((aligned (4)));\n"
  37. "}\n"
  38. "Node;\n"
  39. "typedef struct __attribute__((aligned (4))) Stage\n"
  40. "{\n"
  41. "int first __attribute__((aligned (4)));\n"
  42. "int ntrees __attribute__((aligned (4)));\n"
  43. "float threshold __attribute__((aligned (4)));\n"
  44. "}\n"
  45. "Stage;\n"
  46. "typedef struct __attribute__((aligned (4))) ScaleData\n"
  47. "{\n"
  48. "float scale __attribute__((aligned (4)));\n"
  49. "int szi_width __attribute__((aligned (4)));\n"
  50. "int szi_height __attribute__((aligned (4)));\n"
  51. "int layer_ofs __attribute__((aligned (4)));\n"
  52. "int ystep __attribute__((aligned (4)));\n"
  53. "}\n"
  54. "ScaleData;\n"
  55. "#ifndef SUM_BUF_SIZE\n"
  56. "#define SUM_BUF_SIZE 0\n"
  57. "#endif\n"
  58. "#ifndef NODE_COUNT\n"
  59. "#define NODE_COUNT 1\n"
  60. "#endif\n"
  61. "#ifdef HAAR\n"
  62. "__kernel __attribute__((reqd_work_group_size(LOCAL_SIZE_X,LOCAL_SIZE_Y,1)))\n"
  63. "void runHaarClassifier(\n"
  64. "int nscales, __global const ScaleData* scaleData,\n"
  65. "__global const int* sum,\n"
  66. "int _sumstep, int sumoffset,\n"
  67. "__global const OptHaarFeature* optfeatures,\n"
  68. "__global const Stage* stages,\n"
  69. "__global const Node* nodes,\n"
  70. "__global const float* leaves0,\n"
  71. "volatile __global int* facepos,\n"
  72. "int4 normrect, int sqofs, int2 windowsize)\n"
  73. "{\n"
  74. "int lx = get_local_id(0);\n"
  75. "int ly = get_local_id(1);\n"
  76. "int groupIdx = get_group_id(0);\n"
  77. "int i, ngroups = get_global_size(0)/LOCAL_SIZE_X;\n"
  78. "int scaleIdx, tileIdx, stageIdx;\n"
  79. "int sumstep = (int)(_sumstep/sizeof(int));\n"
  80. "int4 nofs0 = (int4)(mad24(normrect.y, sumstep, normrect.x),\n"
  81. "mad24(normrect.y, sumstep, normrect.x + normrect.z),\n"
  82. "mad24(normrect.y + normrect.w, sumstep, normrect.x),\n"
  83. "mad24(normrect.y + normrect.w, sumstep, normrect.x + normrect.z));\n"
  84. "int normarea = normrect.z * normrect.w;\n"
  85. "float invarea = 1.f/normarea;\n"
  86. "int lidx = ly*LOCAL_SIZE_X + lx;\n"
  87. "#if SUM_BUF_SIZE > 0\n"
  88. "int4 nofs = (int4)(mad24(normrect.y, SUM_BUF_STEP, normrect.x),\n"
  89. "mad24(normrect.y, SUM_BUF_STEP, normrect.x + normrect.z),\n"
  90. "mad24(normrect.y + normrect.w, SUM_BUF_STEP, normrect.x),\n"
  91. "mad24(normrect.y + normrect.w, SUM_BUF_STEP, normrect.x + normrect.z));\n"
  92. "#else\n"
  93. "int4 nofs = nofs0;\n"
  94. "#endif\n"
  95. "#define LOCAL_SIZE (LOCAL_SIZE_X*LOCAL_SIZE_Y)\n"
  96. "__local int lstore[SUM_BUF_SIZE + LOCAL_SIZE*5/2+1];\n"
  97. "#if SUM_BUF_SIZE > 0\n"
  98. "__local int* ibuf = lstore;\n"
  99. "__local int* lcount = ibuf + SUM_BUF_SIZE;\n"
  100. "#else\n"
  101. "__local int* lcount = lstore;\n"
  102. "#endif\n"
  103. "__local float* lnf = (__local float*)(lcount + 1);\n"
  104. "__local float* lpartsum = lnf + LOCAL_SIZE;\n"
  105. "__local short* lbuf = (__local short*)(lpartsum + LOCAL_SIZE);\n"
  106. "for( scaleIdx = nscales-1; scaleIdx >= 0; scaleIdx-- )\n"
  107. "{\n"
  108. "__global const ScaleData* s = scaleData + scaleIdx;\n"
  109. "int ystep = s->ystep;\n"
  110. "int2 worksize = (int2)(max(s->szi_width - windowsize.x, 0), max(s->szi_height - windowsize.y, 0));\n"
  111. "int2 ntiles = (int2)((worksize.x + LOCAL_SIZE_X-1)/LOCAL_SIZE_X,\n"
  112. "(worksize.y + LOCAL_SIZE_Y-1)/LOCAL_SIZE_Y);\n"
  113. "int totalTiles = ntiles.x*ntiles.y;\n"
  114. "for( tileIdx = groupIdx; tileIdx < totalTiles; tileIdx += ngroups )\n"
  115. "{\n"
  116. "int ix0 = (tileIdx % ntiles.x)*LOCAL_SIZE_X;\n"
  117. "int iy0 = (tileIdx / ntiles.x)*LOCAL_SIZE_Y;\n"
  118. "int ix = lx, iy = ly;\n"
  119. "__global const int* psum0 = sum + mad24(iy0, sumstep, ix0) + s->layer_ofs;\n"
  120. "__global const int* psum1 = psum0 + mad24(iy, sumstep, ix);\n"
  121. "if( ix0 >= worksize.x || iy0 >= worksize.y )\n"
  122. "continue;\n"
  123. "#if SUM_BUF_SIZE > 0\n"
  124. "for( i = lidx*4; i < SUM_BUF_SIZE; i += LOCAL_SIZE_X*LOCAL_SIZE_Y*4 )\n"
  125. "{\n"
  126. "int dy = i/SUM_BUF_STEP, dx = i - dy*SUM_BUF_STEP;\n"
  127. "vstore4(vload4(0, psum0 + mad24(dy, sumstep, dx)), 0, ibuf+i);\n"
  128. "}\n"
  129. "#endif\n"
  130. "if( lidx == 0 )\n"
  131. "lcount[0] = 0;\n"
  132. "barrier(CLK_LOCAL_MEM_FENCE);\n"
  133. "if( ix0 + ix < worksize.x && iy0 + iy < worksize.y )\n"
  134. "{\n"
  135. "#if NODE_COUNT==1\n"
  136. "__global const Stump* stump = (__global const Stump*)nodes;\n"
  137. "#else\n"
  138. "__global const Node* node = nodes;\n"
  139. "__global const float* leaves = leaves0;\n"
  140. "#endif\n"
  141. "#if SUM_BUF_SIZE > 0\n"
  142. "__local const int* psum = ibuf + mad24(iy, SUM_BUF_STEP, ix);\n"
  143. "#else\n"
  144. "__global const int* psum = psum1;\n"
  145. "#endif\n"
  146. "__global const int* psqsum = (__global const int*)(psum1 + sqofs);\n"
  147. "float sval = (psum[nofs.x] - psum[nofs.y] - psum[nofs.z] + psum[nofs.w])*invarea;\n"
  148. "float sqval = (psqsum[nofs0.x] - psqsum[nofs0.y] - psqsum[nofs0.z] + psqsum[nofs0.w])*invarea;\n"
  149. "float nf = (float)normarea * sqrt(max(sqval - sval * sval, 0.f));\n"
  150. "nf = nf > 0 ? nf : 1.f;\n"
  151. "for( stageIdx = 0; stageIdx < SPLIT_STAGE; stageIdx++ )\n"
  152. "{\n"
  153. "int ntrees = stages[stageIdx].ntrees;\n"
  154. "float s = 0.f;\n"
  155. "#if NODE_COUNT==1\n"
  156. "for( i = 0; i < ntrees; i++ )\n"
  157. "{\n"
  158. "float4 st = stump[i].st;\n"
  159. "__global const OptHaarFeature* f = optfeatures + as_int(st.x);\n"
  160. "float4 weight = f->weight;\n"
  161. "int4 ofs = f->ofs[0];\n"
  162. "sval = (psum[ofs.x] - psum[ofs.y] - psum[ofs.z] + psum[ofs.w])*weight.x;\n"
  163. "ofs = f->ofs[1];\n"
  164. "sval = mad((float)(psum[ofs.x] - psum[ofs.y] - psum[ofs.z] + psum[ofs.w]), weight.y, sval);\n"
  165. "if( weight.z > 0 )\n"
  166. "{\n"
  167. "ofs = f->ofs[2];\n"
  168. "sval = mad((float)(psum[ofs.x] - psum[ofs.y] - psum[ofs.z] + psum[ofs.w]), weight.z, sval);\n"
  169. "}\n"
  170. "s += (sval < st.y*nf) ? st.z : st.w;\n"
  171. "}\n"
  172. "stump += ntrees;\n"
  173. "#else\n"
  174. "for( i = 0; i < ntrees; i++, node += NODE_COUNT, leaves += NODE_COUNT+1 )\n"
  175. "{\n"
  176. "int idx = 0;\n"
  177. "do\n"
  178. "{\n"
  179. "int4 n = node[idx].n;\n"
  180. "__global const OptHaarFeature* f = optfeatures + n.x;\n"
  181. "float4 weight = f->weight;\n"
  182. "int4 ofs = f->ofs[0];\n"
  183. "sval = (psum[ofs.x] - psum[ofs.y] - psum[ofs.z] + psum[ofs.w])*weight.x;\n"
  184. "ofs = f->ofs[1];\n"
  185. "sval = mad((float)(psum[ofs.x] - psum[ofs.y] - psum[ofs.z] + psum[ofs.w]), weight.y, sval);\n"
  186. "if( weight.z > 0 )\n"
  187. "{\n"
  188. "ofs = f->ofs[2];\n"
  189. "sval = mad((float)(psum[ofs.x] - psum[ofs.y] - psum[ofs.z] + psum[ofs.w]), weight.z, sval);\n"
  190. "}\n"
  191. "idx = (sval < as_float(n.y)*nf) ? n.z : n.w;\n"
  192. "}\n"
  193. "while(idx > 0);\n"
  194. "s += leaves[-idx];\n"
  195. "}\n"
  196. "#endif\n"
  197. "if( s < stages[stageIdx].threshold )\n"
  198. "break;\n"
  199. "}\n"
  200. "if( stageIdx == SPLIT_STAGE && (ystep == 1 || ((ix | iy) & 1) == 0) )\n"
  201. "{\n"
  202. "int count = atomic_inc(lcount);\n"
  203. "lbuf[count] = (int)(ix | (iy << 8));\n"
  204. "lnf[count] = nf;\n"
  205. "}\n"
  206. "}\n"
  207. "for( stageIdx = SPLIT_STAGE; stageIdx < N_STAGES; stageIdx++ )\n"
  208. "{\n"
  209. "barrier(CLK_LOCAL_MEM_FENCE);\n"
  210. "int nrects = lcount[0];\n"
  211. "if( nrects == 0 )\n"
  212. "break;\n"
  213. "barrier(CLK_LOCAL_MEM_FENCE);\n"
  214. "if( lidx == 0 )\n"
  215. "lcount[0] = 0;\n"
  216. "{\n"
  217. "#if NODE_COUNT == 1\n"
  218. "__global const Stump* stump = (__global const Stump*)nodes + stages[stageIdx].first;\n"
  219. "#else\n"
  220. "__global const Node* node = nodes + stages[stageIdx].first*NODE_COUNT;\n"
  221. "__global const float* leaves = leaves0 + stages[stageIdx].first*(NODE_COUNT+1);\n"
  222. "#endif\n"
  223. "int nparts = LOCAL_SIZE / nrects;\n"
  224. "int ntrees = stages[stageIdx].ntrees;\n"
  225. "int ntrees_p = (ntrees + nparts - 1)/nparts;\n"
  226. "int nr = lidx / nparts;\n"
  227. "int partidx = -1, idxval = 0;\n"
  228. "float partsum = 0.f, nf = 0.f;\n"
  229. "if( nr < nrects )\n"
  230. "{\n"
  231. "partidx = lidx % nparts;\n"
  232. "idxval = lbuf[nr];\n"
  233. "nf = lnf[nr];\n"
  234. "{\n"
  235. "int ntrees0 = ntrees_p*partidx;\n"
  236. "int ntrees1 = min(ntrees0 + ntrees_p, ntrees);\n"
  237. "int ix1 = idxval & 255, iy1 = idxval >> 8;\n"
  238. "#if SUM_BUF_SIZE > 0\n"
  239. "__local const int* psum = ibuf + mad24(iy1, SUM_BUF_STEP, ix1);\n"
  240. "#else\n"
  241. "__global const int* psum = psum0 + mad24(iy1, sumstep, ix1);\n"
  242. "#endif\n"
  243. "#if NODE_COUNT == 1\n"
  244. "for( i = ntrees0; i < ntrees1; i++ )\n"
  245. "{\n"
  246. "float4 st = stump[i].st;\n"
  247. "__global const OptHaarFeature* f = optfeatures + as_int(st.x);\n"
  248. "float4 weight = f->weight;\n"
  249. "int4 ofs = f->ofs[0];\n"
  250. "float sval = (psum[ofs.x] - psum[ofs.y] - psum[ofs.z] + psum[ofs.w])*weight.x;\n"
  251. "ofs = f->ofs[1];\n"
  252. "sval = mad((float)(psum[ofs.x] - psum[ofs.y] - psum[ofs.z] + psum[ofs.w]), weight.y, sval);\n"
  253. "if( fabs(weight.z) > 0 )\n"
  254. "{\n"
  255. "ofs = f->ofs[2];\n"
  256. "sval = mad((float)(psum[ofs.x] - psum[ofs.y] - psum[ofs.z] + psum[ofs.w]), weight.z, sval);\n"
  257. "}\n"
  258. "partsum += (sval < st.y*nf) ? st.z : st.w;\n"
  259. "}\n"
  260. "#else\n"
  261. "for( i = ntrees0; i < ntrees1; i++ )\n"
  262. "{\n"
  263. "int idx = 0;\n"
  264. "do\n"
  265. "{\n"
  266. "int4 n = node[i*2 + idx].n;\n"
  267. "__global const OptHaarFeature* f = optfeatures + n.x;\n"
  268. "float4 weight = f->weight;\n"
  269. "int4 ofs = f->ofs[0];\n"
  270. "float sval = (psum[ofs.x] - psum[ofs.y] - psum[ofs.z] + psum[ofs.w])*weight.x;\n"
  271. "ofs = f->ofs[1];\n"
  272. "sval = mad((float)(psum[ofs.x] - psum[ofs.y] - psum[ofs.z] + psum[ofs.w]), weight.y, sval);\n"
  273. "if( weight.z > 0 )\n"
  274. "{\n"
  275. "ofs = f->ofs[2];\n"
  276. "sval = mad((float)(psum[ofs.x] - psum[ofs.y] - psum[ofs.z] + psum[ofs.w]), weight.z, sval);\n"
  277. "}\n"
  278. "idx = (sval < as_float(n.y)*nf) ? n.z : n.w;\n"
  279. "}\n"
  280. "while(idx > 0);\n"
  281. "partsum += leaves[i*3-idx];\n"
  282. "}\n"
  283. "#endif\n"
  284. "}\n"
  285. "}\n"
  286. "lpartsum[lidx] = partsum;\n"
  287. "barrier(CLK_LOCAL_MEM_FENCE);\n"
  288. "if( partidx == 0 )\n"
  289. "{\n"
  290. "float s = lpartsum[nr*nparts];\n"
  291. "for( i = 1; i < nparts; i++ )\n"
  292. "s += lpartsum[i + nr*nparts];\n"
  293. "if( s >= stages[stageIdx].threshold )\n"
  294. "{\n"
  295. "int count = atomic_inc(lcount);\n"
  296. "lbuf[count] = idxval;\n"
  297. "lnf[count] = nf;\n"
  298. "}\n"
  299. "}\n"
  300. "}\n"
  301. "}\n"
  302. "barrier(CLK_LOCAL_MEM_FENCE);\n"
  303. "if( stageIdx == N_STAGES )\n"
  304. "{\n"
  305. "int nrects = lcount[0];\n"
  306. "if( lidx < nrects )\n"
  307. "{\n"
  308. "int nfaces = atomic_inc(facepos);\n"
  309. "if( nfaces < MAX_FACES )\n"
  310. "{\n"
  311. "volatile __global int* face = facepos + 1 + nfaces*3;\n"
  312. "int val = lbuf[lidx];\n"
  313. "face[0] = scaleIdx;\n"
  314. "face[1] = ix0 + (val & 255);\n"
  315. "face[2] = iy0 + (val >> 8);\n"
  316. "}\n"
  317. "}\n"
  318. "}\n"
  319. "}\n"
  320. "}\n"
  321. "}\n"
  322. "#endif\n"
  323. "#ifdef LBP\n"
  324. "#undef CALC_SUM_OFS_\n"
  325. "#define CALC_SUM_OFS_(p0, p1, p2, p3, ptr) \\\n"
  326. "((ptr)[p0] - (ptr)[p1] - (ptr)[p2] + (ptr)[p3])\n"
  327. "__kernel void runLBPClassifierStumpSimple(\n"
  328. "int nscales, __global const ScaleData* scaleData,\n"
  329. "__global const int* sum,\n"
  330. "int _sumstep, int sumoffset,\n"
  331. "__global const OptLBPFeature* optfeatures,\n"
  332. "__global const Stage* stages,\n"
  333. "__global const Stump* stumps,\n"
  334. "__global const int* bitsets,\n"
  335. "int bitsetSize,\n"
  336. "volatile __global int* facepos,\n"
  337. "int2 windowsize)\n"
  338. "{\n"
  339. "int lx = get_local_id(0);\n"
  340. "int ly = get_local_id(1);\n"
  341. "int local_size_x = get_local_size(0);\n"
  342. "int local_size_y = get_local_size(1);\n"
  343. "int groupIdx = get_group_id(1)*get_num_groups(0) + get_group_id(0);\n"
  344. "int ngroups = get_num_groups(0)*get_num_groups(1);\n"
  345. "int scaleIdx, tileIdx, stageIdx;\n"
  346. "int sumstep = (int)(_sumstep/sizeof(int));\n"
  347. "for( scaleIdx = nscales-1; scaleIdx >= 0; scaleIdx-- )\n"
  348. "{\n"
  349. "__global const ScaleData* s = scaleData + scaleIdx;\n"
  350. "int ystep = s->ystep;\n"
  351. "int2 worksize = (int2)(max(s->szi_width - windowsize.x, 0), max(s->szi_height - windowsize.y, 0));\n"
  352. "int2 ntiles = (int2)((worksize.x/ystep + local_size_x-1)/local_size_x,\n"
  353. "(worksize.y/ystep + local_size_y-1)/local_size_y);\n"
  354. "int totalTiles = ntiles.x*ntiles.y;\n"
  355. "for( tileIdx = groupIdx; tileIdx < totalTiles; tileIdx += ngroups )\n"
  356. "{\n"
  357. "int iy = mad24((tileIdx / ntiles.x), local_size_y, ly) * ystep;\n"
  358. "int ix = mad24((tileIdx % ntiles.x), local_size_x, lx) * ystep;\n"
  359. "if( ix < worksize.x && iy < worksize.y )\n"
  360. "{\n"
  361. "__global const int* p = sum + mad24(iy, sumstep, ix) + s->layer_ofs;\n"
  362. "__global const Stump* stump = stumps;\n"
  363. "__global const int* bitset = bitsets;\n"
  364. "for( stageIdx = 0; stageIdx < N_STAGES; stageIdx++ )\n"
  365. "{\n"
  366. "int i, ntrees = stages[stageIdx].ntrees;\n"
  367. "float s = 0.f;\n"
  368. "for( i = 0; i < ntrees; i++, stump++, bitset += bitsetSize )\n"
  369. "{\n"
  370. "float4 st = stump->st;\n"
  371. "__global const OptLBPFeature* f = optfeatures + as_int(st.x);\n"
  372. "int16 ofs = f->ofs;\n"
  373. "int cval = CALC_SUM_OFS_( ofs.s5, ofs.s6, ofs.s9, ofs.sa, p );\n"
  374. "int mask, idx = (CALC_SUM_OFS_( ofs.s0, ofs.s1, ofs.s4, ofs.s5, p ) >= cval ? 4 : 0);\n"
  375. "idx |= (CALC_SUM_OFS_( ofs.s1, ofs.s2, ofs.s5, ofs.s6, p ) >= cval ? 2 : 0);\n"
  376. "idx |= (CALC_SUM_OFS_( ofs.s2, ofs.s3, ofs.s6, ofs.s7, p ) >= cval ? 1 : 0);\n"
  377. "mask = (CALC_SUM_OFS_( ofs.s6, ofs.s7, ofs.sa, ofs.sb, p ) >= cval ? 16 : 0);\n"
  378. "mask |= (CALC_SUM_OFS_( ofs.sa, ofs.sb, ofs.se, ofs.sf, p ) >= cval ? 8 : 0);\n"
  379. "mask |= (CALC_SUM_OFS_( ofs.s9, ofs.sa, ofs.sd, ofs.se, p ) >= cval ? 4 : 0);\n"
  380. "mask |= (CALC_SUM_OFS_( ofs.s8, ofs.s9, ofs.sc, ofs.sd, p ) >= cval ? 2 : 0);\n"
  381. "mask |= (CALC_SUM_OFS_( ofs.s4, ofs.s5, ofs.s8, ofs.s9, p ) >= cval ? 1 : 0);\n"
  382. "s += (bitset[idx] & (1 << mask)) ? st.z : st.w;\n"
  383. "}\n"
  384. "if( s < stages[stageIdx].threshold )\n"
  385. "break;\n"
  386. "}\n"
  387. "if( stageIdx == N_STAGES )\n"
  388. "{\n"
  389. "int nfaces = atomic_inc(facepos);\n"
  390. "if( nfaces < MAX_FACES )\n"
  391. "{\n"
  392. "volatile __global int* face = facepos + 1 + nfaces*3;\n"
  393. "face[0] = scaleIdx;\n"
  394. "face[1] = ix;\n"
  395. "face[2] = iy;\n"
  396. "}\n"
  397. "}\n"
  398. "}\n"
  399. "}\n"
  400. "}\n"
  401. "}\n"
  402. "__kernel __attribute__((reqd_work_group_size(LOCAL_SIZE_X,LOCAL_SIZE_Y,1)))\n"
  403. "void runLBPClassifierStump(\n"
  404. "int nscales, __global const ScaleData* scaleData,\n"
  405. "__global const int* sum,\n"
  406. "int _sumstep, int sumoffset,\n"
  407. "__global const OptLBPFeature* optfeatures,\n"
  408. "__global const Stage* stages,\n"
  409. "__global const Stump* stumps,\n"
  410. "__global const int* bitsets,\n"
  411. "int bitsetSize,\n"
  412. "volatile __global int* facepos,\n"
  413. "int2 windowsize)\n"
  414. "{\n"
  415. "int lx = get_local_id(0);\n"
  416. "int ly = get_local_id(1);\n"
  417. "int groupIdx = get_group_id(0);\n"
  418. "int i, ngroups = get_global_size(0)/LOCAL_SIZE_X;\n"
  419. "int scaleIdx, tileIdx, stageIdx;\n"
  420. "int sumstep = (int)(_sumstep/sizeof(int));\n"
  421. "int lidx = ly*LOCAL_SIZE_X + lx;\n"
  422. "#define LOCAL_SIZE (LOCAL_SIZE_X*LOCAL_SIZE_Y)\n"
  423. "__local int lstore[SUM_BUF_SIZE + LOCAL_SIZE*3/2+1];\n"
  424. "#if SUM_BUF_SIZE > 0\n"
  425. "__local int* ibuf = lstore;\n"
  426. "__local int* lcount = ibuf + SUM_BUF_SIZE;\n"
  427. "#else\n"
  428. "__local int* lcount = lstore;\n"
  429. "#endif\n"
  430. "__local float* lpartsum = (__local float*)(lcount + 1);\n"
  431. "__local short* lbuf = (__local short*)(lpartsum + LOCAL_SIZE);\n"
  432. "for( scaleIdx = nscales-1; scaleIdx >= 0; scaleIdx-- )\n"
  433. "{\n"
  434. "__global const ScaleData* s = scaleData + scaleIdx;\n"
  435. "int ystep = s->ystep;\n"
  436. "int2 worksize = (int2)(max(s->szi_width - windowsize.x, 0), max(s->szi_height - windowsize.y, 0));\n"
  437. "int2 ntiles = (int2)((worksize.x + LOCAL_SIZE_X-1)/LOCAL_SIZE_X,\n"
  438. "(worksize.y + LOCAL_SIZE_Y-1)/LOCAL_SIZE_Y);\n"
  439. "int totalTiles = ntiles.x*ntiles.y;\n"
  440. "for( tileIdx = groupIdx; tileIdx < totalTiles; tileIdx += ngroups )\n"
  441. "{\n"
  442. "int ix0 = (tileIdx % ntiles.x)*LOCAL_SIZE_X;\n"
  443. "int iy0 = (tileIdx / ntiles.x)*LOCAL_SIZE_Y;\n"
  444. "int ix = lx, iy = ly;\n"
  445. "__global const int* psum0 = sum + mad24(iy0, sumstep, ix0) + s->layer_ofs;\n"
  446. "if( ix0 >= worksize.x || iy0 >= worksize.y )\n"
  447. "continue;\n"
  448. "#if SUM_BUF_SIZE > 0\n"
  449. "for( i = lidx*4; i < SUM_BUF_SIZE; i += LOCAL_SIZE_X*LOCAL_SIZE_Y*4 )\n"
  450. "{\n"
  451. "int dy = i/SUM_BUF_STEP, dx = i - dy*SUM_BUF_STEP;\n"
  452. "vstore4(vload4(0, psum0 + mad24(dy, sumstep, dx)), 0, ibuf+i);\n"
  453. "}\n"
  454. "barrier(CLK_LOCAL_MEM_FENCE);\n"
  455. "#endif\n"
  456. "if( lidx == 0 )\n"
  457. "lcount[0] = 0;\n"
  458. "barrier(CLK_LOCAL_MEM_FENCE);\n"
  459. "if( ix0 + ix < worksize.x && iy0 + iy < worksize.y )\n"
  460. "{\n"
  461. "__global const Stump* stump = stumps;\n"
  462. "__global const int* bitset = bitsets;\n"
  463. "#if SUM_BUF_SIZE > 0\n"
  464. "__local const int* p = ibuf + mad24(iy, SUM_BUF_STEP, ix);\n"
  465. "#else\n"
  466. "__global const int* p = psum0 + mad24(iy, sumstep, ix);\n"
  467. "#endif\n"
  468. "for( stageIdx = 0; stageIdx < SPLIT_STAGE; stageIdx++ )\n"
  469. "{\n"
  470. "int ntrees = stages[stageIdx].ntrees;\n"
  471. "float s = 0.f;\n"
  472. "for( i = 0; i < ntrees; i++, stump++, bitset += bitsetSize )\n"
  473. "{\n"
  474. "float4 st = stump->st;\n"
  475. "__global const OptLBPFeature* f = optfeatures + as_int(st.x);\n"
  476. "int16 ofs = f->ofs;\n"
  477. "int cval = CALC_SUM_OFS_( ofs.s5, ofs.s6, ofs.s9, ofs.sa, p );\n"
  478. "int mask, idx = (CALC_SUM_OFS_( ofs.s0, ofs.s1, ofs.s4, ofs.s5, p ) >= cval ? 4 : 0);\n"
  479. "idx |= (CALC_SUM_OFS_( ofs.s1, ofs.s2, ofs.s5, ofs.s6, p ) >= cval ? 2 : 0);\n"
  480. "idx |= (CALC_SUM_OFS_( ofs.s2, ofs.s3, ofs.s6, ofs.s7, p ) >= cval ? 1 : 0);\n"
  481. "mask = (CALC_SUM_OFS_( ofs.s6, ofs.s7, ofs.sa, ofs.sb, p ) >= cval ? 16 : 0);\n"
  482. "mask |= (CALC_SUM_OFS_( ofs.sa, ofs.sb, ofs.se, ofs.sf, p ) >= cval ? 8 : 0);\n"
  483. "mask |= (CALC_SUM_OFS_( ofs.s9, ofs.sa, ofs.sd, ofs.se, p ) >= cval ? 4 : 0);\n"
  484. "mask |= (CALC_SUM_OFS_( ofs.s8, ofs.s9, ofs.sc, ofs.sd, p ) >= cval ? 2 : 0);\n"
  485. "mask |= (CALC_SUM_OFS_( ofs.s4, ofs.s5, ofs.s8, ofs.s9, p ) >= cval ? 1 : 0);\n"
  486. "s += (bitset[idx] & (1 << mask)) ? st.z : st.w;\n"
  487. "}\n"
  488. "if( s < stages[stageIdx].threshold )\n"
  489. "break;\n"
  490. "}\n"
  491. "if( stageIdx == SPLIT_STAGE && (ystep == 1 || ((ix | iy) & 1) == 0) )\n"
  492. "{\n"
  493. "int count = atomic_inc(lcount);\n"
  494. "lbuf[count] = (int)(ix | (iy << 8));\n"
  495. "}\n"
  496. "}\n"
  497. "for( stageIdx = SPLIT_STAGE; stageIdx < N_STAGES; stageIdx++ )\n"
  498. "{\n"
  499. "int nrects = lcount[0];\n"
  500. "barrier(CLK_LOCAL_MEM_FENCE);\n"
  501. "if( nrects == 0 )\n"
  502. "break;\n"
  503. "if( lidx == 0 )\n"
  504. "lcount[0] = 0;\n"
  505. "{\n"
  506. "__global const Stump* stump = stumps + stages[stageIdx].first;\n"
  507. "__global const int* bitset = bitsets + stages[stageIdx].first*bitsetSize;\n"
  508. "int nparts = LOCAL_SIZE / nrects;\n"
  509. "int ntrees = stages[stageIdx].ntrees;\n"
  510. "int ntrees_p = (ntrees + nparts - 1)/nparts;\n"
  511. "int nr = lidx / nparts;\n"
  512. "int partidx = -1, idxval = 0;\n"
  513. "float partsum = 0.f, nf = 0.f;\n"
  514. "if( nr < nrects )\n"
  515. "{\n"
  516. "partidx = lidx % nparts;\n"
  517. "idxval = lbuf[nr];\n"
  518. "{\n"
  519. "int ntrees0 = ntrees_p*partidx;\n"
  520. "int ntrees1 = min(ntrees0 + ntrees_p, ntrees);\n"
  521. "int ix1 = idxval & 255, iy1 = idxval >> 8;\n"
  522. "#if SUM_BUF_SIZE > 0\n"
  523. "__local const int* p = ibuf + mad24(iy1, SUM_BUF_STEP, ix1);\n"
  524. "#else\n"
  525. "__global const int* p = psum0 + mad24(iy1, sumstep, ix1);\n"
  526. "#endif\n"
  527. "for( i = ntrees0; i < ntrees1; i++ )\n"
  528. "{\n"
  529. "float4 st = stump[i].st;\n"
  530. "__global const OptLBPFeature* f = optfeatures + as_int(st.x);\n"
  531. "int16 ofs = f->ofs;\n"
  532. "#define CALC_SUM_OFS_(p0, p1, p2, p3, ptr) \\\n"
  533. "((ptr)[p0] - (ptr)[p1] - (ptr)[p2] + (ptr)[p3])\n"
  534. "int cval = CALC_SUM_OFS_( ofs.s5, ofs.s6, ofs.s9, ofs.sa, p );\n"
  535. "int mask, idx = (CALC_SUM_OFS_( ofs.s0, ofs.s1, ofs.s4, ofs.s5, p ) >= cval ? 4 : 0);\n"
  536. "idx |= (CALC_SUM_OFS_( ofs.s1, ofs.s2, ofs.s5, ofs.s6, p ) >= cval ? 2 : 0);\n"
  537. "idx |= (CALC_SUM_OFS_( ofs.s2, ofs.s3, ofs.s6, ofs.s7, p ) >= cval ? 1 : 0);\n"
  538. "mask = (CALC_SUM_OFS_( ofs.s6, ofs.s7, ofs.sa, ofs.sb, p ) >= cval ? 16 : 0);\n"
  539. "mask |= (CALC_SUM_OFS_( ofs.sa, ofs.sb, ofs.se, ofs.sf, p ) >= cval ? 8 : 0);\n"
  540. "mask |= (CALC_SUM_OFS_( ofs.s9, ofs.sa, ofs.sd, ofs.se, p ) >= cval ? 4 : 0);\n"
  541. "mask |= (CALC_SUM_OFS_( ofs.s8, ofs.s9, ofs.sc, ofs.sd, p ) >= cval ? 2 : 0);\n"
  542. "mask |= (CALC_SUM_OFS_( ofs.s4, ofs.s5, ofs.s8, ofs.s9, p ) >= cval ? 1 : 0);\n"
  543. "partsum += (bitset[i*bitsetSize + idx] & (1 << mask)) ? st.z : st.w;\n"
  544. "}\n"
  545. "}\n"
  546. "}\n"
  547. "lpartsum[lidx] = partsum;\n"
  548. "barrier(CLK_LOCAL_MEM_FENCE);\n"
  549. "if( partidx == 0 )\n"
  550. "{\n"
  551. "float s = lpartsum[nr*nparts];\n"
  552. "for( i = 1; i < nparts; i++ )\n"
  553. "s += lpartsum[i + nr*nparts];\n"
  554. "if( s >= stages[stageIdx].threshold )\n"
  555. "{\n"
  556. "int count = atomic_inc(lcount);\n"
  557. "lbuf[count] = idxval;\n"
  558. "}\n"
  559. "}\n"
  560. "}\n"
  561. "}\n"
  562. "barrier(CLK_LOCAL_MEM_FENCE);\n"
  563. "if( stageIdx == N_STAGES )\n"
  564. "{\n"
  565. "int nrects = lcount[0];\n"
  566. "if( lidx < nrects )\n"
  567. "{\n"
  568. "int nfaces = atomic_inc(facepos);\n"
  569. "if( nfaces < MAX_FACES )\n"
  570. "{\n"
  571. "volatile __global int* face = facepos + 1 + nfaces*3;\n"
  572. "int val = lbuf[lidx];\n"
  573. "face[0] = scaleIdx;\n"
  574. "face[1] = ix0 + (val & 255);\n"
  575. "face[2] = iy0 + (val >> 8);\n"
  576. "}\n"
  577. "}\n"
  578. "}\n"
  579. "}\n"
  580. "}\n"
  581. "}\n"
  582. "#endif\n"
  583. , "91af1bca5e8f46e8426e0f3a1eb8d1f2", NULL};
  584. struct cv::ocl::internal::ProgramEntry objdetect_hog_oclsrc={moduleName, "objdetect_hog",
  585. "#define CELL_WIDTH 8\n"
  586. "#define CELL_HEIGHT 8\n"
  587. "#define CELLS_PER_BLOCK_X 2\n"
  588. "#define CELLS_PER_BLOCK_Y 2\n"
  589. "#define NTHREADS 256\n"
  590. "#define CV_PI_F M_PI_F\n"
  591. "#ifdef INTEL_DEVICE\n"
  592. "#define QANGLE_TYPE int\n"
  593. "#define QANGLE_TYPE2 int2\n"
  594. "#else\n"
  595. "#define QANGLE_TYPE uchar\n"
  596. "#define QANGLE_TYPE2 uchar2\n"
  597. "#endif\n"
  598. "__kernel void compute_hists_lut_kernel(\n"
  599. "const int cblock_stride_x, const int cblock_stride_y,\n"
  600. "const int cnbins, const int cblock_hist_size, const int img_block_width,\n"
  601. "const int blocks_in_group, const int blocks_total,\n"
  602. "const int grad_quadstep, const int qangle_step,\n"
  603. "__global const float* grad, __global const QANGLE_TYPE* qangle,\n"
  604. "__global const float* gauss_w_lut,\n"
  605. "__global float* block_hists, __local float* smem)\n"
  606. "{\n"
  607. "const int lx = get_local_id(0);\n"
  608. "const int lp = lx / 24; \n"
  609. "const int gid = get_group_id(0) * blocks_in_group + lp;\n"
  610. "const int gidY = gid / img_block_width;\n"
  611. "const int gidX = gid - gidY * img_block_width;\n"
  612. "const int lidX = lx - lp * 24;\n"
  613. "const int lidY = get_local_id(1);\n"
  614. "const int cell_x = lidX / 12;\n"
  615. "const int cell_y = lidY;\n"
  616. "const int cell_thread_x = lidX - cell_x * 12;\n"
  617. "__local float* hists = smem + lp * cnbins * (CELLS_PER_BLOCK_X *\n"
  618. "CELLS_PER_BLOCK_Y * 12 + CELLS_PER_BLOCK_X * CELLS_PER_BLOCK_Y);\n"
  619. "__local float* final_hist = hists + cnbins *\n"
  620. "(CELLS_PER_BLOCK_X * CELLS_PER_BLOCK_Y * 12);\n"
  621. "const int offset_x = gidX * cblock_stride_x + (cell_x << 2) + cell_thread_x;\n"
  622. "const int offset_y = gidY * cblock_stride_y + (cell_y << 2);\n"
  623. "__global const float* grad_ptr = (gid < blocks_total) ?\n"
  624. "grad + offset_y * grad_quadstep + (offset_x << 1) : grad;\n"
  625. "__global const QANGLE_TYPE* qangle_ptr = (gid < blocks_total) ?\n"
  626. "qangle + offset_y * qangle_step + (offset_x << 1) : qangle;\n"
  627. "__local float* hist = hists + 12 * (cell_y * CELLS_PER_BLOCK_Y + cell_x) +\n"
  628. "cell_thread_x;\n"
  629. "for (int bin_id = 0; bin_id < cnbins; ++bin_id)\n"
  630. "hist[bin_id * 48] = 0.f;\n"
  631. "const int dist_x = -4 + cell_thread_x - 4 * cell_x;\n"
  632. "const int dist_center_x = dist_x - 4 * (1 - 2 * cell_x);\n"
  633. "const int dist_y_begin = -4 - 4 * lidY;\n"
  634. "for (int dist_y = dist_y_begin; dist_y < dist_y_begin + 12; ++dist_y)\n"
  635. "{\n"
  636. "float2 vote = (float2) (grad_ptr[0], grad_ptr[1]);\n"
  637. "QANGLE_TYPE2 bin = (QANGLE_TYPE2) (qangle_ptr[0], qangle_ptr[1]);\n"
  638. "grad_ptr += grad_quadstep;\n"
  639. "qangle_ptr += qangle_step;\n"
  640. "int dist_center_y = dist_y - 4 * (1 - 2 * cell_y);\n"
  641. "int idx = (dist_center_y + 8) * 16 + (dist_center_x + 8);\n"
  642. "float gaussian = gauss_w_lut[idx];\n"
  643. "idx = (dist_y + 8) * 16 + (dist_x + 8);\n"
  644. "float interp_weight = gauss_w_lut[256+idx];\n"
  645. "hist[bin.x * 48] += gaussian * interp_weight * vote.x;\n"
  646. "hist[bin.y * 48] += gaussian * interp_weight * vote.y;\n"
  647. "}\n"
  648. "barrier(CLK_LOCAL_MEM_FENCE);\n"
  649. "volatile __local float* hist_ = hist;\n"
  650. "for (int bin_id = 0; bin_id < cnbins; ++bin_id, hist_ += 48)\n"
  651. "{\n"
  652. "if (cell_thread_x < 6)\n"
  653. "hist_[0] += hist_[6];\n"
  654. "barrier(CLK_LOCAL_MEM_FENCE);\n"
  655. "if (cell_thread_x < 3)\n"
  656. "hist_[0] += hist_[3];\n"
  657. "barrier(CLK_LOCAL_MEM_FENCE);\n"
  658. "if (cell_thread_x == 0)\n"
  659. "final_hist[(cell_x * 2 + cell_y) * cnbins + bin_id] =\n"
  660. "hist_[0] + hist_[1] + hist_[2];\n"
  661. "}\n"
  662. "barrier(CLK_LOCAL_MEM_FENCE);\n"
  663. "int tid = (cell_y * CELLS_PER_BLOCK_Y + cell_x) * 12 + cell_thread_x;\n"
  664. "if ((tid < cblock_hist_size) && (gid < blocks_total))\n"
  665. "{\n"
  666. "__global float* block_hist = block_hists +\n"
  667. "(gidY * img_block_width + gidX) * cblock_hist_size;\n"
  668. "block_hist[tid] = final_hist[tid];\n"
  669. "}\n"
  670. "}\n"
  671. "__kernel void normalize_hists_36_kernel(__global float* block_hists,\n"
  672. "const float threshold, __local float *squares)\n"
  673. "{\n"
  674. "const int tid = get_local_id(0);\n"
  675. "const int gid = get_global_id(0);\n"
  676. "const int bid = tid / 36; \n"
  677. "const int boffset = bid * 36; \n"
  678. "const int hid = tid - boffset; \n"
  679. "float elem = block_hists[gid];\n"
  680. "squares[tid] = elem * elem;\n"
  681. "barrier(CLK_LOCAL_MEM_FENCE);\n"
  682. "__local float* smem = squares + boffset;\n"
  683. "float sum = smem[hid];\n"
  684. "if (hid < 18)\n"
  685. "smem[hid] = sum = sum + smem[hid + 18];\n"
  686. "barrier(CLK_LOCAL_MEM_FENCE);\n"
  687. "if (hid < 9)\n"
  688. "smem[hid] = sum = sum + smem[hid + 9];\n"
  689. "barrier(CLK_LOCAL_MEM_FENCE);\n"
  690. "if (hid < 4)\n"
  691. "smem[hid] = sum + smem[hid + 4];\n"
  692. "barrier(CLK_LOCAL_MEM_FENCE);\n"
  693. "sum = smem[0] + smem[1] + smem[2] + smem[3] + smem[8];\n"
  694. "elem = elem / (sqrt(sum) + 3.6f);\n"
  695. "elem = min(elem, threshold);\n"
  696. "barrier(CLK_LOCAL_MEM_FENCE);\n"
  697. "squares[tid] = elem * elem;\n"
  698. "barrier(CLK_LOCAL_MEM_FENCE);\n"
  699. "sum = smem[hid];\n"
  700. "if (hid < 18)\n"
  701. "smem[hid] = sum = sum + smem[hid + 18];\n"
  702. "barrier(CLK_LOCAL_MEM_FENCE);\n"
  703. "if (hid < 9)\n"
  704. "smem[hid] = sum = sum + smem[hid + 9];\n"
  705. "barrier(CLK_LOCAL_MEM_FENCE);\n"
  706. "if (hid < 4)\n"
  707. "smem[hid] = sum + smem[hid + 4];\n"
  708. "barrier(CLK_LOCAL_MEM_FENCE);\n"
  709. "sum = smem[0] + smem[1] + smem[2] + smem[3] + smem[8];\n"
  710. "block_hists[gid] = elem / (sqrt(sum) + 1e-3f);\n"
  711. "}\n"
  712. "inline float reduce_smem(volatile __local float* smem, int size)\n"
  713. "{\n"
  714. "unsigned int tid = get_local_id(0);\n"
  715. "float sum = smem[tid];\n"
  716. "if (size >= 512) { if (tid < 256) smem[tid] = sum = sum + smem[tid + 256];\n"
  717. "barrier(CLK_LOCAL_MEM_FENCE); }\n"
  718. "if (size >= 256) { if (tid < 128) smem[tid] = sum = sum + smem[tid + 128];\n"
  719. "barrier(CLK_LOCAL_MEM_FENCE); }\n"
  720. "if (size >= 128) { if (tid < 64) smem[tid] = sum = sum + smem[tid + 64];\n"
  721. "barrier(CLK_LOCAL_MEM_FENCE); }\n"
  722. "if (size >= 64) { if (tid < 32) smem[tid] = sum = sum + smem[tid + 32];\n"
  723. "barrier(CLK_LOCAL_MEM_FENCE); }\n"
  724. "if (size >= 32) { if (tid < 16) smem[tid] = sum = sum + smem[tid + 16];\n"
  725. "barrier(CLK_LOCAL_MEM_FENCE); }\n"
  726. "if (size >= 16) { if (tid < 8) smem[tid] = sum = sum + smem[tid + 8];\n"
  727. "barrier(CLK_LOCAL_MEM_FENCE); }\n"
  728. "if (size >= 8) { if (tid < 4) smem[tid] = sum = sum + smem[tid + 4];\n"
  729. "barrier(CLK_LOCAL_MEM_FENCE); }\n"
  730. "if (size >= 4) { if (tid < 2) smem[tid] = sum = sum + smem[tid + 2];\n"
  731. "barrier(CLK_LOCAL_MEM_FENCE); }\n"
  732. "if (size >= 2) { if (tid < 1) smem[tid] = sum = sum + smem[tid + 1];\n"
  733. "barrier(CLK_LOCAL_MEM_FENCE); }\n"
  734. "return sum;\n"
  735. "}\n"
  736. "__kernel void normalize_hists_kernel(\n"
  737. "const int nthreads, const int block_hist_size, const int img_block_width,\n"
  738. "__global float* block_hists, const float threshold, __local float *squares)\n"
  739. "{\n"
  740. "const int tid = get_local_id(0);\n"
  741. "const int gidX = get_group_id(0);\n"
  742. "const int gidY = get_group_id(1);\n"
  743. "__global float* hist = block_hists + (gidY * img_block_width + gidX) *\n"
  744. "block_hist_size + tid;\n"
  745. "float elem = 0.f;\n"
  746. "if (tid < block_hist_size)\n"
  747. "elem = hist[0];\n"
  748. "squares[tid] = elem * elem;\n"
  749. "barrier(CLK_LOCAL_MEM_FENCE);\n"
  750. "float sum = reduce_smem(squares, nthreads);\n"
  751. "float scale = 1.0f / (sqrt(sum) + 0.1f * block_hist_size);\n"
  752. "elem = min(elem * scale, threshold);\n"
  753. "barrier(CLK_LOCAL_MEM_FENCE);\n"
  754. "squares[tid] = elem * elem;\n"
  755. "barrier(CLK_LOCAL_MEM_FENCE);\n"
  756. "sum = reduce_smem(squares, nthreads);\n"
  757. "scale = 1.0f / (sqrt(sum) + 1e-3f);\n"
  758. "if (tid < block_hist_size)\n"
  759. "hist[0] = elem * scale;\n"
  760. "}\n"
  761. "#define reduce_with_sync(target, sharedMemory, localMemory, tid, offset) \\\n"
  762. "if (tid < target) sharedMemory[tid] = localMemory = localMemory + sharedMemory[tid + offset]; \\\n"
  763. "barrier(CLK_LOCAL_MEM_FENCE);\n"
  764. "__kernel void classify_hists_180_kernel(\n"
  765. "const int cdescr_width, const int cdescr_height, const int cblock_hist_size,\n"
  766. "const int img_win_width, const int img_block_width,\n"
  767. "const int win_block_stride_x, const int win_block_stride_y,\n"
  768. "__global const float * block_hists, __global const float* coefs,\n"
  769. "float free_coef, float threshold, __global uchar* labels)\n"
  770. "{\n"
  771. "const int tid = get_local_id(0);\n"
  772. "const int gidX = get_group_id(0);\n"
  773. "const int gidY = get_group_id(1);\n"
  774. "__global const float* hist = block_hists + (gidY * win_block_stride_y *\n"
  775. "img_block_width + gidX * win_block_stride_x) * cblock_hist_size;\n"
  776. "float product = 0.f;\n"
  777. "for (int i = 0; i < cdescr_height; i++)\n"
  778. "{\n"
  779. "product += coefs[i * cdescr_width + tid] *\n"
  780. "hist[i * img_block_width * cblock_hist_size + tid];\n"
  781. "}\n"
  782. "__local float products[180];\n"
  783. "products[tid] = product;\n"
  784. "barrier(CLK_LOCAL_MEM_FENCE);\n"
  785. "reduce_with_sync(90, products, product, tid, 90);\n"
  786. "reduce_with_sync(45, products, product, tid, 45);\n"
  787. "reduce_with_sync(13, products, product, tid, 32);\n"
  788. "reduce_with_sync(16, products, product, tid, 16);\n"
  789. "reduce_with_sync(8, products, product, tid, 8);\n"
  790. "reduce_with_sync(4, products, product, tid, 4);\n"
  791. "reduce_with_sync(2, products, product, tid, 2);\n"
  792. "if (tid == 0){\n"
  793. "product = product + products[tid + 1];\n"
  794. "labels[gidY * img_win_width + gidX] = (product + free_coef >= threshold);\n"
  795. "}\n"
  796. "}\n"
  797. "__kernel void classify_hists_252_kernel(\n"
  798. "const int cdescr_width, const int cdescr_height, const int cblock_hist_size,\n"
  799. "const int img_win_width, const int img_block_width,\n"
  800. "const int win_block_stride_x, const int win_block_stride_y,\n"
  801. "__global const float * block_hists, __global const float* coefs,\n"
  802. "float free_coef, float threshold, __global uchar* labels)\n"
  803. "{\n"
  804. "const int tid = get_local_id(0);\n"
  805. "const int gidX = get_group_id(0);\n"
  806. "const int gidY = get_group_id(1);\n"
  807. "__global const float* hist = block_hists + (gidY * win_block_stride_y *\n"
  808. "img_block_width + gidX * win_block_stride_x) * cblock_hist_size;\n"
  809. "float product = 0.f;\n"
  810. "if (tid < cdescr_width)\n"
  811. "{\n"
  812. "for (int i = 0; i < cdescr_height; i++)\n"
  813. "product += coefs[i * cdescr_width + tid] *\n"
  814. "hist[i * img_block_width * cblock_hist_size + tid];\n"
  815. "}\n"
  816. "__local float products[NTHREADS];\n"
  817. "products[tid] = product;\n"
  818. "barrier(CLK_LOCAL_MEM_FENCE);\n"
  819. "reduce_with_sync(128, products, product, tid, 128);\n"
  820. "reduce_with_sync(64, products, product, tid, 64);\n"
  821. "reduce_with_sync(32, products, product, tid, 32);\n"
  822. "reduce_with_sync(16, products, product, tid, 16);\n"
  823. "reduce_with_sync(8, products, product, tid, 8);\n"
  824. "reduce_with_sync(4, products, product, tid, 4);\n"
  825. "reduce_with_sync(2, products, product, tid, 2);\n"
  826. "if (tid == 0){\n"
  827. "product = product + products[tid + 1];\n"
  828. "labels[gidY * img_win_width + gidX] = (product + free_coef >= threshold);\n"
  829. "}\n"
  830. "}\n"
  831. "__kernel void classify_hists_kernel(\n"
  832. "const int cdescr_size, const int cdescr_width, const int cblock_hist_size,\n"
  833. "const int img_win_width, const int img_block_width,\n"
  834. "const int win_block_stride_x, const int win_block_stride_y,\n"
  835. "__global const float * block_hists, __global const float* coefs,\n"
  836. "float free_coef, float threshold, __global uchar* labels)\n"
  837. "{\n"
  838. "const int tid = get_local_id(0);\n"
  839. "const int gidX = get_group_id(0);\n"
  840. "const int gidY = get_group_id(1);\n"
  841. "__global const float* hist = block_hists + (gidY * win_block_stride_y *\n"
  842. "img_block_width + gidX * win_block_stride_x) * cblock_hist_size;\n"
  843. "float product = 0.f;\n"
  844. "for (int i = tid; i < cdescr_size; i += NTHREADS)\n"
  845. "{\n"
  846. "int offset_y = i / cdescr_width;\n"
  847. "int offset_x = i - offset_y * cdescr_width;\n"
  848. "product += coefs[i] *\n"
  849. "hist[offset_y * img_block_width * cblock_hist_size + offset_x];\n"
  850. "}\n"
  851. "__local float products[NTHREADS];\n"
  852. "products[tid] = product;\n"
  853. "barrier(CLK_LOCAL_MEM_FENCE);\n"
  854. "reduce_with_sync(128, products, product, tid, 128);\n"
  855. "reduce_with_sync(64, products, product, tid, 64);\n"
  856. "reduce_with_sync(32, products, product, tid, 32);\n"
  857. "reduce_with_sync(16, products, product, tid, 16);\n"
  858. "reduce_with_sync(8, products, product, tid, 8);\n"
  859. "reduce_with_sync(4, products, product, tid, 4);\n"
  860. "reduce_with_sync(2, products, product, tid, 2);\n"
  861. "if (tid == 0){\n"
  862. "products[tid] = product = product + products[tid + 1];\n"
  863. "labels[gidY * img_win_width + gidX] = (product + free_coef >= threshold);\n"
  864. "}\n"
  865. "}\n"
  866. "__kernel void extract_descrs_by_rows_kernel(\n"
  867. "const int cblock_hist_size, const int descriptors_quadstep,\n"
  868. "const int cdescr_size, const int cdescr_width, const int img_block_width,\n"
  869. "const int win_block_stride_x, const int win_block_stride_y,\n"
  870. "__global const float* block_hists, __global float* descriptors)\n"
  871. "{\n"
  872. "int tid = get_local_id(0);\n"
  873. "int gidX = get_group_id(0);\n"
  874. "int gidY = get_group_id(1);\n"
  875. "__global const float* hist = block_hists + (gidY * win_block_stride_y *\n"
  876. "img_block_width + gidX * win_block_stride_x) * cblock_hist_size;\n"
  877. "__global float* descriptor = descriptors +\n"
  878. "(gidY * get_num_groups(0) + gidX) * descriptors_quadstep;\n"
  879. "for (int i = tid; i < cdescr_size; i += NTHREADS)\n"
  880. "{\n"
  881. "int offset_y = i / cdescr_width;\n"
  882. "int offset_x = i - offset_y * cdescr_width;\n"
  883. "descriptor[i] = hist[offset_y * img_block_width * cblock_hist_size + offset_x];\n"
  884. "}\n"
  885. "}\n"
  886. "__kernel void extract_descrs_by_cols_kernel(\n"
  887. "const int cblock_hist_size, const int descriptors_quadstep, const int cdescr_size,\n"
  888. "const int cnblocks_win_x, const int cnblocks_win_y, const int img_block_width,\n"
  889. "const int win_block_stride_x, const int win_block_stride_y,\n"
  890. "__global const float* block_hists, __global float* descriptors)\n"
  891. "{\n"
  892. "int tid = get_local_id(0);\n"
  893. "int gidX = get_group_id(0);\n"
  894. "int gidY = get_group_id(1);\n"
  895. "__global const float* hist = block_hists + (gidY * win_block_stride_y *\n"
  896. "img_block_width + gidX * win_block_stride_x) * cblock_hist_size;\n"
  897. "__global float* descriptor = descriptors +\n"
  898. "(gidY * get_num_groups(0) + gidX) * descriptors_quadstep;\n"
  899. "for (int i = tid; i < cdescr_size; i += NTHREADS)\n"
  900. "{\n"
  901. "int block_idx = i / cblock_hist_size;\n"
  902. "int idx_in_block = i - block_idx * cblock_hist_size;\n"
  903. "int y = block_idx / cnblocks_win_x;\n"
  904. "int x = block_idx - y * cnblocks_win_x;\n"
  905. "descriptor[(x * cnblocks_win_y + y) * cblock_hist_size + idx_in_block] =\n"
  906. "hist[(y * img_block_width + x) * cblock_hist_size + idx_in_block];\n"
  907. "}\n"
  908. "}\n"
  909. "__kernel void compute_gradients_8UC4_kernel(\n"
  910. "const int height, const int width,\n"
  911. "const int img_step, const int grad_quadstep, const int qangle_step,\n"
  912. "const __global uchar4 * img, __global float * grad, __global QANGLE_TYPE * qangle,\n"
  913. "const float angle_scale, const char correct_gamma, const int cnbins)\n"
  914. "{\n"
  915. "const int x = get_global_id(0);\n"
  916. "const int tid = get_local_id(0);\n"
  917. "const int gSizeX = get_local_size(0);\n"
  918. "const int gidY = get_group_id(1);\n"
  919. "__global const uchar4* row = img + gidY * img_step;\n"
  920. "__local float sh_row[(NTHREADS + 2) * 3];\n"
  921. "uchar4 val;\n"
  922. "if (x < width)\n"
  923. "val = row[x];\n"
  924. "else\n"
  925. "val = row[width - 2];\n"
  926. "sh_row[tid + 1] = val.x;\n"
  927. "sh_row[tid + 1 + (NTHREADS + 2)] = val.y;\n"
  928. "sh_row[tid + 1 + 2 * (NTHREADS + 2)] = val.z;\n"
  929. "if (tid == 0)\n"
  930. "{\n"
  931. "val = row[max(x - 1, 1)];\n"
  932. "sh_row[0] = val.x;\n"
  933. "sh_row[(NTHREADS + 2)] = val.y;\n"
  934. "sh_row[2 * (NTHREADS + 2)] = val.z;\n"
  935. "}\n"
  936. "if (tid == gSizeX - 1)\n"
  937. "{\n"
  938. "val = row[min(x + 1, width - 2)];\n"
  939. "sh_row[gSizeX + 1] = val.x;\n"
  940. "sh_row[gSizeX + 1 + (NTHREADS + 2)] = val.y;\n"
  941. "sh_row[gSizeX + 1 + 2 * (NTHREADS + 2)] = val.z;\n"
  942. "}\n"
  943. "barrier(CLK_LOCAL_MEM_FENCE);\n"
  944. "if (x < width)\n"
  945. "{\n"
  946. "float4 a = (float4) (sh_row[tid], sh_row[tid + (NTHREADS + 2)],\n"
  947. "sh_row[tid + 2 * (NTHREADS + 2)], 0);\n"
  948. "float4 b = (float4) (sh_row[tid + 2], sh_row[tid + 2 + (NTHREADS + 2)],\n"
  949. "sh_row[tid + 2 + 2 * (NTHREADS + 2)], 0);\n"
  950. "float4 dx;\n"
  951. "if (correct_gamma == 1)\n"
  952. "dx = sqrt(b) - sqrt(a);\n"
  953. "else\n"
  954. "dx = b - a;\n"
  955. "float4 dy = (float4) 0.f;\n"
  956. "if (gidY > 0 && gidY < height - 1)\n"
  957. "{\n"
  958. "a = convert_float4(img[(gidY - 1) * img_step + x].xyzw);\n"
  959. "b = convert_float4(img[(gidY + 1) * img_step + x].xyzw);\n"
  960. "if (correct_gamma == 1)\n"
  961. "dy = sqrt(b) - sqrt(a);\n"
  962. "else\n"
  963. "dy = b - a;\n"
  964. "}\n"
  965. "float4 mag = hypot(dx, dy);\n"
  966. "float best_dx = dx.x;\n"
  967. "float best_dy = dy.x;\n"
  968. "float mag0 = mag.x;\n"
  969. "if (mag0 < mag.y)\n"
  970. "{\n"
  971. "best_dx = dx.y;\n"
  972. "best_dy = dy.y;\n"
  973. "mag0 = mag.y;\n"
  974. "}\n"
  975. "if (mag0 < mag.z)\n"
  976. "{\n"
  977. "best_dx = dx.z;\n"
  978. "best_dy = dy.z;\n"
  979. "mag0 = mag.z;\n"
  980. "}\n"
  981. "float ang = (atan2(best_dy, best_dx) + CV_PI_F) * angle_scale - 0.5f;\n"
  982. "int hidx = (int)floor(ang);\n"
  983. "ang -= hidx;\n"
  984. "hidx = (hidx + cnbins) % cnbins;\n"
  985. "qangle[(gidY * qangle_step + x) << 1] = hidx;\n"
  986. "qangle[((gidY * qangle_step + x) << 1) + 1] = (hidx + 1) % cnbins;\n"
  987. "grad[(gidY * grad_quadstep + x) << 1] = mag0 * (1.f - ang);\n"
  988. "grad[((gidY * grad_quadstep + x) << 1) + 1] = mag0 * ang;\n"
  989. "}\n"
  990. "}\n"
  991. "__kernel void compute_gradients_8UC1_kernel(\n"
  992. "const int height, const int width,\n"
  993. "const int img_step, const int grad_quadstep, const int qangle_step,\n"
  994. "__global const uchar * img, __global float * grad, __global QANGLE_TYPE * qangle,\n"
  995. "const float angle_scale, const char correct_gamma, const int cnbins)\n"
  996. "{\n"
  997. "const int x = get_global_id(0);\n"
  998. "const int tid = get_local_id(0);\n"
  999. "const int gSizeX = get_local_size(0);\n"
  1000. "const int gidY = get_group_id(1);\n"
  1001. "__global const uchar* row = img + gidY * img_step;\n"
  1002. "__local float sh_row[NTHREADS + 2];\n"
  1003. "if (x < width)\n"
  1004. "sh_row[tid + 1] = row[x];\n"
  1005. "else\n"
  1006. "sh_row[tid + 1] = row[width - 2];\n"
  1007. "if (tid == 0)\n"
  1008. "sh_row[0] = row[max(x - 1, 1)];\n"
  1009. "if (tid == gSizeX - 1)\n"
  1010. "sh_row[gSizeX + 1] = row[min(x + 1, width - 2)];\n"
  1011. "barrier(CLK_LOCAL_MEM_FENCE);\n"
  1012. "if (x < width)\n"
  1013. "{\n"
  1014. "float dx;\n"
  1015. "if (correct_gamma == 1)\n"
  1016. "dx = sqrt(sh_row[tid + 2]) - sqrt(sh_row[tid]);\n"
  1017. "else\n"
  1018. "dx = sh_row[tid + 2] - sh_row[tid];\n"
  1019. "float dy = 0.f;\n"
  1020. "if (gidY > 0 && gidY < height - 1)\n"
  1021. "{\n"
  1022. "float a = (float) img[ (gidY + 1) * img_step + x ];\n"
  1023. "float b = (float) img[ (gidY - 1) * img_step + x ];\n"
  1024. "if (correct_gamma == 1)\n"
  1025. "dy = sqrt(a) - sqrt(b);\n"
  1026. "else\n"
  1027. "dy = a - b;\n"
  1028. "}\n"
  1029. "float mag = hypot(dx, dy);\n"
  1030. "float ang = (atan2(dy, dx) + CV_PI_F) * angle_scale - 0.5f;\n"
  1031. "int hidx = (int)floor(ang);\n"
  1032. "ang -= hidx;\n"
  1033. "hidx = (hidx + cnbins) % cnbins;\n"
  1034. "qangle[ (gidY * qangle_step + x) << 1 ] = hidx;\n"
  1035. "qangle[ ((gidY * qangle_step + x) << 1) + 1 ] = (hidx + 1) % cnbins;\n"
  1036. "grad[ (gidY * grad_quadstep + x) << 1 ] = mag * (1.f - ang);\n"
  1037. "grad[ ((gidY * grad_quadstep + x) << 1) + 1 ] = mag * ang;\n"
  1038. "}\n"
  1039. "}\n"
  1040. , "ceeb73dfe1abfee1e5ddba4466eca44f", NULL};
  1041. }}}
  1042. #endif