Skip to content

Commit 0f4cce8

Browse files
authored
Use sensible defaults for depth/stencil (#1991)
* Use sensible defaults for depth/stencil * Slightly adjust the SDL logic to try and encourage system opinions for -1
1 parent 5f81010 commit 0f4cce8

File tree

4 files changed

+67
-25
lines changed

4 files changed

+67
-25
lines changed

examples/CSharp/Directory.Build.props

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<Project>
22
<PropertyGroup>
33
<AssemblyName>Tutorial</AssemblyName>
4-
<LangVersion>9</LangVersion>
4+
<LangVersion>latest</LangVersion>
55
<WarningsAsErrors>0618</WarningsAsErrors>
66
</PropertyGroup>
77
</Project>

src/Windowing/Silk.NET.Windowing.Common/Interfaces/IViewProperties.cs

+5-3
Original file line numberDiff line numberDiff line change
@@ -78,15 +78,17 @@ public interface IViewProperties
7878
/// Preferred depth buffer bits of the window's framebuffer.
7979
/// </summary>
8080
/// <remarks>
81-
/// Pass <c>null</c> or <c>-1</c> to use the system default.
81+
/// Pass <c>-1</c> to always use the system/backend default. Uses <c>24</c> as the default if <c>null</c> and
82+
/// <see cref="PreferredStencilBufferBits"/> is also <c>null</c>
8283
/// </remarks>
8384
int? PreferredDepthBufferBits { get; }
8485

8586
/// <summary>
8687
/// Preferred stencil buffer bits of the window's framebuffer.
8788
/// </summary>
8889
/// <remarks>
89-
/// Pass <c>null</c> or <c>-1</c> to use the system default.
90+
/// Pass <c>-1</c> to always use the system/backend default. Uses <c>8</c> as the default if <c>null</c> and
91+
/// <see cref="PreferredDepthBufferBits"/> is also <c>null</c>
9092
/// </remarks>
9193
int? PreferredStencilBufferBits { get; }
9294

@@ -106,4 +108,4 @@ public interface IViewProperties
106108
/// </remarks>
107109
int? Samples { get; }
108110
}
109-
}
111+
}

src/Windowing/Silk.NET.Windowing.Glfw/GlfwWindow.cs

+12-2
Original file line numberDiff line numberDiff line change
@@ -376,8 +376,18 @@ protected override void CoreInitialize(WindowOptions opts)
376376
// Set video mode (-1 = don't care)
377377

378378
_glfw.WindowHint(WindowHintInt.RefreshRate, opts.VideoMode.RefreshRate ?? GLFW.Glfw.DontCare);
379-
_glfw.WindowHint(WindowHintInt.DepthBits, opts.PreferredDepthBufferBits ?? GLFW.Glfw.DontCare);
380-
_glfw.WindowHint(WindowHintInt.StencilBits, opts.PreferredStencilBufferBits ?? GLFW.Glfw.DontCare);
379+
_glfw.WindowHint(WindowHintInt.DepthBits, opts.PreferredDepthBufferBits switch
380+
{
381+
null when opts.PreferredStencilBufferBits is null => 24,
382+
{} x => x,
383+
_ => GLFW.Glfw.DontCare
384+
});
385+
_glfw.WindowHint(WindowHintInt.StencilBits, opts.PreferredStencilBufferBits switch
386+
{
387+
null when opts.PreferredDepthBufferBits is null => 8,
388+
{} x => x,
389+
_ => GLFW.Glfw.DontCare
390+
});
381391

382392
_glfw.WindowHint(WindowHintInt.RedBits, opts.PreferredBitDepth?.X ?? GLFW.Glfw.DontCare);
383393
_glfw.WindowHint(WindowHintInt.GreenBits, opts.PreferredBitDepth?.Y ?? GLFW.Glfw.DontCare);

src/Windowing/Silk.NET.Windowing.Sdl/SdlView.cs

+49-19
Original file line numberDiff line numberDiff line change
@@ -149,29 +149,59 @@ protected void CoreInitialize
149149
IsClosingVal = false;
150150

151151
// Set window GL attributes
152-
Sdl.GLSetAttribute(GLattr.DepthSize,
153-
opts.PreferredDepthBufferBits is null || opts.PreferredDepthBufferBits == -1
154-
? 24 : opts.PreferredDepthBufferBits.Value);
155-
156-
Sdl.GLSetAttribute(GLattr.StencilSize,
157-
opts.PreferredStencilBufferBits is null || opts.PreferredStencilBufferBits == -1
158-
? 8 : opts.PreferredStencilBufferBits.Value);
152+
if (opts.PreferredDepthBufferBits != -1)
153+
{
154+
Sdl.GLSetAttribute
155+
(
156+
GLattr.DepthSize,
157+
opts.PreferredDepthBufferBits ?? 24
158+
);
159+
}
159160

160-
Sdl.GLSetAttribute(GLattr.RedSize,
161-
opts.PreferredBitDepth is null || opts.PreferredBitDepth.Value.X == -1
162-
? 8 : opts.PreferredBitDepth.Value.X);
161+
if (opts.PreferredStencilBufferBits != -1)
162+
{
163+
Sdl.GLSetAttribute
164+
(
165+
GLattr.StencilSize,
166+
opts.PreferredStencilBufferBits ?? 8
167+
);
168+
}
163169

164-
Sdl.GLSetAttribute(GLattr.GreenSize,
165-
opts.PreferredBitDepth is null || opts.PreferredBitDepth.Value.Y == -1
166-
? 8 : opts.PreferredBitDepth.Value.Y);
170+
if (opts.PreferredBitDepth?.X != -1)
171+
{
172+
Sdl.GLSetAttribute
173+
(
174+
GLattr.RedSize,
175+
opts.PreferredBitDepth?.X ?? 8
176+
);
177+
}
167178

168-
Sdl.GLSetAttribute(GLattr.BlueSize,
169-
opts.PreferredBitDepth is null || opts.PreferredBitDepth.Value.Z == -1
170-
? 8 : opts.PreferredBitDepth.Value.Z);
179+
if (opts.PreferredBitDepth?.Y != -1)
180+
{
181+
Sdl.GLSetAttribute
182+
(
183+
GLattr.GreenSize,
184+
opts.PreferredBitDepth?.Y ?? 8
185+
);
186+
}
171187

172-
Sdl.GLSetAttribute(GLattr.AlphaSize,
173-
opts.PreferredBitDepth is null || opts.PreferredBitDepth.Value.W == -1
174-
? 8 : opts.PreferredBitDepth.Value.W);
188+
if (opts.PreferredBitDepth?.Z != -1)
189+
{
190+
Sdl.GLSetAttribute
191+
(
192+
GLattr.BlueSize,
193+
opts.PreferredBitDepth?.Z ?? 8
194+
);
195+
}
196+
197+
if (opts.PreferredBitDepth?.W != -1)
198+
{
199+
Sdl.GLSetAttribute
200+
(
201+
GLattr.AlphaSize,
202+
opts.PreferredBitDepth?.W ?? 8
203+
);
204+
}
175205

176206
Sdl.GLSetAttribute(GLattr.Multisamplebuffers, (opts.Samples == null || opts.Samples == -1) ? 0 : 1);
177207
Sdl.GLSetAttribute(GLattr.Multisamplesamples, (opts.Samples == null || opts.Samples == -1) ? 0 : opts.Samples.Value);

0 commit comments

Comments
 (0)