cf4ocl (C Framework for OpenCL)  v2.1.0
Object-oriented framework for developing and benchmarking OpenCL projects in C/C++
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
ccl_sampler_wrapper.c
Go to the documentation of this file.
1 /*
2  * This file is part of cf4ocl (C Framework for OpenCL).
3  *
4  * cf4ocl is free software: you can redistribute it and/or modify
5  * it under the terms of the GNU Lesser General Public License as
6  * published by the Free Software Foundation, either version 3 of the
7  * License, or (at your option) any later version.
8  *
9  * cf4ocl is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  * GNU Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with cf4ocl. If not, see
16  * <http://www.gnu.org/licenses/>.
17  * */
18 
29 #include "ccl_sampler_wrapper.h"
30 #include "_ccl_abstract_wrapper.h"
31 #include "_ccl_defs.h"
32 
38 struct ccl_sampler {
39 
44  CCLWrapper base;
45 
46 };
47 
58 struct ccl_sampler_basic_properties {
59 
61  cl_bool normalized_coords;
62 
64  cl_addressing_mode addressing_mode;
65 
67  cl_filter_mode filter_mode;
68 
69 };
70 
89 static struct ccl_sampler_basic_properties
90  ccl_sampler_get_basic_properties(
91  const cl_sampler_properties *sampler_properties) {
92 
93  /* Set defaults. */
94  struct ccl_sampler_basic_properties sbp =
95  { CL_TRUE, CL_ADDRESS_CLAMP, CL_FILTER_NEAREST };
96 
97  /* Cycle through property list. */
98  if (sampler_properties != NULL) {
99  for (guint i = 0; sampler_properties[i] != 0; i = i + 2) {
100  /* Check if property is a basic known property. */
101  switch (sampler_properties[i]) {
102  case CL_SAMPLER_NORMALIZED_COORDS:
103  sbp.normalized_coords = (cl_bool) sampler_properties[i + 1];
104  break;
105  case CL_SAMPLER_ADDRESSING_MODE:
106  sbp.addressing_mode =
107  (cl_addressing_mode) sampler_properties[i + 1];
108  break;
109  case CL_SAMPLER_FILTER_MODE:
110  sbp.filter_mode =
111  (cl_filter_mode) sampler_properties[i + 1];
112  break;
113  }
114  }
115  }
116 
117  /* Return properties. */
118  return sbp;
119 
120 }
121 
138 CCL_EXPORT
139 CCLSampler* ccl_sampler_new_wrap(cl_sampler sampler) {
140 
141  return (CCLSampler*) ccl_wrapper_new(
142  CCL_SAMPLER, (void*) sampler, sizeof(CCLSampler));
143 
144 }
145 
154 CCL_EXPORT
156 
157  ccl_wrapper_unref((CCLWrapper*) smplr, sizeof(CCLSampler),
158  NULL, (ccl_wrapper_release_cl_object) clReleaseSampler, NULL);
159 
160 }
161 
181 CCL_EXPORT
182 CCLSampler* ccl_sampler_new(CCLContext* ctx, cl_bool normalized_coords,
183  cl_addressing_mode addressing_mode, cl_filter_mode filter_mode,
184  CCLErr** err) {
185 
186  const cl_sampler_properties sp[] = {
187  CL_SAMPLER_NORMALIZED_COORDS, normalized_coords,
188  CL_SAMPLER_ADDRESSING_MODE, addressing_mode,
189  CL_SAMPLER_FILTER_MODE, filter_mode,
190  0
191  };
192 
193  return ccl_sampler_new_full(ctx, sp, err);
194 }
195 
196 
228 CCL_EXPORT
230  const cl_sampler_properties *sampler_properties, CCLErr** err) {
231 
232  /* Make sure err is NULL or it is not set. */
233  g_return_val_if_fail((err) == NULL || *(err) == NULL, NULL);
234  /* Make sure ctx is not NULL. */
235  g_return_val_if_fail(ctx != NULL, NULL);
236 
237  /* New sampler wrapper object to create. */
238  CCLSampler* smplr = NULL;
239  /* OpenCL sampler object to create and wrap. */
240  cl_sampler sampler;
241  /* OpenCL function status. */
242  cl_int ocl_status;
243 
244 #ifdef CL_VERSION_2_0
245 
246  /* OpenCL platform version. */
247  double ocl_ver;
248  /* Internal error handling object. */
249  CCLErr* err_internal = NULL;
250 
251  /* Get context platform version. */
252  ocl_ver = ccl_context_get_opencl_version(ctx, &err_internal);
253  ccl_if_err_propagate_goto(err, err_internal, error_handler);
254 
255  /* Create the OpenCL sampler object. */
256  if (ocl_ver >= 200) {
257  /* Platform is OpenCL >= 2.0, use "new" API. */
258  sampler = clCreateSamplerWithProperties(
259  ccl_context_unwrap(ctx), sampler_properties, &ocl_status);
260  } else {
261  /* Platform is OpenCL <= 1.2, use "old" API. */
262  struct ccl_sampler_basic_properties sbp =
263  ccl_sampler_get_basic_properties(sampler_properties);
264  CCL_BEGIN_IGNORE_DEPRECATIONS
265  sampler = clCreateSampler(ccl_context_unwrap(ctx),
266  sbp.normalized_coords, sbp.addressing_mode, sbp.filter_mode,
267  &ocl_status);
268  CCL_END_IGNORE_DEPRECATIONS
269  }
270 
271 #else
272 
273  /* Create OpenCL sampler object. */
274  struct ccl_sampler_basic_properties sbp =
275  ccl_sampler_get_basic_properties(sampler_properties);
276  sampler = clCreateSampler(ccl_context_unwrap(ctx),
277  sbp.normalized_coords, sbp.addressing_mode, sbp.filter_mode,
278  &ocl_status);
279 
280 #endif
281 
282  /* Check for errors. */
284  CL_SUCCESS != ocl_status, ocl_status, error_handler,
285  "%s: unable to create sampler (OpenCL error %d: %s).",
286  CCL_STRD, ocl_status, ccl_err(ocl_status));
287 
288  /* Create sampler wrapper. */
289  smplr = ccl_sampler_new_wrap(sampler);
290 
291  /* If we got here, everything is OK. */
292  g_assert(err == NULL || *err == NULL);
293  goto finish;
294 
295 error_handler:
296 
297  /* If we got here there was an error, verify that it is so. */
298  g_assert(err == NULL || *err != NULL);
299 
300 finish:
301 
302  /* Return sampler wrapper. */
303  return smplr;
304 
305 
306 }
307 
308 
CCLSampler * ccl_sampler_new_full(CCLContext *ctx, const cl_sampler_properties *sampler_properties, CCLErr **err)
Create a new sampler wrapper object using a list of properties.
#define CCL_OCL_ERROR
Resolves to error category identifying string, in this case an error in the OpenCL library...
Definition: ccl_common.h:324
#define ccl_if_err_create_goto(err, quark, error_condition, error_code, label, msg,...)
If error is detected (error_code != no_error_code), create an error object (CCLErr) and go to the spe...
Definition: _ccl_defs.h:91
#define ccl_context_unwrap(ctx)
Get the OpenCL context object.
Useful definitions used internally by cf4ocl.
The context wrapper class.
#define ccl_if_err_propagate_goto(err_dest, err_src, label)
Same as ccl_if_err_goto(), but rethrows error in a source CCLErr object to a new destination CCLErr o...
Definition: _ccl_defs.h:120
const char * ccl_err(int code)
Convert OpenCL error code to a readable string.
Definition: ccl_errors.c:118
cl_uint ccl_context_get_opencl_version(CCLContext *ctx, CCLErr **err)
Get the OpenCL version of the platform associated with this context.
Definition of a wrapper class and its methods for OpenCL sampler objects.
void ccl_sampler_destroy(CCLSampler *smplr)
Decrements the reference count of the wrapper object.
CCLSampler * ccl_sampler_new(CCLContext *ctx, cl_bool normalized_coords, cl_addressing_mode addressing_mode, cl_filter_mode filter_mode, CCLErr **err)
Create a new sampler wrapper object by specifying a basic set of sampler properties.
Base class for all OpenCL wrappers.
Sampler wrapper class.
Sampler object.
Definition: ccl_common.h:110
GError CCLErr
Error handling class.
Definition: ccl_common.h:291
CCLSampler * ccl_sampler_new_wrap(cl_sampler sampler)
Get the sampler wrapper for the given OpenCL sampler.