-
-
Notifications
You must be signed in to change notification settings - Fork 54
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Introduce explicit
Viewport
sharing (#96)
- Loading branch information
Showing
5 changed files
with
84 additions
and
53 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
use crate::{Cache, Params, Resolution}; | ||
|
||
use wgpu::{BindGroup, Buffer, BufferDescriptor, BufferUsages, Device, Queue}; | ||
|
||
use std::mem; | ||
use std::slice; | ||
|
||
#[derive(Debug)] | ||
pub struct Viewport { | ||
params: Params, | ||
params_buffer: Buffer, | ||
pub(crate) bind_group: BindGroup, | ||
} | ||
|
||
impl Viewport { | ||
pub fn new(device: &Device, cache: &Cache) -> Self { | ||
let params = Params { | ||
screen_resolution: Resolution { | ||
width: 0, | ||
height: 0, | ||
}, | ||
_pad: [0, 0], | ||
}; | ||
|
||
let params_buffer = device.create_buffer(&BufferDescriptor { | ||
label: Some("glyphon params"), | ||
size: mem::size_of::<Params>() as u64, | ||
usage: BufferUsages::UNIFORM | BufferUsages::COPY_DST, | ||
mapped_at_creation: false, | ||
}); | ||
|
||
let bind_group = cache.create_uniforms_bind_group(device, ¶ms_buffer); | ||
|
||
Self { | ||
params, | ||
params_buffer, | ||
bind_group, | ||
} | ||
} | ||
|
||
pub fn update(&mut self, queue: &Queue, resolution: Resolution) { | ||
if self.params.screen_resolution != resolution { | ||
self.params.screen_resolution = resolution; | ||
|
||
queue.write_buffer(&self.params_buffer, 0, unsafe { | ||
slice::from_raw_parts( | ||
&self.params as *const Params as *const u8, | ||
mem::size_of::<Params>(), | ||
) | ||
}); | ||
} | ||
} | ||
|
||
pub fn resolution(&self) -> Resolution { | ||
self.params.screen_resolution | ||
} | ||
} |