Parcourir la source

Compute pipeline works i think, dont really know, now i need to put this texture into the vertex shader

Ghastrod il y a 1 an
Parent
commit
6d089e8ab9
3 fichiers modifiés avec 11 ajouts et 10 suppressions
  1. 5 5
      src/compute/compute_pipeline.rs
  2. 1 1
      src/lib.rs
  3. 5 4
      src/shaders/Heightmap_compute.wgsl

+ 5 - 5
src/compute/compute_pipeline.rs

@@ -21,7 +21,7 @@ pub fn new_compute_pipeline(device: &wgpu::Device)-> (ComputePipeline,BindGroup,
     let output_texture_size = wgpu::Extent3d{
         width: SIZE,
         height: SIZE,
-        depth_or_array_layers: 0
+        depth_or_array_layers: 1
     };
     let output_texture = device.create_texture(&wgpu::TextureDescriptor{
         label: Some("Output Heightmap Texture"),
@@ -29,7 +29,7 @@ pub fn new_compute_pipeline(device: &wgpu::Device)-> (ComputePipeline,BindGroup,
         mip_level_count: 1,
         sample_count: 1,
         dimension: wgpu::TextureDimension::D2,
-        format: wgpu::TextureFormat::Rgba8UnormSrgb,
+        format: wgpu::TextureFormat::R32Float,
         usage: wgpu::TextureUsages::STORAGE_BINDING | wgpu::TextureUsages::COPY_DST | wgpu::TextureUsages::COPY_SRC,
         view_formats: &[]
     });
@@ -45,8 +45,8 @@ pub fn new_compute_pipeline(device: &wgpu::Device)-> (ComputePipeline,BindGroup,
                 binding: 0,
                 visibility: wgpu::ShaderStages::COMPUTE | wgpu::ShaderStages::VERTEX,
                 ty: wgpu::BindingType::StorageTexture{
-                    access: wgpu::StorageTextureAccess::ReadWrite,
-                    format: wgpu::TextureFormat::Rgba8UnormSrgb,
+                    access: wgpu::StorageTextureAccess::WriteOnly,
+                    format: wgpu::TextureFormat::R32Float,
                     view_dimension: wgpu::TextureViewDimension::D2
                 },
                 count: None
@@ -77,7 +77,7 @@ pub fn new_compute_pipeline(device: &wgpu::Device)-> (ComputePipeline,BindGroup,
         label: Some("Compute Pipeline 1"),
         layout: Some(&compute_pipeline_layout),
         module: &compute_shader,
-        entry_point: "@compute"
+        entry_point: "compute_main"
     });
 
     let dispatch = compute_work_group_count((SIZE,SIZE), (16,16));

+ 1 - 1
src/lib.rs

@@ -83,7 +83,7 @@ impl<'a> State<'a> {
             .request_device(
                 &wgpu::DeviceDescriptor {
                     label: None,
-                    required_features: wgpu::Features::POLYGON_MODE_LINE,
+                    required_features: wgpu::Features::POLYGON_MODE_LINE | wgpu::Features::VERTEX_WRITABLE_STORAGE,
                     // WebGL doesn't support all of wgpu's features, so if
                     // we're building for the web we'll have to disable some.
                     required_limits: if cfg!(target_arch = "wasm32") {

+ 5 - 4
src/shaders/Heightmap_compute.wgsl

@@ -39,22 +39,23 @@ struct ComputeInput{
 
 //Actual code
 
-@group(0) @binding(0) var heightmap_texture: texture_2d<rgba8unorm, write>;
+//@group(0) @binding(0) var heightmap_texture: texture_2d<f32>;
+@group(0) @binding(0) var heightmap_texture: texture_storage_2d<r32float, write>;
 fn gaussian(x: f32, y:f32)-> f32{
     let mean = 0.0;
     let spread = 1.0;
     let actual_x = x - mean;
     let actual_y = y - mean;
     let sigma_squared = spread * spread;
-    return (1 / (sqrt(2 * PI)* spread)) * exp(-((actual_x * actual_x) + (actual_y * actual_y)) / (2 * sigmaSqu));
+    return (1 / (sqrt(2 * PI)* spread)) * exp(-((actual_x * actual_x) + (actual_y * actual_y)) / (2 * sigma_squared));
 }
 
 @compute @workgroup_size(1,1)
 fn compute_main(in: ComputeInput){
-    let coordonnees = in.global_invocation_index.xy;
+    let coordonnees = in.global_invocation_id.xy;
     let dimensions = textureDimensions(heightmap_texture);
 
-    let pixel_color_1_channel = sin(coordonnees.x + coordonnees.y);
+    let pixel_color_1_channel = sin(f32(coordonnees.x) + f32(coordonnees.y));
 
     let vec4_pixel_color = vec4<f32>(pixel_color_1_channel);