Skip to content

Using dFdx? #135

Answered by pragma37
aVersionOfReality asked this question in Q&A
Sep 21, 2021 · 1 comments · 2 replies
Discussion options

You must be logged in to vote

The problem is that Malt uses the same source code for vertex and pixel shaders.
This is more convenient for the most part, but it also means that using some built-in GLSL functions requires some extra care.

Derivative functions are only available from pixel shaders, so to use them you have to make sure the code is only present in the generated pixel shader code:

vec3 partial_derivative_x(vec3 v)
{
    #ifdef PIXEL_SHADER
    {
        return dFdx(v);
    }
    #else
    {
        return vec3(0);
    }
    #endif
}

Another workaround could be to just define them as macros on vertex shaders. That way the compiler won't complain:

#ifndef PIXEL_SHADER
#define dFdx(a) (a)
#define dFdy(a) (a)
#…

Replies: 1 comment 2 replies

Comment options

You must be logged in to vote
2 replies
@aVersionOfReality
Comment options

@pragma37
Comment options

Answer selected by aVersionOfReality
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants