A game framework written with osu! in mind.
1// Copyright (c) ppy Pty Ltd <contact@ppy.sh>. Licensed under the MIT Licence.
2// See the LICENCE file in the repository root for full licence text.
3
4using System.Runtime.CompilerServices;
5
6namespace osu.Framework.Graphics.OpenGL
7{
8 /// <summary>
9 /// The depth value used to draw 2D objects to the screen.
10 /// Starts at -1f and increments to 1f for each <see cref="Drawable"/> which draws a opaque interior through <see cref="DrawNode.DrawOpaqueInterior"/>.
11 /// </summary>
12 public class DepthValue
13 {
14 /// <summary>
15 /// A safe value, such that rounding issues don't occur within 16-bit float precision.
16 /// </summary>
17 private const float increment = 0.001f;
18
19 /// <summary>
20 /// Calculated as (1 - (-1)) / increment - 1.
21 /// -1 is subtracted since a depth of 1.0f conflicts with the default backbuffer clear value.
22 /// </summary>
23 private const int max_count = 1999;
24
25 private float depth;
26 private int count;
27
28 public DepthValue()
29 {
30 Reset();
31 }
32
33 /// <summary>
34 /// Increments the depth value.
35 /// </summary>
36 /// <returns>The post-incremented depth value.</returns>
37 [MethodImpl(MethodImplOptions.AggressiveInlining)]
38 internal float Increment()
39 {
40 if (count == max_count)
41 return depth;
42
43 depth += increment;
44 count++;
45
46 return depth;
47 }
48
49 /// <summary>
50 /// Reset to a pristine state.
51 /// </summary>
52 internal void Reset()
53 {
54 depth = -1;
55 count = 0;
56 }
57
58 /// <summary>
59 /// Whether the depth value can be incremented.
60 /// </summary>
61 internal bool CanIncrement
62 {
63 [MethodImpl(MethodImplOptions.AggressiveInlining)]
64 get => count < max_count;
65 }
66
67 [MethodImpl(MethodImplOptions.AggressiveInlining)]
68 public static implicit operator float(DepthValue d) => d.depth;
69 }
70}