Vertex pulling #48
No files matched your search
+18
-6
@@ -35,9 +35,9 @@ public abstract class DefaultChunkRenderer extends ShaderChunkRenderer {
|
||||
|
||||
/**
|
||||
* The texture unit that pulled passes read vertex data from. Loaders bind their own textures to low units, so this
|
||||
* stays clear of them.
|
||||
* stays clear of them. Shader pack programs reserve it when they pull vertex data.
|
||||
*/
|
||||
private static final int VERTEX_DATA_TEXTURE_UNIT = 15;
|
||||
public static final int VERTEX_DATA_TEXTURE_UNIT = 15;
|
||||
|
||||
/**
|
||||
* Stands in for vertex attributes in pulled passes. Compatibility profiles leave a draw undefined when attribute
|
||||
@@ -102,9 +102,11 @@ public abstract class DefaultChunkRenderer extends ShaderChunkRenderer {
|
||||
|
||||
boolean pulled = renderPass.vertexFetchMode() == VertexFetchMode.PULLED;
|
||||
|
||||
int previousActiveTexture = 0;
|
||||
|
||||
if (pulled) {
|
||||
this.currentVertexFormat = PLACEHOLDER_ATTRIBUTE_FORMAT;
|
||||
LWJGL.glActiveTexture(GL13.GL_TEXTURE0 + VERTEX_DATA_TEXTURE_UNIT);
|
||||
previousActiveTexture = this.activateTextureUnit(GL13.GL_TEXTURE0 + VERTEX_DATA_TEXTURE_UNIT);
|
||||
} else {
|
||||
this.currentVertexFormat = ChunkVertexLayout.requireAttributes(renderPass.vertexType().getLayout());
|
||||
}
|
||||
@@ -162,9 +164,7 @@ public abstract class DefaultChunkRenderer extends ShaderChunkRenderer {
|
||||
}
|
||||
|
||||
if (pulled) {
|
||||
// The game caches the active unit, so it has to be put back.
|
||||
// TODO: don't assume unit 0 was active before the pass
|
||||
LWJGL.glActiveTexture(GL13.GL_TEXTURE0);
|
||||
this.activateTextureUnit(previousActiveTexture);
|
||||
}
|
||||
|
||||
this.currentVertexFormat = null;
|
||||
@@ -176,6 +176,18 @@ public abstract class DefaultChunkRenderer extends ShaderChunkRenderer {
|
||||
this.end(renderPass);
|
||||
}
|
||||
|
||||
/**
|
||||
* Makes the given texture unit active. Platforms whose renderer caches the active unit must update that cache.
|
||||
*
|
||||
* @param unit the unit, as {@code GL_TEXTURE0 + n}
|
||||
* @return the unit that was active before, in the same form
|
||||
*/
|
||||
protected int activateTextureUnit(int unit) {
|
||||
LWJGL.glActiveTexture(unit);
|
||||
// TODO: don't assume unit 0 was active before the pass
|
||||
return GL13.GL_TEXTURE0;
|
||||
}
|
||||
|
||||
private static void setModelMatrixUniforms(ChunkShaderInterface shader, RenderRegion region, CameraTransform camera) {
|
||||
float x = getCameraTranslation(region.getOriginX(), camera.intX, camera.fracX);
|
||||
float y = getCameraTranslation(region.getOriginY(), camera.intY, camera.fracY);
|
||||
|
||||
+2
-1
@@ -8,6 +8,7 @@ import org.embeddedt.embeddium.impl.gl.shader.uniform.GlUniformMatrix4f;
|
||||
import org.embeddedt.embeddium.impl.gl.tessellation.GlPrimitiveType;
|
||||
import org.embeddedt.embeddium.impl.render.chunk.compile.sorting.QuadPrimitiveType;
|
||||
import org.embeddedt.embeddium.impl.render.chunk.terrain.TerrainRenderPass;
|
||||
import org.embeddedt.embeddium.impl.render.chunk.vertex.format.VertexFetchGenerator;
|
||||
import org.embeddedt.embeddium.impl.render.chunk.vertex.format.VertexFetchMode;
|
||||
import org.joml.Matrix4fc;
|
||||
import org.taumc.celeritas.lwjgl.MemoryStack;
|
||||
@@ -49,7 +50,7 @@ public class DefaultChunkShaderInterface implements ChunkShaderInterface {
|
||||
this.uniformTextures.put(ChunkShaderTextureSlot.LIGHT, context.bindUniform("u_LightTex", GlUniformInt::new));
|
||||
}
|
||||
if (options.pass().vertexFetchMode() == VertexFetchMode.PULLED) {
|
||||
this.uniformTextures.put(ChunkShaderTextureSlot.VERTEX_DATA, context.bindUniform("u_VertexData", GlUniformInt::new));
|
||||
this.uniformTextures.put(ChunkShaderTextureSlot.VERTEX_DATA, context.bindUniform(VertexFetchGenerator.VERTEX_DATA_SAMPLER, GlUniformInt::new));
|
||||
}
|
||||
|
||||
this.components = options.components().stream().map(c -> c.create(context, environment)).toList();
|
||||
|
||||
+107
-48
@@ -7,24 +7,26 @@ import org.embeddedt.embeddium.impl.gl.attribute.GlVertexFormat;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
import java.util.TreeSet;
|
||||
|
||||
/**
|
||||
* Generates the GLSL that supplies a vertex type's inputs to {@code chunk_vertex.glsl}.
|
||||
* Generates the GLSL that declares every element of a {@link ChunkVertexLayout} as a vertex shader input.
|
||||
* <p>
|
||||
* With attribute fetch, the inputs are declared as {@code in} variables. When pulled, they are declared as globals of the
|
||||
* same names and types, and {@code _vert_fetch()} fills them from the {@code u_VertexData} texture buffer, converting each
|
||||
* component the way attribute fetch would for the same {@link GlVertexAttribute}.
|
||||
* With attribute fetch, the elements are declared as {@code in} variables. When pulled, they are declared as globals of the
|
||||
* same names and types, and {@code _vert_fetch()} fills them from the {@link #VERTEX_DATA_SAMPLER} texture buffer,
|
||||
* converting each component the way attribute fetch would for the same {@link GlVertexAttribute}.
|
||||
* <p>
|
||||
* Callers may declare an element with a different type than its own, as a shader may declare an attribute. Missing
|
||||
* components are taken from {@code (0, 0, 0, 1)}, extra ones are dropped, and components are cast if the scalar types
|
||||
* differ.
|
||||
*/
|
||||
public final class VertexFetchGenerator {
|
||||
public static final String IMPORT_NAME = "sodium:generated/vertex_fetch.glsl";
|
||||
|
||||
/**
|
||||
* The inputs read by {@code chunk_vertex.glsl}, which must change in step with it. Other attributes are left for
|
||||
* external shaders to declare themselves.
|
||||
* The {@code usamplerBuffer} that pulled vertex data is read from.
|
||||
*/
|
||||
public static final Set<String> CHUNK_VERTEX_INPUTS = Set.of("a_PosId", "a_Color", "a_TexCoord", "a_LightCoord", "a_RdhFactor");
|
||||
public static final String VERTEX_DATA_SAMPLER = "u_VertexData";
|
||||
|
||||
public enum Scope {
|
||||
HEADER,
|
||||
@@ -47,12 +49,74 @@ public final class VertexFetchGenerator {
|
||||
*/
|
||||
public record Component(Scope scope, int word, int shift, int bits, Conversion conversion) {}
|
||||
|
||||
public record Element(String name, String glslType, List<Component> components) {}
|
||||
public record Element(String name, GlslType type, List<Component> components) {}
|
||||
|
||||
public enum ScalarType {
|
||||
FLOAT("float", "", "0.0", "1.0"),
|
||||
INT("int", "i", "0", "1"),
|
||||
UINT("uint", "u", "0u", "1u");
|
||||
|
||||
private final String name;
|
||||
private final String vectorPrefix;
|
||||
private final String zero;
|
||||
private final String one;
|
||||
|
||||
ScalarType(String name, String vectorPrefix, String zero, String one) {
|
||||
this.name = name;
|
||||
this.vectorPrefix = vectorPrefix;
|
||||
this.zero = zero;
|
||||
this.one = one;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* A scalar or vector type, the only kinds a vertex input can have here.
|
||||
*/
|
||||
public record GlslType(ScalarType scalar, int count) {
|
||||
public GlslType {
|
||||
if (count < 1 || count > 4) {
|
||||
throw new IllegalArgumentException("Vector types have 1 to 4 components, not " + count);
|
||||
}
|
||||
}
|
||||
|
||||
public static GlslType parse(String name) {
|
||||
for (ScalarType scalar : ScalarType.values()) {
|
||||
if (name.equals(scalar.name)) {
|
||||
return new GlslType(scalar, 1);
|
||||
}
|
||||
|
||||
String prefix = scalar.vectorPrefix + "vec";
|
||||
|
||||
if (name.length() == prefix.length() + 1 && name.startsWith(prefix)) {
|
||||
int count = name.charAt(prefix.length()) - '0';
|
||||
|
||||
if (count >= 2 && count <= 4) {
|
||||
return new GlslType(scalar, count);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
throw new IllegalArgumentException("Unsupported vertex input type " + name);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return this.count == 1 ? this.scalar.name : this.scalar.vectorPrefix + "vec" + this.count;
|
||||
}
|
||||
}
|
||||
|
||||
private VertexFetchGenerator() {}
|
||||
|
||||
public static Map<String, String> sources(ChunkVertexLayout layout, int verticesPerPrimitive, VertexFetchMode mode, boolean legacyGlsl) {
|
||||
return Map.of(IMPORT_NAME, generate(layout, verticesPerPrimitive, mode, legacyGlsl));
|
||||
return sources(layout, verticesPerPrimitive, mode, legacyGlsl, Map.of());
|
||||
}
|
||||
|
||||
/**
|
||||
* @param declaredTypes GLSL types to declare elements with instead of their own, by element name
|
||||
*/
|
||||
public static Map<String, String> sources(ChunkVertexLayout layout, int verticesPerPrimitive, VertexFetchMode mode, boolean legacyGlsl,
|
||||
Map<String, GlslType> declaredTypes) {
|
||||
return Map.of(IMPORT_NAME, generate(layout, verticesPerPrimitive, mode, legacyGlsl, declaredTypes));
|
||||
}
|
||||
|
||||
public static List<Element> plan(ChunkVertexLayout layout) {
|
||||
@@ -69,10 +133,6 @@ public final class VertexFetchGenerator {
|
||||
|
||||
private static void addElements(List<Element> elements, GlVertexFormat format, Scope scope) {
|
||||
for (GlVertexAttribute attribute : format.getAttributes()) {
|
||||
if (!CHUNK_VERTEX_INPUTS.contains(attribute.getName())) {
|
||||
continue;
|
||||
}
|
||||
|
||||
Conversion conversion = conversionOf(attribute);
|
||||
int componentSize = attribute.getFormat().size();
|
||||
var components = new ArrayList<Component>(attribute.getCount());
|
||||
@@ -82,7 +142,7 @@ public final class VertexFetchGenerator {
|
||||
components.add(new Component(scope, offset / 4, (offset % 4) * 8, componentSize * 8, conversion));
|
||||
}
|
||||
|
||||
elements.add(new Element(attribute.getName(), glslType(conversion, attribute.getCount()), components));
|
||||
elements.add(new Element(attribute.getName(), new GlslType(scalarType(conversion), attribute.getCount()), components));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -108,27 +168,20 @@ public final class VertexFetchGenerator {
|
||||
}
|
||||
}
|
||||
|
||||
private static String glslType(Conversion conversion, int count) {
|
||||
String scalar = switch (conversion) {
|
||||
case UINT -> "uint";
|
||||
case SINT -> "int";
|
||||
default -> "float";
|
||||
private static ScalarType scalarType(Conversion conversion) {
|
||||
return switch (conversion) {
|
||||
case UINT -> ScalarType.UINT;
|
||||
case SINT -> ScalarType.INT;
|
||||
default -> ScalarType.FLOAT;
|
||||
};
|
||||
|
||||
if (count == 1) {
|
||||
return scalar;
|
||||
}
|
||||
|
||||
String prefix = switch (conversion) {
|
||||
case UINT -> "u";
|
||||
case SINT -> "i";
|
||||
default -> "";
|
||||
};
|
||||
|
||||
return prefix + "vec" + count;
|
||||
}
|
||||
|
||||
public static String generate(ChunkVertexLayout layout, int verticesPerPrimitive, VertexFetchMode mode, boolean legacyGlsl) {
|
||||
return generate(layout, verticesPerPrimitive, mode, legacyGlsl, Map.of());
|
||||
}
|
||||
|
||||
public static String generate(ChunkVertexLayout layout, int verticesPerPrimitive, VertexFetchMode mode, boolean legacyGlsl,
|
||||
Map<String, GlslType> declaredTypes) {
|
||||
if (mode == VertexFetchMode.ATTRIBUTES && !layout.supportsAttributeFetch()) {
|
||||
throw new IllegalArgumentException("Layout cannot be fetched through vertex attributes: " + layout);
|
||||
}
|
||||
@@ -146,7 +199,7 @@ public final class VertexFetchGenerator {
|
||||
String storage = mode == VertexFetchMode.ATTRIBUTES ? "in " : "";
|
||||
|
||||
for (Element element : elements) {
|
||||
out.append(storage).append(element.glslType()).append(' ').append(element.name()).append(";\n");
|
||||
out.append(storage).append(declaredType(element, declaredTypes)).append(' ').append(element.name()).append(";\n");
|
||||
}
|
||||
|
||||
out.append("\nint _vert_corner;\n\n");
|
||||
@@ -163,7 +216,7 @@ public final class VertexFetchGenerator {
|
||||
.flatMap(e -> e.components().stream())
|
||||
.anyMatch(c -> c.conversion() == Conversion.FLOAT_BITS);
|
||||
|
||||
out.append("uniform usamplerBuffer u_VertexData;\n\n");
|
||||
out.append("uniform usamplerBuffer ").append(VERTEX_DATA_SAMPLER).append(";\n\n");
|
||||
|
||||
if (needsFloatHelper) {
|
||||
appendFloatHelper(out);
|
||||
@@ -198,28 +251,30 @@ public final class VertexFetchGenerator {
|
||||
}
|
||||
|
||||
for (int word : words) {
|
||||
out.append(" uint ").append(wordName(scope, word)).append(" = ").append(fetch).append("(u_VertexData, ")
|
||||
out.append(" uint ").append(wordName(scope, word)).append(" = ").append(fetch).append('(').append(VERTEX_DATA_SAMPLER).append(", ")
|
||||
.append(scope == Scope.HEADER ? "_vf_record" : "_vf_vertex").append(" + ").append(word).append(").r;\n");
|
||||
}
|
||||
}
|
||||
|
||||
for (Element element : elements) {
|
||||
GlslType type = declaredType(element, declaredTypes);
|
||||
var values = new ArrayList<String>(type.count());
|
||||
|
||||
for (int i = 0; i < type.count(); i++) {
|
||||
if (i < element.components().size()) {
|
||||
String value = componentExpression(element.components().get(i), legacyGlsl);
|
||||
values.add(type.scalar() == element.type().scalar() ? value : type.scalar().name + "(" + value + ")");
|
||||
} else {
|
||||
values.add(i == 3 ? type.scalar().one : type.scalar().zero);
|
||||
}
|
||||
}
|
||||
|
||||
out.append(" ").append(element.name()).append(" = ");
|
||||
|
||||
if (element.components().size() == 1) {
|
||||
out.append(componentExpression(element.components().get(0), legacyGlsl));
|
||||
if (type.count() == 1) {
|
||||
out.append(values.get(0));
|
||||
} else {
|
||||
out.append(element.glslType()).append('(');
|
||||
|
||||
for (int i = 0; i < element.components().size(); i++) {
|
||||
if (i > 0) {
|
||||
out.append(", ");
|
||||
}
|
||||
|
||||
out.append(componentExpression(element.components().get(i), legacyGlsl));
|
||||
}
|
||||
|
||||
out.append(')');
|
||||
out.append(type).append('(').append(String.join(", ", values)).append(')');
|
||||
}
|
||||
|
||||
out.append(";\n");
|
||||
@@ -230,6 +285,10 @@ public final class VertexFetchGenerator {
|
||||
return out.toString();
|
||||
}
|
||||
|
||||
private static GlslType declaredType(Element element, Map<String, GlslType> declaredTypes) {
|
||||
return declaredTypes.getOrDefault(element.name(), element.type());
|
||||
}
|
||||
|
||||
/**
|
||||
* Decodes IEEE 754 single-precision bits using only integer and float arithmetic, for GLSL without
|
||||
* {@code uintBitsToFloat}. Exact for normal numbers; denormals become zero, and infinities and NaNs are not handled.
|
||||
|
||||
+51
@@ -16,6 +16,7 @@ import java.nio.ByteBuffer;
|
||||
import java.nio.ByteOrder;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Random;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
@@ -81,6 +82,56 @@ class VertexFetchGeneratorTest {
|
||||
assertFalse(source.contains("uintBitsToFloat"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void declaresEveryElementOfTheLayout() {
|
||||
var layout = ChunkVertexLayout.of(GlVertexFormat.builder(8)
|
||||
.addElement("mc_Entity", 0, GlVertexAttributeFormat.SHORT, 2, false, false)
|
||||
.addElement("anything_else", 4, GlVertexAttributeFormat.UNSIGNED_INT, 1, false, true)
|
||||
.build());
|
||||
|
||||
String source = VertexFetchGenerator.generate(layout, 4, VertexFetchMode.ATTRIBUTES, false);
|
||||
|
||||
assertTrue(source.contains("in vec2 mc_Entity;"));
|
||||
assertTrue(source.contains("in uint anything_else;"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void declaredTypesPadLikeAttributeFetch() {
|
||||
var layout = ChunkVertexLayout.of(GlVertexFormat.builder(4)
|
||||
.addElement("mc_Entity", 0, GlVertexAttributeFormat.SHORT, 2, false, false)
|
||||
.build());
|
||||
var declared = Map.of("mc_Entity", VertexFetchGenerator.GlslType.parse("vec4"));
|
||||
|
||||
String attributes = VertexFetchGenerator.generate(layout, 4, VertexFetchMode.ATTRIBUTES, false, declared);
|
||||
String pulled = VertexFetchGenerator.generate(layout, 4, VertexFetchMode.PULLED, false, declared);
|
||||
|
||||
assertTrue(attributes.contains("in vec4 mc_Entity;"));
|
||||
assertTrue(pulled.contains("\nvec4 mc_Entity;"));
|
||||
assertTrue(pulled.contains("mc_Entity = vec4(float((int((_vf_v0 << 16u)) >> 16)), float((int(_vf_v0) >> 16)), 0.0, 1.0);"),
|
||||
pulled);
|
||||
}
|
||||
|
||||
@Test
|
||||
void declaredTypesTruncateAndCast() {
|
||||
var layout = ChunkVertexLayout.of(GlVertexFormat.builder(4)
|
||||
.addElement("at_midBlock", 0, GlVertexAttributeFormat.BYTE, 4, false, false)
|
||||
.build());
|
||||
var declared = Map.of("at_midBlock", VertexFetchGenerator.GlslType.parse("ivec2"));
|
||||
|
||||
String pulled = VertexFetchGenerator.generate(layout, 4, VertexFetchMode.PULLED, false, declared);
|
||||
|
||||
assertTrue(pulled.contains("at_midBlock = ivec2(int(float((int((_vf_v0 << 24u)) >> 24))), int(float((int((_vf_v0 << 16u)) >> 24))));"),
|
||||
pulled);
|
||||
}
|
||||
|
||||
@Test
|
||||
void parsesOnlyScalarAndVectorTypes() {
|
||||
assertEquals(new VertexFetchGenerator.GlslType(VertexFetchGenerator.ScalarType.UINT, 3), VertexFetchGenerator.GlslType.parse("uvec3"));
|
||||
assertEquals(new VertexFetchGenerator.GlslType(VertexFetchGenerator.ScalarType.INT, 1), VertexFetchGenerator.GlslType.parse("int"));
|
||||
assertThrows(IllegalArgumentException.class, () -> VertexFetchGenerator.GlslType.parse("mat4"));
|
||||
assertThrows(IllegalArgumentException.class, () -> VertexFetchGenerator.GlslType.parse("vec5"));
|
||||
}
|
||||
|
||||
/**
|
||||
* Mirrors the arithmetic of the generated {@code _vf_bits_to_float}, which must be exact for normal floats.
|
||||
*/
|
||||
|
||||
+13
@@ -1,5 +1,6 @@
|
||||
package org.embeddedt.embeddium.impl.modern.render.chunk;
|
||||
|
||||
import com.mojang.blaze3d.platform.GlStateManager;
|
||||
import org.embeddedt.embeddium.impl.Celeritas;
|
||||
import org.embeddedt.embeddium.impl.gl.compat.FogHelper;
|
||||
import org.embeddedt.embeddium.impl.gl.device.RenderDevice;
|
||||
@@ -7,6 +8,7 @@ import org.embeddedt.embeddium.impl.render.chunk.DefaultChunkRenderer;
|
||||
import org.embeddedt.embeddium.impl.render.chunk.RenderPassConfiguration;
|
||||
import org.embeddedt.embeddium.impl.render.chunk.shader.ChunkShaderInterface;
|
||||
import org.embeddedt.embeddium.impl.render.chunk.shader.ChunkShaderTextureSlot;
|
||||
import org.lwjgl.opengl.GL13C;
|
||||
|
||||
public class ModernChunkRenderer extends DefaultChunkRenderer {
|
||||
public ModernChunkRenderer(RenderDevice device, RenderPassConfiguration<?> renderPassConfiguration) {
|
||||
@@ -29,6 +31,17 @@ public class ModernChunkRenderer extends DefaultChunkRenderer {
|
||||
return Celeritas.options().performance.useBlockFaceCulling /*? if shaders {*/ && !net.irisshaders.iris.shadows.ShadowRenderingState.areShadowsCurrentlyBeingRendered() /*?}*/;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected int activateTextureUnit(int unit) {
|
||||
//? if >=26.1 {
|
||||
/*int previous = GL13C.GL_TEXTURE0 + org.embeddedt.embeddium.impl.mixin.core.GlStateManagerAccessor.celeritas$getActiveTexture();
|
||||
*///?} else {
|
||||
int previous = GlStateManager._getActiveTexture();
|
||||
//?}
|
||||
GlStateManager._activeTexture(unit);
|
||||
return previous;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void configureShaderInterface(ChunkShaderInterface shader) {
|
||||
shader.setTextureSlot(ChunkShaderTextureSlot.BLOCK, 0);
|
||||
|
||||
+16
@@ -0,0 +1,16 @@
|
||||
package org.embeddedt.embeddium.impl.mixin.core;
|
||||
|
||||
import com.mojang.blaze3d.platform.GlStateManager;
|
||||
import org.spongepowered.asm.mixin.Mixin;
|
||||
import org.spongepowered.asm.mixin.gen.Accessor;
|
||||
|
||||
@Mixin(GlStateManager.class)
|
||||
public interface GlStateManagerAccessor {
|
||||
/**
|
||||
* {@return the cached active texture unit, as an index rather than {@code GL_TEXTURE0 + n}}
|
||||
*/
|
||||
@Accessor("activeTexture")
|
||||
static int celeritas$getActiveTexture() {
|
||||
throw new AssertionError();
|
||||
}
|
||||
}
|
||||
+6
-7
@@ -91,14 +91,13 @@ public class IrisChunkProgramOverrides {
|
||||
|
||||
createdShaders.values().forEach(builder::attachShader);
|
||||
|
||||
int i = 0;
|
||||
var terrainPass = pass.toTerrainPass(configuration);
|
||||
if (terrainPass.vertexFetchMode() != VertexFetchMode.ATTRIBUTES) {
|
||||
throw new UnsupportedOperationException("Vertex pulling is not supported with shader packs");
|
||||
}
|
||||
var vertexType = configuration.getVertexTypeForPass(terrainPass);
|
||||
for (var attr : ChunkVertexLayout.requireAttributes(vertexType.getLayout()).getAttributes()) {
|
||||
builder.bindAttribute(attr.getName(), i++);
|
||||
if (terrainPass.vertexFetchMode() == VertexFetchMode.ATTRIBUTES) {
|
||||
int i = 0;
|
||||
var vertexType = configuration.getVertexTypeForPass(terrainPass);
|
||||
for (var attr : ChunkVertexLayout.requireAttributes(vertexType.getLayout()).getAttributes()) {
|
||||
builder.bindAttribute(attr.getName(), i++);
|
||||
}
|
||||
}
|
||||
return builder
|
||||
.link((shader) -> {
|
||||
|
||||
+2
-2
@@ -335,7 +335,7 @@ public class IrisRenderingPipeline implements WorldRenderingPipeline, ShaderRend
|
||||
() -> isBeforeTranslucent ? flippedAfterPrepare : flippedAfterTranslucent;
|
||||
|
||||
IntFunction<ProgramSamplers> createTerrainSamplers = (programId) -> {
|
||||
ProgramSamplers.Builder builder = ProgramSamplers.builder(programId, IrisSamplers.WORLD_RESERVED_TEXTURE_UNITS);
|
||||
ProgramSamplers.Builder builder = IrisSamplers.terrainSamplerBuilder(programId);
|
||||
|
||||
ProgramSamplers.CustomTextureSamplerInterceptor customTextureSamplerInterceptor = ProgramSamplers.customTextureSamplerInterceptor(builder, customTextureManager.getCustomTextureIdMap().getOrDefault(TextureStage.GBUFFERS_AND_SHADOW, Object2ObjectMaps.emptyMap()));
|
||||
|
||||
@@ -378,7 +378,7 @@ public class IrisRenderingPipeline implements WorldRenderingPipeline, ShaderRend
|
||||
this.dhCompat = new DHCompat(this, shadowDirectives.isDhShadowEnabled().orElse(true));
|
||||
|
||||
IntFunction<ProgramSamplers> createShadowTerrainSamplers = (programId) -> {
|
||||
ProgramSamplers.Builder builder = ProgramSamplers.builder(programId, IrisSamplers.WORLD_RESERVED_TEXTURE_UNITS);
|
||||
ProgramSamplers.Builder builder = IrisSamplers.terrainSamplerBuilder(programId);
|
||||
|
||||
ProgramSamplers.CustomTextureSamplerInterceptor customTextureSamplerInterceptor = ProgramSamplers.customTextureSamplerInterceptor(builder, customTextureManager.getCustomTextureIdMap().getOrDefault(TextureStage.GBUFFERS_AND_SHADOW, Object2ObjectMaps.emptyMap()));
|
||||
|
||||
|
||||
+72
-22
@@ -12,8 +12,8 @@ import org.embeddedt.embeddium.impl.gl.shader.ShaderConstants;
|
||||
import org.embeddedt.embeddium.impl.gl.shader.ShaderType;
|
||||
import org.embeddedt.embeddium.impl.render.chunk.compile.sorting.QuadPrimitiveType;
|
||||
import org.embeddedt.embeddium.impl.render.chunk.vertex.format.ChunkMeshFormats;
|
||||
import org.embeddedt.embeddium.impl.render.chunk.vertex.format.ChunkVertexLayout;
|
||||
import org.embeddedt.embeddium.impl.render.chunk.vertex.format.VertexFetchGenerator;
|
||||
import org.embeddedt.embeddium.impl.render.chunk.vertex.format.VertexFetchMode;
|
||||
import org.embeddedt.embeddium.impl.render.shader.ShaderLoader;
|
||||
import org.taumc.glsl.ShaderParser;
|
||||
import org.taumc.glsl.ShaderPrinter;
|
||||
@@ -127,6 +127,7 @@ public class ShaderTransformer {
|
||||
EnumMap<ShaderType, String> result = new EnumMap<>(ShaderType.class);
|
||||
EnumMap<ShaderType, Transformer> types = new EnumMap<>(ShaderType.class);
|
||||
EnumMap<ShaderType, String> prepatched = new EnumMap<>(ShaderType.class);
|
||||
Map<String, VertexFetchGenerator.GlslType> vertexInputTypes = new HashMap<>();
|
||||
|
||||
for (ShaderType type : ShaderType.values()) {
|
||||
parameters.type = type;
|
||||
@@ -226,6 +227,11 @@ public class ShaderTransformer {
|
||||
}
|
||||
}
|
||||
CompTransformer.transformEach(transformer, parameters);
|
||||
|
||||
if (patchType == Patch.SODIUM && type == ShaderType.VERTEX) {
|
||||
vertexInputTypes.putAll(takeLayoutInputs(transformer, ((SodiumParameters) parameters).vertexType.getLayout()));
|
||||
}
|
||||
|
||||
types.put(type, transformer);
|
||||
|
||||
var extensions = versionPattern.matcher(getFormattedShader(parsedShader.pre(), "")).replaceFirst("").trim();
|
||||
@@ -238,7 +244,7 @@ public class ShaderTransformer {
|
||||
String header = prepatched.get(entry.getKey());
|
||||
if (entry.getKey() == ShaderType.VERTEX && patchType == Patch.SODIUM) {
|
||||
// Inject _vert_init contents
|
||||
header += computeCeleritasHeader((SodiumParameters)parameters);
|
||||
header += computeCeleritasHeader((SodiumParameters)parameters, vertexInputTypes);
|
||||
}
|
||||
String formattedShader = getFormattedShader(tree, header);
|
||||
result.put(entry.getKey(), formattedShader);
|
||||
@@ -247,10 +253,45 @@ public class ShaderTransformer {
|
||||
return result;
|
||||
}
|
||||
|
||||
private static String computeCeleritasHeader(SodiumParameters parameters) {
|
||||
/**
|
||||
* Removes the vertex stage's declarations of inputs that the layout provides, and returns the types they were declared
|
||||
* with. The generated header then declares those inputs with the same types.
|
||||
*/
|
||||
private static Map<String, VertexFetchGenerator.GlslType> takeLayoutInputs(Transformer translationUnit, ChunkVertexLayout layout) {
|
||||
Set<String> layoutNames = new HashSet<>();
|
||||
layout.vertex().getAttributes().forEach(attribute -> layoutNames.add(attribute.getName()));
|
||||
|
||||
if (layout.header() != null) {
|
||||
layout.header().format().getAttributes().forEach(attribute -> layoutNames.add(attribute.getName()));
|
||||
}
|
||||
|
||||
Map<String, VertexFetchGenerator.GlslType> declaredTypes = new HashMap<>();
|
||||
|
||||
for (int qualifier : new int[] { GLSLLexer.IN, GLSLLexer.ATTRIBUTE }) {
|
||||
translationUnit.findQualifiers(qualifier).forEach((name, declaration) -> {
|
||||
if (!layoutNames.contains(name)) {
|
||||
return;
|
||||
}
|
||||
|
||||
var specifier = declaration.fully_specified_type().type_specifier();
|
||||
|
||||
if (specifier.array_specifier() != null) {
|
||||
throw new IllegalStateException("Vertex input " + name + " cannot be declared as an array");
|
||||
}
|
||||
|
||||
declaredTypes.put(name, VertexFetchGenerator.GlslType.parse(specifier.type_specifier_nonarray().getText()));
|
||||
});
|
||||
}
|
||||
|
||||
declaredTypes.keySet().forEach(translationUnit::removeVariable);
|
||||
|
||||
return declaredTypes;
|
||||
}
|
||||
|
||||
private static String computeCeleritasHeader(SodiumParameters parameters, Map<String, VertexFetchGenerator.GlslType> vertexInputTypes) {
|
||||
var defines = parameters.vertexType.getDefines();
|
||||
var generatedSources = VertexFetchGenerator.sources(parameters.vertexType.getLayout(),
|
||||
QuadPrimitiveType.TRIANGULATED.getVerticesPerPrimitive(), VertexFetchMode.ATTRIBUTES, false);
|
||||
QuadPrimitiveType.TRIANGULATED.getVerticesPerPrimitive(), parameters.vertexFetchMode, false, vertexInputTypes);
|
||||
String chunkVertexHeader = org.embeddedt.embeddium.impl.gl.shader.ShaderParser.parseShader(ShaderLoader.getShaderSource("sodium:include/chunk_vertex.glsl"), ShaderLoader.sourceProvider(generatedSources),
|
||||
ShaderConstants.builder().addAll(defines).build());
|
||||
return "\n\n" + chunkVertexHeader + "\n\n";
|
||||
@@ -285,27 +326,36 @@ public class ShaderTransformer {
|
||||
translationUnit.removeVariable("mc_midTexCoord");
|
||||
}
|
||||
translationUnit.replaceExpression("mc_midTexCoord", "iris_MidTex");
|
||||
switch (type) {
|
||||
case 0:
|
||||
return;
|
||||
case GLSLLexer.BOOL:
|
||||
return;
|
||||
case GLSLLexer.FLOAT:
|
||||
translationUnit.injectFunction("float iris_MidTex = (mc_midTexCoord.x * " + textureScale + ").x;"); //TODO go back to variable if order is fixed
|
||||
break;
|
||||
case GLSLLexer.VEC2:
|
||||
translationUnit.injectFunction("vec2 iris_MidTex = (mc_midTexCoord.xy * " + textureScale + ").xy;");
|
||||
break;
|
||||
case GLSLLexer.VEC3:
|
||||
translationUnit.injectFunction("vec3 iris_MidTex = vec3(mc_midTexCoord.xy * " + textureScale + ", 0.0);");
|
||||
break;
|
||||
case GLSLLexer.VEC4:
|
||||
translationUnit.injectFunction("vec4 iris_MidTex = vec4(mc_midTexCoord.xy * " + textureScale + ", 0.0, 1.0);");
|
||||
break;
|
||||
default:
|
||||
|
||||
String declaration;
|
||||
String value;
|
||||
|
||||
switch (type) {
|
||||
case GLSLLexer.FLOAT -> {
|
||||
declaration = "float iris_MidTex;";
|
||||
value = "(mc_midTexCoord.x * " + textureScale + ")";
|
||||
}
|
||||
case GLSLLexer.VEC2 -> {
|
||||
declaration = "vec2 iris_MidTex;";
|
||||
value = "(mc_midTexCoord.xy * " + textureScale + ")";
|
||||
}
|
||||
case GLSLLexer.VEC3 -> {
|
||||
declaration = "vec3 iris_MidTex;";
|
||||
value = "vec3(mc_midTexCoord.xy * " + textureScale + ", 0.0)";
|
||||
}
|
||||
case GLSLLexer.VEC4 -> {
|
||||
declaration = "vec4 iris_MidTex;";
|
||||
value = "vec4(mc_midTexCoord.xy * " + textureScale + ", 0.0, 1.0)";
|
||||
}
|
||||
default -> {
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
// Assigned in main rather than initialized, since pulled vertex data is only available once _vert_init has run
|
||||
translationUnit.injectVariable(declaration);
|
||||
translationUnit.prependMain("iris_MidTex = " + value + ";");
|
||||
|
||||
translationUnit.injectVariable("in vec2 mc_midTexCoord;"); //TODO why is this inserted oddly?
|
||||
|
||||
}
|
||||
|
||||
+1
-1
@@ -60,7 +60,7 @@ public class TransformPatcherBridge {
|
||||
AlphaTest alpha, ShaderAttributeInputs inputs,
|
||||
Object2ObjectMap<Tri<String, TextureType, TextureStage>, String> textureMap, TerrainRenderPass pass) {
|
||||
return transform(name, sources,
|
||||
new SodiumParameters(Patch.SODIUM, textureMap, alpha, inputs, pass.vertexType()));
|
||||
new SodiumParameters(Patch.SODIUM, textureMap, alpha, inputs, pass.vertexType(), pass.vertexFetchMode()));
|
||||
}
|
||||
|
||||
public static Map<ShaderType, String> patchComposite(
|
||||
|
||||
+5
-1
@@ -10,23 +10,27 @@ import net.irisshaders.iris.pipeline.transform.Patch;
|
||||
import net.irisshaders.iris.shaderpack.texture.TextureStage;
|
||||
import org.embeddedt.embeddium.impl.render.chunk.terrain.TerrainRenderPass;
|
||||
import org.embeddedt.embeddium.impl.render.chunk.vertex.format.ChunkVertexType;
|
||||
import org.embeddedt.embeddium.impl.render.chunk.vertex.format.VertexFetchMode;
|
||||
|
||||
@EqualsAndHashCode(callSuper = true)
|
||||
public class SodiumParameters extends Parameters {
|
||||
public final ShaderAttributeInputs inputs;
|
||||
public final AlphaTest alpha;
|
||||
public final ChunkVertexType vertexType;
|
||||
public final VertexFetchMode vertexFetchMode;
|
||||
|
||||
public SodiumParameters(Patch patch,
|
||||
Object2ObjectMap<Tri<String, TextureType, TextureStage>, String> textureMap,
|
||||
AlphaTest alpha,
|
||||
ShaderAttributeInputs inputs,
|
||||
ChunkVertexType vertexType) {
|
||||
ChunkVertexType vertexType,
|
||||
VertexFetchMode vertexFetchMode) {
|
||||
super(patch, textureMap);
|
||||
this.inputs = inputs;
|
||||
|
||||
this.alpha = alpha;
|
||||
this.vertexType = vertexType;
|
||||
this.vertexFetchMode = vertexFetchMode;
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
@@ -2,8 +2,10 @@ package net.irisshaders.iris.samplers;
|
||||
|
||||
import com.google.common.collect.ImmutableList;
|
||||
import com.google.common.collect.ImmutableSet;
|
||||
import com.mojang.blaze3d.platform.GlStateManager;
|
||||
import it.unimi.dsi.fastutil.objects.Object2ObjectMap;
|
||||
import net.irisshaders.iris.gl.image.GlImage;
|
||||
import net.irisshaders.iris.gl.program.ProgramSamplers;
|
||||
import net.irisshaders.iris.gl.sampler.GlSampler;
|
||||
import net.irisshaders.iris.gl.sampler.SamplerHolder;
|
||||
import net.irisshaders.iris.gl.state.StateUpdateNotifiers;
|
||||
@@ -16,6 +18,8 @@ import net.irisshaders.iris.shadows.ShadowRenderTargets;
|
||||
import net.irisshaders.iris.targets.RenderTarget;
|
||||
import net.irisshaders.iris.targets.RenderTargets;
|
||||
import net.minecraft.client.renderer.texture.AbstractTexture;
|
||||
import org.embeddedt.embeddium.impl.render.chunk.DefaultChunkRenderer;
|
||||
import org.embeddedt.embeddium.impl.render.chunk.vertex.format.VertexFetchGenerator;
|
||||
|
||||
import java.util.Set;
|
||||
import java.util.function.IntSupplier;
|
||||
@@ -28,6 +32,14 @@ public class IrisSamplers {
|
||||
|
||||
public static final ImmutableSet<Integer> WORLD_RESERVED_TEXTURE_UNITS = ImmutableSet.of(0, 1, 2);
|
||||
|
||||
/**
|
||||
* Terrain programs that pull vertex data also reserve the unit Celeritas binds it to.
|
||||
*/
|
||||
private static final ImmutableSet<Integer> PULLED_TERRAIN_RESERVED_TEXTURE_UNITS = ImmutableSet.<Integer>builder()
|
||||
.addAll(WORLD_RESERVED_TEXTURE_UNITS)
|
||||
.add(DefaultChunkRenderer.VERTEX_DATA_TEXTURE_UNIT)
|
||||
.build();
|
||||
|
||||
// TODO: In composite programs, there shouldn't be any reserved textures.
|
||||
// We need a way to restore these texture bindings.
|
||||
public static final ImmutableSet<Integer> COMPOSITE_RESERVED_TEXTURE_UNITS = ImmutableSet.of(1, 2);
|
||||
@@ -40,6 +52,23 @@ public class IrisSamplers {
|
||||
// no construction allowed
|
||||
}
|
||||
|
||||
/**
|
||||
* Starts the samplers of a Celeritas terrain program. Units are only reserved for vertex data when the program reads
|
||||
* it, so that other programs keep every unit for the pack.
|
||||
*/
|
||||
public static ProgramSamplers.Builder terrainSamplerBuilder(int programId) {
|
||||
boolean pulled = GlStateManager._glGetUniformLocation(programId, VertexFetchGenerator.VERTEX_DATA_SAMPLER) != -1;
|
||||
|
||||
if (!pulled) {
|
||||
return ProgramSamplers.builder(programId, WORLD_RESERVED_TEXTURE_UNITS);
|
||||
}
|
||||
|
||||
ProgramSamplers.Builder builder = ProgramSamplers.builder(programId, PULLED_TERRAIN_RESERVED_TEXTURE_UNITS);
|
||||
builder.addExternalSampler(DefaultChunkRenderer.VERTEX_DATA_TEXTURE_UNIT, VertexFetchGenerator.VERTEX_DATA_SAMPLER);
|
||||
|
||||
return builder;
|
||||
}
|
||||
|
||||
public static void initRenderer() {
|
||||
SHADOW_SAMPLER_NEAREST = new GlSampler(false, false, true, true);
|
||||
SHADOW_SAMPLER_LINEAR = new GlSampler(true, false, true, true);
|
||||
|
||||
Reference in new issue
Block a user