Skip to content

Commit

Permalink
Add a HDR demo and angle support
Browse files Browse the repository at this point in the history
  • Loading branch information
jinleili committed Feb 13, 2022
1 parent 82e58b7 commit ba00bcc
Show file tree
Hide file tree
Showing 26 changed files with 301 additions and 76 deletions.
4 changes: 2 additions & 2 deletions Android/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ buildscript {
}
}
plugins {
id 'com.android.application' version '7.1.0' apply false
id 'com.android.library' version '7.1.0' apply false
id 'com.android.application' version '7.1.1' apply false
id 'com.android.library' version '7.1.1' apply false
id 'org.jetbrains.kotlin.android' version '1.5.21' apply false
}

Expand Down
12 changes: 6 additions & 6 deletions Cargo.lock

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

8 changes: 5 additions & 3 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,22 +8,24 @@ version = "0.1.0"
crate-type = ["rlib", "staticlib", "cdylib"]

[features]
angle = ["wgpu/angle"]
default = []

[dependencies]
app-surface = {path = "./app-surface"}
bytemuck = {version = "1.4", features = ["derive"]}
cgmath = "0.18"
log = "0.4.14"
log = "0.4"
noise = {version = "0.7", default-features = false}
pollster = "0.2"
rand = {version = "0.7.2"}
wgpu = {git = "https://github.com/gfx-rs/wgpu", rev = "6931e571"}
# wgpu = {git = "https://github.com/gfx-rs/wgpu", rev = "6931e571"}
wgpu = {git = "https://github.com/jinleili/wgpu", rev = "33c168c"}
# wgpu = {path = "../../forks/wgpu/wgpu"}

[target.'cfg(any(not(target_os = "ios"), not(target_os = "android")))'.dependencies]
async-executor = "1.0"
winit = "*"
winit = "0.26"

[target.'cfg(target_os = "ios")'.dependencies]
env_logger = "0.9"
Expand Down
6 changes: 5 additions & 1 deletion README.MD
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ rustup target add aarch64-apple-ios-sim
### Build and run
Build for iOS device and simulator
```sh
# Use Metal backend
cargo build --target aarch64-apple-ios && cargo build --target <aarch64-apple-ios-sim or x86_64-apple-ios>
```

Expand Down Expand Up @@ -83,7 +84,10 @@ cp target/armv7-linux-androideabi/release/libwgpu_on_app.so android/app/libs/arm

## **Desktop**
```sh
# Run
# Use Metal backend.
cargo run
# Or, use GL backend, need `Angle` libs on your computer.
# https://github.com/gfx-rs/wgpu/pull/2461
WGPU_BACKEND=gl cargo run --features=angle
# Then, press 0, 1, 2, 3, 4 keys change running example.
```
13 changes: 7 additions & 6 deletions app-surface/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,25 +11,26 @@ crate-type = ["rlib"]
default = []

[dependencies]
log = "0.4.14"
log = "0.4"
pollster = "0.2"
wgpu = {git = "https://github.com/gfx-rs/wgpu", rev = "6931e571"}
# wgpu = {git = "https://github.com/gfx-rs/wgpu", rev = "6931e571"}
wgpu = {git = "https://github.com/jinleili/wgpu", rev = "33c168c"}
# wgpu = {path = "../../../forks/wgpu/wgpu"}

[target.'cfg(any(not(target_os = "ios"), not(target_os = "android")))'.dependencies]
async-executor = "1.0"
winit = "*"
winit = "0.26"

[target.'cfg(target_os = "ios")'.dependencies]
core-graphics = "*"
env_logger = "0.9"
lazy_static = "*"
lazy_static = "1.4"
libc = "*"
objc = "*"
objc = "0.2.7"
objc-foundation = "*"

[target.'cfg(target_os = "android")'.dependencies]
android_logger = "0.10.1"
jni = "0.19"
ndk-sys = "*"
raw-window-handle = "*"
raw-window-handle = "0.4"
5 changes: 3 additions & 2 deletions app-surface/src/android.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,11 @@ impl AppSurface {
surface as *mut _,
))
};
let instance = wgpu::Instance::new(wgpu::Backends::VULKAN);
let backend = wgpu::util::backend_bits_from_env().unwrap_or_else(|| wgpu::Backends::VULKAN);
let instance = wgpu::Instance::new(backend);
let surface = unsafe { instance.create_surface(&native_window) };
let (_adapter, device, queue) =
pollster::block_on(crate::request_device(&instance, &surface));
pollster::block_on(crate::request_device(&instance, backend, &surface));

let config = wgpu::SurfaceConfiguration {
usage: wgpu::TextureUsages::RENDER_ATTACHMENT,
Expand Down
5 changes: 3 additions & 2 deletions app-surface/src/app_surface.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,12 @@ pub struct AppSurface {
impl AppSurface {
pub fn new(view: winit::window::Window) -> Self {
let scale_factor = view.scale_factor();
let backend = wgpu::util::backend_bits_from_env().unwrap_or(wgpu::Backends::PRIMARY);
let backend =
wgpu::util::backend_bits_from_env().unwrap_or_else(|| wgpu::Backends::PRIMARY);
let instance = wgpu::Instance::new(backend);
let (physical, surface) = unsafe { (view.inner_size(), instance.create_surface(&view)) };
let (_adapter, device, queue) =
pollster::block_on(crate::request_device(&instance, &surface));
pollster::block_on(crate::request_device(&instance, backend, &surface));

let config = wgpu::SurfaceConfiguration {
usage: wgpu::TextureUsages::RENDER_ATTACHMENT,
Expand Down
6 changes: 3 additions & 3 deletions app-surface/src/ios.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,11 @@ impl AppSurface {
(s.size.width as f32 * scale_factor) as u32,
(s.size.height as f32 * scale_factor) as u32,
);

let instance = wgpu::Instance::new(wgpu::Backends::METAL);
let backend = wgpu::util::backend_bits_from_env().unwrap_or_else(|| wgpu::Backends::METAL);
let instance = wgpu::Instance::new(backend);
let surface = unsafe { instance.create_surface_from_core_animation_layer(obj.metal_layer) };
let (_adapter, device, queue) =
pollster::block_on(crate::request_device(&instance, &surface));
pollster::block_on(crate::request_device(&instance, backend, &surface));
let config = wgpu::SurfaceConfiguration {
usage: wgpu::TextureUsages::RENDER_ATTACHMENT,
format: wgpu::TextureFormat::Bgra8UnormSrgb,
Expand Down
46 changes: 28 additions & 18 deletions app-surface/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use std::sync::Arc;
use std::{ops::Deref, sync::Arc};

mod touch;
pub use touch::*;
Expand All @@ -16,6 +16,13 @@ pub struct SurfaceDeviceQueue {
pub queue: Arc<wgpu::Queue>,
}

impl SurfaceDeviceQueue {
pub fn update_config_format(&mut self, format: wgpu::TextureFormat) {
self.config.format = format;
self.surface.configure(&self.device, &self.config);
}
}

impl std::ops::Deref for AppSurface {
type Target = SurfaceDeviceQueue;
fn deref(&self) -> &Self::Target {
Expand Down Expand Up @@ -47,7 +54,6 @@ pub trait Frame {
let view = frame
.texture
.create_view(&wgpu::TextureViewDescriptor::default());
// frame cannot be drop early
(frame, view)
}
}
Expand All @@ -67,24 +73,28 @@ impl Frame for AppSurface {

async fn request_device(
instance: &wgpu::Instance,
backend: wgpu::Backends,
surface: &wgpu::Surface,
) -> (wgpu::Adapter, wgpu::Device, wgpu::Queue) {
let adapter = instance
.request_adapter(&wgpu::RequestAdapterOptions {
power_preference: wgpu::PowerPreference::default(),
compatible_surface: Some(surface),
force_fallback_adapter: false,
})
.await
.unwrap();
let request_features = if cfg!(target_os = "android") {
// unsupported features on some android: POLYGON_MODE_LINE | VERTEX_WRITABLE_STORAGE
wgpu::Features::default()
} else {
wgpu::Features::MAPPABLE_PRIMARY_BUFFERS
| wgpu::Features::POLYGON_MODE_LINE
| wgpu::Features::VERTEX_WRITABLE_STORAGE
};
let adapter =
wgpu::util::initialize_adapter_from_env_or_default(instance, backend, Some(surface))
.await
.expect("No suitable GPU adapters found on the system!");
let adapter_info = adapter.get_info();
println!("Using {} ({:?})", adapter_info.name, adapter_info.backend);

// unsupported features on some android: POLYGON_MODE_LINE | VERTEX_WRITABLE_STORAGE
let mut request_features = wgpu::Features::empty();
for f in [
wgpu::Features::MAPPABLE_PRIMARY_BUFFERS,
wgpu::Features::POLYGON_MODE_LINE,
wgpu::Features::VERTEX_WRITABLE_STORAGE,
wgpu::Features::TEXTURE_COMPRESSION_ASTC_HDR,
] {
if adapter.features().contains(f) {
request_features |= f;
}
}

let res = adapter
.request_device(
Expand Down
Binary file added assets/12x12.astc
Binary file not shown.
Binary file added assets/6x6.astc
Binary file not shown.
Binary file added assets/8x8.astc
Binary file not shown.
4 changes: 2 additions & 2 deletions cargo-so/README.MD
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ rustup target add armv7-linux-androideabi aarch64-linux-android i686-linux-andro
Build
```sh
# build to all android targets
cargo so build
cargo so build --lib
# build to the specified target
cargo so b --target aarch64-linux-android
cargo so b --lib --target aarch64-linux-android
```
7 changes: 5 additions & 2 deletions cargo-so/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ fn main() {
for target in build_targets {
let triple = target.rust_triple();
// setting ar, linker value
let mut cargo = cargo_ndk(&ndk, target, 30).unwrap();
let mut cargo = cargo_ndk(&ndk, target, 24).unwrap();
cargo.arg("rustc");
if cmd.target().is_none() {
cargo.arg("--target").arg(triple);
Expand All @@ -37,7 +37,10 @@ fn main() {
let _ = std::fs::create_dir_all(&gcc_link_dir);
std::fs::write(gcc_link_dir.join("libgcc.a"), "INPUT(-lunwind)")
.expect("Failed to write");
cargo.arg("-L").arg(gcc_link_dir);
println!("gcc_dir: {:?}", gcc_link_dir);
let test_dir = "/Users/lijinlei/Rust/android-ndk-rs/target/cargo-apk-temp-extra-link-libraries";
// cargo.arg("-L").arg(gcc_link_dir);
cargo.arg("-L").arg(test_dir);
}

if !cargo.status().unwrap().success() {
Expand Down
2 changes: 1 addition & 1 deletion iOS/base/Base.lproj/Main.storyboard
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@
<segments>
<segment title="boids"/>
<segment title="MSAA line"/>
<segment title="cube"/>
<segment title="HDR ASTC"/>
<segment title="water"/>
<segment title="shadow"/>
</segments>
Expand Down
3 changes: 3 additions & 0 deletions iOS/base/MetalView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,9 @@ class MetalView: UIView {
// https://tomisacat.xyz/tech/2017/06/17/scale-nativescale-contentsscale.html
layer.removeAllAnimations()
self.contentScaleFactor = UIScreen.main.nativeScale

// 支持 HDR
// opt-in EDR
}
}

6 changes: 5 additions & 1 deletion iOS/base/ViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,11 @@ class ViewController: UIViewController {
guard let canvas = self.wgpuCanvas else {
return
}
change_example(canvas, Int32(sender.selectedSegmentIndex))
var index = sender.selectedSegmentIndex
if index == 2 {
index = 5
}
change_example(canvas, Int32(index))
}

}
Expand Down
2 changes: 2 additions & 0 deletions iOS/base/libwgpu_on_app.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,6 @@ struct wgpu_canvas *create_wgpu_canvas(struct ios_view_obj object);
void enter_frame(struct wgpu_canvas *data);
void change_example(struct wgpu_canvas *data, int32_t index);

void ffi_main();

#endif /* libwgpu_on_ios_h */
16 changes: 12 additions & 4 deletions iOS/wgpu_on_iOS.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
AD4B1FF826EB1B91008A6AEE /* Base */ = {isa = PBXFileReference; lastKnownFileType = file.storyboard; name = Base; path = Base.lproj/LaunchScreen.storyboard; sourceTree = "<group>"; };
AD4B1FFA26EB1B91008A6AEE /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = "<group>"; };
AD4B200026EB1D31008A6AEE /* MetalView.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = MetalView.swift; sourceTree = "<group>"; };
AD59976927B36E6A00C1D01B /* libwgpu_example.a */ = {isa = PBXFileReference; lastKnownFileType = archive.ar; name = libwgpu_example.a; path = "../../../forks/wgpu-example/target/aarch64-apple-ios/debug/libwgpu_example.a"; sourceTree = "<group>"; };
ADA4046C279ABEE7005BF0DB /* libwgpu_on_app.a */ = {isa = PBXFileReference; lastKnownFileType = archive.ar; name = libwgpu_on_app.a; path = "../target/aarch64-apple-ios/debug/libwgpu_on_app.a"; sourceTree = "<group>"; };
ADA4699026EB27CB008B1C7B /* libwgpu_on_app.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = libwgpu_on_app.h; sourceTree = "<group>"; };
ADA4699226EB2F9A008B1C7B /* wgpu_test-Bridging-Header.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = "wgpu_test-Bridging-Header.h"; sourceTree = "<group>"; };
Expand Down Expand Up @@ -120,6 +121,7 @@
ADA4699326EB3079008B1C7B /* Frameworks */ = {
isa = PBXGroup;
children = (
AD59976927B36E6A00C1D01B /* libwgpu_example.a */,
ADA4046C279ABEE7005BF0DB /* libwgpu_on_app.a */,
AD0CB7D927A402370041811B /* libwgpu_on_app.a */,
ADA4699626EB311F008B1C7B /* libwgpu_on_ios.a */,
Expand Down Expand Up @@ -459,12 +461,15 @@
CURRENT_PROJECT_VERSION = 1.0;
DEVELOPMENT_TEAM = P3HQ9GDFCW;
INFOPLIST_FILE = wgpu_phone/Info.plist;
IPHONEOS_DEPLOYMENT_TARGET = 13.0;
IPHONEOS_DEPLOYMENT_TARGET = 12.3;
LD_RUNPATH_SEARCH_PATHS = (
"$(inherited)",
"@executable_path/Frameworks",
);
LIBRARY_SEARCH_PATHS = "$(PROJECT_DIR)/../target/aarch64-apple-ios/debug";
LIBRARY_SEARCH_PATHS = (
"$(PROJECT_DIR)/../target/aarch64-apple-ios/debug",
"$(PROJECT_DIR)",
);
MARKETING_VERSION = 1.0;
OTHER_LDFLAGS = (
"$(inherited)",
Expand All @@ -489,12 +494,15 @@
CURRENT_PROJECT_VERSION = 1.0;
DEVELOPMENT_TEAM = P3HQ9GDFCW;
INFOPLIST_FILE = wgpu_phone/Info.plist;
IPHONEOS_DEPLOYMENT_TARGET = 13.0;
IPHONEOS_DEPLOYMENT_TARGET = 12.3;
LD_RUNPATH_SEARCH_PATHS = (
"$(inherited)",
"@executable_path/Frameworks",
);
LIBRARY_SEARCH_PATHS = "$(PROJECT_DIR)/../target/aarch64-apple-ios/release";
LIBRARY_SEARCH_PATHS = (
"$(PROJECT_DIR)/../target/aarch64-apple-ios/release",
"$(PROJECT_DIR)",
);
MARKETING_VERSION = 1.0;
OTHER_LDFLAGS = (
"$(inherited)",
Expand Down
Binary file not shown.
2 changes: 0 additions & 2 deletions src/examples/cube.rs
Original file line number Diff line number Diff line change
Expand Up @@ -377,9 +377,7 @@ impl Example for Cube {
rpass.draw_indexed(0..self.index_count as u32, 0, 0..1);
}
}

queue.submit(Some(encoder.finish()));

frame.present();
}
}
Loading

0 comments on commit ba00bcc

Please sign in to comment.