-
-
Notifications
You must be signed in to change notification settings - Fork 52
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Guard 3 different flw_light impls via #define - Guard the inner face correction behind another #define - Add LightSmoothness enum to decide which flw_light impl to use - Make LightSmoothness configurable via a new BackendConfig - Add command to switch LightSmoothness on the fly - Note: currently requires a resource reload so we don't need to compile 4x as many shaders
- Loading branch information
Showing
16 changed files
with
354 additions
and
13 deletions.
There are no files selected for viewing
17 changes: 17 additions & 0 deletions
17
common/src/backend/java/dev/engine_room/flywheel/backend/BackendConfig.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
package dev.engine_room.flywheel.backend; | ||
|
||
import dev.engine_room.flywheel.backend.compile.LightSmoothness; | ||
|
||
public interface BackendConfig { | ||
BackendConfig INSTANCE = FlwBackendXplat.INSTANCE.getConfig(); | ||
|
||
/** | ||
* How smooth/accurate our flw_light impl is. | ||
* | ||
* <p>This makes more sense here as a backend-specific config because it's tightly coupled to | ||
* our backend's implementation. 3rd party backend may have different approaches and configurations. | ||
* | ||
* @return The current light smoothness setting. | ||
*/ | ||
LightSmoothness lightSmoothness(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
14 changes: 14 additions & 0 deletions
14
common/src/backend/java/dev/engine_room/flywheel/backend/LightSmoothnessArgument.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
package dev.engine_room.flywheel.backend; | ||
|
||
import dev.engine_room.flywheel.backend.compile.LightSmoothness; | ||
import net.minecraft.commands.arguments.StringRepresentableArgument; | ||
import net.minecraft.commands.synchronization.SingletonArgumentInfo; | ||
|
||
public class LightSmoothnessArgument extends StringRepresentableArgument<LightSmoothness> { | ||
public static final LightSmoothnessArgument INSTANCE = new LightSmoothnessArgument(); | ||
public static final SingletonArgumentInfo<LightSmoothnessArgument> INFO = SingletonArgumentInfo.contextFree(() -> INSTANCE); | ||
|
||
public LightSmoothnessArgument() { | ||
super(LightSmoothness.CODEC, LightSmoothness::values); | ||
} | ||
} |
38 changes: 38 additions & 0 deletions
38
common/src/backend/java/dev/engine_room/flywheel/backend/compile/LightSmoothness.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
package dev.engine_room.flywheel.backend.compile; | ||
|
||
import java.util.Locale; | ||
|
||
import com.mojang.serialization.Codec; | ||
|
||
import dev.engine_room.flywheel.backend.compile.core.Compilation; | ||
import net.minecraft.util.StringRepresentable; | ||
|
||
public enum LightSmoothness implements StringRepresentable { | ||
FLAT(0, false), | ||
TRI_LINEAR(1, false), | ||
SMOOTH(2, false), | ||
SMOOTH_INNER_FACE_CORRECTED(2, true), | ||
; | ||
|
||
public static final Codec<LightSmoothness> CODEC = StringRepresentable.fromEnum(LightSmoothness::values); | ||
|
||
private final int smoothnessDefine; | ||
private final boolean innerFaceCorrection; | ||
|
||
LightSmoothness(int smoothnessDefine, boolean innerFaceCorrection) { | ||
this.smoothnessDefine = smoothnessDefine; | ||
this.innerFaceCorrection = innerFaceCorrection; | ||
} | ||
|
||
public void onCompile(Compilation comp) { | ||
comp.define("_FLW_LIGHT_SMOOTHNESS", Integer.toString(smoothnessDefine)); | ||
if (innerFaceCorrection) { | ||
comp.define("_FLW_INNER_FACE_CORRECTION"); | ||
} | ||
} | ||
|
||
@Override | ||
public String getSerializedName() { | ||
return name().toLowerCase(Locale.ROOT); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
106 changes: 106 additions & 0 deletions
106
fabric/src/backend/java/dev/engine_room/flywheel/backend/FabricBackendConfig.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,106 @@ | ||
package dev.engine_room.flywheel.backend; | ||
|
||
import java.io.File; | ||
import java.io.FileReader; | ||
import java.io.FileWriter; | ||
import java.nio.file.Path; | ||
import java.util.Locale; | ||
|
||
import com.google.gson.Gson; | ||
import com.google.gson.GsonBuilder; | ||
import com.google.gson.JsonElement; | ||
import com.google.gson.JsonObject; | ||
import com.google.gson.JsonParser; | ||
import com.google.gson.JsonPrimitive; | ||
|
||
import dev.engine_room.flywheel.backend.compile.LightSmoothness; | ||
import net.fabricmc.loader.api.FabricLoader; | ||
|
||
public class FabricBackendConfig implements BackendConfig { | ||
|
||
public static final Path PATH = FabricLoader.getInstance() | ||
.getConfigDir() | ||
.resolve("flywheel-backend.json"); | ||
|
||
public static final FabricBackendConfig INSTANCE = new FabricBackendConfig(PATH.toFile()); | ||
|
||
private static final Gson GSON = new GsonBuilder().setPrettyPrinting() | ||
.create(); | ||
|
||
private final File file; | ||
|
||
public LightSmoothness lightSmoothness = LightSmoothness.SMOOTH; | ||
|
||
public FabricBackendConfig(File file) { | ||
this.file = file; | ||
} | ||
|
||
@Override | ||
public LightSmoothness lightSmoothness() { | ||
return lightSmoothness; | ||
} | ||
|
||
public void load() { | ||
if (file.exists()) { | ||
try (FileReader reader = new FileReader(file)) { | ||
fromJson(JsonParser.parseReader(reader)); | ||
} catch (Exception e) { | ||
FlwBackend.LOGGER.warn("Could not load config from file '{}'", file.getAbsolutePath(), e); | ||
} | ||
} | ||
// In case we found an error in the config file, immediately save to fix it. | ||
save(); | ||
} | ||
|
||
public void save() { | ||
try (FileWriter writer = new FileWriter(file)) { | ||
GSON.toJson(toJson(), writer); | ||
} catch (Exception e) { | ||
FlwBackend.LOGGER.warn("Could not save config to file '{}'", file.getAbsolutePath(), e); | ||
} | ||
} | ||
|
||
public void fromJson(JsonElement json) { | ||
if (!(json instanceof JsonObject object)) { | ||
FlwBackend.LOGGER.warn("Config JSON must be an object"); | ||
lightSmoothness = LightSmoothness.SMOOTH; | ||
return; | ||
} | ||
|
||
readLightSmoothness(object); | ||
} | ||
|
||
private void readLightSmoothness(JsonObject object) { | ||
var backendJson = object.get("lightSmoothness"); | ||
String msg = null; | ||
|
||
if (backendJson instanceof JsonPrimitive primitive && primitive.isString()) { | ||
var value = primitive.getAsString(); | ||
|
||
for (var item : LightSmoothness.values()) { | ||
if (item.name() | ||
.equalsIgnoreCase(value)) { | ||
lightSmoothness = item; | ||
return; | ||
} | ||
} | ||
|
||
msg = "Unknown 'lightSmoothness' value: " + value; | ||
} else if (backendJson != null) { | ||
msg = "'lightSmoothness' value must be a string"; | ||
} | ||
|
||
// Don't log an error if the field is missing. | ||
if (msg != null) { | ||
FlwBackend.LOGGER.warn(msg); | ||
} | ||
lightSmoothness = LightSmoothness.SMOOTH; | ||
} | ||
|
||
public JsonObject toJson() { | ||
JsonObject object = new JsonObject(); | ||
object.addProperty("lightSmoothness", lightSmoothness.toString() | ||
.toLowerCase(Locale.ROOT)); | ||
return object; | ||
} | ||
} |
Oops, something went wrong.