Skip to content

Commit bf06650

Browse files
[render] split texture into another module
1 parent 707c228 commit bf06650

File tree

5 files changed

+93
-61
lines changed

5 files changed

+93
-61
lines changed

crates/eqx_core/src/util_macros.rs

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,18 @@
11
#[macro_export]
2-
macro_rules! shader_src {
3-
($path:literal) => {
4-
include_str!(concat!(
2+
macro_rules! shader_src_from {
3+
($path:expr) => {{
4+
let path = format!(
5+
"{}/../../shaders/{}.wgsl",
56
env!("CARGO_MANIFEST_DIR"),
6-
"/../../shaders/",
7-
$path,
8-
".wgsl"
9-
))
10-
};
7+
$path
8+
);
9+
std::fs::read_to_string(path).unwrap()
10+
}};
1111
}
1212

1313
#[macro_export]
14-
macro_rules! asset_file {
15-
($path:literal) => {
16-
include_bytes!(concat!(env!("CARGO_MANIFEST_DIR"), "/../../assets/", $path))
14+
macro_rules! asset_file_path {
15+
($path:expr) => {
16+
format!("{}/../../assets/{}", env!("CARGO_MANIFEST_DIR"), $path)
1717
};
1818
}

crates/eqx_window/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ version = "0.1.0"
44
edition = "2021"
55

66
[dependencies]
7+
anyhow = "1.0"
78
bytemuck = { version = "1.22", features = ["derive"] }
89
eqx_core = { path = "../eqx_core", version = "0.1.0" }
910
cgmath = "0.18"

crates/eqx_window/src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
mod primitives;
2+
mod texture;
23
mod window;
34

45
pub mod prelude {

crates/eqx_window/src/texture.rs

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
use anyhow::Result;
2+
use eqx_core::asset_file_path;
3+
use std::fs;
4+
5+
pub struct Texture {
6+
#[allow(unused)]
7+
texture: wgpu::Texture,
8+
pub view: wgpu::TextureView,
9+
pub sampler: wgpu::Sampler,
10+
}
11+
12+
impl Texture {
13+
pub fn new_from(path: String, device: &wgpu::Device, queue: &wgpu::Queue) -> Result<Self> {
14+
let path = asset_file_path!(path);
15+
let bytes = fs::read(path)?;
16+
let raw_bytes = bytes.as_slice();
17+
let img = image::load_from_memory(raw_bytes)?;
18+
let rgba = img.to_rgba8();
19+
let dimensions = rgba.dimensions();
20+
let size = wgpu::Extent3d {
21+
width: dimensions.0,
22+
height: dimensions.1,
23+
depth_or_array_layers: 1,
24+
};
25+
26+
let texture = device.create_texture(&wgpu::TextureDescriptor {
27+
label: Some("Texture"),
28+
size,
29+
mip_level_count: 1,
30+
sample_count: 1,
31+
dimension: wgpu::TextureDimension::D2,
32+
format: wgpu::TextureFormat::Rgba8UnormSrgb,
33+
usage: wgpu::TextureUsages::TEXTURE_BINDING | wgpu::TextureUsages::COPY_DST,
34+
view_formats: &[],
35+
});
36+
37+
queue.write_texture(
38+
wgpu::TexelCopyTextureInfo {
39+
texture: &texture,
40+
mip_level: 0,
41+
origin: wgpu::Origin3d::ZERO,
42+
aspect: wgpu::TextureAspect::All,
43+
},
44+
&rgba,
45+
wgpu::TexelCopyBufferLayout {
46+
offset: 0,
47+
bytes_per_row: Some(4 * dimensions.0), // 4 = rgba
48+
rows_per_image: Some(dimensions.1),
49+
},
50+
size,
51+
);
52+
53+
let view = texture.create_view(&wgpu::TextureViewDescriptor::default());
54+
let sampler = device.create_sampler(&wgpu::SamplerDescriptor {
55+
label: Some("TextureSampler"),
56+
address_mode_u: wgpu::AddressMode::ClampToEdge,
57+
address_mode_v: wgpu::AddressMode::ClampToEdge,
58+
address_mode_w: wgpu::AddressMode::ClampToEdge,
59+
mag_filter: wgpu::FilterMode::Linear,
60+
min_filter: wgpu::FilterMode::Linear,
61+
mipmap_filter: wgpu::FilterMode::Nearest,
62+
..Default::default()
63+
});
64+
65+
Ok(Self {
66+
texture,
67+
view,
68+
sampler,
69+
})
70+
}
71+
}

crates/eqx_window/src/window.rs

Lines changed: 9 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
use crate::primitives::{Vertex, TEST_INDICES, TEST_VERTICES};
2+
use crate::texture::Texture;
23
use eqx_app::prelude::Module;
34
use wgpu::util::{BufferInitDescriptor, DeviceExt};
45

@@ -9,6 +10,7 @@ use winit::{
910
window::WindowBuilder,
1011
};
1112

13+
use eqx_core::shader_src_from;
1214
use winit::window::Window as WInitWindow;
1315

1416
const VS_MAIN: &str = "vs_main";
@@ -81,53 +83,8 @@ impl<'a> State<'a> {
8183
desired_maximum_frame_latency: 2,
8284
};
8385

84-
let texture_bytes = eqx_core::asset_file!("texture/test_tree.png");
85-
let texture_img = image::load_from_memory(texture_bytes).unwrap();
86-
let texture_rgba = texture_img.to_rgba8();
87-
let texture_dimensions = texture_rgba.dimensions();
88-
let texture_size = wgpu::Extent3d {
89-
width: texture_dimensions.0,
90-
height: texture_dimensions.1,
91-
depth_or_array_layers: 1,
92-
};
93-
94-
let texture = device.create_texture(&wgpu::TextureDescriptor {
95-
label: Some("Texture"),
96-
size: texture_size,
97-
mip_level_count: 1,
98-
sample_count: 1,
99-
dimension: wgpu::TextureDimension::D2,
100-
format: wgpu::TextureFormat::Rgba8UnormSrgb,
101-
usage: wgpu::TextureUsages::TEXTURE_BINDING | wgpu::TextureUsages::COPY_DST,
102-
view_formats: &[],
103-
});
104-
queue.write_texture(
105-
wgpu::TexelCopyTextureInfo {
106-
texture: &texture,
107-
mip_level: 0,
108-
origin: wgpu::Origin3d::ZERO,
109-
aspect: wgpu::TextureAspect::All,
110-
},
111-
&texture_rgba,
112-
wgpu::TexelCopyBufferLayout {
113-
offset: 0,
114-
bytes_per_row: Some(4 * texture_dimensions.0), // 4 = rgba
115-
rows_per_image: Some(texture_dimensions.1),
116-
},
117-
texture_size,
118-
);
119-
120-
let texture_view = texture.create_view(&wgpu::TextureViewDescriptor::default());
121-
let texture_sampler = device.create_sampler(&wgpu::SamplerDescriptor {
122-
label: Some("TextureSampler"),
123-
address_mode_u: wgpu::AddressMode::ClampToEdge,
124-
address_mode_v: wgpu::AddressMode::ClampToEdge,
125-
address_mode_w: wgpu::AddressMode::ClampToEdge,
126-
mag_filter: wgpu::FilterMode::Linear,
127-
min_filter: wgpu::FilterMode::Linear,
128-
mipmap_filter: wgpu::FilterMode::Nearest,
129-
..Default::default()
130-
});
86+
let texture =
87+
Texture::new_from(String::from("texture/test_tree.png"), &device, &queue).unwrap();
13188

13289
let texture_bind_group_layout =
13390
device.create_bind_group_layout(&wgpu::BindGroupLayoutDescriptor {
@@ -159,18 +116,20 @@ impl<'a> State<'a> {
159116
entries: &[
160117
wgpu::BindGroupEntry {
161118
binding: 0,
162-
resource: wgpu::BindingResource::TextureView(&texture_view),
119+
resource: wgpu::BindingResource::TextureView(&texture.view),
163120
},
164121
wgpu::BindGroupEntry {
165122
binding: 1,
166-
resource: wgpu::BindingResource::Sampler(&texture_sampler),
123+
resource: wgpu::BindingResource::Sampler(&texture.sampler),
167124
},
168125
],
169126
});
170127

128+
let shader_str = shader_src_from!("trig/shader");
129+
171130
let shader = device.create_shader_module(wgpu::ShaderModuleDescriptor {
172131
label: Some("TrigShader"),
173-
source: wgpu::ShaderSource::Wgsl(eqx_core::shader_src!("trig/shader").into()),
132+
source: wgpu::ShaderSource::Wgsl(shader_str.as_str().into()),
174133
});
175134

176135
let render_pipeline_layout =

0 commit comments

Comments
 (0)