Skip to content

Commit

Permalink
Keeping our priorities straight
Browse files Browse the repository at this point in the history
- Implement backend priority system
- Give indirect priority 1000 and instancing 500
- Generate the sorted list of backends on demand in case one changes
  priority at runtime
  • Loading branch information
Jozufozu committed Aug 16, 2024
1 parent a5f49c6 commit 1a8ed8d
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 28 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,15 @@ public interface Backend {
Engine createEngine(LevelAccessor level);

/**
* Get a fallback backend in case this backend is not supported.
* The priority of this backend.
* <p>The backend with the highest priority upon first launch will be chosen as the default backend.
*
* <p>If the selected backend becomes unavailable for whatever reason, the next supported backend
* with a LOWER priority than the selected one will be chosen.
*
* @return The priority of this backend.
*/
Backend findFallback();
int priority();

/**
* Check if this backend is supported.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ public final class Backends {
*/
public static final Backend INSTANCING = SimpleBackend.builder()
.engineFactory(level -> new EngineImpl(level, new InstancedDrawManager(InstancingPrograms.get()), 256))
.priority(500)
.supported(() -> GlCompat.SUPPORTS_INSTANCING && InstancingPrograms.allLoaded() && !ShadersModHandler.isShaderPackInUse())
.register(Flywheel.rl("instancing"));

Expand All @@ -25,7 +26,7 @@ public final class Backends {
*/
public static final Backend INDIRECT = SimpleBackend.builder()
.engineFactory(level -> new EngineImpl(level, new IndirectDrawManager(IndirectPrograms.get()), 256))
.fallback(() -> Backends.INSTANCING)
.priority(1000)
.supported(() -> GlCompat.SUPPORTS_INDIRECT && IndirectPrograms.allLoaded() && !ShadersModHandler.isShaderPackInUse())
.register(Flywheel.rl("indirect"));

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,22 +3,20 @@
import java.util.Objects;
import java.util.function.BooleanSupplier;
import java.util.function.Function;
import java.util.function.Supplier;

import dev.engine_room.flywheel.api.backend.Backend;
import dev.engine_room.flywheel.api.backend.BackendManager;
import dev.engine_room.flywheel.api.backend.Engine;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.world.level.LevelAccessor;

public final class SimpleBackend implements Backend {
private final Function<LevelAccessor, Engine> engineFactory;
private final Supplier<Backend> fallback;
private final int priority;
private final BooleanSupplier isSupported;

public SimpleBackend(Function<LevelAccessor, Engine> engineFactory, Supplier<Backend> fallback, BooleanSupplier isSupported) {
public SimpleBackend(int priority, Function<LevelAccessor, Engine> engineFactory, BooleanSupplier isSupported) {
this.priority = priority;
this.engineFactory = engineFactory;
this.fallback = fallback;
this.isSupported = isSupported;
}

Expand All @@ -32,13 +30,8 @@ public Engine createEngine(LevelAccessor level) {
}

@Override
public Backend findFallback() {
if (isSupported()) {
return this;
} else {
return fallback.get()
.findFallback();
}
public int priority() {
return priority;
}

@Override
Expand All @@ -48,16 +41,16 @@ public boolean isSupported() {

public static final class Builder {
private Function<LevelAccessor, Engine> engineFactory;
private Supplier<Backend> fallback = BackendManager::offBackend;
private int priority = 0;
private BooleanSupplier isSupported;

public Builder engineFactory(Function<LevelAccessor, Engine> engineFactory) {
this.engineFactory = engineFactory;
return this;
}

public Builder fallback(Supplier<Backend> fallback) {
this.fallback = fallback;
public Builder priority(int priority) {
this.priority = priority;
return this;
}

Expand All @@ -68,10 +61,9 @@ public Builder supported(BooleanSupplier isSupported) {

public Backend register(ResourceLocation id) {
Objects.requireNonNull(engineFactory);
Objects.requireNonNull(fallback);
Objects.requireNonNull(isSupported);

return Backend.REGISTRY.registerAndGet(id, new SimpleBackend(engineFactory, fallback, isSupported));
return Backend.REGISTRY.registerAndGet(id, new SimpleBackend(priority, engineFactory, isSupported));
}
}
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
package dev.engine_room.flywheel.impl;

import java.util.ArrayList;

import dev.engine_room.flywheel.api.Flywheel;
import dev.engine_room.flywheel.api.backend.Backend;
import dev.engine_room.flywheel.backend.Backends;
import dev.engine_room.flywheel.impl.visualization.VisualizationManagerImpl;
import dev.engine_room.flywheel.lib.backend.SimpleBackend;
import net.minecraft.client.multiplayer.ClientLevel;
Expand Down Expand Up @@ -31,21 +32,48 @@ public static boolean isBackendOn() {
return backend != OFF_BACKEND;
}

// Don't store this statically because backends can theoretically change their priorities at runtime.
private static ArrayList<Backend> backendsByPriority() {
var backends = new ArrayList<>(Backend.REGISTRY.getAll());

// Sort with keys backwards so that the highest priority is first.
backends.sort((a, b) -> Integer.compare(b.priority(), a.priority()));
return backends;
}

private static Backend findDefaultBackend() {
// TODO: Automatically select the best default config based on the user's driver
// TODO: Figure out how this will work if custom backends are registered and without hardcoding the default backends
return Backends.INDIRECT;
var backendsByPriority = backendsByPriority();
if (backendsByPriority.isEmpty()) {
// This probably shouldn't happen, but fail gracefully.
FlwImpl.LOGGER.warn("No backends registered, defaulting to 'flywheel:off'");
return OFF_BACKEND;
}

return backendsByPriority.get(0);
}

private static void chooseBackend() {
var preferred = FlwConfig.INSTANCE.backend();
var actual = preferred.findFallback();
if (preferred.isSupported()) {
backend = preferred;
return;
}

var backendsByPriority = backendsByPriority();

var startIndex = backendsByPriority.indexOf(preferred) + 1;

if (preferred != actual) {
FlwImpl.LOGGER.warn("Flywheel backend fell back from '{}' to '{}'", Backend.REGISTRY.getIdOrThrow(preferred), Backend.REGISTRY.getIdOrThrow(actual));
// For safety in case we don't find anything
backend = OFF_BACKEND;
for (int i = startIndex; i < backendsByPriority.size(); i++) {
var candidate = backendsByPriority.get(i);
if (candidate.isSupported()) {
backend = candidate;
break;
}
}

backend = actual;
FlwImpl.LOGGER.warn("Flywheel backend fell back from '{}' to '{}'", Backend.REGISTRY.getIdOrThrow(preferred), Backend.REGISTRY.getIdOrThrow(backend));
}

public static String getBackendString() {
Expand Down

0 comments on commit 1a8ed8d

Please sign in to comment.