diff options
author | FalsePattern <me@falsepattern.com> | 2023-11-28 09:41:56 +0100 |
---|---|---|
committer | makamys <makamys@outlook.com> | 2023-12-01 18:34:00 +0100 |
commit | fbe408c777dcf6ea5d2d784b739287d6db2bf52c (patch) | |
tree | 77e4023ef11829d36adf1095a5f20bc8845b32f6 /src | |
parent | a4ec946f5931f8fe6e6271b22c553f99b9d5afb8 (diff) | |
download | Neodymium-fbe408c777dcf6ea5d2d784b739287d6db2bf52c.tar.gz Neodymium-fbe408c777dcf6ea5d2d784b739287d6db2bf52c.tar.bz2 Neodymium-fbe408c777dcf6ea5d2d784b739287d6db2bf52c.zip |
abstraction layer for attribute index/stride/offset
Diffstat (limited to 'src')
4 files changed, 85 insertions, 25 deletions
diff --git a/src/main/java/makamys/neodymium/renderer/NeoRenderer.java b/src/main/java/makamys/neodymium/renderer/NeoRenderer.java index 1aa83b6..61dac28 100644 --- a/src/main/java/makamys/neodymium/renderer/NeoRenderer.java +++ b/src/main/java/makamys/neodymium/renderer/NeoRenderer.java @@ -20,6 +20,8 @@ import java.util.List; import java.util.Map; import java.util.Map.Entry; import java.util.Set; + +import makamys.neodymium.renderer.attribs.AttributeSet; import org.lwjgl.BufferUtils; import org.lwjgl.input.Keyboard; import org.lwjgl.opengl.GL11; @@ -374,11 +376,23 @@ public class NeoRenderer { fogColorBuf.position(0); fogStartEnd.position(0); } + + private AttributeSet attributes; public boolean init() { // The average mesh is 60 KB. Let's be safe and assume 8 KB per mesh. // This means 1 MB of index data per 512 MB of VRAM. MAX_MESHES = Config.VRAMSize * 128; + + attributes = new AttributeSet(); + attributes.addAttribute("POS", 3, 4, GL_FLOAT); + if (Config.shortUV) { + attributes.addAttribute("TEXTURE", 2, 2, GL_UNSIGNED_SHORT); + } else { + attributes.addAttribute("TEXTURE", 2, 4, GL_FLOAT); + } + attributes.addAttribute("BRIGHTNESS", 2, 2, GL_SHORT); + attributes.addAttribute("COLOR", 4, 1, GL_UNSIGNED_BYTE); reloadShader(); @@ -389,18 +403,7 @@ public class NeoRenderer { glBindBuffer(GL_ARRAY_BUFFER, mem.VBO); - int stride = MeshQuad.getStride(); - - glVertexAttribPointer(0, 3, GL_FLOAT, false, stride, 0); - glVertexAttribPointer(1, 2, Config.shortUV ? GL_UNSIGNED_SHORT : GL_FLOAT, false, stride, 3 * 4); - int uvEnd = Config.shortUV ? 4 * 4 : 5 * 4; - glVertexAttribPointer(2, 2, GL_SHORT, false, stride, uvEnd); - glVertexAttribPointer(3, 4, GL_UNSIGNED_BYTE, false, stride, uvEnd + 1 * 4); - - glEnableVertexAttribArray(0); - glEnableVertexAttribArray(1); - glEnableVertexAttribArray(2); - glEnableVertexAttribArray(3); + attributes.enable(); for(int i = 0; i < 2; i++) { piFirst[i] = BufferUtils.createIntBuffer(MAX_MESHES); @@ -415,18 +418,20 @@ public class NeoRenderer { return true; } - public void reloadShader(int pass) { + public void reloadShader(int pass, AttributeSet attributeSet) { for(int hasFog = 0; hasFog <= 1; hasFog++) { - Set<String> defines = new HashSet<>(); + Map<String, String> defines = new HashMap<>(); if(hasFog == 1) { - defines.add("RENDER_FOG"); + defines.put("RENDER_FOG", ""); } if(Config.shortUV) { - defines.add("SHORT_UV"); + defines.put("SHORT_UV", ""); } if(pass == 0) { - defines.add("PASS_0"); + defines.put("PASS_0", ""); } + + attributeSet.addDefines(defines); boolean errors = false; @@ -472,8 +477,8 @@ public class NeoRenderer { } public void reloadShader() { - reloadShader(0); - reloadShader(1); + reloadShader(0, attributes); + reloadShader(1, attributes); } public void destroy() { diff --git a/src/main/java/makamys/neodymium/renderer/attribs/AttributeSet.java b/src/main/java/makamys/neodymium/renderer/attribs/AttributeSet.java new file mode 100644 index 0000000..ebf9c4c --- /dev/null +++ b/src/main/java/makamys/neodymium/renderer/attribs/AttributeSet.java @@ -0,0 +1,49 @@ +package makamys.neodymium.renderer.attribs; + +import org.lwjgl.opengl.GL20; + +import java.util.ArrayList; +import java.util.List; +import java.util.Map; + +public class AttributeSet { + private final List<VertexAttribute> attributes = new ArrayList<>(); + + public void addAttribute(String name, int size, int elementSize, int type) { + int index = attributes.size(); + attributes.add(new VertexAttribute(name, index, size, elementSize, type)); + } + + public void enable() { + int stride = attributes.stream().mapToInt(attrib -> attrib.size * attrib.elementSize).sum(); + int offset = 0; + for (int i = 0, size = attributes.size(); i < size; i++) { + VertexAttribute attribute = attributes.get(i); + GL20.glVertexAttribPointer(i, attribute.size, attribute.type, false, stride, offset); + offset += attribute.size * attribute.elementSize; + GL20.glEnableVertexAttribArray(i); + } + } + + public void addDefines(Map<String, String> defines) { + for (VertexAttribute attribute: attributes) { + defines.put("ATTRIB_" + attribute.name, Integer.toString(attribute.index)); + } + } + + public static class VertexAttribute { + public final String name; + public final int index; + public final int size; + public final int elementSize; + public final int type; + + private VertexAttribute(String name, int index, int size, int elementSize, int type) { + this.name = name; + this.index = index; + this.size = size; + this.elementSize = elementSize; + this.type = type; + } + } +} diff --git a/src/main/java/makamys/neodymium/util/Preprocessor.java b/src/main/java/makamys/neodymium/util/Preprocessor.java index d2fb0ba..cd5d8df 100644 --- a/src/main/java/makamys/neodymium/util/Preprocessor.java +++ b/src/main/java/makamys/neodymium/util/Preprocessor.java @@ -1,10 +1,11 @@ package makamys.neodymium.util; +import java.util.Map; import java.util.Set; public class Preprocessor { - public static String preprocess(String text, Set<String> defines) { + public static String preprocess(String text, Map<String, String> defines) { String[] lines = text.replaceAll("\\r\\n", "\n").split("\\n"); IfElseBlockStatus ifElseBlockStatus = IfElseBlockStatus.NONE; @@ -17,7 +18,7 @@ public class Preprocessor { if(line.startsWith("#ifdef ")) { ifElseBlockStatus = IfElseBlockStatus.IF; - ifElseConditionMet = defines.contains(line.split(" ")[1]); + ifElseConditionMet = defines.containsKey(line.split(" ")[1]); commentLine = true; } else if(line.startsWith("#else")) { ifElseBlockStatus = IfElseBlockStatus.ELSE; @@ -36,6 +37,11 @@ public class Preprocessor { if(commentLine) { lines[i] = "//" + line; + } else { + for (Map.Entry<String, String> define: defines.entrySet()) { + line = line.replace(define.getKey(), define.getValue()); + } + lines[i] = line; } } diff --git a/src/main/resources/shaders/chunk.vert b/src/main/resources/shaders/chunk.vert index 3e12f99..67ce114 100644 --- a/src/main/resources/shaders/chunk.vert +++ b/src/main/resources/shaders/chunk.vert @@ -1,8 +1,8 @@ #version 330 core -layout (location = 0) in vec3 aPos; -layout (location = 1) in vec2 aTexCoord; -layout (location = 2) in vec2 aBTexCoord; -layout (location = 3) in vec4 aColor; +layout (location = ATTRIB_POS) in vec3 aPos; +layout (location = ATTRIB_TEXTURE) in vec2 aTexCoord; +layout (location = ATTRIB_BRIGHTNESS) in vec2 aBTexCoord; +layout (location = ATTRIB_COLOR) in vec4 aColor; uniform mat4 modelView; uniform mat4 proj; |