Skip to content

Commit

Permalink
Use sensible defaults for depth/stencil (#1991)
Browse files Browse the repository at this point in the history
* Use sensible defaults for depth/stencil

* Slightly adjust the SDL logic to try and encourage system opinions for -1
  • Loading branch information
Perksey committed Apr 18, 2024
1 parent 5f81010 commit 0f4cce8
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 25 deletions.
2 changes: 1 addition & 1 deletion examples/CSharp/Directory.Build.props
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project>
<PropertyGroup>
<AssemblyName>Tutorial</AssemblyName>
<LangVersion>9</LangVersion>
<LangVersion>latest</LangVersion>
<WarningsAsErrors>0618</WarningsAsErrors>
</PropertyGroup>
</Project>
Original file line number Diff line number Diff line change
Expand Up @@ -78,15 +78,17 @@ public interface IViewProperties
/// Preferred depth buffer bits of the window's framebuffer.
/// </summary>
/// <remarks>
/// Pass <c>null</c> or <c>-1</c> to use the system default.
/// Pass <c>-1</c> to always use the system/backend default. Uses <c>24</c> as the default if <c>null</c> and
/// <see cref="PreferredStencilBufferBits"/> is also <c>null</c>
/// </remarks>
int? PreferredDepthBufferBits { get; }

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

Expand All @@ -106,4 +108,4 @@ public interface IViewProperties
/// </remarks>
int? Samples { get; }
}
}
}
14 changes: 12 additions & 2 deletions src/Windowing/Silk.NET.Windowing.Glfw/GlfwWindow.cs
Original file line number Diff line number Diff line change
Expand Up @@ -376,8 +376,18 @@ protected override void CoreInitialize(WindowOptions opts)
// Set video mode (-1 = don't care)

_glfw.WindowHint(WindowHintInt.RefreshRate, opts.VideoMode.RefreshRate ?? GLFW.Glfw.DontCare);
_glfw.WindowHint(WindowHintInt.DepthBits, opts.PreferredDepthBufferBits ?? GLFW.Glfw.DontCare);
_glfw.WindowHint(WindowHintInt.StencilBits, opts.PreferredStencilBufferBits ?? GLFW.Glfw.DontCare);
_glfw.WindowHint(WindowHintInt.DepthBits, opts.PreferredDepthBufferBits switch
{
null when opts.PreferredStencilBufferBits is null => 24,
{} x => x,
_ => GLFW.Glfw.DontCare
});
_glfw.WindowHint(WindowHintInt.StencilBits, opts.PreferredStencilBufferBits switch
{
null when opts.PreferredDepthBufferBits is null => 8,
{} x => x,
_ => GLFW.Glfw.DontCare
});

_glfw.WindowHint(WindowHintInt.RedBits, opts.PreferredBitDepth?.X ?? GLFW.Glfw.DontCare);
_glfw.WindowHint(WindowHintInt.GreenBits, opts.PreferredBitDepth?.Y ?? GLFW.Glfw.DontCare);
Expand Down
68 changes: 49 additions & 19 deletions src/Windowing/Silk.NET.Windowing.Sdl/SdlView.cs
Original file line number Diff line number Diff line change
Expand Up @@ -149,29 +149,59 @@ protected void CoreInitialize
IsClosingVal = false;

// Set window GL attributes
Sdl.GLSetAttribute(GLattr.DepthSize,
opts.PreferredDepthBufferBits is null || opts.PreferredDepthBufferBits == -1
? 24 : opts.PreferredDepthBufferBits.Value);

Sdl.GLSetAttribute(GLattr.StencilSize,
opts.PreferredStencilBufferBits is null || opts.PreferredStencilBufferBits == -1
? 8 : opts.PreferredStencilBufferBits.Value);
if (opts.PreferredDepthBufferBits != -1)
{
Sdl.GLSetAttribute
(
GLattr.DepthSize,
opts.PreferredDepthBufferBits ?? 24
);
}

Sdl.GLSetAttribute(GLattr.RedSize,
opts.PreferredBitDepth is null || opts.PreferredBitDepth.Value.X == -1
? 8 : opts.PreferredBitDepth.Value.X);
if (opts.PreferredStencilBufferBits != -1)
{
Sdl.GLSetAttribute
(
GLattr.StencilSize,
opts.PreferredStencilBufferBits ?? 8
);
}

Sdl.GLSetAttribute(GLattr.GreenSize,
opts.PreferredBitDepth is null || opts.PreferredBitDepth.Value.Y == -1
? 8 : opts.PreferredBitDepth.Value.Y);
if (opts.PreferredBitDepth?.X != -1)
{
Sdl.GLSetAttribute
(
GLattr.RedSize,
opts.PreferredBitDepth?.X ?? 8
);
}

Sdl.GLSetAttribute(GLattr.BlueSize,
opts.PreferredBitDepth is null || opts.PreferredBitDepth.Value.Z == -1
? 8 : opts.PreferredBitDepth.Value.Z);
if (opts.PreferredBitDepth?.Y != -1)
{
Sdl.GLSetAttribute
(
GLattr.GreenSize,
opts.PreferredBitDepth?.Y ?? 8
);
}

Sdl.GLSetAttribute(GLattr.AlphaSize,
opts.PreferredBitDepth is null || opts.PreferredBitDepth.Value.W == -1
? 8 : opts.PreferredBitDepth.Value.W);
if (opts.PreferredBitDepth?.Z != -1)
{
Sdl.GLSetAttribute
(
GLattr.BlueSize,
opts.PreferredBitDepth?.Z ?? 8
);
}

if (opts.PreferredBitDepth?.W != -1)
{
Sdl.GLSetAttribute
(
GLattr.AlphaSize,
opts.PreferredBitDepth?.W ?? 8
);
}

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

0 comments on commit 0f4cce8

Please sign in to comment.