ufo-anka-backproject-task.c 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841
  1. /*
  2. * Copyright (C) 2011-2014 Karlsruhe Institute of Technology
  3. *
  4. * This file is part of Ufo.
  5. *
  6. * This library is free software: you can redistribute it and/or
  7. * modify it under the terms of the GNU Lesser General Public
  8. * License as published by the Free Software Foundation, either
  9. * version 3 of the License, or (at your option) any later version.
  10. *
  11. * This library is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * Lesser General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Lesser General Public
  17. * License along with this library. If not, see <http://www.gnu.org/licenses/>.
  18. */
  19. #include <stdio.h>
  20. #include <math.h>
  21. #include <string.h>
  22. #include <glib.h>
  23. #include <glib/gprintf.h>
  24. #ifdef __APPLE__
  25. #include <OpenCL/cl.h>
  26. #else
  27. #include <CL/cl.h>
  28. #endif
  29. #include "ufo-anka-backproject-task.h"
  30. /* Copy only neccessary projection region */
  31. /* TODO: make this a parameter? */
  32. #define COPY_PROJECTION_REGION 1
  33. #define EXTRACT_INT(region, index) g_value_get_int (g_value_array_get_nth ((region), (index)))
  34. #define EXTRACT_FLOAT(region, index) g_value_get_float (g_value_array_get_nth ((region), (index)))
  35. #define REGION_SIZE(region) ((EXTRACT_INT ((region), 2)) == 0) ? 0 : \
  36. ((EXTRACT_INT ((region), 1) - EXTRACT_INT ((region), 0) - 1) /\
  37. EXTRACT_INT ((region), 2) + 1)
  38. #define PAD_TO_DIVIDE(dividend, divisor) ((dividend) + (divisor) - (dividend) % (divisor))
  39. /**
  40. * SECTION:ufo-anka-backproject-task
  41. * @Short_description: Backproject projection by projection
  42. * @Title: anka_backproject
  43. *
  44. */
  45. struct _UfoAnkaBackprojectTaskPrivate {
  46. /* private */
  47. gboolean generated;
  48. guint count;
  49. /* sine and cosine table size based on BURST */
  50. gsize table_size;
  51. /* OpenCL */
  52. cl_context context;
  53. cl_kernel vector_kernel;
  54. cl_kernel scalar_kernel;
  55. cl_sampler sampler;
  56. /* Buffered images for invoking backprojection on BURST projections at once.
  57. * We potentially don't need to copy the last image and can use the one from
  58. * framework directly but it seems to have no performance effects. */
  59. cl_mem images[BURST];
  60. /* properties */
  61. GValueArray *x_region;
  62. GValueArray *y_region;
  63. GValueArray *z_region;
  64. GValueArray *center;
  65. GValueArray *projection_offset;
  66. float sines[BURST], cosines[BURST];
  67. guint num_projections;
  68. gfloat overall_angle;
  69. gfloat tomo_angle;
  70. gfloat lamino_angle;
  71. };
  72. static void ufo_task_interface_init (UfoTaskIface *iface);
  73. G_DEFINE_TYPE_WITH_CODE (UfoAnkaBackprojectTask, ufo_anka_backproject_task, UFO_TYPE_TASK_NODE,
  74. G_IMPLEMENT_INTERFACE (UFO_TYPE_TASK,
  75. ufo_task_interface_init))
  76. #define UFO_ANKA_BACKPROJECT_TASK_GET_PRIVATE(obj) (G_TYPE_INSTANCE_GET_PRIVATE((obj), UFO_TYPE_ANKA_BACKPROJECT_TASK, UfoAnkaBackprojectTaskPrivate))
  77. enum {
  78. PROP_0,
  79. PROP_X_REGION,
  80. PROP_Y_REGION,
  81. PROP_Z_REGION,
  82. PROP_PROJECTION_OFFSET,
  83. PROP_CENTER,
  84. PROP_NUM_PROJECTIONS,
  85. PROP_OVERALL_ANGLE,
  86. PROP_TOMO_ANGLE,
  87. PROP_LAMINO_ANGLE,
  88. N_PROPERTIES
  89. };
  90. static GParamSpec *properties[N_PROPERTIES] = { NULL, };
  91. static inline void
  92. swap (gint *first, gint *second) {
  93. gint tmp;
  94. tmp = *first;
  95. *first = *second;
  96. *second = tmp;
  97. }
  98. /**
  99. * Determine the left and right column to read from a projection at a given
  100. * tomographic angle.
  101. */
  102. static void
  103. determine_x_extrema (gfloat extrema[2], GValueArray *x_extrema, GValueArray *y_extrema,
  104. gfloat tomo_angle, gfloat x_center)
  105. {
  106. gfloat sin_tomo, cos_tomo;
  107. gint x_min, x_max, y_min, y_max;
  108. sin_tomo = sin (tomo_angle);
  109. cos_tomo = cos (tomo_angle);
  110. x_min = EXTRACT_INT (x_extrema, 0);
  111. /* The interval is right-opened when OpenCL indices for both x and y are generated, */
  112. /* so the last index doesn't count */
  113. x_max = EXTRACT_INT (x_extrema, 1) - 1;
  114. y_min = EXTRACT_INT (y_extrema, 0);
  115. y_max = EXTRACT_INT (y_extrema, 1) - 1;
  116. if (sin_tomo < 0) {
  117. swap (&y_min, &y_max);
  118. }
  119. if (cos_tomo < 0) {
  120. swap (&x_min, &x_max);
  121. }
  122. extrema[0] = cos_tomo * x_min + sin_tomo * y_min + x_center;
  123. /* +1 because extrema[1] will be accessed by interpolation
  124. * but the region in copying is right-open */
  125. extrema[1] = cos_tomo * x_max + sin_tomo * y_max + x_center + 1;
  126. }
  127. /**
  128. * Determine the top and bottom row to read from a projection at given
  129. * tomographic and laminographic angles.
  130. */
  131. static void
  132. determine_y_extrema (gfloat extrema[2], GValueArray *x_extrema, GValueArray *y_extrema,
  133. GValueArray *z_extrema, gfloat tomo_angle, gfloat lamino_angle,
  134. gfloat y_center)
  135. {
  136. gfloat sin_tomo, cos_tomo, sin_lamino, cos_lamino;
  137. gint x_min, x_max, y_min, y_max;
  138. sin_tomo = sin (tomo_angle);
  139. cos_tomo = cos (tomo_angle);
  140. sin_lamino = sin (lamino_angle);
  141. cos_lamino = cos (lamino_angle);
  142. x_min = EXTRACT_INT (x_extrema, 0);
  143. x_max = EXTRACT_INT (x_extrema, 1) - 1;
  144. y_min = EXTRACT_INT (y_extrema, 0);
  145. y_max = EXTRACT_INT (y_extrema, 1) - 1;
  146. if (sin_tomo < 0) {
  147. swap (&x_min, &x_max);
  148. }
  149. if (cos_tomo > 0) {
  150. swap (&y_min, &y_max);
  151. }
  152. extrema[0] = sin_tomo * x_min - cos_tomo * y_min;
  153. extrema[1] = sin_tomo * x_max - cos_tomo * y_max;
  154. extrema[0] = extrema[0] * cos_lamino + EXTRACT_INT (z_extrema, 0) * sin_lamino + y_center;
  155. extrema[1] = extrema[1] * cos_lamino + EXTRACT_INT (z_extrema, 1) * sin_lamino + y_center + 1;
  156. }
  157. /**
  158. * clip:
  159. * @result: resulting clipped extrema
  160. * @extrema: (min, max)
  161. * @maximum: projection width or height
  162. *
  163. * Clip extrema to an allowed interval [0, projection width/height)
  164. */
  165. static void
  166. clip (gint result[2], gfloat extrema[2], gint maximum)
  167. {
  168. result[0] = (gint) floorf (extrema[0]);
  169. result[1] = (gint) ceilf (extrema[1]);
  170. if (result[0] < 0) {
  171. result[0] = 0;
  172. }
  173. if (result[0] > maximum) {
  174. result[0] = maximum;
  175. }
  176. if (result[1] < 0) {
  177. result[1] = 0;
  178. }
  179. if (result[1] > maximum) {
  180. result[1] = maximum;
  181. }
  182. if (result[0] == result[1]) {
  183. if (result[1] < maximum) {
  184. result[1]++;
  185. } else if (result[0] > 0) {
  186. result[0]--;
  187. } else {
  188. g_warning ("Cannot extend");
  189. }
  190. } else if (result[0] > result[1]) {
  191. g_warning ("Invalid extrema: minimum larger than maximum");
  192. }
  193. }
  194. /**
  195. * Determine the left and right column to read from a projection at a given
  196. * tomographic angle. The result is bound to [0, projection width)
  197. */
  198. static void
  199. determine_x_region (gint result[2], GValueArray *x_extrema, GValueArray *y_extrema, gfloat tomo_angle,
  200. gfloat x_center, gint width)
  201. {
  202. gfloat extrema[2];
  203. determine_x_extrema (extrema, x_extrema, y_extrema, tomo_angle, x_center);
  204. clip (result, extrema, width);
  205. }
  206. /**
  207. * Determine the top and bottom column to read from a projection at given
  208. * tomographic and laminographic angles. The result is bound to
  209. * [0, projection height).
  210. */
  211. static void
  212. determine_y_region (gint result[2], GValueArray *x_extrema, GValueArray *y_extrema, GValueArray *z_extrema,
  213. gfloat tomo_angle, gfloat lamino_angle, gfloat y_center, gint height)
  214. {
  215. gfloat extrema[2];
  216. determine_y_extrema (extrema, x_extrema, y_extrema, z_extrema, tomo_angle,
  217. lamino_angle, y_center);
  218. clip (result, extrema, height);
  219. }
  220. static void
  221. set_region (GValueArray *src, GValueArray **dst)
  222. {
  223. if (EXTRACT_INT (src, 0) > EXTRACT_INT (src, 1)) {
  224. g_log ("Ufo", G_LOG_LEVEL_CRITICAL,
  225. "Error <%s:%i>: Invalid region [\"from\", \"to\", \"step\"]: [%d, %d, %d], "\
  226. "\"from\" has to be less than or equal to \"to\"",
  227. __FILE__, __LINE__,
  228. EXTRACT_INT (src, 0), EXTRACT_INT (src, 1), EXTRACT_INT (src, 2));
  229. }
  230. else {
  231. g_value_array_free (*dst);
  232. *dst = g_value_array_copy (src);
  233. }
  234. }
  235. UfoNode *
  236. ufo_anka_backproject_task_new (void)
  237. {
  238. return UFO_NODE (g_object_new (UFO_TYPE_ANKA_BACKPROJECT_TASK, NULL));
  239. }
  240. static void
  241. ufo_anka_backproject_task_setup (UfoTask *task,
  242. UfoResources *resources,
  243. GError **error)
  244. {
  245. UfoAnkaBackprojectTaskPrivate *priv;
  246. cl_int cl_error;
  247. gint i;
  248. gchar vector_kernel_name[30];
  249. if (!g_sprintf (vector_kernel_name, "backproject_burst_%d", BURST)) {
  250. g_warning ("Error making burst kernel name");
  251. }
  252. priv = UFO_ANKA_BACKPROJECT_TASK_GET_PRIVATE (task);
  253. priv->context = ufo_resources_get_context (resources);
  254. priv->vector_kernel = ufo_resources_get_kernel (resources, "ankabackprojectburst.cl",
  255. vector_kernel_name, error);
  256. priv->scalar_kernel = ufo_resources_get_kernel (resources, "ankabackprojectburst.cl",
  257. "backproject_burst_1", error);
  258. priv->sampler = clCreateSampler (priv->context,
  259. (cl_bool) FALSE,
  260. CL_ADDRESS_CLAMP,
  261. CL_FILTER_LINEAR,
  262. &cl_error);
  263. UFO_RESOURCES_CHECK_CLERR (clRetainContext (priv->context));
  264. UFO_RESOURCES_CHECK_CLERR (cl_error);
  265. if (priv->vector_kernel) {
  266. UFO_RESOURCES_CHECK_CLERR (clRetainKernel (priv->vector_kernel));
  267. }
  268. if (priv->scalar_kernel) {
  269. UFO_RESOURCES_CHECK_CLERR (clRetainKernel (priv->scalar_kernel));
  270. }
  271. for (i = 0; i < BURST; i++) {
  272. priv->images[i] = NULL;
  273. }
  274. switch (BURST) {
  275. case 1: priv->table_size = sizeof (cl_float); break;
  276. case 2: priv->table_size = sizeof (cl_float2); break;
  277. case 4: priv->table_size = sizeof (cl_float4); break;
  278. case 8: priv->table_size = sizeof (cl_float8); break;
  279. case 16: priv->table_size = sizeof (cl_float16); break;
  280. default: g_warning ("Unsupported vector size"); break;
  281. }
  282. }
  283. static void
  284. ufo_anka_backproject_task_get_requisition (UfoTask *task,
  285. UfoBuffer **inputs,
  286. UfoRequisition *requisition)
  287. {
  288. UfoAnkaBackprojectTaskPrivate *priv;
  289. priv = UFO_ANKA_BACKPROJECT_TASK_GET_PRIVATE (task);
  290. if (!priv->num_projections) {
  291. g_warning ("Number of projections has not been set");
  292. }
  293. requisition->n_dims = 3;
  294. requisition->dims[0] = REGION_SIZE (priv->x_region);
  295. requisition->dims[1] = REGION_SIZE (priv->y_region);
  296. requisition->dims[2] = REGION_SIZE (priv->z_region);
  297. }
  298. static guint
  299. ufo_anka_backproject_task_get_num_inputs (UfoTask *task)
  300. {
  301. return 1;
  302. }
  303. static guint
  304. ufo_anka_backproject_task_get_num_dimensions (UfoTask *task,
  305. guint input)
  306. {
  307. g_return_val_if_fail (input == 0, 0);
  308. return 3;
  309. }
  310. static gboolean
  311. ufo_anka_backproject_task_equal_real (UfoNode *n1,
  312. UfoNode *n2)
  313. {
  314. g_return_val_if_fail (UFO_IS_ANKA_BACKPROJECT_TASK (n1) && UFO_IS_ANKA_BACKPROJECT_TASK (n2), FALSE);
  315. return UFO_ANKA_BACKPROJECT_TASK (n1)->priv->vector_kernel == UFO_ANKA_BACKPROJECT_TASK (n2)->priv->vector_kernel;
  316. }
  317. static UfoTaskMode
  318. ufo_anka_backproject_task_get_mode (UfoTask *task)
  319. {
  320. return UFO_TASK_MODE_REDUCTOR | UFO_TASK_MODE_GPU;
  321. }
  322. static gboolean
  323. ufo_anka_backproject_task_process (UfoTask *task,
  324. UfoBuffer **inputs,
  325. UfoBuffer *output,
  326. UfoRequisition *requisition)
  327. {
  328. UfoAnkaBackprojectTaskPrivate *priv;
  329. UfoGpuNode *node;
  330. UfoProfiler *profiler;
  331. gfloat tomo_angle, *sines, *cosines;
  332. gint i, index;
  333. gint cumulate;
  334. gsize table_size;
  335. gboolean scalar;
  336. /* regions stripped off the "to" value */
  337. gfloat x_region[2], y_region[2], z_region[2], center[2], sin_lamino, cos_lamino;
  338. gint x_copy_region[2], y_copy_region[2];
  339. cl_kernel kernel;
  340. cl_command_queue cmd_queue;
  341. cl_mem image;
  342. cl_mem out_mem;
  343. cl_int cl_error;
  344. /* image creation and copying */
  345. size_t im_width, im_height;
  346. cl_image_format image_fmt;
  347. size_t origin[3];
  348. size_t region[3];
  349. /* keep the warp size satisfied but make sure the local grid is localized
  350. * around a point in 3D for efficient caching */
  351. const gint real_size[4] = {requisition->dims[0], requisition->dims[1], requisition->dims[2], 0};
  352. const gsize local_work_size[] = {16, 8, 8};
  353. gsize global_work_size[3];
  354. global_work_size[0] = requisition->dims[0] % local_work_size[0] ?
  355. PAD_TO_DIVIDE (requisition->dims[0], local_work_size[0]) :
  356. requisition->dims[0];
  357. global_work_size[1] = requisition->dims[1] % local_work_size[1] ?
  358. PAD_TO_DIVIDE (requisition->dims[1], local_work_size[1]) :
  359. requisition->dims[1];
  360. global_work_size[2] = requisition->dims[2] % local_work_size[2] ?
  361. PAD_TO_DIVIDE (requisition->dims[2], local_work_size[2]) :
  362. requisition->dims[2];
  363. priv = UFO_ANKA_BACKPROJECT_TASK (task)->priv;
  364. node = UFO_GPU_NODE (ufo_task_node_get_proc_node (UFO_TASK_NODE (task)));
  365. cmd_queue = ufo_gpu_node_get_cmd_queue (node);
  366. out_mem = ufo_buffer_get_device_array (output, cmd_queue);
  367. /* TODO: Get host/buffer/image depending on where the data *currently* resides. */
  368. /* This Must be made available in UFO core first. */
  369. image = ufo_buffer_get_device_image (inputs[0], cmd_queue);
  370. index = priv->count % BURST;
  371. tomo_angle = priv->tomo_angle > -G_MAXFLOAT ? priv->tomo_angle :
  372. priv->overall_angle * priv->count / priv->num_projections;
  373. priv->sines[index] = sin (tomo_angle);
  374. priv->cosines[index] = cos (tomo_angle);
  375. x_region[0] = (gfloat) EXTRACT_INT (priv->x_region, 0);
  376. x_region[1] = (gfloat) EXTRACT_INT (priv->x_region, 2);
  377. y_region[0] = (gfloat) EXTRACT_INT (priv->y_region, 0);
  378. y_region[1] = (gfloat) EXTRACT_INT (priv->y_region, 2);
  379. z_region[0] = (gfloat) EXTRACT_INT (priv->z_region, 0);
  380. z_region[1] = (gfloat) EXTRACT_INT (priv->z_region, 2);
  381. center[0] = EXTRACT_FLOAT (priv->center, 0) - EXTRACT_INT (priv->projection_offset, 0);
  382. center[1] = EXTRACT_FLOAT (priv->center, 1) - EXTRACT_INT (priv->projection_offset, 1);
  383. sin_lamino = sinf (priv->lamino_angle);
  384. cos_lamino = cosf (priv->lamino_angle);
  385. scalar = priv->count >= priv->num_projections / BURST * BURST ? 1 : 0;
  386. /* copy the image for further usage once BURST images arrived */
  387. clGetImageInfo (image, CL_IMAGE_WIDTH, sizeof (size_t), &im_width, NULL);
  388. clGetImageInfo (image, CL_IMAGE_HEIGHT, sizeof (size_t), &im_height, NULL);
  389. /* If COPY_PROJECTION_REGION is True we copy only the part necessary */
  390. /* for a given tomographic and laminographic angle */
  391. if (COPY_PROJECTION_REGION) {
  392. determine_x_region (x_copy_region, priv->x_region, priv->y_region, tomo_angle,
  393. EXTRACT_FLOAT (priv->center, 0), im_width);
  394. determine_y_region (y_copy_region, priv->x_region, priv->y_region, priv->z_region,
  395. tomo_angle, priv->lamino_angle, EXTRACT_FLOAT (priv->center, 1),
  396. im_height);
  397. origin[0] = x_copy_region[0];
  398. origin[1] = y_copy_region[0];
  399. origin[2] = 0;
  400. region[0] = x_copy_region[1] - x_copy_region[0];
  401. region[1] = y_copy_region[1] - y_copy_region[0];
  402. } else {
  403. origin[0] = origin[1] = origin[2] = 0;
  404. region[0] = im_width;
  405. region[1] = im_height;
  406. }
  407. region[2] = 1;
  408. if (priv->images[index] == NULL) {
  409. clGetImageInfo (image, CL_IMAGE_FORMAT, sizeof (cl_image_format), &image_fmt, NULL);
  410. /* TODO: what with the "other" API? */
  411. priv->images[index] = clCreateImage2D (priv->context,
  412. CL_MEM_READ_ONLY,
  413. &image_fmt,
  414. im_width,
  415. im_height,
  416. 0,
  417. NULL,
  418. &cl_error);
  419. UFO_RESOURCES_CHECK_CLERR (cl_error);
  420. }
  421. UFO_RESOURCES_CHECK_CLERR (clEnqueueCopyImage (cmd_queue,
  422. image,
  423. priv->images[index],
  424. origin,
  425. origin,
  426. region,
  427. 0,
  428. NULL,
  429. NULL));
  430. if (scalar) {
  431. kernel = priv->scalar_kernel;
  432. cumulate = priv->count;
  433. table_size = sizeof (cl_float);
  434. sines = &priv->sines[index];
  435. cosines = &priv->cosines[index];
  436. i = 1;
  437. UFO_RESOURCES_CHECK_CLERR (clSetKernelArg (kernel, 0, sizeof (cl_mem), &priv->images[index]));
  438. } else {
  439. kernel = priv->vector_kernel;
  440. cumulate = priv->count + 1 == BURST ? 0 : 1;
  441. table_size = priv->table_size;
  442. sines = priv->sines;
  443. cosines = priv->cosines;
  444. i = BURST;
  445. UFO_RESOURCES_CHECK_CLERR (clSetKernelArg (kernel, index, sizeof (cl_mem), &priv->images[index]));
  446. }
  447. if (scalar || index == BURST - 1) {
  448. /* Execute the kernel after BURST images have arrived, i.e. we use more
  449. * projections at one invocation, so the number of read/writes to the
  450. * result is reduced by a factor of BURST. If there are not enough
  451. * projecttions left, execute the scalar kernel */
  452. UFO_RESOURCES_CHECK_CLERR (clSetKernelArg (kernel, i++, sizeof (cl_mem), &out_mem));
  453. UFO_RESOURCES_CHECK_CLERR (clSetKernelArg (kernel, i++, sizeof (cl_sampler), &priv->sampler));
  454. UFO_RESOURCES_CHECK_CLERR (clSetKernelArg (kernel, i++, sizeof (cl_int3), real_size));
  455. UFO_RESOURCES_CHECK_CLERR (clSetKernelArg (kernel, i++, sizeof (cl_float2), center));
  456. UFO_RESOURCES_CHECK_CLERR (clSetKernelArg (kernel, i++, sizeof (cl_float2), x_region));
  457. UFO_RESOURCES_CHECK_CLERR (clSetKernelArg (kernel, i++, sizeof (cl_float2), y_region));
  458. UFO_RESOURCES_CHECK_CLERR (clSetKernelArg (kernel, i++, sizeof (cl_float2), z_region));
  459. UFO_RESOURCES_CHECK_CLERR (clSetKernelArg (kernel, i++, sizeof (cl_float), &sin_lamino));
  460. UFO_RESOURCES_CHECK_CLERR (clSetKernelArg (kernel, i++, sizeof (cl_float), &cos_lamino));
  461. UFO_RESOURCES_CHECK_CLERR (clSetKernelArg (kernel, i++, table_size, sines));
  462. UFO_RESOURCES_CHECK_CLERR (clSetKernelArg (kernel, i++, table_size, cosines));
  463. UFO_RESOURCES_CHECK_CLERR (clSetKernelArg (kernel, i, sizeof (cl_int), (cl_int *) &cumulate));
  464. profiler = ufo_task_node_get_profiler (UFO_TASK_NODE (task));
  465. ufo_profiler_call (profiler, cmd_queue, kernel, 3, global_work_size, local_work_size);
  466. }
  467. priv->count++;
  468. return TRUE;
  469. }
  470. static gboolean
  471. ufo_anka_backproject_task_generate (UfoTask *task,
  472. UfoBuffer *output,
  473. UfoRequisition *requisition)
  474. {
  475. UfoAnkaBackprojectTaskPrivate *priv;
  476. priv = UFO_ANKA_BACKPROJECT_TASK_GET_PRIVATE (task);
  477. if (priv->generated) {
  478. return FALSE;
  479. }
  480. priv->generated = TRUE;
  481. return TRUE;
  482. }
  483. static void
  484. ufo_anka_backproject_task_finalize (GObject *object)
  485. {
  486. UfoAnkaBackprojectTaskPrivate *priv;
  487. gint i;
  488. priv = UFO_ANKA_BACKPROJECT_TASK_GET_PRIVATE (object);
  489. g_value_array_free (priv->x_region);
  490. g_value_array_free (priv->y_region);
  491. g_value_array_free (priv->z_region);
  492. g_value_array_free (priv->projection_offset);
  493. g_value_array_free (priv->center);
  494. if (priv->vector_kernel) {
  495. UFO_RESOURCES_CHECK_CLERR (clReleaseKernel (priv->vector_kernel));
  496. priv->vector_kernel = NULL;
  497. }
  498. if (priv->scalar_kernel) {
  499. UFO_RESOURCES_CHECK_CLERR (clReleaseKernel (priv->scalar_kernel));
  500. priv->scalar_kernel = NULL;
  501. }
  502. if (priv->context) {
  503. UFO_RESOURCES_CHECK_CLERR (clReleaseContext (priv->context));
  504. priv->context = NULL;
  505. }
  506. if (priv->sampler) {
  507. UFO_RESOURCES_CHECK_CLERR (clReleaseSampler (priv->sampler));
  508. priv->sampler = NULL;
  509. }
  510. for (i = 0; i < BURST; i++) {
  511. if (priv->images[i] != NULL) {
  512. UFO_RESOURCES_CHECK_CLERR (clReleaseMemObject (priv->images[i]));
  513. priv->images[i] = NULL;
  514. }
  515. }
  516. G_OBJECT_CLASS (ufo_anka_backproject_task_parent_class)->finalize (object);
  517. }
  518. static void
  519. ufo_task_interface_init (UfoTaskIface *iface)
  520. {
  521. iface->setup = ufo_anka_backproject_task_setup;
  522. iface->get_requisition = ufo_anka_backproject_task_get_requisition;
  523. iface->get_num_inputs = ufo_anka_backproject_task_get_num_inputs;
  524. iface->get_num_dimensions = ufo_anka_backproject_task_get_num_dimensions;
  525. iface->get_mode = ufo_anka_backproject_task_get_mode;
  526. iface->process = ufo_anka_backproject_task_process;
  527. iface->generate = ufo_anka_backproject_task_generate;
  528. }
  529. static void
  530. ufo_anka_backproject_task_set_property (GObject *object,
  531. guint property_id,
  532. const GValue *value,
  533. GParamSpec *pspec)
  534. {
  535. UfoAnkaBackprojectTaskPrivate *priv = UFO_ANKA_BACKPROJECT_TASK_GET_PRIVATE (object);
  536. GValueArray *array;
  537. switch (property_id) {
  538. case PROP_X_REGION:
  539. array = (GValueArray *) g_value_get_boxed (value);
  540. set_region (array, &priv->x_region);
  541. break;
  542. case PROP_Y_REGION:
  543. array = (GValueArray *) g_value_get_boxed (value);
  544. set_region (array, &priv->y_region);
  545. break;
  546. case PROP_Z_REGION:
  547. array = (GValueArray *) g_value_get_boxed (value);
  548. set_region (array, &priv->z_region);
  549. break;
  550. case PROP_PROJECTION_OFFSET:
  551. array = (GValueArray *) g_value_get_boxed (value);
  552. g_value_array_free (priv->projection_offset);
  553. priv->projection_offset = g_value_array_copy (array);
  554. break;
  555. case PROP_CENTER:
  556. array = (GValueArray *) g_value_get_boxed (value);
  557. g_value_array_free (priv->center);
  558. priv->center = g_value_array_copy (array);
  559. break;
  560. case PROP_NUM_PROJECTIONS:
  561. priv->num_projections = g_value_get_uint (value);
  562. break;
  563. case PROP_OVERALL_ANGLE:
  564. priv->overall_angle = g_value_get_float (value);
  565. break;
  566. case PROP_TOMO_ANGLE:
  567. priv->tomo_angle = g_value_get_float (value);
  568. break;
  569. case PROP_LAMINO_ANGLE:
  570. priv->lamino_angle = g_value_get_float (value);
  571. break;
  572. default:
  573. G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
  574. break;
  575. }
  576. }
  577. static void
  578. ufo_anka_backproject_task_get_property (GObject *object,
  579. guint property_id,
  580. GValue *value,
  581. GParamSpec *pspec)
  582. {
  583. UfoAnkaBackprojectTaskPrivate *priv = UFO_ANKA_BACKPROJECT_TASK_GET_PRIVATE (object);
  584. switch (property_id) {
  585. case PROP_X_REGION:
  586. g_value_set_boxed (value, priv->x_region);
  587. break;
  588. case PROP_Y_REGION:
  589. g_value_set_boxed (value, priv->y_region);
  590. break;
  591. case PROP_Z_REGION:
  592. g_value_set_boxed (value, priv->z_region);
  593. break;
  594. case PROP_PROJECTION_OFFSET:
  595. g_value_set_boxed (value, priv->projection_offset);
  596. break;
  597. case PROP_CENTER:
  598. g_value_set_boxed (value, priv->center);
  599. break;
  600. case PROP_NUM_PROJECTIONS:
  601. g_value_set_uint (value, priv->num_projections);
  602. break;
  603. case PROP_OVERALL_ANGLE:
  604. g_value_set_float (value, priv->overall_angle);
  605. break;
  606. case PROP_TOMO_ANGLE:
  607. g_value_set_float (value, priv->tomo_angle);
  608. break;
  609. case PROP_LAMINO_ANGLE:
  610. g_value_set_float (value, priv->lamino_angle);
  611. break;
  612. default:
  613. G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
  614. break;
  615. }
  616. }
  617. static void
  618. ufo_anka_backproject_task_class_init (UfoAnkaBackprojectTaskClass *klass)
  619. {
  620. GObjectClass *oclass;
  621. UfoNodeClass *node_class;
  622. oclass = G_OBJECT_CLASS (klass);
  623. node_class = UFO_NODE_CLASS (klass);
  624. oclass->finalize = ufo_anka_backproject_task_finalize;
  625. oclass->set_property = ufo_anka_backproject_task_set_property;
  626. oclass->get_property = ufo_anka_backproject_task_get_property;
  627. GParamSpec *region_vals = g_param_spec_int ("region_values",
  628. "Region values",
  629. "Elements in regions",
  630. G_MININT,
  631. G_MAXINT,
  632. (gint) 0,
  633. G_PARAM_READWRITE);
  634. GParamSpec *float_region_vals = g_param_spec_float ("float_region_values",
  635. "Float Region values",
  636. "Elements in float regions",
  637. -G_MAXFLOAT,
  638. G_MAXFLOAT,
  639. 0.0f,
  640. G_PARAM_READWRITE);
  641. properties[PROP_X_REGION] =
  642. g_param_spec_value_array ("x-region",
  643. "X region for reconstruction as (from, to, step)",
  644. "X region for reconstruction as (from, to, step)",
  645. region_vals,
  646. G_PARAM_READWRITE);
  647. properties[PROP_Y_REGION] =
  648. g_param_spec_value_array ("y-region",
  649. "Y region for reconstruction as (from, to, step)",
  650. "Y region for reconstruction as (from, to, step)",
  651. region_vals,
  652. G_PARAM_READWRITE);
  653. properties[PROP_Z_REGION] =
  654. g_param_spec_value_array ("z-region",
  655. "Z region for reconstruction as (from, to, step)",
  656. "Z region for reconstruction as (from, to, step)",
  657. region_vals,
  658. G_PARAM_READWRITE);
  659. properties[PROP_PROJECTION_OFFSET] =
  660. g_param_spec_value_array ("projection-offset",
  661. "Offset to projection data as (x, y)",
  662. "Offset to projection data as (x, y) for the case input data \
  663. is cropped to the necessary range of interest",
  664. region_vals,
  665. G_PARAM_READWRITE);
  666. properties[PROP_CENTER] =
  667. g_param_spec_value_array ("center",
  668. "Center of the volume with respect to projections (x, y)",
  669. "Center of the volume with respect to projections (x, y), (rotation axes)",
  670. float_region_vals,
  671. G_PARAM_READWRITE);
  672. properties[PROP_OVERALL_ANGLE] =
  673. g_param_spec_float ("overall-angle",
  674. "Angle covered by all projections",
  675. "Angle covered by all projections (can be negative for negative steps "
  676. "in case only num-projections is specified",
  677. -G_MAXFLOAT,
  678. G_MAXFLOAT,
  679. G_PI,
  680. G_PARAM_READWRITE);
  681. properties[PROP_NUM_PROJECTIONS] =
  682. g_param_spec_uint ("num-projections",
  683. "Number of projections",
  684. "Number of projections",
  685. 0,
  686. 16384,
  687. 0,
  688. G_PARAM_READWRITE);
  689. properties[PROP_TOMO_ANGLE] =
  690. g_param_spec_float ("tomo-angle",
  691. "Tomographic rotation angle in radians",
  692. "Tomographic rotation angle in radians (used for acquiring projections)",
  693. -G_MAXFLOAT,
  694. G_MAXFLOAT,
  695. 0.0f,
  696. G_PARAM_READWRITE);
  697. properties[PROP_LAMINO_ANGLE] =
  698. g_param_spec_float ("lamino-angle",
  699. "Absolute laminogrpahic angle in radians",
  700. "Absolute laminogrpahic angle in radians determining the sample tilt",
  701. 0.0f,
  702. (float) G_PI / 2,
  703. 0.0f,
  704. G_PARAM_READWRITE);
  705. for (guint i = PROP_0 + 1; i < N_PROPERTIES; i++)
  706. g_object_class_install_property (oclass, i, properties[i]);
  707. node_class->equal = ufo_anka_backproject_task_equal_real;
  708. g_type_class_add_private (klass, sizeof(UfoAnkaBackprojectTaskPrivate));
  709. }
  710. static void
  711. ufo_anka_backproject_task_init(UfoAnkaBackprojectTask *self)
  712. {
  713. UfoAnkaBackprojectTaskPrivate *priv;
  714. self->priv = priv = UFO_ANKA_BACKPROJECT_TASK_GET_PRIVATE(self);
  715. guint i;
  716. GValue int_zero = G_VALUE_INIT;
  717. GValue float_zero = G_VALUE_INIT;
  718. g_value_init (&int_zero, G_TYPE_INT);
  719. g_value_init (&float_zero, G_TYPE_FLOAT);
  720. g_value_set_int (&int_zero, 0);
  721. g_value_set_float (&float_zero, 0.0f);
  722. self->priv->x_region = g_value_array_new (3);
  723. self->priv->y_region = g_value_array_new (3);
  724. self->priv->z_region = g_value_array_new (3);
  725. self->priv->projection_offset = g_value_array_new (2);
  726. self->priv->center = g_value_array_new (2);
  727. for (i = 0; i < 3; i++) {
  728. g_value_array_insert (self->priv->x_region, i, &int_zero);
  729. g_value_array_insert (self->priv->y_region, i, &int_zero);
  730. g_value_array_insert (self->priv->z_region, i, &int_zero);
  731. if (i < 2) {
  732. g_value_array_insert (self->priv->projection_offset, i, &int_zero);
  733. g_value_array_insert (self->priv->center, i, &float_zero);
  734. }
  735. }
  736. self->priv->num_projections = 0;
  737. self->priv->overall_angle = G_PI;
  738. self->priv->tomo_angle = -G_MAXFLOAT;
  739. self->priv->lamino_angle = 0.0f;
  740. self->priv->count = 0;
  741. self->priv->generated = FALSE;
  742. }