Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

vk: create Sampler from raw handle #3221

Closed
wants to merge 1 commit into from
Closed

Conversation

jinleili
Copy link
Contributor

Checklist

  • Run cargo clippy.
  • Run RUSTFLAGS=--cfg=web_sys_unstable_apis cargo clippy --target wasm32-unknown-unknown if applicable.
  • Add change to CHANGELOG.md. See simple instructions inside file.

Connections
#274
#1733

Description
With xx_from_raw and create_xx_from_hal, wgpu can now create Texture from external memory with zero-copy, when the external memory is Android camera data, it is necessary to chain the pNext of vkSampler and vkTextureView to the vkSamplerYcbcrConversionInfo object.

Testing
Tested locally with the following code:

let (vk_image, vk_sampler) = ac.device.as_hal::<Vulkan, _, _>(|device| {
    let device = device.unwrap().raw_device();
    // ...
    let hal_sampler = create_hal_sampler(device, &external_format, &format_info);
    (image, hal_sampler)
});

let sampler = ac.device.create_sampler_from_hal::<Vulkan>(
    hal_sampler, &wgpu::SamplerDescriptor::default()
);

// ...
unsafe fn create_hal_sampler(
    raw_device: &ash::Device,
    external_format: &ExternalFormatANDROID,
    format_info: &AndroidHardwareBufferFormatPropertiesANDROID,
) -> <Vulkan as Api>::Sampler {
    let conv_info = vk::SamplerYcbcrConversionCreateInfo {
        s_type: StructureType::SAMPLER_YCBCR_CONVERSION_CREATE_INFO,
        p_next: <*const ExternalFormatANDROID>::cast(external_format),
        format: format_info.format,
        ycbcr_model: match format_info.format {
            Format::UNDEFINED => format_info.suggested_ycbcr_model,
            // SD YUV
            _ => vk::SamplerYcbcrModelConversion::YCBCR_601,
        },
        ycbcr_range: format_info.suggested_ycbcr_range,
        components: format_info.sampler_ycbcr_conversion_components,
        x_chroma_offset: format_info.suggested_x_chroma_offset,
        y_chroma_offset: format_info.suggested_y_chroma_offset,
        chroma_filter: vk::Filter::LINEAR,
        force_explicit_reconstruction: vk::Bool32::default(),
    };

    let mut sampler_info = vk::SamplerCreateInfo {
        s_type: StructureType::SAMPLER_CREATE_INFO,
        p_next: std::ptr::null(),
        flags: vk::SamplerCreateFlags::default(),
        mag_filter: vk::Filter::LINEAR,
        min_filter: vk::Filter::LINEAR,
        mipmap_mode: SamplerMipmapMode::default(),
        address_mode_u: SamplerAddressMode::default(),
        address_mode_v: SamplerAddressMode::default(),
        address_mode_w: SamplerAddressMode::default(),
        mip_lod_bias: 0.0,
        anisotropy_enable: Bool32::default(),
        max_anisotropy: 1.0,
        compare_enable: Bool32::default(),
        compare_op: vk::CompareOp::NEVER,
        min_lod: 0.0,
        max_lod: 0.0,
        border_color: vk::BorderColor::default(),
        unnormalized_coordinates: Bool32::default(),
    };

    let conv_sampler_info = vk::SamplerYcbcrConversionInfo {
        s_type: StructureType::SAMPLER_YCBCR_CONVERSION_INFO,
        p_next: ::std::ptr::null(),
        conversion: raw_device
            .create_sampler_ycbcr_conversion(&conv_info, None)
            .expect("Cannot create sampler ycbcr conversion"),
    };

    sampler_info.p_next = <*const vk::SamplerYcbcrConversionInfo>::cast(&conv_sampler_info);

    let vk_sampler = raw_device
        .create_sampler(&sampler_info, None)
        .expect("Cannot create vk sampler");

    <<Vulkan as Api>::Device>::sampler_from_raw(vk_sampler)
}

@codecov-commenter
Copy link

codecov-commenter commented Nov 19, 2022

Codecov Report

Merging #3221 (4163950) into master (04d12ba) will decrease coverage by 0.13%.
The diff coverage is 61.39%.

@@            Coverage Diff             @@
##           master    #3221      +/-   ##
==========================================
- Coverage   64.70%   64.57%   -0.14%     
==========================================
  Files          81       81              
  Lines       38819    39551     +732     
==========================================
+ Hits        25118    25539     +421     
- Misses      13701    14012     +311     
Impacted Files Coverage Δ
player/src/lib.rs 53.31% <ø> (ø)
wgpu-core/src/id.rs 81.25% <0.00%> (-2.63%) ⬇️
wgpu-core/src/lib.rs 96.55% <ø> (ø)
wgpu-core/src/present.rs 0.00% <0.00%> (ø)
wgpu-core/src/resource.rs 25.13% <ø> (ø)
wgpu-hal/src/dx11/device.rs 0.00% <0.00%> (ø)
wgpu-hal/src/empty.rs 0.00% <ø> (ø)
wgpu-hal/src/lib.rs 26.21% <ø> (ø)
wgpu/src/backend/direct.rs 53.83% <0.00%> (-1.28%) ⬇️
wgpu/src/lib.rs 49.89% <0.00%> (-2.93%) ⬇️
... and 44 more

Help us with your feedback. Take ten seconds to tell us how you rate us. Have a feature suggestion? Share it here.

@jinleili jinleili closed this Nov 19, 2022
@jinleili
Copy link
Contributor Author

#3120 has covered this PR

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants