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
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
// Copyright (c) 2020-2021 Via Technology Ltd. All Rights Reserved.
//
// 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.

pub use cl3::memory::*;

use super::context::Context;

use cl3::memory;
use cl3::sampler;
#[allow(unused_imports)]
use cl3::types::{
    cl_addressing_mode, cl_bool, cl_buffer_create_type, cl_filter_mode, cl_image_desc,
    cl_image_format, cl_int, cl_mem, cl_mem_flags, cl_mem_properties, cl_sampler,
    cl_sampler_properties, cl_uint, cl_ulong,
};
use libc::{c_void, intptr_t, size_t};
use std::mem;

pub fn get_mem_type(memobj: cl_mem) -> Result<cl_uint, cl_int> {
    let value = memory::get_mem_object_info(memobj, MemInfo::CL_MEM_TYPE)?;
    Ok(value.to_uint())
}

pub fn get_mem_flags(memobj: cl_mem) -> Result<cl_uint, cl_int> {
    let value = memory::get_mem_object_info(memobj, MemInfo::CL_MEM_FLAGS)?;
    Ok(value.to_uint())
}

pub fn get_mem_size(memobj: cl_mem) -> Result<size_t, cl_int> {
    let value = memory::get_mem_object_info(memobj, MemInfo::CL_MEM_SIZE)?;
    Ok(value.to_size())
}

pub fn get_mem_host_ptr(memobj: cl_mem) -> Result<intptr_t, cl_int> {
    let value = memory::get_mem_object_info(memobj, MemInfo::CL_MEM_HOST_PTR)?;
    Ok(value.to_ptr())
}

pub fn get_mem_map_count(memobj: cl_mem) -> Result<cl_uint, cl_int> {
    let value = memory::get_mem_object_info(memobj, MemInfo::CL_MEM_MAP_COUNT)?;
    Ok(value.to_uint())
}

pub fn get_mem_reference_count(memobj: cl_mem) -> Result<cl_uint, cl_int> {
    let value = memory::get_mem_object_info(memobj, MemInfo::CL_MEM_REFERENCE_COUNT)?;
    Ok(value.to_uint())
}

pub fn get_mem_context(memobj: cl_mem) -> Result<intptr_t, cl_int> {
    let value = memory::get_mem_object_info(memobj, MemInfo::CL_MEM_CONTEXT)?;
    Ok(value.to_ptr())
}

pub fn get_mem_associated_memobject(memobj: cl_mem) -> Result<intptr_t, cl_int> {
    let value = memory::get_mem_object_info(memobj, MemInfo::CL_MEM_ASSOCIATED_MEMOBJECT)?;
    Ok(value.to_ptr())
}

pub fn get_mem_offset(memobj: cl_mem) -> Result<size_t, cl_int> {
    let value = memory::get_mem_object_info(memobj, MemInfo::CL_MEM_OFFSET)?;
    Ok(value.to_size())
}

pub fn get_mem_uses_svm_pointer(memobj: cl_mem) -> Result<cl_uint, cl_int> {
    let value = memory::get_mem_object_info(memobj, MemInfo::CL_MEM_USES_SVM_POINTER)?;
    Ok(value.to_uint())
}

// CL_VERSION_3_0
pub fn get_mem_properties(memobj: cl_mem) -> Result<Vec<cl_ulong>, cl_int> {
    let value = memory::get_mem_object_info(memobj, MemInfo::CL_MEM_PROPERTIES)?;
    Ok(value.to_vec_ulong())
}

/// An OpenCL buffer.  
/// Implements the Drop trait to call release_mem_object when the object is dropped.
pub struct Buffer {
    buffer: cl_mem,
}

impl Drop for Buffer {
    fn drop(&mut self) {
        memory::release_mem_object(self.buffer).unwrap();
    }
}

impl Buffer {
    pub fn new(buffer: cl_mem) -> Buffer {
        Buffer { buffer }
    }

    /// Create a Buffer for a context.  
    ///
    /// * `context` - a valid OpenCL context.
    /// * `flags` - a bit-field used to specify allocation and usage information
    /// about the image memory object being created, see:
    /// [Memory Flags](https://www.khronos.org/registry/OpenCL/specs/3.0-unified/html/OpenCL_API.html#memory-flags-table).
    /// * `count` - the number of T objects to be allocated.
    /// * `host_ptr` - a pointer to the buffer data that may already be allocated
    /// by the application.
    ///
    /// returns a Result containing the new OpenCL buffer object
    /// or the error code from the OpenCL C API function.
    pub fn create<T>(
        context: &Context,
        flags: cl_mem_flags,
        count: size_t,
        host_ptr: *mut c_void,
    ) -> Result<Buffer, cl_int> {
        let buffer =
            memory::create_buffer(context.get(), flags, count * mem::size_of::<T>(), host_ptr)?;
        Ok(Buffer::new(buffer))
    }

    /// Create an OpenCL buffer object for a context.  
    /// CL_VERSION_3_0
    ///
    /// * `context` - a valid OpenCL context.
    /// * `properties` - an optional null terminated list of properties.
    /// * `flags` - a bit-field used to specify allocation and usage information
    /// about the image memory object being created, see:
    /// [Memory Flags](https://www.khronos.org/registry/OpenCL/specs/3.0-unified/html/OpenCL_API.html#memory-flags-table).
    /// * `count` - the number of T objects to be allocated.
    /// * `host_ptr` - a pointer to the buffer data that may already be allocated
    /// by the application.
    ///
    /// returns a Result containing the new OpenCL buffer object
    /// or the error code from the OpenCL C API function.
    #[cfg(feature = "CL_VERSION_3_0")]
    pub fn create_with_properties<T>(
        context: &Context,
        properties: *const cl_mem_properties,
        flags: cl_mem_flags,
        count: size_t,
        host_ptr: *mut c_void,
    ) -> Result<Buffer, cl_int> {
        let buffer = memory::create_buffer_with_properties(
            context.get(),
            properties,
            flags,
            count * mem::size_of::<T>(),
            host_ptr,
        )?;
        Ok(Buffer::new(buffer))
    }

    /// Create an new OpenCL buffer object from an existing buffer object.  
    ///
    /// * `flags` - a bit-field used to specify allocation and usage information
    /// about the sub-buffer memory object being created, see:
    /// [Memory Flags](https://www.khronos.org/registry/OpenCL/specs/3.0-unified/html/OpenCL_API.html#memory-flags-table).
    /// * `buffer_create_type`,`buffer_create_info` - describe the type of
    /// buffer object to be created, see:
    /// [SubBuffer Attributes](https://www.khronos.org/registry/OpenCL/specs/3.0-unified/html/OpenCL_API.html#subbuffer-create-info-table).
    ///
    /// returns a Result containing the new OpenCL buffer object
    /// or the error code from the OpenCL C API function.
    pub fn create_sub_buffer(
        &self,
        flags: cl_mem_flags,
        buffer_create_type: cl_buffer_create_type,
        buffer_create_info: *const c_void,
    ) -> Result<Buffer, cl_int> {
        let buffer =
            memory::create_sub_buffer(self.buffer, flags, buffer_create_type, buffer_create_info)?;
        Ok(Buffer::new(buffer))
    }

    pub fn get(&self) -> cl_mem {
        self.buffer
    }
}

/// An OpenCL image.  
/// Has methods to return information from calls to clGetImageInfo with the
/// appropriate parameters.  
/// Implements the Drop trait to call release_mem_object when the object is dropped.
pub struct Image {
    image: cl_mem,
}

impl Drop for Image {
    fn drop(&mut self) {
        memory::release_mem_object(self.image).unwrap();
    }
}

impl Image {
    pub fn new(image: cl_mem) -> Image {
        Image { image }
    }

    /// Create an OpenCL image object for a context.  
    ///
    /// * `context` - a valid OpenCL context.
    /// * `flags` - a bit-field used to specify allocation and usage information
    /// about the image memory object being created, see:
    /// [Memory Flags](https://www.khronos.org/registry/OpenCL/specs/3.0-unified/html/OpenCL_API.html#memory-flags-table).
    /// * `image_format` - a pointer to a structure that describes format properties
    /// of the image to be allocated.
    /// * `image_desc` - a pointer to a structure that describes type and dimensions
    /// of the image to be allocated.
    /// * `host_ptr` - a pointer to the image data that may already be allocated
    /// by the application.
    ///
    /// returns a Result containing the new OpenCL image object
    /// or the error code from the OpenCL C API function.
    pub fn create<T>(
        context: &Context,
        flags: cl_mem_flags,
        image_format: *const cl_image_format,
        image_desc: *const cl_image_desc,
        host_ptr: *mut c_void,
    ) -> Result<Image, cl_int> {
        let image = memory::create_image(context.get(), flags, image_format, image_desc, host_ptr)?;
        Ok(Image::new(image))
    }

    /// Create an OpenCL image object for a context.  
    /// CL_VERSION_3_0
    ///
    /// * `context` - a valid OpenCL context.
    /// * `properties` - an optional null terminated list of properties.
    /// * `flags` - a bit-field used to specify allocation and usage information
    /// about the image memory object being created, see:
    /// [Memory Flags](https://www.khronos.org/registry/OpenCL/specs/3.0-unified/html/OpenCL_API.html#memory-flags-table).
    /// * `image_format` - a pointer to a structure that describes format properties
    /// of the image to be allocated.
    /// * `image_desc` - a pointer to a structure that describes type and dimensions
    /// of the image to be allocated.
    /// * `host_ptr` - a pointer to the image data that may already be allocated
    /// by the application.
    ///
    /// returns a Result containing the new OpenCL image object
    /// or the error code from the OpenCL C API function.
    #[cfg(feature = "CL_VERSION_3_0")]
    pub fn create_with_properties<T>(
        context: &Context,
        properties: *const cl_mem_properties,
        flags: cl_mem_flags,
        image_format: *const cl_image_format,
        image_desc: *const cl_image_desc,
        host_ptr: *mut c_void,
    ) -> Result<Image, cl_int> {
        let image = memory::create_image_with_properties(
            context.get(),
            properties,
            flags,
            image_format,
            image_desc,
            host_ptr,
        )?;
        Ok(Image::new(image))
    }

    pub fn get(&self) -> cl_mem {
        self.image
    }

    pub fn get_format(&self) -> Result<Vec<cl_image_format>, cl_int> {
        let value = memory::get_image_info(self.image, ImageInfo::CL_IMAGE_FORMAT)?;
        Ok(value.to_vec_image_format())
    }

    pub fn get_element_size(&self) -> Result<size_t, cl_int> {
        let value = memory::get_image_info(self.image, ImageInfo::CL_IMAGE_ELEMENT_SIZE)?;
        Ok(value.to_size())
    }

    pub fn get_row_pitch(&self) -> Result<size_t, cl_int> {
        let value = memory::get_image_info(self.image, ImageInfo::CL_IMAGE_ROW_PITCH)?;
        Ok(value.to_size())
    }

    pub fn get_slice_pitch(&self) -> Result<size_t, cl_int> {
        let value = memory::get_image_info(self.image, ImageInfo::CL_IMAGE_SLICE_PITCH)?;
        Ok(value.to_size())
    }

    pub fn get_width(&self) -> Result<size_t, cl_int> {
        let value = memory::get_image_info(self.image, ImageInfo::CL_IMAGE_WIDTH)?;
        Ok(value.to_size())
    }

    pub fn get_height(&self) -> Result<size_t, cl_int> {
        let value = memory::get_image_info(self.image, ImageInfo::CL_IMAGE_HEIGHT)?;
        Ok(value.to_size())
    }

    pub fn get_depth(&self) -> Result<size_t, cl_int> {
        let value = memory::get_image_info(self.image, ImageInfo::CL_IMAGE_DEPTH)?;
        Ok(value.to_size())
    }
    pub fn get_array_size(&self) -> Result<size_t, cl_int> {
        let value = memory::get_image_info(self.image, ImageInfo::CL_IMAGE_ARRAY_SIZE)?;
        Ok(value.to_size())
    }

    pub fn get_buffer(&self) -> Result<intptr_t, cl_int> {
        let value = memory::get_image_info(self.image, ImageInfo::CL_IMAGE_BUFFER)?;
        Ok(value.to_ptr())
    }

    pub fn get_num_mip_levels(&self) -> Result<cl_uint, cl_int> {
        let value = memory::get_image_info(self.image, ImageInfo::CL_IMAGE_NUM_MIP_LEVELS)?;
        Ok(value.to_uint())
    }

    pub fn get_num_samples(&self) -> Result<cl_uint, cl_int> {
        let value = memory::get_image_info(self.image, ImageInfo::CL_IMAGE_NUM_SAMPLES)?;
        Ok(value.to_uint())
    }
}

/// An OpenCL sampler.  
/// Has methods to return information from calls to clGetSamplerInfo with the
/// appropriate parameters.  
/// Implements the Drop trait to call release_sampler when the object is dropped.
pub struct Sampler {
    sampler: cl_sampler,
}

impl Drop for Sampler {
    fn drop(&mut self) {
        sampler::release_sampler(self.sampler).unwrap();
    }
}

impl Sampler {
    pub fn new(sampler: cl_sampler) -> Sampler {
        Sampler { sampler }
    }

    pub fn create<T>(
        context: &Context,
        normalize_coords: cl_bool,
        addressing_mode: cl_addressing_mode,
        filter_mode: cl_filter_mode,
    ) -> Result<Sampler, cl_int> {
        let sampler = sampler::create_sampler(
            context.get(),
            normalize_coords,
            addressing_mode,
            filter_mode,
        )?;
        Ok(Sampler::new(sampler))
    }

    pub fn create_with_properties<T>(
        context: &Context,
        properties: *const cl_sampler_properties,
    ) -> Result<Sampler, cl_int> {
        let sampler = sampler::create_sampler_with_properties(context.get(), properties)?;
        Ok(Sampler::new(sampler))
    }

    pub fn get(&self) -> cl_sampler {
        self.sampler
    }
}

/// An OpenCL pipe.  
/// Has methods to return information from calls to clGetPipeInfo with the
/// appropriate parameters.  
/// Implements the Drop trait to call release_mem_object when the object is dropped.
pub struct Pipe {
    pipe: cl_mem,
}

impl Drop for Pipe {
    fn drop(&mut self) {
        memory::release_mem_object(self.pipe).unwrap();
    }
}

impl Pipe {
    pub fn new(pipe: cl_mem) -> Pipe {
        Pipe { pipe }
    }

    pub fn create<T>(
        context: &Context,
        flags: cl_mem_flags,
        pipe_packet_size: cl_uint,
        pipe_max_packets: cl_uint,
    ) -> Result<Pipe, cl_int> {
        let pipe = memory::create_pipe(context.get(), flags, pipe_packet_size, pipe_max_packets)?;
        Ok(Pipe::new(pipe))
    }

    pub fn get(&self) -> cl_mem {
        self.pipe
    }

    pub fn get_packet_size(&self) -> Result<cl_uint, cl_int> {
        let value = memory::get_pipe_info(self.pipe, PipeInfo::CL_PIPE_PACKET_SIZE)?;
        Ok(value.to_uint())
    }

    pub fn get_max_packets(&self) -> Result<cl_uint, cl_int> {
        let value = memory::get_pipe_info(self.pipe, PipeInfo::CL_PIPE_MAX_PACKETS)?;
        Ok(value.to_uint())
    }

    pub fn get_properties(&self) -> Result<Vec<intptr_t>, cl_int> {
        let value = memory::get_pipe_info(self.pipe, PipeInfo::CL_PIPE_PROPERTIES)?;
        Ok(value.to_vec_intptr())
    }
}