Răsfoiți Sursa

Was working, but then i tried to copy textures

Ghastrod 1 an în urmă
părinte
comite
45ded4de9c

+ 11 - 5
src/compute/compute_pipeline.rs

@@ -1,8 +1,9 @@
 use serde::de;
-use wgpu::{include_wgsl, util::DeviceExt, BindGroup, BufferUsages, ComputePipeline};
+use wgpu::{include_wgsl, util::DeviceExt, BindGroup, BufferUsages, ComputePipeline, Texture};
 
-pub const SIZE:u32 = 256;
-pub fn new_compute_pipeline(device: &wgpu::Device)-> (ComputePipeline,BindGroup,(u32,u32)){
+use super::SIZE;
+
+pub fn new_compute_pipeline(device: &wgpu::Device)-> (ComputePipeline,BindGroup,(u32,u32), Texture){
 
     // let u32_size = std::mem::size_of::<u64>() as wgpu::BufferAddress;
     // let time_buffer = device.create_buffer(&wgpu::BufferDescriptor{
@@ -34,7 +35,12 @@ pub fn new_compute_pipeline(device: &wgpu::Device)-> (ComputePipeline,BindGroup,
         view_formats: &[]
     });
 
-    let output_texture_view = output_texture.create_view(&wgpu::TextureViewDescriptor::default());
+    let output_texture_view = output_texture.create_view(&wgpu::TextureViewDescriptor{
+        label: Some("Output texture view"),
+        mip_level_count: Some(1),
+        base_mip_level: 0,
+        ..Default::default()
+    });
 
 
 
@@ -81,7 +87,7 @@ pub fn new_compute_pipeline(device: &wgpu::Device)-> (ComputePipeline,BindGroup,
     });
 
     let dispatch = compute_work_group_count((SIZE,SIZE), (16,16));
-    return (compute_pipeline,heightmap_texture_bind_group,dispatch)
+    return (compute_pipeline,heightmap_texture_bind_group,dispatch, output_texture)
 }
 
 pub fn compute_work_group_count(

+ 4 - 1
src/compute/mod.rs

@@ -1 +1,4 @@
-pub mod compute_pipeline;
+pub mod compute_pipeline;
+pub mod visualisation;
+
+pub const SIZE:u32 = 256;

+ 33 - 0
src/compute/visualisation.rs

@@ -0,0 +1,33 @@
+use wgpu::{Texture, TextureView};
+
+use super::SIZE;
+
+
+
+pub fn new(device: &wgpu::Device)-> (Texture,TextureView){
+
+    let visualisation_texture_size = wgpu::Extent3d{
+        width: SIZE,
+        height:SIZE,
+        depth_or_array_layers: 1
+    };
+    let visualization_texture = device.create_texture(&wgpu::TextureDescriptor {
+        label: Some("Visualization Texture"),
+        size: visualisation_texture_size, // Same size as compute shader output texture
+        mip_level_count: 1,
+        sample_count: 1,
+        dimension: wgpu::TextureDimension::D2,
+        format: wgpu::TextureFormat::R32Float, // Same format as compute shader output texture
+        usage: wgpu::TextureUsages::TEXTURE_BINDING | wgpu::TextureUsages::COPY_DST | wgpu::TextureUsages::COPY_SRC,
+        view_formats: &[],
+    });
+    
+    let visualization_texture_view = visualization_texture.create_view(&wgpu::TextureViewDescriptor{
+        label: Some("Visualisation texture view"),
+        mip_level_count: Some(1),
+        base_mip_level: 0,
+        ..Default::default()
+    });
+    
+    return (visualization_texture,visualization_texture_view)
+}

+ 30 - 4
src/lib.rs

@@ -1,7 +1,8 @@
 use std::{iter, time::{Duration, Instant}};
 
 use camera::{camera_buffer, camera_controller, camera_struct::Camera, camera_uniform};
-use wgpu::{include_wgsl, util::DeviceExt, BlendState, ColorTargetState, ComputePipeline, PipelineLayoutDescriptor, RenderPipeline, RenderPipelineDescriptor};
+use compute::SIZE;
+use wgpu::{include_wgsl, util::DeviceExt, BlendState, ColorTargetState, ComputePipeline, PipelineLayoutDescriptor, RenderPipeline, RenderPipelineDescriptor, Texture, TextureView};
 use winit::{
     event::*,
     event_loop::EventLoop,
@@ -48,10 +49,13 @@ struct State<'a> {
     last_fps: Instant,
     //Heightmap Stuff
     texture_bind_group: wgpu::BindGroup,
-    diffuse_texture: texture::texture_struct::Texture,
+    diffuse_texture: texture::texture_struct::Texture2,
     compute_pipeline: ComputePipeline,
     heightmap2_bindgroup: wgpu::BindGroup,
     dispatch: (u32,u32),
+    heightmap2_texture: wgpu::Texture,
+    visualisation_texture: Texture,
+    visualisation_texture_view: TextureView,
 }
 
 impl<'a> State<'a> {
@@ -151,7 +155,7 @@ impl<'a> State<'a> {
 
         //Heightmap texture
         let diffuse_bytes = include_bytes!("./images/heightmap.jpg");
-        let diffuse_texture = texture::texture_struct::Texture::from_bytes(&device, &queue, diffuse_bytes, "Heightmap texture").unwrap();
+        let diffuse_texture = texture::texture_struct::Texture2::from_bytes(&device, &queue, diffuse_bytes, "Heightmap texture").unwrap();
         let diffuse_bind_group_layout = device.create_bind_group_layout(&wgpu::BindGroupLayoutDescriptor{
             label: Some("Heightmap bind group layout"),
             entries:&[
@@ -239,6 +243,7 @@ impl<'a> State<'a> {
         let wireframe = true;
 
         let x = compute::compute_pipeline::new_compute_pipeline(&device);
+        let y = compute::visualisation::new(&device);
         //Compute
         Self {
             wireframe,
@@ -271,7 +276,10 @@ impl<'a> State<'a> {
             texture_bind_group: diffuse_bind_group,
             compute_pipeline: x.0,
             heightmap2_bindgroup: x.1,
-            dispatch: x.2
+            dispatch: x.2,
+            heightmap2_texture: x.3,
+            visualisation_texture: y.0,
+            visualisation_texture_view: y.1,
         }
     }
 
@@ -333,6 +341,24 @@ impl<'a> State<'a> {
             compute_pass.dispatch_workgroups(self.dispatch.0, self.dispatch.1, 1);
         }
 
+        encoder.copy_texture_to_texture(wgpu::ImageCopyTextureBase{
+            texture: &self.heightmap2_texture,
+            mip_level: 1,
+            origin: wgpu::Origin3d::ZERO,
+            aspect: wgpu::TextureAspect::All
+        },
+        wgpu::ImageCopyTextureBase{
+            texture: &self.visualisation_texture,
+            mip_level: 1,
+            origin: wgpu::Origin3d::ZERO,
+            aspect: wgpu::TextureAspect::All
+        },
+        wgpu::Extent3d{
+            width: SIZE,
+            height: SIZE,
+            depth_or_array_layers: 1
+        }
+        );
 
         {
             let mut render_pass = encoder.begin_render_pass(&wgpu::RenderPassDescriptor {

+ 2 - 2
src/texture/texture_struct.rs

@@ -1,13 +1,13 @@
 use anyhow::*;
 use image::GenericImageView;
 
-pub struct Texture {
+pub struct Texture2 {
     pub texture: wgpu::Texture,
     pub view: wgpu::TextureView,
     pub sampler: wgpu::Sampler,
 }
 
-impl Texture {
+impl Texture2 {
     pub fn from_bytes(
         device: &wgpu::Device,
         queue: &wgpu::Queue,