Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
GraphicsTelemetry.cs
using System;
using System.Collections.Generic;
using System.Runtime.InteropServices;
using Microsoft.ApplicationInsights.Channel;
using Microsoft.ApplicationInsights.Extensibility;
namespace TelemetryClientSample.Telemetry
{
class GraphicsTelemetry : ITelemetryInitializer
{
private readonly int featurelevel = -1;
private readonly bool softwareRenderingRegistry;
public GraphicsTelemetry()
{
try
{
featurelevel = GetFeatureLevel();
}
catch { }
var disable = Microsoft.Win32.Registry.GetValue(@"HKEY_CURRENT_USER\Software\Microsoft\Avalon.Graphics", "DisableHWAcceleration", 0);
softwareRenderingRegistry = (disable is int value && value == 1);
System.Windows.Media.RenderCapability.TierChanged += RenderCapability_TierChanged;
}
private void RenderCapability_TierChanged(object sender, EventArgs e)
{
DiagnosticsClient.TrackEvent("DX Rendering Capability Tier Changed", new Dictionary<string, string> { { "DX Rendering Capability Tier", System.Windows.Media.RenderCapability.Tier.ToString() } });
}
public void Initialize(ITelemetry telemetry)
{
telemetry.Context.GlobalProperties["DX Max Hardware Texture Size"] = System.Windows.Media.RenderCapability.MaxHardwareTextureSize.ToString();
telemetry.Context.GlobalProperties["DX FeatureLevel"] = (featurelevel / 10d).ToString();
telemetry.Context.GlobalProperties["DX Registry Forced Software Rendering"] = softwareRenderingRegistry.ToString();
telemetry.Context.GlobalProperties["DX Process RenderMode"] = System.Windows.Media.RenderOptions.ProcessRenderMode.ToString();
telemetry.Context.GlobalProperties["DX Rendering Capability Tier"] = System.Windows.Media.RenderCapability.Tier.ToString();
}
public static int GetFeatureLevel()
{
IntPtr deviceOut;
IntPtr immediateContextOut;
uint featureLevelRef;
uint[] levels = {
D3D_FEATURE_LEVEL_12_1, D3D_FEATURE_LEVEL_12_0,
D3D_FEATURE_LEVEL_11_1, D3D_FEATURE_LEVEL_11_0,
D3D_FEATURE_LEVEL_10_1, D3D_FEATURE_LEVEL_10_0,
D3D_FEATURE_LEVEL_9_3, D3D_FEATURE_LEVEL_9_2, D3D_FEATURE_LEVEL_9_1
};
D3D11CreateDevice(
IntPtr.Zero,
1, //Hardware
IntPtr.Zero, // software renderer module
0, //Creation flags
levels, //Feature levels
(uint)levels.Length,
7, //DX11
out deviceOut,
out featureLevelRef,
out immediateContextOut);
if (deviceOut != null) Marshal.Release(deviceOut);
if (immediateContextOut != null) Marshal.Release(immediateContextOut);
if (featureLevelRef <= D3D_FEATURE_LEVEL_9_1) return 91;
if (featureLevelRef <= D3D_FEATURE_LEVEL_9_2) return 92;
if (featureLevelRef <= D3D_FEATURE_LEVEL_9_3) return 93;
if (featureLevelRef <= D3D_FEATURE_LEVEL_10_0) return 100;
if (featureLevelRef <= D3D_FEATURE_LEVEL_10_1) return 101;
if (featureLevelRef <= D3D_FEATURE_LEVEL_11_0) return 110;
if (featureLevelRef <= D3D_FEATURE_LEVEL_11_1) return 111;
if (featureLevelRef <= D3D_FEATURE_LEVEL_12_0) return 120;
return 121;
}
[DllImport("d3d11.dll", CallingConvention = CallingConvention.StdCall)]
private static extern int D3D11CreateDevice(IntPtr pAdapter, int DriverType, IntPtr Software, uint Flags, uint[] pFeatureLevels, uint FeatureLevels, uint SDKVersion, [Out] out IntPtr ppDevice, [Out]out uint pFeatureLevel, [Out] out IntPtr ppImmediateContext);
private const uint D3D_FEATURE_LEVEL_9_1 = 0x9100;
private const uint D3D_FEATURE_LEVEL_9_2 = 0x9200;
private const uint D3D_FEATURE_LEVEL_9_3 = 0x9300;
private const uint D3D_FEATURE_LEVEL_10_0 = 0xa000;
private const uint D3D_FEATURE_LEVEL_10_1 = 0xa100;
private const uint D3D_FEATURE_LEVEL_11_0 = 0xb000;
private const uint D3D_FEATURE_LEVEL_11_1 = 0xb100;
private const uint D3D_FEATURE_LEVEL_12_0 = 0xc000;
private const uint D3D_FEATURE_LEVEL_12_1 = 0xc100;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment