Procházet zdrojové kódy

Add Fps struct and logic for calculating frames per second

Ghastrod před 1 rokem
rodič
revize
9f3d3557bd
6 změnil soubory, kde provedl 113 přidání a 12 odebrání
  1. 28 2
      src/fps.rs
  2. 2 0
      src/fps_counter.rs
  3. 1 0
      src/main.rs
  4. 17 9
      src/state.rs
  5. 28 0
      src/vertex.rs
  6. 37 1
      src/vertex_buffer.rs

+ 28 - 2
src/fps.rs

@@ -1,3 +1,29 @@
-pub fn fps(&mut frame_count, &self.start_time){
+use std::time::Instant;
 
-}
+pub struct Fps {
+  start_time: Instant,
+  frame_count: u32,
+}
+
+impl Fps {
+  pub fn new() -> Self {
+    Self {
+      start_time: Instant::now(),
+      frame_count: 0,
+    }
+  }
+
+  pub fn update(&mut self) {
+    self.frame_count += 1;
+  }
+
+  pub fn get_fps(&self) -> f32 {
+    let elapsed = self.start_time.elapsed();
+    let seconds = elapsed.as_secs_f32();
+    if seconds > 0.0 {
+      self.frame_count as f32 / seconds
+    } else {
+      0.0
+    }
+  }
+}

+ 2 - 0
src/fps_counter.rs

@@ -0,0 +1,2 @@
+use crate::fps::Fps;
+

+ 1 - 0
src/main.rs

@@ -7,6 +7,7 @@ mod camera_controller;
 mod camera_uniform;
 mod camera_controller2;
 mod fps;
+mod fps_counter;
 
 fn main() {
     pollster::block_on(window::run());

+ 17 - 9
src/state.rs

@@ -3,7 +3,7 @@ use std::iter;
 use wgpu::util::DeviceExt;
 use winit::{event::WindowEvent, window::Window};
 
-use crate::{camera::{self, Camera}, camera_controller, camera_uniform, fps::fps, vertex::Vertex, vertex_buffer::{VERT2, VERT3, VERTICES}};
+use crate::{camera::{self, Camera}, camera_controller, camera_uniform, fps::Fps, vertex::Vertex, vertex_buffer::{VERT2, VERT3, VERTICES}};
 
 
 pub struct State<'a> {
@@ -24,8 +24,8 @@ pub struct State<'a> {
     camera_uniform: camera_uniform::CameraUniform,
     camera_bind_group: wgpu::BindGroup,
     camera_controller: camera_controller::CameraController,
-    frame_count: u32,
-    start_time: std::time::Instant,
+    fps: Fps,
+    last_fps_time: std::time::Instant,
 }
 
 impl<'a> State<'a> {
@@ -227,7 +227,8 @@ impl<'a> State<'a> {
 
 
         //Start time
-        let start_time = std::time::Instant::now();
+        let fps = Fps::new();
+        let last_fps_time = std::time::Instant::now();
         Self {
             surface,
             device,
@@ -243,8 +244,8 @@ impl<'a> State<'a> {
             camera_uniform,
             camera_bind_group,
             camera_controller,
-            start_time,
-            frame_count: 0,
+            fps,
+            last_fps_time,
         }
     }
 
@@ -267,12 +268,19 @@ impl<'a> State<'a> {
     }
 
     pub fn update(&mut self) {
-        self.frame_count += 1;
+        let frame_start = std::time::Instant::now();
+        //Camera logic
         self.camera_controller.update_camera(&mut self.camera);
         self.camera_uniform.update_view_proj(&self.camera);
         self.queue.write_buffer(&self.camera_buffer, 0, bytemuck::cast_slice(&[self.camera_uniform]));
-
-        
+        //Fps logic
+        let frame_time = frame_start.elapsed().as_secs_f32();
+        let now = std::time::Instant::now();
+        if now.duration_since(self.last_fps_time).as_secs_f32() >= 1.0 {
+            self.fps.update();
+            println!("FPS: {:.2}", frame_time*100000.0);
+            self.last_fps_time = now;
+        }
     }
 
     pub fn render(&mut self) -> Result<(), wgpu::SurfaceError> {

+ 28 - 0
src/vertex.rs

@@ -24,4 +24,32 @@ impl Vertex {
             ]
         }
     }
+}
+
+#[repr(C)]
+#[derive(Debug, Copy, Clone, bytemuck::Pod, bytemuck::Zeroable)]
+pub struct Vertex2 {
+    pub position: [f32; 3],
+    pub uv: [f32; 2],
+}
+
+impl Vertex2 {
+    pub fn desc() -> wgpu::VertexBufferLayout<'static> {
+        wgpu::VertexBufferLayout {
+            array_stride: std::mem::size_of::<Vertex>() as wgpu::BufferAddress,
+            step_mode: wgpu::VertexStepMode::Vertex,
+            attributes: &[
+                wgpu::VertexAttribute {
+                    offset: 0,
+                    shader_location: 0,
+                    format: wgpu::VertexFormat::Float32x3,
+                },
+                wgpu::VertexAttribute {
+                    offset: std::mem::size_of::<[f32; 2]>() as wgpu::BufferAddress,
+                    shader_location: 1,
+                    format: wgpu::VertexFormat::Float32x2,
+                }
+            ]
+        }
+    }
 }

+ 37 - 1
src/vertex_buffer.rs

@@ -1,4 +1,4 @@
-use crate::vertex::Vertex;
+use crate::vertex::{Vertex, Vertex2};
 //Debug Triangle
 pub const VERTICES: &[Vertex] = &[
     Vertex { position: [0.0, 0.5, 0.0], color: [1.0, 0.0, 0.0] },
@@ -30,3 +30,39 @@ pub const VERT3 : &[Vertex] = &[
     Vertex { position: [-0.5, 0.5, 0.0], color: [0.0, 0.0, 1.0] },
 ];
 
+fn subdivide_plane(device: &wgpu::Device, vertices: &mut Vec<Vertex2>, x_min: f32, x_max: f32, y_min: f32, y_max: f32, level: u32) {
+    if level == 0 {
+      let half_width = (x_max - x_min) / 2.0;
+      let half_height = (y_max - y_min) / 2.0;
+      
+      // Generate vertices for a single quad based on (x_min, y_min) and (x_max, y_max)
+      vertices.push(Vertex2 { position: [x_min, y_max, 0.0], uv: [0.0, 0.0] }); // Top-left
+      vertices.push(Vertex2 { position: [x_max, y_max, 0.0], uv: [1.0, 0.0] }); // Top-right
+      vertices.push(Vertex2 { position: [x_max, y_min, 0.0], uv: [1.0, 1.0] }); // Bottom-right
+      vertices.push(Vertex2 { position: [x_min, y_min, 0.0], uv: [0.0, 1.0] }); // Bottom-left    
+    } else {
+      let mid_x = (x_min + x_max) / 2.0;
+      let mid_y = (y_min + y_max) / 2.0;
+  
+      subdivide_plane(device, vertices, x_min, mid_x, y_min, mid_y, level - 1);
+      subdivide_plane(device, vertices, mid_x, x_max, y_min, mid_y, level - 1);
+      subdivide_plane(device, vertices, x_min, mid_x, mid_y, y_max, level - 1);
+      subdivide_plane(device, vertices, mid_x, x_max, mid_y, y_max, level - 1);
+  
+      // Alternatively, to create vertex buffer on each recursive call (less memory usage on CPU):
+      // let mut sub_vertices = Vec::new();
+      // subdivide_plane(device, &mut sub_vertices, x_min, mid_x, y_min, mid_y, level - 1);
+      // // ... repeat for other quadrants
+      // 
+      // // Create vertex buffer on GPU from sub_vertices
+      // let vertex_buffer = device.create_buffer(&wgpu::BufferDescriptor {
+      //   usage: wgpu::BufferUsages::VERTEX,
+      //   mapping: wgpu::BufferMapping::WRITE,
+      //   size: sub_vertices.len() * std::mem::size_of::<Vertex>(),
+      // });
+      // let mut buffer_slice = vertex_buffer.slice(..);
+      // buffer_slice.write(&sub_vertices);
+      // // ... (use vertex_buffer for rendering)
+    }
+  }
+