Skip to main content
GameDev.net gamedev.net
🔒 Locked

Multiple Fragment Shaders to Same Program

Started by Psychopathetica Jun 12, 2019 at 7:41 PM 4 replies 6.6k views
Original Post
Psychopathetica
Psychopathetica

Hey there. I am trying to simplify one of my massively sized fragment shaders to a much simpler form but without success by having multiple smaller fragment shaders communicate with one another. Right now in its current state, it has ambient lighting, diffuse lighting, specular lighting, texture mapping, reflection, and cubemapping, maybe a few other things, all bundled up into one enormous fragment shader. And its actually required because of how I designed the program rather than make a bunch of different programs to handle it. For example, if the surface of the texture has reflective properties, I can have it as an option programmably. So my idea is to link one vertex shader, and multiple individual fragment shaders to handle each of the cases in its own glsl file:

- Ambient lighting

- Diffuse lighting

- Specular lighting

- Reflection

... and so on. But the problem is, is that linking more than one fragment shader to the shader program gave me a glAttachShader error even though the program continued to run. I kinda got it working with just 2 fragment shaders but it didnt work at all with 3 or more. So i tried Googling solutions. One solution was to create a method in one shader to call into the main fragment shader. Tried it, and it did not read the method at all. Even with pre processor directives attached. Tried working with ins and outs with 3 or more fragment shaders but it only seemed to work with 2. Honestly I cant find any info on how to simplify complex shaders. Does anyone here have any ideas? Thanks in advance!

_Silence_
_Silence_

This is possible with OpenGL, with limitations, read this. For GL ES, you might have similar information, or you can dig into the official specs on Khronos website.

_WeirdCat_
_WeirdCat_

You can always pharse the shader file before loading it to the GPU

so having some vector math functions in file math_shader.glsl

i do

#include "path_to_the_shader/math_shader.glsl"


then before loading to gpu i add contnents to the main shader

ofc deleteing #include directive cause it not a valid glsl code

(see section 2 - second loop)


AnsiString LoadData(AnsiString fname, TStringList * cmd_list)
{
	TStringList s;
	s.LoadFromFile(fname);
	
	//first apply dynamic consts
	if (cmd_list != 0)
	{
		for (int i=0; i < cmd_list->Count; i++)
		{
			AnsiString var_name = get_before_char(cmd_list->Strings[i], ",", true );
			AnsiString var_value = get_after_char(cmd_list->Strings[i], ",", true );
		
		for (int n=0; n<s.Count;n++)
		if (Pos(var_name, s.Strings[n]) > 0)
		s.Strings[n] = StringReplace(s.Strings[n], var_name, var_value);
		
		
		}
		
	}
	
	
	
	
	bool found = true;
//	if (Pos("form", fname)>0) ALOG(s.GetText());
//parse file for #include directives and remove them	
while (found)
for (int i=0; i<s.Count;i++)
{
	found = false;
	if (Pos("#include", s.Strings[i])>0)
	{
		TStringList p;
		AnsiString fn = get_text_between("\"", "\"", s.Strings[i]);
		ALOG("Including: "+fn);
	if (	p.LoadFromFile(fn)== -1) ALOG("bad filename");

		if (p.Count > 0)
		{
			s.Strings[i] = "";
			int iline = i+1;
			for (int l=0; l<p.Count;l++)
			{
				s.Strings.insert(s.Strings.begin()+iline, p.Strings[l]);
				s.Count = s.Count+1;
				iline = iline+1;
			}
			
			found = true;

			break;
		}
	}
	
}
	
	
	return s.GetText();
	
}




Psychopathetica
Psychopathetica

Hmmm I could try that when I get home. Only thing is that it is in Java since it is an Android app. Maybe GLSL can accept include hashtags or even pragma includes.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.