ufo-anka-padding-task.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404
  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. #ifdef __APPLE__
  20. #include <OpenCL/cl.h>
  21. #else
  22. #include <CL/cl.h>
  23. #endif
  24. #include "ufo-anka-padding-task.h"
  25. /**
  26. * SECTION:ufo-anka-padding-task
  27. * @Short_description: Pad images to some extent
  28. * @Title: anka_padding
  29. *
  30. */
  31. struct _UfoAnkaPaddingTaskPrivate {
  32. /* OpenCL */
  33. cl_context context;
  34. cl_kernel kernel;
  35. cl_sampler sampler;
  36. /* properties */
  37. guint width, height;
  38. gint x, y;
  39. cl_addressing_mode addressing_mode;
  40. };
  41. static void ufo_task_interface_init (UfoTaskIface *iface);
  42. G_DEFINE_TYPE_WITH_CODE (UfoAnkaPaddingTask, ufo_anka_padding_task, UFO_TYPE_TASK_NODE,
  43. G_IMPLEMENT_INTERFACE (UFO_TYPE_TASK,
  44. ufo_task_interface_init))
  45. #define UFO_ANKA_PADDING_TASK_GET_PRIVATE(obj) (G_TYPE_INSTANCE_GET_PRIVATE((obj), UFO_TYPE_ANKA_PADDING_TASK, UfoAnkaPaddingTaskPrivate))
  46. enum {
  47. PROP_0,
  48. PROP_WIDTH,
  49. PROP_HEIGHT,
  50. PROP_X,
  51. PROP_Y,
  52. PROP_ADDRESSING_MODE,
  53. N_PROPERTIES
  54. };
  55. static GParamSpec *properties[N_PROPERTIES] = { NULL, };
  56. static void
  57. change_sampler (UfoAnkaPaddingTaskPrivate *priv)
  58. {
  59. cl_int err;
  60. if (priv->sampler) {
  61. UFO_RESOURCES_CHECK_CLERR (clReleaseSampler (priv->sampler));
  62. }
  63. priv->sampler = clCreateSampler (priv->context,
  64. (cl_bool) TRUE,
  65. priv->addressing_mode,
  66. CL_FILTER_NEAREST,
  67. &err);
  68. UFO_RESOURCES_CHECK_CLERR (err);
  69. }
  70. UfoNode *
  71. ufo_anka_padding_task_new (void)
  72. {
  73. return UFO_NODE (g_object_new (UFO_TYPE_ANKA_PADDING_TASK, NULL));
  74. }
  75. static void
  76. ufo_anka_padding_task_setup (UfoTask *task,
  77. UfoResources *resources,
  78. GError **error)
  79. {
  80. UfoAnkaPaddingTaskPrivate *priv;
  81. priv = UFO_ANKA_PADDING_TASK_GET_PRIVATE (task);
  82. priv->context = ufo_resources_get_context (resources);
  83. priv->kernel = ufo_resources_get_kernel (resources, "ankapadding.cl", "pad", error);
  84. change_sampler (priv);
  85. UFO_RESOURCES_CHECK_CLERR (clRetainContext (priv->context));
  86. if (priv->kernel) {
  87. UFO_RESOURCES_CHECK_CLERR (clRetainKernel (priv->kernel));
  88. }
  89. }
  90. static void
  91. ufo_anka_padding_task_get_requisition (UfoTask *task,
  92. UfoBuffer **inputs,
  93. UfoRequisition *requisition)
  94. {
  95. UfoAnkaPaddingTaskPrivate *priv;
  96. UfoRequisition in_req;
  97. priv = UFO_ANKA_PADDING_TASK_GET_PRIVATE (task);
  98. ufo_buffer_get_requisition (inputs[0], &in_req);
  99. /* if width and height are not set make them as large as input */
  100. if (!priv->width) {
  101. priv->width = (guint) in_req.dims[0];
  102. }
  103. if (!priv->height) {
  104. priv->height = (guint) in_req.dims[1];
  105. }
  106. requisition->n_dims = 2;
  107. requisition->dims[0] = priv->width;
  108. requisition->dims[1] = priv->height;
  109. }
  110. static guint
  111. ufo_anka_padding_task_get_num_inputs (UfoTask *task)
  112. {
  113. return 1;
  114. }
  115. static guint
  116. ufo_anka_padding_task_get_num_dimensions (UfoTask *task,
  117. guint input)
  118. {
  119. g_return_val_if_fail (input == 0, 0);
  120. return 2;
  121. }
  122. static UfoTaskMode
  123. ufo_anka_padding_task_get_mode (UfoTask *task)
  124. {
  125. return UFO_TASK_MODE_PROCESSOR | UFO_TASK_MODE_GPU;
  126. }
  127. static gboolean
  128. ufo_anka_padding_task_equal_real (UfoNode *n1,
  129. UfoNode *n2)
  130. {
  131. g_return_val_if_fail (UFO_IS_ANKA_PADDING_TASK (n2) && UFO_IS_ANKA_PADDING_TASK (n2), FALSE);
  132. return TRUE;
  133. }
  134. static gboolean
  135. ufo_anka_padding_task_process (UfoTask *task,
  136. UfoBuffer **inputs,
  137. UfoBuffer *output,
  138. UfoRequisition *requisition)
  139. {
  140. UfoAnkaPaddingTaskPrivate *priv;
  141. UfoGpuNode *node;
  142. UfoProfiler *profiler;
  143. UfoRequisition in_req;
  144. cl_command_queue cmd_queue;
  145. cl_mem in_image, out_mem;
  146. cl_addressing_mode current_addressing_mode;
  147. guint input_shape[2];
  148. gint offset[2];
  149. priv = UFO_ANKA_PADDING_TASK_GET_PRIVATE (task);
  150. ufo_buffer_get_requisition (inputs[0], &in_req);
  151. node = UFO_GPU_NODE (ufo_task_node_get_proc_node (UFO_TASK_NODE (task)));
  152. cmd_queue = ufo_gpu_node_get_cmd_queue (node);
  153. out_mem = ufo_buffer_get_device_array (output, cmd_queue);
  154. /* change sampler mode if necessary */
  155. UFO_RESOURCES_CHECK_CLERR (clGetSamplerInfo (priv->sampler,
  156. CL_SAMPLER_ADDRESSING_MODE,
  157. sizeof (cl_addressing_mode),
  158. &current_addressing_mode,
  159. NULL));
  160. if (priv->addressing_mode != current_addressing_mode) {
  161. change_sampler (priv);
  162. }
  163. input_shape[0] = in_req.dims[0] - 1;
  164. input_shape[1] = in_req.dims[1] - 1;
  165. offset[0] = priv->x;
  166. offset[1] = priv->y;
  167. in_image = ufo_buffer_get_device_image (inputs[0], cmd_queue);
  168. UFO_RESOURCES_CHECK_CLERR (clSetKernelArg (priv->kernel, 0, sizeof (cl_mem), &in_image));
  169. UFO_RESOURCES_CHECK_CLERR (clSetKernelArg (priv->kernel, 1, sizeof (cl_sampler), &priv->sampler));
  170. UFO_RESOURCES_CHECK_CLERR (clSetKernelArg (priv->kernel, 2, sizeof (cl_mem), &out_mem));
  171. UFO_RESOURCES_CHECK_CLERR (clSetKernelArg (priv->kernel, 3, sizeof (cl_uint2), input_shape));
  172. UFO_RESOURCES_CHECK_CLERR (clSetKernelArg (priv->kernel, 4, sizeof (cl_int2), offset));
  173. profiler = ufo_task_node_get_profiler (UFO_TASK_NODE (task));
  174. ufo_profiler_call (profiler, cmd_queue, priv->kernel, 2, requisition->dims, NULL);
  175. return TRUE;
  176. }
  177. static void
  178. ufo_anka_padding_task_set_property (GObject *object,
  179. guint property_id,
  180. const GValue *value,
  181. GParamSpec *pspec)
  182. {
  183. UfoAnkaPaddingTaskPrivate *priv = UFO_ANKA_PADDING_TASK_GET_PRIVATE (object);
  184. switch (property_id) {
  185. case PROP_WIDTH:
  186. priv->width = g_value_get_uint (value);
  187. break;
  188. case PROP_HEIGHT:
  189. priv->height = g_value_get_uint (value);
  190. break;
  191. case PROP_X:
  192. priv->x = g_value_get_int (value);
  193. break;
  194. case PROP_Y:
  195. priv->y = g_value_get_int (value);
  196. break;
  197. case PROP_ADDRESSING_MODE:
  198. if (!g_strcmp0 (g_value_get_string (value), "none")) {
  199. priv->addressing_mode = CL_ADDRESS_NONE;
  200. }
  201. else if (!g_strcmp0 (g_value_get_string (value), "clamp")) {
  202. priv->addressing_mode = CL_ADDRESS_CLAMP;
  203. }
  204. else if (!g_strcmp0 (g_value_get_string (value), "clamp_to_edge")) {
  205. priv->addressing_mode = CL_ADDRESS_CLAMP_TO_EDGE;
  206. }
  207. else if (!g_strcmp0 (g_value_get_string (value), "repeat")) {
  208. priv->addressing_mode = CL_ADDRESS_REPEAT;
  209. } else {
  210. g_log ("Ufo", G_LOG_LEVEL_CRITICAL,
  211. "Error <%s:%i>: Invalid addressing mode \"%s\", "\
  212. "it has to be one of [\"none\", \"clamp\", \"clamp_to_edge\", \"repeat\"]",
  213. __FILE__,
  214. __LINE__,
  215. g_value_get_string (value));
  216. }
  217. break;
  218. default:
  219. G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
  220. break;
  221. }
  222. }
  223. static void
  224. ufo_anka_padding_task_get_property (GObject *object,
  225. guint property_id,
  226. GValue *value,
  227. GParamSpec *pspec)
  228. {
  229. UfoAnkaPaddingTaskPrivate *priv = UFO_ANKA_PADDING_TASK_GET_PRIVATE (object);
  230. switch (property_id) {
  231. case PROP_WIDTH:
  232. g_value_set_uint (value, priv->width);
  233. break;
  234. case PROP_HEIGHT:
  235. g_value_set_uint (value, priv->height);
  236. break;
  237. case PROP_X:
  238. g_value_set_int (value, priv->x);
  239. break;
  240. case PROP_Y:
  241. g_value_set_int (value, priv->y);
  242. break;
  243. case PROP_ADDRESSING_MODE:
  244. switch (priv->addressing_mode) {
  245. case CL_ADDRESS_NONE:
  246. g_value_set_string (value, "none");
  247. break;
  248. case CL_ADDRESS_CLAMP:
  249. g_value_set_string (value, "clamp");
  250. break;
  251. case CL_ADDRESS_CLAMP_TO_EDGE:
  252. g_value_set_string (value, "clamp_to_edge");
  253. break;
  254. case CL_ADDRESS_REPEAT:
  255. g_value_set_string (value, "repeat");
  256. break;
  257. }
  258. break;
  259. default:
  260. G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
  261. break;
  262. }
  263. }
  264. static void
  265. ufo_anka_padding_task_finalize (GObject *object)
  266. {
  267. UfoAnkaPaddingTaskPrivate *priv;
  268. priv = UFO_ANKA_PADDING_TASK_GET_PRIVATE (object);
  269. if (priv->kernel) {
  270. UFO_RESOURCES_CHECK_CLERR (clReleaseKernel (priv->kernel));
  271. priv->kernel = NULL;
  272. }
  273. if (priv->context) {
  274. UFO_RESOURCES_CHECK_CLERR (clReleaseContext (priv->context));
  275. priv->context = NULL;
  276. }
  277. if (priv->sampler) {
  278. UFO_RESOURCES_CHECK_CLERR (clReleaseSampler (priv->sampler));
  279. priv->sampler = NULL;
  280. }
  281. G_OBJECT_CLASS (ufo_anka_padding_task_parent_class)->finalize (object);
  282. }
  283. static void
  284. ufo_task_interface_init (UfoTaskIface *iface)
  285. {
  286. iface->setup = ufo_anka_padding_task_setup;
  287. iface->get_num_inputs = ufo_anka_padding_task_get_num_inputs;
  288. iface->get_num_dimensions = ufo_anka_padding_task_get_num_dimensions;
  289. iface->get_mode = ufo_anka_padding_task_get_mode;
  290. iface->get_requisition = ufo_anka_padding_task_get_requisition;
  291. iface->process = ufo_anka_padding_task_process;
  292. }
  293. static void
  294. ufo_anka_padding_task_class_init (UfoAnkaPaddingTaskClass *klass)
  295. {
  296. GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
  297. UfoNodeClass *node_class = UFO_NODE_CLASS (klass);
  298. gobject_class->set_property = ufo_anka_padding_task_set_property;
  299. gobject_class->get_property = ufo_anka_padding_task_get_property;
  300. gobject_class->finalize = ufo_anka_padding_task_finalize;
  301. properties[PROP_WIDTH] =
  302. g_param_spec_uint ("width",
  303. "Padded width",
  304. "Padded width",
  305. 0, +8192, 0,
  306. G_PARAM_READWRITE);
  307. properties[PROP_HEIGHT] =
  308. g_param_spec_uint ("height",
  309. "Padded height",
  310. "Padded height",
  311. 0, +8192, 0,
  312. G_PARAM_READWRITE);
  313. properties[PROP_X] =
  314. g_param_spec_int ("x",
  315. "X start index",
  316. "X index for input's 0th column",
  317. -8192, +8192, 0,
  318. G_PARAM_READWRITE);
  319. properties[PROP_Y] =
  320. g_param_spec_int ("y",
  321. "Y start index",
  322. "Y index for input's 0th row",
  323. -8192, +8192, 0,
  324. G_PARAM_READWRITE);
  325. properties[PROP_ADDRESSING_MODE] =
  326. g_param_spec_string ("addressing-mode",
  327. "Outlier treatment",
  328. "Outlier treatment from : \"none\", \"clamp\", \"clamp_to_edge\", \"repeat\"",
  329. "clamp",
  330. G_PARAM_READWRITE);
  331. for (guint i = PROP_0 + 1; i < N_PROPERTIES; i++)
  332. g_object_class_install_property (gobject_class, i, properties[i]);
  333. node_class->equal = ufo_anka_padding_task_equal_real;
  334. g_type_class_add_private (gobject_class, sizeof(UfoAnkaPaddingTaskPrivate));
  335. }
  336. static void
  337. ufo_anka_padding_task_init(UfoAnkaPaddingTask *self)
  338. {
  339. self->priv = UFO_ANKA_PADDING_TASK_GET_PRIVATE(self);
  340. self->priv->width = 0;
  341. self->priv->height = 0;
  342. self->priv->x = 0;
  343. self->priv->y = 0;
  344. self->priv->addressing_mode = CL_ADDRESS_CLAMP;
  345. }