[solved]How to determine if the app is using Desktop GL or DX.

I know internally monogame uses if defines for different platforms. My question is as follows.

Is there a public Get accessor that will return for example DesktopGL or Windows (Dx) that i can call from within Game1 to return what monogame see’s as the TargetPlatform internally ?


Recently i was asking about loading a .fx from a string. (Pumpkin Pudding had the answer)
I would like to wrap that up into a nice little codeblock that doesn’t care if its open gl or dx under the hood and will switch on its own to compensate.

However to do so…

I need to add both dll’s to my project which is fairly easy.
I also need to pass the correct target platform to the call.

In order to do that though… i need to detect the TargetPlatform programmatically before game load() finishes.

Here is a snippet of code to better illustrate the problem.

        public Effect LoadFxFromString(GraphicsDevice gd, string fxcode)
            string sourceFile = Directory.GetCurrentDirectory() + "/tmp.txt";
            File.WriteAllText(sourceFile, fxcode);
            EffectImporter importer = new EffectImporter();
            EffectContent content = importer.Import(sourceFile, null);
            EffectProcessor processor = new EffectProcessor();
            PipelineManager pm = new PipelineManager(string.Empty, string.Empty, string.Empty);
            // ill need to detect the platform to pass the correct target
            // DX
            pm.Platform = TargetPlatform.Windows;
            // GL
            //pm.Platform = TargetPlatform.DesktopGL;
            // ideally instead of the above.
            // pm.Platform = this.CurrentlyRunningTargetPlatform;
            pm.Profile = GraphicsProfile.HiDef;
            PipelineProcessorContext ppc = new PipelineProcessorContext(pm, new PipelineBuildEvent());
            CompiledEffectContent cecontent = processor.Process(content, ppc);
            ContentCompiler compiler = new ContentCompiler();
            return new Effect(gd, cecontent.GetEffectCode());

Essentially i need to know the TargetPlatform programatically.

Other wise ill have to constantly change this depending on the project template i make.
Instead of the return value allowing me to pass the target platform i need to use independently of which template i have selected for my project,

Which would be a little bit disappointing, however the above is very cool already.

You can use reflection to get Shader.Profile: https://github.com/MonoGame/MonoGame/blob/develop/MonoGame.Framework/Graphics/Shader/Shader.cs#L47

// 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);
var extension = value == 1 ? "dx11" : "ogl";

Thanks jjag, its not elegant but it will work.

As far as releasing a effect made by this.

I would probably have to test for then dispose the effects i loaded, if i use this, in order to release them in unload. Does that sound correct?