Skip to content

Commit

Permalink
Update egui to latest and wgpu to 0.16 (#1958)
Browse files Browse the repository at this point in the history
* update to wgpu 0.16 and egui using this version

* shader fixup for type aliases and rectangle shader

* shader signed/unsigned shenanigans

* more signed/unsigned issues

* fix texture component count

* fix picking layer depth readback crash on web

* patch wgpu

* better texture size estimate

* fix patches
  • Loading branch information
Wumpf authored Apr 25, 2023
1 parent f0fdcf3 commit eef4758
Show file tree
Hide file tree
Showing 29 changed files with 524 additions and 369 deletions.
209 changes: 93 additions & 116 deletions Cargo.lock

Large diffs are not rendered by default.

28 changes: 17 additions & 11 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -59,10 +59,10 @@ comfy-table = { version = "6.1", default-features = false }
ctrlc = { version = "3.0", features = ["termination"] }
ecolor = "0.21.0"
eframe = { version = "0.21.3", default-features = false }
egui = { version = "0.21.0", features = ["extra_debug_asserts", "tracing"] }
egui = { version = "0.21.0", features = ["extra_debug_asserts", "log"] }
egui-wgpu = "0.21.0"
egui_dock = "0.4"
egui_extras = { version = "0.21.0", features = ["tracing"] }
egui_extras = { version = "0.21.0", features = ["log"] }
emath = "0.21.0"
enumset = "1.0.12"
epaint = "0.21.0"
Expand All @@ -85,9 +85,8 @@ thiserror = "1.0"
time = { version = "0.3", features = ["wasm-bindgen"] }
tinyvec = { version = "1.6", features = ["alloc", "rustc_1_55"] }
tokio = "1.24"
wgpu = { version = "0.15.1", default-features = false }
wgpu-core = { version = "0.15.1", default-features = false }
wgpu-hal = { version = "0.15.4", default-features = false }
wgpu = { version = "0.16", default-features = false }
wgpu-core = { version = "0.16", default-features = false }


[profile.dev]
Expand All @@ -113,9 +112,16 @@ debug = true
# As a last resport, patch with a commit to our own repository.
# ALWAYS document what PR the commit hash is part of, or when it was merged into the upstream trunk.

# ecolor = { path = "../../egui/crates/ecolor" }
# eframe = { path = "../../egui/crates/eframe" }
# egui = { path = "../../egui/crates/egui" }
# egui-wgpu = { path = "../../egui/crates/egui-wgpu" }
# egui_extras = { path = "../../egui/crates/egui_extras" }
# emath = { path = "../../egui/crates/emath" }
# TODO(andreas/emilk): Update to a stable egui version
# wgpu 0.16 support.
ecolor = { git = "https://github.com/emilk/egui", rev = "0e6d69d" }
eframe = { git = "https://github.com/emilk/egui", rev = "0e6d69d" }
egui = { git = "https://github.com/emilk/egui", rev = "0e6d69d" }
egui-wgpu = { git = "https://github.com/emilk/egui", rev = "0e6d69d" }
egui_extras = { git = "https://github.com/emilk/egui", rev = "0e6d69d" }
emath = { git = "https://github.com/emilk/egui", rev = "0e6d69d" }

# TODO(andreas): Either work around this issue in wgpu-egui (never discard command buffers) or wait for wgpu patch release.
# Fix for command buffer dropping crash https://github.com/gfx-rs/wgpu/pull/3726
wgpu = { git = "https://github.com/rerun-io/wgpu", rev = "de497aeda152a3515bac5eb4bf1b17f1757b9dac" }
wgpu-core = { git = "https://github.com/rerun-io/wgpu", rev = "de497aeda152a3515bac5eb4bf1b17f1757b9dac" }
1 change: 0 additions & 1 deletion crates/re_renderer/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,6 @@ notify = "5.0"
puffin.workspace = true
wgpu = { workspace = true, default-features = false, features = ["wgsl"] }
wgpu-core.workspace = true
wgpu-hal.workspace = true

# wasm
[target.'cfg(target_arch = "wasm32")'.dependencies]
Expand Down
4 changes: 2 additions & 2 deletions crates/re_renderer/shader/depth_cloud.wgsl
Original file line number Diff line number Diff line change
Expand Up @@ -82,9 +82,9 @@ struct PointData {
}

// Backprojects the depth texture using the intrinsics passed in the uniform buffer.
fn compute_point_data(quad_idx: i32) -> PointData {
fn compute_point_data(quad_idx: u32) -> PointData {
let wh = textureDimensions(depth_texture);
let texcoords = IVec2(quad_idx % wh.x, quad_idx / wh.x);
let texcoords = UVec2(quad_idx % wh.x, quad_idx / wh.x);

// TODO(cmc): expose knobs to linearize/normalize/flip/cam-to-plane depth.
let world_space_depth = depth_cloud_info.world_depth_from_texture_value * textureLoad(depth_texture, texcoords, 0).x;
Expand Down
15 changes: 4 additions & 11 deletions crates/re_renderer/shader/lines.wgsl
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,8 @@ struct BatchUniformBuffer {
@group(2) @binding(0)
var<uniform> batch: BatchUniformBuffer;


// textureLoad needs i32 right now, so we use that with all sizes & indices to avoid casts
// https://github.com/gfx-rs/naga/issues/1997
const POSITION_TEXTURE_SIZE: i32 = 512;
const LINE_STRIP_TEXTURE_SIZE: i32 = 256;
const POSITION_TEXTURE_SIZE: u32 = 512u;
const LINE_STRIP_TEXTURE_SIZE: u32 = 256u;

// Flags
// See lines.rs#LineStripFlags
Expand Down Expand Up @@ -87,9 +84,7 @@ struct LineStripData {

// Read and unpack line strip data at a given location
fn read_strip_data(idx: u32) -> LineStripData {
// can be u32 once https://github.com/gfx-rs/naga/issues/1997 is solved
let idx = i32(idx);
let coord = IVec2(idx % LINE_STRIP_TEXTURE_SIZE, idx / LINE_STRIP_TEXTURE_SIZE);
let coord = UVec2(idx % LINE_STRIP_TEXTURE_SIZE, idx / LINE_STRIP_TEXTURE_SIZE);
var raw_data = textureLoad(position_data_texture, coord, 0).xy;

var data: LineStripData;
Expand All @@ -110,9 +105,7 @@ struct PositionData {

// Read and unpack position data at a given location
fn read_position_data(idx: u32) -> PositionData {
// can be u32 once https://github.com/gfx-rs/naga/issues/1997 is solved
let idx = i32(idx);
var raw_data = textureLoad(line_strip_texture, IVec2(idx % POSITION_TEXTURE_SIZE, idx / POSITION_TEXTURE_SIZE), 0);
var raw_data = textureLoad(line_strip_texture, UVec2(idx % POSITION_TEXTURE_SIZE, idx / POSITION_TEXTURE_SIZE), 0);

var data: PositionData;
let pos_4d = batch.world_from_obj * Vec4(raw_data.xyz, 1.0);
Expand Down
9 changes: 3 additions & 6 deletions crates/re_renderer/shader/point_cloud.wgsl
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,7 @@ var<uniform> batch: BatchUniformBuffer;
// Flags
// See point_cloud.rs#PointCloudBatchFlags
const ENABLE_SHADING: u32 = 1u;

// textureLoad needs i32 right now, so we use that with all sizes & indices to avoid casts
// https://github.com/gfx-rs/naga/issues/1997
var<private> TEXTURE_SIZE: i32 = 2048;
const TEXTURE_SIZE: u32 = 2048u;

struct VertexOut {
@builtin(position)
Expand Down Expand Up @@ -75,8 +72,8 @@ struct PointData {
}

// Read and unpack data at a given location
fn read_data(idx: i32) -> PointData {
let coord = IVec2(i32(idx % TEXTURE_SIZE), idx / TEXTURE_SIZE);
fn read_data(idx: u32) -> PointData {
let coord = UVec2(idx % TEXTURE_SIZE, idx / TEXTURE_SIZE);
let position_data = textureLoad(position_data_texture, coord, 0);
let color = textureLoad(color_texture, coord, 0);

Expand Down
8 changes: 4 additions & 4 deletions crates/re_renderer/shader/rectangle_fs.wgsl
Original file line number Diff line number Diff line change
Expand Up @@ -86,10 +86,10 @@ fn fs_main(in: VertexOut) -> @location(0) Vec4 {
let colormap_size = textureDimensions(colormap_texture).xy;
let color_index = normalized_value.r * f32(colormap_size.x * colormap_size.y);
// TODO(emilk): interpolate between neighboring colors for non-integral color indices
let color_index_i32 = i32(color_index);
let x = color_index_i32 % colormap_size.x;
let y = color_index_i32 / colormap_size.x;
texture_color = textureLoad(colormap_texture, IVec2(x, y), 0);
let color_index_u32 = u32(color_index);
let x = color_index_u32 % colormap_size.x;
let y = color_index_u32 / colormap_size.x;
texture_color = textureLoad(colormap_texture, UVec2(x, y), 0);
} else {
return ERROR_RGBA; // unknown color mapper
}
Expand Down
24 changes: 12 additions & 12 deletions crates/re_renderer/shader/types.wgsl
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
// Names chosen to match [`glam`](https://docs.rs/glam/latest/glam/)
type Vec2 = vec2<f32>;
type Vec3 = vec3<f32>;
type Vec4 = vec4<f32>;
type UVec2 = vec2<u32>;
type UVec3 = vec3<u32>;
type UVec4 = vec4<u32>;
type IVec2 = vec2<i32>;
type IVec3 = vec3<i32>;
type IVec4 = vec4<i32>;
type Mat3 = mat3x3<f32>;
type Mat4x3 = mat4x3<f32>;
type Mat4 = mat4x4<f32>;
alias Vec2 = vec2<f32>;
alias Vec3 = vec3<f32>;
alias Vec4 = vec4<f32>;
alias UVec2 = vec2<u32>;
alias UVec3 = vec3<u32>;
alias UVec4 = vec4<u32>;
alias IVec2 = vec2<i32>;
alias IVec3 = vec3<i32>;
alias IVec4 = vec4<i32>;
alias Mat3 = mat3x3<f32>;
alias Mat4x3 = mat4x3<f32>;
alias Mat4 = mat4x4<f32>;

// Extreme values as documented by the spec:
// https://www.w3.org/TR/WGSL/#floating-point-types
Expand Down
4 changes: 2 additions & 2 deletions crates/re_renderer/shader/utils/sphere_quad.wgsl
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,8 @@ fn sphere_quad_span_orthographic(point_pos: Vec3, point_radius: f32, top_bottom:
}

/// Returns the index of the current quad.
fn sphere_quad_index(vertex_idx: u32) -> i32 {
return i32(vertex_idx) / 6;
fn sphere_quad_index(vertex_idx: u32) -> u32 {
return vertex_idx / 6u;
}

struct SphereQuadData {
Expand Down
11 changes: 7 additions & 4 deletions crates/re_renderer/src/allocator/cpu_write_gpu_read_belt.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
use std::{num::NonZeroU32, sync::mpsc};
use std::sync::mpsc;

use crate::wgpu_resources::{BufferDesc, GpuBuffer, GpuBufferPool, Texture2DBufferInfo};
use crate::{
texture_info::Texture2DBufferInfo,
wgpu_resources::{BufferDesc, GpuBuffer, GpuBufferPool},
};

/// A sub-allocated staging buffer that can be written to.
///
Expand Down Expand Up @@ -119,7 +122,7 @@ where
buffer: &self.chunk_buffer,
layout: wgpu::ImageDataLayout {
offset: self.byte_offset_in_chunk_buffer,
bytes_per_row: NonZeroU32::new(buffer_info.bytes_per_row_padded),
bytes_per_row: Some(buffer_info.bytes_per_row_padded),
rows_per_image: None,
},
},
Expand Down Expand Up @@ -290,7 +293,7 @@ impl CpuWriteGpuReadBelt {
);
// Largest uncompressed texture format (btw. many compressed texture format have the same block size!)
debug_assert!(
wgpu::TextureFormat::Rgba32Uint.describe().block_size as u64
wgpu::TextureFormat::Rgba32Uint.block_size(None).unwrap() as u64
<= CpuWriteGpuReadBelt::MIN_OFFSET_ALIGNMENT
);

Expand Down
28 changes: 21 additions & 7 deletions crates/re_renderer/src/allocator/gpu_readback_belt.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use std::{num::NonZeroU32, ops::Range, sync::mpsc};
use std::{ops::Range, sync::mpsc};

use crate::wgpu_resources::{BufferDesc, GpuBuffer, GpuBufferPool, Texture2DBufferInfo};
use crate::texture_info::Texture2DBufferInfo;
use crate::wgpu_resources::{BufferDesc, GpuBuffer, GpuBufferPool};

/// Identifier used to identify a buffer upon retrieval of the data.
///
Expand All @@ -16,6 +17,12 @@ struct PendingReadbackRange {
user_data: GpuReadbackUserDataStorage,
}

#[derive(thiserror::Error, Debug)]
pub enum GpuReadbackError {
#[error("Texture format {0:?} is not supported for readback.")]
UnsupportedTextureFormatForReadback(wgpu::TextureFormat),
}

/// A reserved slice for GPU readback.
///
/// Readback needs to happen from a buffer/texture with copy-source usage,
Expand All @@ -36,8 +43,8 @@ impl GpuReadbackBuffer {
encoder: &mut wgpu::CommandEncoder,
source: wgpu::ImageCopyTexture<'_>,
copy_extents: glam::UVec2,
) {
self.read_multiple_texture2d(encoder, &[(source, copy_extents)]);
) -> Result<(), GpuReadbackError> {
self.read_multiple_texture2d(encoder, &[(source, copy_extents)])
}

/// Reads multiple textures into the same buffer.
Expand All @@ -54,11 +61,17 @@ impl GpuReadbackBuffer {
mut self,
encoder: &mut wgpu::CommandEncoder,
sources_and_extents: &[(wgpu::ImageCopyTexture<'_>, glam::UVec2)],
) {
) -> Result<(), GpuReadbackError> {
for (source, copy_extents) in sources_and_extents {
let start_offset = wgpu::util::align_to(
self.range_in_chunk.start,
source.texture.format().describe().block_size as u64,
source
.texture
.format()
.block_size(Some(source.aspect))
.ok_or(GpuReadbackError::UnsupportedTextureFormatForReadback(
source.texture.format(),
))? as u64,
);

let buffer_info = Texture2DBufferInfo::new(source.texture.format(), *copy_extents);
Expand All @@ -75,7 +88,7 @@ impl GpuReadbackBuffer {
buffer: &self.chunk_buffer,
layout: wgpu::ImageDataLayout {
offset: start_offset,
bytes_per_row: NonZeroU32::new(buffer_info.bytes_per_row_padded),
bytes_per_row: Some(buffer_info.bytes_per_row_padded),
rows_per_image: None,
},
},
Expand All @@ -89,6 +102,7 @@ impl GpuReadbackBuffer {
self.range_in_chunk =
(start_offset + buffer_info.buffer_size_padded)..self.range_in_chunk.end;
}
Ok(())
}

// TODO(andreas): Unused & untested so far!
Expand Down
3 changes: 2 additions & 1 deletion crates/re_renderer/src/allocator/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ mod uniform_buffer_fill;

pub use cpu_write_gpu_read_belt::{CpuWriteGpuReadBelt, CpuWriteGpuReadBuffer};
pub use gpu_readback_belt::{
GpuReadbackBelt, GpuReadbackBuffer, GpuReadbackIdentifier, GpuReadbackUserDataStorage,
GpuReadbackBelt, GpuReadbackBuffer, GpuReadbackError, GpuReadbackIdentifier,
GpuReadbackUserDataStorage,
};
pub use uniform_buffer_fill::{
create_and_fill_uniform_buffer, create_and_fill_uniform_buffer_batch,
Expand Down
3 changes: 2 additions & 1 deletion crates/re_renderer/src/draw_phases/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ pub use outlines::{OutlineConfig, OutlineMaskPreference, OutlineMaskProcessor};

mod picking_layer;
pub use picking_layer::{
PickingLayerId, PickingLayerInstanceId, PickingLayerObjectId, PickingLayerProcessor,
PickingLayerError, PickingLayerId, PickingLayerInstanceId, PickingLayerObjectId,
PickingLayerProcessor,
};

mod screenshot;
Expand Down
Loading

0 comments on commit eef4758

Please sign in to comment.