Skip to main content

dynamic_ocl/safe/buffer/
flags.rs

1//! Buffer access and use flags, to allow validity checks to be performed at
2//! compile time by the type system.
3
4use crate::raw::{
5    cl_mem_flags, CL_MEM_ALLOC_HOST_PTR, CL_MEM_HOST_NO_ACCESS, CL_MEM_HOST_READ_ONLY,
6    CL_MEM_HOST_WRITE_ONLY, CL_MEM_READ_ONLY, CL_MEM_READ_WRITE, CL_MEM_WRITE_ONLY,
7};
8
9mod sealed {
10    use crate::raw::cl_mem_flags;
11
12    pub trait FlagInternal {
13        const FLAGS: cl_mem_flags;
14    }
15}
16
17/// A trait denoting a buffer host accessibility type.
18///
19/// Types implementing this trait indicate whether an OpenCL memory object can
20/// be read/written by the host.
21pub trait HostAccess: sealed::FlagInternal {}
22
23/// A trait denoting a buffer that may be read by the host.
24pub trait HostReadable: HostAccess {}
25
26/// A trait denoting a buffer that may be written by the host.
27pub trait HostWritable: HostAccess {}
28
29/// The host may not read or write the buffer once it's been created.
30pub struct HostNoAccess;
31
32/// The host may only read the buffer once it's been created.
33pub struct HostReadOnly;
34
35/// The host may only write the buffer once it's been created.
36pub struct HostWriteOnly;
37
38/// The host may read and write the buffer.
39pub struct HostReadWrite;
40
41impl sealed::FlagInternal for HostNoAccess {
42    const FLAGS: cl_mem_flags = CL_MEM_HOST_NO_ACCESS;
43}
44
45impl sealed::FlagInternal for HostReadOnly {
46    const FLAGS: cl_mem_flags = CL_MEM_HOST_READ_ONLY;
47}
48
49impl sealed::FlagInternal for HostWriteOnly {
50    const FLAGS: cl_mem_flags = CL_MEM_HOST_WRITE_ONLY;
51}
52
53impl sealed::FlagInternal for HostReadWrite {
54    const FLAGS: cl_mem_flags = 0;
55}
56
57impl HostAccess for HostNoAccess {}
58
59impl HostAccess for HostReadOnly {}
60impl HostReadable for HostReadOnly {}
61
62impl HostAccess for HostWriteOnly {}
63impl HostWritable for HostWriteOnly {}
64
65impl HostAccess for HostReadWrite {}
66impl HostReadable for HostReadWrite {}
67impl HostWritable for HostReadWrite {}
68
69/// A trait denoting a buffer device accessibility type.
70///
71/// Types implementing this trait indicate whether an OpenCL memory object can
72/// be read/written by the OpenCL device.
73pub trait DeviceAccess: sealed::FlagInternal {}
74
75/// The device may only read the buffer.
76pub struct DeviceReadOnly;
77
78/// The device may only write the buffer.
79pub struct DeviceWriteOnly;
80
81/// The device may read and write the buffer.
82pub struct DeviceReadWrite;
83
84impl sealed::FlagInternal for DeviceReadOnly {
85    const FLAGS: cl_mem_flags = CL_MEM_READ_ONLY;
86}
87
88impl sealed::FlagInternal for DeviceWriteOnly {
89    const FLAGS: cl_mem_flags = CL_MEM_WRITE_ONLY;
90}
91
92impl sealed::FlagInternal for DeviceReadWrite {
93    const FLAGS: cl_mem_flags = CL_MEM_READ_WRITE;
94}
95
96impl DeviceAccess for DeviceReadOnly {}
97impl DeviceAccess for DeviceWriteOnly {}
98impl DeviceAccess for DeviceReadWrite {}
99
100/// A trait used to specify extra buffer flags.
101pub trait BufferFlags: sealed::FlagInternal {}
102
103/// Don't set any special buffer flags.
104pub struct NoFlags;
105
106/// Set the `CL_MEM_ALLOC_HOST_PTR` flag indicating that the buffer should be
107/// allocated in host-accessible memory.
108pub struct AllocHostPtr;
109
110impl sealed::FlagInternal for NoFlags {
111    const FLAGS: cl_mem_flags = 0;
112}
113
114impl sealed::FlagInternal for AllocHostPtr {
115    const FLAGS: cl_mem_flags = CL_MEM_ALLOC_HOST_PTR;
116}
117
118impl BufferFlags for NoFlags {}
119impl BufferFlags for AllocHostPtr {}