Skip to main content
added 241 characters in body
Source Link
concept3d
  • 12.8k
  • 4
  • 46
  • 57

Use conditional shader compilation! Well, to explain what I mean:

You can write one shader, that calculate several things, and pass to it a preprocessor token. The shader will compile based on what tokens were passed. This is similar to preprocessor directive we used for a long time in C:

#ifdef WIN32
//do this
#else 
//do that
#endif

The same could be done with any shading language,(unless you are using precompiled binary shaders).

vec3 CalculateTexture(){ ... }

vec3 CalcualteLight() { ... }

void main()
{
#ifdef CALC_TEXTURE
vec3 CalculateTexture();
#endif

#ifdef CALC_LIGHT 
vec3 CalcualteLight();
#endif
//rest of shader code
}

You can pre-pend tokens to your shader source and compile conditionally, for example if you pass #define CALC_TEXTURE this will only compile the part between the appropriate compiler preprocessor.

This is a common technique called Uber-shader, it will let you re-use your shader code by passing the appropriate tokens each time.

I am familiar with this technique in OpenGL, actually the reason glShaderSource takes array of strings is that you can pre-pend tokens to shader source.

void glShaderSource(GLuint shader,
                    GLsizei count,
                    const GLchar **string,
                    const GLint *length);

Am not sure how this is done in DirectX (as am not familiar with it's api), but because HLSL supports preprocessor directives I am sure it's straightforward to be done.

Use conditional shader compilation! Well, to explain what I mean:

You can write one shader, that calculate several things, and pass to it a preprocessor token. The shader will compile based on what tokens were passed. This is similar to preprocessor directive we used for a long time in C:

#ifdef WIN32
//do this
#else 
//do that
#endif

The same could be done with any shading language,(unless you are using precompiled binary shaders).

vec3 CalculateTexture(){ ... }

vec3 CalcualteLight() { ... }

void main()
{
#ifdef CALC_TEXTURE
vec3 CalculateTexture();
#endif

#ifdef CALC_LIGHT 
vec3 CalcualteLight();
#endif
//rest of shader code
}

You can pre-pend tokens to your shader source and compile conditionally, this will let you re-use your shader code by passing the appropriate tokens each time.

I am familiar with this technique in OpenGL, actually the reason glShaderSource takes array of strings is that you can pre-pend tokens to shader source.

void glShaderSource(GLuint shader,
                    GLsizei count,
                    const GLchar **string,
                    const GLint *length);

Am not sure how this is done in DirectX (as am not familiar with it's api), but because HLSL supports preprocessor directives I am sure it's straightforward to be done.

Use conditional shader compilation! Well, to explain what I mean:

You can write one shader, that calculate several things, and pass to it a preprocessor token. The shader will compile based on what tokens were passed. This is similar to preprocessor directive we used for a long time in C:

#ifdef WIN32
//do this
#else 
//do that
#endif

The same could be done with any shading language,(unless you are using precompiled binary shaders).

vec3 CalculateTexture(){ ... }

vec3 CalcualteLight() { ... }

void main()
{
#ifdef CALC_TEXTURE
vec3 CalculateTexture();
#endif

#ifdef CALC_LIGHT 
vec3 CalcualteLight();
#endif
//rest of shader code
}

You can pre-pend tokens to your shader source and compile conditionally for example if you pass #define CALC_TEXTURE this will only compile the part between the appropriate compiler preprocessor.

This is a common technique called Uber-shader, it will let you re-use your shader code by passing the appropriate tokens each time.

I am familiar with this technique in OpenGL, actually the reason glShaderSource takes array of strings is that you can pre-pend tokens to shader source.

void glShaderSource(GLuint shader,
                    GLsizei count,
                    const GLchar **string,
                    const GLint *length);

Am not sure how this is done in DirectX (as am not familiar with it's api), but because HLSL supports preprocessor directives I am sure it's straightforward to be done.

added 61 characters in body
Source Link
concept3d
  • 12.8k
  • 4
  • 46
  • 57

Use conditional shader compilation! Well, to explain what I mean:

You can write one shader, that calculate several things, and pass to it a preprocessor token. The shader will compile based on what tokens were passed. This is similar to preprocessor directive we used for a long time in C:

#ifdef WIN32
//do this
#else 
//do that
#endif

The same cancould be done with shaders any shading language,(unless you are using precompiled binary shaders).

vec3 CalculateTexture(){ ... }

vec3 CalcualteLight() { ... }

void main()
{
#ifdef CALC_TEXTURE
vec3 CalculateTexture();
#endif

#ifdef CALC_LIGHT 
vec3 CalcualteLight();
#endif
//rest of shader code
}

You can pre-pend tokens to your shader source and compile conditionally, this will let you re-use your shader code by passing the appropriate tokens each time. 

I am familairfamiliar with this technique in OpenGL, actually the reason glShaderSource takes array of strings is that you can pre-pend tokens to shader source.

void glShaderSource(GLuint shader,
                    GLsizei count,
                    const GLchar **string,
                    const GLint *length);

Am not sure how this is done in DirectX (as am not familiar with it's api), but because HLSL supports preprocessor directives I am sure it's straightforward to be done.

Use conditional shader compilation! Well, to explain what I mean:

You can write one shader, that calculate several things, and pass to it a preprocessor token. The shader will compile based on what tokens were passed. This is similar to preprocessor directive we used for a long time in C:

#ifdef WIN32
//do this
#else 
//do that
#endif

The same can be done with shaders (unless you are using precompiled binary shaders). You can pre-pend tokens to your shader source and compile conditionally. I am familair with this technique in OpenGL, actually the reason glShaderSource takes array of strings is that you can pre-pend tokens to shader source.

void glShaderSource(GLuint shader,
                    GLsizei count,
                    const GLchar **string,
                    const GLint *length);

Am not sure how this is done in DirectX (as am not familiar with it's api), but because HLSL supports preprocessor directives I am sure it's straightforward to be done.

Use conditional shader compilation! Well, to explain what I mean:

You can write one shader, that calculate several things, and pass to it a preprocessor token. The shader will compile based on what tokens were passed. This is similar to preprocessor directive we used for a long time in C:

#ifdef WIN32
//do this
#else 
//do that
#endif

The same could be done with any shading language,(unless you are using precompiled binary shaders).

vec3 CalculateTexture(){ ... }

vec3 CalcualteLight() { ... }

void main()
{
#ifdef CALC_TEXTURE
vec3 CalculateTexture();
#endif

#ifdef CALC_LIGHT 
vec3 CalcualteLight();
#endif
//rest of shader code
}

You can pre-pend tokens to your shader source and compile conditionally, this will let you re-use your shader code by passing the appropriate tokens each time. 

I am familiar with this technique in OpenGL, actually the reason glShaderSource takes array of strings is that you can pre-pend tokens to shader source.

void glShaderSource(GLuint shader,
                    GLsizei count,
                    const GLchar **string,
                    const GLint *length);

Am not sure how this is done in DirectX (as am not familiar with it's api), but because HLSL supports preprocessor directives I am sure it's straightforward to be done.

Source Link
concept3d
  • 12.8k
  • 4
  • 46
  • 57

Use conditional shader compilation! Well, to explain what I mean:

You can write one shader, that calculate several things, and pass to it a preprocessor token. The shader will compile based on what tokens were passed. This is similar to preprocessor directive we used for a long time in C:

#ifdef WIN32
//do this
#else 
//do that
#endif

The same can be done with shaders (unless you are using precompiled binary shaders). You can pre-pend tokens to your shader source and compile conditionally. I am familair with this technique in OpenGL, actually the reason glShaderSource takes array of strings is that you can pre-pend tokens to shader source.

void glShaderSource(GLuint shader,
                    GLsizei count,
                    const GLchar **string,
                    const GLint *length);

Am not sure how this is done in DirectX (as am not familiar with it's api), but because HLSL supports preprocessor directives I am sure it's straightforward to be done.