opengl proper using of program

Started by
6 comments, last by Shaarigan 7 years, 11 months ago
If I have some shader functions to draw single meshes with large number of vertices, some brushes with gradient colors, some particles, should I put these functions in different render passes in a single program or in different programs? will switch programs will do much damage to performce?
Advertisement

Try to reduce to some minimum or too many programs will hurt your performance.

+ There's a limit to it (Find the constant that defines it).

It would be good to split different effects into different shaders.

If you have similar effects, try to build one shader for all of them.

ok, thanks very much!

You could also link shaders together after compiling them in modern OpenGL (didnt try it for a version lower thant 4.3). In general shaders can (and propably should) only act on one object at a time. If you have particle system you shouldnt use the same shader for a static mesh.

In GL you compile the shader first then attach it to the program, you could use that to build a couple of shaders for the needs of the object rendered with where same kind of objects should use same shader but keep in mind to have only one main per shader type (vertes, fragment and so on). Getting the program object means linking compiled shader assemblies to one single assembly so you even could resolve functions used but not declared in the source.

For example


//fragment
#pragma version 450

vec4 colorComponent;
out vec4 Color;

void main()
{
  #ifdef USE_TEXTURE_UNIT_0
  ProcessTextureUnit_0(colorComponent);
  #endif
  #ifdef USE_TEXTURE_UNIT_1
  ProcessTextureUnit_1(colorComponent);
  #endif
  #ifdef USE_TEXTURE_UNIT_2
  ProcessTextureUnit_2(colorComponent);
  #endif
  #ifdef USE_COLOR_COMPONENT
  ProcessColor(colorComponent);
  #endif

  Color = colorComponent;
}

And in your ColorShader have


#pragma version 450

in vec3 BaseColor;

void ProcessColor(inout vec4 colorComponent)
{
  colorComponent =
  {
     colorComponent.x * BaseColor.x,
     colorComponent.y * BaseColor.y,
     colorComponent.z * BaseColor.z,
     colorComponent.w
  };
}

Or you use one single shader with all #ifdef-blocks as done in Unreal Engine 3 and compile justs etting the flags right

You could also link shaders together after compiling them in modern OpenGL (didnt try it for a version lower thant 4.3). In general shaders can (and propably should) only act on one object at a time. If you have particle system you shouldnt use the same shader for a static mesh.

In GL you compile the shader first then attach it to the program, you could use that to build a couple of shaders for the needs of the object rendered with where same kind of objects should use same shader but keep in mind to have only one main per shader type (vertes, fragment and so on). Getting the program object means linking compiled shader assemblies to one single assembly so you even could resolve functions used but not declared in the source.

For example


//fragment
#pragma version 450

vec4 colorComponent;
out vec4 Color;

void main()
{
  #ifdef USE_TEXTURE_UNIT_0
  ProcessTextureUnit_0(colorComponent);
  #endif
  #ifdef USE_TEXTURE_UNIT_1
  ProcessTextureUnit_1(colorComponent);
  #endif
  #ifdef USE_TEXTURE_UNIT_2
  ProcessTextureUnit_2(colorComponent);
  #endif
  #ifdef USE_COLOR_COMPONENT
  ProcessColor(colorComponent);
  #endif

  Color = colorComponent;
}

And in your ColorShader have


#pragma version 450

in vec3 BaseColor;

void ProcessColor(inout vec4 colorComponent)
{
  colorComponent =
  {
     colorComponent.x * BaseColor.x,
     colorComponent.y * BaseColor.y,
     colorComponent.z * BaseColor.z,
     colorComponent.w
  };
}

Or you use one single shader with all #ifdef-blocks as done in Unreal Engine 3 and compile justs etting the flags right

sorry for the late reply... :(

but in this way I have to compile many program objects and switch between them during runtime. if so, will it damage performance of thr program?

The problem isnt compiling many program objects because you first need to take care that you unify your materials. Dont create a new material for an object that uses color and one that dosent but has similar properties and is rendered similar rather than setting a default color value for white for example. There arent that many kinds of rendering normaly like the few standard shaders used in mostly every game (specular, bumped, diffuse and so on) and a few for special advantages like particle system and post processing so there might be not even 100 shader programs you need to compile.

When you take the link different kinds of shaders together method then you still have one single program object per shader and also minimize the shader objects created because you compile the code for each shader object only once and link them together in any way to the program object by attaching the handle returned by GL to your created program handle and then call the linker.

Performance is lost when switching shaders during render step so you need to build your render pipeline to minimize switching by sorting your objects either by shader and then by texture or vise versa like I described here

http://www.gamedev.net/topic/678502-best-way-to-abstract-shaders-in-a-small-engine/#entry5290755

sorry but I still dont get it...I mean if I have a fragment shader subroutine that use a texture and another subroutine that uses only vertex color, how can I merge these two into one in order to minimize states changes? seems impossible to me

So your problem is more logical or technical?

You know what the #ifdef Tag does in GLSL?

I just take the above example as base for this so lets think you have again


//fragment
#pragma version 450

vec4 colorComponent;
out vec4 Color;

void main()
{
  #ifdef USE_TEXTURE_UNIT_0
  ProcessTextureUnit_0(colorComponent);
  #endif
  #ifdef USE_TEXTURE_UNIT_1
  ProcessTextureUnit_1(colorComponent);
  #endif
  #ifdef USE_TEXTURE_UNIT_2
  ProcessTextureUnit_2(colorComponent);
  #endif
  #ifdef USE_COLOR_COMPONENT
  ProcessColor(colorComponent);
  #endif

  Color = colorComponent;
}

as your main Shader linking to the other components and an example component


#pragma version 450

in vec3 BaseColor;

void ProcessColor(inout vec4 colorComponent)
{
  colorComponent =
  {
     colorComponent.x * BaseColor.x,
     colorComponent.y * BaseColor.y,
     colorComponent.z * BaseColor.z,
     colorComponent.w
  };
}

So you want the color fragment shader enabled then you need to first pass the color shader component to the compiler by call


GLuint fragmentShader = glCreateShader(GL_FRAGMENT_SHADER);
glShaderSource(fragmentShader, 1, &ColorComponentSource, 0);
glCompileShader(fragmentShader);

glGetShaderiv(fragmentShader, GL_COMPILE_STATUS, &isCompiled);
if(isCompiled == GL_FALSE)
{
  //throw error here
  return;
}

YourShaderManager.StoreComponent(fragmentShader, YOUR_COLOR_SHADER_COMPONENT_ID)

You never discard that shader until your program leaves. After that you pass the compiler flags to glsl and your 'main' shader


GLuint fragmentShader = glCreateShader(GL_FRAGMENT_SHADER);

const char* defines = "#define USE_COLOR_COMPONENT 1\n"; //add defines here seperated by \n
const char* sources[2] = { defines, FragmentMainSource };
glShaderSource(fragmentShader, 2, sources, 0);
glCompileShader(fragmentShader);

glGetShaderiv(fragmentShader, GL_COMPILE_STATUS, &isCompiled);
if(isCompiled == GL_FALSE)
{
  //throw error here
  return;
}

GLuint program = glCreateProgram();
/**
  Dont forget vertex stuff here ;)
*/
glAttachShader(program, YourShaderManager.GetComponent(YOUR_COLOR_SHADER_COMPONENT_ID));
glAttachShader(program, fragmentShader);

glLinkProgram(program);

(I have not checked this for syntax corectness)

and this will result in your fragment shader because the code will look in a way like this to the linker


<colorShader assembly>

in vec3 BaseColor;

void ProcessColor(inout vec4 colorComponent)
{
  colorComponent =
  {
    colorComponent.x * BaseColor.x,
    colorComponent.y * BaseColor.y,
    colorComponent.z * BaseColor.z,
    colorComponent.w
  };
}

<fragmentShader assembly>
#define USE_COLOR_COMPONENT 1

vec4 colorComponent;
out vec4 Color;

void main()
{
  #ifdef USE_TEXTURE_UNIT_0
  <ignored>
  #endif
  #ifdef USE_TEXTURE_UNIT_1
  <ignored>
  #endif
  #ifdef USE_TEXTURE_UNIT_2
  <ignored>
  #endif
  #ifdef USE_COLOR_COMPONENT
  ProcessColor(colorComponent);
  <not defined here but linker will find in previous assembly>
  #endif

  Color = colorComponent;
}

And the program will finaly look like this


in vec3 BaseColor;

void ProcessColor(inout vec4 colorComponent)
{
  colorComponent =
  {
    colorComponent.x * BaseColor.x,
    colorComponent.y * BaseColor.y,
    colorComponent.z * BaseColor.z,
    colorComponent.w
  };
}

vec4 colorComponent;
out vec4 Color;

void main()
{
  ProcessColor(colorComponent);
  Color = colorComponent;
}

Or depending on the optimizer of your GPU vendor


in vec3 BaseColor;
out vec4 Color;

void main()
{
  Color =
  {
    colorComponent.x * BaseColor.x,
    colorComponent.y * BaseColor.y,
    colorComponent.z * BaseColor.z,
    colorComponent.w
  };
}

I hope that will help you

This topic is closed to new replies.

Advertisement