Skip to content

Commit

Permalink
at_velocity and aggressive packing
Browse files Browse the repository at this point in the history
  • Loading branch information
IMS212 committed Aug 8, 2023
1 parent 07b4cd2 commit cf77ea1
Show file tree
Hide file tree
Showing 16 changed files with 347 additions and 53 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ public static int create(String name, GlShader... shaders) {
GlStateManager._glBindAttribLocation(program, 12, "mc_midTexCoord");
GlStateManager._glBindAttribLocation(program, 13, "at_tangent");
GlStateManager._glBindAttribLocation(program, 14, "at_midBlock");
GlStateManager._glBindAttribLocation(program, 14, "at_velocity");

// TODO: more hardcoding for 1.17
GlStateManager._glBindAttribLocation(program, 0, "Position");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ public class MixinEntityRenderDispatcher {
}

CapturedRenderingState.INSTANCE.setCurrentEntity(intId);
CapturedRenderingState.INSTANCE.setUniqueEntityId(entity.getId());

return type ->
bufferSource.getBuffer(OuterWrappedRenderType.wrapExactlyOnce("iris:is_entity", type, EntityRenderStateShard.INSTANCE));
Expand All @@ -69,5 +70,6 @@ public class MixinEntityRenderDispatcher {
PoseStack poseStack, MultiBufferSource bufferSource, int light,
CallbackInfo ci) {
CapturedRenderingState.INSTANCE.setCurrentEntity(0);
CapturedRenderingState.INSTANCE.setUniqueEntityId(0);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,8 @@ public abstract class MixinBufferBuilder extends DefaultedVertexConsumer impleme
@Shadow
public abstract void nextElement();

@Shadow public abstract void putFloat(int pBufferBuilder0, float pFloat1);

@Override
public void iris$beginWithoutExtending(VertexFormat.Mode drawMode, VertexFormat vertexFormat) {
iris$shouldNotExtend = true;
Expand Down Expand Up @@ -204,20 +206,26 @@ public abstract class MixinBufferBuilder extends DefaultedVertexConsumer impleme
this.nextElement();

// MID_TEXTURE_ELEMENT
this.putFloat(0, 0);
this.putFloat(4, 0);
this.putShort(0, (short) 0);
this.putShort(2, (short) 0);
this.nextElement();
// TANGENT_ELEMENT
this.putInt(0, 0);
this.nextElement();
if (iris$isTerrain) {
// MID_BLOCK_ELEMENT
int posIndex = this.nextElementByte - 48;
int posIndex = this.nextElementByte - 44;
float x = buffer.getFloat(posIndex);
float y = buffer.getFloat(posIndex + 4);
float z = buffer.getFloat(posIndex + 8);
this.putInt(0, ExtendedDataHelper.computeMidBlock(x, y, z, currentLocalPosX, currentLocalPosY, currentLocalPosZ));
this.nextElement();
} else {
// VELOCITY_ELEMENT
this.putFloat(0, 0);
this.putFloat(4, 0);
this.putFloat(8, 0);
this.nextElement();
}

vertexCount++;
Expand All @@ -227,6 +235,11 @@ public abstract class MixinBufferBuilder extends DefaultedVertexConsumer impleme
}
}

@Unique
private static short encodeTexture(float value) {
return (short) (Math.min(0.99999997F, value) * 65536);
}

@Unique
private void fillExtendedData(int vertexAmount) {
vertexCount = 0;
Expand All @@ -251,17 +264,20 @@ private void fillExtendedData(int vertexAmount) {
int normalOffset;
int tangentOffset;
if (iris$isTerrain) {
midUOffset = 16;
midVOffset = 12;
normalOffset = 24;
midUOffset = 12;
midVOffset = 10;
normalOffset = 20;
tangentOffset = 8;
} else {
midUOffset = 14;
midVOffset = 10;
normalOffset = 24;
tangentOffset = 6;
midUOffset = 20;
midVOffset = 18;
normalOffset = 28;
tangentOffset = 16;
}

short midUFinal = encodeTexture(midU);
short midVFinal = encodeTexture(midV);

if (vertexAmount == 3) {
// NormalHelper.computeFaceNormalTri(normal, polygon); // Removed to enable smooth shaded triangles. Mods rendering triangles with bad normals need to recalculate their normals manually or otherwise shading might be inconsistent.

Expand All @@ -270,8 +286,8 @@ private void fillExtendedData(int vertexAmount) {

int tangent = NormalHelper.computeTangentSmooth(NormI8.unpackX(packedNormal), NormI8.unpackY(packedNormal), NormI8.unpackZ(packedNormal), polygon);

buffer.putFloat(nextElementByte - midUOffset - stride * vertex, midU);
buffer.putFloat(nextElementByte - midVOffset - stride * vertex, midV);
buffer.putShort(nextElementByte - midUOffset - stride * vertex, midUFinal);
buffer.putShort(nextElementByte - midVOffset - stride * vertex, midVFinal);
buffer.putInt(nextElementByte - tangentOffset - stride * vertex, tangent);
}
} else {
Expand All @@ -280,8 +296,8 @@ private void fillExtendedData(int vertexAmount) {
int tangent = NormalHelper.computeTangent(normal.x, normal.y, normal.z, polygon);

for (int vertex = 0; vertex < vertexAmount; vertex++) {
buffer.putFloat(nextElementByte - midUOffset - stride * vertex, midU);
buffer.putFloat(nextElementByte - midVOffset - stride * vertex, midV);
buffer.putShort(nextElementByte - midUOffset - stride * vertex, midUFinal);
buffer.putShort(nextElementByte - midVOffset - stride * vertex, midVFinal);
buffer.putInt(nextElementByte - normalOffset - stride * vertex, packedNormal);
buffer.putInt(nextElementByte - tangentOffset - stride * vertex, tangent);
}
Expand Down
8 changes: 8 additions & 0 deletions src/main/java/net/coderbot/iris/pipeline/HandRenderer.java
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,8 @@ public void renderSolid(PoseStack poseStack, float tickDelta, Camera camera, Gam

renderingSolid = true;

CapturedRenderingState.INSTANCE.setUniqueEntityId(Minecraft.getInstance().player.getId());

gameRenderer.itemInHandRenderer.renderHandsWithItems(tickDelta, poseStack, bufferSource.getUnflushableWrapper(), Minecraft.getInstance().player, Minecraft.getInstance().getEntityRenderDispatcher().getPackedLightCoords(camera.getEntity(), tickDelta));

Minecraft.getInstance().getProfiler().pop();
Expand All @@ -102,6 +104,8 @@ public void renderSolid(PoseStack poseStack, float tickDelta, Camera camera, Gam

pipeline.setPhase(WorldRenderingPhase.NONE);

CapturedRenderingState.INSTANCE.setUniqueEntityId(0);

ACTIVE = false;
}

Expand All @@ -120,6 +124,8 @@ public void renderTranslucent(PoseStack poseStack, float tickDelta, Camera camer

setupGlState(gameRenderer, camera, poseStack, tickDelta);

CapturedRenderingState.INSTANCE.setUniqueEntityId(Minecraft.getInstance().player.getId());

gameRenderer.itemInHandRenderer.renderHandsWithItems(tickDelta, poseStack, bufferSource, Minecraft.getInstance().player, Minecraft.getInstance().getEntityRenderDispatcher().getPackedLightCoords(camera.getEntity(), tickDelta));

poseStack.popPose();
Expand All @@ -132,6 +138,8 @@ public void renderTranslucent(PoseStack poseStack, float tickDelta, Camera camer

pipeline.setPhase(WorldRenderingPhase.NONE);

CapturedRenderingState.INSTANCE.setUniqueEntityId(0);

ACTIVE = false;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ public static void transform(

CommonTransformer.transform(t, tree, root, parameters, false);

SodiumTransformer.replaceMidTexCoord(t, tree, root, (1.0f / 65536));

if (parameters.type.glShaderType == ShaderType.VERTEX) {
// Alias of gl_MultiTexCoord1 on 1.15+ for OptiFine
// See https://github.com/IrisShaders/Iris/issues/1149
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ public class CapturedRenderingState {
private int currentRenderedBlockEntity;

private int currentRenderedEntity = -1;
private int uniqueEntityId = -1;
private int currentRenderedItem = -1;

private float currentAlphaTest;
Expand Down Expand Up @@ -118,4 +119,12 @@ public float getCloudTime() {
public void setCloudTime(float cloudTime) {
this.cloudTime = cloudTime;
}

public int getUniqueEntityId() {
return uniqueEntityId;
}

public void setUniqueEntityId(int id) {
uniqueEntityId = id;
}
}
20 changes: 12 additions & 8 deletions src/main/java/net/coderbot/iris/vertices/IrisVertexFormats.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ public class IrisVertexFormats {
public static final VertexFormatElement TANGENT_ELEMENT;
public static final VertexFormatElement MID_BLOCK_ELEMENT;
public static final VertexFormatElement PADDING_SHORT;
public static final VertexFormatElement VELOCITY_FLOAT;

public static final VertexFormat TERRAIN;
public static final VertexFormat ENTITY;
Expand All @@ -21,10 +22,11 @@ public class IrisVertexFormats {
static {
ENTITY_ELEMENT = new VertexFormatElement(11, VertexFormatElement.Type.SHORT, VertexFormatElement.Usage.GENERIC, 2);
ENTITY_ID_ELEMENT = new VertexFormatElement(11, VertexFormatElement.Type.USHORT, VertexFormatElement.Usage.UV, 3);
MID_TEXTURE_ELEMENT = new VertexFormatElement(12, VertexFormatElement.Type.FLOAT, VertexFormatElement.Usage.GENERIC, 2);
MID_TEXTURE_ELEMENT = new VertexFormatElement(12, VertexFormatElement.Type.USHORT, VertexFormatElement.Usage.GENERIC, 2);
TANGENT_ELEMENT = new VertexFormatElement(13, VertexFormatElement.Type.BYTE, VertexFormatElement.Usage.GENERIC, 4);
MID_BLOCK_ELEMENT = new VertexFormatElement(14, VertexFormatElement.Type.BYTE, VertexFormatElement.Usage.GENERIC, 3);
PADDING_SHORT = new VertexFormatElement(1, VertexFormatElement.Type.SHORT, VertexFormatElement.Usage.PADDING, 1);
VELOCITY_FLOAT = new VertexFormatElement(14, VertexFormatElement.Type.FLOAT, VertexFormatElement.Usage.GENERIC, 3);

ImmutableMap.Builder<String, VertexFormatElement> terrainElements = ImmutableMap.builder();
ImmutableMap.Builder<String, VertexFormatElement> entityElements = ImmutableMap.builder();
Expand All @@ -37,10 +39,10 @@ public class IrisVertexFormats {
terrainElements.put("Normal", DefaultVertexFormat.ELEMENT_NORMAL); // 31
terrainElements.put("Padding", DefaultVertexFormat.ELEMENT_PADDING); // 32
terrainElements.put("mc_Entity", ENTITY_ELEMENT); // 36
terrainElements.put("mc_midTexCoord", MID_TEXTURE_ELEMENT); // 44
terrainElements.put("at_tangent", TANGENT_ELEMENT); // 48
terrainElements.put("at_midBlock", MID_BLOCK_ELEMENT); // 51
terrainElements.put("Padding2", DefaultVertexFormat.ELEMENT_PADDING); // 52
terrainElements.put("mc_midTexCoord", MID_TEXTURE_ELEMENT); // 40
terrainElements.put("at_tangent", TANGENT_ELEMENT); // 44
terrainElements.put("at_midBlock", MID_BLOCK_ELEMENT); // 47
terrainElements.put("Padding2", DefaultVertexFormat.ELEMENT_PADDING); // 48

entityElements.put("Position", DefaultVertexFormat.ELEMENT_POSITION); // 12
entityElements.put("Color", DefaultVertexFormat.ELEMENT_COLOR); // 16
Expand All @@ -50,9 +52,9 @@ public class IrisVertexFormats {
entityElements.put("Normal", DefaultVertexFormat.ELEMENT_NORMAL); // 35
entityElements.put("Padding", DefaultVertexFormat.ELEMENT_PADDING); // 36
entityElements.put("iris_Entity", ENTITY_ID_ELEMENT); // 40
entityElements.put("mc_midTexCoord", MID_TEXTURE_ELEMENT); // 48
entityElements.put("at_tangent", TANGENT_ELEMENT); // 52
entityElements.put("Padding2", PADDING_SHORT); // 52
entityElements.put("mc_midTexCoord", MID_TEXTURE_ELEMENT); // 44
entityElements.put("at_tangent", TANGENT_ELEMENT); // 48
entityElements.put("at_velocity", VELOCITY_FLOAT); // 60

cloudsElements.put("Position", DefaultVertexFormat.ELEMENT_POSITION); // 12
cloudsElements.put("Color", DefaultVertexFormat.ELEMENT_COLOR); // 16
Expand All @@ -62,6 +64,8 @@ public class IrisVertexFormats {
TERRAIN = new VertexFormat(terrainElements.build());
ENTITY = new VertexFormat(entityElements.build());
CLOUDS = new VertexFormat(cloudsElements.build());

debug(ENTITY);
}

private static void debug(VertexFormat format) {
Expand Down
Loading

0 comments on commit cf77ea1

Please sign in to comment.