Vertex pulling #48
No files matched your search
+17
@@ -99,6 +99,23 @@ public final class VertexFetchGenerator {
|
||||
throw new IllegalArgumentException("Unsupported vertex input type " + name);
|
||||
}
|
||||
|
||||
/**
|
||||
* {@return GLSL converting a value of type {@code from} to this type, the way attribute fetch would}
|
||||
*/
|
||||
public String adapt(String expression, GlslType from) {
|
||||
if (this.equals(from)) {
|
||||
return expression;
|
||||
}
|
||||
|
||||
var out = new StringBuilder(this.toString()).append('(').append(expression);
|
||||
|
||||
for (int i = from.count; i < this.count; i++) {
|
||||
out.append(", ").append(i == 3 ? this.scalar.one : this.scalar.zero);
|
||||
}
|
||||
|
||||
return out.append(')').toString();
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return this.count == 1 ? this.scalar.name : this.scalar.vectorPrefix + "vec" + this.count;
|
||||
|
||||
+5
-1
@@ -58,6 +58,11 @@ public class VanillaLikeChunkVertex implements ChunkVertexType {
|
||||
}
|
||||
|
||||
public static void writeVertex(long ptr, Material material, ChunkVertexEncoder.Vertex vertex, int sectionIndex) {
|
||||
writeVertexWithoutRdhFactor(ptr, material, vertex, sectionIndex);
|
||||
LWJGL.memPutInt(ptr + 28, vertex.rdhFactor);
|
||||
}
|
||||
|
||||
public static void writeVertexWithoutRdhFactor(long ptr, Material material, ChunkVertexEncoder.Vertex vertex, int sectionIndex) {
|
||||
LWJGL.memPutFloat(ptr + 0, vertex.x);
|
||||
LWJGL.memPutFloat(ptr + 4, vertex.y);
|
||||
LWJGL.memPutFloat(ptr + 8, vertex.z);
|
||||
@@ -65,7 +70,6 @@ public class VanillaLikeChunkVertex implements ChunkVertexType {
|
||||
LWJGL.memPutFloat(ptr + 16, encodeTexture(vertex.u));
|
||||
LWJGL.memPutFloat(ptr + 20, encodeTexture(vertex.v));
|
||||
LWJGL.memPutInt(ptr + 24, (encodeDrawParameters(material, sectionIndex) << 0) | (encodeLight(vertex.light) << 16));
|
||||
LWJGL.memPutInt(ptr + 28, vertex.rdhFactor);
|
||||
}
|
||||
|
||||
private static int encodeDrawParameters(Material material, int sectionIndex) {
|
||||
|
||||
+10
@@ -124,6 +124,16 @@ class VertexFetchGeneratorTest {
|
||||
pulled);
|
||||
}
|
||||
|
||||
@Test
|
||||
void adaptsValuesLikeAttributeFetch() {
|
||||
var vec4 = VertexFetchGenerator.GlslType.parse("vec4");
|
||||
|
||||
assertEquals("x", vec4.adapt("x", vec4));
|
||||
assertEquals("vec3(x)", VertexFetchGenerator.GlslType.parse("vec3").adapt("x", vec4));
|
||||
assertEquals("vec4(x, 0.0, 1.0)", vec4.adapt("x", VertexFetchGenerator.GlslType.parse("vec2")));
|
||||
assertEquals("ivec4(x, 1)", VertexFetchGenerator.GlslType.parse("ivec4").adapt("x", VertexFetchGenerator.GlslType.parse("vec3")));
|
||||
}
|
||||
|
||||
@Test
|
||||
void parsesOnlyScalarAndVectorTypes() {
|
||||
assertEquals(new VertexFetchGenerator.GlslType(VertexFetchGenerator.ScalarType.UINT, 3), VertexFetchGenerator.GlslType.parse("uvec3"));
|
||||
|
||||
+21
-11
@@ -8,23 +8,33 @@ import org.embeddedt.embeddium.impl.render.chunk.vertex.format.ChunkVertexLayout
|
||||
import org.embeddedt.embeddium.impl.render.chunk.vertex.format.ChunkVertexType;
|
||||
|
||||
/**
|
||||
* Like HFPModelVertexType, but extended to support Iris. The extensions aren't particularly efficient right now.
|
||||
* Like HFPModelVertexType, but extended to support Iris.
|
||||
*/
|
||||
public class XHFPModelVertexType implements ChunkVertexType {
|
||||
public static final ChunkVertexType BASE_VERTEX_TYPE = ChunkMeshFormats.VANILLA_LIKE;
|
||||
|
||||
public static final int STRIDE = 48;
|
||||
public static final GlVertexFormat VERTEX_FORMAT = GlVertexFormat.builder(STRIDE)
|
||||
// Exclude the rdh factor as shaders cannot use it
|
||||
.addElements(ChunkVertexLayout.requireAttributes(BASE_VERTEX_TYPE.getLayout()).getAttributes().stream().filter(a -> !a.getName().equals("a_RdhFactor")).toList())
|
||||
.addElement("mc_midTexCoord", GlVertexFormat.NEXT_ALIGNED_POINTER, GlVertexAttributeFormat.UNSIGNED_SHORT, 2, false, false)
|
||||
.addElement("at_tangent", GlVertexFormat.NEXT_ALIGNED_POINTER, GlVertexAttributeFormat.BYTE, 4, true, false)
|
||||
.addElement("iris_Normal", GlVertexFormat.NEXT_ALIGNED_POINTER, GlVertexAttributeFormat.BYTE, 3, true, false)
|
||||
.addElement("mc_Entity", GlVertexFormat.NEXT_ALIGNED_POINTER, GlVertexAttributeFormat.SHORT, 2, false, false)
|
||||
.addElement("at_midBlock", GlVertexFormat.NEXT_ALIGNED_POINTER, GlVertexAttributeFormat.BYTE, 4, false, false)
|
||||
public static final int HEADER_STRIDE = 20;
|
||||
public static final int VERTEX_STRIDE = 28;
|
||||
|
||||
/**
|
||||
* Packs the block's position within its section (4 bits per axis), whether {@code at_midBlock} has an offset
|
||||
* (bit 12), and the block's light emission (bits 16 to 23).
|
||||
*/
|
||||
public static final String BLOCK_INFO = "celeritas_BlockInfo";
|
||||
|
||||
public static final GlVertexFormat HEADER_FORMAT = GlVertexFormat.builder(HEADER_STRIDE)
|
||||
.addElement("mc_midTexCoord", 0, GlVertexAttributeFormat.UNSIGNED_SHORT, 2, false, false)
|
||||
.addElement("at_tangent", 4, GlVertexAttributeFormat.BYTE, 4, true, false)
|
||||
.addElement("iris_Normal", 8, GlVertexAttributeFormat.BYTE, 3, true, false)
|
||||
.addElement("mc_Entity", 12, GlVertexAttributeFormat.SHORT, 2, false, false)
|
||||
.addElement(BLOCK_INFO, 16, GlVertexAttributeFormat.UNSIGNED_INT, 1, false, true)
|
||||
.build();
|
||||
|
||||
public static final ChunkVertexLayout LAYOUT = ChunkVertexLayout.of(VERTEX_FORMAT);
|
||||
public static final GlVertexFormat VERTEX_FORMAT = GlVertexFormat.builder(VERTEX_STRIDE)
|
||||
.addElements(ChunkVertexLayout.requireAttributes(BASE_VERTEX_TYPE.getLayout()).getAttributes().stream().filter(a -> !a.getName().equals("a_RdhFactor")).toList())
|
||||
.build();
|
||||
|
||||
public static final ChunkVertexLayout LAYOUT = ChunkVertexLayout.withHeader(VERTEX_FORMAT, HEADER_FORMAT, 4);
|
||||
|
||||
private static final int TEXTURE_MAX_VALUE = 32768;
|
||||
|
||||
|
||||
+47
-76
@@ -15,7 +15,6 @@ import org.embeddedt.embeddium.impl.modern.render.chunk.compile.pipeline.BlockRe
|
||||
import org.embeddedt.embeddium.impl.render.chunk.terrain.material.Material;
|
||||
import org.embeddedt.embeddium.impl.render.chunk.vertex.format.ChunkVertexEncoder;
|
||||
import org.embeddedt.embeddium.impl.render.chunk.vertex.format.impl.VanillaLikeChunkVertex;
|
||||
import net.irisshaders.iris.vertices.ExtendedDataHelper;
|
||||
import net.irisshaders.iris.vertices.NormI8;
|
||||
import net.irisshaders.iris.vertices.NormalHelper;
|
||||
import org.embeddedt.embeddium.impl.render.texture.TextureAtlasExtended;
|
||||
@@ -27,16 +26,14 @@ import org.taumc.celeritas.shaders.CeleritasShaders;
|
||||
import java.util.Objects;
|
||||
import java.util.concurrent.atomic.AtomicBoolean;
|
||||
|
||||
import static net.irisshaders.iris.compat.sodium.impl.vertex_format.terrain_xhfp.XHFPModelVertexType.STRIDE;
|
||||
import static net.irisshaders.iris.compat.sodium.impl.vertex_format.terrain_xhfp.XHFPModelVertexType.HEADER_STRIDE;
|
||||
import static net.irisshaders.iris.compat.sodium.impl.vertex_format.terrain_xhfp.XHFPModelVertexType.VERTEX_STRIDE;
|
||||
|
||||
public class XHFPTerrainVertex implements ChunkVertexEncoder, ContextAwareChunkVertexEncoder {
|
||||
|
||||
private final QuadViewTerrain.QuadViewTerrainUnsafe quad = new QuadViewTerrain.QuadViewTerrainUnsafe();
|
||||
private final Vector3f normal = new Vector3f();
|
||||
|
||||
private int vertexCount;
|
||||
private float uSum;
|
||||
private float vSum;
|
||||
|
||||
private static final BlockState AIR = Blocks.AIR.defaultBlockState();
|
||||
|
||||
@@ -57,11 +54,11 @@ public class XHFPTerrainVertex implements ChunkVertexEncoder, ContextAwareChunkV
|
||||
|
||||
private final FallbackTextureMaterials fallbackMaterials = WorldRenderingSettings.INSTANCE.getFallbackTextureMaterialMapping();
|
||||
|
||||
private static final int MID_TEX_OFFSET = XHFPModelVertexType.VERTEX_FORMAT.getAttribute("mc_midTexCoord").getPointer();
|
||||
private static final int MC_ENTITY_OFFSET = XHFPModelVertexType.VERTEX_FORMAT.getAttribute("mc_Entity").getPointer();
|
||||
private static final int MID_BLOCK_OFFSET = XHFPModelVertexType.VERTEX_FORMAT.getAttribute("at_midBlock").getPointer();
|
||||
private static final int NORMAL_OFFSET = XHFPModelVertexType.VERTEX_FORMAT.getAttribute("iris_Normal").getPointer();
|
||||
private static final int TANGENT_OFFSET = XHFPModelVertexType.VERTEX_FORMAT.getAttribute("at_tangent").getPointer();
|
||||
private static final int MID_TEX_OFFSET = XHFPModelVertexType.HEADER_FORMAT.getAttribute("mc_midTexCoord").getPointer();
|
||||
private static final int MC_ENTITY_OFFSET = XHFPModelVertexType.HEADER_FORMAT.getAttribute("mc_Entity").getPointer();
|
||||
private static final int NORMAL_OFFSET = XHFPModelVertexType.HEADER_FORMAT.getAttribute("iris_Normal").getPointer();
|
||||
private static final int TANGENT_OFFSET = XHFPModelVertexType.HEADER_FORMAT.getAttribute("at_tangent").getPointer();
|
||||
private static final int BLOCK_INFO_OFFSET = XHFPModelVertexType.HEADER_FORMAT.getAttribute(XHFPModelVertexType.BLOCK_INFO).getPointer();
|
||||
|
||||
private static final ClassValue<AtomicBoolean> GET_APPEARANCE_CRASHED = new ClassValue<>() {
|
||||
@Override
|
||||
@@ -150,79 +147,53 @@ public class XHFPTerrainVertex implements ChunkVertexEncoder, ContextAwareChunkV
|
||||
|
||||
@Override
|
||||
public long writePrimitive(long ptr, Material material, Vertex[] vertices, int chunkId) {
|
||||
long vertexPtr = ptr + HEADER_STRIDE;
|
||||
float uSum = 0.0f;
|
||||
float vSum = 0.0f;
|
||||
|
||||
for (Vertex vertex : vertices) {
|
||||
ptr = this.writeVertex(ptr, material, vertex, chunkId);
|
||||
uSum += vertex.u;
|
||||
vSum += vertex.v;
|
||||
|
||||
VanillaLikeChunkVertex.writeVertexWithoutRdhFactor(vertexPtr, material, vertex, chunkId);
|
||||
|
||||
vertexPtr += VERTEX_STRIDE;
|
||||
}
|
||||
|
||||
return ptr;
|
||||
}
|
||||
float midU = uSum / vertices.length;
|
||||
float midV = vSum / vertices.length;
|
||||
|
||||
private long writeVertex(long ptr, Material material, Vertex vertex, int chunkId) {
|
||||
uSum += vertex.u;
|
||||
vSum += vertex.v;
|
||||
vertexCount++;
|
||||
MemoryUtil.memPutInt(ptr + MID_TEX_OFFSET, XHFPModelVertexType.encodeTexture(midU, midV));
|
||||
|
||||
vertex.rdhFactor = 0;
|
||||
VanillaLikeChunkVertex.writeVertex(ptr, material, vertex, chunkId);
|
||||
short blockId = this.blockId;
|
||||
|
||||
MemoryUtil.memPutShort(ptr + MC_ENTITY_OFFSET + 2, renderType);
|
||||
MemoryUtil.memPutInt(ptr + MID_BLOCK_OFFSET, ignoreMidBlock ? 0 : ExtendedDataHelper.computeMidBlock(vertex.x, vertex.y, vertex.z, localPosX, localPosY, localPosZ));
|
||||
MemoryUtil.memPutByte(ptr + MID_BLOCK_OFFSET + 3, lightValue);
|
||||
|
||||
if (vertexCount == 4) {
|
||||
vertexCount = 0;
|
||||
|
||||
uSum *= 0.25f;
|
||||
vSum *= 0.25f;
|
||||
|
||||
int midUV = XHFPModelVertexType.encodeTexture(uSum, vSum);
|
||||
|
||||
MemoryUtil.memPutInt(ptr + MID_TEX_OFFSET, midUV);
|
||||
MemoryUtil.memPutInt(ptr + MID_TEX_OFFSET - STRIDE, midUV);
|
||||
MemoryUtil.memPutInt(ptr + MID_TEX_OFFSET - STRIDE * 2, midUV);
|
||||
MemoryUtil.memPutInt(ptr + MID_TEX_OFFSET - STRIDE * 3, midUV);
|
||||
|
||||
short blockId = this.blockId;
|
||||
|
||||
if (blockId == -1 && fallbackMaterials != null) {
|
||||
// Try to fall back to the "canonical" block ID for the texture, this can often greatly improve
|
||||
// the visuals of facade-style blocks
|
||||
TextureAtlasSprite sprite = ((TextureAtlasExtended)blocksAtlas).celeritas$findFromUV(uSum, vSum);
|
||||
if (sprite != null) {
|
||||
blockId = (short) fallbackMaterials.getFallbackId(sprite, this.blockState);
|
||||
}
|
||||
}
|
||||
|
||||
MemoryUtil.memPutShort(ptr + MC_ENTITY_OFFSET, blockId);
|
||||
MemoryUtil.memPutShort(ptr + MC_ENTITY_OFFSET - STRIDE, blockId);
|
||||
MemoryUtil.memPutShort(ptr + MC_ENTITY_OFFSET - STRIDE * 2, blockId);
|
||||
MemoryUtil.memPutShort(ptr + MC_ENTITY_OFFSET - STRIDE * 3, blockId);
|
||||
|
||||
uSum = 0;
|
||||
vSum = 0;
|
||||
|
||||
// normal computation
|
||||
// Implementation based on the algorithm found here:
|
||||
// https://github.com/IrisShaders/ShaderDoc/blob/master/vertex-format-extensions.md#surface-normal-vector
|
||||
|
||||
quad.setup(ptr, STRIDE);
|
||||
NormalHelper.computeFaceNormal(normal, quad);
|
||||
int packedNormal = NormI8.pack(normal);
|
||||
|
||||
|
||||
MemoryUtil.memPutInt(ptr + NORMAL_OFFSET, packedNormal);
|
||||
MemoryUtil.memPutInt(ptr + NORMAL_OFFSET - STRIDE, packedNormal);
|
||||
MemoryUtil.memPutInt(ptr + NORMAL_OFFSET - STRIDE * 2, packedNormal);
|
||||
MemoryUtil.memPutInt(ptr + NORMAL_OFFSET - STRIDE * 3, packedNormal);
|
||||
|
||||
int tangent = NormalHelper.computeTangent(normal.x, normal.y, normal.z, quad);
|
||||
|
||||
MemoryUtil.memPutInt(ptr + TANGENT_OFFSET, tangent);
|
||||
MemoryUtil.memPutInt(ptr + TANGENT_OFFSET - STRIDE, tangent);
|
||||
MemoryUtil.memPutInt(ptr + TANGENT_OFFSET - STRIDE * 2, tangent);
|
||||
MemoryUtil.memPutInt(ptr + TANGENT_OFFSET - STRIDE * 3, tangent);
|
||||
if (blockId == -1 && fallbackMaterials != null) {
|
||||
// Try to fall back to the "canonical" block ID for the texture, this can often greatly improve
|
||||
// the visuals of facade-style blocks
|
||||
TextureAtlasSprite sprite = ((TextureAtlasExtended)blocksAtlas).celeritas$findFromUV(midU, midV);
|
||||
if (sprite != null) {
|
||||
blockId = (short) fallbackMaterials.getFallbackId(sprite, this.blockState);
|
||||
}
|
||||
}
|
||||
|
||||
return ptr + STRIDE;
|
||||
MemoryUtil.memPutShort(ptr + MC_ENTITY_OFFSET, blockId);
|
||||
MemoryUtil.memPutShort(ptr + MC_ENTITY_OFFSET + 2, renderType);
|
||||
|
||||
// Vertex positions are relative to the section, so only the block's position within it is needed
|
||||
MemoryUtil.memPutInt(ptr + BLOCK_INFO_OFFSET, (localPosX & 15)
|
||||
| ((localPosY & 15) << 4)
|
||||
| ((localPosZ & 15) << 8)
|
||||
| (ignoreMidBlock ? 0 : 1 << 12)
|
||||
| ((lightValue & 0xFF) << 16));
|
||||
|
||||
// Implementation based on the algorithm found here:
|
||||
// https://github.com/IrisShaders/ShaderDoc/blob/master/vertex-format-extensions.md#surface-normal-vector
|
||||
quad.setup(vertexPtr - VERTEX_STRIDE, VERTEX_STRIDE);
|
||||
NormalHelper.computeFaceNormal(normal, quad);
|
||||
|
||||
MemoryUtil.memPutInt(ptr + NORMAL_OFFSET, NormI8.pack(normal));
|
||||
MemoryUtil.memPutInt(ptr + TANGENT_OFFSET, NormalHelper.computeTangent(normal.x, normal.y, normal.z, quad));
|
||||
|
||||
return vertexPtr;
|
||||
}
|
||||
}
|
||||
+41
@@ -1,6 +1,7 @@
|
||||
package net.irisshaders.iris.pipeline.foss_transform;
|
||||
|
||||
import it.unimi.dsi.fastutil.objects.Object2ObjectLinkedOpenHashMap;
|
||||
import net.irisshaders.iris.compat.sodium.impl.vertex_format.terrain_xhfp.XHFPModelVertexType;
|
||||
import net.irisshaders.iris.gl.blending.AlphaTest;
|
||||
import net.irisshaders.iris.pipeline.transform.Patch;
|
||||
import net.irisshaders.iris.pipeline.transform.parameter.Parameters;
|
||||
@@ -360,6 +361,46 @@ public class ShaderTransformer {
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* XHFP doesn't store {@code at_midBlock}: it is derived from the block's position within its section, which the vertex
|
||||
* position is relative to, and stored in 1/64ths of a block with the block's light emission in {@code w}.
|
||||
*/
|
||||
public static void replaceMidBlock(Transformer translationUnit, SodiumParameters parameters) {
|
||||
if (!(parameters.vertexType instanceof XHFPModelVertexType)) {
|
||||
return;
|
||||
}
|
||||
|
||||
var declaredType = findInputType(translationUnit, "at_midBlock");
|
||||
|
||||
if (declaredType == null) {
|
||||
return;
|
||||
}
|
||||
|
||||
translationUnit.removeVariable("at_midBlock");
|
||||
translationUnit.injectVariable(declaredType + " at_midBlock;");
|
||||
translationUnit.injectFunction(String.join("\n",
|
||||
"vec4 iris_ComputeMidBlock() {",
|
||||
" uint info = " + XHFPModelVertexType.BLOCK_INFO + ";",
|
||||
" vec3 block = vec3(uvec3(info, info >> 4u, info >> 8u) & uvec3(15u));",
|
||||
" vec3 offset = (info & 0x1000u) != 0u ? (block + 0.5 - _vert_position) * 64.0 : vec3(0.0);",
|
||||
" return vec4(offset, float((info >> 16u) & 0xFFu));",
|
||||
"}"));
|
||||
// Assigned in main, after _vert_init has provided _vert_position
|
||||
translationUnit.prependMain("at_midBlock = " + declaredType.adapt("iris_ComputeMidBlock()", VertexFetchGenerator.GlslType.parse("vec4")) + ";");
|
||||
}
|
||||
|
||||
private static VertexFetchGenerator.GlslType findInputType(Transformer translationUnit, String name) {
|
||||
for (int qualifier : new int[] { GLSLLexer.IN, GLSLLexer.ATTRIBUTE }) {
|
||||
var declaration = translationUnit.findQualifiers(qualifier).get(name);
|
||||
|
||||
if (declaration != null) {
|
||||
return VertexFetchGenerator.GlslType.parse(declaration.fully_specified_type().type_specifier().getText());
|
||||
}
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
public static void addIfNotExists(Transformer translationUnit, String name, String code) {
|
||||
if (!translationUnit.hasVariable(name)) {
|
||||
translationUnit.injectVariable(code);
|
||||
|
||||
+3
@@ -29,6 +29,7 @@ public class SodiumTransformer {
|
||||
|
||||
translationUnit.replaceExpression("textureMatrix", "mat4(1.0f)");
|
||||
replaceMidTexCoord(translationUnit, XHFPModelVertexType.MID_TEX_SCALE);
|
||||
replaceMidBlock(translationUnit, parameters);
|
||||
|
||||
injectVertInit(translationUnit, parameters);
|
||||
}
|
||||
@@ -98,6 +99,8 @@ public class SodiumTransformer {
|
||||
"vec4 getVertexPosition() { return vec4(_vert_position + u_RegionOffset + _get_draw_translation(_draw_id), 1.0f); }");
|
||||
translationUnit.replaceExpression("gl_Vertex", "getVertexPosition()");
|
||||
|
||||
replaceMidBlock(translationUnit, parameters);
|
||||
|
||||
// inject here so that _vert_position is available to the above. (injections
|
||||
// inject in reverse order if performed piece-wise but in correct order if
|
||||
// performed as an array of injections)
|
||||
|
||||
Reference in new issue
Block a user