VL.Fuse icon indicating copy to clipboard operation
VL.Fuse copied to clipboard

Better way to handle Code Delegates using Mixins

Open texone opened this issue 4 years ago • 3 comments

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();
    }
};

texone avatar Feb 07 '21 20:02 texone

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 avatar Mar 19 '21 06:03 everyoneishappy

Just pinging this to highlight for discussion

everyoneishappy avatar Jul 16 '21 03:07 everyoneishappy

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

everyoneishappy avatar Sep 01 '21 10:09 everyoneishappy