#[Compute Shader]How to move texture from gpu world to cpu

1 messages · Page 1 of 1 (latest)

brave garnet
#

OH I AM DUMB ASS

#

I'ts just one post under me

#
pub fn setup(mut commands: Commands, mut images: ResMut<Assets<Image>>) {
    let mut image = Image::new_target_texture(SIZE.width, SIZE.height, TextureFormat::Rgba8Unorm);

    image.texture_descriptor.usage =
        TextureUsages::COPY_DST       // Destination for a copy (maybe from another texture)
            | TextureUsages::STORAGE_BINDING  // Writable by the compute shader
            | TextureUsages::TEXTURE_BINDING  // Readable by a regular rendering shader
            | TextureUsages::COPY_SRC;        // ALLOWS THE GPU TO COPY *FROM* THIS TEXTURE

    let perlin_handle = images.add(image);

    // Insert your resource so other systems can find the handle
    commands.insert_resource(NoiseImageOutput {
        perlin_texture: perlin_handle.clone(),
    });

    // Don't forget to add the shader settings resource
    commands.insert_resource(NoiseShaderSettings {
        frequency: 0.02,
        amplitude: 1.0,
    });

    commands.spawn(Readback::texture(perlin_handle)).observe( // <- magic here
        |trigger: Trigger<ReadbackComplete>| {
            // You probably want to interpret the data as a color rather than a `ShaderType`,
            // but in this case we know the data is a single channel storage texture, so we can
            // interpret it as a `Vec<u32>`
            let data: Vec<u32> = trigger.event().to_shader_type();
            info!("Image {:?}", data);
        },
    );
}