aboutsummaryrefslogtreecommitdiffhomepage
path: root/zluda/src/impl/array.rs
blob: ab2db78f441130ffa328e73ec3a2235544d59d7e (plain)
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
use std::{mem, ptr};

use crate::hip_call_cuda;

use super::hipfix;
use cuda_types::*;
use hip_runtime_sys::*;

pub(crate) unsafe fn create_3d(
    array: *mut CUarray,
    allocate_array: *const HIP_ARRAY3D_DESCRIPTOR,
) -> Result<(), CUresult> {
    if let (Some(array_ptr), Some(desc)) = (
        array.as_mut(),
        (allocate_array as *const HIP_ARRAY3D_DESCRIPTOR).as_ref(),
    ) {
        let mut desc = *desc;
        let (hack_flag, format) = hipfix::get_non_broken_format(desc.Format);
        desc.Format = format;
        hipfix::array_3d_create(&mut desc);
        let mut hip_array = mem::zeroed();
        hip_call_cuda!(hipArray3DCreate(&mut hip_array, &mut desc as _));
        (&mut *hip_array).textureType = hack_flag;
        let layered_dimensions = if desc.Flags & hipArrayLayered != 0 {
            if desc.Height == 0 {
                1usize
            } else {
                2
            }
        } else {
            0
        };
        *array_ptr = hipfix::array::to_cuda(hip_array, layered_dimensions);
        Ok(())
    } else {
        Err(CUresult::CUDA_ERROR_INVALID_VALUE)
    }
}

pub(crate) unsafe fn get_descriptor_3d(
    array_descriptor: *mut CUDA_ARRAY3D_DESCRIPTOR,
    array: CUarray,
) -> hipError_t {
    let layered = hipfix::array::get_layered_dimensions(array);
    let mut flags = if layered > 0 { CUDA_ARRAY3D_LAYERED } else { 0 };
    // HIP surfaces are always ld/st capable you want it or not
    flags |= CUDA_ARRAY3D_SURFACE_LDST;
    let array = hipfix::array::get(array);
    if let (Some(array), Some(array_descriptor)) = (array.as_ref(), array_descriptor.as_mut()) {
        *array_descriptor = CUDA_ARRAY3D_DESCRIPTOR {
            Width: array.width as usize,
            Height: array.height as usize,
            Depth: array.depth as usize,
            NumChannels: array.NumChannels,
            Format: mem::transmute(array.Format), // compatible
            Flags: flags,
        };
        hipError_t::hipSuccess
    } else {
        hipError_t::hipErrorInvalidValue
    }
}

pub(crate) unsafe fn create(
    array: *mut *mut CUarray_st,
    desc: *const HIP_ARRAY_DESCRIPTOR,
) -> Result<(), CUresult> {
    if array == ptr::null_mut() {
        return Err(CUresult::CUDA_ERROR_INVALID_VALUE);
    }
    if let Some(desc) = (desc as *const HIP_ARRAY_DESCRIPTOR).as_ref() {
        let mut desc = *desc;
        let (hack_flag, format) = hipfix::get_non_broken_format(desc.Format);
        desc.Format = format;
        let mut hip_array = ptr::null_mut();
        hip_call_cuda!(hipArrayCreate(&mut hip_array, &desc));
        (&mut *hip_array).textureType = hack_flag;
        *array = hip_array.cast();
        Ok(())
    } else {
        Err(CUresult::CUDA_ERROR_INVALID_VALUE)
    }
}