Pepper's catches

- GlCompat now a singleton, doesn't need to be re-created
 - Fix crash with F3 open and backend off
 - Clear program spec map before loading
 - Merge FlywheelClient and Flywheel classes
This commit is contained in:
Jozufozu 2022-02-01 12:46:47 -08:00
parent a1791399b6
commit b403ca3d2b
12 changed files with 86 additions and 90 deletions

View file

@ -4,15 +4,26 @@ import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.apache.maven.artifact.versioning.ArtifactVersion;
import com.jozufozu.flywheel.backend.Backend;
import com.jozufozu.flywheel.backend.OptifineHandler;
import com.jozufozu.flywheel.config.EngineArgument;
import com.jozufozu.flywheel.config.FlwCommands;
import com.jozufozu.flywheel.config.FlwConfig;
import com.jozufozu.flywheel.core.Contexts;
import com.jozufozu.flywheel.core.PartialModel;
import com.jozufozu.flywheel.core.StitchedSprite;
import com.jozufozu.flywheel.core.compile.ProgramCompiler;
import com.jozufozu.flywheel.event.ReloadRenderersEvent;
import com.jozufozu.flywheel.mixin.PausedPartialTickAccessor;
import com.jozufozu.flywheel.vanilla.VanillaInstances;
import net.minecraft.commands.synchronization.ArgumentTypes;
import net.minecraft.commands.synchronization.EmptyArgumentSerializer;
import net.minecraft.resources.ResourceLocation;
import net.minecraftforge.api.distmarker.Dist;
import net.minecraftforge.common.MinecraftForge;
import net.minecraftforge.eventbus.api.IEventBus;
import net.minecraftforge.fml.CrashReportCallables;
import net.minecraftforge.fml.DistExecutor;
import net.minecraftforge.fml.ModList;
import net.minecraftforge.fml.common.Mod;
@ -39,17 +50,43 @@ public class Flywheel {
.getModEventBus()
.addListener(this::setup);
MinecraftForge.EVENT_BUS.addListener(FlwCommands::onServerStarting);
FlwConfig.init();
DistExecutor.unsafeRunWhenOn(Dist.CLIENT, () -> FlywheelClient::clientInit);
DistExecutor.unsafeRunWhenOn(Dist.CLIENT, () -> Flywheel::clientInit);
}
public static ResourceLocation rl(String path) {
return new ResourceLocation(ID, path);
}
public static void clientInit() {
CrashReportCallables.registerCrashCallable("Flywheel Backend", Backend::getBackendDescriptor);
OptifineHandler.init();
Backend.init();
IEventBus modEventBus = FMLJavaModLoadingContext.get()
.getModEventBus();
modEventBus.addListener(Contexts::flwInit);
modEventBus.addListener(PartialModel::onModelRegistry);
modEventBus.addListener(PartialModel::onModelBake);
modEventBus.addListener(StitchedSprite::onTextureStitchPre);
modEventBus.addListener(StitchedSprite::onTextureStitchPost);
MinecraftForge.EVENT_BUS.addListener(FlwCommands::registerClientCommands);
MinecraftForge.EVENT_BUS.<ReloadRenderersEvent>addListener(ProgramCompiler::invalidateAll);
VanillaInstances.init();
// https://github.com/Jozufozu/Flywheel/issues/69
// Weird issue with accessor loading.
// Only thing I've seen that's close to a fix is to force the class to load before trying to use it.
// From the SpongePowered discord:
// https://discord.com/channels/142425412096491520/626802111455297538/675007581168599041
LOGGER.info("Successfully loaded {}", PausedPartialTickAccessor.class.getName());
}
private void setup(final FMLCommonSetupEvent event) {
ArgumentTypes.register(rl("engine").toString(), EngineArgument.class, new EmptyArgumentSerializer<>(EngineArgument::getInstance));
}

View file

@ -1,45 +0,0 @@
package com.jozufozu.flywheel;
import com.jozufozu.flywheel.backend.Backend;
import com.jozufozu.flywheel.backend.OptifineHandler;
import com.jozufozu.flywheel.core.Contexts;
import com.jozufozu.flywheel.core.PartialModel;
import com.jozufozu.flywheel.core.StitchedSprite;
import com.jozufozu.flywheel.core.compile.ProgramCompiler;
import com.jozufozu.flywheel.event.ReloadRenderersEvent;
import com.jozufozu.flywheel.mixin.PausedPartialTickAccessor;
import com.jozufozu.flywheel.vanilla.VanillaInstances;
import net.minecraftforge.common.MinecraftForge;
import net.minecraftforge.eventbus.api.IEventBus;
import net.minecraftforge.fml.CrashReportCallables;
import net.minecraftforge.fml.javafmlmod.FMLJavaModLoadingContext;
public class FlywheelClient {
public static void clientInit() {
CrashReportCallables.registerCrashCallable("Flywheel Backend", Backend::getBackendDescriptor);
OptifineHandler.init();
Backend.init();
IEventBus modEventBus = FMLJavaModLoadingContext.get()
.getModEventBus();
modEventBus.addListener(Contexts::flwInit);
modEventBus.addListener(PartialModel::onModelRegistry);
modEventBus.addListener(PartialModel::onModelBake);
modEventBus.addListener(StitchedSprite::onTextureStitchPre);
modEventBus.addListener(StitchedSprite::onTextureStitchPost);
MinecraftForge.EVENT_BUS.<ReloadRenderersEvent>addListener(ProgramCompiler::invalidateAll);
VanillaInstances.init();
// https://github.com/Jozufozu/Flywheel/issues/69
// Weird issue with accessor loading.
// Only thing I've seen that's close to a fix is to force the class to load before trying to use it.
// From the SpongePowered discord:
// https://discord.com/channels/142425412096491520/626802111455297538/675007581168599041
Flywheel.LOGGER.info("Successfully loaded {}", PausedPartialTickAccessor.class.getName());
}
}

View file

@ -21,8 +21,6 @@ public class Backend {
private static FlwEngine engine;
public static GlCompat compat;
private static final Loader loader = new Loader();
public static FlwEngine getEngine() {
@ -45,9 +43,7 @@ public class Backend {
public static void refresh() {
OptifineHandler.refresh();
compat = new GlCompat();
engine = chooseEngine(compat);
engine = chooseEngine();
}
public static boolean isOn() {
@ -79,7 +75,7 @@ public class Backend {
RenderWork.enqueue(Minecraft.getInstance().levelRenderer::allChanged);
}
private static FlwEngine chooseEngine(GlCompat compat) {
private static FlwEngine chooseEngine() {
FlwEngine preferredChoice = FlwConfig.get()
.getEngine();
@ -87,7 +83,7 @@ public class Backend {
boolean canUseEngine = switch (preferredChoice) {
case OFF -> true;
case BATCHING -> !usingShaders;
case INSTANCING -> !usingShaders && compat.instancedArraysSupported();
case INSTANCING -> !usingShaders && GlCompat.getInstance().instancedArraysSupported();
};
return canUseEngine ? preferredChoice : FlwEngine.OFF;

View file

@ -42,7 +42,7 @@ public class Loader implements ResourceManagerReloadListener {
public static final String PROGRAM_DIR = "flywheel/programs/";
private static final Gson GSON = new GsonBuilder().create();
private final Map<ResourceLocation, ProgramSpec> programSpecRegistry = new HashMap<>();
private final Map<ResourceLocation, ProgramSpec> programs = new HashMap<>();
private boolean firstLoad = true;
@ -59,7 +59,7 @@ public class Loader implements ResourceManagerReloadListener {
@Nullable
public ProgramSpec get(ResourceLocation name) {
return programSpecRegistry.get(name);
return programs.get(name);
}
@Override
@ -91,6 +91,8 @@ public class Loader implements ResourceManagerReloadListener {
}
private void loadProgramSpecs(ResourceManager manager) {
programs.clear();
Collection<ResourceLocation> programSpecs = manager.listResources(PROGRAM_DIR, s -> s.endsWith(".json"));
for (ResourceLocation location : programSpecs) {
@ -109,21 +111,15 @@ public class Loader implements ResourceManagerReloadListener {
spec.setName(specName);
register(spec);
if (programs.containsKey(specName)) {
throw new IllegalStateException("Program spec '" + specName + "' already registered.");
}
programs.put(specName, spec);
} catch (Exception e) {
Backend.LOGGER.error(e);
}
}
}
/**
* Register a shader program.
*/
private void register(ProgramSpec spec) {
ResourceLocation name = spec.name;
if (programSpecRegistry.containsKey(name)) {
throw new IllegalStateException("Program spec '" + name + "' already registered.");
}
programSpecRegistry.put(name, spec);
}
}

View file

@ -4,8 +4,8 @@ import java.nio.ByteBuffer;
import org.lwjgl.opengl.GL20;
import com.jozufozu.flywheel.backend.Backend;
import com.jozufozu.flywheel.backend.gl.GlObject;
import com.jozufozu.flywheel.backend.gl.versioned.GlCompat;
public abstract class GlBuffer extends GlObject {
@ -21,7 +21,8 @@ public abstract class GlBuffer extends GlObject {
* @return A buffer that will be persistent if the driver supports it.
*/
public static GlBuffer requestPersistent(GlBufferType type) {
if (Backend.compat.bufferStorageSupported()) {
if (GlCompat.getInstance()
.bufferStorageSupported()) {
return new PersistentGlBuffer(type);
} else {
return new MappedGlBuffer(type);

View file

@ -8,10 +8,10 @@ import java.nio.ByteBuffer;
import org.lwjgl.opengl.GL30;
import com.jozufozu.flywheel.backend.Backend;
import com.jozufozu.flywheel.backend.gl.GlFence;
import com.jozufozu.flywheel.backend.gl.error.GlError;
import com.jozufozu.flywheel.backend.gl.error.GlException;
import com.jozufozu.flywheel.backend.gl.versioned.GlCompat;
public class PersistentGlBuffer extends GlBuffer implements Mappable {
@ -46,7 +46,7 @@ public class PersistentGlBuffer extends GlBuffer implements Mappable {
fence.clear();
Backend.compat.bufferStorage.bufferStorage(type, size, flags);
GlCompat.getInstance().bufferStorage.bufferStorage(type, size, flags);
ByteBuffer byteBuffer = GL30.glMapBufferRange(type.glEnum, 0, size, flags);

View file

@ -20,11 +20,20 @@ import net.minecraft.Util;
*/
public class GlCompat {
private static GlCompat instance;
public static GlCompat getInstance() {
if (instance == null) {
instance = new GlCompat();
}
return instance;
}
public final InstancedArrays instancedArrays;
public final BufferStorage bufferStorage;
public final boolean amd;
public GlCompat() {
private GlCompat() {
GLCapabilities caps = GL.createCapabilities();
instancedArrays = getLatest(InstancedArrays.class, caps);
bufferStorage = getLatest(BufferStorage.class, caps);

View file

@ -1,8 +1,5 @@
package com.jozufozu.flywheel.backend.instancing;
import java.util.List;
import com.jozufozu.flywheel.Flywheel;
import com.jozufozu.flywheel.api.instance.DynamicInstance;
import com.jozufozu.flywheel.api.instance.TickableInstance;
import com.jozufozu.flywheel.backend.Backend;
@ -137,10 +134,4 @@ public class InstanceWorld {
.forEach(entityInstanceManager::add);
}
public void getDebugString(List<String> debug) {
debug.add("");
debug.add("Flywheel: " + Flywheel.VERSION);
debug.add("B: " + blockEntityInstanceManager.getObjectCount() + ", E: " + entityInstanceManager.getObjectCount());
engine.addDebugInfo(debug);
}
}

View file

@ -2,6 +2,7 @@ package com.jozufozu.flywheel.backend.instancing;
import java.util.List;
import com.jozufozu.flywheel.Flywheel;
import com.jozufozu.flywheel.backend.Backend;
import com.jozufozu.flywheel.event.BeginFrameEvent;
import com.jozufozu.flywheel.event.ReloadRenderersEvent;
@ -116,7 +117,16 @@ public class InstancedRenderDispatcher {
}
public static void getDebugString(List<String> debug) {
instanceWorlds.get(Minecraft.getInstance().level)
.getDebugString(debug);
debug.add("");
debug.add("Flywheel: " + Flywheel.VERSION);
if (Backend.isOn()) {
InstanceWorld instanceWorld = instanceWorlds.get(Minecraft.getInstance().level);
debug.add("B: " + instanceWorld.blockEntityInstanceManager.getObjectCount() + ", E: " + instanceWorld.entityInstanceManager.getObjectCount());
instanceWorld.engine.addDebugInfo(debug);
} else {
debug.add("Disabled");
}
}
}

View file

@ -6,11 +6,11 @@ import com.jozufozu.flywheel.Flywheel;
import com.jozufozu.flywheel.api.InstanceData;
import com.jozufozu.flywheel.api.struct.Instanced;
import com.jozufozu.flywheel.api.struct.StructWriter;
import com.jozufozu.flywheel.backend.Backend;
import com.jozufozu.flywheel.backend.gl.GlVertexArray;
import com.jozufozu.flywheel.backend.gl.buffer.GlBuffer;
import com.jozufozu.flywheel.backend.gl.buffer.GlBufferType;
import com.jozufozu.flywheel.backend.gl.buffer.MappedBuffer;
import com.jozufozu.flywheel.backend.gl.versioned.GlCompat;
import com.jozufozu.flywheel.backend.instancing.AbstractInstancer;
import com.jozufozu.flywheel.backend.model.BufferedModel;
import com.jozufozu.flywheel.backend.model.ModelAllocator;
@ -198,7 +198,7 @@ public class GPUInstancer<D extends InstanceData> extends AbstractInstancer<D> {
vao.bindAttributes(attributeBaseIndex, instanceFormat);
for (int i = 0; i < instanceFormat.getAttributeCount(); i++) {
Backend.compat.instancedArrays.vertexAttribDivisor(attributeBaseIndex + i, 1);
GlCompat.getInstance().instancedArrays.vertexAttribDivisor(attributeBaseIndex + i, 1);
}
}
}

View file

@ -7,8 +7,8 @@ import com.jozufozu.flywheel.api.InstanceData;
import com.jozufozu.flywheel.api.MaterialGroup;
import com.jozufozu.flywheel.api.struct.Instanced;
import com.jozufozu.flywheel.api.struct.StructType;
import com.jozufozu.flywheel.backend.Backend;
import com.jozufozu.flywheel.backend.RenderLayer;
import com.jozufozu.flywheel.backend.gl.versioned.GlCompat;
import com.jozufozu.flywheel.backend.model.FallbackAllocator;
import com.jozufozu.flywheel.backend.model.ModelAllocator;
import com.jozufozu.flywheel.backend.model.ModelPool;
@ -40,7 +40,8 @@ public class InstancedMaterialGroup<P extends WorldProgram> implements MaterialG
public InstancedMaterialGroup(InstancingEngine<P> owner, RenderType type) {
this.owner = owner;
this.type = type;
if (Backend.compat.onAMDWindows()) {
if (GlCompat.getInstance()
.onAMDWindows()) {
this.allocator = FallbackAllocator.INSTANCE;
} else {
this.allocator = new ModelPool(Formats.POS_TEX_NORMAL, 2048);

View file

@ -9,7 +9,7 @@ import net.minecraft.commands.Commands;
import net.minecraftforge.client.event.RegisterClientCommandsEvent;
public class FlwCommands {
public static void onServerStarting(RegisterClientCommandsEvent event) {
public static void registerClientCommands(RegisterClientCommandsEvent event) {
CommandDispatcher<CommandSourceStack> dispatcher = event.getDispatcher();
dispatcher.register(Commands.literal("flywheel")