openfl icon indicating copy to clipboard operation
openfl copied to clipboard

hard to diagnose error in shader

Open Geokureli opened this issue 1 year ago • 3 comments

This may be flixel's fault, but I noticed that this:

class SimpleShader extends flixel.system.FlxAssets.FlxShader
{
	@:glFragmentSource('
		#pragmaheader // Note the missing space between pragma and header
		
		void main()
		{
			gl_FragColor = texture2D(bitmap, openfl_TextureCoordv);
		}
	')
	public function new()
	{
		super();
	}
}

Causes extremely different errors than simple omitting the pragma header line altogether, the latter throws a glsl parsing error, where the former causes shader.bitmap to be null, I'm wondering why that is, and whether the resulting error can be less cryptic

Geokureli avatar Aug 20 '24 20:08 Geokureli

It's not flixel's fault. The #pragma header directive is preprocessed by a simple string replace, here:

https://github.com/openfl/openfl/blob/e1fd1a04a09ad43950d0d84e490166e05d152171/src/openfl/utils/_internal/ShaderMacro.hx#L106

Missing the space means that the substitution won't take place, and the resulting shader string is sent as-is to the opengl driver. The error message is GPU-vendor-dependent: and some vendors might also treat it as a warning and/or just skip the faulty line, so you could have a shader working fine on your GPU and then failing for some of your players.

Probably the only way to catch this type of errors independently of opengl drivers is to use a Haxe based GLSL parser/compiler like Heaps' HXSL.

giuppe avatar Sep 02 '24 19:09 giuppe

I'm mainly surprised it's not giving this error:

'pragmaheader' : invalid directive name

Geokureli avatar Sep 03 '24 16:09 Geokureli

Could OpenFL check if #pragma header is in the source via something like StringTools.contains and warn/error if it isn't?

ACrazyTown avatar Jan 28 '25 13:01 ACrazyTown