Seregon/StratoSDK

StratoSDK is a framework with a declarative approach similar to Flutter/React, written and designed entirely for Rust.

Rust/27.3 KB/No license
crates/strato-ui-renderer/src/rendering/wgpu/resources/quad.rs
1use wgpu::{
2 util::{BufferInitDescriptor, DeviceExt},
3 Buffer, RenderPass,
4};
5 
6use crate::rendering::wgpu::shader_types;
7 
8/// The vertex buffer slot used for quad vertex data.
9const VERTEX_BUFFER_SLOT: u32 = 0;
10 
11/// Ordered list of indices in the [`VERTICES`] array to be used as part of an index buffer.
12pub(in crate::rendering::wgpu) const INDICES: &[u16] = &[0, 1, 2, 2, 3, 1];
13 
14/// List of vertex positions in normalized device coordinates (NDC) that are used when rendering.
15/// Similar to our metal renderer, we hardcode a list of vertices for each rect we render, and then
16/// determine the actual position of the rect in NDC within the vertex shader.
17const VERTICES: &[shader_types::Vertex] = &[
18 shader_types::Vertex {
19 position: shader_types::vec2f(0.0, 0.0),
20 },
21 shader_types::Vertex {
22 position: shader_types::vec2f(1.0, 0.0),
23 },
24 shader_types::Vertex {
25 position: shader_types::vec2f(0.0, 1.0),
26 },
27 shader_types::Vertex {
28 position: shader_types::vec2f(1.0, 1.0),
29 },
30];
31 
32pub(super) struct Resources {
33 index_buffer: Buffer,
34 vertex_buffer: Buffer,
35}
36 
37impl Resources {
38 pub fn new(device: &wgpu::Device) -> Self {
39 let index_buffer = device.create_buffer_init(&BufferInitDescriptor {
40 label: Some("Quad Index Buffer"),
41 contents: bytemuck::cast_slice(INDICES),
42 usage: wgpu::BufferUsages::INDEX,
43 });
44 
45 let vertex_buffer = device.create_buffer_init(&BufferInitDescriptor {
46 label: Some("Quad Vertex Buffer"),
47 contents: bytemuck::cast_slice(VERTICES),
48 usage: wgpu::BufferUsages::VERTEX,
49 });
50 
51 Self {
52 index_buffer,
53 vertex_buffer,
54 }
55 }
56 
57 pub fn configure_render_pass<'a>(&'a self, render_pass: &mut RenderPass<'a>) {
58 render_pass.set_vertex_buffer(VERTEX_BUFFER_SLOT, self.vertex_buffer.slice(..));
59 render_pass.set_index_buffer(self.index_buffer.slice(..), wgpu::IndexFormat::Uint16);
60 }
61}
62