tirbofish/dropbear
main / crates / kino-ui / src / rendering / texture.rs · 6489 bytes
crates/kino-ui/src/rendering/texture.rs
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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
use std::hash::{DefaultHasher, Hash, Hasher};
use wgpu::{
BindGroup, BindGroupDescriptor, BindGroupEntry, BindGroupLayout, BindingResource, Device,
Extent3d, Origin3d, Queue, RenderPass, TexelCopyBufferLayout, TexelCopyTextureInfo,
TextureAspect, TextureDescriptor, TextureDimension, TextureFormat, TextureUsages,
};
/// A GPU texture that can be bound in shaders for rendering
///
/// Wraps a `wgpu::Texture`, its view, sampler, & bind group
#[derive(Debug, PartialEq)]
pub struct Texture {
pub(crate) hash: u64,
bind_group: BindGroup,
}
impl Texture {
fn bytes_per_pixel(format: TextureFormat) -> Option<u32> {
match format {
TextureFormat::Rgba8Unorm
| TextureFormat::Rgba8UnormSrgb
| TextureFormat::Bgra8Unorm
| TextureFormat::Bgra8UnormSrgb => Some(4),
TextureFormat::Rgba16Float
| TextureFormat::Rgba16Unorm
| TextureFormat::Rgba16Snorm
| TextureFormat::Rgba16Uint
| TextureFormat::Rgba16Sint => Some(8),
TextureFormat::Rgba32Float | TextureFormat::Rgba32Uint | TextureFormat::Rgba32Sint => {
Some(16)
}
_ => None,
}
}
/// Creates a new texture from raw RGBA image data,
/// uploads the data, & builds the bind group using the layout
///
/// - `data`: Must be in tightly packed 8-bit RGBA format
/// - `width`, `height`: Dimensions of the image in pixels
pub fn from_bytes(
device: &Device,
queue: &Queue,
bind_group_layout: &BindGroupLayout,
data: &[u8],
width: u32,
height: u32,
texture_format: TextureFormat,
) -> Self {
log::debug!("Creating new texture");
let bytes_per_pixel = Self::bytes_per_pixel(texture_format).unwrap_or(4);
let expected_len = (width as usize)
.saturating_mul(height as usize)
.saturating_mul(bytes_per_pixel as usize);
if data.len() != expected_len {
log::error!(
"Texture data length {} does not match expected {} for {:?}",
data.len(),
expected_len,
texture_format
);
}
let texture = device.create_texture(&TextureDescriptor {
label: None,
size: Extent3d {
width,
height,
depth_or_array_layers: 1,
},
mip_level_count: 1,
sample_count: 1,
dimension: TextureDimension::D2,
format: texture_format,
usage: TextureUsages::TEXTURE_BINDING | TextureUsages::COPY_DST,
view_formats: &[],
});
queue.write_texture(
TexelCopyTextureInfo {
texture: &texture,
mip_level: 0,
origin: Origin3d::ZERO,
aspect: TextureAspect::All,
},
data,
TexelCopyBufferLayout {
offset: 0,
bytes_per_row: Some(bytes_per_pixel * width),
rows_per_image: Some(height),
},
Extent3d {
width,
height,
depth_or_array_layers: 1,
},
);
let view = texture.create_view(&Default::default());
let sampler = device.create_sampler(&Default::default());
let bind_group = device.create_bind_group(&BindGroupDescriptor {
label: None,
layout: bind_group_layout,
entries: &[
BindGroupEntry {
binding: 0,
resource: BindingResource::TextureView(&view),
},
BindGroupEntry {
binding: 1,
resource: BindingResource::Sampler(&sampler),
},
],
});
let mut hasher = DefaultHasher::new();
data.hash(&mut hasher);
let hash = hasher.finish();
log::debug!("Created new texture [{}]", hash);
Self { hash, bind_group }
}
/// Creates a 1×1 white fallback texture
///
/// Used when no valid texture is provided for a draw call
pub fn create_default(
device: &Device,
queue: &Queue,
layout: &BindGroupLayout,
texture_format: TextureFormat,
) -> Self {
log::debug!("Creating standard white texture");
Self::from_bytes(
device,
queue,
layout,
&[255u8, 255, 255, 255],
1,
1,
texture_format,
)
}
pub fn create_render_target(
device: &Device,
bind_group_layout: &BindGroupLayout,
width: u32,
height: u32,
texture_format: TextureFormat,
) -> (Self, wgpu::TextureView) {
let texture = device.create_texture(&TextureDescriptor {
label: Some("kino billboard render target"),
size: Extent3d {
width,
height,
depth_or_array_layers: 1,
},
mip_level_count: 1,
sample_count: 1,
dimension: TextureDimension::D2,
format: texture_format,
usage: TextureUsages::TEXTURE_BINDING | TextureUsages::RENDER_ATTACHMENT,
view_formats: &[],
});
let view = texture.create_view(&Default::default());
let sampler = device.create_sampler(&Default::default());
let bind_group = device.create_bind_group(&BindGroupDescriptor {
label: Some("kino billboard render target bind group"),
layout: bind_group_layout,
entries: &[
BindGroupEntry {
binding: 0,
resource: BindingResource::TextureView(&view),
},
BindGroupEntry {
binding: 1,
resource: BindingResource::Sampler(&sampler),
},
],
});
(
Self {
hash: 0,
bind_group,
},
view,
)
}
/// Binds this texture at the given index in the render pass
///
/// - `index` must match the bind group index used in the pipeline layout
pub fn bind(&self, pass: &mut RenderPass, index: u32) {
pass.set_bind_group(index, &self.bind_group, &[]);
}
}