Skip to content

Commit 9dc8995

Browse files
committed
Merge branch '1.20.1/dev' into 1.21.1/dev
2 parents a2a0210 + 0fd1ee7 commit 9dc8995

File tree

10 files changed

+139
-12
lines changed

10 files changed

+139
-12
lines changed

common/src/backend/java/dev/engine_room/flywheel/backend/engine/DrawManager.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -185,6 +185,12 @@ public void delete() {
185185

186186
public abstract void triggerFallback();
187187

188+
public abstract MeshPool meshPool();
189+
190+
public Map<InstancerKey<?>, N> instancers() {
191+
return instancers;
192+
}
193+
188194
protected record UninitializedInstancer<N, I extends Instance>(InstancerKey<I> key, N instancer) {
189195
}
190196
}

common/src/backend/java/dev/engine_room/flywheel/backend/engine/EngineImpl.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,10 @@ public LightStorage lightStorage() {
130130
return lightStorage;
131131
}
132132

133+
public DrawManager<? extends AbstractInstancer<?>> drawManager() {
134+
return drawManager;
135+
}
136+
133137
private class VisualizationContextImpl implements VisualizationContext {
134138
private final InstancerProviderImpl instancerProvider;
135139

common/src/backend/java/dev/engine_room/flywheel/backend/engine/MeshPool.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,10 @@ public void delete() {
137137
meshList.clear();
138138
}
139139

140+
public List<PooledMesh> pooledMeshes() {
141+
return meshList;
142+
}
143+
140144
public class PooledMesh extends ReferenceCounted {
141145
public static final int INVALID_BASE_VERTEX = -1;
142146

common/src/backend/java/dev/engine_room/flywheel/backend/engine/indirect/IndirectDrawManager.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -288,4 +288,9 @@ public void triggerFallback() {
288288
IndirectPrograms.kill();
289289
Minecraft.getInstance().levelRenderer.allChanged();
290290
}
291+
292+
@Override
293+
public MeshPool meshPool() {
294+
return meshPool;
295+
}
291296
}

common/src/backend/java/dev/engine_room/flywheel/backend/engine/instancing/InstancedDrawManager.java

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -160,7 +160,7 @@ private void submitDraws() {
160160

161161
uploadMaterialUniform(program, material);
162162

163-
program.setUInt("_flw_vertexOffset", drawCall.mesh()
163+
program.setUInt("_flw_baseVertex", drawCall.mesh()
164164
.baseVertex());
165165

166166
MaterialRenderState.setup(material);
@@ -184,7 +184,7 @@ private void submitOitDraws(PipelineCompiler.OitMode mode) {
184184

185185
uploadMaterialUniform(program, material);
186186

187-
program.setUInt("_flw_vertexOffset", drawCall.mesh()
187+
program.setUInt("_flw_baseVertex", drawCall.mesh()
188188
.baseVertex());
189189

190190
MaterialRenderState.setupOit(material);
@@ -304,6 +304,11 @@ public void triggerFallback() {
304304
Minecraft.getInstance().levelRenderer.allChanged();
305305
}
306306

307+
@Override
308+
public MeshPool meshPool() {
309+
return meshPool;
310+
}
311+
307312
public static void uploadMaterialUniform(GlProgram program, Material material) {
308313
int packedFogAndCutout = MaterialEncoder.packUberShader(material);
309314
int packedMaterialProperties = MaterialEncoder.packProperties(material);

common/src/backend/resources/assets/flywheel/flywheel/internal/api_impl.vert

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,4 +13,4 @@ out float flw_distance;
1313

1414
FlwMaterial flw_material;
1515

16-
#define flw_vertexId gl_VertexID
16+
uint flw_vertexId;

common/src/backend/resources/assets/flywheel/flywheel/internal/common.vert

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,9 @@ mat3 _flw_normalMatrix;
7575
flat out uvec2 _flw_ids;
7676
#endif
7777

78-
void _flw_main(in FlwInstance instance, in uint stableInstanceID, in uint modelID) {
78+
void _flw_main(in FlwInstance instance, in uint stableInstanceID, in uint baseVertex) {
79+
flw_vertexId = gl_VertexID - baseVertex;
80+
7981
_flw_layoutVertex();
8082
flw_instanceVertex(instance);
8183
flw_materialVertex();
@@ -96,6 +98,6 @@ void _flw_main(in FlwInstance instance, in uint stableInstanceID, in uint modelI
9698
gl_Position = flw_viewProjection * flw_vertexPos;
9799

98100
#ifdef _FLW_DEBUG
99-
_flw_ids = uvec2(stableInstanceID, modelID);
101+
_flw_ids = uvec2(stableInstanceID, baseVertex);
100102
#endif
101103
}

common/src/backend/resources/assets/flywheel/flywheel/internal/instancing/main.vert

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ uniform mat4 _flw_modelMatrixUniform;
1010
uniform mat3 _flw_normalMatrixUniform;
1111
#endif
1212

13-
uniform uint _flw_vertexOffset;
13+
uniform uint _flw_baseVertex;
1414

1515
void main() {
1616
_flw_unpackMaterialProperties(_flw_packedMaterial.y, flw_material);
@@ -22,5 +22,5 @@ void main() {
2222
_flw_normalMatrix = _flw_normalMatrixUniform;
2323
#endif
2424

25-
_flw_main(instance, uint(gl_InstanceID), _flw_vertexOffset);
25+
_flw_main(instance, uint(gl_InstanceID), _flw_baseVertex);
2626
}

common/src/main/java/dev/engine_room/flywheel/impl/FlwDebugInfo.java

Lines changed: 103 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,15 @@
55
import org.jetbrains.annotations.Nullable;
66

77
import dev.engine_room.flywheel.api.visualization.VisualizationManager;
8+
import dev.engine_room.flywheel.backend.engine.AbstractInstancer;
9+
import dev.engine_room.flywheel.backend.engine.DrawManager;
810
import dev.engine_room.flywheel.backend.gl.GlCompat;
911
import dev.engine_room.flywheel.impl.visualization.VisualizationManagerImpl;
1012
import dev.engine_room.flywheel.lib.memory.FlwMemoryTracker;
1113
import dev.engine_room.flywheel.lib.util.StringUtil;
14+
import it.unimi.dsi.fastutil.ints.IntArrayList;
15+
import it.unimi.dsi.fastutil.ints.IntComparators;
16+
import it.unimi.dsi.fastutil.ints.IntList;
1217
import net.minecraft.ChatFormatting;
1318
import net.minecraft.client.Minecraft;
1419
import net.minecraft.core.Vec3i;
@@ -32,10 +37,15 @@ public static StringBuilder appendLine(StringBuilder dst, String str) {
3237
}
3338

3439
/**
35-
* Append a header to the given StringBuilder, preceded by two new lines for separation.
40+
* Append a header to the given StringBuilder.
3641
*/
3742
public static void appendHeader(StringBuilder dst, String str) {
38-
dst.append("\n\n## ");
43+
dst.append("\n## ");
44+
dst.append(str);
45+
}
46+
47+
public static void appendHeader2(StringBuilder dst, String str) {
48+
dst.append("\n### ");
3949
dst.append(str);
4050
}
4151

@@ -92,7 +102,93 @@ private static void addBackendDebugInfo(@Nullable VisualizationManagerImpl manag
92102
var lut = engineImpl.lightStorage()
93103
.createLut();
94104

95-
appendLine(out, "Light LUT Size: ").append(lut.size() * Integer.BYTES);
105+
appendLine(out, "Light LUT Size: ").append(lut.size() * Integer.BYTES)
106+
.append(" bytes");
107+
108+
DrawManager<? extends AbstractInstancer<?>> drawManager = engineImpl.drawManager();
109+
addMeshDebugInfo(out, drawManager);
110+
addInstancerDebugInfo(out, drawManager);
111+
}
112+
113+
private static void addInstancerDebugInfo(StringBuilder out, DrawManager<? extends AbstractInstancer<?>> drawManager) {
114+
appendHeader2(out, "Instancers");
115+
116+
var instancers = drawManager.instancers();
117+
118+
appendLine(out, "Count: ").append(instancers.size());
119+
120+
{
121+
IntList meshCountsToSort = new IntArrayList();
122+
for (var instancerKey : instancers.keySet()) {
123+
meshCountsToSort.add(instancerKey.model()
124+
.meshes()
125+
.size());
126+
}
127+
appendPercentiles(out, "Mesh Count Percentiles", meshCountsToSort);
128+
}
129+
130+
{
131+
int totalInstanceCount = 0;
132+
IntList instanceCountsToSort = new IntArrayList();
133+
for (var instancer : instancers.values()) {
134+
var instanceCount = instancer.instanceCount();
135+
totalInstanceCount += instanceCount;
136+
instanceCountsToSort.add(instanceCount);
137+
}
138+
appendLine(out, "Total Instance Count: ").append(totalInstanceCount);
139+
appendPercentiles(out, "Instance Count Percentiles", instanceCountsToSort);
140+
}
141+
}
142+
143+
private static void addMeshDebugInfo(StringBuilder out, DrawManager<? extends AbstractInstancer<?>> drawManager) {
144+
var meshPool = drawManager.meshPool()
145+
.pooledMeshes();
146+
147+
appendHeader2(out, "Meshes");
148+
149+
var numMeshes = meshPool.size();
150+
151+
appendLine(out, "Count: ").append(numMeshes);
152+
153+
{
154+
int totalVertices = 0;
155+
IntList vertexCountsToSort = new IntArrayList();
156+
for (var pooledMesh : meshPool) {
157+
int vertexCount = pooledMesh.vertexCount();
158+
159+
vertexCountsToSort.add(vertexCount);
160+
totalVertices += vertexCount;
161+
}
162+
163+
appendLine(out, "Total Vertex Count: ").append(totalVertices);
164+
appendPercentiles(out, "Vertex Count Percentiles", vertexCountsToSort);
165+
}
166+
}
167+
168+
private static void appendPercentiles(StringBuilder out, String prefix, IntList unsortedCounts) {
169+
var size = unsortedCounts.size();
170+
171+
if (size == 0) {
172+
// Append
173+
appendLine(out, "Empty dataset, no percentiles.");
174+
return;
175+
}
176+
177+
unsortedCounts.sort(IntComparators.NATURAL_COMPARATOR);
178+
179+
int p10Index = Math.min(size / 10, size - 1);
180+
int p50Index = Math.min(size / 2, size - 1);
181+
int p90Index = Math.min(size * 9 / 10, size - 1);
182+
183+
appendLine(out, prefix).append(":\n ")
184+
.append("P10: ")
185+
.append(unsortedCounts.getInt(p10Index))
186+
.append(", P50: ")
187+
.append(unsortedCounts.getInt(p50Index))
188+
.append(", P90: ")
189+
.append(unsortedCounts.getInt(p90Index))
190+
.append(", Max: ")
191+
.append(unsortedCounts.getInt(size - 1));
96192
}
97193

98194
private static void addVisualizationManagerDebugInfo(@Nullable VisualizationManagerImpl manager, StringBuilder out) {
@@ -137,8 +233,10 @@ private static void addSystemDebugInfo(StringBuilder out) {
137233
.append(" (")
138234
.append(System.getProperty("os.arch"))
139235
.append(")");
140-
appendLine(out, "Flw CPU Memory: ").append(FlwMemoryTracker.getCpuMemory());
141-
appendLine(out, "Flw GPU Memory: ").append(FlwMemoryTracker.getGpuMemory());
236+
appendLine(out, "Flw CPU Memory: ").append(FlwMemoryTracker.getCpuMemory())
237+
.append(" bytes");
238+
appendLine(out, "Flw GPU Memory: ").append(FlwMemoryTracker.getGpuMemory())
239+
.append(" bytes");
142240
}
143241

144242
private static void addOpenGLDebugInfo(StringBuilder out) {

docs/flywheel/api/snippets/vertex.glsl

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,9 @@ ivec2 flw_vertexOverlay;
88
vec2 flw_vertexLight;
99
vec3 flw_vertexNormal;
1010

11+
// The index of the current vertex in the mesh that is being drawn.
12+
/*const*/ uint flw_vertexId;
13+
1114
/*const*/ FlwMaterial flw_material;
1215

1316
// To be implemented by the instance shader.

0 commit comments

Comments
 (0)