Fix formatting of previous commit

This commit is contained in:
Keavon Chambers 2022-08-04 00:18:42 -07:00
parent d09f023618
commit 0c2dbd411b
11 changed files with 226 additions and 234 deletions

View file

@ -3,7 +3,7 @@ name = "graphene-std"
version = "0.1.0"
edition = "2021"
description = "Graphene standard library"
authors = ["Dennis Kobert <dennis@kobert.dev>"]
authors = ["Graphite Authors <contact@graphite.rs>"]
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
@ -22,11 +22,11 @@ dyn-any = {path = "../../libraries/dyn-any", features = ["derive"]}
graph-proc-macros = {path = "../proc-macro", optional = true}
once_cell = {version= "1.10", optional = true}
ide = { version = "*", package = "ra_ap_ide", optional = true }
ide_db = { version = "*", package = "ra_ap_ide_db" , optional = true }
ide_db = { version = "*", package = "ra_ap_ide_db", optional = true }
storage-map = { version = "*", optional = true }
lock_api = { version= "*", optional = true }
parking_lot = { version = "*", optional = true }
#pretty-token-stream = {path = "../../pretty-token-stream"}
syn = {version = "1.0", default-features = false, features = ["parsing", "printing"]}
proc-macro2 = {version = "1.0", default-features = false, features = ["proc-macro"]}
syn = {version = "1.0", default-features = false, features = ["parsing", "printing"]}
proc-macro2 = {version = "1.0", default-features = false, features = ["proc-macro"]}
quote = {version = "1.0", default-features = false }

View file

@ -1,13 +1,13 @@
use parking_lot::RawRwLock;
use std::{
any::Any,
borrow::Borrow,
cell::RefCell,
collections::{hash_map::DefaultHasher, HashMap},
hash::{Hash, Hasher},
iter,
iter::Sum,
marker::PhantomData,
any::Any,
borrow::Borrow,
cell::RefCell,
collections::{hash_map::DefaultHasher, HashMap},
hash::{Hash, Hasher},
iter,
iter::Sum,
marker::PhantomData,
};
use storage_map::{StorageMap, StorageMapGuard};
@ -16,33 +16,29 @@ use graphene_api::{DynamicInput, Node};
/// Caches the output of a given Node and acts as a proxy
/// Automatically resets if it receives different input
pub struct SmartCacheNode<'n, 'c, NODE: Node + 'c> {
node: &'n NODE,
map: StorageMap<RawRwLock, HashMap<u64, CacheNode<'n, 'c, NODE>>>,
node: &'n NODE,
map: StorageMap<RawRwLock, HashMap<u64, CacheNode<'n, 'c, NODE>>>,
}
impl<'n: 'c, 'c, NODE: Node + 'c> Node for SmartCacheNode<'n, 'c, NODE>
where
for<'a> NODE::Input<'a>: Hash,
for<'a> NODE::Input<'a>: Hash,
{
type Input<'a> = NODE::Input<'a> where Self: 'a, 'c : 'a;
type Output<'a> = StorageMapGuard<'a, RawRwLock, CacheNode<'n, 'c, NODE>> where Self: 'a, 'c: 'a;
fn eval<'a, I: Borrow<Self::Input<'a>>>(&'a self, input: I) -> Self::Output<'a> {
let mut hasher = DefaultHasher::new();
input.borrow().hash(&mut hasher);
let hash = hasher.finish();
type Input<'a> = NODE::Input<'a> where Self: 'a, 'c : 'a;
type Output<'a> = StorageMapGuard<'a, RawRwLock, CacheNode<'n, 'c, NODE>> where Self: 'a, 'c: 'a;
fn eval<'a, I: Borrow<Self::Input<'a>>>(&'a self, input: I) -> Self::Output<'a> {
let mut hasher = DefaultHasher::new();
input.borrow().hash(&mut hasher);
let hash = hasher.finish();
self.map
.get_or_create_with(&hash, || CacheNode::new(self.node))
}
self.map.get_or_create_with(&hash, || CacheNode::new(self.node))
}
}
impl<'n, 'c, NODE: Node> SmartCacheNode<'n, 'c, NODE> {
pub fn clear(&'n mut self) {
self.map = StorageMap::default();
}
pub fn new(node: &'n NODE) -> SmartCacheNode<'n, 'c, NODE> {
SmartCacheNode {
node,
map: StorageMap::default(),
}
}
pub fn clear(&'n mut self) {
self.map = StorageMap::default();
}
pub fn new(node: &'n NODE) -> SmartCacheNode<'n, 'c, NODE> {
SmartCacheNode { node, map: StorageMap::default() }
}
}

View file

@ -13,8 +13,8 @@ pub type DynNode<'n, T> = &'n (dyn Node<'n, Output = T> + 'n);
pub type DynAnyNode<'n> = &'n (dyn Node<'n, Output = &'n dyn DynAny<'n>> + 'n);
pub trait DynamicInput<'n> {
fn set_kwarg_by_name(&mut self, name: &str, value: DynAnyNode<'n>);
fn set_arg_by_index(&mut self, index: usize, value: DynAnyNode<'n>);
fn set_kwarg_by_name(&mut self, name: &str, value: DynAnyNode<'n>);
fn set_arg_by_index(&mut self, index: usize, value: DynAnyNode<'n>);
}
use quote::quote;
@ -25,89 +25,89 @@ use syn::{Expr, ExprPath, Type};
/// on the gpu an fn node is constructed that takes a value
/// node as input
pub struct NodeGraph {
/// Collection of nodes with their corresponding inputs.
/// The first node always always has to be an Input Node.
pub nodes: Vec<NodeKind>,
pub output: Type,
pub input: Type,
/// Collection of nodes with their corresponding inputs.
/// The first node always always has to be an Input Node.
pub nodes: Vec<NodeKind>,
pub output: Type,
pub input: Type,
}
pub enum NodeKind {
Value(Expr),
Input,
Node(ExprPath, Vec<usize>),
Value(Expr),
Input,
Node(ExprPath, Vec<usize>),
}
impl NodeGraph {
pub fn serialize_function(&self) -> proc_macro2::TokenStream {
let output_type = &self.output;
let input_type = &self.input;
pub fn serialize_function(&self) -> proc_macro2::TokenStream {
let output_type = &self.output;
let input_type = &self.input;
fn nid(id: &usize) -> syn::Ident {
let str = format!("n{id}");
syn::Ident::new(str.as_str(), proc_macro2::Span::call_site())
}
let mut nodes = Vec::new();
for (ref id, node) in self.nodes.iter().enumerate() {
let id = nid(id).clone();
let line = match node {
NodeKind::Value(val) => {
quote! {let #id = graphene_core::value::ValueNode::new(#val);}
}
NodeKind::Node(node, ids) => {
let ids = ids.iter().map(nid).collect::<Vec<_>>();
quote! {let #id = #node::new((#(&#ids),*));}
}
NodeKind::Input => {
quote! { let n0 = graphene_core::value::ValueNode::new(input);}
}
};
nodes.push(line)
}
let last_id = self.nodes.len() - 1;
let last_id = nid(&last_id);
let ret = quote! { #last_id.eval() };
let function = quote! {
fn node_graph(input: #input_type) -> #output_type {
#(#nodes)*
#ret
}
};
function
}
pub fn serialize_gpu(&self, name: &str) -> proc_macro2::TokenStream {
let function = self.serialize_function();
let output_type = &self.output;
let input_type = &self.input;
fn nid(id: &usize) -> syn::Ident {
let str = format!("n{id}");
syn::Ident::new(str.as_str(), proc_macro2::Span::call_site())
}
let mut nodes = Vec::new();
for (ref id, node) in self.nodes.iter().enumerate() {
let id = nid(id).clone();
let line = match node {
NodeKind::Value(val) => {
quote! {let #id = graphene_core::value::ValueNode::new(#val);}
}
NodeKind::Node(node, ids) => {
let ids = ids.iter().map(nid).collect::<Vec<_>>();
quote! {let #id = #node::new((#(&#ids),*));}
}
NodeKind::Input => {
quote! { let n0 = graphene_core::value::ValueNode::new(input);}
}
};
nodes.push(line)
}
let last_id = self.nodes.len() - 1;
let last_id = nid(&last_id);
let ret = quote! { #last_id.eval() };
let function = quote! {
fn node_graph(input: #input_type) -> #output_type {
#(#nodes)*
#ret
}
};
function
}
pub fn serialize_gpu(&self, name: &str) -> proc_macro2::TokenStream {
let function = self.serialize_function();
let output_type = &self.output;
let input_type = &self.input;
quote! {
#[cfg(target_arch = "spirv")]
pub mod gpu {
//#![deny(warnings)]
#[repr(C)]
pub struct PushConsts {
n: u32,
node: u32,
}
use super::*;
quote! {
#[cfg(target_arch = "spirv")]
pub mod gpu {
//#![deny(warnings)]
#[repr(C)]
pub struct PushConsts {
n: u32,
node: u32,
}
use super::*;
use spirv_std::glam::UVec3;
use spirv_std::glam::UVec3;
#[allow(unused)]
#[spirv(compute(threads(64)))]
pub fn #name(
#[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: &PushConsts,
) {
#function
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]);
}
}
}
}
}
#[allow(unused)]
#[spirv(compute(threads(64)))]
pub fn #name(
#[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: &PushConsts,
) {
#function
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]);
}
}
}
}
}
}

View file

@ -4,36 +4,36 @@ use std::marker::PhantomData;
/// Caches the output of a given Node and acts as a proxy
pub struct CacheNode<'n, CachedNode: Node<'n>> {
node: CachedNode,
cache: OnceCell<CachedNode::Output>,
_phantom: PhantomData<&'n ()>,
node: CachedNode,
cache: OnceCell<CachedNode::Output>,
_phantom: PhantomData<&'n ()>,
}
impl<'n, CashedNode: Node<'n>> Node<'n> for CacheNode<'n, CashedNode>
where
CashedNode::Output: 'n,
CashedNode::Output: 'n,
{
type Output = &'n CashedNode::Output;
fn eval(&'n self) -> Self::Output {
self.cache.get_or_init(|| self.node.eval())
}
type Output = &'n CashedNode::Output;
fn eval(&'n self) -> Self::Output {
self.cache.get_or_init(|| self.node.eval())
}
}
impl<'n, CachedNode: Node<'n>> CacheNode<'n, CachedNode> {
pub fn clear(&'n mut self) {
self.cache = OnceCell::new();
}
pub fn new(node: CachedNode) -> CacheNode<'n, CachedNode> {
CacheNode {
node,
cache: OnceCell::new(),
_phantom: PhantomData,
}
}
pub fn clear(&'n mut self) {
self.cache = OnceCell::new();
}
pub fn new(node: CachedNode) -> CacheNode<'n, CachedNode> {
CacheNode {
node,
cache: OnceCell::new(),
_phantom: PhantomData,
}
}
}
impl<'n, CachedNode: Node<'n>> Cache for CacheNode<'n, CachedNode> {
fn clear(&mut self) {
self.cache = OnceCell::new();
}
fn clear(&mut self) {
self.cache = OnceCell::new();
}
}
/*use dyn_any::{DynAny, StaticType};

View file

@ -7,43 +7,43 @@ use dyn_any::{DynAny, StaticType, StaticTypeSized};
pub struct AnyRefNode<'n, N: Node<'n>>(N, PhantomData<&'n ()>);
impl<'n, N: Node<'n, Output = &'n O>, O: DynAny<'n> + 'n> Node<'n> for AnyRefNode<'n, N> {
type Output = &'n (dyn DynAny<'n>);
fn eval(&'n self) -> Self::Output {
let value: &O = self.0.eval();
value
}
type Output = &'n (dyn DynAny<'n>);
fn eval(&'n self) -> Self::Output {
let value: &O = self.0.eval();
value
}
}
impl<'n, N: Node<'n, Output = &'n O>, O: 'n + ?Sized> AnyRefNode<'n, N> {
pub fn new(n: N) -> AnyRefNode<'n, N> {
AnyRefNode(n, PhantomData)
}
pub fn new(n: N) -> AnyRefNode<'n, N> {
AnyRefNode(n, PhantomData)
}
}
pub struct StorageNode<'n>(&'n dyn Node<'n, Output = &'n dyn DynAny<'n>>);
impl<'n> Node<'n> for StorageNode<'n> {
type Output = &'n (dyn DynAny<'n>);
fn eval(&'n self) -> Self::Output {
self.0.eval()
}
type Output = &'n (dyn DynAny<'n>);
fn eval(&'n self) -> Self::Output {
self.0.eval()
}
}
impl<'n> StorageNode<'n> {
pub fn new<N: Node<'n, Output = &'n dyn DynAny<'n>>>(n: &'n N) -> StorageNode<'n> {
StorageNode(n)
}
pub fn new<N: Node<'n, Output = &'n dyn DynAny<'n>>>(n: &'n N) -> StorageNode<'n> {
StorageNode(n)
}
}
#[derive(Default)]
pub struct AnyValueNode<'n, T>(T, PhantomData<&'n ()>);
impl<'n, T: 'n + DynAny<'n>> Node<'n> for AnyValueNode<'n, T> {
type Output = &'n dyn DynAny<'n>;
fn eval(&'n self) -> &'n dyn DynAny<'n> {
&self.0
}
type Output = &'n dyn DynAny<'n>;
fn eval(&'n self) -> &'n dyn DynAny<'n> {
&self.0
}
}
impl<'n, T> AnyValueNode<'n, T> {
pub const fn new(value: T) -> AnyValueNode<'n, T> {
AnyValueNode(value, PhantomData)
}
pub const fn new(value: T) -> AnyValueNode<'n, T> {
AnyValueNode(value, PhantomData)
}
}