Shader defines

Posted By: Slin

Shader defines - 05/13/13 18:26

It would be a big improvement, if there where lots of shader defines and shaders being compiled and used for the needed variations.
Things like fog, sun, number of lights effecting the object, alpha clipping, clipplanes, shadow receiver, animations and more.
This way one could write one "Übershader" assign it to all objects which are supposed to use it and it would just work for all different situations like a reflection view, indoor and outdoor levels and so on.
Sure one could do the same with branching, but with a noticeable performance hit.

It would also be great if there was a possibility not to clear the depth buffer before rendering some view, as for example in case of the PSSM shadows it should be a usefull performance gain to get rid of any overdraw by reusing the depth buffer.
Posted By: HeelX

Re: Shader defines - 05/13/13 21:07

Originally Posted By: Slin
It would be a big improvement, if there where lots of shader defines and shaders being compiled and used for the needed variations


I receive great workflow results with dynamically compiled shaders, based on the .fx code and a preamble, generated during runtime, used together with the following function:

Code:
MATERIAL* avCompileMtl (MATERIAL* m, char* file, char* preamble, char* technique)
{
    if (m == NULL)
        return NULL;
    
    if (technique != NULL)
        m->technique = technique;

    if (file != NULL)
    {
        int s = 0;
        void* c = file_load(file, NULL, &s);

        if (s != 0)
        {
            STRING* strShader = NULL;
            
            if (preamble != NULL)
            {
                strShader = str_create(preamble);
                
                str_cat(strShader, "\n");
                str_cat(strShader, c);
                
                effect_load(m, strShader);
            }
            else
                effect_load(m, c);
                
            file_load(NULL, c, NULL);
            
            if (strShader != NULL)
                ptr_remove(strShader);
        }
    }
            
    return m;
}



In your game infrastructure, you just need to create a function that evaluates certain information (like variables and flags) and generate a series of defines (separated with "\n") to generate a preamble, that you can use to structure your HLSL shader-code with #ifdef and #ifndef. You can even use this to dynamically re-arrange samplers, by adding e.g. "#define SKIN_SPEC entSkin3" to your preamble and use SKIN_SPEC in the sampler definition: sampler smpSpecular = sampler_state { Texture = < SKIN_SPEC >; };, whereas the string above is e.g. generated by an int and sprintf.

Originally Posted By: Slin
It would also be great if there was a possibility not to clear the depth buffer before rendering some view


Do you mean the NOSKY flag? I used it to render particles into a SSAO-shaded scene and it works really good.
© 2024 lite-C Forums