Skip to content

Commit

Permalink
inline GetDisplayResolution() (#1599)
Browse files Browse the repository at this point in the history
* inline GetDisplayResolution()

* refactor CorrectBackBufferSize()

* swap output check

* remove out parameters

* remove unnecessary code

* init targetModeDesc

* move targetModeDesc

* using output

* compact code

* swap _swapChain check

* swap HardwareModeSwitch check

* local displayMode

* comments

* inline CorrectBackBufferSize()

* GetClosestBackBufferSize(...) params

* GetClosestBackBufferSize(...) out params

* common width,height

* add TODO
  • Loading branch information
nkast authored May 22, 2024
1 parent 67c1ae9 commit 59326dd
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 65 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,8 @@ private List<GraphicsAdapter> CreateAdapterList()
int monitorCount = dxAdapter.GetOutputCount();
for (int j = 0; j < monitorCount; j++)
{
using (DXGI.Output dxMonitor = dxAdapter.GetOutput(j))
DXGI.Output dxMonitor;
using (dxMonitor = dxAdapter.GetOutput(j))
{
ConcreteGraphicsAdapter adapterStrategy = new ConcreteGraphicsAdapter(dxAdapter, dxMonitor);
GraphicsAdapter adapter = base.CreateGraphicsAdapter(adapterStrategy);
Expand Down
133 changes: 69 additions & 64 deletions MonoGame.Framework/Graphics/.DX11/ConcreteGraphicsDevice.cs
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,25 @@ public override void Reset(PresentationParameters presentationParameters)
this.PresentationParameters = presentationParameters;

#if WINDOWSDX
CorrectBackBufferSize();
if (this.PresentationParameters.IsFullScreen)
{
int width, height;
if (!this.PresentationParameters.HardwareModeSwitch)
{
DisplayMode displayMode = Adapter.CurrentDisplayMode;
width = displayMode.Width;
height = displayMode.Height;
}
else
{
int preferredWidth = this.PresentationParameters.BackBufferWidth;
int preferredHeight = this.PresentationParameters.BackBufferHeight;
SurfaceFormat preferredFormat = this.PresentationParameters.BackBufferFormat;
GetClosestBackBufferSize(preferredWidth, preferredHeight, preferredFormat, out width, out height);
}
this.PresentationParameters.BackBufferWidth = width;
this.PresentationParameters.BackBufferHeight = height;
}
#endif

if (this.PresentationParameters.DeviceWindowHandle == IntPtr.Zero)
Expand Down Expand Up @@ -384,80 +402,69 @@ private void CreateDeviceIndependentResources()
protected override void PlatformInitialize()
{
#if WINDOWSDX
CorrectBackBufferSize();
#endif
CreateSizeDependentResources();
}

#if WINDOWSDX
private void CorrectBackBufferSize()
{
// Window size can be modified when we're going full screen, we need to take that into account
// so the back buffer has the right size.
if (PresentationParameters.IsFullScreen)
if (this.PresentationParameters.IsFullScreen)
{
int newWidth, newHeight;
if (PresentationParameters.HardwareModeSwitch)
int width, height;
if (!this.PresentationParameters.HardwareModeSwitch)
{
GetModeSwitchedSize(out newWidth, out newHeight);
DisplayMode displayMode = Adapter.CurrentDisplayMode;
width = displayMode.Width;
height = displayMode.Height;
}
else
{
GetDisplayResolution(out newWidth, out newHeight);
int preferredWidth = this.PresentationParameters.BackBufferWidth;
int preferredHeight = this.PresentationParameters.BackBufferHeight;
SurfaceFormat preferredFormat = this.PresentationParameters.BackBufferFormat;
GetClosestBackBufferSize(preferredWidth, preferredHeight, preferredFormat, out width, out height);
}

PresentationParameters.BackBufferWidth = newWidth;
PresentationParameters.BackBufferHeight = newHeight;
this.PresentationParameters.BackBufferWidth = width;
this.PresentationParameters.BackBufferHeight = height;
}
#endif
CreateSizeDependentResources();
}

private void GetModeSwitchedSize(out int width, out int height)
#if WINDOWSDX
private void GetClosestBackBufferSize(int preferredWidth, int preferredHeight, SurfaceFormat preferredFormat,
out int width, out int height)
{
width = preferredWidth;
height = preferredHeight;

// TODO: we can get a matching DisplayMode from this.Adapter.SupportedDisplayModes

DXGI.Output output = null;
if (_swapChain == null)
if (_swapChain != null)
{
try { output = _swapChain.ContainingOutput; }
catch (DX.SharpDXException) { /* ContainingOutput fails on a headless device */ }
}
else
{
// get the primary output
// TODO: GetAdapter1(0) here is wrong. Get the DXGI.Adapter1 from ((IPlatformGraphicsAdapter)this.Adapter).Strategy.ToConcrete<ConcreteGraphicsAdapter>()._dxAdapter.
using (DXGI.Factory1 factory = new DXGI.Factory1())
using (DXGI.Adapter1 adapter = factory.GetAdapter1(0))
output = adapter.Outputs[0];
}
else

if (output != null)
{
try
using (output)
{
output = _swapChain.ContainingOutput;
DXGI.ModeDescription targetModeDesc = new DXGI.ModeDescription();
targetModeDesc.Scaling = DXGI.DisplayModeScaling.Unspecified;
targetModeDesc.Width = preferredWidth;
targetModeDesc.Height = preferredHeight;
targetModeDesc.Format = preferredFormat.ToDXFormat();

DXGI.ModeDescription closestMode;
output.GetClosestMatchingMode(this.D3DDevice, targetModeDesc, out closestMode);
width = closestMode.Width;
height = closestMode.Height;
}
catch (DX.SharpDXException) { /* ContainingOutput fails on a headless device */ }
}

DXGI.Format format = PresentationParameters.BackBufferFormat.ToDXFormat();
DXGI.ModeDescription target = new DXGI.ModeDescription
{
Format = format,
Scaling = DXGI.DisplayModeScaling.Unspecified,
Width = PresentationParameters.BackBufferWidth,
Height = PresentationParameters.BackBufferHeight,
};

if (output == null)
{
width = PresentationParameters.BackBufferWidth;
height = PresentationParameters.BackBufferHeight;
}
else
{
DXGI.ModeDescription closest;
output.GetClosestMatchingMode(this.D3DDevice, target, out closest);
width = closest.Width;
height = closest.Height;
output.Dispose();
}
}

private void GetDisplayResolution(out int width, out int height)
{
width = Adapter.CurrentDisplayMode.Width;
height = Adapter.CurrentDisplayMode.Height;
}

#endif
Expand Down Expand Up @@ -496,24 +503,22 @@ internal void RefreshAdapter()
return;

DXGI.Output output = null;
try
{
output = _swapChain.ContainingOutput;
}
try { output = _swapChain.ContainingOutput; }
catch (DX.SharpDXException) { /* ContainingOutput fails on a headless device */ }

if (output != null)
{
foreach (GraphicsAdapter adapter in GraphicsAdapter.Adapters)
using (output)
{
if (adapter.DeviceName == output.Description.DeviceName)
foreach (GraphicsAdapter adapter in GraphicsAdapter.Adapters)
{
Adapter = adapter;
break;
if (adapter.DeviceName == output.Description.DeviceName)
{
Adapter = adapter;
break;
}
}
}

output.Dispose();
}
}
#endif
Expand Down

0 comments on commit 59326dd

Please sign in to comment.