Skip to content

Commit

Permalink
Fixes for Skybox and All Examples (#4780)
Browse files Browse the repository at this point in the history
  • Loading branch information
cwfitzgerald committed Nov 27, 2023
1 parent 2964eed commit 4f24c31
Show file tree
Hide file tree
Showing 26 changed files with 153 additions and 56 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,10 @@ Bottom level categories:

This adds a way to allow a Vulkan driver which is non-compliant per VK_KHR_driver_properties to be enumerated. This is intended for testing new Vulkan drivers which are not Vulkan compliant yet.

### `DeviceExt::create_texture_with_data` Allows Mip-Major Data

Previously, `DeviceExt::create_texture_with_data` only allowed data to be provided in layer major order. There is now a `order` parameter which allows you to specify if the data is in layer major or mip major order.

### New Features

#### General
Expand Down
34 changes: 10 additions & 24 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,6 @@ cfg_aliases = "0.1"
cfg-if = "1"
codespan-reporting = "0.11"
ctor = "0.2"
ddsfile = "0.5.2"
encase = "0.6"
env_logger = "0.10"
fern = "0.6"
Expand All @@ -88,6 +87,7 @@ getrandom = "0.2"
glam = "0.24.2"
heck = "0.4.0"
image = { version = "0.24", default-features = false, features = ["png"] }
ktx2 = "0.3"
# libloading 0.8 switches from `winapi` to `windows-sys`; permit either
libloading = ">=0.7, <0.9"
libc = "0.2"
Expand Down
2 changes: 1 addition & 1 deletion examples/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,11 @@ test = false
[dependencies]
bytemuck.workspace = true
cfg-if.workspace = true
ddsfile.workspace = true
encase = { workspace = true, features = ["glam"] }
flume.workspace = true
getrandom.workspace = true
glam.workspace = true
ktx2.workspace = true
log.workspace = true
nanorand.workspace = true
noise.workspace = true
Expand Down
17 changes: 12 additions & 5 deletions examples/src/framework.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use wgpu::{Instance, Surface};
use winit::{
dpi::PhysicalSize,
event::{Event, KeyEvent, StartCause, WindowEvent},
event_loop::{ControlFlow, EventLoop, EventLoopWindowTarget},
event_loop::{EventLoop, EventLoopWindowTarget},
keyboard::{Key, NamedKey},
window::Window,
};
Expand Down Expand Up @@ -218,7 +218,17 @@ impl SurfaceWrapper {

match surface.get_current_texture() {
Ok(frame) => frame,
Err(_) => {
// If we timed out, just try again
Err(wgpu::SurfaceError::Timeout) => surface
.get_current_texture()
.expect("Failed to acquire next surface texture!"),
Err(
// If the surface is outdated, or was lost, reconfigure it.
wgpu::SurfaceError::Outdated
| wgpu::SurfaceError::Lost
// If OutOfMemory happens, reconfiguring may not help, but we might as well try
| wgpu::SurfaceError::OutOfMemory,
) => {
surface.configure(&context.device, self.config());
surface
.get_current_texture()
Expand Down Expand Up @@ -380,9 +390,6 @@ async fn start<E: Example>(title: &str) {
let _ = (event_loop_function)(
window_loop.event_loop,
move |event: Event<()>, target: &EventLoopWindowTarget<()>| {
// We set to refresh as fast as possible.
target.set_control_flow(ControlFlow::Poll);

match event {
ref e if SurfaceWrapper::start_condition(e) => {
surface.resume(&context, window_loop.window.clone(), E::SRGB);
Expand Down
Binary file added examples/src/mipmap/screenshot_query.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file removed examples/src/skybox/images/astc.dds
Binary file not shown.
Binary file added examples/src/skybox/images/astc.ktx2
Binary file not shown.
Binary file removed examples/src/skybox/images/bc1.dds
Binary file not shown.
Binary file added examples/src/skybox/images/bc7.ktx2
Binary file not shown.
Binary file removed examples/src/skybox/images/bgra.dds
Binary file not shown.
Binary file removed examples/src/skybox/images/etc2.dds
Binary file not shown.
Binary file added examples/src/skybox/images/etc2.ktx2
Binary file not shown.
45 changes: 45 additions & 0 deletions examples/src/skybox/images/generation.bash
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
# Needs montage from ImageMagick in PATH
# Needs compressonatorcli.exe from https://github.com/GPUOpen-Tools/compressonator in PATH
# Needs PVRTexToolCLI.exe from https://developer.imaginationtech.com/pvrtextool/ in PATH

# Generate a skybox image from 6 jpeg in the folder in first argument.
# The images must be named right.jpg, left.jpg, top.jpg, bottom.jpg, back.jpg, front.jpg
#
# Must be called from the root of the project.
#
# bash examples/src/skybox/images/generation.bash ./path/to/images/folder

SCRIPT_DIRECTORY=examples/src/skybox/images
CHUNK_SIZE="256x256"

set -e

# ensure the script is called from the root of the project
if [ ! -f "$SCRIPT_DIRECTORY/generation.bash" ]; then
echo "The script must be called from the root of the project!"
exit 1
fi

# ensure an argument is passed
if [ $# -eq 0 ]; then
echo "No arguments supplied!"
echo
echo "Usage: bash examples/src/skybox/images/generation.bash ./path/to/images/folder"
exit 1
fi

TEMP=examples/src/skybox/images/tmp

mkdir -p $TEMP
# resize images to 256x256
magick mogrify -path $TEMP -resize 256x256 -format png $1/*.jpg
# create an uncompressed ktx2 cubemap file
PVRTexToolCLI.exe -i $TEMP/right.png,$TEMP/left.png,$TEMP/top.png,$TEMP/bottom.png,$TEMP/front.png,$TEMP/back.png -ics SRGB -cube -m -f r8g8b8a8,UBN,SRGB -o $SCRIPT_DIRECTORY/rgba8.ktx2
# create the bc7 compressed ktx2 cubemap files using compressonator
compressonatorcli.exe -fd BC7 $SCRIPT_DIRECTORY/rgba8.ktx2 $SCRIPT_DIRECTORY/bc7.ktx2
# create the etc2 and astc compressed ktx2 cubemap file using PVRTexTool
#
# compressonator has support for etc2, but the result looks terrible.
PVRTexToolCLI.exe -i $SCRIPT_DIRECTORY/rgba8.ktx2 -ics srgb -m -f ETC2_RGB_A1,UBN,SRGB -q etcslow -o $SCRIPT_DIRECTORY/etc2.ktx2
PVRTexToolCLI.exe -i $SCRIPT_DIRECTORY/rgba8.ktx2 -ics srgb -m -f ASTC_4X4,UBN,SRGB -q astcexhaustive -o $SCRIPT_DIRECTORY/astc.ktx2
rm -r $TEMP
Binary file added examples/src/skybox/images/rgba8.ktx2
Binary file not shown.
42 changes: 25 additions & 17 deletions examples/src/skybox/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use bytemuck::{Pod, Zeroable};
use std::{borrow::Cow, f32::consts};
use wgpu::{util::DeviceExt, AstcBlock, AstcChannel};

const IMAGE_SIZE: u32 = 128;
const IMAGE_SIZE: u32 = 256;

#[derive(Clone, Copy, Pod, Zeroable)]
#[repr(C)]
Expand Down Expand Up @@ -266,20 +266,20 @@ impl crate::framework::Example for Example {
let device_features = device.features();

let skybox_format = if device_features.contains(wgpu::Features::TEXTURE_COMPRESSION_ASTC) {
log::info!("Using ASTC");
log::info!("Using astc");
wgpu::TextureFormat::Astc {
block: AstcBlock::B4x4,
channel: AstcChannel::UnormSrgb,
}
} else if device_features.contains(wgpu::Features::TEXTURE_COMPRESSION_ETC2) {
log::info!("Using ETC2");
wgpu::TextureFormat::Etc2Rgb8UnormSrgb
log::info!("Using etc2");
wgpu::TextureFormat::Etc2Rgb8A1UnormSrgb
} else if device_features.contains(wgpu::Features::TEXTURE_COMPRESSION_BC) {
log::info!("Using BC");
wgpu::TextureFormat::Bc1RgbaUnormSrgb
log::info!("Using bc7");
wgpu::TextureFormat::Bc7RgbaUnormSrgb
} else {
log::info!("Using plain");
wgpu::TextureFormat::Bgra8UnormSrgb
log::info!("Using rgba8");
wgpu::TextureFormat::Rgba8UnormSrgb
};

let size = wgpu::Extent3d {
Expand All @@ -306,28 +306,36 @@ impl crate::framework::Example for Example {
wgpu::TextureFormat::Astc {
block: AstcBlock::B4x4,
channel: AstcChannel::UnormSrgb,
} => &include_bytes!("images/astc.dds")[..],
wgpu::TextureFormat::Etc2Rgb8UnormSrgb => &include_bytes!("images/etc2.dds")[..],
wgpu::TextureFormat::Bc1RgbaUnormSrgb => &include_bytes!("images/bc1.dds")[..],
wgpu::TextureFormat::Bgra8UnormSrgb => &include_bytes!("images/bgra.dds")[..],
} => &include_bytes!("images/astc.ktx2")[..],
wgpu::TextureFormat::Etc2Rgb8A1UnormSrgb => &include_bytes!("images/etc2.ktx2")[..],
wgpu::TextureFormat::Bc7RgbaUnormSrgb => &include_bytes!("images/bc7.ktx2")[..],
wgpu::TextureFormat::Rgba8UnormSrgb => &include_bytes!("images/rgba8.ktx2")[..],
_ => unreachable!(),
};

let image = ddsfile::Dds::read(&mut std::io::Cursor::new(&bytes)).unwrap();
let reader = ktx2::Reader::new(bytes).unwrap();
let header = reader.header();

let mut image = Vec::with_capacity(reader.data().len());
for level in reader.levels() {
image.extend_from_slice(level);
}

let texture = device.create_texture_with_data(
queue,
&wgpu::TextureDescriptor {
size,
mip_level_count: max_mips,
mip_level_count: header.level_count,
sample_count: 1,
dimension: wgpu::TextureDimension::D2,
format: skybox_format,
usage: wgpu::TextureUsages::TEXTURE_BINDING | wgpu::TextureUsages::COPY_DST,
label: None,
view_formats: &[],
},
&image.data,
// KTX2 stores mip levels in mip major order.
wgpu::util::TextureDataOrder::MipMajor,
&image,
);

let texture_view = texture.create_view(&wgpu::TextureViewDescriptor {
Expand Down Expand Up @@ -477,8 +485,8 @@ static TEST: crate::framework::ExampleTestParams = crate::framework::ExampleTest
#[cfg(test)]
#[wgpu_test::gpu_test]
static TEST_BCN: crate::framework::ExampleTestParams = crate::framework::ExampleTestParams {
name: "skybox-bc1",
image_path: "/examples/src/skybox/screenshot_bc1.png",
name: "skybox-bc7",
image_path: "/examples/src/skybox/screenshot_bc7.png",
width: 1024,
height: 768,
optional_features: wgpu::Features::TEXTURE_COMPRESSION_BC,
Expand Down
Binary file modified examples/src/skybox/screenshot.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified examples/src/skybox/screenshot_astc.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file removed examples/src/skybox/screenshot_bc1.png
Binary file not shown.
Binary file added examples/src/skybox/screenshot_bc7.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified examples/src/skybox/screenshot_etc2.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1 change: 1 addition & 0 deletions tests/tests/mem_leaks.rs
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,7 @@ fn draw_test_with_reports(
usage: wgpu::TextureUsages::RENDER_ATTACHMENT | wgpu::TextureUsages::COPY_DST,
view_formats: &[],
},
wgpu::util::TextureDataOrder::LayerMajor,
&[0, 0, 0, 1],
);
let texture_view = texture.create_view(&wgpu::TextureViewDescriptor::default());
Expand Down
1 change: 1 addition & 0 deletions tests/tests/shader_view_format/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ fn reinterpret(
sample_count: 1,
view_formats: &[reinterpret_to],
},
wgpu::util::TextureDataOrder::LayerMajor,
bytemuck::cast_slice(src_data),
);
let tv = tex.create_view(&wgpu::TextureViewDescriptor {
Expand Down
1 change: 1 addition & 0 deletions tests/tests/vertex_indices/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -316,6 +316,7 @@ fn vertex_index_common(ctx: TestingContext) {
usage: wgpu::TextureUsages::RENDER_ATTACHMENT | wgpu::TextureUsages::COPY_DST,
view_formats: &[],
},
wgpu::util::TextureDataOrder::LayerMajor,
&[0, 0, 0, 1],
)
.create_view(&wgpu::TextureViewDescriptor::default());
Expand Down
Loading

0 comments on commit 4f24c31

Please sign in to comment.