Bilinear interpolation for quad vertex data via geometry shader #12

Closed
embeddedt wants to merge 18 commits from geotesting into stonecutter
pull from: geotesting
4 changed files with 191 additions and 52 deletions
Showing only changes of commit f3b84442ad - Show all commits

No files matched your search

@@ -0,0 +1,38 @@
package org.embeddedt.embeddium.impl.common.util;
import java.util.Locale;
import java.util.concurrent.TimeUnit;
public class TimeUtil {
private static final TimeUnit[] UNITS = TimeUnit.values();
private static TimeUnit chooseUnit(long duration, TimeUnit unit) {
for (int i = UNITS.length - 1; i > unit.ordinal(); i--) {
if (UNITS[i].convert(duration, unit) > 0) {
return UNITS[i];
}
}
return unit;
}
private static String abbreviateTime(TimeUnit unit) {
return switch (unit) {
case NANOSECONDS -> "ns";
case MICROSECONDS -> "us";
case MILLISECONDS -> "ms";
case SECONDS -> "s";
case MINUTES -> "min";
case HOURS -> "h";
case DAYS -> "d";
};
}
public static String stringifyTime(long duration, TimeUnit unit) {
return stringifyTime(duration, unit, chooseUnit(duration, unit));
}
public static String stringifyTime(long duration, TimeUnit unit, TimeUnit displayUnit) {
double displayDuration = ((double)duration) / unit.convert(1, displayUnit);
return String.format(Locale.ROOT, "%.4g %s", displayDuration, abbreviateTime(displayUnit));
}
}
@@ -0,0 +1,74 @@
package org.embeddedt.embeddium.impl.gl.profiling;
import it.unimi.dsi.fastutil.objects.ObjectArrayFIFOQueue;
import lombok.Getter;
import org.lwjgl.opengl.GL32C;
import org.lwjgl.opengl.GL33C;
import java.io.Closeable;
public class TimerQueryManager implements Closeable {
private static final int INVALID_ID = -1;
/**
* The number of frames the timer query manager should wait before reading the data from the GPU.
*/
private static final int QUERY_FRAME_LAG_COUNT = 3;
private record InFlightQuery(int startTime, int endTime) {
long getTimeDelta() {
long startTime = GL33C.glGetQueryObjectui64(this.startTime, GL32C.GL_QUERY_RESULT);
long endTime = GL33C.glGetQueryObjectui64(this.endTime, GL32C.GL_QUERY_RESULT);
return endTime - startTime;
}
void delete() {
GL32C.glDeleteQueries(startTime);
GL32C.glDeleteQueries(endTime);
}
}
private final ObjectArrayFIFOQueue<InFlightQuery> inFlightQueries = new ObjectArrayFIFOQueue<>();
private int startQueryId = INVALID_ID;
@Getter
private long lastTime;
public void startProfiling() {
if (startQueryId != INVALID_ID) {
throw new IllegalStateException("Query already started but not ended");
}
int id = GL32C.glGenQueries();
GL33C.glQueryCounter(id, GL33C.GL_TIMESTAMP);
startQueryId = id;
}
public void finishProfiling() {
if (startQueryId == INVALID_ID) {
throw new IllegalStateException("Trying to end query that hasn't started yet");
}
int id = GL32C.glGenQueries();
GL33C.glQueryCounter(id, GL33C.GL_TIMESTAMP);
inFlightQueries.enqueue(new InFlightQuery(startQueryId, id));
startQueryId = -1;
}
public void updateTime() {
if (inFlightQueries.size() < QUERY_FRAME_LAG_COUNT) {
return;
}
var query = inFlightQueries.dequeue();
lastTime = query.getTimeDelta();
query.delete();
}
@Override
public void close() {
while (!inFlightQueries.isEmpty()) {
inFlightQueries.dequeue().delete();
}
if (startQueryId != -1) {
GL32C.glDeleteQueries(startQueryId);
startQueryId = -1;
}
}
}
@@ -1,11 +1,14 @@
package org.embeddedt.embeddium.impl.render.chunk;
import com.google.common.base.Suppliers;
import it.unimi.dsi.fastutil.longs.Long2ReferenceMap;
import it.unimi.dsi.fastutil.longs.Long2ReferenceOpenHashMap;
import it.unimi.dsi.fastutil.objects.*;
import lombok.Getter;
import org.embeddedt.embeddium.impl.common.util.TimeUtil;
import org.embeddedt.embeddium.impl.gl.device.CommandList;
import org.embeddedt.embeddium.impl.gl.device.RenderDevice;
import org.embeddedt.embeddium.impl.gl.profiling.TimerQueryManager;
import org.embeddedt.embeddium.impl.render.chunk.compile.ChunkBuildContext;
import org.embeddedt.embeddium.impl.render.chunk.compile.ChunkBuildOutput;
import org.embeddedt.embeddium.impl.render.chunk.compile.executor.ChunkBuilder;
@@ -40,6 +43,7 @@ import org.joml.Vector3ic;
import java.util.*;
import java.util.concurrent.ConcurrentLinkedDeque;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.locks.LockSupport;
import java.util.function.BiFunction;
import java.util.function.BooleanSupplier;
@@ -78,6 +82,8 @@ public abstract class RenderSectionManager {
protected final ReferenceSet<RenderSection> sectionsWithGlobalEntities = new ReferenceOpenHashSet<>();
private final Object2ObjectOpenHashMap<TerrainRenderPass, TimerQueryManager> renderPassDrawTimers = new Object2ObjectOpenHashMap<>();
public RenderSectionManager(RenderPassConfiguration<?> configuration, Supplier<ChunkBuildContext> contextSupplier, BiFunction<RenderDevice, RenderPassConfiguration<?>, ChunkRenderer> chunkRenderer, int renderDistance, CommandList commandList, int minSection, int maxSection, int requestedThreads) {
this.chunkRenderer = chunkRenderer.apply(RenderDevice.INSTANCE, configuration);
@@ -124,6 +130,8 @@ public abstract class RenderSectionManager {
while ((task = this.asyncSubmittedTasks.poll()) != null) {
task.run();
}
this.renderPassDrawTimers.values().forEach(TimerQueryManager::updateTime);
}
/**
@@ -316,8 +324,14 @@ public abstract class RenderSectionManager {
RenderDevice device = RenderDevice.INSTANCE;
CommandList commandList = device.createCommandList();
var timer = renderPassDrawTimers.computeIfAbsent(pass, $ -> new TimerQueryManager());
timer.startProfiling();
this.chunkRenderer.render(matrices, commandList, this.getCurrentRenderListManager().getRenderLists(), pass, new CameraTransform(x, y, z));
timer.finishProfiling();
commandList.flush();
}
@@ -557,6 +571,9 @@ public abstract class RenderSectionManager {
this.chunkRenderer.delete(commandList);
}
this.renderPassDrawTimers.values().forEach(TimerQueryManager::close);
this.renderPassDrawTimers.clear();
this.sectionsWithGlobalEntities.clear();
}
@@ -689,6 +706,16 @@ public abstract class RenderSectionManager {
return list;
}
private Object2LongMap<TerrainRenderPass> computeRenderPassTimingsMap() {
Object2LongOpenHashMap<TerrainRenderPass> map = new Object2LongOpenHashMap<>();
for (var entry : renderPassDrawTimers.entrySet()) {
map.put(entry.getKey(), entry.getValue().getLastTime());
}
return map;
}
protected final Supplier<Object2LongMap<TerrainRenderPass>> renderPassTimingsDebounced = Suppliers.memoizeWithExpiration(this::computeRenderPassTimingsMap, 1, TimeUnit.SECONDS);
public Collection<String> getDebugStrings() {
List<String> list = new ArrayList<>();
@@ -731,7 +758,28 @@ public abstract class RenderSectionManager {
this.getCurrentRenderListManager().getRebuildLists().hasAdditionalUpdates() ? "(++)" : ""
));
list.addAll(this.renderListManager.getRenderListDebugStrings());
var debugStats = renderListManager.getDebugStatistics();
var counts = debugStats.renderPassCounts().object2IntEntrySet().stream().sorted(Comparator.comparingInt(e -> -e.getIntValue())).iterator();
var timingMap = renderPassTimingsDebounced.get();
while (counts.hasNext()) {
var entry = counts.next();
var duration = timingMap.get(entry.getKey());
String time;
if (duration == 0) {
time = "?? ms";
} else {
time = TimeUtil.stringifyTime(duration, TimeUnit.NANOSECONDS);
}
list.add(entry.getKey().name() + " - " + entry.getIntValue() + " sections, " + time);
}
if (renderListManager.getRenderLists().getPasses().stream().anyMatch(TerrainRenderPass::isSorted)) {
list.add(debugStats.getSortingString());
}
return list;
}
@@ -1,6 +1,5 @@
package org.embeddedt.embeddium.impl.render.chunk.lists;
import com.google.common.collect.ImmutableList;
import it.unimi.dsi.fastutil.longs.Long2ReferenceMap;
import it.unimi.dsi.fastutil.longs.Long2ReferenceOpenHashMap;
import it.unimi.dsi.fastutil.objects.Object2IntOpenHashMap;
@@ -18,10 +17,7 @@ import org.embeddedt.embeddium.impl.util.PositionUtil;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import java.util.ArrayDeque;
import java.util.Comparator;
import java.util.List;
import java.util.Objects;
import java.util.*;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
@@ -58,7 +54,27 @@ public class RenderListManager {
@Nullable
private final SectionTicker sectionTicker;
private List<String> renderListDebugStrings;
public record RenderListDebugStatistics(Object2IntOpenHashMap<TerrainRenderPass> renderPassCounts, int[] sortingSectionCounts) {
public String getSortingString() {
StringBuilder sb = new StringBuilder();
sb.append("Sorting: ");
TranslucentQuadAnalyzer.Level[] values = TranslucentQuadAnalyzer.Level.VALUES;
for (int i = 0; i < values.length; i++) {
TranslucentQuadAnalyzer.Level level = values[i];
sb.append(level.name());
sb.append('=');
sb.append(sortingSectionCounts[level.ordinal()]);
if((i + 1) < values.length) {
sb.append(", ");
}
}
return sb.toString();
}
}
private RenderListDebugStatistics debugStatistics;
public RenderListManager(int minSectionY, int maxSectionY, boolean useAsyncGraphSearch, @Nullable SectionTicker sectionTicker) {
this.sectionTicker = sectionTicker;
@@ -116,7 +132,7 @@ public class RenderListManager {
this.currentOcclusionFuture = null;
this.lastUpdatedFrame = this.pendingLastUpdatedFrame;
this.renderListDebugStrings = null;
this.debugStatistics = null;
}
Runnable task;
@@ -245,17 +261,17 @@ public class RenderListManager {
}
}
public List<String> getRenderListDebugStrings() {
if (this.renderListDebugStrings == null) {
this.renderListDebugStrings = computeRenderListDebugStrings();
public RenderListDebugStatistics getDebugStatistics() {
if (this.debugStatistics == null) {
this.debugStatistics = computeDebugStatistics();
}
return this.renderListDebugStrings;
return this.debugStatistics;
}
private List<String> computeRenderListDebugStrings() {
private RenderListDebugStatistics computeDebugStatistics() {
Object2IntOpenHashMap<TerrainRenderPass> renderPassCounts = new Object2IntOpenHashMap<>();
var iterator = renderLists.iterator();
int[] sectionCounts = new int[TranslucentQuadAnalyzer.Level.VALUES.length];
@@ -307,43 +323,6 @@ public class RenderListManager {
}
}
ImmutableList.Builder<String> builder = ImmutableList.builder();
StringBuilder sb = new StringBuilder("Passes: ");
var iter = renderPassCounts.object2IntEntrySet().stream().sorted(Comparator.comparingInt(e -> -e.getIntValue())).iterator();
while (iter.hasNext()) {
var entry = iter.next();
sb.append(Character.toUpperCase(entry.getKey().name().charAt(0)));
sb.append('=');
sb.append(entry.getIntValue());
if (iter.hasNext()) {
sb.append(", ");
}
}
builder.add(sb.toString());
if (isSorting) {
sb = new StringBuilder();
sb.append("Sorting: ");
TranslucentQuadAnalyzer.Level[] values = TranslucentQuadAnalyzer.Level.VALUES;
for (int i = 0; i < values.length; i++) {
TranslucentQuadAnalyzer.Level level = values[i];
sb.append(level.name());
sb.append('=');
sb.append(sectionCounts[level.ordinal()]);
if((i + 1) < values.length) {
sb.append(", ");
}
}
builder.add(sb.toString());
}
return builder.build();
return new RenderListDebugStatistics(renderPassCounts, sectionCounts);
}
}