Refactor: Texture
This commit is contained in:
parent
1af8e63641
commit
b210a5f742
4 changed files with 84 additions and 70 deletions
|
@ -7,7 +7,7 @@ pub use camera::{
|
||||||
};
|
};
|
||||||
|
|
||||||
mod texture;
|
mod texture;
|
||||||
pub use texture::Texture;
|
pub use texture::{Texture, TextureManager};
|
||||||
|
|
||||||
mod instance;
|
mod instance;
|
||||||
pub use instance::{
|
pub use instance::{
|
||||||
|
|
66
src/render/texture/mod.rs
Normal file
66
src/render/texture/mod.rs
Normal file
|
@ -0,0 +1,66 @@
|
||||||
|
mod texture;
|
||||||
|
pub use texture::Texture;
|
||||||
|
use wgpu::{BindGroup, Device, Queue};
|
||||||
|
|
||||||
|
pub struct TextureManager {
|
||||||
|
texture_bind_group_layout: wgpu::BindGroupLayout,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl TextureManager {
|
||||||
|
pub fn new(device: &Device) -> Self {
|
||||||
|
let texture_bind_group_layout =
|
||||||
|
device.create_bind_group_layout(&wgpu::BindGroupLayoutDescriptor {
|
||||||
|
entries: &[
|
||||||
|
wgpu::BindGroupLayoutEntry {
|
||||||
|
binding: 0,
|
||||||
|
visibility: wgpu::ShaderStages::FRAGMENT,
|
||||||
|
ty: wgpu::BindingType::Texture {
|
||||||
|
multisampled: false,
|
||||||
|
view_dimension: wgpu::TextureViewDimension::D2,
|
||||||
|
sample_type: wgpu::TextureSampleType::Float { filterable: true },
|
||||||
|
},
|
||||||
|
count: None,
|
||||||
|
},
|
||||||
|
wgpu::BindGroupLayoutEntry {
|
||||||
|
binding: 1,
|
||||||
|
visibility: wgpu::ShaderStages::FRAGMENT,
|
||||||
|
ty: wgpu::BindingType::Sampler(wgpu::SamplerBindingType::Filtering),
|
||||||
|
count: None,
|
||||||
|
},
|
||||||
|
],
|
||||||
|
label: Some("texture_bind_group_layout"),
|
||||||
|
});
|
||||||
|
Self {
|
||||||
|
texture_bind_group_layout,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn create_texture_from_bytes(
|
||||||
|
&self,
|
||||||
|
device: &Device,
|
||||||
|
queue: &Queue,
|
||||||
|
bytes: &[u8],
|
||||||
|
label: &str,
|
||||||
|
) -> BindGroup {
|
||||||
|
let diffuse_texture = Texture::from_bytes(&device, &queue, bytes, label).unwrap();
|
||||||
|
|
||||||
|
device.create_bind_group(&wgpu::BindGroupDescriptor {
|
||||||
|
layout: &self.texture_bind_group_layout,
|
||||||
|
entries: &[
|
||||||
|
wgpu::BindGroupEntry {
|
||||||
|
binding: 0,
|
||||||
|
resource: wgpu::BindingResource::TextureView(&diffuse_texture.view),
|
||||||
|
},
|
||||||
|
wgpu::BindGroupEntry {
|
||||||
|
binding: 1,
|
||||||
|
resource: wgpu::BindingResource::Sampler(&diffuse_texture.sampler),
|
||||||
|
},
|
||||||
|
],
|
||||||
|
label: Some(&format!("diffuse_bind_group_{}", label)),
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn get_texture_bind_group_layout(&self) -> &wgpu::BindGroupLayout {
|
||||||
|
&self.texture_bind_group_layout
|
||||||
|
}
|
||||||
|
}
|
86
src/state.rs
86
src/state.rs
|
@ -1,4 +1,4 @@
|
||||||
use crate::{meshs::DefaultMesh, render::Renderable};
|
use crate::{meshs::DefaultMesh, render::{Renderable, TextureManager}};
|
||||||
|
|
||||||
use super::render::{
|
use super::render::{
|
||||||
Vertex, Camera, CameraUniform, CameraController, Texture, InstanceRaw
|
Vertex, Camera, CameraUniform, CameraController, Texture, InstanceRaw
|
||||||
|
@ -23,6 +23,8 @@ pub struct State {
|
||||||
camera_controller: CameraController,
|
camera_controller: CameraController,
|
||||||
depth_texture: Texture,
|
depth_texture: Texture,
|
||||||
mesh: DefaultMesh,
|
mesh: DefaultMesh,
|
||||||
|
#[allow(dead_code)]
|
||||||
|
texture_manager: TextureManager,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl State {
|
impl State {
|
||||||
|
@ -79,30 +81,7 @@ impl State {
|
||||||
};
|
};
|
||||||
surface.configure(&device, &config);
|
surface.configure(&device, &config);
|
||||||
|
|
||||||
// Binding des textures
|
let texture_manager = TextureManager::new(&device);
|
||||||
let texture_bind_group_layout =
|
|
||||||
device.create_bind_group_layout(&wgpu::BindGroupLayoutDescriptor {
|
|
||||||
entries: &[
|
|
||||||
wgpu::BindGroupLayoutEntry {
|
|
||||||
binding: 0,
|
|
||||||
visibility: wgpu::ShaderStages::FRAGMENT,
|
|
||||||
ty: wgpu::BindingType::Texture {
|
|
||||||
multisampled: false,
|
|
||||||
view_dimension: wgpu::TextureViewDimension::D2,
|
|
||||||
sample_type: wgpu::TextureSampleType::Float { filterable: true },
|
|
||||||
},
|
|
||||||
count: None,
|
|
||||||
},
|
|
||||||
wgpu::BindGroupLayoutEntry {
|
|
||||||
binding: 1,
|
|
||||||
visibility: wgpu::ShaderStages::FRAGMENT,
|
|
||||||
ty: wgpu::BindingType::Sampler(wgpu::SamplerBindingType::Filtering),
|
|
||||||
count: None,
|
|
||||||
},
|
|
||||||
],
|
|
||||||
label: Some("texture_bind_group_layout"),
|
|
||||||
});
|
|
||||||
// FIN Binding des textures
|
|
||||||
|
|
||||||
let shader = device.create_shader_module(&wgpu::ShaderModuleDescriptor {
|
let shader = device.create_shader_module(&wgpu::ShaderModuleDescriptor {
|
||||||
label: Some("Shader"),
|
label: Some("Shader"),
|
||||||
|
@ -164,7 +143,7 @@ impl State {
|
||||||
let render_pipeline_layout =
|
let render_pipeline_layout =
|
||||||
device.create_pipeline_layout(&wgpu::PipelineLayoutDescriptor {
|
device.create_pipeline_layout(&wgpu::PipelineLayoutDescriptor {
|
||||||
label: Some("Render Pipeline Layout"),
|
label: Some("Render Pipeline Layout"),
|
||||||
bind_group_layouts: &[&texture_bind_group_layout, &camera_bind_group_layout],
|
bind_group_layouts: &[&texture_manager.get_texture_bind_group_layout(), &camera_bind_group_layout],
|
||||||
push_constant_ranges: &[],
|
push_constant_ranges: &[],
|
||||||
});
|
});
|
||||||
|
|
||||||
|
@ -212,51 +191,19 @@ impl State {
|
||||||
multiview: None,
|
multiview: None,
|
||||||
});
|
});
|
||||||
|
|
||||||
// Loading mesh
|
let diffuse_bind_group = texture_manager.create_texture_from_bytes(
|
||||||
|
|
||||||
let diffuse_bytes = include_bytes!(concat!(env!("CARGO_MANIFEST_DIR"), "/res/images/happy-tree.png"));
|
|
||||||
let diffuse_texture =
|
|
||||||
Texture::from_bytes(&device, &queue, diffuse_bytes, "happy-tree.png")
|
|
||||||
.unwrap();
|
|
||||||
|
|
||||||
let diffuse_bind_group = device.create_bind_group(&wgpu::BindGroupDescriptor {
|
|
||||||
layout: &texture_bind_group_layout,
|
|
||||||
entries: &[
|
|
||||||
wgpu::BindGroupEntry {
|
|
||||||
binding: 0,
|
|
||||||
resource: wgpu::BindingResource::TextureView(&diffuse_texture.view),
|
|
||||||
},
|
|
||||||
wgpu::BindGroupEntry {
|
|
||||||
binding: 1,
|
|
||||||
resource: wgpu::BindingResource::Sampler(&diffuse_texture.sampler),
|
|
||||||
},
|
|
||||||
],
|
|
||||||
label: Some("diffuse_bind_group"),
|
|
||||||
});
|
|
||||||
|
|
||||||
let diffuse_bytes_pikachu = include_bytes!(concat!(env!("CARGO_MANIFEST_DIR"), "/res/images/pikachu.png"));
|
|
||||||
let diffuse_texture_pikachu = Texture::from_bytes(
|
|
||||||
&device,
|
&device,
|
||||||
&queue,
|
&queue,
|
||||||
diffuse_bytes_pikachu,
|
include_bytes!(concat!(env!("CARGO_MANIFEST_DIR"), "/res/images/happy-tree.png")),
|
||||||
"pikachu.png",
|
"happy-tree.png",
|
||||||
)
|
);
|
||||||
.unwrap();
|
|
||||||
|
|
||||||
let diffuse_bind_group_pikachu = device.create_bind_group(&wgpu::BindGroupDescriptor {
|
let diffuse_bind_group_pikachu = texture_manager.create_texture_from_bytes(
|
||||||
layout: &texture_bind_group_layout,
|
&device,
|
||||||
entries: &[
|
&queue,
|
||||||
wgpu::BindGroupEntry {
|
include_bytes!(concat!(env!("CARGO_MANIFEST_DIR"), "/res/images/pikachu.png")),
|
||||||
binding: 0,
|
"pikachu.png",
|
||||||
resource: wgpu::BindingResource::TextureView(&diffuse_texture_pikachu.view),
|
);
|
||||||
},
|
|
||||||
wgpu::BindGroupEntry {
|
|
||||||
binding: 1,
|
|
||||||
resource: wgpu::BindingResource::Sampler(&diffuse_texture_pikachu.sampler),
|
|
||||||
},
|
|
||||||
],
|
|
||||||
label: Some("diffuse_bind_group"),
|
|
||||||
});
|
|
||||||
|
|
||||||
let mut mesh = DefaultMesh::new(diffuse_bind_group, diffuse_bind_group_pikachu);
|
let mut mesh = DefaultMesh::new(diffuse_bind_group, diffuse_bind_group_pikachu);
|
||||||
mesh.prepare(&device);
|
mesh.prepare(&device);
|
||||||
|
@ -274,7 +221,8 @@ impl State {
|
||||||
camera_bind_group,
|
camera_bind_group,
|
||||||
camera_controller,
|
camera_controller,
|
||||||
depth_texture,
|
depth_texture,
|
||||||
mesh
|
mesh,
|
||||||
|
texture_manager
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue