diff options
author | makamys <makamys@outlook.com> | 2022-06-22 01:56:39 +0200 |
---|---|---|
committer | makamys <makamys@outlook.com> | 2022-06-22 01:56:39 +0200 |
commit | 7daa2bff67e12d1c752a837efb4ec1ae17265545 (patch) | |
tree | 1f7185b4d6c9ad16b1bee00903f69e490752a152 /src/main/java | |
parent | 93cdfdb87467725594072627a24335864aa695d8 (diff) | |
download | Neodymium-7daa2bff67e12d1c752a837efb4ec1ae17265545.tar.gz Neodymium-7daa2bff67e12d1c752a837efb4ec1ae17265545.tar.bz2 Neodymium-7daa2bff67e12d1c752a837efb4ec1ae17265545.zip |
Use preprocessor macros in shader
Increases FPS from 300 to 340
Diffstat (limited to 'src/main/java')
-rw-r--r-- | src/main/java/makamys/neodymium/renderer/NeoRenderer.java | 17 | ||||
-rw-r--r-- | src/main/java/makamys/neodymium/util/Preprocessor.java | 49 |
2 files changed, 63 insertions, 3 deletions
diff --git a/src/main/java/makamys/neodymium/renderer/NeoRenderer.java b/src/main/java/makamys/neodymium/renderer/NeoRenderer.java index 172ddcd..d307c28 100644 --- a/src/main/java/makamys/neodymium/renderer/NeoRenderer.java +++ b/src/main/java/makamys/neodymium/renderer/NeoRenderer.java @@ -14,10 +14,12 @@ import java.util.Arrays; import java.util.Collections; import java.util.Comparator; import java.util.HashMap; +import java.util.HashSet; import java.util.Iterator; import java.util.List; import java.util.Map; import java.util.Queue; +import java.util.Set; import java.util.concurrent.ConcurrentLinkedQueue; import org.lwjgl.BufferUtils; @@ -30,6 +32,7 @@ import makamys.neodymium.Neodymium; import makamys.neodymium.ducks.IWorldRenderer; import makamys.neodymium.renderer.Mesh.GPUStatus; import makamys.neodymium.util.GuiHelper; +import makamys.neodymium.util.Preprocessor; import makamys.neodymium.util.Util; import net.minecraft.block.Block; import net.minecraft.block.material.Material; @@ -464,12 +467,20 @@ public class NeoRenderer { } public void reloadShader() { - boolean errors = false; + Set<String> defines = new HashSet<>(); + if(Config.renderFog) { + defines.add("RENDER_FOG"); + } + if(Config.simplifyChunkMeshes) { + defines.add("SIMPLIFY_MESHES"); + } + boolean errors = false; + int vertexShader; vertexShader = glCreateShader(GL_VERTEX_SHADER); - glShaderSource(vertexShader, Util.readFile("shaders/chunk.vert")); + glShaderSource(vertexShader, Preprocessor.preprocess(Util.readFile("shaders/chunk.vert"), defines)); glCompileShader(vertexShader); if(glGetShaderi(vertexShader, GL_COMPILE_STATUS) == 0) { @@ -480,7 +491,7 @@ public class NeoRenderer { int fragmentShader; fragmentShader = glCreateShader(GL_FRAGMENT_SHADER); - glShaderSource(fragmentShader, Util.readFile("shaders/chunk.frag")); + glShaderSource(fragmentShader, Preprocessor.preprocess(Util.readFile("shaders/chunk.frag"), defines)); glCompileShader(fragmentShader); if(glGetShaderi(fragmentShader, GL_COMPILE_STATUS) == 0) { diff --git a/src/main/java/makamys/neodymium/util/Preprocessor.java b/src/main/java/makamys/neodymium/util/Preprocessor.java new file mode 100644 index 0000000..8ee9229 --- /dev/null +++ b/src/main/java/makamys/neodymium/util/Preprocessor.java @@ -0,0 +1,49 @@ +package makamys.neodymium.util; + +import java.util.Set; + +public class Preprocessor { + + public static String preprocess(String text, Set<String> defines) { + String[] lines = text.replaceAll("\\r\\n", "\\n").split("\\n"); + + IfElseBlockStatus ifElseBlockStatus = IfElseBlockStatus.NONE; + boolean ifElseConditionMet = false; + + for(int i = 0; i < lines.length; i++) { + String line = lines[i]; + + boolean commentLine = false; + + if(line.startsWith("#ifdef ")) { + ifElseBlockStatus = IfElseBlockStatus.IF; + ifElseConditionMet = defines.contains(line.split(" ")[1]); + commentLine = true; + } else if(line.startsWith("#else")) { + ifElseBlockStatus = IfElseBlockStatus.ELSE; + commentLine = true; + } else if(line.startsWith("#endif")) { + ifElseBlockStatus = IfElseBlockStatus.NONE; + commentLine = true; + } else { + if(ifElseBlockStatus == IfElseBlockStatus.IF && !ifElseConditionMet) { + commentLine = true; + } + if(ifElseBlockStatus == IfElseBlockStatus.ELSE && ifElseConditionMet) { + commentLine = true; + } + } + + if(commentLine) { + lines[i] = "//" + line; + } + } + + return String.join("\n", lines); + } + + public static enum IfElseBlockStatus { + NONE, IF, ELSE; + } + +} |