ufo-scale-task.c 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  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 #UfoScaleTask:kernel from
  7. * #UfoScaleTask: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. #UfoScaleTask:num-dims must be changed, if the kernel
  10. * accesses either one or three dimensional index spaces.
  11. */
  12. #ifdef __APPLE__
  13. #include <Filter/cl.h>
  14. #else
  15. #include <CL/cl.h>
  16. #endif
  17. #include "ufo-scale-task.h"
  18. struct _UfoScaleTaskPrivate {
  19. cl_kernel kernel;
  20. gfloat scale;
  21. };
  22. static void ufo_task_interface_init (UfoTaskIface *iface);
  23. static void ufo_gpu_task_interface_init (UfoGpuTaskIface *iface);
  24. G_DEFINE_TYPE_WITH_CODE (UfoScaleTask, ufo_scale_task, UFO_TYPE_TASK_NODE,
  25. G_IMPLEMENT_INTERFACE (UFO_TYPE_TASK,
  26. ufo_task_interface_init)
  27. G_IMPLEMENT_INTERFACE (UFO_TYPE_GPU_TASK,
  28. ufo_gpu_task_interface_init))
  29. #define UFO_SCALE_TASK_GET_PRIVATE(obj) (G_TYPE_INSTANCE_GET_PRIVATE((obj), UFO_TYPE_SCALE_TASK, UfoScaleTaskPrivate))
  30. enum {
  31. PROP_0,
  32. PROP_SCALE,
  33. N_PROPERTIES
  34. };
  35. static GParamSpec *properties[N_PROPERTIES] = { NULL, };
  36. UfoNode *
  37. ufo_scale_task_new (void)
  38. {
  39. return UFO_NODE (g_object_new (UFO_TYPE_SCALE_TASK, NULL));
  40. }
  41. static gboolean
  42. ufo_scale_task_process (UfoGpuTask *task,
  43. UfoBuffer **inputs,
  44. UfoBuffer *output,
  45. UfoRequisition *requisition,
  46. UfoGpuNode *node)
  47. {
  48. UfoScaleTaskPrivate *priv;
  49. cl_command_queue cmd_queue;
  50. cl_mem in_mem;
  51. cl_mem out_mem;
  52. priv = UFO_SCALE_TASK (task)->priv;
  53. cmd_queue = ufo_gpu_node_get_cmd_queue (node);
  54. in_mem = ufo_buffer_get_device_array (inputs[0], cmd_queue);
  55. out_mem = ufo_buffer_get_device_array (output, cmd_queue);
  56. UFO_RESOURCES_CHECK_CLERR (clSetKernelArg (priv->kernel, 0, sizeof (cl_mem), &in_mem));
  57. UFO_RESOURCES_CHECK_CLERR (clSetKernelArg (priv->kernel, 1, sizeof (cl_mem), &out_mem));
  58. UFO_RESOURCES_CHECK_CLERR (clSetKernelArg (priv->kernel, 2, sizeof (cl_float), &priv->scale));
  59. UFO_RESOURCES_CHECK_CLERR (clEnqueueNDRangeKernel (cmd_queue,
  60. priv->kernel,
  61. 2, NULL, requisition->dims, NULL,
  62. 0, NULL, NULL));
  63. return TRUE;
  64. }
  65. static void
  66. ufo_scale_task_get_structure (UfoTask *task,
  67. guint *n_inputs,
  68. UfoInputParam **in_params,
  69. UfoTaskMode *mode)
  70. {
  71. UfoScaleTaskPrivate *priv;
  72. priv = UFO_SCALE_TASK_GET_PRIVATE (task);
  73. *mode = UFO_TASK_MODE_SINGLE;
  74. *n_inputs = 1;
  75. *in_params = g_new0 (UfoInputParam, 1);
  76. (*in_params)[0].n_dims = 2;
  77. }
  78. static void
  79. ufo_scale_task_setup (UfoTask *task,
  80. UfoResources *resources,
  81. GError **error)
  82. {
  83. UfoScaleTaskPrivate *priv;
  84. priv = UFO_SCALE_TASK_GET_PRIVATE (task);
  85. priv->kernel = ufo_resources_get_kernel (resources,
  86. "scale.cl",
  87. "scale",
  88. error);
  89. if (priv->kernel != NULL)
  90. UFO_RESOURCES_CHECK_CLERR (clRetainKernel (priv->kernel));
  91. }
  92. static void
  93. ufo_scale_task_get_requisition (UfoTask *task,
  94. UfoBuffer **inputs,
  95. UfoRequisition *requisition)
  96. {
  97. UfoScaleTaskPrivate *priv;
  98. priv = UFO_SCALE_TASK_GET_PRIVATE (task);
  99. ufo_buffer_get_requisition (inputs[0], requisition);
  100. }
  101. static UfoNode *
  102. ufo_scale_task_copy_real (UfoNode *node,
  103. GError **error)
  104. {
  105. UfoScaleTask *orig;
  106. UfoScaleTask *copy;
  107. orig = UFO_SCALE_TASK (node);
  108. copy = UFO_SCALE_TASK (ufo_scale_task_new ());
  109. g_object_set (G_OBJECT (copy),
  110. "scale", orig->priv->scale,
  111. NULL);
  112. return UFO_NODE (copy);
  113. }
  114. static gboolean
  115. ufo_scale_task_equal_real (UfoNode *n1,
  116. UfoNode *n2)
  117. {
  118. g_return_val_if_fail (UFO_IS_SCALE_TASK (n1) && UFO_IS_SCALE_TASK (n2), FALSE);
  119. return TRUE;
  120. }
  121. static void
  122. ufo_scale_task_finalize (GObject *object)
  123. {
  124. UfoScaleTaskPrivate *priv;
  125. priv = UFO_SCALE_TASK_GET_PRIVATE (object);
  126. if (priv->kernel) {
  127. clReleaseKernel (priv->kernel);
  128. priv->kernel = NULL;
  129. }
  130. G_OBJECT_CLASS (ufo_scale_task_parent_class)->finalize (object);
  131. }
  132. static void
  133. ufo_task_interface_init (UfoTaskIface *iface)
  134. {
  135. iface->setup = ufo_scale_task_setup;
  136. iface->get_requisition = ufo_scale_task_get_requisition;
  137. iface->get_structure = ufo_scale_task_get_structure;
  138. }
  139. static void
  140. ufo_gpu_task_interface_init (UfoGpuTaskIface *iface)
  141. {
  142. iface->process = ufo_scale_task_process;
  143. }
  144. static void
  145. ufo_scale_task_set_property (GObject *object,
  146. guint property_id,
  147. const GValue *value,
  148. GParamSpec *pspec)
  149. {
  150. UfoScaleTaskPrivate *priv = UFO_SCALE_TASK_GET_PRIVATE (object);
  151. switch (property_id) {
  152. case PROP_SCALE:
  153. priv->scale = g_value_get_float (value);
  154. break;
  155. default:
  156. G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
  157. break;
  158. }
  159. }
  160. static void
  161. ufo_scale_task_get_property (GObject *object,
  162. guint property_id,
  163. GValue *value,
  164. GParamSpec *pspec)
  165. {
  166. UfoScaleTaskPrivate *priv = UFO_SCALE_TASK_GET_PRIVATE (object);
  167. switch (property_id) {
  168. case PROP_SCALE:
  169. g_value_set_float (value, priv->scale);
  170. break;
  171. default:
  172. G_OBJECT_WARN_INVALID_PROPERTY_ID(object, property_id, pspec);
  173. break;
  174. }
  175. }
  176. static void
  177. ufo_scale_task_class_init (UfoScaleTaskClass *klass)
  178. {
  179. GObjectClass *oclass;
  180. UfoNodeClass *node_class;
  181. oclass = G_OBJECT_CLASS (klass);
  182. node_class = UFO_NODE_CLASS (klass);
  183. oclass->finalize = ufo_scale_task_finalize;
  184. oclass->set_property = ufo_scale_task_set_property;
  185. oclass->get_property = ufo_scale_task_get_property;
  186. properties[PROP_SCALE] =
  187. g_param_spec_float ("scale",
  188. "Scale",
  189. "Scale for each pixel",
  190. -5.0, 10.0, 1.0,
  191. G_PARAM_READWRITE);
  192. for (guint i = PROP_0 + 1; i < N_PROPERTIES; i++)
  193. g_object_class_install_property (oclass, i, properties[i]);
  194. node_class->copy = ufo_scale_task_copy_real;
  195. node_class->equal = ufo_scale_task_equal_real;
  196. g_type_class_add_private(klass, sizeof(UfoScaleTaskPrivate));
  197. }
  198. static void
  199. ufo_scale_task_init (UfoScaleTask *self)
  200. {
  201. UfoScaleTaskPrivate *priv;
  202. self->priv = priv = UFO_SCALE_TASK_GET_PRIVATE (self);
  203. priv->kernel = NULL;
  204. }