Configure gpu surface on non wasm32 targets

This commit is contained in:
Dennis Kobert
2023-06-16 19:56:52 +02:00
committed by Keavon Chambers
parent 989c8ad5f8
commit 8cd161f087
8 changed files with 82 additions and 19 deletions

3
Cargo.lock generated
View File

@@ -1749,8 +1749,10 @@ version = "0.1.0"
dependencies = [
"bezier-rs",
"bitflags 1.3.2",
"chrono",
"dyn-any",
"env_logger",
"fern",
"future-executor",
"futures",
"glam",
@@ -1829,6 +1831,7 @@ dependencies = [
"wgpu",
"wgpu-executor",
"wgpu-types",
"winit",
"xxhash-rust",
]

View File

@@ -5,8 +5,13 @@ edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
[features]
gpu = ["interpreted-executor/gpu", "graphene-std/gpu", "graphene-core/gpu", "wgpu-executor", "gpu-executor"]
default = ["wgpu"]
wgpu = ["wgpu-executor", "gpu"]
[dependencies]
log = "0.4"
bitflags = "1.2.1"
serde = { version = "1.0", features = ["derive"] }
@@ -30,6 +35,8 @@ future-executor = { path = "../future-executor", optional = true }
wasm-bindgen = { version = "0.2.86", optional = true }
futures = "0.3.28"
fern = { version = "0.6.2", features = ["colored"] }
chrono = "0.4.26"
[dependencies.document-legacy]
path = "../../document-legacy"

View File

@@ -1,3 +1,4 @@
use fern::colors::{Color, ColoredLevelConfig};
use std::{collections::HashMap, error::Error};
use document_legacy::{
@@ -25,6 +26,28 @@ impl NodeGraphUpdateSender for UpdateLogger {
}
fn main() -> Result<(), Box<dyn Error>> {
let colors = ColoredLevelConfig::new()
.debug(Color::Magenta)
.info(Color::Green)
.error(Color::Red);
fern::Dispatch::new()
.chain(std::io::stdout())
.level_for("foodcalc", log::LevelFilter::Trace)
.level_for("sqlx", log::LevelFilter::Trace)
.level_for("iced", log::LevelFilter::Trace)
.level(log::LevelFilter::Trace)
.format(move |out, message, record| {
out.finish(format_args!(
"[{}]{} {}",
// This will color the log level only, not the whole line. Just a touch.
colors.color(record.level()),
chrono::Utc::now().format("[%Y-%m-%d %H:%M:%S]"),
message
))
})
.apply()
.unwrap();
let document_path = std::env::args().nth(1).expect("No document path provided");
let document_string = std::fs::read_to_string(&document_path).expect("Failed to read document");
@@ -45,7 +68,7 @@ fn main() -> Result<(), Box<dyn Error>> {
let editor_api = WasmEditorApi {
image_frame: None,
font_cache: &FontCache::default(),
application_io: &WasmApplicationIo::default(),
application_io: &block_on(WasmApplicationIo::new()),
node_graph_message_sender: &UpdateLogger {},
imaginate_preferences: &ImaginatePreferences::default(),
};
@@ -92,7 +115,16 @@ pub fn wrap_network_in_scope(mut network: NodeNetwork) -> NodeNetwork {
};
// wrap the inner network in a scope
let nodes = vec![begin_scope(), inner_network, end_scope()];
let nodes = vec![
begin_scope(),
inner_network,
DocumentNode {
name: "End Scope".to_string(),
implementation: DocumentNodeImplementation::proto("graphene_core::memo::EndLetNode<_>"),
inputs: vec![NodeInput::node(0, 0), NodeInput::node(1, 0)],
..Default::default()
},
];
NodeNetwork {
inputs: vec![0],
outputs: vec![NodeOutput::new(2, 0)],
@@ -138,11 +170,3 @@ fn begin_scope() -> DocumentNode {
..Default::default()
}
}
fn end_scope() -> DocumentNode {
DocumentNode {
name: "End Scope".to_string(),
implementation: DocumentNodeImplementation::proto("graphene_core::memo::EndLetNode<_>"),
inputs: vec![NodeInput::value(TaggedValue::None, true), NodeInput::value(TaggedValue::ImageFrame(ImageFrame::empty()), true)],
..Default::default()
}
}

File diff suppressed because one or more lines are too long

View File

@@ -10,12 +10,7 @@ license = "MIT OR Apache-2.0"
[features]
default = ["wasm", "imaginate"]
gpu = [
"graphene-core/gpu",
"gpu-compiler-bin-wrapper",
"compilation-client",
"gpu-executor",
]
gpu = ["graphene-core/gpu", "gpu-compiler-bin-wrapper", "compilation-client", "gpu-executor"]
vulkan = ["gpu", "vulkan-executor"]
wgpu = ["gpu", "wgpu-executor"]
quantization = ["autoquant"]
@@ -62,6 +57,7 @@ js-sys = { version = "0.3.63", optional = true }
wgpu-types = "0.16.0"
wgpu = "0.16.1"
wasm-bindgen-futures = { version = "0.4.36", optional = true }
winit = "0.28.6"
[dependencies.serde]
version = "1.0"

View File

@@ -51,12 +51,16 @@ impl<'a> From<&'a WasmApplicationIo> for &'a WgpuExecutor {
pub type WasmEditorApi<'a> = graphene_core::application_io::EditorApi<'a, WasmApplicationIo>;
impl ApplicationIo for WasmApplicationIo {
#[cfg(target_arch = "wasm32")]
type Surface = HtmlCanvasElement;
#[cfg(not(target_arch = "wasm32"))]
type Surface = winit::window::Window;
#[cfg(feature = "wgpu")]
type Executor = WgpuExecutor;
#[cfg(not(feature = "wgpu"))]
type Executor = ();
#[cfg(target_arch = "wasm32")]
fn create_surface(&self) -> SurfaceHandle<Self::Surface> {
let wrapper = || {
let document = window().expect("should have a window in this context").document().expect("window should have a document");
@@ -90,7 +94,17 @@ impl ApplicationIo for WasmApplicationIo {
wrapper().expect("should be able to set canvas in global scope")
}
#[cfg(not(target_arch = "wasm32"))]
fn create_surface(&self) -> SurfaceHandle<Self::Surface> {
let event_loop = winit::event_loop::EventLoop::new();
let window = winit::window::WindowBuilder::new().with_title("Graphite").build(&event_loop).unwrap();
SurfaceHandle {
surface_id: SurfaceId(window.id().into()),
surface: window,
}
}
#[cfg(target_arch = "wasm32")]
fn destroy_surface(&self, surface_id: SurfaceId) {
let window = window().expect("should have a window in this context");
let window = Object::from(window);
@@ -111,6 +125,9 @@ impl ApplicationIo for WasmApplicationIo {
wrapper().expect("should be able to set canvas in global scope")
}
#[cfg(not(target_arch = "wasm32"))]
fn destroy_surface(&self, surface_id: SurfaceId) {}
#[cfg(feature = "wgpu")]
fn gpu_executor(&self) -> Option<&Self::Executor> {
self.gpu_executor.as_ref()
@@ -123,7 +140,7 @@ pub type WasmSurfaceHandleFrame = SurfaceHandleFrame<HtmlCanvasElement>;
pub struct CreateSurfaceNode {}
#[node_macro::node_fn(CreateSurfaceNode)]
async fn create_surface_node<'a: 'input>(editor: WasmEditorApi<'a>) -> Arc<SurfaceHandle<HtmlCanvasElement>> {
async fn create_surface_node<'a: 'input>(editor: WasmEditorApi<'a>) -> Arc<SurfaceHandle<<WasmApplicationIo as ApplicationIo>::Surface>> {
editor.application_io.create_surface().into()
}

View File

@@ -284,7 +284,7 @@ fn node_registry() -> HashMap<NodeIdentifier, HashMap<NodeIOTypes, NodeConstruct
),
#[cfg(feature = "gpu")]
async_node!(gpu_executor::ReadOutputBufferNode<_, _>, input: Arc<ShaderInput<WgpuExecutor>>, output: Vec<u8>, params: [&WgpuExecutor, ()]),
#[cfg(all(feature = "gpu", target_arch = "wasm32"))]
#[cfg(feature = "gpu")]
async_node!(gpu_executor::CreateGpuSurfaceNode, input: WasmEditorApi, output: Arc<SurfaceHandle<<WgpuExecutor as GpuExecutor>::Surface>>, params: []),
#[cfg(feature = "gpu")]
async_node!(gpu_executor::RenderTextureNode<_, _>, input: ShaderInputFrame<WgpuExecutor>, output: SurfaceFrame, params: [Arc<SurfaceHandle<<WgpuExecutor as GpuExecutor>::Surface>>, &WgpuExecutor]),

View File

@@ -396,6 +396,20 @@ impl gpu_executor::GpuExecutor for WgpuExecutor {
#[cfg(not(target_arch = "wasm32"))]
fn create_surface(&self, window: SurfaceHandle<winit::window::Window>) -> Result<SurfaceHandle<wgpu::Surface>> {
let surface = unsafe { self.context.instance.create_surface(&window.surface) }?;
let surface_caps = surface.get_capabilities(&self.context.adapter);
let surface_format = wgpu::TextureFormat::Bgra8Unorm;
let config = wgpu::SurfaceConfiguration {
usage: wgpu::TextureUsages::RENDER_ATTACHMENT,
format: surface_format,
width: 1920,
height: 1080,
present_mode: surface_caps.present_modes[0],
alpha_mode: wgpu::CompositeAlphaMode::PreMultiplied,
view_formats: vec![wgpu::TextureFormat::Bgra8UnormSrgb],
};
surface.configure(&self.context.device, &config);
let surface_id = window.surface_id;
Ok(SurfaceHandle { surface_id, surface })
}
@@ -404,6 +418,7 @@ impl gpu_executor::GpuExecutor for WgpuExecutor {
impl WgpuExecutor {
pub async fn new() -> Option<Self> {
let context = Context::new().await?;
println!("wgpu executor created");
let texture_bind_group_layout = context.device.create_bind_group_layout(&wgpu::BindGroupLayoutDescriptor {
entries: &[