pub struct GraphicsContext {
pub instance: Instance,
pub adapter: Adapter,
pub device: Device,
pub queue: Queue,
/* private fields */
}Expand description
A globally shared graphics context.
Fields§
§instance: Instance§adapter: Adapter§device: Device§queue: QueueImplementations§
Source§impl GraphicsContext
impl GraphicsContext
Sourcepub fn new_sync() -> &'static Self
pub fn new_sync() -> &'static Self
Creates a new graphics context synchronously.
See GraphicsContext::new for the asynchronous version.
Examples found in repository?
examples/multi_window.rs (line 32)
28fn main() {
29 logging::init();
30
31 run_app(|ctx| {
32 let graphics_ctx = GraphicsContext::new_sync();
33
34 let mut windows = HashMap::new();
35
36 // Create 3 windows with different colors
37 let colors = [
38 wgpu::Color {
39 r: 0.8,
40 g: 0.2,
41 b: 0.2,
42 a: 1.0,
43 },
44 wgpu::Color {
45 r: 0.2,
46 g: 0.8,
47 b: 0.2,
48 a: 1.0,
49 },
50 wgpu::Color {
51 r: 0.2,
52 g: 0.2,
53 b: 0.8,
54 a: 1.0,
55 },
56 ];
57
58 for (i, color) in colors.iter().enumerate() {
59 let window = ctx
60 .create_window(WindowDescriptor {
61 title: format!("Window {} - Multi-Window Example", i + 1),
62 size: Some(PhysicalSize::new(400.0, 300.0)),
63 ..Default::default()
64 })
65 .expect("Failed to create window");
66
67 let renderable_window = RenderableWindow::new_with_descriptor(
68 window,
69 graphics_ctx,
70 WindowContextDescriptor {
71 format: Some(wgpu::TextureFormat::Bgra8UnormSrgb),
72 ..Default::default()
73 },
74 );
75
76 let window_id = renderable_window.id();
77 windows.insert(window_id, (renderable_window, *color));
78 }
79
80 Box::new(App {
81 context: graphics_ctx,
82 windows,
83 })
84 });
85}More examples
examples/sprite_sheet.rs (line 152)
148fn main() {
149 logging::init();
150
151 run_app(|ctx| {
152 let graphics_ctx = GraphicsContext::new_sync();
153 let mut windows = HashMap::new();
154
155 let scale = Window::platform_dpi() as f32;
156 let window = ctx
157 .create_window(WindowDescriptor {
158 title: "Sprite Sheet Animation Example".to_string(),
159 size: Some(PhysicalSize::new(400.0 * scale, 400.0 * scale)),
160 ..Default::default()
161 })
162 .expect("Failed to create window");
163
164 let renderable_window = RenderableWindow::new_with_descriptor(
165 window,
166 graphics_ctx,
167 WindowContextDescriptor {
168 format: Some(wgpu::TextureFormat::Bgra8UnormSrgb),
169 ..Default::default()
170 },
171 );
172
173 let window_id = renderable_window.id();
174 windows.insert(window_id, renderable_window);
175
176 // Generate sprite sheet
177 let (sprite_data, tex_width, tex_height) = generate_sprite_sheet_data();
178 let sprite_sheet = SpriteSheet::from_data(
179 graphics_ctx,
180 &sprite_data,
181 tex_width,
182 tex_height,
183 SpriteSheetDescriptor {
184 sprite_width: 64,
185 sprite_height: 64,
186 columns: 4,
187 rows: 1,
188 ..Default::default()
189 },
190 );
191
192 // Create animation (4 frames at 8 fps)
193 let animation = SpriteAnimation::new(4, 8.0);
194
195 // Create shader module
196 let shader = graphics_ctx.device.create_shader_module(wgpu::ShaderModuleDescriptor {
197 label: Some("Sprite Shader"),
198 source: wgpu::ShaderSource::Wgsl(SHADER.into()),
199 });
200
201 // Create bind group layout
202 let bind_group_layout = graphics_ctx.device.create_bind_group_layout(&wgpu::BindGroupLayoutDescriptor {
203 label: Some("Sprite Bind Group Layout"),
204 entries: &[
205 wgpu::BindGroupLayoutEntry {
206 binding: 0,
207 visibility: wgpu::ShaderStages::VERTEX,
208 ty: wgpu::BindingType::Buffer {
209 ty: wgpu::BufferBindingType::Uniform,
210 has_dynamic_offset: false,
211 min_binding_size: None,
212 },
213 count: None,
214 },
215 wgpu::BindGroupLayoutEntry {
216 binding: 1,
217 visibility: wgpu::ShaderStages::FRAGMENT,
218 ty: wgpu::BindingType::Texture {
219 sample_type: wgpu::TextureSampleType::Float { filterable: true },
220 view_dimension: wgpu::TextureViewDimension::D2,
221 multisampled: false,
222 },
223 count: None,
224 },
225 wgpu::BindGroupLayoutEntry {
226 binding: 2,
227 visibility: wgpu::ShaderStages::FRAGMENT,
228 ty: wgpu::BindingType::Sampler(wgpu::SamplerBindingType::Filtering),
229 count: None,
230 },
231 ],
232 });
233
234 // Create pipeline layout
235 let pipeline_layout = graphics_ctx.device.create_pipeline_layout(&wgpu::PipelineLayoutDescriptor {
236 label: Some("Sprite Pipeline Layout"),
237 bind_group_layouts: &[&bind_group_layout],
238 push_constant_ranges: &[],
239 });
240
241 // Create render pipeline
242 let pipeline = graphics_ctx.device.create_render_pipeline(&wgpu::RenderPipelineDescriptor {
243 label: Some("Sprite Pipeline"),
244 layout: Some(&pipeline_layout),
245 vertex: wgpu::VertexState {
246 module: &shader,
247 entry_point: Some("vs_main"),
248 buffers: &[wgpu::VertexBufferLayout {
249 array_stride: std::mem::size_of::<Vertex>() as u64,
250 step_mode: wgpu::VertexStepMode::Vertex,
251 attributes: &[
252 wgpu::VertexAttribute {
253 offset: 0,
254 shader_location: 0,
255 format: wgpu::VertexFormat::Float32x2,
256 },
257 wgpu::VertexAttribute {
258 offset: 8,
259 shader_location: 1,
260 format: wgpu::VertexFormat::Float32x2,
261 },
262 ],
263 }],
264 compilation_options: wgpu::PipelineCompilationOptions::default(),
265 },
266 fragment: Some(wgpu::FragmentState {
267 module: &shader,
268 entry_point: Some("fs_main"),
269 targets: &[Some(wgpu::ColorTargetState {
270 format: wgpu::TextureFormat::Bgra8UnormSrgb,
271 blend: Some(wgpu::BlendState::ALPHA_BLENDING),
272 write_mask: wgpu::ColorWrites::ALL,
273 })],
274 compilation_options: wgpu::PipelineCompilationOptions::default(),
275 }),
276 primitive: wgpu::PrimitiveState {
277 topology: wgpu::PrimitiveTopology::TriangleList,
278 ..Default::default()
279 },
280 depth_stencil: None,
281 multisample: wgpu::MultisampleState::default(),
282 multiview: None,
283 cache: None,
284 });
285
286 // Create uniform buffer
287 let uniforms = Uniforms {
288 mvp: [
289 [1.0, 0.0, 0.0, 0.0],
290 [0.0, 1.0, 0.0, 0.0],
291 [0.0, 0.0, 1.0, 0.0],
292 [0.0, 0.0, 0.0, 1.0],
293 ],
294 };
295 let uniform_buffer = graphics_ctx.device.create_buffer_init(&wgpu::util::BufferInitDescriptor {
296 label: Some("Uniform Buffer"),
297 contents: bytemuck::cast_slice(&[uniforms]),
298 usage: wgpu::BufferUsages::UNIFORM | wgpu::BufferUsages::COPY_DST,
299 });
300
301 // Create sampler
302 let sampler = graphics_ctx.device.create_sampler(&wgpu::SamplerDescriptor {
303 label: Some("Sprite Sampler"),
304 mag_filter: wgpu::FilterMode::Linear,
305 min_filter: wgpu::FilterMode::Linear,
306 ..Default::default()
307 });
308
309 // Create bind group
310 let bind_group = graphics_ctx.device.create_bind_group(&wgpu::BindGroupDescriptor {
311 label: Some("Sprite Bind Group"),
312 layout: &bind_group_layout,
313 entries: &[
314 wgpu::BindGroupEntry {
315 binding: 0,
316 resource: uniform_buffer.as_entire_binding(),
317 },
318 wgpu::BindGroupEntry {
319 binding: 1,
320 resource: wgpu::BindingResource::TextureView(sprite_sheet.view()),
321 },
322 wgpu::BindGroupEntry {
323 binding: 2,
324 resource: wgpu::BindingResource::Sampler(&sampler),
325 },
326 ],
327 });
328
329 // Initial vertex buffer (will be updated each frame with new UVs)
330 let vertices = create_quad_vertices(0.0, 0.0, 1.0, 1.0);
331 let vertex_buffer = graphics_ctx.device.create_buffer_init(&wgpu::util::BufferInitDescriptor {
332 label: Some("Vertex Buffer"),
333 contents: bytemuck::cast_slice(&vertices),
334 usage: wgpu::BufferUsages::VERTEX | wgpu::BufferUsages::COPY_DST,
335 });
336
337 Box::new(App {
338 _context: graphics_ctx,
339 windows,
340 pipeline,
341 bind_group,
342 vertex_buffer,
343 uniform_buffer,
344 sprite_sheet,
345 animation,
346 last_update: Instant::now(),
347 })
348 });
349}examples/textured_window.rs (line 23)
19fn main() {
20 logging::init();
21
22 run_app(|ctx| {
23 let graphics_ctx = GraphicsContext::new_sync();
24
25 let window = ctx
26 .create_window(WindowDescriptor {
27 title: "Textured Window".to_string(),
28 ..Default::default()
29 })
30 .expect("Failed to create window");
31
32 let window = RenderableWindow::new_with_descriptor(
33 window,
34 graphics_ctx,
35 WindowContextDescriptor {
36 format: Some(wgpu::TextureFormat::Bgra8UnormSrgb),
37 ..Default::default()
38 },
39 );
40
41 let shader = graphics_ctx
42 .device
43 .create_shader_module(wgpu::ShaderModuleDescriptor {
44 label: Some("Texture Shader"),
45 source: wgpu::ShaderSource::Wgsl(include_str!("textured_window.wgsl").into()),
46 });
47
48 let texture_size = wgpu::Extent3d {
49 width: 256,
50 height: 256,
51 depth_or_array_layers: 1,
52 };
53
54 let texture = graphics_ctx
55 .device
56 .create_texture(&wgpu::TextureDescriptor {
57 label: Some("Example Texture"),
58 size: texture_size,
59 mip_level_count: 1,
60 sample_count: 1,
61 dimension: wgpu::TextureDimension::D2,
62 format: wgpu::TextureFormat::Rgba8UnormSrgb,
63 usage: wgpu::TextureUsages::TEXTURE_BINDING | wgpu::TextureUsages::COPY_DST,
64 view_formats: &[],
65 });
66
67 let mut texture_data = vec![0u8; (256 * 256 * 4) as usize];
68 for y in 0..256 {
69 for x in 0..256 {
70 let idx = ((y * 256 + x) * 4) as usize;
71 texture_data[idx] = x as u8;
72 texture_data[idx + 1] = y as u8;
73 texture_data[idx + 2] = ((x + y) / 2) as u8;
74 texture_data[idx + 3] = 255;
75 }
76 }
77
78 graphics_ctx.queue.write_texture(
79 wgpu::TexelCopyTextureInfo {
80 texture: &texture,
81 mip_level: 0,
82 origin: wgpu::Origin3d::ZERO,
83 aspect: wgpu::TextureAspect::All,
84 },
85 &texture_data,
86 wgpu::TexelCopyBufferLayout {
87 offset: 0,
88 bytes_per_row: Some(256 * 4),
89 rows_per_image: Some(256),
90 },
91 texture_size,
92 );
93
94 let texture_view = texture.create_view(&wgpu::TextureViewDescriptor::default());
95 let sampler = graphics_ctx
96 .device
97 .create_sampler(&wgpu::SamplerDescriptor {
98 address_mode_u: wgpu::AddressMode::ClampToEdge,
99 address_mode_v: wgpu::AddressMode::ClampToEdge,
100 address_mode_w: wgpu::AddressMode::ClampToEdge,
101 mag_filter: wgpu::FilterMode::Linear,
102 min_filter: wgpu::FilterMode::Nearest,
103 mipmap_filter: wgpu::FilterMode::Nearest,
104 ..Default::default()
105 });
106
107 let bind_group_layout =
108 graphics_ctx
109 .device
110 .create_bind_group_layout(&wgpu::BindGroupLayoutDescriptor {
111 label: Some("Texture Bind Group Layout"),
112 entries: &[
113 wgpu::BindGroupLayoutEntry {
114 binding: 0,
115 visibility: wgpu::ShaderStages::FRAGMENT,
116 ty: wgpu::BindingType::Texture {
117 multisampled: false,
118 view_dimension: wgpu::TextureViewDimension::D2,
119 sample_type: wgpu::TextureSampleType::Float { filterable: true },
120 },
121 count: None,
122 },
123 wgpu::BindGroupLayoutEntry {
124 binding: 1,
125 visibility: wgpu::ShaderStages::FRAGMENT,
126 ty: wgpu::BindingType::Sampler(wgpu::SamplerBindingType::Filtering),
127 count: None,
128 },
129 ],
130 });
131
132 let bind_group = graphics_ctx
133 .device
134 .create_bind_group(&wgpu::BindGroupDescriptor {
135 label: Some("Texture Bind Group"),
136 layout: &bind_group_layout,
137 entries: &[
138 wgpu::BindGroupEntry {
139 binding: 0,
140 resource: wgpu::BindingResource::TextureView(&texture_view),
141 },
142 wgpu::BindGroupEntry {
143 binding: 1,
144 resource: wgpu::BindingResource::Sampler(&sampler),
145 },
146 ],
147 });
148
149 let pipeline_layout =
150 graphics_ctx
151 .device
152 .create_pipeline_layout(&wgpu::PipelineLayoutDescriptor {
153 label: Some("Render Pipeline Layout"),
154 bind_group_layouts: &[&bind_group_layout],
155 push_constant_ranges: &[],
156 });
157
158 let pipeline =
159 graphics_ctx
160 .device
161 .create_render_pipeline(&wgpu::RenderPipelineDescriptor {
162 label: Some("Render Pipeline"),
163 layout: Some(&pipeline_layout),
164 vertex: wgpu::VertexState {
165 module: &shader,
166 entry_point: Some("vs_main"),
167 buffers: &[wgpu::VertexBufferLayout {
168 array_stride: 4 * 4,
169 step_mode: wgpu::VertexStepMode::Vertex,
170 attributes: &wgpu::vertex_attr_array![0 => Float32x2, 1 => Float32x2],
171 }],
172 compilation_options: wgpu::PipelineCompilationOptions::default(),
173 },
174 fragment: Some(wgpu::FragmentState {
175 module: &shader,
176 entry_point: Some("fs_main"),
177 targets: &[Some(wgpu::ColorTargetState {
178 format: wgpu::TextureFormat::Bgra8UnormSrgb,
179 blend: Some(wgpu::BlendState::REPLACE),
180 write_mask: wgpu::ColorWrites::ALL,
181 })],
182 compilation_options: wgpu::PipelineCompilationOptions::default(),
183 }),
184 primitive: wgpu::PrimitiveState {
185 topology: wgpu::PrimitiveTopology::TriangleList,
186 strip_index_format: None,
187 front_face: wgpu::FrontFace::Ccw,
188 cull_mode: Some(wgpu::Face::Back),
189 polygon_mode: wgpu::PolygonMode::Fill,
190 unclipped_depth: false,
191 conservative: false,
192 },
193 depth_stencil: None,
194 multisample: wgpu::MultisampleState {
195 count: 1,
196 mask: !0,
197 alpha_to_coverage_enabled: false,
198 },
199 multiview: None,
200 cache: None,
201 });
202
203 #[rustfmt::skip]
204 let vertices: &[f32] = &[
205 -0.8, -0.8, 0.0, 1.0,
206 0.8, -0.8, 1.0, 1.0,
207 0.8, 0.8, 1.0, 0.0,
208 -0.8, -0.8, 0.0, 1.0,
209 0.8, 0.8, 1.0, 0.0,
210 -0.8, 0.8, 0.0, 0.0,
211 ];
212
213 let vertex_buffer = graphics_ctx.device.create_buffer(&wgpu::BufferDescriptor {
214 label: Some("Vertex Buffer"),
215 size: (vertices.len() * std::mem::size_of::<f32>()) as u64,
216 usage: wgpu::BufferUsages::VERTEX | wgpu::BufferUsages::COPY_DST,
217 mapped_at_creation: false,
218 });
219
220 graphics_ctx
221 .queue
222 .write_buffer(&vertex_buffer, 0, bytemuck::cast_slice(vertices));
223
224 let window_id = window.id();
225
226 Box::new(App {
227 window,
228 window_id,
229 pipeline,
230 bind_group,
231 vertex_buffer,
232 })
233 });
234}examples/image_blitting.rs (line 171)
167fn main() {
168 logging::init();
169
170 run_app(|ctx| {
171 let graphics_ctx = GraphicsContext::new_sync();
172 let mut windows = HashMap::new();
173
174 let scale = Window::platform_dpi() as f32;
175 let window = ctx
176 .create_window(WindowDescriptor {
177 title: "Image Blitting Example".to_string(),
178 size: Some(PhysicalSize::new(800.0 * scale, 600.0 * scale)),
179 ..Default::default()
180 })
181 .expect("Failed to create window");
182
183 let renderable_window = RenderableWindow::new_with_descriptor(
184 window,
185 graphics_ctx,
186 WindowContextDescriptor {
187 format: Some(wgpu::TextureFormat::Bgra8UnormSrgb),
188 ..Default::default()
189 },
190 );
191
192 let window_id = renderable_window.id();
193 windows.insert(window_id, renderable_window);
194
195 // Create shader module
196 let shader = graphics_ctx.device.create_shader_module(wgpu::ShaderModuleDescriptor {
197 label: Some("Blit Shader"),
198 source: wgpu::ShaderSource::Wgsl(SHADER.into()),
199 });
200
201 // Create bind group layout
202 let bind_group_layout = graphics_ctx.device.create_bind_group_layout(&wgpu::BindGroupLayoutDescriptor {
203 label: Some("Blit Bind Group Layout"),
204 entries: &[
205 wgpu::BindGroupLayoutEntry {
206 binding: 0,
207 visibility: wgpu::ShaderStages::VERTEX | wgpu::ShaderStages::FRAGMENT,
208 ty: wgpu::BindingType::Buffer {
209 ty: wgpu::BufferBindingType::Uniform,
210 has_dynamic_offset: false,
211 min_binding_size: None,
212 },
213 count: None,
214 },
215 wgpu::BindGroupLayoutEntry {
216 binding: 1,
217 visibility: wgpu::ShaderStages::FRAGMENT,
218 ty: wgpu::BindingType::Texture {
219 sample_type: wgpu::TextureSampleType::Float { filterable: true },
220 view_dimension: wgpu::TextureViewDimension::D2,
221 multisampled: false,
222 },
223 count: None,
224 },
225 wgpu::BindGroupLayoutEntry {
226 binding: 2,
227 visibility: wgpu::ShaderStages::FRAGMENT,
228 ty: wgpu::BindingType::Sampler(wgpu::SamplerBindingType::Filtering),
229 count: None,
230 },
231 ],
232 });
233
234 // Create pipeline layout
235 let pipeline_layout = graphics_ctx.device.create_pipeline_layout(&wgpu::PipelineLayoutDescriptor {
236 label: Some("Blit Pipeline Layout"),
237 bind_group_layouts: &[&bind_group_layout],
238 push_constant_ranges: &[],
239 });
240
241 // Create render pipeline
242 let pipeline = graphics_ctx.device.create_render_pipeline(&wgpu::RenderPipelineDescriptor {
243 label: Some("Blit Pipeline"),
244 layout: Some(&pipeline_layout),
245 vertex: wgpu::VertexState {
246 module: &shader,
247 entry_point: Some("vs_main"),
248 buffers: &[wgpu::VertexBufferLayout {
249 array_stride: std::mem::size_of::<Vertex>() as u64,
250 step_mode: wgpu::VertexStepMode::Vertex,
251 attributes: &[
252 wgpu::VertexAttribute {
253 offset: 0,
254 shader_location: 0,
255 format: wgpu::VertexFormat::Float32x2,
256 },
257 wgpu::VertexAttribute {
258 offset: 8,
259 shader_location: 1,
260 format: wgpu::VertexFormat::Float32x2,
261 },
262 ],
263 }],
264 compilation_options: wgpu::PipelineCompilationOptions::default(),
265 },
266 fragment: Some(wgpu::FragmentState {
267 module: &shader,
268 entry_point: Some("fs_main"),
269 targets: &[Some(wgpu::ColorTargetState {
270 format: wgpu::TextureFormat::Bgra8UnormSrgb,
271 blend: Some(wgpu::BlendState::ALPHA_BLENDING),
272 write_mask: wgpu::ColorWrites::ALL,
273 })],
274 compilation_options: wgpu::PipelineCompilationOptions::default(),
275 }),
276 primitive: wgpu::PrimitiveState {
277 topology: wgpu::PrimitiveTopology::TriangleList,
278 strip_index_format: None,
279 front_face: wgpu::FrontFace::Ccw,
280 cull_mode: None,
281 polygon_mode: wgpu::PolygonMode::Fill,
282 unclipped_depth: false,
283 conservative: false,
284 },
285 depth_stencil: None,
286 multisample: wgpu::MultisampleState::default(),
287 multiview: None,
288 cache: None,
289 });
290
291 // Create vertex buffer for a fullscreen quad
292 let vertices = [
293 Vertex { position: [-0.8, -0.8], uv: [0.0, 1.0] },
294 Vertex { position: [0.8, -0.8], uv: [1.0, 1.0] },
295 Vertex { position: [0.8, 0.8], uv: [1.0, 0.0] },
296 Vertex { position: [-0.8, -0.8], uv: [0.0, 1.0] },
297 Vertex { position: [0.8, 0.8], uv: [1.0, 0.0] },
298 Vertex { position: [-0.8, 0.8], uv: [0.0, 0.0] },
299 ];
300 let vertex_buffer = graphics_ctx.device.create_buffer_init(&wgpu::util::BufferInitDescriptor {
301 label: Some("Vertex Buffer"),
302 contents: bytemuck::cast_slice(&vertices),
303 usage: wgpu::BufferUsages::VERTEX,
304 });
305
306 // Create uniform buffer
307 let uniforms = Uniforms {
308 mvp: [
309 [1.0, 0.0, 0.0, 0.0],
310 [0.0, 1.0, 0.0, 0.0],
311 [0.0, 0.0, 1.0, 0.0],
312 [0.0, 0.0, 0.0, 1.0],
313 ],
314 tint: [1.0, 1.0, 1.0, 1.0],
315 };
316 let uniform_buffer = graphics_ctx.device.create_buffer_init(&wgpu::util::BufferInitDescriptor {
317 label: Some("Uniform Buffer"),
318 contents: bytemuck::cast_slice(&[uniforms]),
319 usage: wgpu::BufferUsages::UNIFORM | wgpu::BufferUsages::COPY_DST,
320 });
321
322 // Create CPU-side image buffer
323 let mut image_buffer = ImageBuffer::new(256, 256);
324 image_buffer.clear(30, 30, 40, 255);
325
326 // Create GPU texture
327 let texture = graphics_ctx.device.create_texture(&wgpu::TextureDescriptor {
328 label: Some("Blit Texture"),
329 size: wgpu::Extent3d {
330 width: 256,
331 height: 256,
332 depth_or_array_layers: 1,
333 },
334 mip_level_count: 1,
335 sample_count: 1,
336 dimension: wgpu::TextureDimension::D2,
337 format: wgpu::TextureFormat::Rgba8UnormSrgb,
338 usage: wgpu::TextureUsages::TEXTURE_BINDING | wgpu::TextureUsages::COPY_DST,
339 view_formats: &[],
340 });
341
342 // Create texture view and sampler
343 let texture_view = texture.create_view(&wgpu::TextureViewDescriptor::default());
344 let sampler = graphics_ctx.device.create_sampler(&wgpu::SamplerDescriptor {
345 label: Some("Blit Sampler"),
346 address_mode_u: wgpu::AddressMode::ClampToEdge,
347 address_mode_v: wgpu::AddressMode::ClampToEdge,
348 address_mode_w: wgpu::AddressMode::ClampToEdge,
349 mag_filter: wgpu::FilterMode::Nearest, // Pixel-perfect rendering
350 min_filter: wgpu::FilterMode::Nearest,
351 mipmap_filter: wgpu::FilterMode::Nearest,
352 ..Default::default()
353 });
354
355 // Create bind group
356 let bind_group = graphics_ctx.device.create_bind_group(&wgpu::BindGroupDescriptor {
357 label: Some("Blit Bind Group"),
358 layout: &bind_group_layout,
359 entries: &[
360 wgpu::BindGroupEntry {
361 binding: 0,
362 resource: uniform_buffer.as_entire_binding(),
363 },
364 wgpu::BindGroupEntry {
365 binding: 1,
366 resource: wgpu::BindingResource::TextureView(&texture_view),
367 },
368 wgpu::BindGroupEntry {
369 binding: 2,
370 resource: wgpu::BindingResource::Sampler(&sampler),
371 },
372 ],
373 });
374
375 Box::new(App {
376 context: graphics_ctx,
377 windows,
378 pipeline,
379 bind_group_layout,
380 vertex_buffer,
381 texture,
382 bind_group,
383 uniform_buffer,
384 image_buffer,
385 start_time: Instant::now(),
386 })
387 });
388}examples/renderer_api.rs (line 32)
28fn main() {
29 logging::init();
30
31 run_app(|ctx| {
32 let graphics_ctx = GraphicsContext::new_sync();
33 let renderer = Renderer::new(graphics_ctx);
34
35 let window = ctx
36 .create_window(WindowDescriptor {
37 title: "Renderer API Example".to_string(),
38 size: Some(PhysicalSize::new(800.0, 600.0)),
39 ..Default::default()
40 })
41 .expect("Failed to create window");
42
43 let window = RenderableWindow::new_with_descriptor(
44 window,
45 graphics_ctx,
46 WindowContextDescriptor {
47 format: Some(wgpu::TextureFormat::Bgra8UnormSrgb),
48 ..Default::default()
49 },
50 );
51
52 let window_id = window.id();
53
54 // Create shader using Renderer API
55 let shader = renderer.create_shader(Some("Color Shader"), SHADER_SOURCE);
56
57 // Create texture using Renderer helper
58 let texture_data = create_gradient_texture();
59 let texture = renderer.create_texture_2d(
60 Some("Gradient Texture"),
61 256,
62 256,
63 wgpu::TextureFormat::Rgba8UnormSrgb,
64 wgpu::TextureUsages::TEXTURE_BINDING,
65 &texture_data,
66 );
67
68 let texture_view = texture.create_view(&wgpu::TextureViewDescriptor::default());
69 let sampler = renderer.create_linear_sampler(Some("Linear Sampler"));
70
71 // Create bind group using Renderer API
72 let bind_group_layout = renderer.create_bind_group_layout(
73 Some("Texture Bind Group Layout"),
74 &[
75 wgpu::BindGroupLayoutEntry {
76 binding: 0,
77 visibility: wgpu::ShaderStages::FRAGMENT,
78 ty: wgpu::BindingType::Texture {
79 multisampled: false,
80 view_dimension: wgpu::TextureViewDimension::D2,
81 sample_type: wgpu::TextureSampleType::Float { filterable: true },
82 },
83 count: None,
84 },
85 wgpu::BindGroupLayoutEntry {
86 binding: 1,
87 visibility: wgpu::ShaderStages::FRAGMENT,
88 ty: wgpu::BindingType::Sampler(wgpu::SamplerBindingType::Filtering),
89 count: None,
90 },
91 ],
92 );
93
94 let bind_group = renderer.create_bind_group(
95 Some("Texture Bind Group"),
96 &bind_group_layout,
97 &[
98 wgpu::BindGroupEntry {
99 binding: 0,
100 resource: wgpu::BindingResource::TextureView(&texture_view),
101 },
102 wgpu::BindGroupEntry {
103 binding: 1,
104 resource: wgpu::BindingResource::Sampler(&sampler),
105 },
106 ],
107 );
108
109 let pipeline_layout = renderer.create_pipeline_layout(
110 Some("Render Pipeline Layout"),
111 &[&bind_group_layout],
112 &[],
113 );
114
115 // Create pipeline using Renderer API with BlendMode
116 let pipeline = renderer.create_render_pipeline(&wgpu::RenderPipelineDescriptor {
117 label: Some("Render Pipeline"),
118 layout: Some(&pipeline_layout),
119 vertex: wgpu::VertexState {
120 module: &shader,
121 entry_point: Some("vs_main"),
122 buffers: &[wgpu::VertexBufferLayout {
123 array_stride: 4 * 4,
124 step_mode: wgpu::VertexStepMode::Vertex,
125 attributes: &wgpu::vertex_attr_array![0 => Float32x2, 1 => Float32x2],
126 }],
127 compilation_options: wgpu::PipelineCompilationOptions::default(),
128 },
129 fragment: Some(wgpu::FragmentState {
130 module: &shader,
131 entry_point: Some("fs_main"),
132 // Use BlendMode for transparent rendering
133 targets: &[Some(
134 BlendMode::Alpha.to_color_target_state(wgpu::TextureFormat::Rgba8UnormSrgb),
135 )],
136 compilation_options: wgpu::PipelineCompilationOptions::default(),
137 }),
138 primitive: wgpu::PrimitiveState {
139 topology: wgpu::PrimitiveTopology::TriangleList,
140 strip_index_format: None,
141 front_face: wgpu::FrontFace::Ccw,
142 cull_mode: Some(wgpu::Face::Back),
143 polygon_mode: wgpu::PolygonMode::Fill,
144 unclipped_depth: false,
145 conservative: false,
146 },
147 depth_stencil: None,
148 multisample: wgpu::MultisampleState {
149 count: 1,
150 mask: !0,
151 alpha_to_coverage_enabled: false,
152 },
153 multiview: None,
154 cache: None,
155 });
156
157 #[rustfmt::skip]
158 let vertices: &[f32] = &[
159 -0.8, -0.8, 0.0, 1.0,
160 0.8, -0.8, 1.0, 1.0,
161 0.8, 0.8, 1.0, 0.0,
162 -0.8, -0.8, 0.0, 1.0,
163 0.8, 0.8, 1.0, 0.0,
164 -0.8, 0.8, 0.0, 0.0,
165 ];
166
167 // Create vertex buffer using Renderer helper
168 let vertex_buffer = renderer.create_vertex_buffer(Some("Vertex Buffer"), vertices);
169
170 // Create offscreen framebuffer using the new Framebuffer abstraction
171 let offscreen_fb = Framebuffer::builder(400, 300)
172 .format(wgpu::TextureFormat::Rgba8UnormSrgb)
173 .label("Offscreen FB")
174 .build(graphics_ctx);
175
176 // Create blit shader and pipeline for rendering framebuffer to surface
177 let blit_shader = renderer.create_shader(Some("Blit Shader"), BLIT_SHADER_SOURCE);
178
179 let blit_bind_group_layout = renderer.create_bind_group_layout(
180 Some("Blit Bind Group Layout"),
181 &[
182 wgpu::BindGroupLayoutEntry {
183 binding: 0,
184 visibility: wgpu::ShaderStages::FRAGMENT,
185 ty: wgpu::BindingType::Texture {
186 multisampled: false,
187 view_dimension: wgpu::TextureViewDimension::D2,
188 sample_type: wgpu::TextureSampleType::Float { filterable: true },
189 },
190 count: None,
191 },
192 wgpu::BindGroupLayoutEntry {
193 binding: 1,
194 visibility: wgpu::ShaderStages::FRAGMENT,
195 ty: wgpu::BindingType::Sampler(wgpu::SamplerBindingType::Filtering),
196 count: None,
197 },
198 ],
199 );
200
201 let blit_bind_group = renderer.create_bind_group(
202 Some("Blit Bind Group"),
203 &blit_bind_group_layout,
204 &[
205 wgpu::BindGroupEntry {
206 binding: 0,
207 resource: wgpu::BindingResource::TextureView(offscreen_fb.color_view()),
208 },
209 wgpu::BindGroupEntry {
210 binding: 1,
211 resource: wgpu::BindingResource::Sampler(&sampler),
212 },
213 ],
214 );
215
216 let blit_pipeline_layout = renderer.create_pipeline_layout(
217 Some("Blit Pipeline Layout"),
218 &[&blit_bind_group_layout],
219 &[],
220 );
221
222 let blit_pipeline = renderer.create_render_pipeline(&wgpu::RenderPipelineDescriptor {
223 label: Some("Blit Pipeline"),
224 layout: Some(&blit_pipeline_layout),
225 vertex: wgpu::VertexState {
226 module: &blit_shader,
227 entry_point: Some("vs_main"),
228 buffers: &[],
229 compilation_options: wgpu::PipelineCompilationOptions::default(),
230 },
231 fragment: Some(wgpu::FragmentState {
232 module: &blit_shader,
233 entry_point: Some("fs_main"),
234 // Use PremultipliedAlpha for framebuffer blitting
235 targets: &[Some(
236 BlendMode::PremultipliedAlpha
237 .to_color_target_state(wgpu::TextureFormat::Bgra8UnormSrgb),
238 )],
239 compilation_options: wgpu::PipelineCompilationOptions::default(),
240 }),
241 primitive: wgpu::PrimitiveState {
242 topology: wgpu::PrimitiveTopology::TriangleList,
243 ..Default::default()
244 },
245 depth_stencil: None,
246 multisample: wgpu::MultisampleState::default(),
247 multiview: None,
248 cache: None,
249 });
250
251 tracing::info!("Renderer initialized successfully");
252 tracing::info!("Device: {:?}", renderer.context().info());
253
254 Box::new(RendererApp {
255 context: graphics_ctx,
256 renderer,
257 window,
258 window_id,
259 pipeline,
260 bind_group,
261 vertex_buffer,
262 offscreen_fb,
263 blit_pipeline,
264 blit_bind_group,
265 time: 0.0,
266 })
267 });
268}Sourcepub async fn new() -> &'static Self
pub async fn new() -> &'static Self
Creates a new graphics context asynchronously.
This returns a static reference to simplify the public API and lifecycle
Sourcepub async fn new_with_descriptor(
descriptor: GraphicsContextDescriptor,
) -> &'static Self
pub async fn new_with_descriptor( descriptor: GraphicsContextDescriptor, ) -> &'static Self
Creates a new graphics context with custom descriptor.
§Panics
Panics if any required features are not supported by the adapter.
Sourcepub fn info(&self) -> AdapterInfo
pub fn info(&self) -> AdapterInfo
Get device info
Examples found in repository?
examples/renderer_api.rs (line 252)
28fn main() {
29 logging::init();
30
31 run_app(|ctx| {
32 let graphics_ctx = GraphicsContext::new_sync();
33 let renderer = Renderer::new(graphics_ctx);
34
35 let window = ctx
36 .create_window(WindowDescriptor {
37 title: "Renderer API Example".to_string(),
38 size: Some(PhysicalSize::new(800.0, 600.0)),
39 ..Default::default()
40 })
41 .expect("Failed to create window");
42
43 let window = RenderableWindow::new_with_descriptor(
44 window,
45 graphics_ctx,
46 WindowContextDescriptor {
47 format: Some(wgpu::TextureFormat::Bgra8UnormSrgb),
48 ..Default::default()
49 },
50 );
51
52 let window_id = window.id();
53
54 // Create shader using Renderer API
55 let shader = renderer.create_shader(Some("Color Shader"), SHADER_SOURCE);
56
57 // Create texture using Renderer helper
58 let texture_data = create_gradient_texture();
59 let texture = renderer.create_texture_2d(
60 Some("Gradient Texture"),
61 256,
62 256,
63 wgpu::TextureFormat::Rgba8UnormSrgb,
64 wgpu::TextureUsages::TEXTURE_BINDING,
65 &texture_data,
66 );
67
68 let texture_view = texture.create_view(&wgpu::TextureViewDescriptor::default());
69 let sampler = renderer.create_linear_sampler(Some("Linear Sampler"));
70
71 // Create bind group using Renderer API
72 let bind_group_layout = renderer.create_bind_group_layout(
73 Some("Texture Bind Group Layout"),
74 &[
75 wgpu::BindGroupLayoutEntry {
76 binding: 0,
77 visibility: wgpu::ShaderStages::FRAGMENT,
78 ty: wgpu::BindingType::Texture {
79 multisampled: false,
80 view_dimension: wgpu::TextureViewDimension::D2,
81 sample_type: wgpu::TextureSampleType::Float { filterable: true },
82 },
83 count: None,
84 },
85 wgpu::BindGroupLayoutEntry {
86 binding: 1,
87 visibility: wgpu::ShaderStages::FRAGMENT,
88 ty: wgpu::BindingType::Sampler(wgpu::SamplerBindingType::Filtering),
89 count: None,
90 },
91 ],
92 );
93
94 let bind_group = renderer.create_bind_group(
95 Some("Texture Bind Group"),
96 &bind_group_layout,
97 &[
98 wgpu::BindGroupEntry {
99 binding: 0,
100 resource: wgpu::BindingResource::TextureView(&texture_view),
101 },
102 wgpu::BindGroupEntry {
103 binding: 1,
104 resource: wgpu::BindingResource::Sampler(&sampler),
105 },
106 ],
107 );
108
109 let pipeline_layout = renderer.create_pipeline_layout(
110 Some("Render Pipeline Layout"),
111 &[&bind_group_layout],
112 &[],
113 );
114
115 // Create pipeline using Renderer API with BlendMode
116 let pipeline = renderer.create_render_pipeline(&wgpu::RenderPipelineDescriptor {
117 label: Some("Render Pipeline"),
118 layout: Some(&pipeline_layout),
119 vertex: wgpu::VertexState {
120 module: &shader,
121 entry_point: Some("vs_main"),
122 buffers: &[wgpu::VertexBufferLayout {
123 array_stride: 4 * 4,
124 step_mode: wgpu::VertexStepMode::Vertex,
125 attributes: &wgpu::vertex_attr_array![0 => Float32x2, 1 => Float32x2],
126 }],
127 compilation_options: wgpu::PipelineCompilationOptions::default(),
128 },
129 fragment: Some(wgpu::FragmentState {
130 module: &shader,
131 entry_point: Some("fs_main"),
132 // Use BlendMode for transparent rendering
133 targets: &[Some(
134 BlendMode::Alpha.to_color_target_state(wgpu::TextureFormat::Rgba8UnormSrgb),
135 )],
136 compilation_options: wgpu::PipelineCompilationOptions::default(),
137 }),
138 primitive: wgpu::PrimitiveState {
139 topology: wgpu::PrimitiveTopology::TriangleList,
140 strip_index_format: None,
141 front_face: wgpu::FrontFace::Ccw,
142 cull_mode: Some(wgpu::Face::Back),
143 polygon_mode: wgpu::PolygonMode::Fill,
144 unclipped_depth: false,
145 conservative: false,
146 },
147 depth_stencil: None,
148 multisample: wgpu::MultisampleState {
149 count: 1,
150 mask: !0,
151 alpha_to_coverage_enabled: false,
152 },
153 multiview: None,
154 cache: None,
155 });
156
157 #[rustfmt::skip]
158 let vertices: &[f32] = &[
159 -0.8, -0.8, 0.0, 1.0,
160 0.8, -0.8, 1.0, 1.0,
161 0.8, 0.8, 1.0, 0.0,
162 -0.8, -0.8, 0.0, 1.0,
163 0.8, 0.8, 1.0, 0.0,
164 -0.8, 0.8, 0.0, 0.0,
165 ];
166
167 // Create vertex buffer using Renderer helper
168 let vertex_buffer = renderer.create_vertex_buffer(Some("Vertex Buffer"), vertices);
169
170 // Create offscreen framebuffer using the new Framebuffer abstraction
171 let offscreen_fb = Framebuffer::builder(400, 300)
172 .format(wgpu::TextureFormat::Rgba8UnormSrgb)
173 .label("Offscreen FB")
174 .build(graphics_ctx);
175
176 // Create blit shader and pipeline for rendering framebuffer to surface
177 let blit_shader = renderer.create_shader(Some("Blit Shader"), BLIT_SHADER_SOURCE);
178
179 let blit_bind_group_layout = renderer.create_bind_group_layout(
180 Some("Blit Bind Group Layout"),
181 &[
182 wgpu::BindGroupLayoutEntry {
183 binding: 0,
184 visibility: wgpu::ShaderStages::FRAGMENT,
185 ty: wgpu::BindingType::Texture {
186 multisampled: false,
187 view_dimension: wgpu::TextureViewDimension::D2,
188 sample_type: wgpu::TextureSampleType::Float { filterable: true },
189 },
190 count: None,
191 },
192 wgpu::BindGroupLayoutEntry {
193 binding: 1,
194 visibility: wgpu::ShaderStages::FRAGMENT,
195 ty: wgpu::BindingType::Sampler(wgpu::SamplerBindingType::Filtering),
196 count: None,
197 },
198 ],
199 );
200
201 let blit_bind_group = renderer.create_bind_group(
202 Some("Blit Bind Group"),
203 &blit_bind_group_layout,
204 &[
205 wgpu::BindGroupEntry {
206 binding: 0,
207 resource: wgpu::BindingResource::TextureView(offscreen_fb.color_view()),
208 },
209 wgpu::BindGroupEntry {
210 binding: 1,
211 resource: wgpu::BindingResource::Sampler(&sampler),
212 },
213 ],
214 );
215
216 let blit_pipeline_layout = renderer.create_pipeline_layout(
217 Some("Blit Pipeline Layout"),
218 &[&blit_bind_group_layout],
219 &[],
220 );
221
222 let blit_pipeline = renderer.create_render_pipeline(&wgpu::RenderPipelineDescriptor {
223 label: Some("Blit Pipeline"),
224 layout: Some(&blit_pipeline_layout),
225 vertex: wgpu::VertexState {
226 module: &blit_shader,
227 entry_point: Some("vs_main"),
228 buffers: &[],
229 compilation_options: wgpu::PipelineCompilationOptions::default(),
230 },
231 fragment: Some(wgpu::FragmentState {
232 module: &blit_shader,
233 entry_point: Some("fs_main"),
234 // Use PremultipliedAlpha for framebuffer blitting
235 targets: &[Some(
236 BlendMode::PremultipliedAlpha
237 .to_color_target_state(wgpu::TextureFormat::Bgra8UnormSrgb),
238 )],
239 compilation_options: wgpu::PipelineCompilationOptions::default(),
240 }),
241 primitive: wgpu::PrimitiveState {
242 topology: wgpu::PrimitiveTopology::TriangleList,
243 ..Default::default()
244 },
245 depth_stencil: None,
246 multisample: wgpu::MultisampleState::default(),
247 multiview: None,
248 cache: None,
249 });
250
251 tracing::info!("Renderer initialized successfully");
252 tracing::info!("Device: {:?}", renderer.context().info());
253
254 Box::new(RendererApp {
255 context: graphics_ctx,
256 renderer,
257 window,
258 window_id,
259 pipeline,
260 bind_group,
261 vertex_buffer,
262 offscreen_fb,
263 blit_pipeline,
264 blit_bind_group,
265 time: 0.0,
266 })
267 });
268}Sourcepub fn wgpu_features(&self) -> Features
pub fn wgpu_features(&self) -> Features
Get raw wgpu device features
Sourcepub fn gpu_features(&self) -> GpuFeatures
pub fn gpu_features(&self) -> GpuFeatures
Get the enabled GPU features (high-level wrapper).
Sourcepub fn has_feature(&self, feature: GpuFeatures) -> bool
pub fn has_feature(&self, feature: GpuFeatures) -> bool
Check if a specific GPU feature is enabled.
Sourcepub fn has_all_features(&self, features: GpuFeatures) -> bool
pub fn has_all_features(&self, features: GpuFeatures) -> bool
Check if all specified GPU features are enabled.
Sourcepub fn require_feature(&self, feature: GpuFeatures)
pub fn require_feature(&self, feature: GpuFeatures)
Assert that a feature is available, panicking with a clear message if not.
Use this before operations that require specific features.
Auto Trait Implementations§
impl Freeze for GraphicsContext
impl !RefUnwindSafe for GraphicsContext
impl Send for GraphicsContext
impl Sync for GraphicsContext
impl Unpin for GraphicsContext
impl !UnwindSafe for GraphicsContext
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more
Source§impl<T> Downcast for Twhere
T: Any,
impl<T> Downcast for Twhere
T: Any,
Source§fn into_any(self: Box<T>) -> Box<dyn Any>
fn into_any(self: Box<T>) -> Box<dyn Any>
Convert
Box<dyn Trait> (where Trait: Downcast) to Box<dyn Any>. Box<dyn Any> can
then be further downcast into Box<ConcreteType> where ConcreteType implements Trait.Source§fn into_any_rc(self: Rc<T>) -> Rc<dyn Any>
fn into_any_rc(self: Rc<T>) -> Rc<dyn Any>
Convert
Rc<Trait> (where Trait: Downcast) to Rc<Any>. Rc<Any> can then be
further downcast into Rc<ConcreteType> where ConcreteType implements Trait.Source§fn as_any(&self) -> &(dyn Any + 'static)
fn as_any(&self) -> &(dyn Any + 'static)
Convert
&Trait (where Trait: Downcast) to &Any. This is needed since Rust cannot
generate &Any’s vtable from &Trait’s.Source§fn as_any_mut(&mut self) -> &mut (dyn Any + 'static)
fn as_any_mut(&mut self) -> &mut (dyn Any + 'static)
Convert
&mut Trait (where Trait: Downcast) to &Any. This is needed since Rust cannot
generate &mut Any’s vtable from &mut Trait’s.Source§impl<T> DowncastSync for T
impl<T> DowncastSync for T
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
Source§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
Converts
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
Converts
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read more