Skip to content

Commit 8a690ea

Browse files
committed
OpenGL: GraphicsAdapter.Outputs is now properly populated
1 parent b4752b8 commit 8a690ea

2 files changed

Lines changed: 89 additions & 4 deletions

File tree

sources/engine/Stride.Graphics/OpenGL/GraphicsAdapter.OpenGL.cs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,15 +21,19 @@ public unsafe partial class GraphicsAdapter
2121

2222
internal GraphicsAdapter()
2323
{
24-
outputs = new [] { new GraphicsOutput() };
25-
2624
// set default values
2725
int detectedVersion = 100;
2826

2927
string renderer, vendor, version;
3028
int versionMajor, versionMinor;
3129

3230
var SDL = Stride.Graphics.SDL.Window.SDL;
31+
32+
//Initialize outputs
33+
outputs = new GraphicsOutput[SDL.GetNumVideoDisplays()];
34+
for (int i = 0; i < outputs.Length; i++)
35+
outputs[i] = new GraphicsOutput(this, i);
36+
3337
// Some platforms (i.e. Android) can only have a single window
3438
var sdlWindow = DefaultWindow;
3539
if (sdlWindow == null)

sources/engine/Stride.Graphics/OpenGL/GraphicsOutput.OpenGL.cs

Lines changed: 83 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,66 @@
33
#if STRIDE_GRAPHICS_API_OPENGL
44

55
using System;
6+
using System.Collections.Generic;
7+
using System.Linq;
68

79
namespace Stride.Graphics
810
{
911
public partial class GraphicsOutput
1012
{
13+
/// <summary>
14+
/// Initializes a new instance of <see cref="GraphicsOutput" />.
15+
/// </summary>
16+
/// <param name="adapter">The adapter.</param>
17+
/// <param name="displayIndex">Index of the output.</param>
18+
/// <exception cref="System.ArgumentNullException">output</exception>
19+
public GraphicsOutput(GraphicsAdapter adapter, int displayIndex)
20+
{
21+
if (adapter == null) throw new ArgumentNullException("adapter");
22+
23+
this.adapter = adapter;
24+
this.displayIndex = displayIndex;
25+
26+
var SDL = Stride.Graphics.SDL.Window.SDL;
27+
28+
unsafe
29+
{
30+
var bounds = new Silk.NET.Maths.Rectangle<int>();
31+
SDL.GetDisplayBounds(displayIndex, &bounds);
32+
desktopBounds.Width = bounds.Size.X;
33+
desktopBounds.Height = bounds.Size.Y;
34+
desktopBounds.X = bounds.Origin.X;
35+
desktopBounds.Y = bounds.Origin.Y;
36+
}
37+
38+
DisplayName = SDL.GetDisplayNameS(displayIndex);
39+
}
40+
41+
private readonly int displayIndex;
42+
43+
public string DisplayName { get; init; }
44+
45+
/// <summary>
46+
/// Find the display mode that most closely matches the requested display mode.
47+
/// </summary>
48+
/// <param name="targetProfiles">The target profile, as available formats are different depending on the feature level..</param>
49+
/// <param name="mode">The mode.</param>
50+
/// <returns>Returns the closes display mode.</returns>
1151
public DisplayMode FindClosestMatchingDisplayMode(GraphicsProfile[] targetProfiles, DisplayMode mode)
1252
{
13-
return mode;
53+
var SDL = Stride.Graphics.SDL.Window.SDL;
54+
55+
DisplayMode closest;
56+
unsafe
57+
{
58+
var modeSDL = new Silk.NET.SDL.DisplayMode(0, mode.Width, mode.Height, mode.RefreshRate.Denominator / mode.RefreshRate.Numerator);
59+
var closestSDL = new Silk.NET.SDL.DisplayMode();
60+
61+
SDL.GetClosestDisplayMode(displayIndex, &modeSDL, &closestSDL);
62+
closest = new DisplayMode(PixelFormat.None, closestSDL.W, closestSDL.H, new Rational(1, closestSDL.RefreshRate));
63+
}
64+
65+
return closest;
1466
}
1567

1668
public IntPtr MonitorHandle
@@ -20,11 +72,40 @@ public IntPtr MonitorHandle
2072

2173
private void InitializeSupportedDisplayModes()
2274
{
75+
var SDL = Stride.Graphics.SDL.Window.SDL;
76+
77+
var modesMap = new Dictionary<string, DisplayMode>();
78+
var modeCount = SDL.GetNumDisplayModes(displayIndex);
79+
80+
unsafe
81+
{
82+
for (int i = 0; i < modeCount; i++)
83+
{
84+
var sdlMode = new Silk.NET.SDL.DisplayMode();
85+
SDL.GetDisplayMode(displayIndex, i, &sdlMode);
86+
87+
var key = $"{sdlMode.W};{sdlMode.H};{sdlMode.RefreshRate}";
88+
89+
if (!modesMap.ContainsKey(key))
90+
{
91+
//We should probably convert the sdlMode format
92+
//to the engine's Pixel Format
93+
modesMap.Add(key, new DisplayMode(PixelFormat.None, sdlMode.W, sdlMode.H, new Rational(1, sdlMode.RefreshRate)));
94+
}
95+
}
96+
}
97+
98+
supportedDisplayModes = modesMap
99+
.Select(x => x.Value)
100+
.ToArray();
23101
}
24102

25103
private void InitializeCurrentDisplayMode()
26104
{
27-
currentDisplayMode = null;
105+
var SDL = Stride.Graphics.SDL.Window.SDL;
106+
var mode = new DisplayMode(PixelFormat.None, desktopBounds.Width, desktopBounds.Height, new Rational(1, 0));
107+
108+
currentDisplayMode = FindClosestMatchingDisplayMode([], mode);
28109
}
29110
}
30111
}

0 commit comments

Comments
 (0)