StratoSDK is a framework with a declarative approach similar to Flutter/React, written and designed entirely for Rust.
| 1 | use std::mem; |
| 2 | |
| 3 | use pathfinder_geometry::vector::Vector2F; |
| 4 | use wgpu::{BindGroup, BindGroupLayout, Buffer}; |
| 5 | |
| 6 | use crate::rendering::wgpu::{shader_types, Resources}; |
| 7 | |
| 8 | pub(super) struct Uniforms { |
| 9 | bind_group_layout: BindGroupLayout, |
| 10 | bind_group: BindGroup, |
| 11 | buffer: Buffer, |
| 12 | } |
| 13 | |
| 14 | impl Uniforms { |
| 15 | pub fn new(device: &wgpu::Device) -> Self { |
| 16 | let bind_group_layout = device.create_bind_group_layout(&wgpu::BindGroupLayoutDescriptor { |
| 17 | label: Some("Quad Uniforms Bind Group Layout"), |
| 18 | entries: &[wgpu::BindGroupLayoutEntry { |
| 19 | binding: 0, |
| 20 | visibility: wgpu::ShaderStages::VERTEX, |
| 21 | ty: wgpu::BindingType::Buffer { |
| 22 | ty: wgpu::BufferBindingType::Uniform, |
| 23 | has_dynamic_offset: false, |
| 24 | min_binding_size: wgpu::BufferSize::new( |
| 25 | mem::size_of::<shader_types::Uniforms>() as wgpu::BufferAddress, |
| 26 | ), |
| 27 | }, |
| 28 | count: None, |
| 29 | }], |
| 30 | }); |
| 31 | |
| 32 | let buffer = device.create_buffer(&wgpu::BufferDescriptor { |
| 33 | label: Some("Uniforms buffer"), |
| 34 | size: mem::size_of::<shader_types::Uniforms>() as wgpu::BufferAddress, |
| 35 | usage: wgpu::BufferUsages::UNIFORM | wgpu::BufferUsages::COPY_DST, |
| 36 | mapped_at_creation: false, |
| 37 | }); |
| 38 | |
| 39 | let bind_group = device.create_bind_group(&wgpu::BindGroupDescriptor { |
| 40 | label: Some("Uniforms Bind Group"), |
| 41 | layout: &bind_group_layout, |
| 42 | entries: &[wgpu::BindGroupEntry { |
| 43 | binding: 0, |
| 44 | resource: buffer.as_entire_binding(), |
| 45 | }], |
| 46 | }); |
| 47 | |
| 48 | Self { |
| 49 | bind_group_layout, |
| 50 | bind_group, |
| 51 | buffer, |
| 52 | } |
| 53 | } |
| 54 | |
| 55 | pub fn bind_group_layout(&self) -> &BindGroupLayout { |
| 56 | &self.bind_group_layout |
| 57 | } |
| 58 | |
| 59 | pub fn configure_render_pass<'a>( |
| 60 | &'a self, |
| 61 | render_pass: &mut wgpu::RenderPass<'a>, |
| 62 | drawable_size: Vector2F, |
| 63 | resources: &Resources, |
| 64 | ) { |
| 65 | let uniforms = shader_types::Uniforms::new(drawable_size); |
| 66 | resources |
| 67 | .queue |
| 68 | .write_buffer(&self.buffer, 0, bytemuck::cast_slice(&[uniforms])); |
| 69 | render_pass.set_bind_group(0, &self.bind_group, &[]); |
| 70 | } |
| 71 | } |
| 72 |