1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
use pill_engine::internal::TextureType;
use anyhow::*;
use image::GenericImageView;
use std::{ num::NonZeroU32 };
pill_core::define_new_pill_slotmap_key! {
pub struct RendererTextureHandle;
}
pub struct RendererTexture {
pub texture: wgpu::Texture,
pub texture_view: wgpu::TextureView,
pub sampler: wgpu::Sampler,
}
impl RendererTexture {
pub const DEPTH_FORMAT: wgpu::TextureFormat = wgpu::TextureFormat::Depth32Float;
pub fn new_texture(
device: &wgpu::Device,
queue: &wgpu::Queue,
name: Option<&str>,
image_data: &image::DynamicImage,
texture_type: TextureType,
) -> Result<Self> {
let dimensions = image_data.dimensions();
let rgba = image_data.to_rgba8();
let size = wgpu::Extent3d {
width: dimensions.0,
height: dimensions.1,
depth_or_array_layers: 1,
};
let format = match texture_type {
TextureType::Color => wgpu::TextureFormat::Rgba8UnormSrgb,
TextureType::Normal => wgpu::TextureFormat::Rgba8Unorm,
};
let texture = device.create_texture(&wgpu::TextureDescriptor {
label: name,
size,
mip_level_count: 1,
sample_count: 1,
dimension: wgpu::TextureDimension::D2,
format,
usage: wgpu::TextureUsages::TEXTURE_BINDING | wgpu::TextureUsages::COPY_DST,
});
queue.write_texture(
wgpu::ImageCopyTexture {
aspect: wgpu::TextureAspect::All,
texture: &texture,
mip_level: 0,
origin: wgpu::Origin3d::ZERO,
},
&rgba,
wgpu::ImageDataLayout {
offset: 0,
bytes_per_row: NonZeroU32::new(4 * dimensions.0),
rows_per_image: NonZeroU32::new(dimensions.1),
},
size,
);
let texture_view = texture.create_view(&wgpu::TextureViewDescriptor::default());
let sampler = device.create_sampler(&wgpu::SamplerDescriptor {
address_mode_u: wgpu::AddressMode::Repeat,
address_mode_v: wgpu::AddressMode::Repeat,
address_mode_w: wgpu::AddressMode::Repeat,
mag_filter: wgpu::FilterMode::Linear,
min_filter: wgpu::FilterMode::Nearest,
mipmap_filter: wgpu::FilterMode::Nearest,
..Default::default()
});
let texture = Self {
texture,
texture_view,
sampler,
};
Ok(texture)
}
pub fn new_depth_texture(
device: &wgpu::Device,
surface_configuration: &wgpu::SurfaceConfiguration,
label: &str,
) -> Result<Self> {
let size = wgpu::Extent3d {
width: surface_configuration.width,
height: surface_configuration.height,
depth_or_array_layers: 1,
};
let texture = device.create_texture(&wgpu::TextureDescriptor {
label: Some(label),
size,
mip_level_count: 1,
sample_count: 1,
dimension: wgpu::TextureDimension::D2,
format: Self::DEPTH_FORMAT,
usage: wgpu::TextureUsages::RENDER_ATTACHMENT | wgpu::TextureUsages::TEXTURE_BINDING,
});
let texture_view = texture.create_view(&wgpu::TextureViewDescriptor::default());
let sampler = device.create_sampler(&wgpu::SamplerDescriptor {
address_mode_u: wgpu::AddressMode::Repeat,
address_mode_v: wgpu::AddressMode::Repeat,
address_mode_w: wgpu::AddressMode::Repeat,
mag_filter: wgpu::FilterMode::Linear,
min_filter: wgpu::FilterMode::Linear,
mipmap_filter: wgpu::FilterMode::Nearest,
compare: Some(wgpu::CompareFunction::LessEqual),
lod_min_clamp: -100.0,
lod_max_clamp: 100.0,
..Default::default()
});
let texture = Self {
texture,
texture_view,
sampler,
};
Ok(texture)
}
}