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
//! An image sampler.

use std;
use std::ops::{Deref, DerefMut};
use core::{self, Result as OclCoreResult, Sampler as SamplerCore, AddressingMode, FilterMode,
    SamplerInfo, SamplerInfoResult};
use error::{Error as OclError, Result as OclResult};
use standard::Context;

/// An image sampler used to process images.
#[derive(Clone, Debug)]
#[repr(C)]
pub struct Sampler(SamplerCore);

impl Sampler {
    /// Creates and returns a new sampler.
    ///
    /// ## Enum Quick Reference
    ///
    /// `addressing_mode`:
    ///
    /// - AddressingMode::None
    /// - AddressingMode::ClampToEdge
    /// - AddressingMode::Clamp
    /// - AddressingMode::Repeat
    /// - AddressingMode::MirroredRepeat
    ///
    /// `filter_mode`:
    ///
    /// - FilterMode::Nearest
    /// - FilterMode::Linear
    ///
    /// See [SDK Docs](https://www.khronos.org/registry/cl/sdk/1.2/docs/man/xhtml/clCreateSampler.html)
    /// for more information.
    ///
    pub fn new(context: &Context, normalize_coords: bool, addressing_mode: AddressingMode,
            filter_mode: FilterMode) -> OclResult<Sampler> {
        let sampler_core = core::create_sampler(context, normalize_coords,
            addressing_mode, filter_mode).map_err(OclError::from)?;

        Ok(Sampler(sampler_core))
    }

    /// Creates and returns a new sampler with some default settings.
    ///
    /// ## Defaults
    ///
    /// - `normalize_coords`: false
    /// - `addressing_mode`: `AddressingMode::None`
    /// - `filter_mode`: `FilterMode::Nearest`
    ///
    pub fn with_defaults(context: &Context) -> OclResult<Sampler> {
        let sampler_core = core::create_sampler(context, false,
            AddressingMode::None, FilterMode::Nearest).map_err(OclError::from)?;

        Ok(Sampler(sampler_core))
    }

    /// Returns various kinds of information about the sampler.
    pub fn info(&self, info_kind: SamplerInfo) -> OclCoreResult<SamplerInfoResult> {
        // match core::get_sampler_info(&self.0, info_kind) {
        //     Ok(res) => res,
        //     Err(err) => SamplerInfoResult::Error(Box::new(err)),
        // }
        core::get_sampler_info(&self.0, info_kind)
    }

    fn fmt_info(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
        f.debug_struct("Sampler")
            .field("ReferenceCount", &self.info(SamplerInfo::ReferenceCount))
            .field("Context", &self.info(SamplerInfo::Context))
            .field("NormalizedCoords", &self.info(SamplerInfo::NormalizedCoords))
            .field("AddressingMode", &self.info(SamplerInfo::AddressingMode))
            .field("FilterMode", &self.info(SamplerInfo::FilterMode))
            .finish()
    }
}

impl std::fmt::Display for Sampler {
    fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
        self.fmt_info(f)
    }
}

impl Deref for Sampler {
    type Target = SamplerCore;

    fn deref(&self) -> &SamplerCore {
        &self.0
    }
}

impl DerefMut for Sampler {
    fn deref_mut(&mut self) -> &mut SamplerCore {
        &mut self.0
    }
}