1
1
use baseview:: { MouseEvent , Size , Window , WindowHandler , WindowOpenOptions } ;
2
- use wgpu:: { util:: DeviceExt , Buffer , Device , Queue , RenderPipeline , Surface , SurfaceCapabilities } ;
2
+ use wgpu:: {
3
+ util:: DeviceExt , BindGroup , Buffer , Device , Queue , RenderPipeline , Surface , SurfaceCapabilities ,
4
+ } ;
5
+
6
+ const WINDOW_SIZE : u32 = 512 ;
3
7
4
8
struct WgpuExample {
5
9
pipeline : RenderPipeline ,
6
10
device : Device ,
7
11
surface : Surface ,
8
12
queue : Queue ,
9
13
vertex_buffer : Buffer ,
14
+ mouse_pos_buffer : Buffer ,
10
15
surface_caps : SurfaceCapabilities ,
16
+ bind_group : BindGroup ,
17
+ size : ( u32 , u32 ) ,
11
18
}
12
19
13
20
impl < ' a > WgpuExample {
@@ -40,6 +47,33 @@ impl<'a> WgpuExample {
40
47
usage : wgpu:: BufferUsages :: VERTEX ,
41
48
} ) ;
42
49
50
+ let mouse_pos_buffer = device. create_buffer_init ( & wgpu:: util:: BufferInitDescriptor {
51
+ label : Some ( "mouse position buffer" ) ,
52
+ contents : bytemuck:: cast_slice ( & [ 0.0 , 0.0 ] ) ,
53
+ usage : wgpu:: BufferUsages :: UNIFORM | wgpu:: BufferUsages :: COPY_DST ,
54
+ } ) ;
55
+ let bind_group_layout = device. create_bind_group_layout ( & wgpu:: BindGroupLayoutDescriptor {
56
+ label : None ,
57
+ entries : & [ wgpu:: BindGroupLayoutEntry {
58
+ binding : 0 ,
59
+ visibility : wgpu:: ShaderStages :: VERTEX ,
60
+ ty : wgpu:: BindingType :: Buffer {
61
+ ty : wgpu:: BufferBindingType :: Uniform ,
62
+ has_dynamic_offset : false ,
63
+ min_binding_size : None ,
64
+ } ,
65
+ count : None ,
66
+ } ] ,
67
+ } ) ;
68
+ let bind_group = device. create_bind_group ( & wgpu:: BindGroupDescriptor {
69
+ label : None ,
70
+ layout : & bind_group_layout,
71
+ entries : & [ wgpu:: BindGroupEntry {
72
+ binding : 0 ,
73
+ resource : mouse_pos_buffer. as_entire_binding ( ) ,
74
+ } ] ,
75
+ } ) ;
76
+
43
77
let surface_caps = surface. get_capabilities ( & adapter) ;
44
78
let surface_format = surface_caps
45
79
. formats
@@ -50,8 +84,8 @@ impl<'a> WgpuExample {
50
84
let config = wgpu:: SurfaceConfiguration {
51
85
usage : wgpu:: TextureUsages :: RENDER_ATTACHMENT ,
52
86
format : surface_format,
53
- width : 512 ,
54
- height : 512 ,
87
+ width : WINDOW_SIZE ,
88
+ height : WINDOW_SIZE ,
55
89
present_mode : surface_caps. present_modes [ 0 ] ,
56
90
alpha_mode : surface_caps. alpha_modes [ 0 ] ,
57
91
view_formats : vec ! [ ] ,
@@ -62,7 +96,7 @@ impl<'a> WgpuExample {
62
96
let render_pipeline_layout =
63
97
device. create_pipeline_layout ( & wgpu:: PipelineLayoutDescriptor {
64
98
label : Some ( "Render Pipeline Layout" ) ,
65
- bind_group_layouts : & [ ] ,
99
+ bind_group_layouts : & [ & bind_group_layout ] ,
66
100
push_constant_ranges : & [ ] ,
67
101
} ) ;
68
102
@@ -103,7 +137,17 @@ impl<'a> WgpuExample {
103
137
104
138
surface. configure ( & device, & config) ;
105
139
106
- Self { pipeline, device, surface, queue, vertex_buffer, surface_caps }
140
+ Self {
141
+ pipeline,
142
+ device,
143
+ surface,
144
+ queue,
145
+ vertex_buffer,
146
+ surface_caps,
147
+ bind_group,
148
+ mouse_pos_buffer,
149
+ size : ( WINDOW_SIZE , WINDOW_SIZE ) ,
150
+ }
107
151
}
108
152
}
109
153
@@ -133,6 +177,7 @@ impl WindowHandler for WgpuExample {
133
177
} ) ;
134
178
135
179
render_pass. set_pipeline ( & self . pipeline ) ;
180
+ render_pass. set_bind_group ( 0 , & self . bind_group , & [ ] ) ;
136
181
render_pass. set_vertex_buffer ( 0 , self . vertex_buffer . slice ( ..) ) ;
137
182
render_pass. draw ( 0 ..3 , 0 ..1 ) ;
138
183
}
@@ -145,29 +190,21 @@ impl WindowHandler for WgpuExample {
145
190
) -> baseview:: EventStatus {
146
191
match event {
147
192
baseview:: Event :: Mouse ( MouseEvent :: CursorMoved { position, modifiers : _ } ) => {
148
- let center_x: f32 = ( position. x as f32 - 256.0 ) / 256.0 ;
149
- let center_y: f32 = ( 256.0 - position. y as f32 ) / 256.0 ;
150
- let vertices = & [
151
- Vertex { position : [ center_x, center_y + 0.25 , 0.0 ] , color : [ 1.0 , 0.0 , 0.0 ] } ,
152
- Vertex {
153
- position : [ center_x - 0.25 , center_y - 0.25 , 0.0 ] ,
154
- color : [ 0.0 , 1.0 , 0.0 ] ,
155
- } ,
156
- Vertex {
157
- position : [ center_x + 0.25 , center_y - 0.25 , 0.0 ] ,
158
- color : [ 0.0 , 0.0 , 1.0 ] ,
159
- } ,
160
- ] ;
161
- let vertex_buffer =
162
- self . device . create_buffer_init ( & wgpu:: util:: BufferInitDescriptor {
163
- label : Some ( "Vertex Buffer" ) ,
164
- contents : bytemuck:: cast_slice ( vertices) ,
165
- usage : wgpu:: BufferUsages :: VERTEX ,
166
- } ) ;
167
-
168
- self . vertex_buffer = vertex_buffer;
193
+ let center_x: f32 =
194
+ ( position. x as f32 - ( self . size . 0 as f32 / 2.0 ) ) / ( self . size . 0 as f32 / 2.0 ) ;
195
+ let center_y: f32 =
196
+ ( ( self . size . 1 as f32 / 2.0 ) - position. y as f32 ) / ( self . size . 1 as f32 / 2.0 ) ;
197
+
198
+ self . queue . write_buffer (
199
+ & self . mouse_pos_buffer ,
200
+ 0 ,
201
+ bytemuck:: cast_slice ( & [ center_x, center_y] ) ,
202
+ )
169
203
}
170
204
baseview:: Event :: Window ( baseview:: WindowEvent :: Resized ( size) ) => {
205
+ let width = size. physical_size ( ) . width ;
206
+ let height = size. physical_size ( ) . height ;
207
+
171
208
let surface_format = self
172
209
. surface_caps
173
210
. formats
@@ -181,13 +218,13 @@ impl WindowHandler for WgpuExample {
181
218
& wgpu:: SurfaceConfiguration {
182
219
usage : wgpu:: TextureUsages :: RENDER_ATTACHMENT ,
183
220
format : surface_format,
184
- width : size . physical_size ( ) . width ,
185
- height : size . physical_size ( ) . height ,
221
+ width,
222
+ height,
186
223
present_mode : self . surface_caps . present_modes [ 0 ] ,
187
224
alpha_mode : self . surface_caps . alpha_modes [ 0 ] ,
188
225
view_formats : vec ! [ ] ,
189
226
} ,
190
- )
227
+ ) ;
191
228
}
192
229
_ => { }
193
230
}
@@ -226,7 +263,7 @@ impl Vertex {
226
263
fn main ( ) {
227
264
let window_open_options = WindowOpenOptions {
228
265
title : "wgpu on baseview" . into ( ) ,
229
- size : Size :: new ( 512.0 , 512.0 ) ,
266
+ size : Size :: new ( WINDOW_SIZE as f64 , WINDOW_SIZE as f64 ) ,
230
267
scale : baseview:: WindowScalePolicy :: SystemScaleFactor ,
231
268
gl_config : None ,
232
269
} ;
0 commit comments