use crate::context::GpuContext;
use crate::resource::GpuTextureView;
use crate::types::*;
use std::sync::atomic::{AtomicU64, Ordering};
#[allow(dead_code)]
static NEXT_SURFACE_ID: AtomicU64 = AtomicU64::new(1);
pub struct GpuSurface {
inner: wgpu::Surface<'static>,
id: u64,
config: wgpu::SurfaceConfiguration,
}
impl GpuSurface {
#[allow(dead_code)]
pub(crate) fn from_raw(
inner: wgpu::Surface<'static>,
config: wgpu::SurfaceConfiguration,
) -> Self {
Self {
inner,
id: NEXT_SURFACE_ID.fetch_add(1, Ordering::Relaxed),
config,
}
}
pub fn id(&self) -> u64 {
self.id
}
pub fn format(&self) -> TextureFormat {
self.config.format
}
pub fn present_mode(&self) -> PresentMode {
self.config.present_mode
}
pub fn width(&self) -> u32 {
self.config.width
}
pub fn height(&self) -> u32 {
self.config.height
}
pub fn configure(&mut self, gpu: &GpuContext) {
self.inner.configure(gpu.device(), &self.config);
}
pub fn resize(&mut self, gpu: &GpuContext, width: u32, height: u32) {
if width > 0 && height > 0 {
self.config.width = width;
self.config.height = height;
self.configure(gpu);
}
}
pub fn set_present_mode(&mut self, gpu: &GpuContext, mode: PresentMode) {
self.config.present_mode = mode;
self.configure(gpu);
}
pub fn get_current_texture(&self) -> Result<GpuSurfaceTexture, SurfaceError> {
let frame = self.inner.get_current_texture()?;
Ok(GpuSurfaceTexture { inner: frame })
}
pub fn raw(&self) -> &wgpu::Surface<'static> {
&self.inner
}
pub fn config(&self) -> &wgpu::SurfaceConfiguration {
&self.config
}
pub fn get_capabilities(&self, adapter: &wgpu::Adapter) -> wgpu::SurfaceCapabilities {
self.inner.get_capabilities(adapter)
}
}
pub struct GpuSurfaceTexture {
inner: wgpu::SurfaceTexture,
}
impl GpuSurfaceTexture {
pub fn create_view(&self, format: TextureFormat) -> GpuTextureView {
let view = self
.inner
.texture
.create_view(&wgpu::TextureViewDescriptor::default());
GpuTextureView::from_surface(view, format)
}
pub fn present(self) {
self.inner.present();
}
pub fn raw(&self) -> &wgpu::SurfaceTexture {
&self.inner
}
}