Skip to content

Commit

Permalink
Regularly scheduled maintenance
Browse files Browse the repository at this point in the history
- Remove ModelHolder and ModelCache
- Remove lib/util.FlwUtil
- Remove lib/util.Pair and replace usages with com.mojang.datafixers.util.Pair
- Remove lib/util.Unit and replace usages with net.minecraft.util.Unit
- Make ResourceReloadHolder and ResourceReloadCache final and move to util
- Clean up code in backend/glsl
- Move LightSmoothnessArgument to impl
  • Loading branch information
PepperCode1 committed Sep 20, 2024
1 parent 62a0954 commit 5b6463b
Show file tree
Hide file tree
Showing 66 changed files with 251 additions and 364 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import dev.engine_room.flywheel.backend.compile.LightSmoothness;

public interface BackendConfig {
BackendConfig INSTANCE = FlwBackendXplat.INSTANCE.getConfig();
BackendConfig INSTANCE = FlwBackend.config();

/**
* How smooth/accurate our flw_light impl is.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,17 +1,25 @@
package dev.engine_room.flywheel.backend;

import org.jetbrains.annotations.UnknownNullability;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import dev.engine_room.flywheel.api.Flywheel;

public final class FlwBackend {
public static final Logger LOGGER = LoggerFactory.getLogger(Flywheel.ID + "/backend");
@UnknownNullability
private static BackendConfig config;

private FlwBackend() {
}

public static void init() {
public static BackendConfig config() {
return config;
}

public static void init(BackendConfig config) {
FlwBackend.config = config;
Backends.init();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,4 @@ public interface FlwBackendXplat {
FlwBackendXplat INSTANCE = DependencyInjection.load(FlwBackendXplat.class, "dev.engine_room.flywheel.backend.FlwBackendXplatImpl");

int getLightEmission(BlockState state, BlockGetter level, BlockPos pos);

BackendConfig getConfig();
}
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,9 @@ protected void generateUnpacking(GlslBuilder builder) {

fnBody.ret(GlslExpr.call(STRUCT_NAME, unpackArgs));

builder._addRaw("uniform usamplerBuffer _flw_instances;");
builder.uniform()
.type("usamplerBuffer")
.name("_flw_instances");
builder.blankLine();
builder.function()
.signature(FnSignature.create()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public String source() {
var builder = new GlslBuilder();

var instance = builder.struct();
instance.setName(STRUCT_NAME);
instance.name(STRUCT_NAME);
for (var element : layout.elements()) {
instance.addField(LayoutInterpreter.typeName(element.type()), element.name());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ protected void generateUnpacking(GlslBuilder builder) {

fnBody.ret(GlslExpr.call(STRUCT_NAME, unpackArgs));

builder._addRaw("layout(std430, binding = " + BufferBindings.INSTANCE + ") restrict readonly buffer InstanceBuffer {\n"
builder._raw("layout(std430, binding = " + BufferBindings.INSTANCE + ") restrict readonly buffer InstanceBuffer {\n"
+ " uint _flw_instances[];\n"
+ "};");
builder.blankLine();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentLinkedQueue;

import com.mojang.datafixers.util.Pair;

import dev.engine_room.flywheel.api.backend.Engine;
import dev.engine_room.flywheel.api.instance.Instance;
import dev.engine_room.flywheel.api.instance.InstanceType;
Expand All @@ -16,7 +18,6 @@
import dev.engine_room.flywheel.backend.FlwBackend;
import dev.engine_room.flywheel.backend.engine.embed.Environment;
import dev.engine_room.flywheel.backend.engine.embed.EnvironmentStorage;
import dev.engine_room.flywheel.lib.util.Pair;
import it.unimi.dsi.fastutil.ints.Int2ObjectArrayMap;
import it.unimi.dsi.fastutil.ints.Int2ObjectMap;
import net.minecraft.client.resources.model.ModelBakery;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -219,8 +219,8 @@ public void renderCrumbling(List<Engine.CrumblingBlock> crumblingBlocks) {
TextureBinder.bind(ModelBakery.BREAKING_LOCATIONS.get(progressEntry.getIntKey()));

for (var instanceHandlePair : progressEntry.getValue()) {
IndirectInstancer<?> instancer = instanceHandlePair.first();
int instanceIndex = instanceHandlePair.second().index;
IndirectInstancer<?> instancer = instanceHandlePair.getFirst();
int instanceIndex = instanceHandlePair.getSecond().index;

for (IndirectDraw draw : instancer.draws()) {
// Transform the material to be suited for crumbling.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -180,8 +180,8 @@ public void renderCrumbling(List<Engine.CrumblingBlock> crumblingBlocks) {
TextureBinder.bind(ModelBakery.BREAKING_LOCATIONS.get(progressEntry.getIntKey()));

for (var instanceHandlePair : progressEntry.getValue()) {
InstancedInstancer<?> instancer = instanceHandlePair.first();
var index = instanceHandlePair.second().index;
InstancedInstancer<?> instancer = instanceHandlePair.getFirst();
var index = instanceHandlePair.getSecond().index;

program.setInt("_flw_baseInstance", index);

Expand Down
Original file line number Diff line number Diff line change
@@ -1,19 +1,20 @@
package dev.engine_room.flywheel.backend.gl.array;

import java.util.Arrays;
import java.util.BitSet;
import java.util.List;

import org.lwjgl.opengl.GL45C;
import org.lwjgl.system.Checks;

import dev.engine_room.flywheel.backend.gl.GlCompat;
import dev.engine_room.flywheel.lib.util.FlwUtil;
import net.minecraft.Util;

public class GlVertexArrayDSA extends GlVertexArray {
public static final boolean SUPPORTED = isSupported();
private final BitSet attributeEnabled = new BitSet(MAX_ATTRIBS);
private final VertexAttribute[] attributes = new VertexAttribute[MAX_ATTRIBS];
private final int[] attributeBindings = FlwUtil.initArray(MAX_ATTRIBS, -1);
private final int[] attributeBindings = Util.make(new int[MAX_ATTRIBS], a -> Arrays.fill(a, -1));
private final int[] bindingBuffers = new int[MAX_ATTRIB_BINDINGS];
private final long[] bindingOffsets = new long[MAX_ATTRIB_BINDINGS];
private final int[] bindingStrides = new int[MAX_ATTRIB_BINDINGS];
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package dev.engine_room.flywheel.backend.gl.array;

import java.util.Arrays;
import java.util.BitSet;
import java.util.List;

Expand All @@ -12,13 +13,13 @@

import dev.engine_room.flywheel.backend.gl.GlCompat;
import dev.engine_room.flywheel.backend.gl.buffer.GlBufferType;
import dev.engine_room.flywheel.lib.util.FlwUtil;
import net.minecraft.Util;

public abstract class GlVertexArrayGL3 extends GlVertexArray {
private final BitSet attributeDirty = new BitSet(MAX_ATTRIBS);
private final int[] attributeOffsets = new int[MAX_ATTRIBS];
private final VertexAttribute[] attributes = new VertexAttribute[MAX_ATTRIBS];
private final int[] attributeBindings = FlwUtil.initArray(MAX_ATTRIBS, -1);
private final int[] attributeBindings = Util.make(new int[MAX_ATTRIBS], a -> Arrays.fill(a, -1));
private final int[] bindingBuffers = new int[MAX_ATTRIB_BINDINGS];
private final long[] bindingOffsets = new long[MAX_ATTRIB_BINDINGS];
private final int[] bindingStrides = new int[MAX_ATTRIB_BINDINGS];
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package dev.engine_room.flywheel.backend.gl.array;

import java.util.Arrays;
import java.util.BitSet;
import java.util.List;

Expand All @@ -9,13 +10,13 @@
import dev.engine_room.flywheel.backend.gl.GlCompat;
import dev.engine_room.flywheel.backend.gl.GlStateTracker;
import dev.engine_room.flywheel.backend.gl.buffer.GlBufferType;
import dev.engine_room.flywheel.lib.util.FlwUtil;
import net.minecraft.Util;

public class GlVertexArraySeparateAttributes extends GlVertexArray {
public static final boolean SUPPORTED = isSupported();
private final BitSet attributeEnabled = new BitSet(MAX_ATTRIBS);
private final VertexAttribute[] attributes = new VertexAttribute[MAX_ATTRIBS];
private final int[] attributeBindings = FlwUtil.initArray(MAX_ATTRIBS, -1);
private final int[] attributeBindings = Util.make(new int[MAX_ATTRIBS], a -> Arrays.fill(a, -1));
private final int[] bindingBuffers = new int[MAX_ATTRIB_BINDINGS];
private final long[] bindingOffsets = new long[MAX_ATTRIB_BINDINGS];
private final int[] bindingStrides = new int[MAX_ATTRIB_BINDINGS];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,10 @@
import java.util.List;
import java.util.stream.Collectors;

import com.mojang.datafixers.util.Pair;

import dev.engine_room.flywheel.backend.glsl.error.ErrorBuilder;
import dev.engine_room.flywheel.backend.glsl.span.Span;
import dev.engine_room.flywheel.lib.util.Pair;
import net.minecraft.ResourceLocationException;
import net.minecraft.resources.ResourceLocation;

Expand Down Expand Up @@ -38,9 +39,9 @@ public ErrorBuilder generateMessage() {
.pointAtFile(location);

for (var innerError : innerErrors) {
var err = innerError.second()
var err = innerError.getSecond()
.generateMessage();
out.pointAt(innerError.first())
out.pointAt(innerError.getFirst())
.nested(err);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,21 +4,20 @@
import java.util.Collection;
import java.util.HashSet;
import java.util.List;
import java.util.Optional;
import java.util.Set;

import org.jetbrains.annotations.Nullable;

import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableMap;
import com.mojang.datafixers.util.Pair;

import dev.engine_room.flywheel.backend.glsl.parse.Import;
import dev.engine_room.flywheel.backend.glsl.parse.ShaderField;
import dev.engine_room.flywheel.backend.glsl.parse.ShaderFunction;
import dev.engine_room.flywheel.backend.glsl.parse.ShaderStruct;
import dev.engine_room.flywheel.backend.glsl.span.Span;
import dev.engine_room.flywheel.backend.glsl.span.StringSpan;
import dev.engine_room.flywheel.lib.util.Pair;
import dev.engine_room.flywheel.lib.util.ResourceUtil;
import net.minecraft.ResourceLocationException;
import net.minecraft.resources.ResourceLocation;
Expand Down Expand Up @@ -172,44 +171,48 @@ public Span getLineSpanMatching(int line, @Nullable String match) {
* @param name The name of the struct to find.
* @return null if no definition matches the name.
*/
public Optional<ShaderStruct> findStructByName(String name) {
@Nullable
public ShaderStruct findStruct(String name) {
ShaderStruct struct = structs.get(name);

if (struct != null) {
return Optional.of(struct);
return struct;
}

for (var include : included) {
var external = include.structs.get(name);
var external = include.findStruct(name);

if (external != null) {
return Optional.of(external);
return external;
}
}

return Optional.empty();
return null;
}

/**
* Search this file and recursively search all imports to find a function definition matching the given name.
*
* @param name The name of the function to find.
* @return Optional#empty() if no definition matches the name.
* @return null if no definition matches the name.
*/
public Optional<ShaderFunction> findFunction(String name) {
ShaderFunction local = functions.get(name);
@Nullable
public ShaderFunction findFunction(String name) {
ShaderFunction function = functions.get(name);

if (local != null) return Optional.of(local);
if (function != null) {
return function;
}

for (var include : included) {
var external = include.functions.get(name);
var external = include.findFunction(name);

if (external != null) {
return Optional.of(external);
return external;
}
}

return Optional.empty();
return null;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -128,10 +128,6 @@ public int length() {
return raw.length();
}

public int lineStartCol(int spanLine) {
return 0;
}

public int lineWidth(int spanLine) {
return lines.get(spanLine)
.length();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ public class ErrorBuilder {
private final List<ErrorLine> lines = new ArrayList<>();

private ErrorBuilder() {

}

public static ErrorBuilder create() {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package dev.engine_room.flywheel.backend.glsl.error.lines;

public interface ErrorLine {

default int neededMargin() {
return left().length();
}
Expand All @@ -17,6 +16,7 @@ default String build() {
default String left() {
return "";
}

default String right() {
return "";
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package dev.engine_room.flywheel.backend.glsl.error.lines;

public record FileLine(String fileName) implements ErrorLine {

@Override
public String left() {
return "-";
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package dev.engine_room.flywheel.backend.glsl.error.lines;

public record HeaderLine(String level, CharSequence message) implements ErrorLine {

@Override
public int neededMargin() {
return -1;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package dev.engine_room.flywheel.backend.glsl.error.lines;

public record SourceLine(String number, String line) implements ErrorLine {

public static SourceLine numbered(int number, String line) {
return new SourceLine(Integer.toString(number), line);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,6 @@ public SpanHighlightLine(int firstCol, int lastCol) {
line = generateUnderline(firstCol, lastCol);
}

@Override
public String left() {
return "";
}

@Override
public String right() {
return line;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package dev.engine_room.flywheel.backend.glsl.error.lines;

public record TextLine(String msg) implements ErrorLine {

@Override
public String build() {
return msg;
Expand Down
Loading

0 comments on commit 5b6463b

Please sign in to comment.