ufo-scale-task.c 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  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. G_DEFINE_TYPE_WITH_CODE (UfoScaleTask, ufo_scale_task, UFO_TYPE_TASK_NODE,
  24. G_IMPLEMENT_INTERFACE (UFO_TYPE_TASK,
  25. ufo_task_interface_init))
  26. #define UFO_SCALE_TASK_GET_PRIVATE(obj) (G_TYPE_INSTANCE_GET_PRIVATE((obj), UFO_TYPE_SCALE_TASK, UfoScaleTaskPrivate))
  27. enum {
  28. PROP_0,
  29. PROP_SCALE,
  30. N_PROPERTIES
  31. };
  32. static GParamSpec *properties[N_PROPERTIES] = { NULL, };
  33. UfoNode *
  34. ufo_scale_task_new (void)
  35. {
  36. return UFO_NODE (g_object_new (UFO_TYPE_SCALE_TASK, NULL));
  37. }
  38. static gboolean
  39. ufo_scale_task_process (UfoTask *task,
  40. UfoBuffer **inputs,
  41. UfoBuffer *output,
  42. UfoRequisition *requisition)
  43. {
  44. UfoScaleTaskPrivate *priv;
  45. UfoGpuNode *node;
  46. cl_command_queue cmd_queue;
  47. cl_mem in_mem;
  48. cl_mem out_mem;
  49. priv = UFO_SCALE_TASK (task)->priv;
  50. node = UFO_GPU_NODE (ufo_task_node_get_proc_node (UFO_TASK_NODE (task)));
  51. cmd_queue = ufo_gpu_node_get_cmd_queue (node);
  52. in_mem = ufo_buffer_get_device_array (inputs[0], cmd_queue);
  53. out_mem = ufo_buffer_get_device_array (output, cmd_queue);
  54. UFO_RESOURCES_CHECK_CLERR (clSetKernelArg (priv->kernel, 0, sizeof (cl_mem), &in_mem));
  55. UFO_RESOURCES_CHECK_CLERR (clSetKernelArg (priv->kernel, 1, sizeof (cl_mem), &out_mem));
  56. UFO_RESOURCES_CHECK_CLERR (clSetKernelArg (priv->kernel, 2, sizeof (cl_float), &priv->scale));
  57. UFO_RESOURCES_CHECK_CLERR (clEnqueueNDRangeKernel (cmd_queue,
  58. priv->kernel,
  59. 2, NULL, requisition->dims, NULL,
  60. 0, NULL, NULL));
  61. return TRUE;
  62. }
  63. static guint
  64. ufo_scale_task_get_num_inputs (UfoTask *task)
  65. {
  66. return 1;
  67. }
  68. static guint
  69. ufo_scale_task_get_num_dimensions (UfoTask *task,
  70. guint input)
  71. {
  72. g_return_val_if_fail (input == 0, 0);
  73. return 2;
  74. }
  75. static UfoTaskMode
  76. ufo_scale_task_get_mode (UfoTask *task)
  77. {
  78. return UFO_TASK_MODE_PROCESSOR | UFO_TASK_MODE_GPU;
  79. }
  80. static void
  81. ufo_scale_task_setup (UfoTask *task,
  82. UfoResources *resources,
  83. GError **error)
  84. {
  85. UfoScaleTaskPrivate *priv;
  86. priv = UFO_SCALE_TASK_GET_PRIVATE (task);
  87. priv->kernel = ufo_resources_get_kernel (resources,
  88. "scale.cl",
  89. "scale",
  90. error);
  91. if (priv->kernel != NULL)
  92. UFO_RESOURCES_CHECK_CLERR (clRetainKernel (priv->kernel));
  93. }
  94. static void
  95. ufo_scale_task_get_requisition (UfoTask *task,
  96. UfoBuffer **inputs,
  97. UfoRequisition *requisition)
  98. {
  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_num_inputs = ufo_scale_task_get_num_inputs;
  138. iface->get_num_dimensions = ufo_scale_task_get_num_dimensions;
  139. iface->get_mode = ufo_scale_task_get_mode;
  140. iface->process = ufo_scale_task_process;
  141. }
  142. static void
  143. ufo_scale_task_set_property (GObject *object,
  144. guint property_id,
  145. const GValue *value,
  146. GParamSpec *pspec)
  147. {
  148. UfoScaleTaskPrivate *priv = UFO_SCALE_TASK_GET_PRIVATE (object);
  149. switch (property_id) {
  150. case PROP_SCALE:
  151. priv->scale = g_value_get_float (value);
  152. break;
  153. default:
  154. G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
  155. break;
  156. }
  157. }
  158. static void
  159. ufo_scale_task_get_property (GObject *object,
  160. guint property_id,
  161. GValue *value,
  162. GParamSpec *pspec)
  163. {
  164. UfoScaleTaskPrivate *priv = UFO_SCALE_TASK_GET_PRIVATE (object);
  165. switch (property_id) {
  166. case PROP_SCALE:
  167. g_value_set_float (value, priv->scale);
  168. break;
  169. default:
  170. G_OBJECT_WARN_INVALID_PROPERTY_ID(object, property_id, pspec);
  171. break;
  172. }
  173. }
  174. static void
  175. ufo_scale_task_class_init (UfoScaleTaskClass *klass)
  176. {
  177. GObjectClass *oclass;
  178. UfoNodeClass *node_class;
  179. oclass = G_OBJECT_CLASS (klass);
  180. node_class = UFO_NODE_CLASS (klass);
  181. oclass->finalize = ufo_scale_task_finalize;
  182. oclass->set_property = ufo_scale_task_set_property;
  183. oclass->get_property = ufo_scale_task_get_property;
  184. properties[PROP_SCALE] =
  185. g_param_spec_float ("scale",
  186. "Scale",
  187. "Scale for each pixel",
  188. -5.0, 10.0, 1.0,
  189. G_PARAM_READWRITE);
  190. for (guint i = PROP_0 + 1; i < N_PROPERTIES; i++)
  191. g_object_class_install_property (oclass, i, properties[i]);
  192. node_class->copy = ufo_scale_task_copy_real;
  193. node_class->equal = ufo_scale_task_equal_real;
  194. g_type_class_add_private(klass, sizeof(UfoScaleTaskPrivate));
  195. }
  196. static void
  197. ufo_scale_task_init (UfoScaleTask *self)
  198. {
  199. UfoScaleTaskPrivate *priv;
  200. self->priv = priv = UFO_SCALE_TASK_GET_PRIVATE (self);
  201. priv->kernel = NULL;
  202. }