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
use super::*;
use super::errors::*;

use std::cell::Cell;
use std::cmp::max;

#[derive(Debug)]
pub struct Texture3D {
    id: TextureId,
    bindless_id: TextureBindlessId,

    pub size: Texture3DSize,
    pub internal_format: TextureInternalFormat,
    pub format: TextureFormat,
    pub data_type: TextureDataType,

    bindless_counter: Cell<usize>,
}

#[derive(Debug, Copy, Clone, PartialEq)]
pub struct Texture3DSize(usize, usize, usize);

#[derive(Debug, Copy, Clone, PartialEq, Default)]
pub struct Texture3DParameters {
    pub min: TextureTexelFilter,
    pub mag: TextureTexelFilter,
    pub mipmap: TextureMipmapFilter,
    pub wrap_s: TextureWrapMode,
    pub wrap_t: TextureWrapMode,
    pub wrap_r: TextureWrapMode,
}

impl Texture for Texture3D {
    fn get_id(&self) -> TextureId {
        self.id
    }

    fn get_bindless_texture_id(&self) -> TextureBindlessId {
        self.bindless_id
    }

    fn make_resident_texture(&self) -> ResidentTexture {
        ResidentTexture::new(self.get_bindless_texture_id(), &self.bindless_counter)
    }

    fn get_target(&self) -> TextureTarget {
        TextureTarget::Texture3D
    }
}

impl Texture3D {
    pub fn new<T: TextureDataPrimitive>(
        size: Texture3DSize,
        internal_format: TextureInternalFormat,
        format: TextureFormat,
        data_type: TextureDataType,
        parameters: Texture3DParameters,
        levels: usize,
        data: &[T],
    ) -> Result<Texture3D> {
        let mut texture =
            Texture3D::new_empty(size, internal_format, format, data_type, parameters, levels)
                .chain_err(|| "Could not create texture")?;

        texture
            .set_data(data)
            .chain_err(|| "Could not set data of texture")?;

        Ok(texture)
    }

    pub fn new_empty(
        size: Texture3DSize,
        internal_format: TextureInternalFormat,
        format: TextureFormat,
        data_type: TextureDataType,
        parameters: Texture3DParameters,
        levels: usize,
    ) -> Result<Texture3D> {
        unsafe {
            let id = create_texture(TextureTarget::Texture2D);

            let mut texture = Texture3D {
                id: id,
                bindless_id: TextureBindlessId::empty(), // placeholder

                size: size,
                internal_format: internal_format,
                format: format,
                data_type: data_type,

                bindless_counter: Cell::new(0),
            };

            texture_storage_3d(id, levels, internal_format, (size.0, size.1, size.2))
                .chain_err(|| "Unable to allocate memory for texture")?;

            texture
                .set_parameters(parameters)
                .chain_err(|| "Unable to set texture parameters")?;

            texture.bindless_id =
                get_texture_handle(id.into()).chain_err(|| "Could not create bindless id")?;

            Ok(texture)
        }
    }

    pub fn make_resident_image(
        &self,
        level: usize,
        internal_format: ImageInternalFormat,
        access: ImageAccess,
    ) -> Result<ResidentImage> {
        let bindless_id = unsafe {
            get_image_handle(self.id, level, Some(0), internal_format)
                .chain_err(|| "Could not create image handle")?
        };

        ResidentImage::new(bindless_id, access)
    }

    pub fn set_data<T: TextureDataPrimitive>(&mut self, data: &[T]) -> Result<()> {
        if self.size.0 * self.size.1 * self.size.2 * self.format.n_components() != data.len() {
            bail!("Data length does not match texture size");
        }
        unsafe {
            texture_subimage_3d(
                self.id,
                0,
                (0, 0, 0),
                (self.size.0, self.size.1, self.size.2),
                self.format.into(),
                self.data_type.into(),
                data,
            ).chain_err(|| "Could not set texture data")?;
        }
        Ok(())
    }

    pub fn set_subdata<T: TextureDataPrimitive>(
        &mut self,
        size: Texture3DSize,
        offset: Texture3DSize,
        data: &[T],
    ) -> Result<()> {
        if size.0 * size.1 * size.2 * self.format.n_components() != data.len() {
            bail!("Data length does not match proclaimed size");
        }
        if size.0 + offset.0 >= self.size.0 || size.1 + offset.1 >= self.size.1 ||
            size.2 + offset.2 >= self.size.2
        {
            bail!("Size + offset is outside texture bounds");
        }
        unsafe {
            texture_subimage_3d(
                self.id,
                0,
                (offset.0, offset.1, offset.2),
                (size.0, size.1, size.2),
                self.format.into(),
                self.data_type.into(),
                data,
            ).chain_err(|| "Could not set texture data")?;
        }
        Ok(())
    }

    pub fn set_data_mipmap<T: TextureDataPrimitive>(
        &mut self,
        level: usize,
        data: &[T],
    ) -> Result<()> {
        if discrete_floored_log2(max(self.size.0, max(self.size.1, self.size.2))) < level {
            bail!("Mipmap level references greater level than texture has");
        }
        if texture_size_mipmap(self.size.0, level) * texture_size_mipmap(self.size.1, level) *
            texture_size_mipmap(self.size.2, level) * self.format.n_components() !=
            data.len()
        {
            bail!("Data length does not match texture size");
        }
        unsafe {
            texture_subimage_3d(
                self.id,
                0,
                (0, 0, 0),
                (self.size.0, self.size.1, self.size.2),
                self.format.into(),
                self.data_type.into(),
                data,
            ).chain_err(|| "Could not set texture data")?;
        }
        Ok(())
    }

    pub fn set_subdata_mipmap<T: TextureDataPrimitive>(
        &mut self,
        level: usize,
        size: Texture3DSize,
        offset: Texture3DSize,
        data: &[T],
    ) -> Result<()> {
        if discrete_floored_log2(max(self.size.0, max(self.size.1, self.size.2))) < level {
            bail!("Mipmap level references greater level than texture has");
        }
        if texture_size_mipmap(self.size.0, level) * texture_size_mipmap(self.size.1, level) *
            texture_size_mipmap(self.size.2, level) * self.format.n_components() !=
            data.len()
        {
            bail!("Data length does not match texture size");
        }
        if size.0 + offset.0 >= texture_size_mipmap(self.size.0, level) ||
            size.1 + offset.1 >= texture_size_mipmap(self.size.1, level) ||
            size.2 + offset.2 >= texture_size_mipmap(self.size.2, level)
        {
            bail!("Size + offset is outside texture bounds");
        }
        unsafe {
            texture_subimage_3d(
                self.id,
                0,
                (offset.0, offset.1, offset.2),
                (size.0, size.1, size.2),
                self.format.into(),
                self.data_type.into(),
                data,
            ).chain_err(|| "Could not set texture data")?;
        }
        Ok(())
    }

    fn set_parameters(&mut self, parameters: Texture3DParameters) -> Result<()> {
        let min = TextureParameterOption::MinFilter(parameters.min, parameters.mipmap);
        let mag = TextureParameterOption::MagFilter(parameters.mag);
        let wrap_s = TextureParameterOption::WrapS(parameters.wrap_s);
        let wrap_t = TextureParameterOption::WrapT(parameters.wrap_t);
        let wrap_r = TextureParameterOption::WrapR(parameters.wrap_r);

        unsafe {
            texture_parameter_i(self.id, min).chain_err(|| "Could not set min parameter")?;
            texture_parameter_i(self.id, mag).chain_err(|| "Could not set mag parameter")?;
            texture_parameter_i(self.id, wrap_s).chain_err(|| "Could not set wrap s parameter")?;
            texture_parameter_i(self.id, wrap_t).chain_err(|| "Could not set wrap t parameter")?;
            texture_parameter_i(self.id, wrap_r).chain_err(|| "Could not set wrap r parameter")?;
        }

        Ok(())
    }
}

impl Drop for Texture3D {
    fn drop(&mut self) {
        unsafe { delete_texture(self.id) };
    }
}