Jump to content
  • Advertisement
Sign in to follow this  
Medo Mex

Managing Shaders in Game Engine

This topic is 1073 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

The idea that I was thinking about is that I could write a shader function inside a file for each effect that I want to accomplish, for example:
bloom.hlsl
blur.hlsl
light.hlsl
etc...
 
bloom.hlsl will contain a function like the following:
float4 bloom()
{
     // Code here for bloom here...
}
 
and I will be having a global shader which will contain something like the following;
#ifdef STRUCT_POSITION_TEXCOORD
// Code here to define structure for position and texture coord
#endif

#ifdef STRUCT_POSITION_COLOR
// Code here to define structure for position and color
#endif

#ifdef STRUCT_POSITION_TEXCOORD_NORMAL
// Code here to define structure for position, texture coordinate and normal
#endif

// And so on...


PS_IN VertexShader(VS_IN input)
{
// Code here for global vertex shader...

#ifdef STRUCT_POSITION_TEXCOORD
output.texcoord = input.texcoord;
#endif

#ifdef STRUCT_POSITION_TEXCOORD_NORMAL
output.texcoord = input.texcoord;
output.normal = input.normal;
#endif

// And so on...
}

float4 PixelShader(PS_IN input)
{
     return COLOR_VAR;
}
 
Now, when the game load, I will load global shader into char* and then replace COLOR_VAR with the appropriate function names, example:
shaderCode = replace("COLOR_VAR, "blur(bloom(color))", shaderCode);
 
and then prepend the used function to the beginning of the global shader and then compile the shader.
 
Now we have a shader that we can use.
 
Is that a good way for managing shaders in the game engine?
 
What's your suggestions?
Edited by Medo3337

Share this post


Link to post
Share on other sites
Advertisement
It sounds like a good/ workable approach.
I don't have enough expierence to be fully qualified, but I believe it's a good idea to make a choice between shader per material versus shader per object. With additionally shaders that apply (additional) based on other critera, for example for special effects, post processing for everything "on screen" etc.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!