Shader problem on Android with gl_FragDepth

I’ve got a shader that I’m using to merge a color texture and depth texture with my MonoGame rendered data. It’s working fine in my UWP build, but it is failing in my Android build.

The MonoGame pipeline compiles my HLSL just fine, but it fails at runtime when GL.CompileShader call in Shader.OpenGL.cs with this error:

ERROR: 0:18: 'gl_FragDepth' : undeclared identifier

The compiled shader looks like this:

#ifdef GL_ES
precision mediump float;
precision mediump int;

vec4 ps_r0;
uniform sampler2D ps_s0;
uniform sampler2D ps_s2;
varying vec4 vTexCoord0;
#define ps_v0 vTexCoord0
#define ps_oC0 gl_FragColor
#define ps_oDepth gl_FragDepth

void main()
	ps_oC0 = texture2D(ps_s0, ps_v0.xy);
	ps_r0 = texture2D(ps_s2, ps_v0.xy);
	ps_oDepth = ps_r0.x;

I’ve tried on a couple of devices, both of which are using OpenGL ES 3.2 as verified by this output:

V/AndroidGameView(20506): Created GLES 3.2 Context

As far as I can tell, gl_FragDepth should be supported. And this is happening during the fragment shader as I get no error about using the gl_FragColor command.

If I don’t modify the depth and just set the color (so the shader does include gl_FragColor) then it compiles and works just fine.

So it seems to specifically be the depth.

Everything I read says this should work. Any ideas?

If I not mistake, you r try to do MRT/ framebuffer stuff. I having compileShader issue too. No one can help on this, I did some research, it seem like the shader that generated by mojoshader is not compatible with the latest gles 3.0.

It’ only available on gles 3.0. the generated shader is base on #version 100. It only work on #version 300

Yes, I am trying to merge a separate frame buffer with depth information into my MonoGame scene.

And you are right, the current pipeline does not generate #version 300 shaders and that is indeed the problem.

This is quite disappointing and I see no workaround. :frowning: