@@ -3,8 +3,9 @@ use winit::{
3
3
event_loop:: { ControlFlow , EventLoop } ,
4
4
window:: { Window , WindowBuilder } ,
5
5
} ;
6
-
7
6
use wgpu:: util:: DeviceExt ;
7
+ mod texture;
8
+
8
9
9
10
fn main ( ) {
10
11
env_logger:: init ( ) ;
@@ -115,6 +116,7 @@ struct State {
115
116
index_buffer : wgpu:: Buffer ,
116
117
num_indices : u32 ,
117
118
diffuse_bind_group : wgpu:: BindGroup ,
119
+ diffuse_texture : texture:: Texture ,
118
120
}
119
121
120
122
impl State {
@@ -153,60 +155,8 @@ impl State {
153
155
let swap_chain = device. create_swap_chain ( & surface, & sc_desc) ;
154
156
155
157
let diffuse_bytes = include_bytes ! ( "happy-tree.png" ) ;
156
- let diffuse_image = image:: load_from_memory ( diffuse_bytes) . unwrap ( ) ;
157
- let diffuse_rgba = diffuse_image. as_rgba8 ( ) . unwrap ( ) ;
158
-
159
- use image:: GenericImageView ;
160
- let dimensions = diffuse_image. dimensions ( ) ;
161
-
162
- let texture_size = wgpu:: Extent3d {
163
- width : dimensions. 0 ,
164
- height : dimensions. 1 ,
165
- depth_or_array_layers : 1 ,
166
- } ;
167
-
168
- let diffuse_texture = device. create_texture (
169
- & wgpu:: TextureDescriptor {
170
- size : texture_size,
171
- mip_level_count : 1 ,
172
- sample_count : 1 ,
173
- dimension : wgpu:: TextureDimension :: D2 ,
174
- format : wgpu:: TextureFormat :: Rgba8UnormSrgb ,
175
- usage : wgpu:: TextureUsage :: SAMPLED | wgpu:: TextureUsage :: COPY_DST ,
176
- label : Some ( "diffuse_texture" ) ,
177
- }
178
- ) ;
179
158
180
- queue. write_texture (
181
- wgpu:: ImageCopyTexture {
182
- texture : & diffuse_texture,
183
- mip_level : 0 ,
184
- origin : wgpu:: Origin3d :: ZERO ,
185
- } ,
186
- & diffuse_rgba,
187
- wgpu:: ImageDataLayout {
188
- offset : 0 ,
189
- bytes_per_row : std:: num:: NonZeroU32 :: new ( 4 * dimensions. 0 ) ,
190
- rows_per_image : std:: num:: NonZeroU32 :: new ( dimensions. 1 ) ,
191
- } ,
192
- texture_size,
193
- ) ;
194
-
195
- let diffuse_texture_view = diffuse_texture. create_view (
196
- & wgpu:: TextureViewDescriptor :: default ( )
197
- ) ;
198
-
199
- let diffuse_sampler = device. create_sampler (
200
- & wgpu:: SamplerDescriptor {
201
- address_mode_u : wgpu:: AddressMode :: ClampToEdge ,
202
- address_mode_v : wgpu:: AddressMode :: ClampToEdge ,
203
- address_mode_w : wgpu:: AddressMode :: ClampToEdge ,
204
- mag_filter : wgpu:: FilterMode :: Linear ,
205
- min_filter : wgpu:: FilterMode :: Nearest ,
206
- mipmap_filter : wgpu:: FilterMode :: Nearest ,
207
- ..Default :: default ( )
208
- }
209
- ) ;
159
+ let diffuse_texture = texture:: Texture :: from_bytes ( & device, & queue, diffuse_bytes, "happy-tree.png" ) . unwrap ( ) ;
210
160
211
161
let texture_bind_group_layout = device. create_bind_group_layout (
212
162
& wgpu:: BindGroupLayoutDescriptor {
@@ -245,11 +195,11 @@ impl State {
245
195
entries : & [
246
196
wgpu:: BindGroupEntry {
247
197
binding : 0 ,
248
- resource : wgpu:: BindingResource :: TextureView ( & diffuse_texture_view ) ,
198
+ resource : wgpu:: BindingResource :: TextureView ( & diffuse_texture . view ) ,
249
199
} ,
250
200
wgpu:: BindGroupEntry {
251
201
binding : 1 ,
252
- resource : wgpu:: BindingResource :: Sampler ( & diffuse_sampler ) ,
202
+ resource : wgpu:: BindingResource :: Sampler ( & diffuse_texture . sampler ) ,
253
203
}
254
204
] ,
255
205
label : Some ( "diffuse_bind_group" ) ,
@@ -337,6 +287,7 @@ impl State {
337
287
index_buffer,
338
288
num_indices,
339
289
diffuse_bind_group,
290
+ diffuse_texture,
340
291
}
341
292
}
342
293
0 commit comments