Jump to content

View more

Image of the Day

I've done more tweaks to my color palette selector. #gamedev #screenshotsaturday #madewithunity https://t.co/aJXrC4ruRg
IOTD | Top Screenshots

The latest, straight to your Inbox.

Subscribe to GameDev.net Direct to receive the latest updates and exclusive content.

Sign up now

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

4: Adsense

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 away2903   Members   


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?



#2 Kaptein   Prime Members   


Posted 19 December 2013 - 12:48 PM

It's very convenient. For example:



#version 330




layout(location = 0) in vec4 in_vertex;










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   


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.

http://tinyurl.com/shewonyay - Thanks so much for those who voted on my GF's Competition Cosplay Entry for Cosplayzine. She won! I owe you all beers :)

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

#4 away2903   Members   


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.