pub fn make_texture<Data: GpuUploadable>(
    sbp: &mut StagingBufferPool,
    descriptor_set: DescriptorSet,
    data: Data,
    extent: Extent2D,
    ty: ImageType,
    _format: VulkanFormat,
    sampler: VkSampler,
    samples: SampleCount,
) -> Result<GpuTexture2D, GpuUploadError> {
    let vd = sbp.vd().clone();
    let tex_len = data.data_len();

    // Create the target image.
    let mut image = GpuImage::new(
        &vd,
        ty.into(),
        extent,
        VulkanFormat::B8G8R8A8_sRGB.into(),
        samples,
        ImageUsage::TransferDst | ImageUsage::Sampled,
        VmaMemoryUsageType::GpuOnly,
    );

    // Acquire a staging buffer.
    let mut sb = sbp
        .try_get(tex_len)
        .or(Err(GpuUploadError::NoAvailableStagingBuffer))?;

    // Upload data to the staging buffer.
    let offset = sb
        .buffer_mut()
        .push(data)
        .map_err(|e| map_buffer_error(e))?;

    // Convert target image from Undefined to TransferDst.
    let barrier = get_layout_transition_pipeline_barrier(
        image.object(),
        image.format(),
        ImageLayout::Undefined,
        ImageLayout::TransferDstOptimal,
    );

    // Undefined -> TransferDstOptimal
    sb.begin().pipeline_barrier(barrier);

    image.set_layout(ImageLayout::TransferDstOptimal);

    sb.copy_buffer_to_image(offset, &image)
        .end()
        .submit_and_wait_device();

    let barrier = get_layout_transition_pipeline_barrier(
        image.object(),
        image.format(),
        ImageLayout::TransferDstOptimal,
        ImageLayout::ShaderReadOnlyOptimal,
    );

    // TransferDstOptimal -> ShaderReadOnlyOptimal
    sb.reset()
        .begin()
        .pipeline_barrier(barrier)
        .end()
        .submit_and_wait_device();

    image.set_layout(ImageLayout::ShaderReadOnlyOptimal);

    // Create VkImageView.
    let image_view = create_image_view(
        &vd,
        image.object(),
        ImageAspect::Color,
        image.format(),
        ImageViewType::V2D,
        1,
    );

    // Update descriptor set.
    let image_info = VkDescriptorImageInfo {
        sampler,
        image_view,
        image_layout: ImageLayout::ShaderReadOnlyOptimal.into(),
    };

    let writer = VkWriteDescriptorSet {
        dst_set: descriptor_set.object(),
        dst_binding: 0,
        dst_array_element: 0,
        descriptor_count: 1,
        descriptor_type: DescriptorType::CombinedImageSampler.into(),
        image_info: &image_info,
        ..Default::default()
    };

    vd.write_descriptor_set(&writer);

    Ok(GpuTexture2D {
        image,
        image_view,
        sharing_mode: SharingMode::Exclusive,
        descriptor: VkDescriptorImageInfo {
            sampler,
            image_view,
            image_layout: ImageLayout::ShaderReadOnlyOptimal.into(),
        },
        mip_level: 1,
        descriptor_set,
    })
}
/// Make a GpuTexture and upload image data to GPU.
Make a GpuTexture and upload image data to GPU.
Make a GpuTexture and upload image data to GPU .
pub fn make_texture<Data: GpuUploadable>(
    sbp: &mut StagingBufferPool,
    descriptor_set: DescriptorSet,
    data: Data,
    extent: Extent2D,
    ty: ImageType,
    _format: VulkanFormat,
    sampler: VkSampler,
    samples: SampleCount,
) -> Result<GpuTexture2D, GpuUploadError> {
    let vd = sbp.vd().clone();
    let tex_len = data.data_len();
    // Create the target image.
    let mut image = GpuImage::new(
        &vd,
        ty.into(),
        extent,
        VulkanFormat::B8G8R8A8_sRGB.into(),
        samples,
        ImageUsage::TransferDst | ImageUsage::Sampled,
        VmaMemoryUsageType::GpuOnly,
    );
    // Acquire a staging buffer.
    let mut sb = sbp
        .try_get(tex_len)
        .or(Err(GpuUploadError::NoAvailableStagingBuffer))?;
    // Upload data to the staging buffer.
    let offset = sb
        .buffer_mut()
        .push(data)
        .map_err(|e| map_buffer_error(e))?;
    // Convert target image from Undefined to TransferDst.
    let barrier = get_layout_transition_pipeline_barrier(
        image.object(),
        image.format(),
        ImageLayout::Undefined,
        ImageLayout::TransferDstOptimal,
    );
    // Undefined -> TransferDstOptimal
    sb.begin().pipeline_barrier(barrier);
    image.set_layout(ImageLayout::TransferDstOptimal);
    sb.copy_buffer_to_image(offset, &image)
        .end()
        .submit_and_wait_device();
    let barrier = get_layout_transition_pipeline_barrier(
        image.object(),
        image.format(),
        ImageLayout::TransferDstOptimal,
        ImageLayout::ShaderReadOnlyOptimal,
    );
    // TransferDstOptimal -> ShaderReadOnlyOptimal
    sb.reset()
        .begin()
        .pipeline_barrier(barrier)
        .end()
        .submit_and_wait_device();
    image.set_layout(ImageLayout::ShaderReadOnlyOptimal);
    // Create VkImageView.
    let image_view = create_image_view(
        &vd,
        image.object(),
        ImageAspect::Color,
        image.format(),
        ImageViewType::V2D,
        1,
    );
    // Update descriptor set.
    let image_info = VkDescriptorImageInfo {
        sampler,
        image_view,
        image_layout: ImageLayout::ShaderReadOnlyOptimal.into(),
    };
    let writer = VkWriteDescriptorSet {
        dst_set: descriptor_set.object(),
        dst_binding: 0,
        dst_array_element: 0,
        descriptor_count: 1,
        descriptor_type: DescriptorType::CombinedImageSampler.into(),
        image_info: &image_info,
        ..Default::default()
    };
    vd.write_descriptor_set(&writer);
    Ok(GpuTexture2D {
        image,
        image_view,
        sharing_mode: SharingMode::Exclusive,
        descriptor: VkDescriptorImageInfo {
            sampler,
            image_view,
            image_layout: ImageLayout::ShaderReadOnlyOptimal.into(),
        },
        mip_level: 1,
        descriptor_set,
    })
}
pub fn make_texture < Data : GpuUploadable > ( sbp : & mut StagingBufferPool , descriptor_set : DescriptorSet , data : Data , extent : Extent2D , ty : ImageType , _format : VulkanFormat , sampler : VkSampler , samples : SampleCount , ) -> Result < GpuTexture2D , GpuUploadError > { let vd = sbp . vd ( ) . clone ( ) ; let tex_len = data . data_len ( ) ; // Create the target image. let mut image = GpuImage :: new ( & vd , ty . into ( ) , extent , VulkanFormat :: B8G8R8A8_sRGB . into ( ) , samples , ImageUsage :: TransferDst | ImageUsage :: Sampled , VmaMemoryUsageType :: GpuOnly , ) ; // Acquire a staging buffer. let mut sb = sbp . try_get ( tex_len ) . or ( Err ( GpuUploadError :: NoAvailableStagingBuffer ) ) ? ; // Upload data to the staging buffer. let offset = sb . buffer_mut ( ) . push ( data ) . map_err ( | e | map_buffer_error ( e ) ) ? ; // Convert target image from Undefined to TransferDst. let barrier = get_layout_transition_pipeline_barrier ( image . object ( ) , image . format ( ) , ImageLayout :: Undefined , ImageLayout :: TransferDstOptimal , ) ; // Undefined -> TransferDstOptimal sb . begin ( ) . pipeline_barrier ( barrier ) ; image . set_layout ( ImageLayout :: TransferDstOptimal ) ; sb . copy_buffer_to_image ( offset , & image ) . end ( ) . submit_and_wait_device ( ) ; let barrier = get_layout_transition_pipeline_barrier ( image . object ( ) , image . format ( ) , ImageLayout :: TransferDstOptimal , ImageLayout :: ShaderReadOnlyOptimal , ) ; // TransferDstOptimal -> ShaderReadOnlyOptimal sb . reset ( ) . begin ( ) . pipeline_barrier ( barrier ) . end ( ) . submit_and_wait_device ( ) ; image . set_layout ( ImageLayout :: ShaderReadOnlyOptimal ) ; // Create VkImageView. let image_view = create_image_view ( & vd , image . object ( ) , ImageAspect :: Color , image . format ( ) , ImageViewType :: V2D , 1 , ) ; // Update descriptor set. let image_info = VkDescriptorImageInfo { sampler , image_view , image_layout : ImageLayout :: ShaderReadOnlyOptimal . into ( ) , } ; let writer = VkWriteDescriptorSet { dst_set : descriptor_set . object ( ) , dst_binding : 0 , dst_array_element : 0 , descriptor_count : 1 , descriptor_type : DescriptorType :: CombinedImageSampler . into ( ) , image_info : & image_info , .. Default :: default ( ) } ; vd . write_descriptor_set ( & writer ) ; Ok ( GpuTexture2D { image , image_view , sharing_mode : SharingMode :: Exclusive , descriptor : VkDescriptorImageInfo { sampler , image_view , image_layout : ImageLayout :: ShaderReadOnlyOptimal . into ( ) , } , mip_level : 1 , descriptor_set , } ) }
Make a GpuTexture and upload image data to GPU.
Make a GpuTexture and upload image data to GPU .
sbp
&mut StagingBufferPool
descriptor_set
DescriptorSet
data
Data
extent
Extent2D
ty
ImageType
_format
VulkanFormat
sampler
VkSampler
samples
SampleCount
sbp
&mut StagingBufferPool
descriptor_set
DescriptorSet
data
Data
extent
Extent2D
ty
ImageType
_format
VulkanFormat
sampler
VkSampler
samples
SampleCount