2021-05-02 01:32:09 +02:00
|
|
|
package com.jozufozu.flywheel.backend;
|
2021-02-02 22:11:22 +01:00
|
|
|
|
2021-05-18 23:05:52 +02:00
|
|
|
import java.util.ArrayList;
|
2021-05-05 06:00:55 +02:00
|
|
|
import java.util.Collection;
|
2021-02-18 19:43:22 +01:00
|
|
|
import java.util.HashMap;
|
2021-05-18 23:05:52 +02:00
|
|
|
import java.util.List;
|
2021-02-18 19:43:22 +01:00
|
|
|
import java.util.Map;
|
|
|
|
|
|
|
|
import org.apache.logging.log4j.LogManager;
|
|
|
|
import org.apache.logging.log4j.Logger;
|
2021-03-24 23:48:15 +01:00
|
|
|
import org.lwjgl.opengl.GL;
|
2021-02-18 19:43:22 +01:00
|
|
|
import org.lwjgl.opengl.GLCapabilities;
|
|
|
|
|
2021-05-05 06:00:55 +02:00
|
|
|
import com.jozufozu.flywheel.backend.gl.shader.GlProgram;
|
2021-05-02 01:32:09 +02:00
|
|
|
import com.jozufozu.flywheel.backend.gl.versioned.GlCompat;
|
2021-05-16 01:41:56 +02:00
|
|
|
import com.jozufozu.flywheel.backend.instancing.InstanceData;
|
2021-06-05 02:55:05 +02:00
|
|
|
import com.jozufozu.flywheel.backend.instancing.InstancedRenderDispatcher;
|
2021-05-05 06:00:55 +02:00
|
|
|
import com.jozufozu.flywheel.backend.instancing.MaterialSpec;
|
2021-05-31 02:05:41 +02:00
|
|
|
import com.jozufozu.flywheel.core.WorldContext;
|
|
|
|
import com.jozufozu.flywheel.core.shader.spec.ProgramSpec;
|
2021-02-09 06:17:25 +01:00
|
|
|
import com.simibubi.create.foundation.config.AllConfigs;
|
2021-02-18 19:43:22 +01:00
|
|
|
|
2021-02-07 23:15:52 +01:00
|
|
|
import net.minecraft.client.Minecraft;
|
|
|
|
import net.minecraft.resources.IReloadableResourceManager;
|
|
|
|
import net.minecraft.resources.IResourceManager;
|
|
|
|
import net.minecraft.util.ResourceLocation;
|
2021-04-09 23:48:44 +02:00
|
|
|
import net.minecraft.util.math.vector.Matrix4f;
|
2021-06-05 00:56:46 +02:00
|
|
|
import net.minecraft.world.IWorld;
|
2021-04-08 19:22:11 +02:00
|
|
|
import net.minecraft.world.World;
|
2021-02-02 22:11:22 +01:00
|
|
|
|
|
|
|
public class Backend {
|
2021-03-24 23:48:15 +01:00
|
|
|
public static final Logger log = LogManager.getLogger(Backend.class);
|
2021-04-09 23:48:44 +02:00
|
|
|
|
2021-06-05 00:56:46 +02:00
|
|
|
public static final ShaderSources SHADER_SOURCES = new ShaderSources();
|
2021-04-09 23:48:44 +02:00
|
|
|
|
2021-03-24 23:48:15 +01:00
|
|
|
public static GLCapabilities capabilities;
|
2021-04-01 05:53:02 +02:00
|
|
|
public static GlCompat compat;
|
2021-03-24 23:48:15 +01:00
|
|
|
|
2021-05-05 08:56:50 +02:00
|
|
|
private static Matrix4f projectionMatrix = new Matrix4f();
|
2021-06-01 22:47:38 +02:00
|
|
|
private static boolean instancedArrays;
|
2021-03-24 23:48:15 +01:00
|
|
|
private static boolean enabled;
|
|
|
|
|
2021-05-05 08:56:50 +02:00
|
|
|
static final Map<ResourceLocation, MaterialSpec<?>> materialRegistry = new HashMap<>();
|
2021-05-18 23:05:52 +02:00
|
|
|
static final List<ShaderContext<?>> contexts = new ArrayList<>();
|
2021-05-05 06:00:55 +02:00
|
|
|
static final Map<ResourceLocation, ProgramSpec> programSpecRegistry = new HashMap<>();
|
2021-05-04 06:46:33 +02:00
|
|
|
|
|
|
|
static {
|
|
|
|
register(WorldContext.INSTANCE);
|
2021-06-05 02:55:05 +02:00
|
|
|
register(InstancedRenderDispatcher.CRUMBLING);
|
2021-05-04 06:46:33 +02:00
|
|
|
}
|
2021-03-24 23:48:15 +01:00
|
|
|
|
|
|
|
public Backend() {
|
|
|
|
throw new IllegalStateException();
|
|
|
|
}
|
|
|
|
|
2021-06-04 06:23:06 +02:00
|
|
|
/**
|
|
|
|
* Get a string describing the Flywheel backend. When there are eventually multiple backends
|
|
|
|
* (Meshlet, MDI, GL31 Draw Instanced are planned), this will name which one is in use.
|
|
|
|
*/
|
2021-06-01 22:47:38 +02:00
|
|
|
public static String getBackendDescriptor() {
|
|
|
|
if (canUseInstancing()) {
|
|
|
|
return "GL33 Instanced Arrays";
|
|
|
|
}
|
|
|
|
|
|
|
|
if (canUseVBOs()) {
|
|
|
|
return "VBOs";
|
|
|
|
}
|
|
|
|
|
|
|
|
return "Disabled";
|
|
|
|
}
|
|
|
|
|
2021-03-24 23:48:15 +01:00
|
|
|
/**
|
2021-05-04 06:46:33 +02:00
|
|
|
* Register a shader program.
|
2021-03-24 23:48:15 +01:00
|
|
|
*/
|
2021-05-04 06:46:33 +02:00
|
|
|
public static ProgramSpec register(ProgramSpec spec) {
|
2021-03-24 23:48:15 +01:00
|
|
|
ResourceLocation name = spec.name;
|
2021-05-05 06:00:55 +02:00
|
|
|
if (programSpecRegistry.containsKey(name)) {
|
2021-03-24 23:48:15 +01:00
|
|
|
throw new IllegalStateException("Program spec '" + name + "' already registered.");
|
|
|
|
}
|
2021-05-05 06:00:55 +02:00
|
|
|
programSpecRegistry.put(name, spec);
|
2021-03-24 23:48:15 +01:00
|
|
|
return spec;
|
|
|
|
}
|
|
|
|
|
2021-05-04 06:46:33 +02:00
|
|
|
/**
|
|
|
|
* Register a shader context.
|
|
|
|
*/
|
2021-05-05 06:00:55 +02:00
|
|
|
public static <P extends GlProgram> ShaderContext<P> register(ShaderContext<P> spec) {
|
2021-05-18 23:05:52 +02:00
|
|
|
contexts.add(spec);
|
2021-05-04 06:46:33 +02:00
|
|
|
return spec;
|
2021-03-24 23:48:15 +01:00
|
|
|
}
|
|
|
|
|
2021-05-05 06:00:55 +02:00
|
|
|
/**
|
|
|
|
* Register an instancing material.
|
|
|
|
*/
|
2021-05-16 01:41:56 +02:00
|
|
|
public static <D extends InstanceData> MaterialSpec<D> register(MaterialSpec<D> spec) {
|
2021-05-05 06:00:55 +02:00
|
|
|
ResourceLocation name = spec.name;
|
|
|
|
if (materialRegistry.containsKey(name)) {
|
|
|
|
throw new IllegalStateException("Material spec '" + name + "' already registered.");
|
|
|
|
}
|
|
|
|
materialRegistry.put(name, spec);
|
|
|
|
return spec;
|
|
|
|
}
|
|
|
|
|
2021-05-23 02:45:01 +02:00
|
|
|
public static ProgramSpec getSpec(ResourceLocation name) {
|
|
|
|
return programSpecRegistry.get(name);
|
|
|
|
}
|
|
|
|
|
2021-05-31 02:05:41 +02:00
|
|
|
/**
|
|
|
|
* Used to avoid calling Flywheel functions on (fake) worlds that don't specifically support it.
|
|
|
|
*/
|
2021-06-05 00:56:46 +02:00
|
|
|
public static boolean isFlywheelWorld(IWorld world) {
|
2021-05-31 02:05:41 +02:00
|
|
|
return (world instanceof IFlywheelWorld && ((IFlywheelWorld) world).supportsFlywheel()) || world == Minecraft.getInstance().world;
|
2021-03-24 23:48:15 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
public static boolean available() {
|
|
|
|
return canUseVBOs();
|
|
|
|
}
|
|
|
|
|
|
|
|
public static boolean canUseInstancing() {
|
2021-06-01 22:47:38 +02:00
|
|
|
return enabled && instancedArrays;
|
2021-03-24 23:48:15 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
public static boolean canUseVBOs() {
|
|
|
|
return enabled && gl20();
|
|
|
|
}
|
|
|
|
|
|
|
|
public static boolean gl33() {
|
|
|
|
return capabilities.OpenGL33;
|
|
|
|
}
|
|
|
|
|
|
|
|
public static boolean gl20() {
|
|
|
|
return capabilities.OpenGL20;
|
|
|
|
}
|
|
|
|
|
|
|
|
public static void init() {
|
|
|
|
// Can be null when running datagenerators due to the unfortunate time we call this
|
|
|
|
Minecraft mc = Minecraft.getInstance();
|
|
|
|
if (mc == null) return;
|
|
|
|
|
|
|
|
IResourceManager manager = mc.getResourceManager();
|
|
|
|
|
|
|
|
if (manager instanceof IReloadableResourceManager) {
|
2021-06-05 00:56:46 +02:00
|
|
|
((IReloadableResourceManager) manager).addReloadListener(SHADER_SOURCES);
|
2021-03-24 23:48:15 +01:00
|
|
|
}
|
2021-06-05 00:56:46 +02:00
|
|
|
|
|
|
|
OptifineHandler.init();
|
2021-03-24 23:48:15 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
public static void refresh() {
|
|
|
|
capabilities = GL.createCapabilities();
|
|
|
|
|
2021-04-01 05:53:02 +02:00
|
|
|
compat = new GlCompat(capabilities);
|
2021-03-24 23:48:15 +01:00
|
|
|
|
2021-06-01 22:47:38 +02:00
|
|
|
instancedArrays = compat.vertexArrayObjectsSupported() &&
|
2021-03-24 23:48:15 +01:00
|
|
|
compat.drawInstancedSupported() &&
|
|
|
|
compat.instancedArraysSupported();
|
|
|
|
|
|
|
|
enabled = AllConfigs.CLIENT.experimentalRendering.get() && !OptifineHandler.usingShaders();
|
|
|
|
}
|
2021-05-05 06:00:55 +02:00
|
|
|
|
|
|
|
public static void reloadWorldRenderers() {
|
|
|
|
RenderWork.enqueue(Minecraft.getInstance().worldRenderer::loadRenderers);
|
|
|
|
}
|
|
|
|
|
|
|
|
public static boolean canUseInstancing(World world) {
|
|
|
|
return canUseInstancing() && isFlywheelWorld(world);
|
|
|
|
}
|
|
|
|
|
|
|
|
public static Collection<MaterialSpec<?>> allMaterials() {
|
|
|
|
return materialRegistry.values();
|
|
|
|
}
|
|
|
|
|
|
|
|
public static Collection<ProgramSpec> allPrograms() {
|
|
|
|
return programSpecRegistry.values();
|
|
|
|
}
|
2021-05-05 08:56:50 +02:00
|
|
|
|
|
|
|
public static Matrix4f getProjectionMatrix() {
|
|
|
|
return projectionMatrix;
|
|
|
|
}
|
|
|
|
|
|
|
|
public static void setProjectionMatrix(Matrix4f projectionMatrix) {
|
|
|
|
Backend.projectionMatrix = projectionMatrix;
|
|
|
|
}
|
2021-02-02 22:11:22 +01:00
|
|
|
}
|