TheFuseLab / VL.Fuse

A library for visually programming on the GPU, built to enable rapid workflows and modular approaches to accelerated graphics, logic and computation.
https://www.thefuselab.io
MIT License
262 stars 21 forks source link

Better way to handle Code Delegates using Mixins #4

Open texone opened 3 years ago

texone commented 3 years ago

We want to find a better way to handle delegates without strange templating syntax prefarebly by using mixins here is a proposal in shader code.

First write an abstract shader class that is used

shader AbstractColor
{

    abstract float4 colorValue();

    float4 computeColor(){
        return colorValue() / 4;
    }
};

Fuse will generate an internal shader with the implementation

shader ColorImplementation : AbstractColor
{

    float4 colorValue(){
        return float4(0.0,1.0,1.0,1.0);
    }
};

This will be placed into the final generated shader with composition

shader MyColor_DrawFX : VS_PS_Base
{

    [Color]
    float4 Color = float4(1, 0, 0, 1);

    ColorImplementation colorImp; 

    override stage void VSMain()
    {
        streams.ShadingPosition = mul(streams.Position, WorldViewProjection);
    }

    ColorImplementation colorImp; 

    override stage void PSMain() 
    {
        streams.Depth = 1;
        streams.ColorTarget = colorImp.computeColor();
    }
};

This could also be used to already define delegate functions in the mixin like this

shader AbstractColor
{

    abstract float4 colorValue();

    float4 colorRed(){
        return float4(1,0,0,1);
    }

    float4 colorBlue(){
        return float4(0,0,1,1);
    }

    float4 computeColor(){
        return colorValue() / 4;
    }
};

Fuse would use one of the provides functions

shader ColorImplementation : AbstractColor
{

    float4 colorValue(){
        return colorBlue();
    }
};
everyoneishappy commented 3 years ago

Would be great to have a better way to handle this- the current workarounds are limited and stop the same function being swapped out more then once: https://github.com/TheFuseLab/Fuse/commit/7555dafaf5dcba8ac29f748d79bb996e5dafa712

If the higher function is defined in the template system rather then from mixin then this will work better, but it's not really scalable or modular for the sdsl files like that.

everyoneishappy commented 3 years ago

Just pinging this to highlight for discussion

everyoneishappy commented 3 years ago

https://github.com/vvvv/VL.Stride/blob/preview/gamma-2021.4/packages/VL.Stride.Runtime/src/Rendering/Effects/EffectShaderNodes.Utils.cs#L171

https://github.com/vvvv/VL.Stride/blob/preview/gamma-2021.4/packages/VL.Stride.Runtime/src/Rendering/Effects/EffectShaderNodes.ShaderFX.cs