Shader Comments and Special Characters

BlitzMax Forums/OpenGL Module/Shader Comments and Special Characters

BLaBZ(Posted 2012) [#1]
Hello!

I've entered the wonderful world of shaders and have been making great progress.

I'm reading the shaders from a file using this

Function GetFileData:String(path:String)
	Local str:String = ""
	Local stream:TStream = ReadFile(path)
	While Not Eof(stream)
		str:+ReadLine(stream)
	Wend
	CloseStream(stream)
	Return str
End Function


And loading and compiling the shader with

	Function _LoadShader(ShaderCode:String, ShaderObject:Int)
		Local ShaderCodeC:Byte Ptr = ShaderCode.ToCString()
		Local ShaderCodeLen:Int    = ShaderCode.Length
		
		glShaderSource(ShaderObject, 1, Varptr ShaderCodeC, Varptr ShaderCodeLen)
		
		MemFree(ShaderCodeC)
	End Function


For some reason, if I have comments or other common characters that aren't part of the shader it throws an exception.

Am I suppose to remove certain characters? Is there a way to cleanse the string before it gets compiled?

Thanks!


Streaksy(Posted 2015) [#2]
Well... Just disregard lines that are supposed to be comments, I guess...


grable(Posted 2015) [#3]
Since your throwing away newlines, comments will end up removing most of the the code ;)

Use LoadText or add newlines manually, and it should be easier to debug the shader as well (with correct line numbers and all).


markcw(Posted 2015) [#4]
Even though this is a 2 year old post now I might as well add my findings.

Like grable says, best to load shaders from text files as this removes the need to parse lines, but you can get away with comments in strings if you're careful by either using "/* multiline comment */" or by always adding a newline after "// singleline comment " + "~n".


Bobysait(Posted 2016) [#5]
old topic, but I think the issue on this post is just :
str:+ReadLine(stream)


it removes the CRLF from the line.
change this to
str:+ReadLine(stream)+chr(13)+chr(10)

or directly happens the source to a bank and convert to String using String.FromBytes(Bank.buf(),Bank.length)

Function GetFileData:String(path:String)
	Local stream:TStream = ReadFile(path)
	Local bank:TBank = CreateBank(stream.Size())
	Stream.ReadBytes(bank.Buf(),bank.Size());
	Local source:String = String.FromBytes(bank.Buf(), bank.Size())
	CloseStream(stream)
	Return source
End Function


Like this, the source file won't be corrupted.


* ---------------------------- *
Explanation about your error :

with your code, whenever you add a comment ( "//" ), and as long as you ends with no "Line Feed", it's all what comes after the comment symbol that is commented in the shader :
for example, this original code
uniform float value;
out vec4 frag;
const vec3 unit3 = vec3(1,1,1);
void main (){
  // output fragment color
  frag = vec4(unit3*value,1.0);
}


becomes this, when you add lines with your initial method :
uniform float value;out vec4 frag;const vec3 unit3 = vec3(1,1,1);void main (){  // output fragment color  frag = vec4(unit3*value,1.0);}

So, while if there were no comments the shader would work because of the explicit ";", it breaks with comment because all the right part of the line is commented, so you get a malformed shader with a missing bracket to enclose the main function, and lots of missing code.