wgpu_rust_renderer/renderer/
wgpu_indices.rs1use std::collections::HashMap;
4
5use crate::{
6 geometry::index::Index,
7 resource::resource::{
8 ResourceId,
9 ResourcePools,
10 },
11};
12
13pub struct WGPUIndices {
14 indices: HashMap<ResourceId<Index>, wgpu::Buffer>,
15}
16
17impl WGPUIndices {
18 pub fn new() -> Self {
19 WGPUIndices {
20 indices: HashMap::new()
21 }
22 }
23
24 pub fn borrow(&self, index: &ResourceId<Index>) -> Option<&wgpu::Buffer> {
25 self.indices.get(index)
26 }
27
28 pub fn update(
30 &mut self,
31 device: &wgpu::Device,
32 pools: &ResourcePools,
33 index_rid: &ResourceId<Index>,
34 ) {
35 if !self.indices.contains_key(index_rid) {
36 if let Some(index) = pools.borrow::<Index>().borrow(index_rid) {
37 self.indices.insert(*index_rid, create_buffer(
38 device,
39 bytemuck::cast_slice(index.borrow_data()),
40 wgpu::BufferUsages::INDEX,
41 ));
42 }
43 }
44 }
45}
46
47fn create_buffer(device: &wgpu::Device, contents: &[u8], usage: wgpu::BufferUsages) -> wgpu::Buffer {
49 use wgpu::util::DeviceExt;
50 device.create_buffer_init(&wgpu::util::BufferInitDescriptor {
51 label: None,
52 contents: contents,
53 usage: usage,
54 })
55}