Android Shadowmap and render target SufaceFormat (lack of float support)

I have my Shadowmap stuff working on PC (OpenGL), however it does not appear to be working on Android due to the lack of SufaceFormat.Float support.

Switching to SufaceFormat.Color breaks the Shadowmap.

Any ideas / suggestions on how I can fix this?

Thanks!

You should be able to correctly encode the float in a Color struct, but I don’t know how exactly to best go about that and if there are any pitfalls/caveats.

@Jjagg Thanks… I managed to get it working using SurfaceFormat.Color and using some conversions I found online to pack a float into an RGBA color.

It does lose some precision, so doesn’t look as clean & nice as the native floating point version of the shadowmap.

I believe float rendertargets are supported via the OpenGL ES 2.0 extensions. Do you know if this is something that is supported in MG? I did some searching, but couldn’t find anything regarding Monogame and the ES 2.0 extensions.

MonoGame does not expose what extensions are present since it’s not cross-platform. MG does not check what GL API is being used, it always uses the GL_FLOAT value. Currently MG will just attempt to create a texture with the given format, however because the FLOAT token from the extension does not match the value of the OpenGL GL_FLOAT token you can’t try to use it through the MG API.

I suppose MG should try the extension token instead under GLES 2.0. That should be fairly simple to implement. If you’re interested in this, can you open an issue on GitHub?
I just checked and GL_FLOAT is in the GLES 3.0 spec, so this should just work under GLES 3.0 with MG.

@Jjagg So are you saying this should just work? When trying to run my project with the SurfaceFormat set to SurfaceFormat.Single, I get an exception saying that that SurfaceFormat is not supported.

I am running it on a Pixel C tablet which supports OpenGL ES 3.2, so I would expect it to work if MG was checking which flags / version was enabled or available.

I can open an issue for this… but I am not sure what the issue should say.

Hmm, yes for GL ES 3.0+ I’d expectrd it to work. Can you post the stack trace of the exception? I’ll open an issue for it tomorrow :slight_smile:

@Jjagg

The error I get when I try using SurfaceFormat.Single is:

System.NotSupportedException: Specified method is not supported.

The specific line in the code that causes this is:

_shadowMap = new RenderTarget2D(GraphicsDevice, 2048, 2048, false, SurfaceFormat.Single, DepthFormat.Depth24);

I tried both SurfaceFormat.Single as well as SurfaceFormat.HalfSingle. Neither works. If I change the SurfaceFormat to Color (or anything along those lines), it works.

I reread the extension and under GL ES 2.0 we can’t support SurfaceFormat.Single, because the single value is in the Red channel, while OpenGL ES 2.0 with the extension only supports it in the alpha channel. For OpenGL ES 3.0 - like I mentioned before - things should just work. I think MG does a check somewhere and is unaware that the float format should ‘Just Work’ for OpenGL ES 3.0, so I’ll open an issue for that. EDIT: GitHub issue

@YTN this PR might be of interest :slight_smile: https://github.com/MonoGame/MonoGame/pull/6264
With that I can create SurfaceFormat.Single on my Pixel 2.

1 Like

@dellis1972 That’s awesome!

Thanks for the heads up… Will give this a try as soon as I can.

Much appreciated for resolving this so quickly!

@dellis1972

I installed the latest build (3.7.0.1448) which looks like it should have your changes. However, when I change my rendertarget to Single and try to run my Android project, I get the same error on creating a rendertarget i.e. I get the error:

System.NotSupportedException: Specified method is not supported

I am not sure what I am doing differently.

Can you paste the code you’re using to create your rendertarget?

Are there any special settings (android or Monogame) that I need to enable for this?

Thanks!

The PR has not been merged yet. But you can get the PR build from http://teamcity.monogame.net/viewLog.html?buildId=65642&buildTypeId=MonoGame_PackagingWindows&tab=artifacts (If you are on windows) or http://teamcity.monogame.net/viewLog.html?buildId=65636&buildTypeId=MonoGame_PackageMacAndLinux&tab=artifacts for Mac/linux.

In case you want to build from source instead, I wrote a gist with instructions for that some time ago: https://gist.github.com/Jjagg/3a2cf257910b21fd0c143d81ef63637a

@dellis1972 @Jjagg Thanks… I installed build 1441 that @dellis1972 linked to and it works like a charm!

Thanks again for the quick resolution on this!

1 Like