Restructure GPU execution to model GPU pipelines in the node graph (#1088)

* Start implementing GpuExecutor for wgpu

* Implement read_output_buffer function

* Implement extraction node in the compiler

* Generate type annotations during shader compilation

* Start adding node wrapprs for graph execution api

* Wrap more of the api in nodes

* Restructure Pipeline to accept arbitrary shader inputs

* Adapt nodes to new trait definitions

* Start implementing gpu-compiler trait

* Adapt shader generation

* Hardstuck on pointer casts

* Pass nodes as references in gpu code to avoid zsts

* Update gcore to compile on the gpu

* Fix color doc tests

* Impl Node for node refs
This commit is contained in:
Dennis Kobert
2023-04-23 10:18:31 +02:00
committed by Keavon Chambers
parent 161bbc62b4
commit bdc1ef926a
43 changed files with 1874 additions and 515 deletions

View File

@@ -122,6 +122,20 @@ name = "bytemuck"
version = "1.12.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "aaa3a8d9a1ca92e282c96a32d6511b695d7d994d1d102ba85d279f9b2756947f"
dependencies = [
"bytemuck_derive",
]
[[package]]
name = "bytemuck_derive"
version = "1.4.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1aca418a974d83d40a0c1f0c5cba6ff4bc28d8df099109ca459a2118d40b6322"
dependencies = [
"proc-macro2",
"quote",
"syn",
]
[[package]]
name = "byteorder"
@@ -293,7 +307,7 @@ dependencies = [
[[package]]
name = "dyn-any"
version = "0.2.1"
version = "0.3.1"
dependencies = [
"dyn-any-derive",
"glam",
@@ -302,7 +316,7 @@ dependencies = [
[[package]]
name = "dyn-any-derive"
version = "0.2.1"
version = "0.3.0"
dependencies = [
"proc-macro2",
"quote",
@@ -346,6 +360,106 @@ version = "1.0.7"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3f9eec918d3f24069decb9af1554cad7c880e2da24a9afd88aca000531ab82c1"
[[package]]
name = "futures"
version = "0.3.27"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "531ac96c6ff5fd7c62263c5e3c67a603af4fcaee2e1a0ae5565ba3a11e69e549"
dependencies = [
"futures-channel",
"futures-core",
"futures-executor",
"futures-io",
"futures-sink",
"futures-task",
"futures-util",
]
[[package]]
name = "futures-channel"
version = "0.3.27"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "164713a5a0dcc3e7b4b1ed7d3b433cabc18025386f9339346e8daf15963cf7ac"
dependencies = [
"futures-core",
"futures-sink",
]
[[package]]
name = "futures-core"
version = "0.3.27"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "86d7a0c1aa76363dac491de0ee99faf6941128376f1cf96f07db7603b7de69dd"
[[package]]
name = "futures-executor"
version = "0.3.27"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1997dd9df74cdac935c76252744c1ed5794fac083242ea4fe77ef3ed60ba0f83"
dependencies = [
"futures-core",
"futures-task",
"futures-util",
]
[[package]]
name = "futures-intrusive"
version = "0.5.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1d930c203dd0b6ff06e0201a4a2fe9149b43c684fd4420555b26d21b1a02956f"
dependencies = [
"futures-core",
"lock_api",
"parking_lot",
]
[[package]]
name = "futures-io"
version = "0.3.27"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "89d422fa3cbe3b40dca574ab087abb5bc98258ea57eea3fd6f1fa7162c778b91"
[[package]]
name = "futures-macro"
version = "0.3.27"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3eb14ed937631bd8b8b8977f2c198443447a8355b6e3ca599f38c975e5a963b6"
dependencies = [
"proc-macro2",
"quote",
"syn",
]
[[package]]
name = "futures-sink"
version = "0.3.27"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ec93083a4aecafb2a80a885c9de1f0ccae9dbd32c2bb54b0c3a65690e0b8d2f2"
[[package]]
name = "futures-task"
version = "0.3.27"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "fd65540d33b37b16542a0438c12e6aeead10d4ac5d05bd3f805b8f35ab592879"
[[package]]
name = "futures-util"
version = "0.3.27"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3ef6b17e481503ec85211fed8f39d1970f128935ca1f814cd32ac4a6842e84ab"
dependencies = [
"futures-channel",
"futures-core",
"futures-io",
"futures-macro",
"futures-sink",
"futures-task",
"memchr",
"pin-project-lite",
"pin-utils",
"slab",
]
[[package]]
name = "fxhash"
version = "0.2.1"
@@ -382,6 +496,8 @@ version = "0.22.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "12f597d56c1bd55a811a1be189459e8fad2bbc272616375602443bdfb37fa774"
dependencies = [
"bytemuck",
"num-traits",
"serde",
]
@@ -418,6 +534,7 @@ dependencies = [
"bytemuck",
"dyn-any",
"glam",
"gpu-executor",
"graph-craft",
"graphene-core",
"log",
@@ -430,6 +547,26 @@ dependencies = [
"tera",
]
[[package]]
name = "gpu-executor"
version = "0.1.0"
dependencies = [
"anyhow",
"base64",
"bytemuck",
"dyn-any",
"futures",
"futures-intrusive",
"glam",
"graph-craft",
"graphene-core",
"log",
"node-macro",
"num-traits",
"serde",
"spirv",
]
[[package]]
name = "graph-craft"
version = "0.1.0"
@@ -454,17 +591,22 @@ name = "graphene-core"
version = "0.1.0"
dependencies = [
"async-trait",
"base64",
"bezier-rs",
"bytemuck",
"dyn-any",
"glam",
"kurbo",
"log",
"node-macro",
"num-derive",
"num-traits",
"once_cell",
"rand_chacha",
"serde",
"specta",
"spin",
"spirv-std",
]
[[package]]
@@ -549,6 +691,12 @@ dependencies = [
"cfg-if",
]
[[package]]
name = "internal-iterator"
version = "0.2.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a668ef46056a63366da9d74f48062da9ece1a27958f2f3704aa6f7421c4433f5"
[[package]]
name = "itertools"
version = "0.10.5"
@@ -643,6 +791,12 @@ dependencies = [
"cfg-if",
]
[[package]]
name = "longest-increasing-subsequence"
version = "0.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b3bd0dd2cd90571056fdb71f6275fada10131182f84899f4b2a916e565d81d86"
[[package]]
name = "memchr"
version = "2.5.0"
@@ -658,6 +812,17 @@ dependencies = [
"syn",
]
[[package]]
name = "num-derive"
version = "0.3.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "876a53fff98e03a936a674b29568b0e605f06b29372c2489ff4de23f1949743d"
dependencies = [
"proc-macro2",
"quote",
"syn",
]
[[package]]
name = "num-integer"
version = "0.1.45"
@@ -693,6 +858,29 @@ version = "1.17.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b7e5500299e16ebb147ae15a00a942af264cf3688f47923b8fc2cd5858f23ad3"
[[package]]
name = "parking_lot"
version = "0.12.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3742b2c103b9f06bc9fff0a37ff4912935851bee6d36f3c02bcc755bcfec228f"
dependencies = [
"lock_api",
"parking_lot_core",
]
[[package]]
name = "parking_lot_core"
version = "0.9.7"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9069cbb9f99e3a5083476ccb29ceb1de18b9118cafa53e90c9551235de2b9521"
dependencies = [
"cfg-if",
"libc",
"redox_syscall",
"smallvec",
"windows-sys",
]
[[package]]
name = "parse-zoneinfo"
version = "0.3.0"
@@ -797,6 +985,18 @@ dependencies = [
"uncased",
]
[[package]]
name = "pin-project-lite"
version = "0.2.9"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e0a7ae3ac2f1173085d398531c705756c94a4c56843785df85a60c1a0afac116"
[[package]]
name = "pin-utils"
version = "0.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8b870d8c151b6f2fb93e84a13146138f05d02ed11c7e7c54f8826aaaf7c9f184"
[[package]]
name = "ppv-lite86"
version = "0.2.17"
@@ -917,14 +1117,15 @@ checksum = "08d43f7aa6b08d49f382cde6a7982047c3426db949b1424bc4b7ec9ae12c6ce2"
[[package]]
name = "rustc_codegen_spirv"
version = "0.5.0"
version = "0.7.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1b803b49618cdde99e1065af9415f489b993374765e30a6b80f2bea2cca65914"
checksum = "48862935255ac76002118e3561c54f3fb413a752ef5cd0bdae693cf5c5ce37a5"
dependencies = [
"ar",
"either",
"hashbrown 0.11.2",
"indexmap",
"itertools",
"lazy_static",
"libc",
"num-traits",
@@ -944,9 +1145,9 @@ dependencies = [
[[package]]
name = "rustc_codegen_spirv-types"
version = "0.5.0"
version = "0.7.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "330aedc6b09b9bf3c58cc7fb942c1377310a9cff00fae9e4f6cc09a7a28f542e"
checksum = "339309d7fce2e7204decea1b2683c4d439a6ebe4c1518d8134fe10aefeae7362"
dependencies = [
"rspirv",
"serde",
@@ -1037,6 +1238,15 @@ version = "0.3.10"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7bd3e3206899af3f8b12af284fafc038cc1dc2b41d1b89dd17297221c5d225de"
[[package]]
name = "slab"
version = "0.4.8"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "6528351c9bc8ab22353f9d776db39a20288e8d6c37ef8cfe3317cf875eecfc2d"
dependencies = [
"autocfg",
]
[[package]]
name = "slug"
version = "0.1.4"
@@ -1092,16 +1302,18 @@ dependencies = [
[[package]]
name = "spirt"
version = "0.1.0"
version = "0.2.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "06834ebbbbc6f86448fd5dc7ccbac80e36f52f8d66838683752e19d3cae9a459"
checksum = "e24fa996f12f3c667efbceaa99c222b8910a295a14d2c43c3880dfab2752def7"
dependencies = [
"arrayvec",
"bytemuck",
"elsa",
"indexmap",
"internal-iterator",
"itertools",
"lazy_static",
"longest-increasing-subsequence",
"rustc-hash",
"serde",
"serde_json",
@@ -1120,9 +1332,9 @@ dependencies = [
[[package]]
name = "spirv-builder"
version = "0.5.0"
version = "0.7.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "93f656f97ac742e5603843d2ea3ea644cdf5630635b3320ec87666385766e7ab"
checksum = "0310607328cbb098b681e4580a25871fcf72dd9e1d560e7cc09c409eedef2a27"
dependencies = [
"memchr",
"raw-string",
@@ -1132,6 +1344,37 @@ dependencies = [
"serde_json",
]
[[package]]
name = "spirv-std"
version = "0.7.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3197bd4c021c2dfc0f9dfb356312c8f7842d972d5545c308ad86422c2e2d3e66"
dependencies = [
"bitflags",
"glam",
"num-traits",
"spirv-std-macros",
"spirv-std-types",
]
[[package]]
name = "spirv-std-macros"
version = "0.7.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "bbaffad626ab9d3ac61c4b74b5d51cb52f1939a8041d7ac09ec828eb4ad44d72"
dependencies = [
"proc-macro2",
"quote",
"spirv-std-types",
"syn",
]
[[package]]
name = "spirv-std-types"
version = "0.7.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ab83875e851bc803c687024d2d950730f350c0073714b95b3a6b1d22e9eac42a"
[[package]]
name = "spirv-tools"
version = "0.9.0"
@@ -1434,6 +1677,72 @@ version = "0.4.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "712e227841d057c1ee1cd2fb22fa7e5a5461ae8e48fa2ca79ec42cfc1931183f"
[[package]]
name = "windows-sys"
version = "0.45.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "75283be5efb2831d37ea142365f009c02ec203cd29a3ebecbc093d52315b66d0"
dependencies = [
"windows-targets",
]
[[package]]
name = "windows-targets"
version = "0.42.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8e5180c00cd44c9b1c88adb3693291f1cd93605ded80c250a75d472756b4d071"
dependencies = [
"windows_aarch64_gnullvm",
"windows_aarch64_msvc",
"windows_i686_gnu",
"windows_i686_msvc",
"windows_x86_64_gnu",
"windows_x86_64_gnullvm",
"windows_x86_64_msvc",
]
[[package]]
name = "windows_aarch64_gnullvm"
version = "0.42.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "597a5118570b68bc08d8d59125332c54f1ba9d9adeedeef5b99b02ba2b0698f8"
[[package]]
name = "windows_aarch64_msvc"
version = "0.42.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e08e8864a60f06ef0d0ff4ba04124db8b0fb3be5776a5cd47641e942e58c4d43"
[[package]]
name = "windows_i686_gnu"
version = "0.42.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "c61d927d8da41da96a81f029489353e68739737d3beca43145c8afec9a31a84f"
[[package]]
name = "windows_i686_msvc"
version = "0.42.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "44d840b6ec649f480a41c8d80f9c65108b92d89345dd94027bfe06ac444d1060"
[[package]]
name = "windows_x86_64_gnu"
version = "0.42.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8de912b8b8feb55c064867cf047dda097f92d51efad5b491dfb98f6bbb70cb36"
[[package]]
name = "windows_x86_64_gnullvm"
version = "0.42.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "26d41b46a36d453748aedef1486d5c7a85db22e56aff34643984ea85514e94a3"
[[package]]
name = "windows_x86_64_msvc"
version = "0.42.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9aec5da331524158c6d1a4ac0ab1541149c0b9505fde06423b02f5ef0106b9f0"
[[package]]
name = "xxhash-rust"
version = "0.8.6"

View File

@@ -13,18 +13,25 @@ serde = ["graphene-core/serde", "glam/serde"]
[dependencies]
graphene-core = { path = "../gcore", features = ["async", "std", "alloc"] }
graph-craft = {path = "../graph-craft", features = ["serde"] }
dyn-any = { path = "../../libraries/dyn-any", features = ["log-bad-types", "rc", "glam"] }
graph-craft = { path = "../graph-craft", features = ["serde"] }
gpu-executor = { path = "../gpu-executor" }
dyn-any = { path = "../../libraries/dyn-any", features = [
"log-bad-types",
"rc",
"glam",
] }
num-traits = "0.2"
log = "0.4"
serde = { version = "1", features = ["derive", "rc"]}
serde = { version = "1", features = ["derive", "rc"] }
glam = { version = "0.22" }
base64 = "0.13"
bytemuck = { version = "1.8" }
nvtx = { version = "1.1.1", optional = true }
tempfile = "3"
spirv-builder = { version = "0.5", default-features = false, features=["use-installed-tools"] }
spirv-builder = { version = "0.7", default-features = false, features = [
"use-installed-tools",
] }
tera = { version = "1.17.1" }
anyhow = "1.0.66"
serde_json = "1.0.91"

View File

@@ -11,7 +11,8 @@ profiling = []
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
graph-craft = {path = "../../graph-craft" , features = ["serde"]}
graph-craft = { path = "../../graph-craft", features = ["serde"] }
gpu-executor = { path = "../../gpu-executor" }
log = "0.4"
anyhow = "1.0.66"
serde_json = "1.0.91"

View File

@@ -1,8 +1,15 @@
use gpu_executor::ShaderIO;
use graph_craft::{proto::ProtoNetwork, Type};
use serde::{Deserialize, Serialize};
use std::io::Write;
pub fn compile_spirv(network: &graph_craft::document::NodeNetwork, input_type: &str, output_type: &str, compile_dir: Option<&str>, manifest_path: &str) -> anyhow::Result<Vec<u8>> {
let serialized_graph = serde_json::to_string(&network)?;
pub fn compile_spirv(request: &CompileRequest, compile_dir: Option<&str>, manifest_path: &str) -> anyhow::Result<Vec<u8>> {
let serialized_graph = serde_json::to_string(&gpu_executor::CompileRequest {
network: request.network.clone(),
io: request.shader_io.clone(),
})?;
let features = "";
#[cfg(feature = "profiling")]
let features = "profiling";
@@ -19,9 +26,6 @@ pub fn compile_spirv(network: &graph_craft::document::NodeNetwork, input_type: &
.envs(non_cargo_env_vars)
.arg("--features")
.arg(features)
.arg("--")
.arg(input_type)
.arg(output_type)
// TODO: handle None case properly
.arg(compile_dir.unwrap())
.stdin(std::process::Stdio::piped())
@@ -38,16 +42,27 @@ pub fn compile_spirv(network: &graph_craft::document::NodeNetwork, input_type: &
#[derive(Serialize, Deserialize, Debug, Clone, PartialEq)]
pub struct CompileRequest {
network: graph_craft::document::NodeNetwork,
input_type: String,
output_type: String,
network: graph_craft::proto::ProtoNetwork,
input_types: Vec<Type>,
output_type: Type,
shader_io: ShaderIO,
}
impl CompileRequest {
pub fn new(network: graph_craft::document::NodeNetwork, input_type: String, output_type: String) -> Self {
Self { network, input_type, output_type }
pub fn new(network: ProtoNetwork, input_types: Vec<Type>, output_type: Type, io: ShaderIO) -> Self {
// TODO: add type checking
// for (input, buffer) in input_types.iter().zip(io.inputs.iter()) {
// assert_eq!(input, &buffer.ty());
// }
// assert_eq!(output_type, io.output.ty());
Self {
network,
input_types,
output_type,
shader_io: io,
}
}
pub fn compile(&self, compile_dir: &str, manifest_path: &str) -> anyhow::Result<Vec<u8>> {
compile_spirv(&self.network, &self.input_type, &self.output_type, Some(compile_dir), manifest_path)
compile_spirv(self, Some(compile_dir), manifest_path)
}
}

View File

@@ -1,3 +1,10 @@
[toolchain]
channel = "nightly-2022-12-18"
components = ["rust-src", "rustc-dev", "llvm-tools-preview", "clippy", "cargofmt", "rustc"]
channel = "nightly-2023-03-04"
components = [
"rust-src",
"rustc-dev",
"llvm-tools-preview",
"clippy",
"rustfmt",
"rustc",
]

View File

@@ -1,6 +1,8 @@
use std::path::Path;
use gpu_executor::{GPUConstant, ShaderIO, ShaderInput, SpirVCompiler};
use graph_craft::proto::*;
use graphene_core::Cow;
use std::path::{Path, PathBuf};
use tera::Context;
fn create_cargo_toml(metadata: &Metadata) -> Result<String, tera::Error> {
@@ -24,10 +26,10 @@ impl Metadata {
}
}
pub fn create_files(matadata: &Metadata, network: &ProtoNetwork, compile_dir: &Path, input_type: &str, output_type: &str) -> anyhow::Result<()> {
pub fn create_files(metadata: &Metadata, network: &ProtoNetwork, compile_dir: &Path, io: &ShaderIO) -> anyhow::Result<()> {
let src = compile_dir.join("src");
let cargo_file = compile_dir.join("Cargo.toml");
let cargo_toml = create_cargo_toml(matadata)?;
let cargo_toml = create_cargo_toml(metadata)?;
std::fs::write(cargo_file, cargo_toml)?;
let toolchain_file = compile_dir.join("rust-toolchain.toml");
@@ -44,26 +46,100 @@ pub fn create_files(matadata: &Metadata, network: &ProtoNetwork, compile_dir: &P
}
}
let lib = src.join("lib.rs");
let shader = serialize_gpu(network, input_type, output_type)?;
println!("{}", shader);
let shader = serialize_gpu(network, io)?;
eprintln!("{}", shader);
std::fs::write(lib, shader)?;
Ok(())
}
pub fn serialize_gpu(network: &ProtoNetwork, input_type: &str, output_type: &str) -> anyhow::Result<String> {
assert_eq!(network.inputs.len(), 1);
fn constant_attribute(constant: &GPUConstant) -> &'static str {
match constant {
GPUConstant::SubGroupId => "subgroup_id",
GPUConstant::SubGroupInvocationId => "subgroup_local_invocation_id",
GPUConstant::SubGroupSize => todo!(),
GPUConstant::NumSubGroups => "num_subgroups",
GPUConstant::WorkGroupId => "workgroup_id",
GPUConstant::WorkGroupInvocationId => "local_invocation_id",
GPUConstant::WorkGroupSize => todo!(),
GPUConstant::NumWorkGroups => "num_workgroups",
GPUConstant::GlobalInvocationId => "global_invocation_id",
GPUConstant::GlobalSize => todo!(),
}
}
pub fn construct_argument(input: &ShaderInput<()>, position: u32) -> String {
match input {
ShaderInput::Constant(constant) => format!("#[spirv({})] i{}: {},", constant_attribute(constant), position, constant.ty()),
ShaderInput::UniformBuffer(_, ty) => {
format!("#[spirv(uniform, descriptor_set = 0, binding = {})] i{}: &[{}]", position, position, ty,)
}
ShaderInput::StorageBuffer(_, ty) | ShaderInput::ReadBackBuffer(_, ty) => {
format!("#[spirv(storage_buffer, descriptor_set = 0, binding = {})] i{}: &[{}]", position, position, ty,)
}
ShaderInput::OutputBuffer(_, ty) => {
format!("#[spirv(storage_buffer, descriptor_set = 0, binding = {})] i{}: &mut[{}]", position, position, ty,)
}
ShaderInput::WorkGroupMemory(_, ty) => format!("#[spirv(workgroup_memory] i{}: {}", position, ty,),
}
}
struct GpuCompiler {
compile_dir: PathBuf,
}
impl SpirVCompiler for GpuCompiler {
fn compile(&self, network: ProtoNetwork, io: &ShaderIO) -> anyhow::Result<gpu_executor::Shader> {
let metadata = Metadata::new("project".to_owned(), vec!["test@example.com".to_owned()]);
create_files(&metadata, &network, &self.compile_dir, io)?;
let result = compile(&self.compile_dir)?;
let bytes = std::fs::read(result.module.unwrap_single())?;
let words = bytes.chunks(4).map(|chunk| u32::from_ne_bytes(chunk.try_into().unwrap())).collect::<Vec<_>>();
Ok(gpu_executor::Shader {
source: Cow::Owned(words),
name: "",
io: io.clone(),
})
}
}
pub fn serialize_gpu(network: &ProtoNetwork, io: &ShaderIO) -> anyhow::Result<String> {
fn nid(id: &u64) -> String {
format!("n{id}")
}
dbg!(&network);
dbg!(&io);
let inputs = io.inputs.iter().enumerate().map(|(i, input)| construct_argument(input, i as u32)).collect::<Vec<_>>();
let mut nodes = Vec::new();
let mut input_nodes = Vec::new();
#[derive(serde::Serialize)]
struct Node {
id: String,
fqn: String,
args: Vec<String>,
}
for id in network.inputs.iter() {
let Some((_, node)) = network.nodes.iter().find(|(i, _)| i == id) else {
anyhow::bail!("Input node not found");
};
let fqn = &node.identifier.name;
let id = nid(id);
input_nodes.push(Node {
id,
fqn: fqn.to_string().split("<").next().unwrap().to_owned(),
args: node.construction_args.new_function_args(),
});
}
for (ref id, node) in network.nodes.iter() {
if network.inputs.contains(id) {
continue;
}
let fqn = &node.identifier.name;
let id = nid(id);
@@ -78,8 +154,8 @@ pub fn serialize_gpu(network: &ProtoNetwork, input_type: &str, output_type: &str
let mut tera = tera::Tera::default();
tera.add_raw_template("spirv", template)?;
let mut context = Context::new();
context.insert("input_type", &input_type);
context.insert("output_type", &output_type);
context.insert("inputs", &inputs);
context.insert("input_nodes", &input_nodes);
context.insert("nodes", &nodes);
context.insert("last_node", &nid(&network.output));
context.insert("compute_threads", &64);
@@ -89,14 +165,15 @@ pub fn serialize_gpu(network: &ProtoNetwork, input_type: &str, output_type: &str
use spirv_builder::{MetadataPrintout, SpirvBuilder, SpirvMetadata};
pub fn compile(dir: &Path) -> Result<spirv_builder::CompileResult, spirv_builder::SpirvBuilderError> {
dbg!(&dir);
let result = SpirvBuilder::new(dir, "spirv-unknown-spv1.5")
let result = SpirvBuilder::new(dir, "spirv-unknown-vulkan1.2")
.print_metadata(MetadataPrintout::DependencyOnly)
.multimodule(false)
.preserve_bindings(true)
.release(true)
//.relax_struct_store(true)
//.relax_block_layout(true)
.spirv_metadata(SpirvMetadata::Full)
.extra_arg("no-early-report-zombies")
.extra_arg("no-infer-storage-classes")
.extra_arg("spirt-passes=qptr")
.build()?;
Ok(result)
@@ -104,7 +181,6 @@ pub fn compile(dir: &Path) -> Result<spirv_builder::CompileResult, spirv_builder
#[cfg(test)]
mod test {
#[test]
fn test_create_cargo_toml() {
let cargo_toml = super::create_cargo_toml(&super::Metadata {

View File

@@ -1,4 +1,5 @@
use gpu_compiler as compiler;
use gpu_executor::CompileRequest;
use graph_craft::document::NodeNetwork;
use std::io::Write;
@@ -6,17 +7,13 @@ fn main() -> anyhow::Result<()> {
println!("Starting GPU Compiler!");
let mut stdin = std::io::stdin();
let mut stdout = std::io::stdout();
let input_type = std::env::args().nth(1).expect("input type arg missing");
let output_type = std::env::args().nth(2).expect("output type arg missing");
let compile_dir = std::env::args().nth(3).map(|x| std::path::PathBuf::from(&x)).unwrap_or(tempfile::tempdir()?.into_path());
let network: NodeNetwork = serde_json::from_reader(&mut stdin)?;
let compiler = graph_craft::executor::Compiler {};
let proto_network = compiler.compile_single(network, true).unwrap();
let compile_dir = std::env::args().nth(1).map(|x| std::path::PathBuf::from(&x)).unwrap_or(tempfile::tempdir()?.into_path());
let request: CompileRequest = serde_json::from_reader(&mut stdin)?;
dbg!(&compile_dir);
let metadata = compiler::Metadata::new("project".to_owned(), vec!["test@example.com".to_owned()]);
compiler::create_files(&metadata, &proto_network, &compile_dir, &input_type, &output_type)?;
compiler::create_files(&metadata, &request.network, &compile_dir, &request.io)?;
let result = compiler::compile(&compile_dir)?;
let bytes = std::fs::read(result.module.unwrap_single())?;

View File

@@ -1,7 +1,7 @@
[package]
authors = [{% for author in authors %}"{{author}}", {% endfor %}]
name = "{{name}}-node"
version = "0.1.0"
authors = [{%for author in authors%}"{{author}}", {%endfor%}]
edition = "2021"
license = "MIT OR Apache-2.0"
publish = false
@@ -13,5 +13,7 @@ crate-type = ["dylib", "lib"]
libm = { git = "https://github.com/rust-lang/libm", tag = "0.2.5" }
[dependencies]
spirv-std = { version = "0.5" , features= ["glam"]}
graphene-core = {path = "{{gcore_path}}", default-features = false, features = ["gpu"]}
spirv-std = { version = "0.7" }
graphene-core = { path = "{{gcore_path}}", default-features = false, features = [
"gpu",
] }

View File

@@ -1,3 +1,10 @@
[toolchain]
channel = "nightly-2022-12-18"
components = ["rust-src", "rustc-dev", "llvm-tools-preview", "clippy", "cargofmt", "rustc"]
channel = "nightly-2023-03-04"
components = [
"rust-src",
"rustc-dev",
"llvm-tools-preview",
"clippy",
"rustfmt",
"rustc",
]

View File

@@ -1,6 +1,5 @@
#![no_std]
#![feature(unchecked_math)]
#![deny(warnings)]
#[cfg(target_arch = "spirv")]
extern crate spirv_std;
@@ -14,25 +13,23 @@ pub mod gpu {
#[allow(unused)]
#[spirv(compute(threads({{compute_threads}})))]
pub fn eval (
#[spirv(global_invocation_id)] global_id: UVec3,
#[spirv(storage_buffer, descriptor_set = 0, binding = 0)] a: &[{{input_type}}],
#[spirv(storage_buffer, descriptor_set = 0, binding = 1)] y: &mut [{{output_type}}],
//#[spirv(push_constant)] push_consts: &graphene_core::gpu::PushConstants,
{% for input in inputs %}
{{input}}
{% endfor %}
) {
let gid = global_id.x as usize;
// Only process up to n, which is the length of the buffers.
//if global_id.x < push_consts.n {
y[gid] = node_graph(a[gid]);
//}
}
fn node_graph(input: {{input_type}}) -> {{output_type}} {
use graphene_core::Node;
{% for input in input_nodes %}
let i{{loop.index0}} = graphene_core::value::CopiedNode::new(i{{loop.index0}});
let _{{input.id}} = {{input.fqn}}::new({% for arg in input.args %}{{arg}}, {% endfor %});
let {{input.id}} = graphene_core::structural::ComposeNode::new(i{{loop.index0}}, _{{input.id}});
{% endfor %}
{% for node in nodes %}
let {{node.id}} = {{node.fqn}}::new({% for arg in node.args %}{{arg}}, {% endfor %});
{% endfor %}
{{last_node}}.eval(input)
}
let output = {{last_node}}.eval(());
// TODO: Write output to buffer
}
}