1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
/*
 * sampler.rs - Sampler object API wrappers (Part of OpenCL Runtime Layer).
 *
 * Copyright 2020-2021 Naman Bishnoi
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
//!
//! A sampler object describes how to sample an image when the image is read in the kernel.
//! The built-in functions to read from an image in a kernel take a sampler as an argument.
//! The sampler arguments to the image read function can be sampler objects created using
//! OpenCL functions and passed as argument values to the kernel or can be samplers declared
//! inside a kernel.
//!
use crate::objects::enums::{ParamValue, Size};
use crate::objects::functions::status_update;
use crate::objects::structs::{SamplerInfo, StatusCode};
use crate::objects::types::{APIResult, ContextPtr, LongProperties, SamplerPtr};
use crate::{gen_param_value, size_getter};
use libc::c_void;
use opencl_heads::ffi;
use opencl_heads::ffi::clGetSamplerInfo;
use opencl_heads::types::*;
use std::ptr;

//TODO: Generalize the AddressingMode and FilterMode structs for create_sampler
//Deprecated since OpenCL 2.0
pub fn create_sampler(
    context: &ContextPtr,
    normalized_coords: cl_bool,
    addressing_mode: cl_addressing_mode,
    filter_mode: cl_filter_mode,
) -> APIResult<SamplerPtr> {
    let fn_name = "clCreateSampler";
    let mut status_code = StatusCode::INVALID_VALUE;
    let sampler_ptr = unsafe {
        ffi::clCreateSampler(
            context.unwrap(),
            normalized_coords,
            addressing_mode,
            filter_mode,
            &mut status_code,
        )
    };
    status_update(
        status_code,
        fn_name,
        SamplerPtr::from_ptr(sampler_ptr, fn_name)?,
    )
}

/// * context must be a valid OpenCL context.
/// * sampler_properties specifies 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. The
/// list of supported properties is described in the Sampler Properties table.
/// If a supported property and its value is not specified in sampler_properties,
/// its default value will be used. sampler_properties can be NULL in which
/// case the default values for supported sampler properties will be used.
pub fn create_sampler_with_properties(
    context: &ContextPtr,
    // sampler_properties: Option<[cl_ulong; 5]>,
    sampler_properties: &LongProperties,
) -> APIResult<SamplerPtr> {
    let fn_name = "clCreateSamplerWithProperties";
    let mut status_code = StatusCode::INVALID_VALUE;
    let sampler_properties = match sampler_properties {
        Some(x) => x.as_ptr(),
        None => ptr::null(),
    };
    let sampler_ptr = unsafe {
        ffi::clCreateSamplerWithProperties(context.unwrap(), sampler_properties, &mut status_code)
    };
    status_update(
        status_code,
        fn_name,
        SamplerPtr::from_ptr(sampler_ptr, fn_name)?,
    )
}

pub fn retain_sampler(sampler: &SamplerPtr) -> APIResult<()> {
    let fn_name = "clRetainSampler";
    let status_code = unsafe { ffi::clRetainSampler(sampler.unwrap()) };
    status_update(status_code, fn_name, ())
}
pub fn release_sampler(sampler: &SamplerPtr) -> APIResult<()> {
    let fn_name = "clReleaseSampler";
    let status_code = unsafe { ffi::clReleaseSampler(sampler.unwrap()) };
    status_update(status_code, fn_name, ())
}

pub fn get_sampler_info(
    sampler: &SamplerPtr,
    param_name: cl_sampler_info,
) -> APIResult<ParamValue> {
    type S = SamplerInfo;
    let fn_name = "clGetSamplerInfo";
    let sampler = sampler.unwrap();
    size_getter!(get_sampler_info_size, clGetSamplerInfo);
    match param_name {
        S::ADDRESSING_MODE | S::FILTER_MODE | S::NORMALIZED_COORDS | S::REFERENCE_COUNT => {
            let param_value = gen_param_value!(clGetSamplerInfo, u32, sampler, param_name);
            Ok(ParamValue::UInt(param_value))
        }
        S::CONTEXT => {
            let param_value = gen_param_value!(clGetSamplerInfo, isize, sampler, param_name);
            Ok(ParamValue::CPtr(param_value))
        }
        // Missing before 3.0
        S::PROPERTIES => {
            let size = get_sampler_info_size(sampler, param_name)?;
            let param_value = gen_param_value!(clGetSamplerInfo, isize, sampler, param_name, size);
            Ok(ParamValue::ArrCPtr(param_value))
        }
        _ => status_update(40404, fn_name, ParamValue::default()),
    }
}

/************************/
/* /\ /\ /\ /\ /\ /\ /\ */
/*|__|__|__|__|__|__|__|*/
/*|  |  |  |  |  |  |  |*/
/*|  |  Unit Tests  |  |*/
/*|__|__|__|__|__|__|__|*/
/*|__|__|__|__|__|__|__|*/
/************************/

#[cfg(test)]
mod tests {
    use super::*;
    use crate::api::context::{create_context, release_context};
    use crate::api::device::get_device_ids;
    use crate::api::platform::get_platform_ids;
    use crate::objects::bitfields::DeviceType;
    use crate::objects::property::SamplerProperties;
    use crate::objects::structs::{AddressingMode, FilterMode};
    use crate::objects::traits::GetSetGo;
    use crate::objects::types::{PlatformPtr, WrapMutPtr};

    #[test]
    fn test_create_sampler() {
        let platform_ids = get_platform_ids().unwrap();
        // Choose the first platform
        // let platform_id = platform_ids[0];
        let platform_id = PlatformPtr::from_ptr(platform_ids[0], "test_fn").unwrap();

        let device_ids =
            get_device_ids(&platform_id, DeviceType::new(DeviceType::DEFAULT).unwrap()).unwrap();
        assert!(0 < device_ids.len());

        let context = create_context(&None, device_ids, None, WrapMutPtr::null());
        let context = context.unwrap();

        // TODO: Generalize the property generator for every API
        // let sampler_properties = Some([
        //     SamplerInfo::ADDRESSING_MODE as cl_ulong,
        //     AddressingMode::REPEAT as cl_ulong,
        //     SamplerInfo::FILTER_MODE as cl_ulong,
        //     FilterMode::LINEAR as cl_ulong,
        //     0,
        // ]);

        // let sampler_props = SamplerProps {
        //     normalized_coords: true,
        //     addressing_mode: Some(AddressingMode::new(AddressingMode::REPEAT).unwrap()),
        //     // addressing_mode: Some(AddressingMode::REPEAT),
        //     // filter_mode: Some(FilterMode::LINEAR),
        //     filter_mode: Some(FilterMode::new(FilterMode::LINEAR).unwrap()),
        // }
        // .gen();
        let sampler_props = SamplerProperties.gen(
            None,
            Some(AddressingMode::new(AddressingMode::REPEAT).unwrap()),
            Some(FilterMode::new(FilterMode::LINEAR).unwrap()),
        );

        let sampler = create_sampler_with_properties(&context, &sampler_props);
        let sampler = sampler.unwrap();
        // eprintln!("CL_CONTEXT_PTR: {:?}", context);
        assert_ne!(sampler.unwrap(), ptr::null_mut());
        release_sampler(&sampler).unwrap();
        release_context(context).unwrap();
    }

    #[test]
    fn test_get_sampler_info() {
        let platform_ids = get_platform_ids().unwrap();
        // Choose the first platform
        // let platform_id = platform_ids[0];
        let platform_id = PlatformPtr::from_ptr(platform_ids[0], "test_fn").unwrap();

        let device_ids =
            get_device_ids(&platform_id, DeviceType::new(DeviceType::DEFAULT).unwrap()).unwrap();
        assert!(0 < device_ids.len());

        let context = create_context(&None, device_ids, None, WrapMutPtr::null());
        let context = context.unwrap();

        // TODO: Generalize the property generator for every API
        // let sampler_properties = Some([
        //     SamplerInfo::ADDRESSING_MODE as cl_ulong,
        //     AddressingMode::REPEAT as cl_ulong,
        //     SamplerInfo::FILTER_MODE as cl_ulong,
        //     FilterMode::LINEAR as cl_ulong,
        //     0,
        // ]);
        // let sampler_props = SamplerProps {
        //     normalized_coords: true,
        //     addressing_mode: Some(AddressingMode::new(AddressingMode::REPEAT).unwrap()),
        //     filter_mode: Some(FilterMode::new(FilterMode::LINEAR).unwrap()),
        // }
        // .gen();
        let sampler_props = SamplerProperties.gen(
            Some(true),
            Some(AddressingMode::new(AddressingMode::REPEAT).unwrap()),
            Some(FilterMode::new(FilterMode::LINEAR).unwrap()),
        );

        let sampler = create_sampler_with_properties(&context, &sampler_props);
        let sampler = sampler.unwrap();
        // Context information
        let sampler_context = get_sampler_info(&sampler, SamplerInfo::CONTEXT);
        eprintln!("{:?}", sampler_context);
        match sampler_context {
            Ok(_) => assert!(true),
            Err(_) => assert!(false),
        };
        // Reference count
        let sampler_reference_count = get_sampler_info(&sampler, SamplerInfo::REFERENCE_COUNT);
        eprintln!("{:?}", sampler_reference_count);
        match sampler_reference_count {
            Ok(_) => assert!(true),
            Err(_) => assert!(false),
        };
        // Addressing mode
        let sampler_addressing_mode = get_sampler_info(&sampler, SamplerInfo::ADDRESSING_MODE);
        eprintln!("{:?}", sampler_addressing_mode);
        match sampler_addressing_mode {
            Ok(_) => assert!(true),
            Err(_) => assert!(false),
        };
        // Filter mode
        let sampler_filter_mode = get_sampler_info(&sampler, SamplerInfo::FILTER_MODE);
        eprintln!("{:?}", sampler_filter_mode);
        match sampler_filter_mode {
            Ok(_) => assert!(true),
            Err(_) => assert!(false),
        };
        release_sampler(&sampler).unwrap();
        release_context(context).unwrap();
    }
}