/** * Tests creation (using "simple" constructor), getting info from and * destruction of sampler wrapper objects. * */ static void create_info_destroy_test() { /* Test variables. */ CCLContext* ctx = NULL; CCLSampler* s = NULL; cl_sampler sampler = NULL; GError* err = NULL; cl_int ocl_status; const cl_sampler_properties sampler_properties[] = { CL_SAMPLER_NORMALIZED_COORDS, CL_TRUE, CL_SAMPLER_ADDRESSING_MODE, CL_ADDRESS_NONE, CL_SAMPLER_FILTER_MODE, CL_FILTER_NEAREST, 0}; /* Get the test context with the pre-defined device. */ ctx = ccl_test_context_new(&err); g_assert_no_error(err); /* Test three ways to create a sampler. */ for (cl_uint i = 0; i < 3; ++i) { /* Create sampler wrapper. */ switch (i) { case 0: /* Create sampler using "simple" constructor. */ s = ccl_sampler_new(ctx, CL_TRUE, CL_ADDRESS_NONE, CL_FILTER_NEAREST, &err); g_assert_no_error(err); break; case 1: /* Using the "full" constructor. */ s = ccl_sampler_new_full(ctx, sampler_properties, &err); g_assert_no_error(err); break; case 2: /* Using the "wrap" constructor. */ CCL_BEGIN_IGNORE_DEPRECATIONS sampler = clCreateSampler(ccl_context_unwrap(ctx), CL_TRUE, CL_ADDRESS_NONE, CL_FILTER_NEAREST, &ocl_status); g_assert_cmpint(ocl_status, ==, CL_SUCCESS); CCL_END_IGNORE_DEPRECATIONS s = ccl_sampler_new_wrap(sampler); g_assert_cmphex(GPOINTER_TO_UINT(sampler), ==, GPOINTER_TO_UINT(ccl_sampler_unwrap(s))); break; } /* Get some info and check if the return value is as expected. */ cl_addressing_mode am; am = ccl_sampler_get_info_scalar( s, CL_SAMPLER_ADDRESSING_MODE, cl_addressing_mode, &err); g_assert_no_error(err); g_assert_cmpuint(am, ==, CL_ADDRESS_NONE); cl_filter_mode fm; fm = ccl_sampler_get_info_scalar( s, CL_SAMPLER_FILTER_MODE, cl_filter_mode, &err); g_assert_no_error(err); g_assert_cmpuint(fm, ==, CL_FILTER_NEAREST); cl_bool nc; nc = ccl_sampler_get_info_scalar( s, CL_SAMPLER_NORMALIZED_COORDS, cl_bool, &err); g_assert_no_error(err); g_assert_cmpuint(nc, ==, CL_TRUE); cl_context context; context = ccl_sampler_get_info_scalar( s, CL_SAMPLER_CONTEXT, cl_context, &err); g_assert_no_error(err); g_assert_cmphex(GPOINTER_TO_UINT(context), ==, GPOINTER_TO_UINT(ccl_context_unwrap(ctx))); /* Destroy sampler. */ ccl_sampler_destroy(s); } /* Destroy context. */ ccl_context_destroy(ctx); /* Confirm that memory allocated by wrappers has been properly * freed. */ g_assert(ccl_wrapper_memcheck()); }
/** * Create a new sampler wrapper object using a list of properties. * * If a supported property is not specified, a default value is used. * Some valid properties are `CL_SAMPLER_NORMALIZED_COORDS` (default * value is `CL_TRUE`), `CL_SAMPLER_ADDRESSING_MODE` (default value is * `CL_ADDRESS_CLAMP`) and `CL_SAMPLER_FILTER_MODE` (default value is * `CL_FILTER_NEAREST`). * * This function mimicks the style of the OpenCL 2.0 sampler * constructor, clCreateSamplerWithProperties(), but can be used with * any version of OpenCL. Thus, The underlying OpenCL sampler object is * created using: * * * clCreateSampler() - for platforms with OpenCL version <= 1.2 * * clCreateSamplerWithProperties() - for platforms with OpenCL version * >= 2.0. * * @public @memberof ccl_sampler * * @param[in] ctx A context wrapper object. * @param[in] sampler_properties A list of sampler property names and * their corresponding values. Each sampler property name is immediately * followed by the corresponding desired value. The list is terminated * with 0. If a supported property is not specified, its default value * will be used. If `NULL`, default values for supported sampler * properties will be used. * @param[out] err Return location for a ::CCLErr object, or `NULL` if error * reporting is to be ignored. * @return A new sampler wrapper object or `NULL` if an error occurs. * */ CCL_EXPORT CCLSampler* ccl_sampler_new_full(CCLContext* ctx, const cl_sampler_properties *sampler_properties, CCLErr** err) { /* Make sure err is NULL or it is not set. */ g_return_val_if_fail((err) == NULL || *(err) == NULL, NULL); /* Make sure ctx is not NULL. */ g_return_val_if_fail(ctx != NULL, NULL); /* New sampler wrapper object to create. */ CCLSampler* smplr = NULL; /* OpenCL sampler object to create and wrap. */ cl_sampler sampler; /* OpenCL function status. */ cl_int ocl_status; #ifdef CL_VERSION_2_0 /* OpenCL platform version. */ double ocl_ver; /* Internal error handling object. */ CCLErr* err_internal = NULL; /* Get context platform version. */ ocl_ver = ccl_context_get_opencl_version(ctx, &err_internal); ccl_if_err_propagate_goto(err, err_internal, error_handler); /* Create the OpenCL sampler object. */ if (ocl_ver >= 200) { /* Platform is OpenCL >= 2.0, use "new" API. */ sampler = clCreateSamplerWithProperties( ccl_context_unwrap(ctx), sampler_properties, &ocl_status); } else { /* Platform is OpenCL <= 1.2, use "old" API. */ struct ccl_sampler_basic_properties sbp = ccl_sampler_get_basic_properties(sampler_properties); CCL_BEGIN_IGNORE_DEPRECATIONS sampler = clCreateSampler(ccl_context_unwrap(ctx), sbp.normalized_coords, sbp.addressing_mode, sbp.filter_mode, &ocl_status); CCL_END_IGNORE_DEPRECATIONS } #else /* Create OpenCL sampler object. */ struct ccl_sampler_basic_properties sbp = ccl_sampler_get_basic_properties(sampler_properties); sampler = clCreateSampler(ccl_context_unwrap(ctx), sbp.normalized_coords, sbp.addressing_mode, sbp.filter_mode, &ocl_status); #endif /* Check for errors. */ ccl_if_err_create_goto(*err, CCL_OCL_ERROR, CL_SUCCESS != ocl_status, ocl_status, error_handler, "%s: unable to create sampler (OpenCL error %d: %s).", CCL_STRD, ocl_status, ccl_err(ocl_status)); /* Create sampler wrapper. */ smplr = ccl_sampler_new_wrap(sampler); /* If we got here, everything is OK. */ g_assert(err == NULL || *err == NULL); goto finish; error_handler: /* If we got here there was an error, verify that it is so. */ g_assert(err == NULL || *err != NULL); finish: /* Return sampler wrapper. */ return smplr; }