Jump to content

  • Log In with Google      Sign In   
  • Create Account


Shaders (GLSL) in one file? Is it practical?


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
3 replies to this topic

#1 mychii   Members   -  Reputation: 403

Like
0Likes
Like

Posted 19 December 2013 - 12:04 PM

Hi, I'm currently playing around with WebGL, which of course, like OpenGL, deal with shaders. Since web always download files, I started to think that I better combine both vertex and fragment shaders into one file so I can just request that file as one shader program entirely.

 

Do you think it's practical? Is there any possibility that either shader importantly/needed to be shared among each other?

 

Thanks.



Sponsor:

#2 Kaptein   Prime Members   -  Reputation: 1945

Like
3Likes
Like

Posted 19 December 2013 - 12:48 PM

It's very convenient. For example:

#define FRAGMENT_PROGRAM

#define VERTEX_PROGRAM

#version 330

 

#ifdef VERTEX_PROGRAM

 

layout(location = 0) in vec4 in_vertex;

...

 

#endif

#ifdef FRAGMENT_PROGRAM

 

...

 

#endif

 

That makes loading the file relatively simple, since you just run it through a function that spits out both vertex and fragment:

One string without the first define, and the second without the other. The rest of the files contents are still present in both files, but the shader compiler will compile only the relevant parts. Take care though if you are compiling shaders during rendering. In that case you will want to pay special attention to the length of the shader code.

 

The benefit of doing this, is reducing the number of files you need to keep track of, and in the webserver case, the files you need to transfer

The contents in both are related in that the vertex program sends data to the fragment program. The transferred data you use in the fragment shader must come from matching out statements on the vertex program.

 

I used to separate the files previously, and it was OK. I just got to the point where I had alot of shaders, and it helped abit to reduce number of files.

You do lose the ability to combine common vertex programs with specialized shaders, such as the generic fullscreen quad vertex shader.


Edited by Kaptein, 19 December 2013 - 01:22 PM.


#3 Karsten_   Members   -  Reputation: 1543

Like
1Likes
Like

Posted 20 December 2013 - 04:56 AM

As Kaptein suggested, as you load the file into your program, you just prepend the correct ifdef before the shader source code before sending it to OpenGL.

Another way of doing this could be as follows.

char *sources[2] = { "#version 330\n#define VERTEX_PROGRAM\n", sourceFromFile };
glShaderSourceARB(shader, 2, sources, NULL);

This basically send the define to OpenGL just before the rest of the shader source.


Edited by Karsten_, 20 December 2013 - 05:10 AM.

Mutiny - Open-source C++ Unity re-implementation.
Defile of Eden 2 - FreeBSD and OpenBSD binaries of our latest game.


#4 mychii   Members   -  Reputation: 403

Like
0Likes
Like

Posted 20 December 2013 - 11:16 AM

Thanks for the replies guys! :)






Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS