Skip to main content

oxicuda_ptx/ir/
texture.rs

1//! Texture and surface operation types for PTX instructions.
2//!
3//! This module defines enumerations for texture dimensionality and surface
4//! operation kinds, used by the `tex` and `suld`/`sust` instruction families.
5
6// ---------------------------------------------------------------------------
7// Texture dimension
8// ---------------------------------------------------------------------------
9
10/// Texture dimension for `tex` instructions.
11///
12/// PTX texture fetch instructions are suffixed with the dimensionality
13/// (`.1d`, `.2d`, `.3d`) to specify the coordinate space.
14#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
15pub enum TextureDim {
16    /// 1-dimensional texture.
17    Tex1d,
18    /// 2-dimensional texture.
19    Tex2d,
20    /// 3-dimensional texture.
21    Tex3d,
22}
23
24impl TextureDim {
25    /// Returns the PTX dimension suffix (e.g., `".1d"`, `".2d"`, `".3d"`).
26    #[must_use]
27    pub const fn as_ptx_str(self) -> &'static str {
28        match self {
29            Self::Tex1d => ".1d",
30            Self::Tex2d => ".2d",
31            Self::Tex3d => ".3d",
32        }
33    }
34}
35
36// ---------------------------------------------------------------------------
37// Surface operation
38// ---------------------------------------------------------------------------
39
40/// Surface operation kind for `suld` (load) and `sust` (store) instructions.
41///
42/// Surfaces provide typed access to CUDA surface memory, supporting both
43/// read and write operations through the hardware texture unit.
44#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
45pub enum SurfaceOp {
46    /// Surface load (`suld`).
47    Load,
48    /// Surface store (`sust`).
49    Store,
50}
51
52impl SurfaceOp {
53    /// Returns the PTX instruction prefix (e.g., `"suld"`, `"sust"`).
54    #[must_use]
55    pub const fn as_ptx_str(self) -> &'static str {
56        match self {
57            Self::Load => "suld",
58            Self::Store => "sust",
59        }
60    }
61}
62
63// ---------------------------------------------------------------------------
64// Tests
65// ---------------------------------------------------------------------------
66
67#[cfg(test)]
68mod tests {
69    use super::*;
70
71    #[test]
72    fn texture_dim_ptx_str() {
73        assert_eq!(TextureDim::Tex1d.as_ptx_str(), ".1d");
74        assert_eq!(TextureDim::Tex2d.as_ptx_str(), ".2d");
75        assert_eq!(TextureDim::Tex3d.as_ptx_str(), ".3d");
76    }
77
78    #[test]
79    fn surface_op_ptx_str() {
80        assert_eq!(SurfaceOp::Load.as_ptx_str(), "suld");
81        assert_eq!(SurfaceOp::Store.as_ptx_str(), "sust");
82    }
83}