Understanding Preprocessor Directives in Monogame

I want to check and see if a project is using DirectX or OpenGL at runtime.
I would like to use the syntax:

            System.Diagnostics.Debug.WriteLine("directX true");

            System.Diagnostics.Debug.WriteLine("opengl true");

However, this doesn’t work.
What do I need to do to get this working?
I have platform dependent code paths.
This is probably a stupid question, forgive me.


I should probably clarify that I’m using a single set of classes shared between an OpenGL project and a SharpDX/DirectX project, which is why I need to create platform specific code paths in a single class.

So I don’t really have to use Preprocessor Directives to accomplish what I want.
I want to be able to determine if the project is using OpenGL or SharpDX/DirectX at runtime.
Preprocessor Directives seem like the simplest way to do this.
But is there a better way?

The defines from the reference MG assembly don’t carry over to your project just like that. You could define them yourself depending on the backend you’re using. You can also determine the backend at runtime by digging into the MG assembly.

Since the MG assembly you’re using is compiled for either OpenGL or DirectX, there are some things that are different between the two that you can use to figure out what is being used. The ‘cleanest’ way IMO to figure this out is to use reflection to get Shader.Profile. I think there are plans to open this part of the code up, so you won’t have to use reflection in the future. For OpenGL this returns 0 and for DirectX it returns 1.

So I’m new to reflection, and I’m getting the error: ‘Shader’ is inaccessible due to its protection level. I can see the ‘internal partial class Shader’ definition in the code you linked to.

How would I go about accessing this partial class?
I’m attempting to do so like this:
System.Reflection.FieldInfo info = typeof(Microsoft.Xna.Framework.Graphics.Shader);

Any tips on using reflection? I was going to collect the fields from Shader, then check Shader.Profile for the value you mentioned. Is this the ‘proper’ way of doing so?

I’ve done it like this before:

private static bool IsUsingDX() {
    // use reflection to figure out if Shader.Profile is OpenGL (0) or DirectX (1)
    var mgAssembly = Assembly.GetAssembly(typeof (Game));
    var shaderType = mgAssembly.GetType("Microsoft.Xna.Framework.Graphics.Shader");      
    var profileProperty = shaderType.GetProperty("Profile");  
    var value = (int) profileProperty.GetValue(null);
    return value == 1;

I’m not sure if this is the best way to do it though.

Thanks for quick reply and help, however I’m getting a null reference on profileProperty.
Should I include a null check?

Are you using 3.5 maybe? I did this in the develop branch. Maybe the name changed or something.

Yeah, I’m using 3.5.

Is there a way to just collect all of shader’s fields?
Then I could inspect and find the property that represents shader.profile?
Could I do that with System.Reflection.FieldInfo?

Again, thanks for your help with this.

The Profile property wasn’t there yet in the 3.5 release. You can see it in the master branch on GitHub https://github.com/MonoGame/MonoGame/blob/master/MonoGame.Framework/Graphics/Shader/Shader.cs
You’ll have to figure out something else. You can just search through the partial classes (ending in .OpenGL and .DirectX) and find a difference that can help you figure out what assembly is loaded in. Maybe there’s also a difference in the assembly metadata or something.

Funny, I was just looking at the master branch and noticed that too.

It seems Shader.Profile simply returns PlatformProfile().
I’m wondering if this is available in 3.5?
If it is, where would be the best place to access it?

In any case, I’m really looking forward to 3.6.

It’s not. It would be in Shader.DirectX.cs and Shader.OpenGL.cs files. MG uses partial classes to handle platform specific implementations. Depending on the assembly one of those two is used. Protobuild handles including the right one. They both implement PlatformProfile and for DX the implementation just returns 1 while for OpenGL it returns 0. You can see this in the develop branch

Would it be possible to inspect Game.Platform ?

More to the point, is it possible to compare GamePlatform.Desktop to a type like OpenTKGamePlatform ?

I’m thinking something like if (Game.Platform == OpenTKGamePlatform), but I’m not sure if this is a possible comparison or how to do it. I’ve been trying for a few hours now, but I’ve hit a brickwall. Any ideas on how I could use Game.Platform to determine if the game is using OpenGL or DirectX? Or am I approaching this wrong?

Thanks in advance.