@@ -2,22 +2,20 @@ use bytemuck::{Pod, Zeroable};
2
2
use imgui:: * ;
3
3
use imgui_wgpu:: { Renderer , RendererConfig , Texture , TextureConfig } ;
4
4
use pollster:: block_on;
5
- use std:: num:: NonZeroU32 ;
6
5
use std:: time:: Instant ;
7
- use wgpu:: { util:: DeviceExt , BlendState , Extent3d } ;
6
+ use wgpu:: { include_wgsl , util:: DeviceExt , Extent3d } ;
8
7
use winit:: {
9
8
dpi:: LogicalSize ,
10
9
event:: { ElementState , Event , KeyboardInput , VirtualKeyCode , WindowEvent } ,
11
10
event_loop:: { ControlFlow , EventLoop } ,
12
11
window:: Window ,
13
12
} ;
14
13
15
- // Example code modified from https://github.com/gfx-rs/wgpu-rs/tree/master/examples/cube
16
-
17
14
const OPENGL_TO_WGPU_MATRIX : cgmath:: Matrix4 < f32 > = cgmath:: Matrix4 :: new (
18
15
1.0 , 0.0 , 0.0 , 0.0 , 0.0 , 1.0 , 0.0 , 0.0 , 0.0 , 0.0 , 0.5 , 0.0 , 0.0 , 0.0 , 0.5 , 1.0 ,
19
16
) ;
20
17
18
+ // Example code modified from https://github.com/gfx-rs/wgpu-rs/tree/master/examples/cube
21
19
#[ repr( C ) ]
22
20
#[ derive( Clone , Copy , Pod , Zeroable ) ]
23
21
struct Vertex {
@@ -79,10 +77,8 @@ fn create_vertices() -> (Vec<Vertex>, Vec<u16>) {
79
77
}
80
78
81
79
fn create_texels ( size : usize ) -> Vec < u8 > {
82
- use std:: iter;
83
-
84
80
( 0 ..size * size)
85
- . flat_map ( |id| {
81
+ . map ( |id| {
86
82
// get high five for recognizing this ;)
87
83
let cx = 3.0 * ( id % size) as f32 / ( size - 1 ) as f32 - 2.0 ;
88
84
let cy = 2.0 * ( id / size) as f32 / ( size - 1 ) as f32 - 1.0 ;
@@ -93,10 +89,7 @@ fn create_texels(size: usize) -> Vec<u8> {
93
89
y = 2.0 * old_x * y + cy;
94
90
count += 1 ;
95
91
}
96
- iter:: once ( 0xFF - ( count * 5 ) as u8 )
97
- . chain ( iter:: once ( 0xFF - ( count * 15 ) as u8 ) )
98
- . chain ( iter:: once ( 0xFF - ( count * 50 ) as u8 ) )
99
- . chain ( iter:: once ( 1 ) )
92
+ count
100
93
} )
101
94
. collect ( )
102
95
}
@@ -112,10 +105,10 @@ struct Example {
112
105
}
113
106
114
107
impl Example {
115
- fn generate_matrix ( aspect_ratio : f32 , theta : f32 ) -> cgmath:: Matrix4 < f32 > {
108
+ fn generate_matrix ( aspect_ratio : f32 ) -> cgmath:: Matrix4 < f32 > {
116
109
let mx_projection = cgmath:: perspective ( cgmath:: Deg ( 45f32 ) , aspect_ratio, 1.0 , 10.0 ) ;
117
110
let mx_view = cgmath:: Matrix4 :: look_at_rh (
118
- cgmath:: Point3 :: new ( 6.0 * theta . cos ( ) , 6.0 * theta . sin ( ) , 3.0 ) ,
111
+ cgmath:: Point3 :: new ( 1.5f32 , - 5.0 , 3.0 ) ,
119
112
cgmath:: Point3 :: new ( 0f32 , 0.0 , 0.0 ) ,
120
113
cgmath:: Vector3 :: unit_z ( ) ,
121
114
) ;
@@ -126,7 +119,7 @@ impl Example {
126
119
127
120
impl Example {
128
121
fn init (
129
- surface_desc : & wgpu:: SurfaceConfiguration ,
122
+ config : & wgpu:: SurfaceConfiguration ,
130
123
device : & wgpu:: Device ,
131
124
queue : & wgpu:: Queue ,
132
125
) -> Self {
@@ -167,20 +160,11 @@ impl Example {
167
160
visibility : wgpu:: ShaderStages :: FRAGMENT ,
168
161
ty : wgpu:: BindingType :: Texture {
169
162
multisampled : false ,
170
- sample_type : wgpu:: TextureSampleType :: Float { filterable : true } ,
163
+ sample_type : wgpu:: TextureSampleType :: Uint ,
171
164
view_dimension : wgpu:: TextureViewDimension :: D2 ,
172
165
} ,
173
166
count : None ,
174
167
} ,
175
- wgpu:: BindGroupLayoutEntry {
176
- binding : 2 ,
177
- visibility : wgpu:: ShaderStages :: FRAGMENT ,
178
- ty : wgpu:: BindingType :: Sampler {
179
- filtering : true ,
180
- comparison : false ,
181
- } ,
182
- count : None ,
183
- } ,
184
168
] ,
185
169
} ) ;
186
170
let pipeline_layout = device. create_pipeline_layout ( & wgpu:: PipelineLayoutDescriptor {
@@ -203,38 +187,23 @@ impl Example {
203
187
mip_level_count : 1 ,
204
188
sample_count : 1 ,
205
189
dimension : wgpu:: TextureDimension :: D2 ,
206
- format : wgpu:: TextureFormat :: Rgba8UnormSrgb ,
190
+ format : wgpu:: TextureFormat :: R8Uint ,
207
191
usage : wgpu:: TextureUsages :: TEXTURE_BINDING | wgpu:: TextureUsages :: COPY_DST ,
208
192
} ) ;
209
193
let texture_view = texture. create_view ( & wgpu:: TextureViewDescriptor :: default ( ) ) ;
210
194
queue. write_texture (
211
- wgpu:: ImageCopyTexture {
212
- texture : & texture,
213
- mip_level : 0 ,
214
- origin : wgpu:: Origin3d :: ZERO ,
215
- aspect : wgpu:: TextureAspect :: All ,
216
- } ,
195
+ texture. as_image_copy ( ) ,
217
196
& texels,
218
197
wgpu:: ImageDataLayout {
219
198
offset : 0 ,
220
- bytes_per_row : NonZeroU32 :: new ( 4 * size) ,
199
+ bytes_per_row : Some ( std :: num :: NonZeroU32 :: new ( size) . unwrap ( ) ) ,
221
200
rows_per_image : None ,
222
201
} ,
223
202
texture_extent,
224
203
) ;
225
204
226
205
// Create other resources
227
- let sampler = device. create_sampler ( & wgpu:: SamplerDescriptor {
228
- address_mode_u : wgpu:: AddressMode :: ClampToEdge ,
229
- address_mode_v : wgpu:: AddressMode :: ClampToEdge ,
230
- address_mode_w : wgpu:: AddressMode :: ClampToEdge ,
231
- mag_filter : wgpu:: FilterMode :: Nearest ,
232
- min_filter : wgpu:: FilterMode :: Linear ,
233
- mipmap_filter : wgpu:: FilterMode :: Nearest ,
234
- ..Default :: default ( )
235
- } ) ;
236
- let mx_total =
237
- Self :: generate_matrix ( surface_desc. width as f32 / surface_desc. height as f32 , 0.0 ) ;
206
+ let mx_total = Self :: generate_matrix ( config. width as f32 / config. height as f32 ) ;
238
207
let mx_ref: & [ f32 ; 16 ] = mx_total. as_ref ( ) ;
239
208
let uniform_buf = device. create_buffer_init ( & wgpu:: util:: BufferInitDescriptor {
240
209
label : Some ( "Uniform Buffer" ) ,
@@ -254,62 +223,48 @@ impl Example {
254
223
binding : 1 ,
255
224
resource : wgpu:: BindingResource :: TextureView ( & texture_view) ,
256
225
} ,
257
- wgpu:: BindGroupEntry {
258
- binding : 2 ,
259
- resource : wgpu:: BindingResource :: Sampler ( & sampler) ,
260
- } ,
261
226
] ,
262
227
label : None ,
263
228
} ) ;
264
229
265
- // Create the render pipeline
266
- let vs_module =
267
- device. create_shader_module ( & wgpu:: include_spirv!( "../resources/cube.vert.spv" ) ) ;
268
- let fs_module =
269
- device. create_shader_module ( & wgpu:: include_spirv!( "../resources/cube.frag.spv" ) ) ;
230
+ let shader = device. create_shader_module ( & include_wgsl ! ( "../resources/cube.wgsl" ) ) ;
231
+
232
+ let vertex_buffers = [ wgpu:: VertexBufferLayout {
233
+ array_stride : vertex_size as wgpu:: BufferAddress ,
234
+ step_mode : wgpu:: VertexStepMode :: Vertex ,
235
+ attributes : & [
236
+ wgpu:: VertexAttribute {
237
+ format : wgpu:: VertexFormat :: Float32x4 ,
238
+ offset : 0 ,
239
+ shader_location : 0 ,
240
+ } ,
241
+ wgpu:: VertexAttribute {
242
+ format : wgpu:: VertexFormat :: Float32x2 ,
243
+ offset : 4 * 4 ,
244
+ shader_location : 1 ,
245
+ } ,
246
+ ] ,
247
+ } ] ;
270
248
271
249
let pipeline = device. create_render_pipeline ( & wgpu:: RenderPipelineDescriptor {
272
250
label : None ,
273
251
layout : Some ( & pipeline_layout) ,
274
252
vertex : wgpu:: VertexState {
275
- module : & vs_module,
276
- entry_point : "main" ,
277
- buffers : & [ wgpu:: VertexBufferLayout {
278
- array_stride : vertex_size as wgpu:: BufferAddress ,
279
- step_mode : wgpu:: VertexStepMode :: Vertex ,
280
- attributes : & [
281
- wgpu:: VertexAttribute {
282
- format : wgpu:: VertexFormat :: Float32x4 ,
283
- offset : 0 ,
284
- shader_location : 0 ,
285
- } ,
286
- wgpu:: VertexAttribute {
287
- format : wgpu:: VertexFormat :: Float32x2 ,
288
- offset : 4 * 4 ,
289
- shader_location : 1 ,
290
- } ,
291
- ] ,
292
- } ] ,
253
+ module : & shader,
254
+ entry_point : "vs_main" ,
255
+ buffers : & vertex_buffers,
293
256
} ,
257
+ fragment : Some ( wgpu:: FragmentState {
258
+ module : & shader,
259
+ entry_point : "fs_main" ,
260
+ targets : & [ config. format . into ( ) ] ,
261
+ } ) ,
294
262
primitive : wgpu:: PrimitiveState {
295
- front_face : wgpu:: FrontFace :: Ccw ,
296
263
cull_mode : Some ( wgpu:: Face :: Back ) ,
297
264
..Default :: default ( )
298
265
} ,
299
266
depth_stencil : None ,
300
267
multisample : wgpu:: MultisampleState :: default ( ) ,
301
- fragment : Some ( wgpu:: FragmentState {
302
- module : & fs_module,
303
- entry_point : "main" ,
304
- targets : & [ wgpu:: ColorTargetState {
305
- format : surface_desc. format ,
306
- blend : Some ( BlendState {
307
- color : wgpu:: BlendComponent :: REPLACE ,
308
- alpha : wgpu:: BlendComponent :: REPLACE ,
309
- } ) ,
310
- write_mask : wgpu:: ColorWrites :: ALL ,
311
- } ] ,
312
- } ) ,
313
268
} ) ;
314
269
315
270
// Done
@@ -329,7 +284,7 @@ impl Example {
329
284
}
330
285
331
286
fn setup_camera ( & mut self , queue : & wgpu:: Queue , size : [ f32 ; 2 ] ) {
332
- let mx_total = Self :: generate_matrix ( size[ 0 ] / size[ 1 ] , self . time * 0.1 ) ;
287
+ let mx_total = Self :: generate_matrix ( size[ 0 ] / size[ 1 ] ) ;
333
288
let mx_ref: & [ f32 ; 16 ] = mx_total. as_ref ( ) ;
334
289
queue. write_buffer ( & self . uniform_buf , 0 , bytemuck:: cast_slice ( mx_ref) ) ;
335
290
}
@@ -341,14 +296,14 @@ impl Example {
341
296
let mut rpass = encoder. begin_render_pass ( & wgpu:: RenderPassDescriptor {
342
297
label : None ,
343
298
color_attachments : & [ wgpu:: RenderPassColorAttachment {
344
- view : & view ,
299
+ view,
345
300
resolve_target : None ,
346
301
ops : wgpu:: Operations {
347
302
load : wgpu:: LoadOp :: Clear ( wgpu:: Color {
348
303
r : 0.1 ,
349
304
g : 0.2 ,
350
305
b : 0.3 ,
351
- a : 0.1 , // semi-transparent background
306
+ a : 1.0 ,
352
307
} ) ,
353
308
store : true ,
354
309
} ,
@@ -551,7 +506,7 @@ fn main() {
551
506
// Store the new size of Image() or None to indicate that the window is collapsed.
552
507
let mut new_example_size: Option < [ f32 ; 2 ] > = None ;
553
508
554
- imgui:: Window :: new ( im_str ! ( "Cube" ) )
509
+ imgui:: Window :: new ( "Cube" )
555
510
. size ( [ 512.0 , 512.0 ] , Condition :: FirstUseEver )
556
511
. build ( & ui, || {
557
512
new_example_size = Some ( ui. content_region_avail ( ) ) ;
0 commit comments