LYGIA Shader Library

derivative (lygia/sample/derivative)

sample derrivatives

Dependencies:

Use:

sampleDerivative(<SAMPLER_TYPE> tex, <vec2> st)

Check it on Github



#ifndef SAMPLERDERIVATIVE_FNC
#define SAMPLERDERIVATIVE_FNC(TEX, UV) SAMPLER_FNC(TEX, UV).r
#endif

#ifndef FNC_SAMPLEDERIVATIVE
#define FNC_SAMPLEDERIVATIVE
vec2 sampleDerivative(in SAMPLER_TYPE tex, in vec2 st, vec2 pixel) { 
    float p = SAMPLERDERIVATIVE_FNC(tex, st); 

    #if defined(SAMPLEDERRIVATIVE_DD)
    return -vec2(dFdx(p), dFdy(p));

    #elif defined(SAMPLEDERRIVATIVE_FAST)
    float h1 = SAMPLERDERIVATIVE_FNC(tex, st + vec2(pixel.x,0.0));
    float v1 = SAMPLERDERIVATIVE_FNC(tex, st + vec2(0.0,pixel.y));
    return (p - vec2(h1, v1));

    #else
    float center      = SAMPLERDERIVATIVE_FNC(tex, st);
    float topLeft     = SAMPLERDERIVATIVE_FNC(tex, st - pixel);
    float left        = SAMPLERDERIVATIVE_FNC(tex, st - vec2(pixel.x, .0));
    float bottomLeft  = SAMPLERDERIVATIVE_FNC(tex, st + vec2(-pixel.x, pixel.y));
    float top         = SAMPLERDERIVATIVE_FNC(tex, st - vec2(.0, pixel.y));
    float bottom      = SAMPLERDERIVATIVE_FNC(tex, st + vec2(.0, pixel.y));
    float topRight    = SAMPLERDERIVATIVE_FNC(tex, st + vec2(pixel.x, -pixel.y));
    float right       = SAMPLERDERIVATIVE_FNC(tex, st + vec2(pixel.x, .0));
    float bottomRight = SAMPLERDERIVATIVE_FNC(tex, st + pixel);

    float dX = topRight + 2. * right + bottomRight - topLeft - 2. * left - bottomLeft;
    float dY = bottomLeft + 2. * bottom + bottomRight - topLeft - 2. * top - topRight;

    return vec2(dX, dY);
    #endif
}
#endif

Dependencies:

Use:

sampleDerivative(<SAMPLER_TYPE> tex, <float2> st)

Check it on Github



#ifndef SAMPLERDERIVATIVE_FNC
#define SAMPLERDERIVATIVE_FNC(TEX, UV) SAMPLER_FNC(tex, UV).r
#endif

#ifndef FNC_SAMPLEDERIVATIVE
#define FNC_SAMPLEDERIVATIVE
float2 sampleDerivative(in SAMPLER_TYPE tex, in float2 st, float2 pixel) { 
    float p = SAMPLERDERIVATIVE_FNC(tex, st); 

    #if defined(SAMPLEDERRIVATIVE_DD)
    return -float2(ddx(p), ddy(p));

    #elif defined(SAMPLEDERRIVATIVE_FAST)
    float h1 = SAMPLERDERIVATIVE_FNC(tex, st + float2(pixel.x,0.0));
    float v1 = SAMPLERDERIVATIVE_FNC(tex, st + float2(0.0,pixel.y));
    return (p - float2(h1, v1));

    #else
    float center      = SAMPLERDERIVATIVE_FNC(tex, st);
    float topLeft     = SAMPLERDERIVATIVE_FNC(tex, st - pixel);
    float left        = SAMPLERDERIVATIVE_FNC(tex, st - float2(pixel.x, .0));
    float bottomLeft  = SAMPLERDERIVATIVE_FNC(tex, st + float2(-pixel.x, pixel.y));
    float top         = SAMPLERDERIVATIVE_FNC(tex, st - float2(.0, pixel.y));
    float bottom      = SAMPLERDERIVATIVE_FNC(tex, st + float2(.0, pixel.y));
    float topRight    = SAMPLERDERIVATIVE_FNC(tex, st + float2(pixel.x, -pixel.y));
    float right       = SAMPLERDERIVATIVE_FNC(tex, st + float2(pixel.x, .0));
    float bottomRight = SAMPLERDERIVATIVE_FNC(tex, st + pixel);

    float dX = topRight + 2. * right + bottomRight - topLeft - 2. * left - bottomLeft;
    float dY = bottomLeft + 2. * bottom + bottomRight - topLeft - 2. * top - topRight;

    return float2(dX, dY);
    #endif
}
#endif

Licenses

LYGIA is dual-licensed under the Prosperity License and the Patron License for sponsors and contributors.

Sponsors and contributors are automatically added to the Patron License and they can ignore the any non-commercial rule of the Prosperity Licensed software (please take a look to the exception).

It's also possible to get a permanent comercial license hook to a single and specific version of LYGIA.

Get the latest news and releases

Sign up for the news letter bellow, joing the LYGIA's channel on Discord or follow the Github repository