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::error::{Result as OclResult};
use core::{self, Sampler as SamplerCore, AddressingMode, FilterMode, SamplerInfo, SamplerInfoResult};
use standard::Context;

/// An image sampler used to process images.
#[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 = try!(core::create_sampler(context, normalize_coords,
            addressing_mode, filter_mode));

        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 = try!(core::create_sampler(context, false,
            AddressingMode::None, FilterMode::Nearest));

        Ok(Sampler(sampler_core))
    }

    /// Returns various kinds of information about the sampler.
    pub fn info(&self, info_kind: SamplerInfo) -> 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
    }
}