This commit is contained in:
Dennis Kobert 2023-06-27 22:37:24 +02:00 committed by Keavon Chambers
parent edb33e0c82
commit 1e52715d95
13 changed files with 174 additions and 110 deletions

View file

@ -43,7 +43,7 @@ async fn post_compile_spirv(State(state): State<Arc<AppState>>, Json(compile_req
let result = compile_request.compile(state.compile_dir.path().to_str().expect("non utf8 tempdir path"), &path).map_err(|e| {
eprintln!("compilation failed: {}", e);
StatusCode::INTERNAL_SERVER_ERROR
});
state.cache.write().unwrap().insert(compile_request, result.clone());
result
})?;
state.cache.write().unwrap().insert(compile_request, Ok(result.clone()));
Ok(result)
}

View file

@ -16,6 +16,17 @@ impl<T: Fn(I) -> O, I, O> FnNode<T, I, O> {
}
}
pub struct FnOnceNode<T: FnMut(I) -> O, I, O: Default>(T, PhantomData<(I, O)>);
impl<'i, T: FnMut(I) -> O + 'i, I: 'i, O: 'i + Default> FnOnceNode<T, I, O> {
pub fn new(f: T) -> Self {
FnOnceNode(f, PhantomData)
}
fn eval(&'i mut self, input: I) -> O {
self.0(input)
}
}
pub struct FnNodeWithState<'i, T: Fn(I, &'i State) -> O, I, O, State: 'i>(T, State, PhantomData<(&'i O, I)>);
impl<'i, I: 'i, O: 'i, State, T: Fn(I, &'i State) -> O + 'i> Node<'i, I> for FnNodeWithState<'i, T, I, O, State> {
type Output = O;

View file

@ -44,7 +44,7 @@ use core::any::TypeId;
pub use raster::Color;
// pub trait Node: for<'n> NodeIO<'n> {
pub trait Node<'i, Input: 'i>: 'i {
pub trait Node<'i, Input: 'i>: 'i + NodeMut<'i, Input, MutOutput = Self::Output> {
type Output: 'i;
fn eval(&'i self, input: Input) -> Self::Output;
fn reset(&self) {}
@ -55,6 +55,32 @@ pub trait Node<'i, Input: 'i>: 'i {
}
}
pub trait NodeMut<'i, Input: 'i>: 'i {
type MutOutput: 'i;
fn eval_mut(&'i mut self, input: Input) -> Self::MutOutput;
}
pub trait NodeOnce<'i, Input>
where
Input: 'i,
{
type OnceOutput: 'i;
fn eval_once(self, input: Input) -> Self::OnceOutput;
}
impl<'i, T: Node<'i, I>, I: 'i> NodeOnce<'i, I> for &'i T {
type OnceOutput = T::Output;
fn eval_once(self, input: I) -> Self::OnceOutput {
(self).eval(input)
}
}
impl<'i, T: Node<'i, I> + ?Sized, I: 'i> NodeMut<'i, I> for T {
type MutOutput = T::Output;
fn eval_mut(&'i mut self, input: I) -> Self::MutOutput {
(*self).eval(input)
}
}
#[cfg(feature = "alloc")]
mod types;
#[cfg(feature = "alloc")]
@ -98,52 +124,40 @@ where
{
}
impl<'i, 's: 'i, I: 'i, O: 'i, N: Node<'i, I, Output = O>> Node<'i, I> for &'s N {
impl<'i, 's: 'i, I: 'i, N: Node<'i, I> + ?Sized> Node<'i, I> for &'i N {
type Output = N::Output;
fn eval(&'i self, input: I) -> N::Output {
(*self).eval(input)
}
}
#[cfg(feature = "alloc")]
impl<'i, 's: 'i, I: 'i, O: 'i, N: Node<'i, I, Output = O> + ?Sized> Node<'i, I> for Box<N> {
type Output = O;
fn eval(&'i self, input: I) -> Self::Output {
fn eval(&'i self, input: I) -> O {
(**self).eval(input)
}
}
#[cfg(feature = "alloc")]
impl<'i, 's: 'i, I: 'i, O: 'i, N: Node<'i, I, Output = O>> Node<'i, I> for Box<N> {
impl<'i, 's: 'i, I: 'i, O: 'i, N: Node<'i, I, Output = O> + ?Sized> Node<'i, I> for alloc::sync::Arc<N> {
type Output = O;
fn eval(&'i self, input: I) -> Self::Output {
(**self).eval(input)
}
}
#[cfg(feature = "alloc")]
impl<'i, 's: 'i, I: 'i, O: 'i, N: Node<'i, I, Output = O>> Node<'i, I> for alloc::sync::Arc<N> {
type Output = O;
fn eval(&'i self, input: I) -> Self::Output {
fn eval(&'i self, input: I) -> O {
(**self).eval(input)
}
}
impl<'i, I: 'i, O: 'i> Node<'i, I> for &'i dyn Node<'i, I, Output = O> {
type Output = O;
fn eval(&'i self, input: I) -> Self::Output {
(**self).eval(input)
}
}
use core::pin::Pin;
use dyn_any::StaticTypeSized;
#[cfg(feature = "alloc")]
impl<'i, I: 'i, O: 'i> Node<'i, I> for Pin<Box<dyn Node<'i, I, Output = O> + 'i>> {
impl<'i, I: 'i, O: 'i> Node<'i, I> for Pin<Box<dyn Node<'i, I, Output = O, MutOutput = O> + 'i>> {
type Output = O;
fn eval(&'i self, input: I) -> Self::Output {
fn eval(&'i self, input: I) -> O {
(**self).eval(input)
}
}
impl<'i, I: 'i, O: 'i> Node<'i, I> for Pin<&'i (dyn NodeIO<'i, I, Output = O> + 'i)> {
impl<'i, I: 'i, O: 'i> Node<'i, I> for Pin<&'i (dyn NodeIO<'i, I, Output = O, MutOutput = O> + 'i)> {
type Output = O;
fn eval(&'i self, input: I) -> Self::Output {
fn eval(&'i self, input: I) -> O {
(**self).eval(input)
}
}

View file

@ -21,7 +21,7 @@ where
// TODO: This should return a reference to the cached cached_value
// but that requires a lot of lifetime magic <- This was suggested by copilot but is pretty acurate xD
type Output = Pin<Box<dyn Future<Output = T> + 'i>>;
fn eval(&'i self, input: ()) -> Self::Output {
fn eval(&'i self, input: ()) -> Pin<Box<dyn Future<Output = T> + 'i>> {
Box::pin(async move {
if let Some(cached_value) = self.cache.take() {
self.cache.set(Some(cached_value.clone()));

View file

@ -1,58 +1,69 @@
use crate::raster::Color;
use crate::Node;
use bytemuck::{Pod, Zeroable};
use dyn_any::{DynAny, StaticType};
#[cfg(target_arch = "spirv")]
use spirv_std::num_traits::Float;
#[derive(Clone, Debug, DynAny, PartialEq)]
#[derive(Clone, Copy, Debug, DynAny, PartialEq, Pod, Zeroable)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
#[repr(C)]
pub struct Quantization {
pub fn_index: usize,
pub a: f32,
pub b: f32,
pub c: f32,
pub d: f32,
pub b_and_bits: u32,
}
impl Quantization {
pub fn a(&self) -> f32 {
self.a
}
pub fn b(&self) -> i32 {
(self.b_and_bits >> 16) as i32
}
pub fn bits(&self) -> u32 {
self.b_and_bits & 0xFF
}
}
impl core::hash::Hash for Quantization {
fn hash<H: core::hash::Hasher>(&self, state: &mut H) {
self.fn_index.hash(state);
self.a.to_bits().hash(state);
self.b.to_bits().hash(state);
self.c.to_bits().hash(state);
self.d.to_bits().hash(state);
self.bits().hash(state);
self.a().to_bits().hash(state);
self.b().hash(state);
}
}
impl Default for Quantization {
fn default() -> Self {
Self {
fn_index: Default::default(),
a: 1.,
b: Default::default(),
c: Default::default(),
d: Default::default(),
}
Self { a: 1., b_and_bits: 8 }
}
}
pub type QuantizationChannels = [Quantization; 4];
#[repr(transparent)]
#[derive(DynAny, Clone, Copy, Debug, PartialEq, Eq, Pod, Zeroable)]
pub struct PackedPixel(u32);
fn quantize(value: f32, quantization: &Quantization) -> f32 {
let Quantization { fn_index, a, b, c, d } = quantization;
match fn_index {
1 => ((value + a) * d).abs().ln() * b + c,
_ => a * value + b,
}
#[inline(always)]
fn quantize(value: f32, offset: u32, quantization: &Quantization) -> u32 {
let a = quantization.a();
let bits = quantization.bits();
let b = quantization.b();
let value = (((a * value) * (1 << bits) as f32) as i32 + b as i32) as u32;
value << (32 - bits - offset)
}
fn decode(value: f32, quantization: &Quantization) -> f32 {
let Quantization { fn_index, a, b, c, d } = quantization;
match fn_index {
1 => -(-c / b).exp() * (a * d * (c / b).exp() - (value / b).exp()) / d,
_ => (value - b) / a,
}
#[inline(always)]
fn decode(value: u32, offset: u32, quantization: &Quantization) -> f32 {
let a = quantization.a();
let bits = quantization.bits();
let b = quantization.b();
let value = (value << offset) >> (32 - bits);
let value = value as i32 - b;
(value as f32 / (1 << bits) as f32) / a
}
pub struct QuantizeNode<Quantization> {
@ -60,14 +71,18 @@ pub struct QuantizeNode<Quantization> {
}
#[node_macro::node_fn(QuantizeNode)]
fn quantize_fn<'a>(color: Color, quantization: [Quantization; 4]) -> Color {
let quant = quantization.as_slice();
let r = quantize(color.r(), &quant[0]);
let g = quantize(color.g(), &quant[1]);
let b = quantize(color.b(), &quant[2]);
let a = quantize(color.a(), &quant[3]);
fn quantize_fn<'a>(color: Color, quantization: [Quantization; 4]) -> PackedPixel {
let quant = quantization;
let mut offset = 0;
let r = quantize(color.r(), offset, &quant[0]);
offset += quant[0].bits();
let g = quantize(color.g(), offset, &quant[1]);
offset += quant[1].bits();
let b = quantize(color.b(), offset, &quant[2]);
offset += quant[2].bits();
let a = quantize(color.a(), offset, &quant[3]);
Color::from_rgbaf32_unchecked(r, g, b, a)
PackedPixel(r | g | b | a)
}
pub struct DeQuantizeNode<Quantization> {
@ -75,12 +90,16 @@ pub struct DeQuantizeNode<Quantization> {
}
#[node_macro::node_fn(DeQuantizeNode)]
fn dequantize_fn<'a>(color: Color, quantization: [Quantization; 4]) -> Color {
let quant = quantization.as_slice();
let r = decode(color.r(), &quant[0]);
let g = decode(color.g(), &quant[1]);
let b = decode(color.b(), &quant[2]);
let a = decode(color.a(), &quant[3]);
fn dequantize_fn<'a>(color: PackedPixel, quantization: [Quantization; 4]) -> Color {
let quant = quantization;
let mut offset = 0;
let r = decode(color.0, offset, &quant[0]);
offset += quant[0].bits();
let g = decode(color.0, offset, &quant[1]);
offset += quant[1].bits();
let b = decode(color.0, offset, &quant[2]);
offset += quant[2].bits();
let a = decode(color.0, offset, &quant[3]);
Color::from_rgbaf32_unchecked(r, g, b, a)
}

View file

@ -2,7 +2,7 @@ use core::marker::PhantomData;
use crate::Node;
#[derive(Clone)]
#[derive(Clone, Copy)]
pub struct ComposeNode<First, Second, I> {
first: First,
second: Second,

View file

@ -216,10 +216,10 @@ pub fn compile(dir: &Path) -> Result<spirv_builder::CompileResult, spirv_builder
.release(true)
.spirv_metadata(SpirvMetadata::Full)
.capability(spirv_builder::Capability::Float64)
//.extra_arg("no-early-report-zombies")
//.extra_arg("no-infer-storage-classes")
//.extra_arg("spirt-passes=qptr,reduce")
.extra_arg("spirt-passes=reduce")
//.capability(spirv_builder::Capability::VariablePointersStorageBuffer)
.extra_arg("no-early-report-zombies")
.extra_arg("no-infer-storage-classes")
.extra_arg("spirt-passes=qptr")
.build()?;
Ok(result)

View file

@ -23,13 +23,11 @@ extern crate spirv_std;
use graphene_core::raster::adjustments::{BlendMode, BlendNode};
use graphene_core::Color;
/*
{% for input in input_nodes %}
let i{{input.index}} = graphene_core::value::CopiedNode::new(i{{input.index}});
let _i{{input.index}} = graphene_core::value::CopiedNode::new(i{{input.index}});
let _{{input.id}} = {{input.fqn}}::new({% for arg in input.args %}{{arg}}, {% endfor %});
let {{input.id}} = graphene_core::structural::ComposeNode::new(i{{input.index}}, _{{input.id}});
let {{input.id}} = graphene_core::structural::ComposeNode::new(_i{{input.index}}, _{{input.id}});
{% endfor %}
*/
{% for node in nodes %}
let {{node.id}} = {{node.fqn}}::new({% for arg in node.args %}{{arg}}, {% endfor %});
@ -37,7 +35,7 @@ extern crate spirv_std;
{% for output in output_nodes %}
let v = {{output}}.eval(());
o{{loop.index0}}[(_global_index.y * i0 + _global_index.x) as usize] = v;
//o{{loop.index0}}[(_global_index.y * i1 + _global_index.x) as usize] = v;
{% endfor %}
// TODO: Write output to buffer
}

View file

@ -19,11 +19,11 @@ pub type DynFuture<'n, T> = Pin<Box<dyn core::future::Future<Output = T> + 'n>>;
pub type LocalFuture<'n, T> = Pin<Box<dyn core::future::Future<Output = T> + 'n>>;
pub type Any<'n> = Box<dyn DynAny<'n> + 'n>;
pub type FutureAny<'n> = DynFuture<'n, Any<'n>>;
pub type TypeErasedNode<'n> = dyn for<'i> NodeIO<'i, Any<'i>, Output = FutureAny<'i>> + 'n;
pub type TypeErasedPinnedRef<'n> = Pin<&'n (dyn for<'i> NodeIO<'i, Any<'i>, Output = FutureAny<'i>> + 'n)>;
pub type TypeErasedRef<'n> = &'n (dyn for<'i> NodeIO<'i, Any<'i>, Output = FutureAny<'i>> + 'n);
pub type TypeErasedBox<'n> = Box<dyn for<'i> NodeIO<'i, Any<'i>, Output = FutureAny<'i>> + 'n>;
pub type TypeErasedPinned<'n> = Pin<Box<dyn for<'i> NodeIO<'i, Any<'i>, Output = FutureAny<'i>> + 'n>>;
pub type TypeErasedNode<'n> = dyn for<'i> NodeIO<'i, Any<'i>, Output = FutureAny<'i>, MutOutput = FutureAny<'i>> + 'n;
pub type TypeErasedPinnedRef<'n> = Pin<&'n TypeErasedNode<'n>>;
pub type TypeErasedRef<'n> = &'n TypeErasedNode<'n>;
pub type TypeErasedBox<'n> = Box<TypeErasedNode<'n>>;
pub type TypeErasedPinned<'n> = Pin<Box<TypeErasedNode<'n>>>;
pub type NodeConstructor = for<'a> fn(Vec<Arc<NodeContainer>>) -> DynFuture<'static, TypeErasedBox<'static>>;

View file

@ -44,7 +44,7 @@ async fn main() -> Result<(), Box<dyn Error>> {
let device = application_io.gpu_executor().unwrap().context.device.clone();
std::thread::spawn(move || loop {
std::thread::sleep(std::time::Duration::from_nanos(1));
std::thread::sleep(std::time::Duration::from_nanos(10));
device.poll(wgpu::Maintain::Poll);
});

View file

@ -4,6 +4,7 @@ use gpu_executor::{GpuExecutor, ShaderIO, ShaderInput};
use graph_craft::document::value::TaggedValue;
use graph_craft::document::*;
use graph_craft::proto::*;
use graphene_core::quantization::{PackedPixel, QuantizationChannels};
use graphene_core::raster::*;
use graphene_core::*;
use wgpu_executor::WgpuExecutor;
@ -112,15 +113,27 @@ async fn create_compute_pass_descriptor(node: DocumentNode, image: &ImageFrame<C
log::debug!("inner_network: {:?}", inner_network);
let network = NodeNetwork {
inputs: vec![], //vec![0, 1],
outputs: vec![NodeOutput::new(1, 0)],
inputs: vec![1, 2], //vec![0, 1],
outputs: vec![NodeOutput::new(6, 0)],
nodes: [
DocumentNode {
name: "Slice".into(),
inputs: vec![NodeInput::Inline(InlineRust::new("i1[(_global_index.y * i0 + _global_index.x) as usize]".into(), concrete![Color]))],
inputs: vec![NodeInput::Inline(InlineRust::new("i2[(_global_index.y * i1 + _global_index.x) as usize]".into(), concrete![Color]))],
implementation: DocumentNodeImplementation::Unresolved("graphene_core::value::CopiedNode".into()),
..Default::default()
},
DocumentNode {
name: "Quantization".into(),
inputs: vec![NodeInput::Network(concrete!(quantization::Quantization))],
implementation: DocumentNodeImplementation::Unresolved("graphene_core::ops::CloneNode".into()),
..Default::default()
},
DocumentNode {
name: "Width".into(),
inputs: vec![NodeInput::Network(concrete!(u32))],
implementation: DocumentNodeImplementation::Unresolved("graphene_core::ops::IdNode".into()),
..Default::default()
},
/*DocumentNode {
name: "Index".into(),
//inputs: vec![NodeInput::Network(concrete!(UVec3))],
@ -135,26 +148,36 @@ async fn create_compute_pass_descriptor(node: DocumentNode, image: &ImageFrame<C
implementation: DocumentNodeImplementation::Unresolved("graphene_core::storage::GetNode".into()),
..Default::default()
},*/
DocumentNode {
name: "Dequantize".into(),
inputs: vec![NodeInput::node(0, 0), NodeInput::node(1, 0)],
implementation: DocumentNodeImplementation::proto("graphene_core::quantization::DeQuantizeNode"),
..Default::default()
},
DocumentNode {
name: "MapNode".into(),
inputs: vec![NodeInput::node(0, 0)],
inputs: vec![NodeInput::node(3, 0)],
implementation: DocumentNodeImplementation::Network(inner_network),
..Default::default()
},
/*
DocumentNode {
name: "Quantize".into(),
inputs: vec![NodeInput::node(4, 0), NodeInput::node(1, 0)],
implementation: DocumentNodeImplementation::proto("graphene_core::quantization::QuantizeNode"),
..Default::default()
},
DocumentNode {
name: "SaveNode".into(),
inputs: vec![
//NodeInput::node(0, 0),
NodeInput::node(5, 0),
NodeInput::Inline(InlineRust::new(
"o0[_global_index.x as usize] = i0[_global_index.x as usize]".into(),
"move |x| o0[(_global_index.y * i1 + _global_index.x) as usize] = x".into(),
Type::Fn(Box::new(concrete!(Color)), Box::new(concrete!(()))),
)),
],
implementation: DocumentNodeImplementation::Unresolved("graphene_core::value::ValueNode".into()),
implementation: DocumentNodeImplementation::Unresolved("graphene_core::generic::FnOnceNode".into()),
..Default::default()
},
*/
]
.into_iter()
.enumerate()
@ -171,12 +194,13 @@ async fn create_compute_pass_descriptor(node: DocumentNode, image: &ImageFrame<C
vec![concrete!(Color)],
ShaderIO {
inputs: vec![
ShaderInput::UniformBuffer((), concrete!(quantization::QuantizationChannels)),
ShaderInput::UniformBuffer((), concrete!(u32)),
ShaderInput::StorageBuffer((), concrete!(Color)),
ShaderInput::StorageBuffer((), concrete!(PackedPixel)),
//ShaderInput::Constant(gpu_executor::GPUConstant::GlobalInvocationId),
ShaderInput::OutputBuffer((), concrete!(Color)),
ShaderInput::OutputBuffer((), concrete!(PackedPixel)),
],
output: ShaderInput::OutputBuffer((), concrete!(Color)),
output: ShaderInput::OutputBuffer((), concrete!(PackedPixel)),
},
)
.await
@ -202,6 +226,7 @@ async fn create_compute_pass_descriptor(node: DocumentNode, image: &ImageFrame<C
return frame;*/
log::debug!("creating buffer");
let width_uniform = executor.create_uniform_buffer(image.image.width).unwrap();
let quantization_uniform = executor.create_uniform_buffer(QuantizationChannels::default()).unwrap();
let storage_buffer = executor
.create_storage_buffer(
image.image.data.clone(),

View file

@ -7,8 +7,7 @@ use dyn_any::DynAny;
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 {
fn eval(&'n self) -> &'n (dyn DynAny<'n>) {
let value: &O = self.0.eval();
value
}
@ -22,8 +21,7 @@ impl<'n, N: Node<'n, Output = &'n O>, O: 'n + ?Sized> AnyRefNode<'n, N> {
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 {
fn eval(&'n self) -> &'n (dyn DynAny<'n>) {
self.0.eval()
}
}
@ -36,7 +34,6 @@ impl<'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
}

View file

@ -1,7 +1,7 @@
use graph_craft::imaginate_input::{ImaginateCache, ImaginateController, ImaginateMaskStartingFill, ImaginateSamplingMethod};
use graph_craft::proto::{NodeConstructor, TypeErasedBox};
use graphene_core::ops::IdNode;
use graphene_core::quantization::QuantizationChannels;
use graphene_core::quantization::{QuantizationChannels, PackedPixel};
use graphene_core::raster::brush_cache::BrushCache;
use graphene_core::raster::color::Color;
@ -513,8 +513,8 @@ fn node_registry() -> HashMap<NodeIdentifier, HashMap<NodeIOTypes, NodeConstruct
register_node!(graphene_std::raster::ImageFrameNode<_, _>, input: Image<Color>, params: [DAffine2]),
#[cfg(feature = "quantization")]
register_node!(graphene_std::quantization::GenerateQuantizationNode<_, _>, input: ImageFrame<Color>, params: [u32, u32]),
raster_node!(graphene_core::quantization::QuantizeNode<_>, params: [QuantizationChannels]),
raster_node!(graphene_core::quantization::DeQuantizeNode<_>, params: [QuantizationChannels]),
register_node!(graphene_core::quantization::QuantizeNode<_>, input: Color, params: [QuantizationChannels]),
register_node!(graphene_core::quantization::DeQuantizeNode<_>, input: PackedPixel, params: [QuantizationChannels]),
register_node!(graphene_core::ops::CloneNode<_>, input: &QuantizationChannels, params: []),
register_node!(graphene_core::transform::TransformNode<_, _, _, _, _>, input: VectorData, params: [DVec2, f32, DVec2, DVec2, DVec2]),
register_node!(graphene_core::transform::TransformNode<_, _, _, _, _>, input: ImageFrame<Color>, params: [DVec2, f32, DVec2, DVec2, DVec2]),