From 811aa673e9e832ebe38bf56a087f32fdc7aba59c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?H=C3=A9ctor=20Ram=C3=B3n=20Jim=C3=A9nez?= Date: Tue, 14 Nov 2023 15:48:01 +0100 Subject: [PATCH] Improve module hierarchy of `custom_shader` example --- examples/custom_shader/src/main.rs | 7 +- examples/custom_shader/src/primitive.rs | 97 ----------------- examples/custom_shader/src/scene.rs | 103 ++++++++++++++++-- .../custom_shader/src/{ => scene}/camera.rs | 0 .../custom_shader/src/{ => scene}/pipeline.rs | 35 +++--- .../{primitive => scene/pipeline}/buffer.rs | 0 .../src/{primitive => scene/pipeline}/cube.rs | 2 +- .../{primitive => scene/pipeline}/uniforms.rs | 3 +- .../{primitive => scene/pipeline}/vertex.rs | 0 widget/src/shader.rs | 1 + 10 files changed, 122 insertions(+), 126 deletions(-) delete mode 100644 examples/custom_shader/src/primitive.rs rename examples/custom_shader/src/{ => scene}/camera.rs (100%) rename examples/custom_shader/src/{ => scene}/pipeline.rs (96%) rename examples/custom_shader/src/{primitive => scene/pipeline}/buffer.rs (100%) rename examples/custom_shader/src/{primitive => scene/pipeline}/cube.rs (99%) rename examples/custom_shader/src/{primitive => scene/pipeline}/uniforms.rs (95%) rename examples/custom_shader/src/{primitive => scene/pipeline}/vertex.rs (100%) diff --git a/examples/custom_shader/src/main.rs b/examples/custom_shader/src/main.rs index f4853507..2eb1ac4a 100644 --- a/examples/custom_shader/src/main.rs +++ b/examples/custom_shader/src/main.rs @@ -1,11 +1,6 @@ -mod camera; -mod pipeline; -mod primitive; mod scene; -use crate::camera::Camera; -use crate::pipeline::Pipeline; -use crate::scene::Scene; +use scene::Scene; use iced::executor; use iced::time::Instant; diff --git a/examples/custom_shader/src/primitive.rs b/examples/custom_shader/src/primitive.rs deleted file mode 100644 index f5862ab3..00000000 --- a/examples/custom_shader/src/primitive.rs +++ /dev/null @@ -1,97 +0,0 @@ -pub mod cube; -pub mod vertex; - -mod buffer; -mod uniforms; - -pub use buffer::Buffer; -pub use cube::Cube; -pub use uniforms::Uniforms; -pub use vertex::Vertex; - -use crate::wgpu; -use crate::Camera; -use crate::Pipeline; - -use iced::advanced::graphics::Transformation; -use iced::widget::shader; -use iced::{Color, Rectangle, Size}; - -/// A collection of `Cube`s that can be rendered. -#[derive(Debug)] -pub struct Primitive { - cubes: Vec, - uniforms: Uniforms, - show_depth_buffer: bool, -} - -impl Primitive { - pub fn new( - cubes: &[Cube], - camera: &Camera, - bounds: Rectangle, - show_depth_buffer: bool, - light_color: Color, - ) -> Self { - let uniforms = Uniforms::new(camera, bounds, light_color); - - Self { - cubes: cubes - .iter() - .map(cube::Raw::from_cube) - .collect::>(), - uniforms, - show_depth_buffer, - } - } -} - -impl shader::Primitive for Primitive { - fn prepare( - &self, - format: wgpu::TextureFormat, - device: &wgpu::Device, - queue: &wgpu::Queue, - target_size: Size, - _scale_factor: f32, - _transform: Transformation, - storage: &mut shader::Storage, - ) { - if !storage.has::() { - storage.store(Pipeline::new(device, queue, format, target_size)); - } - - let pipeline = storage.get_mut::().unwrap(); - - //upload data to GPU - pipeline.update( - device, - queue, - target_size, - &self.uniforms, - self.cubes.len(), - &self.cubes, - ); - } - - fn render( - &self, - storage: &shader::Storage, - bounds: Rectangle, - target: &wgpu::TextureView, - _target_size: Size, - encoder: &mut wgpu::CommandEncoder, - ) { - //at this point our pipeline should always be initialized - let pipeline = storage.get::().unwrap(); - - //render primitive - pipeline.render( - target, - encoder, - bounds, - self.cubes.len() as u32, - self.show_depth_buffer, - ); - } -} diff --git a/examples/custom_shader/src/scene.rs b/examples/custom_shader/src/scene.rs index ab923093..3b291ce2 100644 --- a/examples/custom_shader/src/scene.rs +++ b/examples/custom_shader/src/scene.rs @@ -1,13 +1,21 @@ -use crate::camera::Camera; -use crate::primitive; -use crate::primitive::cube::Cube; -use glam::Vec3; +mod camera; +mod pipeline; + +use camera::Camera; +use pipeline::Pipeline; + +use crate::wgpu; +use pipeline::cube::{self, Cube}; + +use iced::mouse; +use iced::time::Duration; use iced::widget::shader; -use iced::{mouse, Color, Rectangle}; +use iced::{Color, Rectangle, Size}; + +use glam::Vec3; use rand::Rng; use std::cmp::Ordering; use std::iter; -use std::time::Duration; pub const MAX: u32 = 500; @@ -72,7 +80,7 @@ impl Scene { impl shader::Program for Scene { type State = (); - type Primitive = primitive::Primitive; + type Primitive = Primitive; fn draw( &self, @@ -80,7 +88,7 @@ impl shader::Program for Scene { _cursor: mouse::Cursor, bounds: Rectangle, ) -> Self::Primitive { - primitive::Primitive::new( + Primitive::new( &self.cubes, &self.camera, bounds, @@ -90,6 +98,85 @@ impl shader::Program for Scene { } } +/// A collection of `Cube`s that can be rendered. +#[derive(Debug)] +pub struct Primitive { + cubes: Vec, + uniforms: pipeline::Uniforms, + show_depth_buffer: bool, +} + +impl Primitive { + pub fn new( + cubes: &[Cube], + camera: &Camera, + bounds: Rectangle, + show_depth_buffer: bool, + light_color: Color, + ) -> Self { + let uniforms = pipeline::Uniforms::new(camera, bounds, light_color); + + Self { + cubes: cubes + .iter() + .map(cube::Raw::from_cube) + .collect::>(), + uniforms, + show_depth_buffer, + } + } +} + +impl shader::Primitive for Primitive { + fn prepare( + &self, + format: wgpu::TextureFormat, + device: &wgpu::Device, + queue: &wgpu::Queue, + target_size: Size, + _scale_factor: f32, + _transform: shader::Transformation, + storage: &mut shader::Storage, + ) { + if !storage.has::() { + storage.store(Pipeline::new(device, queue, format, target_size)); + } + + let pipeline = storage.get_mut::().unwrap(); + + //upload data to GPU + pipeline.update( + device, + queue, + target_size, + &self.uniforms, + self.cubes.len(), + &self.cubes, + ); + } + + fn render( + &self, + storage: &shader::Storage, + bounds: Rectangle, + target: &wgpu::TextureView, + _target_size: Size, + encoder: &mut wgpu::CommandEncoder, + ) { + //at this point our pipeline should always be initialized + let pipeline = storage.get::().unwrap(); + + //render primitive + pipeline.render( + target, + encoder, + bounds, + self.cubes.len() as u32, + self.show_depth_buffer, + ); + } +} + fn rnd_origin() -> Vec3 { Vec3::new( rand::thread_rng().gen_range(-4.0..4.0), diff --git a/examples/custom_shader/src/camera.rs b/examples/custom_shader/src/scene/camera.rs similarity index 100% rename from examples/custom_shader/src/camera.rs rename to examples/custom_shader/src/scene/camera.rs diff --git a/examples/custom_shader/src/pipeline.rs b/examples/custom_shader/src/scene/pipeline.rs similarity index 96% rename from examples/custom_shader/src/pipeline.rs rename to examples/custom_shader/src/scene/pipeline.rs index 9343e5e0..0967e139 100644 --- a/examples/custom_shader/src/pipeline.rs +++ b/examples/custom_shader/src/scene/pipeline.rs @@ -1,6 +1,15 @@ -use crate::primitive; -use crate::primitive::cube; -use crate::primitive::{Buffer, Uniforms}; +pub mod cube; + +mod buffer; +mod uniforms; +mod vertex; + +pub use cube::Cube; +pub use uniforms::Uniforms; + +use buffer::Buffer; +use vertex::Vertex; + use crate::wgpu; use crate::wgpu::util::DeviceExt; @@ -221,7 +230,7 @@ impl Pipeline { device.create_shader_module(wgpu::ShaderModuleDescriptor { label: Some("cubes shader"), source: wgpu::ShaderSource::Wgsl(std::borrow::Cow::Borrowed( - include_str!("shaders/cubes.wgsl"), + include_str!("../shaders/cubes.wgsl"), )), }); @@ -232,7 +241,7 @@ impl Pipeline { vertex: wgpu::VertexState { module: &shader, entry_point: "vs_main", - buffers: &[primitive::Vertex::desc(), cube::Raw::desc()], + buffers: &[Vertex::desc(), cube::Raw::desc()], }, primitive: wgpu::PrimitiveState::default(), depth_stencil: Some(wgpu::DepthStencilState { @@ -468,7 +477,7 @@ impl DepthPipeline { device.create_shader_module(wgpu::ShaderModuleDescriptor { label: Some("cubes.depth_pipeline.shader"), source: wgpu::ShaderSource::Wgsl(std::borrow::Cow::Borrowed( - include_str!("shaders/depth.wgsl"), + include_str!("../shaders/depth.wgsl"), )), }); @@ -573,12 +582,12 @@ impl DepthPipeline { } fn load_skybox_data() -> Vec { - let pos_x: &[u8] = include_bytes!("textures/skybox/pos_x.jpg"); - let neg_x: &[u8] = include_bytes!("textures/skybox/neg_x.jpg"); - let pos_y: &[u8] = include_bytes!("textures/skybox/pos_y.jpg"); - let neg_y: &[u8] = include_bytes!("textures/skybox/neg_y.jpg"); - let pos_z: &[u8] = include_bytes!("textures/skybox/pos_z.jpg"); - let neg_z: &[u8] = include_bytes!("textures/skybox/neg_z.jpg"); + let pos_x: &[u8] = include_bytes!("../textures/skybox/pos_x.jpg"); + let neg_x: &[u8] = include_bytes!("../textures/skybox/neg_x.jpg"); + let pos_y: &[u8] = include_bytes!("../textures/skybox/pos_y.jpg"); + let neg_y: &[u8] = include_bytes!("../textures/skybox/neg_y.jpg"); + let pos_z: &[u8] = include_bytes!("../textures/skybox/pos_z.jpg"); + let neg_z: &[u8] = include_bytes!("../textures/skybox/neg_z.jpg"); let data: [&[u8]; 6] = [pos_x, neg_x, pos_y, neg_y, pos_z, neg_z]; @@ -597,7 +606,7 @@ fn load_skybox_data() -> Vec { } fn load_normal_map_data() -> Vec { - let bytes: &[u8] = include_bytes!("textures/ice_cube_normal_map.png"); + let bytes: &[u8] = include_bytes!("../textures/ice_cube_normal_map.png"); image::load_from_memory_with_format(bytes, image::ImageFormat::Png) .unwrap() diff --git a/examples/custom_shader/src/primitive/buffer.rs b/examples/custom_shader/src/scene/pipeline/buffer.rs similarity index 100% rename from examples/custom_shader/src/primitive/buffer.rs rename to examples/custom_shader/src/scene/pipeline/buffer.rs diff --git a/examples/custom_shader/src/primitive/cube.rs b/examples/custom_shader/src/scene/pipeline/cube.rs similarity index 99% rename from examples/custom_shader/src/primitive/cube.rs rename to examples/custom_shader/src/scene/pipeline/cube.rs index 7ece685d..de8bad6c 100644 --- a/examples/custom_shader/src/primitive/cube.rs +++ b/examples/custom_shader/src/scene/pipeline/cube.rs @@ -1,4 +1,4 @@ -use crate::primitive::Vertex; +use crate::scene::pipeline::Vertex; use crate::wgpu; use glam::{vec2, vec3, Vec3}; diff --git a/examples/custom_shader/src/primitive/uniforms.rs b/examples/custom_shader/src/scene/pipeline/uniforms.rs similarity index 95% rename from examples/custom_shader/src/primitive/uniforms.rs rename to examples/custom_shader/src/scene/pipeline/uniforms.rs index 4fcb413b..1eac8292 100644 --- a/examples/custom_shader/src/primitive/uniforms.rs +++ b/examples/custom_shader/src/scene/pipeline/uniforms.rs @@ -1,4 +1,5 @@ -use crate::camera::Camera; +use crate::scene::Camera; + use iced::{Color, Rectangle}; #[derive(Copy, Clone, Debug, bytemuck::Pod, bytemuck::Zeroable)] diff --git a/examples/custom_shader/src/primitive/vertex.rs b/examples/custom_shader/src/scene/pipeline/vertex.rs similarity index 100% rename from examples/custom_shader/src/primitive/vertex.rs rename to examples/custom_shader/src/scene/pipeline/vertex.rs diff --git a/widget/src/shader.rs b/widget/src/shader.rs index fe6214db..ca140627 100644 --- a/widget/src/shader.rs +++ b/widget/src/shader.rs @@ -17,6 +17,7 @@ use crate::renderer::wgpu::primitive::pipeline; use std::marker::PhantomData; +pub use crate::graphics::Transformation; pub use crate::renderer::wgpu::wgpu; pub use pipeline::{Primitive, Storage};