Skip to content

Commit 4aee7f5

Browse files
committed
profile cpu to gpu sync
1 parent ccad1d6 commit 4aee7f5

6 files changed

Lines changed: 53 additions & 8 deletions

File tree

Editor/EditorWindow.cs

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -137,13 +137,27 @@ private void DoRender(FrameEventArgs args)
137137
try
138138
{
139139
Manager.Render();
140+
WaitForGpu();
141+
SwapBuffers();
140142
}
141143
finally
142144
{
143145
Profiler.EndFrame();
144146
}
147+
}
148+
149+
private const int MaxFramesInFlight = 2;
150+
private readonly Queue<IntPtr> _frameFences = new();
151+
private void WaitForGpu()
152+
{
153+
using var _ = Profiler.Cpu("Wait for GPU");
145154

146-
SwapBuffers();
155+
_frameFences.Enqueue(GL.FenceSync(SyncCondition.SyncGpuCommandsComplete, WaitSyncFlags.None));
156+
if (_frameFences.Count <= MaxFramesInFlight) return;
157+
158+
var fence = _frameFences.Dequeue();
159+
GL.ClientWaitSync(fence, ClientWaitSyncFlags.SyncFlushCommandsBit, long.MaxValue);
160+
GL.DeleteSync(fence);
147161
}
148162

149163
private void DoTextInput(TextInputEventArgs e)
@@ -167,6 +181,11 @@ protected override void Dispose(bool disposing)
167181
{
168182
CursorState = CursorState.Normal;
169183
Manager.Dispose();
184+
185+
while (_frameFences.TryDequeue(out var fence))
186+
{
187+
GL.DeleteSync(fence);
188+
}
170189
}
171190

172191
base.Dispose(disposing);

Snooper/Core/Containers/Resources/CullView.cs

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,26 @@
33

44
namespace Snooper.Core.Containers.Resources;
55

6-
public readonly struct CullView(IViewProjectionProvider view, CameraComponent lodReference)
6+
public readonly struct CullView
77
{
8-
public readonly Matrix4x4 ViewProjection = view.ViewMatrix * view.ProjectionMatrix;
9-
public readonly Vector3 LodReferencePosition = lodReference.InverseViewMatrix.Translation;
10-
public readonly float LodProjectionScale = lodReference.ProjectionMatrix.M22;
8+
public readonly Matrix4x4 ViewProjection;
9+
public readonly Vector3 LodReferencePosition;
10+
public readonly float LodProjectionScale;
11+
public readonly float LodOrthoExtent;
12+
13+
public CullView(IViewProjectionProvider view, CameraComponent lodReference)
14+
{
15+
ViewProjection = view.ViewMatrix * view.ProjectionMatrix;
16+
LodReferencePosition = lodReference.InverseViewMatrix.Translation;
17+
LodProjectionScale = lodReference.ProjectionMatrix.M22;
18+
LodOrthoExtent = 0.0f;
19+
}
20+
21+
public CullView(ShadowMapView view, CameraComponent distanceReference)
22+
{
23+
ViewProjection = view.ViewProjection;
24+
LodReferencePosition = distanceReference.InverseViewMatrix.Translation;
25+
LodProjectionScale = 0.0f;
26+
LodOrthoExtent = view.OrthoExtent;
27+
}
1128
}

Snooper/Core/Containers/Resources/CullingResources.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,7 @@ public void UpdateOverrideLod(BufferAllocation allocation, int overrideLod)
8080

8181
private readonly Plane[] _planes = new Plane[Settings.MaxCullingViews * 6];
8282
private readonly Vector4[] _lodReferences = new Vector4[Settings.MaxCullingViews];
83+
private readonly float[] _lodOrthoExtents = new float[Settings.MaxCullingViews];
8384

8485
public void Cull<TInstanceData>(ReadOnlySpan<CullView> views, ShaderStorageBuffer<TInstanceData> instances, IndirectDrawBuffer commands) where TInstanceData : unmanaged, IPerInstanceData
8586
{
@@ -98,11 +99,13 @@ public void Cull<TInstanceData>(ReadOnlySpan<CullView> views, ShaderStorageBuffe
9899
_planes[b + 5] = new Plane(matrix.M14 - matrix.M13, matrix.M24 - matrix.M23, matrix.M34 - matrix.M33, matrix.M44 - matrix.M43); // Top
99100

100101
_lodReferences[i] = new Vector4(views[i].LodReferencePosition, views[i].LodProjectionScale);
102+
_lodOrthoExtents[i] = views[i].LodOrthoExtent;
101103
}
102104

103105
_compute.Use();
104106
_compute.SetUniform("uFrustumPlanes", _planes);
105107
_compute.SetUniform("uLodReference", _lodReferences);
108+
_compute.SetUniform("uLodOrthoExtent", _lodOrthoExtents);
106109
_compute.SetUniform("uViewCount", (uint) viewCount);
107110
_compute.SetUniform("uViewCapacity", (uint) commands.Capacity);
108111

Snooper/Rendering/Components/Camera/ShadowMapView.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,14 @@ namespace Snooper.Rendering.Components.Camera;
1111
public Matrix4x4 ViewProjection { get; }
1212

1313
public int Slot { get; }
14+
public float OrthoExtent { get; }
1415
public float TexelWorldSize { get; }
1516
public float DepthScale { get; }
1617
public float SplitFar { get; }
1718

1819
public int ViewIndex => Slot + 1;
1920

20-
public ShadowMapView(Matrix4x4 viewMatrix, Matrix4x4 projectionMatrix, int slot, float texelWorldSize, float depthScale, float splitFar)
21+
public ShadowMapView(Matrix4x4 viewMatrix, Matrix4x4 projectionMatrix, int slot, float orthoExtent, float texelWorldSize, float depthScale, float splitFar)
2122
{
2223
ViewMatrix = viewMatrix;
2324
ProjectionMatrix = projectionMatrix;
@@ -26,6 +27,7 @@ public ShadowMapView(Matrix4x4 viewMatrix, Matrix4x4 projectionMatrix, int slot,
2627
ViewProjection = viewMatrix * projectionMatrix;
2728

2829
Slot = slot;
30+
OrthoExtent = orthoExtent;
2931
TexelWorldSize = texelWorldSize;
3032
DepthScale = depthScale;
3133
SplitFar = splitFar;

Snooper/Rendering/Containers/Framebuffers/SunCascades.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ public ShadowMapView[] Update(CameraComponent camera, DirectionalLightComponent
9898
// default [-1, 1] clip range, so window depth only ever spans [0.5, 1.0]
9999
var depthScale = 0.5f / depthRange;
100100

101-
Views[i] = new ShadowMapView(viewMatrix, projectionMatrix, FirstSlot + i, texelWorldSize, depthScale, far);
101+
Views[i] = new ShadowMapView(viewMatrix, projectionMatrix, FirstSlot + i, radius, texelWorldSize, depthScale, far);
102102
}
103103

104104
return Views;

Snooper/Shaders/culling.comp

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ layout(std430, binding = BINDING_CULL_SECTIONS) buffer SectionDescriptorsBuffer
3939

4040
uniform vec4 uFrustumPlanes[MAX_CULLING_VIEWS * 6];
4141
uniform vec4 uLodReference[MAX_CULLING_VIEWS];
42+
uniform float uLodOrthoExtent[MAX_CULLING_VIEWS]; // > 0 marks the view orthographic, and is its half-width in world units
4243

4344
uniform uint uViewCount;
4445
uniform uint uViewCapacity;
@@ -106,6 +107,7 @@ void main()
106107

107108
vec3 lodRefPos = uLodReference[view].xyz;
108109
float lodScale = uLodReference[view].w;
110+
float lodOrthoExtent = uLodOrthoExtent[view];
109111

110112
uint localMin = 0xFFFFFFFFu;
111113
uint localMax = 0;
@@ -181,7 +183,9 @@ void main()
181183
else
182184
{
183185
// Automatic LOD selection based on screen size
184-
float screenRadius = mesh.SphereRadius * lodScale / sClosestDist[0];
186+
float screenRadius = lodOrthoExtent > 0.0
187+
? mesh.SphereRadius / lodOrthoExtent
188+
: mesh.SphereRadius * lodScale / sClosestDist[0];
185189
float screenSize = screenRadius * 2.0;
186190

187191
for (uint i = 1u; i <= mesh.MaxLOD; ++i)

0 commit comments

Comments
 (0)