Jump to content
  • Advertisement
Sign in to follow this  
sunsflower

OpenGL opengl proper using of program

This topic is 882 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

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?

Share this post


Link to post
Share on other sites
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. 

Share this post


Link to post
Share on other sites

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

Share this post


Link to post
Share on other sites

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?

Share this post


Link to post
Share on other sites

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

Edited by Shaarigan

Share this post


Link to post
Share on other sites

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

Share this post


Link to post
Share on other sites

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

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.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!