summaryrefslogtreecommitdiffstats
path: root/wgpu/src/buffer
diff options
context:
space:
mode:
authorLibravatar Héctor Ramón Jiménez <hector0193@gmail.com>2022-11-03 05:00:35 +0100
committerLibravatar Héctor Ramón Jiménez <hector0193@gmail.com>2022-11-03 05:00:35 +0100
commit99cf98971dae22ae65adb2104c5a3eec578649f1 (patch)
tree63dacbee235d79c5895fda9072a0f6b683129131 /wgpu/src/buffer
parent7e22e2d45293c5916812be03dc7367134b69b3ad (diff)
downloadiced-99cf98971dae22ae65adb2104c5a3eec578649f1.tar.gz
iced-99cf98971dae22ae65adb2104c5a3eec578649f1.tar.bz2
iced-99cf98971dae22ae65adb2104c5a3eec578649f1.zip
Rename `buffers` module to `buffer`
... and move `StaticBuffer` to nested `static` module
Diffstat (limited to '')
-rw-r--r--wgpu/src/buffer.rs3
-rw-r--r--wgpu/src/buffer/dynamic.rs (renamed from wgpu/src/buffers/dynamic.rs)106
-rw-r--r--wgpu/src/buffer/static.rs (renamed from wgpu/src/buffers.rs)7
3 files changed, 58 insertions, 58 deletions
diff --git a/wgpu/src/buffer.rs b/wgpu/src/buffer.rs
new file mode 100644
index 00000000..7c092d0b
--- /dev/null
+++ b/wgpu/src/buffer.rs
@@ -0,0 +1,3 @@
+//! Utilities for buffer operations.
+pub mod dynamic;
+pub mod r#static;
diff --git a/wgpu/src/buffers/dynamic.rs b/wgpu/src/buffer/dynamic.rs
index f1262d83..c0c48c74 100644
--- a/wgpu/src/buffers/dynamic.rs
+++ b/wgpu/src/buffer/dynamic.rs
@@ -3,57 +3,10 @@ use encase::private::WriteInto;
use encase::ShaderType;
use std::marker::PhantomData;
-// Currently supported dynamic buffers.
-enum BufferType {
- Uniform(encase::DynamicUniformBuffer<Vec<u8>>),
- Storage(encase::DynamicStorageBuffer<Vec<u8>>),
-}
-
-impl BufferType {
- /// Writes the current value to its CPU buffer with proper alignment.
- pub(super) fn write<T: ShaderType + WriteInto>(
- &mut self,
- value: &T,
- ) -> wgpu::DynamicOffset {
- match self {
- BufferType::Uniform(buf) => buf
- .write(value)
- .expect("Error when writing to dynamic uniform buffer.")
- as u32,
- BufferType::Storage(buf) => buf
- .write(value)
- .expect("Error when writing to dynamic storage buffer.")
- as u32,
- }
- }
-
- /// Returns bytearray of aligned CPU buffer.
- pub(super) fn get_ref(&self) -> &Vec<u8> {
- match self {
- BufferType::Uniform(buf) => buf.as_ref(),
- BufferType::Storage(buf) => buf.as_ref(),
- }
- }
-
- /// Resets the CPU buffer.
- pub(super) fn clear(&mut self) {
- match self {
- BufferType::Uniform(buf) => {
- buf.as_mut().clear();
- buf.set_offset(0);
- }
- BufferType::Storage(buf) => {
- buf.as_mut().clear();
- buf.set_offset(0);
- }
- }
- }
-}
-
/// A dynamic buffer is any type of buffer which does not have a static offset.
pub(crate) struct Buffer<T: ShaderType> {
offsets: Vec<wgpu::DynamicOffset>,
- cpu: BufferType,
+ cpu: Internal,
gpu: wgpu::Buffer,
label: &'static str,
size: u64,
@@ -65,7 +18,7 @@ impl<T: ShaderType + WriteInto> Buffer<T> {
pub fn uniform(device: &wgpu::Device, label: &'static str) -> Self {
Buffer::new(
device,
- BufferType::Uniform(encase::DynamicUniformBuffer::new(Vec::new())),
+ Internal::Uniform(encase::DynamicUniformBuffer::new(Vec::new())),
label,
wgpu::BufferUsages::UNIFORM | wgpu::BufferUsages::COPY_DST,
)
@@ -75,7 +28,7 @@ impl<T: ShaderType + WriteInto> Buffer<T> {
pub fn storage(device: &wgpu::Device, label: &'static str) -> Self {
Buffer::new(
device,
- BufferType::Storage(encase::DynamicStorageBuffer::new(Vec::new())),
+ Internal::Storage(encase::DynamicStorageBuffer::new(Vec::new())),
label,
wgpu::BufferUsages::STORAGE | wgpu::BufferUsages::COPY_DST,
)
@@ -83,7 +36,7 @@ impl<T: ShaderType + WriteInto> Buffer<T> {
fn new(
device: &wgpu::Device,
- dynamic_buffer_type: BufferType,
+ dynamic_buffer_type: Internal,
label: &'static str,
usage: wgpu::BufferUsages,
) -> Self {
@@ -135,10 +88,10 @@ impl<T: ShaderType + WriteInto> Buffer<T> {
if self.size < new_size {
let usages = match self.cpu {
- BufferType::Uniform(_) => {
+ Internal::Uniform(_) => {
wgpu::BufferUsages::UNIFORM | wgpu::BufferUsages::COPY_DST
}
- BufferType::Storage(_) => {
+ Internal::Storage(_) => {
wgpu::BufferUsages::STORAGE | wgpu::BufferUsages::COPY_DST
}
};
@@ -197,3 +150,50 @@ impl<T: ShaderType + WriteInto> Buffer<T> {
self.cpu.clear();
}
}
+
+// Currently supported dynamic buffers.
+enum Internal {
+ Uniform(encase::DynamicUniformBuffer<Vec<u8>>),
+ Storage(encase::DynamicStorageBuffer<Vec<u8>>),
+}
+
+impl Internal {
+ /// Writes the current value to its CPU buffer with proper alignment.
+ pub(super) fn write<T: ShaderType + WriteInto>(
+ &mut self,
+ value: &T,
+ ) -> wgpu::DynamicOffset {
+ match self {
+ Internal::Uniform(buf) => buf
+ .write(value)
+ .expect("Error when writing to dynamic uniform buffer.")
+ as u32,
+ Internal::Storage(buf) => buf
+ .write(value)
+ .expect("Error when writing to dynamic storage buffer.")
+ as u32,
+ }
+ }
+
+ /// Returns bytearray of aligned CPU buffer.
+ pub(super) fn get_ref(&self) -> &Vec<u8> {
+ match self {
+ Internal::Uniform(buf) => buf.as_ref(),
+ Internal::Storage(buf) => buf.as_ref(),
+ }
+ }
+
+ /// Resets the CPU buffer.
+ pub(super) fn clear(&mut self) {
+ match self {
+ Internal::Uniform(buf) => {
+ buf.as_mut().clear();
+ buf.set_offset(0);
+ }
+ Internal::Storage(buf) => {
+ buf.as_mut().clear();
+ buf.set_offset(0);
+ }
+ }
+ }
+}
diff --git a/wgpu/src/buffers.rs b/wgpu/src/buffer/static.rs
index 707ad832..cf06790c 100644
--- a/wgpu/src/buffers.rs
+++ b/wgpu/src/buffer/static.rs
@@ -1,6 +1,3 @@
-//! Utilities for buffer operations.
-pub mod dynamic;
-
use bytemuck::{Pod, Zeroable};
use std::marker::PhantomData;
use std::mem;
@@ -11,7 +8,7 @@ const DEFAULT_STATIC_BUFFER_COUNT: wgpu::BufferAddress = 128;
/// A generic buffer struct useful for items which have no alignment requirements
/// (e.g. Vertex, Index buffers) & no dynamic offsets.
#[derive(Debug)]
-pub(crate) struct StaticBuffer<T> {
+pub(crate) struct Buffer<T> {
//stored sequentially per mesh iteration; refers to the offset index in the GPU buffer
offsets: Vec<wgpu::BufferAddress>,
label: &'static str,
@@ -21,7 +18,7 @@ pub(crate) struct StaticBuffer<T> {
_data: PhantomData<T>,
}
-impl<T: Pod + Zeroable> StaticBuffer<T> {
+impl<T: Pod + Zeroable> Buffer<T> {
/// Initialize a new static buffer.
pub fn new(
device: &wgpu::Device,