#![allow(non_camel_case_types, deprecated)]
#![allow(clippy::not_unsafe_ptr_arg_deref, clippy::wildcard_in_or_patterns)]
pub use opencl_sys::{
cl_addressing_mode, cl_bool, cl_context, cl_filter_mode, cl_int, cl_sampler, cl_sampler_info,
cl_sampler_properties, cl_uint, cl_ulong, CL_INVALID_VALUE, CL_SAMPLER_ADDRESSING_MODE,
CL_SAMPLER_CONTEXT, CL_SAMPLER_FILTER_MODE, CL_SAMPLER_LOD_MAX, CL_SAMPLER_LOD_MIN,
CL_SAMPLER_MIP_FILTER_MODE, CL_SAMPLER_NORMALIZED_COORDS, CL_SAMPLER_PROPERTIES,
CL_SAMPLER_REFERENCE_COUNT, CL_SUCCESS,
};
use opencl_sys::{clCreateSampler, clGetSamplerInfo, clReleaseSampler, clRetainSampler};
#[cfg(feature = "CL_VERSION_2_0")]
use opencl_sys::clCreateSamplerWithProperties;
use super::info_type::InfoType;
use super::{api_info_size, api_info_value, api_info_vector};
use libc::{c_void, intptr_t, size_t};
use std::mem;
use std::ptr;
#[cfg_attr(
any(
feature = "CL_VERSION_2_0",
feature = "CL_VERSION_2_1",
feature = "CL_VERSION_2_2",
feature = "CL_VERSION_3_0"
),
deprecated(
since = "0.1.0",
note = "From CL_VERSION_2_0 use create_sampler_with_properties"
)
)]
#[inline]
pub fn create_sampler(
context: cl_context,
normalize_coords: cl_bool,
addressing_mode: cl_addressing_mode,
filter_mode: cl_filter_mode,
) -> Result<cl_sampler, cl_int> {
let mut status: cl_int = CL_INVALID_VALUE;
let sampler: cl_sampler = unsafe {
clCreateSampler(
context,
normalize_coords,
addressing_mode,
filter_mode,
&mut status,
)
};
if CL_SUCCESS != status {
Err(status)
} else {
Ok(sampler)
}
}
#[cfg(feature = "CL_VERSION_2_0")]
#[inline]
pub fn create_sampler_with_properties(
context: cl_context,
properties: *const cl_sampler_properties,
) -> Result<cl_sampler, cl_int> {
let mut status: cl_int = CL_INVALID_VALUE;
let sampler: cl_sampler =
unsafe { clCreateSamplerWithProperties(context, properties, &mut status) };
if CL_SUCCESS != status {
Err(status)
} else {
Ok(sampler)
}
}
#[inline]
pub unsafe fn retain_sampler(sampler: cl_sampler) -> Result<(), cl_int> {
let status: cl_int = clRetainSampler(sampler);
if CL_SUCCESS != status {
Err(status)
} else {
Ok(())
}
}
#[inline]
pub unsafe fn release_sampler(sampler: cl_sampler) -> Result<(), cl_int> {
let status: cl_int = clReleaseSampler(sampler);
if CL_SUCCESS != status {
Err(status)
} else {
Ok(())
}
}
pub fn get_sampler_data(
sampler: cl_sampler,
param_name: cl_sampler_info,
) -> Result<Vec<u8>, cl_int> {
api_info_size!(get_size, clGetSamplerInfo);
let size = get_size(sampler, param_name)?;
api_info_vector!(get_vector, u8, clGetSamplerInfo);
get_vector(sampler, param_name, size)
}
pub fn get_sampler_info(
sampler: cl_sampler,
param_name: cl_sampler_info,
) -> Result<InfoType, cl_int> {
match param_name {
CL_SAMPLER_REFERENCE_COUNT
| CL_SAMPLER_NORMALIZED_COORDS
| CL_SAMPLER_ADDRESSING_MODE
| CL_SAMPLER_FILTER_MODE => {
api_info_value!(get_value, cl_uint, clGetSamplerInfo);
Ok(InfoType::Uint(get_value(sampler, param_name)?))
}
CL_SAMPLER_CONTEXT => {
api_info_value!(get_value, intptr_t, clGetSamplerInfo);
Ok(InfoType::Ptr(get_value(sampler, param_name)?))
}
CL_SAMPLER_PROPERTIES => {
api_info_size!(get_size, clGetSamplerInfo);
api_info_vector!(get_vec, cl_ulong, clGetSamplerInfo);
let size = get_size(sampler, param_name)?;
Ok(InfoType::VecUlong(get_vec(sampler, param_name, size)?))
}
CL_SAMPLER_MIP_FILTER_MODE
| CL_SAMPLER_LOD_MIN
| CL_SAMPLER_LOD_MAX
| _ =>
Ok(InfoType::VecUchar(get_sampler_data(sampler, param_name)?))
}
}