ufo-padding-2d-task.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438
  1. /**
  2. * SECTION:ufo-filter-task
  3. * @Short_description: Process arbitrary Filter kernels
  4. * @Title: filter
  5. *
  6. * This module is used to load an arbitrary #UfoPadding2DTask:kernel from
  7. * #UfoPadding2DTask:filename and execute it on each input. The kernel must have
  8. * only two global float array parameters, the first represents the input, the
  9. * second one the output. #UfoPadding2DTask:num-dims must be changed, if the kernel
  10. * accesses either one or three dimensional index spaces.
  11. */
  12. #ifdef __APPLE__
  13. #include <OpenCL/cl.h>
  14. #else
  15. #include <CL/cl.h>
  16. #endif
  17. #include "ufo-padding-2d-task.h"
  18. typedef enum {
  19. PADDING_ZERO = 0,
  20. PADDING_CONST,
  21. PADDING_GAVG,
  22. PADDING_BREP
  23. } PaddingMode;
  24. struct _UfoPadding2DTaskPrivate {
  25. guint in_width;
  26. guint in_height;
  27. guint out_width;
  28. guint out_height;
  29. // extent adds
  30. guint xl;
  31. guint xr;
  32. guint yt;
  33. guint yb;
  34. size_t global_work_size_small[2];
  35. size_t global_work_size_large[2];
  36. PaddingMode mode;
  37. // padding constant
  38. float pconst;
  39. cl_kernel kernel_iconst;
  40. cl_kernel kernel_cpyimg;
  41. cl_kernel kernel_brep;
  42. };
  43. static void ufo_task_interface_init (UfoTaskIface *iface);
  44. G_DEFINE_TYPE_WITH_CODE (UfoPadding2DTask, ufo_padding_2d_task, UFO_TYPE_TASK_NODE,
  45. G_IMPLEMENT_INTERFACE (UFO_TYPE_TASK,
  46. ufo_task_interface_init))
  47. #define UFO_PADDING_2D_TASK_GET_PRIVATE(obj) (G_TYPE_INSTANCE_GET_PRIVATE((obj), UFO_TYPE_PADDING_2D_TASK, UfoPadding2DTaskPrivate))
  48. enum {
  49. PROP_0,
  50. PROP_XL,
  51. PROP_XR,
  52. PROP_YT,
  53. PROP_YB,
  54. PROP_MODE,
  55. PROP_PCONST,
  56. N_PROPERTIES
  57. };
  58. static GParamSpec *properties[N_PROPERTIES] = { NULL, };
  59. UfoNode *
  60. ufo_padding_2d_task_new (void)
  61. {
  62. return UFO_NODE (g_object_new (UFO_TYPE_PADDING_2D_TASK, NULL));
  63. }
  64. static gboolean
  65. ufo_padding_2d_task_process (UfoTask *task,
  66. UfoBuffer **inputs,
  67. UfoBuffer *output,
  68. UfoRequisition *requisition)
  69. {
  70. UfoPadding2DTaskPrivate *priv;
  71. UfoGpuNode *node;
  72. cl_command_queue cmd_queue;
  73. cl_mem in_mem;
  74. cl_mem out_mem;
  75. priv = UFO_PADDING_2D_TASK (task)->priv;
  76. const PaddingMode mode = priv->mode;
  77. const guint pxl = priv->xl;
  78. const guint pyt = priv->yt;
  79. float pval = priv->pconst;
  80. const guint ixs = priv->in_width;
  81. const guint iys = priv->in_height;
  82. const guint oxs = priv->out_width;
  83. if (mode == PADDING_GAVG) {
  84. gfloat *indata = ufo_buffer_get_host_array (inputs[0], NULL);
  85. gfloat sum = 0;
  86. guint psz = ixs * iys;
  87. for (guint i =0; i < psz; i++)
  88. sum += indata[i];
  89. pval = sum / (gfloat) psz;
  90. }
  91. node = UFO_GPU_NODE (ufo_task_node_get_proc_node (UFO_TASK_NODE (task)));
  92. cmd_queue = ufo_gpu_node_get_cmd_queue (node);
  93. in_mem = ufo_buffer_get_device_array (inputs[0], cmd_queue);
  94. out_mem = ufo_buffer_get_device_array (output, cmd_queue);
  95. if ((mode == PADDING_ZERO) || (mode == PADDING_CONST) || (mode == PADDING_GAVG)) {
  96. cl_kernel k_iconst = priv->kernel_iconst;
  97. cl_kernel k_cpyimg = priv->kernel_cpyimg;
  98. /// fill with constant
  99. UFO_RESOURCES_CHECK_CLERR(clSetKernelArg( k_iconst, 0, sizeof(cl_mem), (void *) &out_mem));
  100. UFO_RESOURCES_CHECK_CLERR(clSetKernelArg( k_iconst, 1, sizeof(int), &oxs));
  101. UFO_RESOURCES_CHECK_CLERR(clSetKernelArg( k_iconst, 2, sizeof(float), &pval));
  102. UFO_RESOURCES_CHECK_CLERR(clEnqueueNDRangeKernel(cmd_queue, k_iconst,
  103. 2, NULL, priv->global_work_size_large, NULL,
  104. 0, NULL, NULL));
  105. /// copy old image
  106. UFO_RESOURCES_CHECK_CLERR(clSetKernelArg( k_cpyimg, 0, sizeof(cl_mem), (void *) &in_mem));
  107. UFO_RESOURCES_CHECK_CLERR(clSetKernelArg( k_cpyimg, 1, sizeof(cl_mem), (void *) &out_mem));
  108. UFO_RESOURCES_CHECK_CLERR(clSetKernelArg( k_cpyimg, 2, sizeof(int), &ixs));
  109. UFO_RESOURCES_CHECK_CLERR(clSetKernelArg( k_cpyimg, 3, sizeof(int), &oxs));
  110. UFO_RESOURCES_CHECK_CLERR(clSetKernelArg( k_cpyimg, 4, sizeof(int), &pxl));
  111. UFO_RESOURCES_CHECK_CLERR(clSetKernelArg( k_cpyimg, 5, sizeof(int), &pyt));
  112. UFO_RESOURCES_CHECK_CLERR(clEnqueueNDRangeKernel(cmd_queue, k_cpyimg,
  113. 2, NULL, priv->global_work_size_small, NULL,
  114. 0, NULL, NULL));
  115. }
  116. if (mode == PADDING_BREP) {
  117. cl_kernel k_brep = priv->kernel_brep;
  118. UFO_RESOURCES_CHECK_CLERR(clSetKernelArg( k_brep, 0, sizeof(cl_mem), (void *) &in_mem));
  119. UFO_RESOURCES_CHECK_CLERR(clSetKernelArg( k_brep, 1, sizeof(cl_mem), (void *) &out_mem));
  120. UFO_RESOURCES_CHECK_CLERR(clSetKernelArg( k_brep, 2, sizeof(int), &ixs));
  121. UFO_RESOURCES_CHECK_CLERR(clSetKernelArg( k_brep, 3, sizeof(int), &iys));
  122. UFO_RESOURCES_CHECK_CLERR(clSetKernelArg( k_brep, 4, sizeof(int), &oxs));
  123. UFO_RESOURCES_CHECK_CLERR(clSetKernelArg( k_brep, 5, sizeof(int), &pxl));
  124. UFO_RESOURCES_CHECK_CLERR(clSetKernelArg( k_brep, 6, sizeof(int), &pyt));
  125. UFO_RESOURCES_CHECK_CLERR(clEnqueueNDRangeKernel(cmd_queue, k_brep,
  126. 2, NULL, priv->global_work_size_large, NULL,
  127. 0, NULL, NULL));
  128. }
  129. return TRUE;
  130. }
  131. static void
  132. ufo_padding_2d_task_setup (UfoTask *task,
  133. UfoResources *resources,
  134. GError **error)
  135. {
  136. UfoPadding2DTaskPrivate *priv;
  137. priv = UFO_PADDING_2D_TASK_GET_PRIVATE (task);
  138. priv->kernel_iconst = ufo_resources_get_kernel (resources, "padding_2d.cl", "padding_2d_init_const", error);
  139. priv->kernel_cpyimg = ufo_resources_get_kernel (resources, "padding_2d.cl", "padding_2d_copy_in", error);
  140. priv->kernel_brep = ufo_resources_get_kernel (resources, "padding_2d.cl", "padding_2d_brep", error);
  141. }
  142. static void
  143. ufo_padding_2d_task_get_requisition (UfoTask *task,
  144. UfoBuffer **inputs,
  145. UfoRequisition *requisition)
  146. {
  147. UfoPadding2DTaskPrivate *priv;
  148. UfoRequisition in_req;
  149. priv = UFO_PADDING_2D_TASK_GET_PRIVATE (task);
  150. ufo_buffer_get_requisition (inputs[0], &in_req);
  151. priv->in_width = in_req.dims[0];
  152. priv->in_height = in_req.dims[1];
  153. requisition->n_dims = 2;
  154. requisition->dims[0] = priv->out_width = priv->xl + priv->in_width + priv->xr;
  155. requisition->dims[1] = priv->out_height = priv->yt + priv->in_height + priv->yb;
  156. priv->global_work_size_small[0] = (size_t) priv->in_width;
  157. priv->global_work_size_small[1] = (size_t) priv->in_height;
  158. priv->global_work_size_large[0] = requisition->dims[0];
  159. priv->global_work_size_large[1] = requisition->dims[1];
  160. }
  161. static guint
  162. ufo_padding_2d_task_get_num_inputs (UfoTask *task)
  163. {
  164. return 1;
  165. }
  166. static guint
  167. ufo_padding_2d_task_get_num_dimensions (UfoTask *task,
  168. guint input)
  169. {
  170. g_return_val_if_fail (input == 0, 0);
  171. return 2;
  172. }
  173. static UfoTaskMode
  174. ufo_padding_2d_task_get_mode (UfoTask *task)
  175. {
  176. return UFO_TASK_MODE_PROCESSOR | UFO_TASK_MODE_GPU;
  177. }
  178. static UfoNode *
  179. ufo_padding_2d_task_copy_real (UfoNode *node,
  180. GError **error)
  181. {
  182. UfoPadding2DTask *orig;
  183. UfoPadding2DTask *copy;
  184. orig = UFO_PADDING_2D_TASK (node);
  185. copy = UFO_PADDING_2D_TASK (ufo_padding_2d_task_new ());
  186. copy->priv->xl = orig->priv->xl;
  187. copy->priv->xr = orig->priv->xr;
  188. copy->priv->yb = orig->priv->yb;
  189. copy->priv->yt = orig->priv->yt;
  190. copy->priv->mode = orig->priv->mode;
  191. copy->priv->pconst = orig->priv->pconst;
  192. return UFO_NODE (copy);
  193. }
  194. static gboolean
  195. ufo_padding_2d_task_equal_real (UfoNode *n1,
  196. UfoNode *n2)
  197. {
  198. g_return_val_if_fail (UFO_IS_PADDING_2D_TASK (n1) && UFO_IS_PADDING_2D_TASK (n2), FALSE);
  199. return TRUE;
  200. }
  201. static void
  202. ufo_padding_2d_task_finalize (GObject *object)
  203. {
  204. G_OBJECT_CLASS (ufo_padding_2d_task_parent_class)->finalize (object);
  205. }
  206. static void
  207. ufo_task_interface_init (UfoTaskIface *iface)
  208. {
  209. iface->setup = ufo_padding_2d_task_setup;
  210. iface->get_requisition = ufo_padding_2d_task_get_requisition;
  211. iface->get_num_inputs = ufo_padding_2d_task_get_num_inputs;
  212. iface->get_num_dimensions = ufo_padding_2d_task_get_num_dimensions;
  213. iface->get_mode = ufo_padding_2d_task_get_mode;
  214. iface->process = ufo_padding_2d_task_process;
  215. }
  216. static void
  217. ufo_padding_2d_task_set_property (GObject *object,
  218. guint property_id,
  219. const GValue *value,
  220. GParamSpec *pspec)
  221. {
  222. UfoPadding2DTaskPrivate *priv = UFO_PADDING_2D_TASK_GET_PRIVATE (object);
  223. switch (property_id) {
  224. case PROP_XL:
  225. priv->xl = g_value_get_uint(value);
  226. break;
  227. case PROP_XR:
  228. priv->xr = g_value_get_uint(value);
  229. break;
  230. case PROP_YT:
  231. priv->yt = g_value_get_uint(value);
  232. break;
  233. case PROP_YB:
  234. priv->yb = g_value_get_uint(value);
  235. break;
  236. case PROP_MODE:
  237. if (!g_strcmp0(g_value_get_string(value), "zero"))
  238. priv->mode = PADDING_ZERO;
  239. else if (!g_strcmp0(g_value_get_string(value), "const"))
  240. priv->mode = PADDING_CONST;
  241. else if (!g_strcmp0(g_value_get_string(value), "gavg"))
  242. priv->mode = PADDING_GAVG;
  243. else if (!g_strcmp0(g_value_get_string(value), "brep"))
  244. priv->mode = PADDING_BREP;
  245. else
  246. G_OBJECT_WARN_INVALID_PROPERTY_ID(object, property_id, pspec);
  247. break;
  248. case PROP_PCONST:
  249. priv->pconst = (float) g_value_get_double(value);
  250. break;
  251. default:
  252. G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
  253. break;
  254. }
  255. }
  256. static void
  257. ufo_padding_2d_task_get_property (GObject *object,
  258. guint property_id,
  259. GValue *value,
  260. GParamSpec *pspec)
  261. {
  262. UfoPadding2DTaskPrivate *priv = UFO_PADDING_2D_TASK_GET_PRIVATE (object);
  263. switch (property_id) {
  264. case PROP_XL:
  265. g_value_set_uint(value, priv->xl);
  266. break;
  267. case PROP_XR:
  268. g_value_set_uint(value, priv->xr);
  269. break;
  270. case PROP_YT:
  271. g_value_set_uint(value, priv->yt);
  272. break;
  273. case PROP_YB:
  274. g_value_set_uint(value, priv->yb);
  275. break;
  276. case PROP_MODE:
  277. switch (priv->mode) {
  278. case PADDING_ZERO:
  279. g_value_set_string(value, "zero");
  280. break;
  281. case PADDING_CONST:
  282. g_value_set_string(value, "const");
  283. break;
  284. case PADDING_GAVG:
  285. g_value_set_string(value, "gavg");
  286. break;
  287. case PADDING_BREP:
  288. g_value_set_string(value, "brep");
  289. break;
  290. default:
  291. G_OBJECT_WARN_INVALID_PROPERTY_ID(object, property_id, pspec);
  292. break;
  293. }
  294. break;
  295. case PROP_PCONST:
  296. g_value_set_double(value, priv->pconst);
  297. break;
  298. default:
  299. G_OBJECT_WARN_INVALID_PROPERTY_ID(object, property_id, pspec);
  300. break;
  301. }
  302. }
  303. static void
  304. ufo_padding_2d_task_class_init (UfoPadding2DTaskClass *klass)
  305. {
  306. GObjectClass *oclass;
  307. UfoNodeClass *node_class;
  308. oclass = G_OBJECT_CLASS (klass);
  309. node_class = UFO_NODE_CLASS (klass);
  310. oclass->finalize = ufo_padding_2d_task_finalize;
  311. oclass->set_property = ufo_padding_2d_task_set_property;
  312. oclass->get_property = ufo_padding_2d_task_get_property;
  313. properties[PROP_XL] =
  314. g_param_spec_uint("xl",
  315. "Number of additional pixel on the left hand image side",
  316. "Number of additional pixel on the left hand image side",
  317. 0, 16384, 1,
  318. G_PARAM_READWRITE);
  319. properties[PROP_XR] =
  320. g_param_spec_uint("xr",
  321. "Number of additional pixel on the right hand image side",
  322. "Number of additional pixel on the right hand image side",
  323. 0, 16384, 1,
  324. G_PARAM_READWRITE);
  325. properties[PROP_YT] =
  326. g_param_spec_uint("yt",
  327. "Number of additional pixel on the top image side",
  328. "Number of additional pixel on the top image side",
  329. 0, 16384, 1,
  330. G_PARAM_READWRITE);
  331. properties[PROP_YB] =
  332. g_param_spec_uint("yb",
  333. "Number of additional pixel on the bottom image side",
  334. "Number of additional pixel on the bottom image side",
  335. 0, 16384, 1,
  336. G_PARAM_READWRITE);
  337. properties[PROP_MODE] =
  338. g_param_spec_string("mode",
  339. "Padding mode can be 'zero', 'const', 'gavg' or 'brep' ",
  340. "Padding mode can be 'zero', 'const', 'gavg' or 'brep' ",
  341. "zero",
  342. G_PARAM_READWRITE);
  343. properties[PROP_PCONST] =
  344. g_param_spec_double("pconst",
  345. "Padding constant",
  346. "Padding constant",
  347. -320000.0,
  348. 320000.0,
  349. 0.0,
  350. G_PARAM_READWRITE);
  351. for (guint i = PROP_0 + 1; i < N_PROPERTIES; i++)
  352. g_object_class_install_property (oclass, i, properties[i]);
  353. node_class->copy = ufo_padding_2d_task_copy_real;
  354. node_class->equal = ufo_padding_2d_task_equal_real;
  355. g_type_class_add_private(klass, sizeof(UfoPadding2DTaskPrivate));
  356. }
  357. static void
  358. ufo_padding_2d_task_init (UfoPadding2DTask *self)
  359. {
  360. UfoPadding2DTaskPrivate *priv;
  361. self->priv = priv = UFO_PADDING_2D_TASK_GET_PRIVATE (self);
  362. priv->xl = 1;
  363. priv->xr = 1;
  364. priv->yt = 1;
  365. priv->yb = 1;
  366. priv->mode = PADDING_ZERO;
  367. priv->pconst = 0.0;
  368. priv->kernel_iconst = NULL;
  369. priv->kernel_cpyimg = NULL;
  370. priv->kernel_brep = NULL;
  371. }