Skip to content

Commit

Permalink
Seeing#: Updates DXGI Factory / Adapter initialization
Browse files Browse the repository at this point in the history
  • Loading branch information
RolandKoenig committed Feb 19, 2017
1 parent b4a2fa1 commit 545a145
Show file tree
Hide file tree
Showing 7 changed files with 63 additions and 78 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,14 @@ public void ChangeOutputMode(EngineOutputModeInfo newMode)
m_renderLoop.ForceViewRefresh();
}

/// <summary>
/// Ensures that fullsreen mode is currently active.
/// </summary>
public void CheckAndEnsureFullsceeenActive()
{
m_checkFullscreenNextTime = true;
}

Control IInputControlHost.GetWinFormsInputControl()
{
return m_dummyForm;
Expand Down Expand Up @@ -275,6 +283,7 @@ void IRenderLoopHost.OnRenderLoop_PrepareRendering(EngineDevice device)
SharpDX.Mathematics.Interop.RawBool isFullscreen = false;
DXGI.Output fullscreenOutput = null;
m_swapChain.GetFullscreenState(out isFullscreen, out fullscreenOutput);
GraphicsHelper.SafeDispose(ref fullscreenOutput);
if (isFullscreen == false)
{
m_isInFullscreen = false;
Expand Down
19 changes: 1 addition & 18 deletions SeeingSharp.Multimedia_SHARED/Core/_Devices/EngineDevice.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ You should have received a copy of the GNU Lesser General Public License
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using SeeingSharp.Util;

// Some namespace mappings
#if UNIVERSAL
Expand Down Expand Up @@ -179,24 +180,6 @@ internal DXGI.SampleDescription GetSampleDescription(AntialiasingQualityLevel qu
return new DXGI.SampleDescription(1, 0);
}

/// <summary>
/// Creates a list containing all available devices.
/// </summary>
public static IEnumerable<EngineDevice> CreateDevices(GraphicsCore core, GraphicsCoreConfiguration coreConfiguration, bool debugEnabled)
{
using(DXGI.Factory1 dxgiFactory = new DXGI.Factory1())
{
int adapterCount = dxgiFactory.GetAdapterCount1();
for(int loop=0 ; loop<adapterCount; loop++)
{
DXGI.Adapter1 actAdapter = dxgiFactory.GetAdapter1(loop);
bool isSoftware = actAdapter.Description1.Description.StartsWith("Microsoft Basic Render Driver");

yield return new EngineDevice(core, coreConfiguration, actAdapter, isSoftware, debugEnabled);
}
}
}

/// <summary>
/// Get the sample description for the given quality level.
/// </summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,26 +31,14 @@ You should have received a copy of the GNU Lesser General Public License
using D3D = SharpDX.Direct3D;
using D3D11 = SharpDX.Direct3D11;

//Some type mappings
#if WINRT || UNIVERSAL
using DxgiFactory = SharpDX.DXGI.Factory1;
using DxgiAdapter = SharpDX.DXGI.Adapter1;
using DxgiDevice = SharpDX.DXGI.Device1;
#endif
#if DESKTOP
using DxgiAdapter = SharpDX.DXGI.Adapter1;
using DxgiDevice = SharpDX.DXGI.Device1;
using DxgiFactory = SharpDX.DXGI.Factory1;
#endif

namespace SeeingSharp.Multimedia.Core
{
public class EngineAdapterInfo
{
private const string TRANSLATABLE_GROUP_COMMON_HARDWARE_INFO = "Common hardware information";

private List<EngineOutputInfo> m_outputs;
private DxgiAdapter m_adapter;
private DXGI.Adapter1 m_adapter;
private int m_adapterIndex;
private bool m_isSoftware;
private D3D.FeatureLevel m_d3d11FeatureLevel;
Expand All @@ -59,7 +47,7 @@ public class EngineAdapterInfo
/// <summary>
/// Initializes a new instance of the <see cref="EngineAdapterInfo" /> class.
/// </summary>
internal EngineAdapterInfo(int adapterIndex, DxgiAdapter adapter)
internal EngineAdapterInfo(int adapterIndex, DXGI.Adapter1 adapter)
{
m_outputs = new List<EngineOutputInfo>();
m_adapter = adapter;
Expand Down Expand Up @@ -108,7 +96,7 @@ public List<EngineOutputInfo> Outputs
/// <summary>
/// Gets the corresponding adapter.
/// </summary>
internal DxgiAdapter Adapter
internal DXGI.Adapter1 Adapter
{
get { return m_adapter; }
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,69 +26,50 @@ You should have received a copy of the GNU Lesser General Public License
using System.Collections.ObjectModel;
using System.Threading;
using System.Threading.Tasks;
using SeeingSharp.Util;

//Some namespace mappings
using DXGI = SharpDX.DXGI;
using D3D = SharpDX.Direct3D;
using D3D11 = SharpDX.Direct3D11;

//Some type mappings
using DxgiAdapter = SharpDX.DXGI.Adapter1;
using DxgiDevice = SharpDX.DXGI.Device1;
using DxgiFactory = SharpDX.DXGI.Factory1;

namespace SeeingSharp.Multimedia.Core
{
public class EngineHardwareInfo
{
private DxgiFactory m_dxgiFactory;
private DXGI.Factory1 m_dxgiFactory;
private List<EngineAdapterInfo> m_adapters;

/// <summary>
/// Initializes a new instance of the <see cref="EngineHardwareInfo" /> class.
/// </summary>
internal EngineHardwareInfo()
: this(new DxgiFactory())
{

}

/// <summary>
/// Initializes a new instance of the <see cref="EngineHardwareInfo" /> class.
/// </summary>
internal EngineHardwareInfo(DxgiFactory dxgiFactory)
{
m_dxgiFactory = dxgiFactory;
m_adapters = new List<EngineAdapterInfo>();

LoadAdapterInformation();
this.CreateFactory();
this.LoadAdapterInformation();
}

internal DXGI.Output GetOutputByOutputInfo(EngineOutputInfo outputInfo)
private void CreateFactory()
{
int adapterCount = m_dxgiFactory.GetAdapterCount1();
if(outputInfo.AdapterIndex >= adapterCount) { throw new SeeingSharpException($"Unable to find adapter with index {outputInfo.AdapterIndex}!"); }

using (DXGI.Adapter1 adapter = m_dxgiFactory.GetAdapter1(outputInfo.AdapterIndex))
{
int outputCount = adapter.GetOutputCount();
if(outputInfo.OutputIndex >= outputCount) { throw new SeeingSharpException($"Unable to find output with index {outputInfo.OutputIndex} on adapter {outputInfo.AdapterIndex}!"); }

return adapter.GetOutput(outputInfo.OutputIndex);
}
m_dxgiFactory = CommonTools.TryExecute(() => new DXGI.Factory4());
if (m_dxgiFactory == null) { m_dxgiFactory = CommonTools.TryExecute(() => new DXGI.Factory2()); }
if (m_dxgiFactory == null) { m_dxgiFactory = CommonTools.TryExecute(() => new DXGI.Factory1()); }
if (m_dxgiFactory == null) { throw new SeeingSharpGraphicsException("Unable to create the DXGI Factory object!"); }
}

/// <summary>
/// Loads all adapter information and builds up all needed view models in a background thread.
/// </summary>
private void LoadAdapterInformation()
{
m_adapters = new List<EngineAdapterInfo>();

int adapterCount = m_dxgiFactory.GetAdapterCount1();
for (int loop = 0; loop < adapterCount; loop++)
{
try
{
DxgiAdapter actAdapter = m_dxgiFactory.GetAdapter1(loop);
DXGI.Adapter1 actAdapter = m_dxgiFactory.GetAdapter1(loop);
m_adapters.Add(new EngineAdapterInfo(loop, actAdapter));
}
catch (Exception)
Expand All @@ -99,6 +80,20 @@ private void LoadAdapterInformation()
}
}

internal DXGI.Output GetOutputByOutputInfo(EngineOutputInfo outputInfo)
{
int adapterCount = m_dxgiFactory.GetAdapterCount1();
if(outputInfo.AdapterIndex >= adapterCount) { throw new SeeingSharpException($"Unable to find adapter with index {outputInfo.AdapterIndex}!"); }

using (DXGI.Adapter1 adapter = m_dxgiFactory.GetAdapter1(outputInfo.AdapterIndex))
{
int outputCount = adapter.GetOutputCount();
if(outputInfo.OutputIndex >= outputCount) { throw new SeeingSharpException($"Unable to find output with index {outputInfo.OutputIndex} on adapter {outputInfo.AdapterIndex}!"); }

return adapter.GetOutput(outputInfo.OutputIndex);
}
}

/// <summary>
/// Gets a collection containing all adapters.
/// </summary>
Expand Down
21 changes: 21 additions & 0 deletions SeeingSharp_SHARED/Util/CommonTools.cs
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,27 @@ public static T[] GetBackingArray<T>(Queue<T> queue)

}

public static T TryExecute<T>(Func<T> funcToExec)
{
try
{
return funcToExec();
}
catch
{
return default(T);
}
}

public static void TryExecute(Action actionToExecute)
{
try
{
actionToExecute();
}
catch { }
}

/// <summary>
/// Calls the given function asynchronous.
/// </summary>
Expand Down
10 changes: 0 additions & 10 deletions Tests/SeeingSharp.DesktopFullscreenTest/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -42,16 +42,6 @@ private static async void OnApplication_FirstIdle(object sender, EventArgs e)
s_renderTarget.RenderLoop.SceneComponents.Add(new SimpleCenteredCubeComponent());
s_renderTarget.RenderLoop.SceneComponents.Add(new FocusedPointCameraComponent());
s_renderTarget.WindowDestroyed += (innerSender, innerEArgs) => Application.Exit();

await Task.Delay(5000);

s_renderTarget.ChangeOutputMode(
GraphicsCore.Current.DefaultOutput.SupportedModes.FirstOrDefault((actMode) => actMode.PixelWidth == 1600));

await Task.Delay(5000);

s_renderTarget.ChangeOutputMode(
GraphicsCore.Current.DefaultOutput.DefaultMode);
}

//*********************************************************************
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
- http://www.rolandk.de/wp (the autors homepage, german)
Copyright (C) 2016 Roland König (RolandK)
This program is distributed under the terms of the Microsoft Public License (Ms-PL)-
More info at https://msdn.microsoft.com/en-us/library/ff647676.aspx
This program is distributed under the terms of the Microsoft Public License (Ms-PL)-
More info at https://msdn.microsoft.com/en-us/library/ff647676.aspx
*/
#endregion License information (SeeingSharp and all based games/applications)

Expand Down Expand Up @@ -39,7 +39,6 @@ public class ObjectSelectionBehavior : Behavior<SeeingSharpRendererElement>
private Scene m_scene;
private RenderLoop m_renderLoop;
private GenericObject m_hoveredObject;
private GenericObject m_selectedObject;
#endregion Scene state

public ObjectSelectionBehavior()
Expand Down Expand Up @@ -108,7 +107,7 @@ private async void OnRefeshTimer_Tick(object sender, EventArgs e)

// Sync
GenericObject newHovered = null;
if ((topUnderCursor != null) && (m_selectedObject != topUnderCursor)) { newHovered = topUnderCursor; }
if (topUnderCursor != null) { newHovered = topUnderCursor; }

// Apply changes to the scene
if (m_hoveredObject != newHovered)
Expand Down

0 comments on commit 545a145

Please sign in to comment.