Archive 17/01/2023.

Using structs in shaders

franck22000

Hello, is it possible to do some refractoring in shaders system to use structs instead of that:

void VS(float4 iPos : POSITION, #ifndef BILLBOARD float3 iNormal : NORMAL, #endif #ifndef NOUV float2 iTexCoord : TEXCOORD0, #endif ... ...

That would be much more nicer and easier to write shaders :slight_smile:

codingmonkey

Yes it possible )
at first of all you will need going to Shader.cpp and fix it in CommentOutFunction

bool Shader::BeginLoad(Deserializer& source)
{
    Graphics* graphics = GetSubsystem<Graphics>();
    if (!graphics)
        return false;

    // Load the shader source code and resolve any includes
    timeStamp_ = 0;
    String shaderCode;
    if (!ProcessSource(shaderCode, source))
        return false;

    // Comment out the unneeded shader function
    vsSourceCode_ = shaderCode;
    psSourceCode_ = shaderCode;
    //CommentOutFunction(vsSourceCode_, "void PS(");
    //CommentOutFunction(psSourceCode_, "void VS(");
    CommentOutFunction(vsSourceCode_, "PS_OUTPUT PS(");
    CommentOutFunction(psSourceCode_, "VS_OUTPUT VS(");

    // OpenGL: rename either VS() or PS() to main()
#ifdef URHO3D_OPENGL
    vsSourceCode_.Replace("void VS(", "void main(");
    psSourceCode_.Replace("void PS(", "void main(");
#endif

    RefreshMemoryUse();
    return true;
}

and then you are free to use structs in shaders
little example:

[pastebin]pjRA4tZ5[/pastebin]

actually I rewrite most of them in this style (except some part of post effect shaders), but now I want do some refactoring with names
because the Output or Input it’s very long names(IMO), instead these names I want to change it to OUT and IN
also I checking this shaders on DX9 by running std urho’s examples and it seams that all run OK even on DX9

codingmonkey

there is fixed names(OUT/IN) version.
github.com/MonkeyFirst/urho3d-s … 11-structs

#includes...

#ifdef COMPILEVS

struct VS_INPUT
{
    //vars
};

struct VS_OUTPUT
{
    //vars
};

VS_OUTPUT VS(VS_INPUT IN)
{
    VS_OUTPUT OUT;
    //code
    return OUT;
}
#endif

#ifdef COMPILEPS

struct PS_INPUT
{
    //vars
};

struct PS_OUTPUT
{
    //vars
};

PS_OUTPUT PS(PS_INPUT IN)
{
    PS_OUTPUT OUT;
    //code
    return OUT;
}
#endif