Skip to content

Commit

Permalink
Merge branch 'spike/jmx' into spike/gaugeMap
Browse files Browse the repository at this point in the history
  • Loading branch information
keturn committed Nov 16, 2021
2 parents 8bd533e + eaa7bf9 commit 036f139
Show file tree
Hide file tree
Showing 10 changed files with 178 additions and 46 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,9 @@ public class SkeletalMeshComponent implements VisualComponent<SkeletalMeshCompon
public EntityRef rootBone = EntityRef.NULL;
public float animationTime;

@Replicate
public Vector3f scale = new Vector3f(1, 1, 1);
@Replicate
public Vector3f translate = new Vector3f();

@Replicate
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -256,7 +256,6 @@ public void renderOpaque() {
location.getWorldPosition(worldPos);
float worldScale = location.getWorldScale();


aabb = aabb.transform(new Matrix4f().translationRotateScale(worldPos, worldRot, worldScale), new AABBf());

//Scale bounding box for skeletalMesh.
Expand Down Expand Up @@ -310,9 +309,9 @@ public void renderOpaque() {
} else {
logger.warn("Unable to resolve bone \"{}\"", bone.getName());
boneTransforms[bone.getIndex()] = new Matrix4f();

}
}

((OpenGLSkeletalMesh) skeletalMesh.mesh).setScaleTranslate(skeletalMesh.scale, skeletalMesh.translate);
((OpenGLSkeletalMesh) skeletalMesh.mesh).render(Arrays.asList(boneTransforms));
}
Expand All @@ -329,42 +328,75 @@ public void renderOverlay() {

Vector3f cameraPosition = worldRenderer.getActiveCamera().getPosition();

Vector3f worldPos = new Vector3f();
Vector3f worldPositionCameraSpace = new Vector3f();
worldPos.sub(cameraPosition, worldPositionCameraSpace);
Matrix4f matrixCameraSpace = new Matrix4f().translationRotateScale(worldPositionCameraSpace, new Quaternionf(), 1.0f);
Matrix4f modelViewMatrix = new Matrix4f(worldRenderer.getActiveCamera().getViewMatrix()).mul(matrixCameraSpace);
material.setMatrix4("projectionMatrix", worldRenderer.getActiveCamera().getProjectionMatrix());
material.setMatrix4("modelViewMatrix", modelViewMatrix, true);
Matrix4f relMat = new Matrix4f();
Matrix4f relFinal = new Matrix4f();
Matrix4f entityTransform = new Matrix4f();

Matrix4f result = new Matrix4f();
Vector3f currentPos = new Vector3f();

int index = 0;
for (EntityRef entity : entityManager.getEntitiesWith(SkeletalMeshComponent.class, LocationComponent.class)) {
SkeletalMeshComponent skeletalMesh = entity.getComponent(SkeletalMeshComponent.class);
LocationComponent locationComponent = entity.getComponent(LocationComponent.class);
if (skeletalMesh.boneEntities == null) {
continue;
}

Vector3f location = locationComponent.getWorldPosition(new Vector3f());
Quaternionf rotation = locationComponent.getWorldRotation(new Quaternionf());
entityTransform.translationRotateScale(location, rotation, 1.0f); // transformation of the entity

// position is referenced around (0,0,0) (worldposition - cameraposition)
Vector3f worldPositionCameraSpace = cameraPosition.negate(new Vector3f());

// same heightOffset is applied to worldPositionCameraSpace from #renderOpaque()
// TODO: resolve repeated logic for transformation applied to bones
worldPositionCameraSpace.y += skeletalMesh.heightOffset;

Matrix4f matrixCameraSpace = new Matrix4f().translationRotateScale(worldPositionCameraSpace, new Quaternionf(), 1.0f);
Matrix4f modelViewMatrix = new Matrix4f(worldRenderer.getActiveCamera().getViewMatrix()).mul(matrixCameraSpace);
material.setMatrix4("projectionMatrix", worldRenderer.getActiveCamera().getProjectionMatrix());
material.setMatrix4("modelViewMatrix", modelViewMatrix, true);

for (Bone bone : skeletalMesh.mesh.getBones()) {
Bone parentBone = bone.getParent();
EntityRef boneEntity = skeletalMesh.boneEntities.get(bone.getName());
if (parentBone == null) {
continue;
}

// TODO: the position of the bone is de-coupled from the actual translation/scale
EntityRef boneParentEntity = skeletalMesh.boneEntities.get(parentBone.getName());

LocationComponent locCompA = boneEntity.getComponent(LocationComponent.class);
LocationComponent locCompB = boneParentEntity.getComponent(LocationComponent.class);

Vector3f worldPosA = locCompA.getWorldPosition(new Vector3f());
Vector3f worldPosB = locCompB.getWorldPosition(new Vector3f());
// need to calculate the relative transformation from the entity to the start of the bone
locCompA.getRelativeTransform(relMat.identity(), entity);
// entityTransform * (scale, translation) * relativeMat * [x,y,z,1]
result.set(entityTransform)
.mul(relFinal.identity()
.scale(skeletalMesh.scale)
.translate(skeletalMesh.translate)
.mul(relMat))
.transformPosition(currentPos.zero()); // get the position of the start of the bone
meshData.position.put(currentPos); // the start of the bone

// need to calculate the relative transformation from the entity to the connecting bone
locCompB.getRelativeTransform(relMat.identity(), entity);
// entityTransform * (scale, translation) * relativeMat * [x,y,z,1]
result.set(entityTransform)
.mul(relFinal
.identity()
.scale(skeletalMesh.scale)
.translate(skeletalMesh.translate)
.mul(relMat))
.transformPosition(currentPos.zero()); // get the position to the connecting bone
meshData.position.put(currentPos); // the end of the bone

meshData.color0.put(Color.white);
meshData.color0.put(Color.white);

meshData.position.put(worldPosA);
meshData.position.put(worldPosB);

meshData.indices.putAll(new int[]{
index, index + 1
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import org.terasology.engine.persistence.StorageManager;
import org.terasology.engine.registry.In;
import org.terasology.engine.rendering.nui.CoreScreenLayer;
import org.terasology.engine.utilities.OperatingSystemMemory;
import org.terasology.engine.world.WorldProvider;
import org.terasology.engine.world.chunks.Chunks;
import org.terasology.nui.databinding.ReadOnlyBinding;
Expand All @@ -31,7 +32,7 @@
*/
public class DebugOverlay extends CoreScreenLayer {

public static final double MB_SIZE = 1048576.0;
public static final float MB_SIZE = 1048576.0f;

@In
private Config config;
Expand Down Expand Up @@ -72,13 +73,40 @@ public Boolean get() {
});

UILabel debugLine1 = find("debugLine1", UILabel.class);

// This limit doesn't change after start-up.
final long dataLimit = OperatingSystemMemory.isAvailable()
? OperatingSystemMemory.dataAndStackSizeLimit() : -1;

if (debugLine1 != null) {
debugLine1.bindText(new ReadOnlyBinding<String>() {
debugLine1.bindText(new ReadOnlyBinding<>() {
@Override
public String get() {
double memoryUsage = ((double) Runtime.getRuntime().totalMemory() - (double) Runtime.getRuntime().freeMemory()) / MB_SIZE;
return String.format("FPS: %.2f, Memory Usage: %.2f MB, Total Memory: %.2f MB, Max Memory: %.2f MB",
time.getFps(), memoryUsage, Runtime.getRuntime().totalMemory() / MB_SIZE, Runtime.getRuntime().maxMemory() / MB_SIZE);
Runtime runtime = Runtime.getRuntime();
long totalHeapSize = runtime.totalMemory();
float usedHeapMemory = ((float) totalHeapSize - (float) runtime.freeMemory()) / MB_SIZE;
String s = String.format(
"FPS: %.1f, Heap Usage: %.1f MB, Total Heap: %.1f MB, Max Heap: %.1f MB",
time.getFps(),
usedHeapMemory,
totalHeapSize / MB_SIZE,
runtime.maxMemory() / MB_SIZE
);
if (OperatingSystemMemory.isAvailable()) {
// Check data size, because that's the one comparable to Terasology#setMemoryLimit
long dataSize = OperatingSystemMemory.dataAndStackSize();
// How much bigger is that than the number reported by the Java runtime?
long nonJavaHeapDataSize = dataSize - totalHeapSize;
String limitString = (dataLimit > 0)
? String.format(" / %.1f MB (%02d%%)", dataLimit / MB_SIZE, 100 * dataSize / dataLimit)
: "";
return String.format(
"%s, Data: %.1f MB%s, Extra: %.1f MB",
s, dataSize / MB_SIZE, limitString, nonJavaHeapDataSize / MB_SIZE
);
} else {
return s;
}
}
});
}
Expand Down Expand Up @@ -158,7 +186,7 @@ public String get() {
debugInfo.bindText(new ReadOnlyBinding<String>() {
@Override
public String get() {
return String.format("[H] : Debug Documentation");
return "[H] : Debug Documentation";
}
});
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,9 @@ public MetricsMode(String name) {
*/
public abstract boolean isAvailable();

public abstract boolean isPerformanceManagerMode();
public boolean isPerformanceManagerMode() {
return false;
}

/**
* A (human readable) name for the metrics mode.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,9 +50,4 @@ public String getMetrics() {
public boolean isAvailable() {
return networkSystem.getMode() != NetworkMode.NONE;
}

@Override
public boolean isPerformanceManagerMode() {
return false;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,4 @@ public String getMetrics() {
public boolean isAvailable() {
return true;
}

@Override
public boolean isPerformanceManagerMode() {
return false;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,4 @@ public String getMetrics() {
public boolean isAvailable() {
return true;
}

@Override
public boolean isPerformanceManagerMode() {
return false;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,4 @@ public String getMetrics() {
public boolean isAvailable() {
return CoreRegistry.get(WorldRenderer.class) != null;
}

@Override
public boolean isPerformanceManagerMode() {
return false;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -140,15 +140,11 @@ public void doRender(List<Vector3f> verts, List<Vector3f> normals) {
}

public void render() {
// preRender();
doRender(data.getBindPoseVertexPositions(), data.getBindPoseVertexNormals());
// postRender();
}

public void render(List<Matrix4f> boneTransforms) {
// preRender();
doRender(data.getVertexPositions(boneTransforms), data.getVertexNormals(boneTransforms));
// postRender();
}

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
// Copyright 2021 The Terasology Foundation
// SPDX-License-Identifier: Apache-2.0

package org.terasology.engine.utilities;

import com.sun.jna.platform.unix.LibC;

import java.io.IOException;
import java.io.UncheckedIOException;
import java.nio.file.Files;
import java.nio.file.Path;

/**
* Monitor process memory usage.
* <p>
* This checks process's total memory usage as seen by the operating system.
* This includes memory not managed by the JVM.
*/
public final class OperatingSystemMemory {
public static final int PAGE_SIZE = 1 << 12; // 4 kB on x86 platforms

private static final Path PROC_STATM = Path.of("/proc/self/statm");

private OperatingSystemMemory() { }

public static boolean isAvailable() {
return OS.IS_LINUX;
}

public static long residentSetSize() {
try {
return STATM.RESIDENT.inBytes(Files.readString(PROC_STATM));
} catch (IOException e) {
throw new UncheckedIOException(e);
}
}

public static long dataAndStackSize() {
try {
return STATM.DATA.inBytes(Files.readString(PROC_STATM));
} catch (IOException e) {
throw new UncheckedIOException(e);
}
}

public static long dataAndStackSizeLimit() {
final LibC.Rlimit dataLimit = new LibC.Rlimit();
LibC.INSTANCE.getrlimit(LibC.RLIMIT_DATA, dataLimit);
return dataLimit.rlim_cur;
}

/**
* The fields of /proc/[pid]/statm
* <p>
* Note from proc(5):
* <blockquote><p>
* Some of these values are inaccurate because of a kernel-internal scalability optimization.
* If accurate values are required, use /proc/[pid]/smaps or /proc/[pid]/smaps_rollup instead,
* which are much slower but provide accurate, detailed information.
* </p></blockquote>
*/
enum STATM {
/** total program size */
SIZE(0),
/** resident set size */
RESIDENT(1),
/** number of resident shared pages */
SHARED(2),
/** text (code) */
TEXT(3),
/** unused since Linux 2.6 */
LIB(4),
/** data + stack */
DATA(5),
/** unused since Linux 2.6 */
DT(6);

private final short index;

STATM(int i) {
index = (short) i;
}

public long rawValue(String line) {
return Long.parseLong(line.split(" ")[index]);
}

public long inBytes(String line) {
return rawValue(line) * PAGE_SIZE;
}
}
}

0 comments on commit 036f139

Please sign in to comment.