GLSL shaders

Hi there,

I know that currently MonoGame do not support GLSL shaders.

Is there a way to use a GLSL shader bypassing the content processor or any other way?

Thank you.

Is this shader not convertible to a usable format?

sorry I don’t understand your question.

I’m not using shaders yet but I want (if possible) to begin with GLSL so I can use them in almost all platforms.

Right, ok, just that is not the question you gave… Good luck however.

Hopefully someone understands better now…

Monogame right now uses HLSL shaders for every platform, but when compiling for non-DirectX it is automatically converted into GLSL by a parser.

I can see why you would want to learn GLSL over HLSL, but they are so similar that you don’t really miss out when choosing the one over the other. Java and C# are relatively different in comparison.

It mostly comes down to different naming conventions (for example to get the fraction of a float you use frac() in HLSL and fract() in glsl, you use vec3 in glsl and float3 in hlsl etc.) and the way some buffers are set up, but the way they work when it really comes down to it is basically the same.


If they’re so similar then I’ll learn HLSL.

Thank you for your explanation.

I’m working on supporting GLSL or rather GLFX which is GLSL mixed with effect syntax like semantics and techniques. I don’t have a whole lot of spare time atm, but it’s not as much work as I initially thought so I should be able to finish it in a while :slight_smile: The plan is to drop MojoShader in favor of HLSL2GLSL which translates source code rather than bytecode. Supporting GLSL and HLSL natively also means we can have a look at implementing newer features (that MojoShader doesn’t support) like geometry, compute shaders and other shader stages. We also need this once we look at Metal and Vulkan support. But developing the new low level graphics API will be a lot harder than this.

1 Like

It’s nice to hear that!

@Jjagg, I’ve taken a peek and tested out some the stuff you’ve been working on for the GLSL on the MonoGame - glsl branch. So far with those changes on that glsl branch I’ve been able to get some my custom content compiled on Mac. This included custom shader and processor . I have yet to test it in runtime, which is the next step, but so far it looks promising. I was just wondering what is still left for you to do on that to have it fully workable and able to be part of monogame ? Is there a thread/ community forum or some place you can forward me to where i can check and track this work and discuss with folks as you progress with this ? Being able to compile and have this stuff working would really be great.Any other references you can throw my way would help , specifically related to compiling shaders / workign with effects on Mac to build content for iOS

You mean my GLFX branch, right? The one from ?
That is also the place for comments or reporting issues you might run into when testing this now an where you can track development. For more ‘informal’ chat or an explanation of how this stuff works you can post here or in a new thread on the forums or on the MG gitter channel.

The runtime might work as is, but I’ve only tested it with MGs SpriteEffect. Your chances are pretty good since building the effect was succesful. The next steps for GLFX are translating the other stock effects and doing a lot of testing. There’s probably some bugs, but in general the implementation is done. After we finish the aforementioned steps we can make GLFX a part of MonoGame. It would not become the default solution until we have a stable tool for translating GLSL to HLSL, but people that want to use it can. They’d probably still have to build from source though.

I’m currently writing a custom parser for the effect syntax that should make the GLFX path a lot more stable and in general throw better error messages. One of the reasons I’m working on the parser (there’s reasons that are not related here too) is I had to hack around the limitations of the current effect parser to get GlFX working and that might cause some issues later on.

I’d gladly explain how this all works in more detail later when I have some more time. Are you interested in the internals or just the practical end-user side of things?

1 Like

The glfx one sorry my bad, yip the one on your repo over here :
I was building from here, specifically testing out the change done in the content pipeline to get the effects working and getting my shaders compiling on MAC - { MonoGame.Framework.Content.Pipeline} EffectsProcessor
What you say all sounds super positive, is certainly good news and that it might all still just work when deployed. Also that majority implementation is there. I’ll be giving the runtime a spin this week, cross my fingers and hope it all just works :slight_smile: Great thanks to you gents and the community for this, its very much appreciated ! I was not too concerned with internals just yet since I was more focused on porting my current framework and dev pipeline from windows over to MAC. Things were going smooth until I realized I’d anyway need a windows pc to compile shaders :slight_smile: I’ve read bits here and there on the workings but still trying to piece it all together. Any info will help so will climb into those channels you mention. If it ends up needing to climb into nuts and bolts anything contributed would be very much appreciated. The repos and comments on there have already been invaluable.

To generate the runtime for shaders built with GLFX, you have to explicitly enable the GLFX service because Protobuild will by default generate the runtime for MojoShader built shaders (which are handled differently). To do that instead of just running mono Protobuild.exe you should run mono Protobuild.exe -enable GLFX. If you run into any issues let me know :slight_smile:

Thanks for the advice on that JJagg . What version of OpenTK are you using . Running protobuild with -enable GLFX and attempting to build MonoGame.FrameWork.MacOS gives me these compile errors.

It’s not using MonoMac/OpenTK, but the DesktopGL path which uses SDL for windowing (and input) and custom OpenGL bindings. You probably need to explicitly generate the DesktopGL version if it’s trying to generate MonoMac by default. I’m not sure if that will just work though, I don’t have a Mac :confused:

Thanks for the feedback on this Jjagg. Bringing stuff over from windows to Mac I’m currently facing the challenging of porting some of my bigger hlsl shaders to glsl now. Some of the more elaborate hlsl shaders have proven to be a bit of a challenge. Any suggestions on how one could do this more reliably ? Is re-writing the hlsl shader to glsl the best choice ? In doing this conversion from hlsl-glsl is there any gotchas deviation or difference in what is specified in the shading language here:

There are some tools around to do the translation for your. You’d need to add the effect syntax after that. I prefer doing it by hand using find-replace (float → vec, frac → fract, lerp → mix…), that should handle most things really.

It’s just standard GLSL, but with the effect syntax available.

It’s not using MonoMac/OpenTK, but the DesktopGL path which uses SDL for windowing (and input) and custom OpenGL bindings. You probably need to explicitly generate the DesktopGL version if it’s trying to generate MonoMac by default. I’m not sure if that will just work though, I don’t have a Mac

@Jjagg, any guidance or pointers to material on how I can do this ? I’ve made some good progress with the changes to my shaders but I think Im hitting some runtime issues now which appear could be related to this.

Ok, it turns out it was not this which caused the major issues. I got this all working in the end but there was some things I had to change to make this work on Mac. I had to change the updateparameterdelegate in ConstantBuffer.GLFX.cs to compile on MAC and then drop a new version of MonoGame.Framework. I then had issues when I tried to compile my custom model skinning processor on my Mac with the pipeline tool and noticed the runtime did not work. It turns out the issue was due to this in my shader:

I have a mat4 array in my effect that I use for the bones for my model for animation which seems to have been the causing all my problems at runtime. The inputs were not mapping them like the other params did.
I had to make a few changes in the pipeline goodies and the runtime glfx classes to read this bones matrix and after I did that my stuff started to work on my Mac at runtime. It seems the custom effects for my skinning which I rewrote from hlsl to glsl has compiled on the Mac along with the texture, specular and normal maps and work at runtime now at least and my animations seem to be working for my models. I’m doing some more tests on the animation and texturing side of things to see if there are any bugs but so far its great and I am super happy I can get my content compiled and working on the Mac. I have a bunch deferred render effects I need to take through this process as well but I expect this should all just work. Thanks again for this work Jjagg!!

Wow, that’s great news! :slight_smile: Please post back any bugs you find (or even better, fix ;)) either here or in the PR
EDIT: Sorry, I missed your earlier post!

Things are moving along but I’ve having some issues with porting of some my deferred rendering stuff. I’ve trying to wire up some my shaders for which I use to setup/clear and write to GBuffer. Im trying to write the Texture, Normal and Depth using gl_FragData as such:

 gl_FragData[0] = Color;
 gl_FragData[1] = Normal;  #{ERROR: 0:40}
 gl_FragData[2] = Depth;    #{ERROR: 0:41}

But on runtime getting shader compile errors in Shader.OpenGL.cs

ERROR: 0:40: Index 1 beyond bounds (size 1)
ERROR: 0:41: Index 2 beyond bounds (size 1)

I think these are due to the gl_FragData being size 1 and not 3. @JJagg, any ideas how I can write this data in FragShader ? I typically need to clear the GBuffer, Write to it and need an output from the FragShader for Texture, Normal and Depth.

UPDATE: It might be related to something happening on the device when setting multiple render targets. I get the error below when I set 3 render targets with …GraphicsDevice.SetRenderTargets(_renderTargetBinding.ToArray())

In debug it simply crashes when doing PlatformApplyRenderTargets(). It guessing its something happening {or not happening} when attempting to do attachments for the fragdata. I.m testing this on iPhone 7 simulator, can I set multiple render targets for iOS ? Any thoughts on this one ?