Update wgpu to 0.15 (#332)

* Update wgpu to 0.15
* Fix the panic described in #330
* Specify `min_binding_size` since we know what it is.
This commit is contained in:
Jay Oster 2023-01-27 21:49:40 -08:00 committed by GitHub
parent bf296a455e
commit 332a02f12d
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
10 changed files with 499 additions and 202 deletions

580
Cargo.lock generated

File diff suppressed because it is too large Load diff

View file

@ -24,10 +24,10 @@ bytemuck = "1.12"
raw-window-handle = "0.5" raw-window-handle = "0.5"
thiserror = "1.0" thiserror = "1.0"
ultraviolet = "0.9" ultraviolet = "0.9"
wgpu = "0.14" wgpu = "0.15"
[target.'cfg(target_arch = "wasm32")'.dependencies] [target.'cfg(target_arch = "wasm32")'.dependencies]
wgpu = { version = "0.14", features = ["webgl"] } wgpu = { version = "0.15", features = ["webgl"] }
[target.'cfg(not(target_arch = "wasm32"))'.dependencies] [target.'cfg(not(target_arch = "wasm32"))'.dependencies]
pollster = "0.2" pollster = "0.2"

View file

@ -24,13 +24,13 @@ struct Locals {
} }
@group(0) @binding(2) var<uniform> r_locals: Locals; @group(0) @binding(2) var<uniform> r_locals: Locals;
let tau: f32 = 6.283185307179586476925286766559; const tau: f32 = 6.283185307179586476925286766559;
let bias: f32 = 0.2376; // Offset the circular time input so it is never 0 const bias: f32 = 0.2376; // Offset the circular time input so it is never 0
// Random functions based on https://thebookofshaders.com/10/ // Random functions based on https://thebookofshaders.com/10/
let random_scale: f32 = 43758.5453123; const random_scale: f32 = 43758.5453123;
let random_x: f32 = 12.9898; const random_x: f32 = 12.9898;
let random_y: f32 = 78.233; const random_y: f32 = 78.233;
fn random(x: f32) -> f32 { fn random(x: f32) -> f32 {
return fract(sin(x) * random_scale); return fract(sin(x) * random_scale);

View file

@ -101,7 +101,7 @@ impl NoiseRenderer {
ty: wgpu::BindingType::Buffer { ty: wgpu::BindingType::Buffer {
ty: wgpu::BufferBindingType::Uniform, ty: wgpu::BufferBindingType::Uniform,
has_dynamic_offset: false, has_dynamic_offset: false,
min_binding_size: None, min_binding_size: wgpu::BufferSize::new(std::mem::size_of::<f32>() as u64),
}, },
count: None, count: None,
}, },
@ -229,6 +229,10 @@ fn create_texture_view(
dimension: wgpu::TextureDimension::D2, dimension: wgpu::TextureDimension::D2,
format: pixels.render_texture_format(), format: pixels.render_texture_format(),
usage: wgpu::TextureUsages::TEXTURE_BINDING | wgpu::TextureUsages::RENDER_ATTACHMENT, usage: wgpu::TextureUsages::TEXTURE_BINDING | wgpu::TextureUsages::RENDER_ATTACHMENT,
view_formats: &[
pixels.render_texture_format().add_srgb_suffix(),
pixels.render_texture_format().remove_srgb_suffix(),
],
}; };
Ok(device Ok(device

View file

@ -11,10 +11,14 @@ default = ["optimize"]
[dependencies] [dependencies]
env_logger = "0.10" env_logger = "0.10"
imgui = "0.9" imgui = "0.10"
imgui-wgpu = "0.21" # imgui-wgpu = "0.21"
imgui-winit-support = "0.9" imgui-winit-support = "0.10"
log = "0.4" log = "0.4"
pixels = { path = "../.." } pixels = { path = "../.." }
winit = "0.27" winit = "0.27"
winit_input_helper = "0.13" winit_input_helper = "0.13"
[dependencies.imgui-wgpu]
git = "https://github.com/Yatekii/imgui-wgpu-rs.git"
rev = "1355ebf8181bc51aea4dbd2009ea124f5da90542"

View file

@ -10,11 +10,23 @@ optimize = ["log/release_max_level_warn"]
default = ["optimize"] default = ["optimize"]
[dependencies] [dependencies]
egui = "0.20" # egui = "0.20"
egui-wgpu = "0.20" # egui-wgpu = "0.20"
egui-winit = { version = "0.20", default-features = false, features = ["links"] } # egui-winit = { version = "0.20", default-features = false, features = ["links"] }
env_logger = "0.10" env_logger = "0.10"
log = "0.4" log = "0.4"
pixels = { path = "../.." } pixels = { path = "../.." }
winit = "0.27" winit = "0.27"
winit_input_helper = "0.13" winit_input_helper = "0.13"
[dependencies.egui]
git = "https://github.com/emilk/egui.git"
rev = "f222ee044edf8beebfaf5dd7be15c9f318f20886"
[dependencies.egui-wgpu]
git = "https://github.com/emilk/egui.git"
rev = "f222ee044edf8beebfaf5dd7be15c9f318f20886"
[dependencies.egui-winit]
git = "https://github.com/emilk/egui.git"
rev = "f222ee044edf8beebfaf5dd7be15c9f318f20886"

View file

@ -13,4 +13,4 @@ default = ["optimize"]
env_logger = "0.10" env_logger = "0.10"
log = "0.4" log = "0.4"
pixels = { path = "../.." } pixels = { path = "../.." }
tao = "0.15" tao = "0.17"

View file

@ -254,10 +254,13 @@ impl<'req, 'dev, 'win, W: HasRawWindowHandle + HasRawDisplayHandle>
/// ///
/// Returns an error when a [`wgpu::Adapter`] cannot be found. /// Returns an error when a [`wgpu::Adapter`] cannot be found.
async fn build_impl(self) -> Result<Pixels, Error> { async fn build_impl(self) -> Result<Pixels, Error> {
let instance = wgpu::Instance::new(self.backend); let instance = wgpu::Instance::new(wgpu::InstanceDescriptor {
backends: self.backend,
..Default::default()
});
// TODO: Use `options.pixel_aspect_ratio` to stretch the scaled texture // TODO: Use `options.pixel_aspect_ratio` to stretch the scaled texture
let surface = unsafe { instance.create_surface(self.surface_texture.window) }; let surface = unsafe { instance.create_surface(self.surface_texture.window) }?;
let compatible_surface = Some(&surface); let compatible_surface = Some(&surface);
let request_adapter_options = &self.request_adapter_options; let request_adapter_options = &self.request_adapter_options;
let adapter = match wgpu::util::initialize_adapter_from_env(&instance, self.backend) { let adapter = match wgpu::util::initialize_adapter_from_env(&instance, self.backend) {
@ -290,16 +293,15 @@ impl<'req, 'dev, 'win, W: HasRawWindowHandle + HasRawDisplayHandle>
..wgpu::DeviceDescriptor::default() ..wgpu::DeviceDescriptor::default()
}); });
let (device, queue) = adapter let (device, queue) = adapter.request_device(&device_descriptor, None).await?;
.request_device(&device_descriptor, None)
.await
.map_err(Error::DeviceNotFound)?;
let surface_capabilities = surface.get_capabilities(&adapter);
let present_mode = self.present_mode; let present_mode = self.present_mode;
let surface_texture_format = self.surface_texture_format.unwrap_or_else(|| { let surface_texture_format = self.surface_texture_format.unwrap_or_else(|| {
*surface *surface_capabilities
.get_supported_formats(&adapter) .formats
.first() .iter()
.find(|format| texture_format_is_srgb(**format))
.unwrap_or(&wgpu::TextureFormat::Bgra8UnormSrgb) .unwrap_or(&wgpu::TextureFormat::Bgra8UnormSrgb)
}); });
let render_texture_format = self.render_texture_format.unwrap_or(surface_texture_format); let render_texture_format = self.render_texture_format.unwrap_or(surface_texture_format);
@ -318,6 +320,7 @@ impl<'req, 'dev, 'win, W: HasRawWindowHandle + HasRawDisplayHandle>
// Render texture values // Render texture values
&surface_size, &surface_size,
render_texture_format, render_texture_format,
// Clear color and blending values
clear_color, clear_color,
blend_state, blend_state,
)?; )?;
@ -326,7 +329,7 @@ impl<'req, 'dev, 'win, W: HasRawWindowHandle + HasRawDisplayHandle>
let mut pixels = Vec::with_capacity(pixels_buffer_size); let mut pixels = Vec::with_capacity(pixels_buffer_size);
pixels.resize_with(pixels_buffer_size, Default::default); pixels.resize_with(pixels_buffer_size, Default::default);
let alpha_mode = surface.get_supported_alpha_modes(&adapter)[0]; let alpha_mode = surface_capabilities.alpha_modes[0];
// Instantiate the Pixels struct // Instantiate the Pixels struct
let context = PixelsContext { let context = PixelsContext {
@ -461,6 +464,10 @@ pub(crate) fn create_backing_texture(
dimension: wgpu::TextureDimension::D2, dimension: wgpu::TextureDimension::D2,
format: backing_texture_format, format: backing_texture_format,
usage: wgpu::TextureUsages::TEXTURE_BINDING | wgpu::TextureUsages::COPY_DST, usage: wgpu::TextureUsages::TEXTURE_BINDING | wgpu::TextureUsages::COPY_DST,
view_formats: &[
backing_texture_format.add_srgb_suffix(),
backing_texture_format.remove_srgb_suffix(),
],
}); });
let texture_view = texture.create_view(&wgpu::TextureViewDescriptor::default()); let texture_view = texture.create_view(&wgpu::TextureViewDescriptor::default());
@ -486,6 +493,24 @@ pub(crate) fn create_backing_texture(
)) ))
} }
#[inline]
const fn texture_format_is_srgb(texture_format: wgpu::TextureFormat) -> bool {
use wgpu::TextureFormat::*;
matches!(
texture_format,
Rgba8UnormSrgb
| Bgra8UnormSrgb
| Bc1RgbaUnormSrgb
| Etc2Rgb8UnormSrgb
| Etc2Rgb8A1UnormSrgb
| Bc2RgbaUnormSrgb
| Bc3RgbaUnormSrgb
| Bc7RgbaUnormSrgb
| Etc2Rgba8UnormSrgb
)
}
#[rustfmt::skip] #[rustfmt::skip]
#[inline] #[inline]
const fn get_texture_format_size(texture_format: wgpu::TextureFormat) -> f32 { const fn get_texture_format_size(texture_format: wgpu::TextureFormat) -> f32 {
@ -507,7 +532,8 @@ const fn get_texture_format_size(texture_format: wgpu::TextureFormat) -> f32 {
R8Unorm R8Unorm
| R8Snorm | R8Snorm
| R8Uint | R8Uint
| R8Sint => 1.0, // 8.0 / 8.0 | R8Sint
| Stencil8 => 1.0, // 8.0 / 8.0
// 16-bit formats, 8 bits per component // 16-bit formats, 8 bits per component
R16Uint R16Uint

View file

@ -118,10 +118,13 @@ pub enum Error {
AdapterNotFound, AdapterNotFound,
/// Equivalent to [`wgpu::RequestDeviceError`] /// Equivalent to [`wgpu::RequestDeviceError`]
#[error("No wgpu::Device found.")] #[error("No wgpu::Device found.")]
DeviceNotFound(wgpu::RequestDeviceError), DeviceNotFound(#[from] wgpu::RequestDeviceError),
/// Equivalent to [`wgpu::SurfaceError`] /// Equivalent to [`wgpu::SurfaceError`]
#[error("The GPU failed to acquire a surface frame.")] #[error("The GPU failed to acquire a surface frame.")]
Surface(wgpu::SurfaceError), Surface(#[from] wgpu::SurfaceError),
/// Equivalent to [`wgpu::CreateSurfaceError`]
#[error("Unable to create a surface.")]
CreateSurface(#[from] wgpu::CreateSurfaceError),
/// Equivalent to [`TextureError`] /// Equivalent to [`TextureError`]
#[error("Texture creation failed: {0}")] #[error("Texture creation failed: {0}")]
InvalidTexture(#[from] TextureError), InvalidTexture(#[from] TextureError),
@ -457,8 +460,7 @@ impl Pixels {
self.context.surface.get_current_texture() self.context.surface.get_current_texture()
} }
err => Err(err), err => Err(err),
}) })?;
.map_err(Error::Surface)?;
let mut encoder = let mut encoder =
self.context self.context
.device .device
@ -510,6 +512,10 @@ impl Pixels {
height: self.surface_size.height, height: self.surface_size.height,
present_mode: self.present_mode, present_mode: self.present_mode,
alpha_mode: self.alpha_mode, alpha_mode: self.alpha_mode,
view_formats: vec![
self.surface_texture_format.add_srgb_suffix(),
self.surface_texture_format.remove_srgb_suffix(),
],
}, },
); );
} }

View file

@ -106,7 +106,7 @@ impl ScalingRenderer {
ty: wgpu::BindingType::Buffer { ty: wgpu::BindingType::Buffer {
ty: wgpu::BufferBindingType::Uniform, ty: wgpu::BufferBindingType::Uniform,
has_dynamic_offset: false, has_dynamic_offset: false,
min_binding_size: None, // TODO: More efficient to specify this min_binding_size: wgpu::BufferSize::new(transform_bytes.len() as u64),
}, },
count: None, count: None,
}, },
@ -230,10 +230,11 @@ impl ScalingMatrix {
let (texture_width, texture_height) = texture_size; let (texture_width, texture_height) = texture_size;
let (screen_width, screen_height) = screen_size; let (screen_width, screen_height) = screen_size;
let width_ratio = (screen_width / texture_width).max(1.0);
let height_ratio = (screen_height / texture_height).max(1.0);
// Get smallest scale size // Get smallest scale size
let scale = (screen_width / texture_width) let scale = width_ratio.clamp(1.0, height_ratio).floor();
.clamp(1.0, screen_height / texture_height)
.floor();
let scaled_width = texture_width * scale; let scaled_width = texture_width * scale;
let scaled_height = texture_height * scale; let scaled_height = texture_height * scale;