Cleanup ffi refs in texture.rs

This commit is contained in:
Luke Street 2022-02-09 00:21:41 -05:00
parent d84f71f9dc
commit b13ff392ef
1 changed files with 25 additions and 28 deletions

View File

@ -9,10 +9,7 @@ use wgpu::{util::DeviceExt, ImageDataLayout};
use crate::{ use crate::{
get_app, get_app,
shaders::{ shaders::{cxxbridge::ffi, STATE},
cxxbridge::ffi::{TextureFormat, TextureRef},
STATE,
},
}; };
pub(crate) struct TextureWithView { pub(crate) struct TextureWithView {
@ -37,20 +34,20 @@ pub(crate) fn create_static_texture_2d(
width: u32, width: u32,
height: u32, height: u32,
mips: u32, mips: u32,
format: TextureFormat, format: ffi::TextureFormat,
data: &[u8], data: &[u8],
label: &str, label: &str,
) -> TextureRef { ) -> ffi::TextureRef {
let gpu = &get_app().gpu; let gpu = &get_app().gpu;
let extent = wgpu::Extent3d { width, height, depth_or_array_layers: 1 }; let extent = wgpu::Extent3d { width, height, depth_or_array_layers: 1 };
let wgpu_format = match format { let wgpu_format = match format {
TextureFormat::RGBA8 => wgpu::TextureFormat::Rgba8Unorm, ffi::TextureFormat::RGBA8 => wgpu::TextureFormat::Rgba8Unorm,
TextureFormat::R8 => wgpu::TextureFormat::R8Unorm, ffi::TextureFormat::R8 => wgpu::TextureFormat::R8Unorm,
TextureFormat::R32Float => wgpu::TextureFormat::R32Float, ffi::TextureFormat::R32Float => wgpu::TextureFormat::R32Float,
TextureFormat::DXT1 => wgpu::TextureFormat::Bc1RgbaUnorm, ffi::TextureFormat::DXT1 => wgpu::TextureFormat::Bc1RgbaUnorm,
TextureFormat::DXT3 => wgpu::TextureFormat::Bc3RgbaUnorm, ffi::TextureFormat::DXT3 => wgpu::TextureFormat::Bc3RgbaUnorm,
TextureFormat::DXT5 => wgpu::TextureFormat::Bc5RgUnorm, ffi::TextureFormat::DXT5 => wgpu::TextureFormat::Bc5RgUnorm,
TextureFormat::BPTC => wgpu::TextureFormat::Bc7RgbaUnorm, ffi::TextureFormat::BPTC => wgpu::TextureFormat::Bc7RgbaUnorm,
_ => todo!(), _ => todo!(),
}; };
let texture = gpu.device.create_texture_with_data( let texture = gpu.device.create_texture_with_data(
@ -84,26 +81,26 @@ pub(crate) fn create_static_texture_2d(
state.textures.insert(id, TextureWithView::new(texture, wgpu_format, extent)); state.textures.insert(id, TextureWithView::new(texture, wgpu_format, extent));
} }
} }
TextureRef { id, render: false } ffi::TextureRef { id, render: false }
} }
pub(crate) fn create_dynamic_texture_2d( pub(crate) fn create_dynamic_texture_2d(
width: u32, width: u32,
height: u32, height: u32,
mips: u32, mips: u32,
format: TextureFormat, format: ffi::TextureFormat,
label: &str, label: &str,
) -> TextureRef { ) -> ffi::TextureRef {
let gpu = &get_app().gpu; let gpu = &get_app().gpu;
let extent = wgpu::Extent3d { width, height, depth_or_array_layers: 1 }; let extent = wgpu::Extent3d { width, height, depth_or_array_layers: 1 };
let wgpu_format = match format { let wgpu_format = match format {
TextureFormat::RGBA8 => wgpu::TextureFormat::Rgba8Unorm, ffi::TextureFormat::RGBA8 => wgpu::TextureFormat::Rgba8Unorm,
TextureFormat::R8 => wgpu::TextureFormat::R8Unorm, ffi::TextureFormat::R8 => wgpu::TextureFormat::R8Unorm,
TextureFormat::R32Float => wgpu::TextureFormat::R32Float, ffi::TextureFormat::R32Float => wgpu::TextureFormat::R32Float,
TextureFormat::DXT1 => wgpu::TextureFormat::Bc1RgbaUnorm, ffi::TextureFormat::DXT1 => wgpu::TextureFormat::Bc1RgbaUnorm,
TextureFormat::DXT3 => wgpu::TextureFormat::Bc3RgbaUnorm, ffi::TextureFormat::DXT3 => wgpu::TextureFormat::Bc3RgbaUnorm,
TextureFormat::DXT5 => wgpu::TextureFormat::Bc5RgUnorm, ffi::TextureFormat::DXT5 => wgpu::TextureFormat::Bc5RgUnorm,
TextureFormat::BPTC => wgpu::TextureFormat::Bc7RgbaUnorm, ffi::TextureFormat::BPTC => wgpu::TextureFormat::Bc7RgbaUnorm,
_ => todo!(), _ => todo!(),
}; };
let texture = gpu.device.create_texture(&wgpu::TextureDescriptor { let texture = gpu.device.create_texture(&wgpu::TextureDescriptor {
@ -132,7 +129,7 @@ pub(crate) fn create_dynamic_texture_2d(
state.textures.insert(id, TextureWithView::new(texture, wgpu_format, extent)); state.textures.insert(id, TextureWithView::new(texture, wgpu_format, extent));
} }
} }
TextureRef { id, render: false } ffi::TextureRef { id, render: false }
} }
pub(crate) fn create_render_texture( pub(crate) fn create_render_texture(
@ -142,7 +139,7 @@ pub(crate) fn create_render_texture(
color_bind_count: u32, color_bind_count: u32,
depth_bind_count: u32, depth_bind_count: u32,
label: &str, label: &str,
) -> TextureRef { ) -> ffi::TextureRef {
let gpu = &get_app().gpu; let gpu = &get_app().gpu;
let color_texture = if color_bind_count > 0 { let color_texture = if color_bind_count > 0 {
let extent = wgpu::Extent3d { width, height, depth_or_array_layers: color_bind_count }; let extent = wgpu::Extent3d { width, height, depth_or_array_layers: color_bind_count };
@ -222,10 +219,10 @@ pub(crate) fn create_render_texture(
state.render_textures.insert(id, RenderTexture { color_texture, depth_texture }); state.render_textures.insert(id, RenderTexture { color_texture, depth_texture });
} }
} }
TextureRef { id, render: true } ffi::TextureRef { id, render: true }
} }
pub(crate) fn write_texture(handle: TextureRef, data: &[u8]) { pub(crate) fn write_texture(handle: ffi::TextureRef, data: &[u8]) {
if handle.render { if handle.render {
panic!("Can't write to render texture"); panic!("Can't write to render texture");
} }
@ -256,7 +253,7 @@ pub(crate) fn write_texture(handle: TextureRef, data: &[u8]) {
} }
} }
pub(crate) fn drop_texture(handle: TextureRef) { pub(crate) fn drop_texture(handle: ffi::TextureRef) {
let state = unsafe { STATE.as_mut().unwrap_unchecked() }; let state = unsafe { STATE.as_mut().unwrap_unchecked() };
if handle.render { if handle.render {
state.render_textures.remove(&handle.id).expect("Render texture already dropped"); state.render_textures.remove(&handle.id).expect("Render texture already dropped");