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
use crate::GpuImageData;
use rafx_api::extra::upload::{RafxTransferUpload, RafxUploadError};
use rafx_api::{
    RafxBarrierQueueTransition, RafxCmdCopyBufferToTextureParams, RafxDeviceContext, RafxExtents3D,
    RafxQueue, RafxResourceState, RafxResourceType, RafxSampleCount, RafxTexture,
    RafxTextureBarrier, RafxTextureDef, RafxTextureDimensions,
};

// Arbitrary, not sure if there is any real requirement
pub const IMAGE_UPLOAD_REQUIRED_SUBRESOURCE_ALIGNMENT: u64 = 16;

pub struct ImageUploadParams<'a> {
    pub resource_type: RafxResourceType,
    pub generate_mips: bool,
    pub layer_swizzle: Option<&'a [u32]>,
}

impl<'a> Default for ImageUploadParams<'a> {
    fn default() -> Self {
        ImageUploadParams {
            resource_type: RafxResourceType::TEXTURE,
            generate_mips: false,
            layer_swizzle: None,
        }
    }
}

// This function is a little more complex to use than enqueue_load_images but can support cubemaps
// We create a layer for each layer_image_assignment, and copy from the decoded_image
// at the index matching the assignment
pub fn enqueue_load_image(
    device_context: &RafxDeviceContext,
    upload: &mut RafxTransferUpload,
    image_data: &GpuImageData,
    params: ImageUploadParams,
) -> Result<RafxTexture, RafxUploadError> {
    // All images must have identical mip level count, sizes, etc.
    #[cfg(debug_assertions)]
    image_data.verify_state();

    //
    // Determine the total amount of data we need to upload and verify there is enough space
    //
    let bytes_required = image_data.total_size(IMAGE_UPLOAD_REQUIRED_SUBRESOURCE_ALIGNMENT as u64);

    let has_space_available = upload.has_space_available(
        bytes_required as usize,
        IMAGE_UPLOAD_REQUIRED_SUBRESOURCE_ALIGNMENT as usize,
        1,
    );

    if !has_space_available {
        Err(RafxUploadError::BufferFull)?;
    }

    //
    // Determine mip count
    //
    let mip_count = if params.generate_mips {
        rafx_api::extra::mipmaps::mip_level_max_count_for_image_size(
            image_data.width,
            image_data.height,
        )
    } else {
        image_data.layers[0].mip_levels.len() as u32
    };

    //
    // Push all image layers/levels into the staging buffer, keeping note of offsets within the
    // buffer where each resource is stored
    //
    let mut layer_offsets = Vec::default();
    for layer in &image_data.layers {
        let mut level_offsets = Vec::default();
        for level in &layer.mip_levels {
            let offset = upload.push(
                &level.data,
                IMAGE_UPLOAD_REQUIRED_SUBRESOURCE_ALIGNMENT as usize,
            )?;
            level_offsets.push(offset);
        }
        layer_offsets.push(level_offsets);
    }

    // If we are swizzling layers, we create a layer per layer_swizzle entry. Otherwise, we use
    // the number of layers in the image data
    let layer_count = params
        .layer_swizzle
        .map(|x| x.len())
        .unwrap_or_else(|| image_data.layers.len()) as u32;

    //
    // Create the texture
    //
    assert!(mip_count > 0);
    let texture = device_context.create_texture(&RafxTextureDef {
        extents: RafxExtents3D {
            width: image_data.width,
            height: image_data.height,
            depth: 1,
        },
        array_length: layer_count,
        mip_count,
        sample_count: RafxSampleCount::SampleCount1,
        format: image_data.format,
        resource_type: params.resource_type,
        dimensions: RafxTextureDimensions::Dim2D,
    })?;

    //
    // Write into the transfer command buffer
    // - transition destination memory to receive the data
    // - copy the data
    // - transition the destination to the graphics queue
    //

    upload
        .transfer_command_buffer()
        .cmd_resource_barrier(
            &[],
            &[RafxTextureBarrier {
                texture: &texture,
                src_state: RafxResourceState::UNDEFINED,
                dst_state: RafxResourceState::COPY_DST,
                queue_transition: RafxBarrierQueueTransition::None,
                array_slice: None,
                mip_slice: None,
            }],
        )
        .unwrap();

    for dst_layer_index in 0..layer_count {
        let src_layer_index = if let Some(layer_swizzle) = params.layer_swizzle {
            layer_swizzle[dst_layer_index as usize] as usize
        } else {
            dst_layer_index as usize
        };

        for level_index in 0..image_data.layers[src_layer_index].mip_levels.len() {
            upload
                .transfer_command_buffer()
                .cmd_copy_buffer_to_texture(
                    upload.staging_buffer(),
                    &texture,
                    &RafxCmdCopyBufferToTextureParams {
                        buffer_offset: layer_offsets[src_layer_index][level_index],
                        array_layer: dst_layer_index as u16,
                        mip_level: level_index as u8,
                    },
                )
                .unwrap();
        }
    }

    log::debug!(
        "upload image {}x{} format {:?} layers: {} levels: {} generate mips: {} resource type: {:?}",
        image_data.width,
        image_data.height,
        image_data.format,
        layer_count,
        mip_count,
        params.generate_mips,
        params.resource_type
    );

    if params.generate_mips && mip_count > 1 {
        //
        // Transition the first mip range to COPY_SRC on graphics queue (release)
        //
        upload.transfer_command_buffer().cmd_resource_barrier(
            &[],
            &[RafxTextureBarrier {
                texture: &texture,
                src_state: RafxResourceState::COPY_DST,
                dst_state: RafxResourceState::COPY_SRC,
                queue_transition: RafxBarrierQueueTransition::ReleaseTo(
                    upload.dst_queue().queue_type(),
                ),
                array_slice: None,
                mip_slice: Some(0),
            }],
        )?;

        //
        // Transition the first mip range to COPY_SRC on graphics queue (acquire)
        //
        upload.dst_command_buffer().cmd_resource_barrier(
            &[],
            &[RafxTextureBarrier {
                texture: &texture,
                src_state: RafxResourceState::COPY_DST,
                dst_state: RafxResourceState::COPY_SRC,
                queue_transition: RafxBarrierQueueTransition::AcquireFrom(
                    upload.transfer_queue().queue_type(),
                ),
                array_slice: None,
                mip_slice: Some(0),
            }],
        )?;

        rafx_api::extra::mipmaps::generate_mipmaps(upload.dst_command_buffer(), &texture)?;

        //
        // Transition everything to the final layout
        //
        upload.dst_command_buffer().cmd_resource_barrier(
            &[],
            &[RafxTextureBarrier {
                texture: &texture,
                src_state: RafxResourceState::COPY_SRC,
                dst_state: RafxResourceState::SHADER_RESOURCE,
                queue_transition: RafxBarrierQueueTransition::None,
                array_slice: None,
                mip_slice: None,
            }],
        )?;
    } else {
        upload.transfer_command_buffer().cmd_resource_barrier(
            &[],
            &[RafxTextureBarrier {
                texture: &texture,
                src_state: RafxResourceState::COPY_DST,
                dst_state: RafxResourceState::SHADER_RESOURCE,
                queue_transition: RafxBarrierQueueTransition::ReleaseTo(
                    upload.dst_queue().queue_type(),
                ),
                array_slice: None,
                mip_slice: None,
            }],
        )?;

        upload.dst_command_buffer().cmd_resource_barrier(
            &[],
            &[RafxTextureBarrier {
                texture: &texture,
                src_state: RafxResourceState::COPY_DST,
                dst_state: RafxResourceState::SHADER_RESOURCE,
                queue_transition: RafxBarrierQueueTransition::AcquireFrom(
                    upload.transfer_queue().queue_type(),
                ),
                array_slice: None,
                mip_slice: None,
            }],
        )?;
    }

    Ok(texture)
}

pub fn load_image_blocking(
    device_context: &RafxDeviceContext,
    transfer_queue: &RafxQueue,
    dst_queue: &RafxQueue,
    upload_buffer_max_size: u64,
    image_data: &GpuImageData,
    params: ImageUploadParams,
) -> Result<RafxTexture, RafxUploadError> {
    let total_size = image_data.total_size(IMAGE_UPLOAD_REQUIRED_SUBRESOURCE_ALIGNMENT);
    if upload_buffer_max_size < total_size {
        Err(RafxUploadError::BufferFull)?;
    }

    let mut upload = RafxTransferUpload::new(
        device_context,
        transfer_queue,
        dst_queue,
        upload_buffer_max_size,
    )?;

    let texture = enqueue_load_image(device_context, &mut upload, image_data, params)?;

    upload.block_until_upload_complete()?;

    Ok(texture)
}