Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

at_velocity and aggressive packing #2082

Closed
wants to merge 11 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions buildscript/src/main/java/Buildscript.java
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,7 @@ public void getModDependencies(ModDependencyCollector d) {
jij(d.addMaven(FabricMaven.URL, new MavenId(FabricMaven.GROUP_ID + ".fabric-api", "fabric-key-binding-api-v1", "1.0.23+aaaf9d332d"), ModDependencyFlag.COMPILE, ModDependencyFlag.RUNTIME));

jij(d.addMaven(Maven.MAVEN_CENTRAL, new MavenId("io.github.douira:glsl-transformer:2.0.0-pre13"), ModDependencyFlag.COMPILE, ModDependencyFlag.RUNTIME));
jij(d.addMaven(Maven.MAVEN_CENTRAL, new MavenId("net.jodah:expiringmap:0.5.10"), ModDependencyFlag.COMPILE, ModDependencyFlag.RUNTIME));
jij(d.addMaven(Maven.MAVEN_CENTRAL, new MavenId("org.antlr:antlr4-runtime:4.11.1"), ModDependencyFlag.COMPILE, ModDependencyFlag.RUNTIME));
d.addMaven("https://api.modrinth.com/maven", new MavenId("maven.modrinth", "distanthorizons", "2.0.0-a-1.19.4"), ModDependencyFlag.COMPILE);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,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");

GlStateManager._glBindAttribLocation(program, 0, "Position");
GlStateManager._glBindAttribLocation(program, 1, "UV0");
Expand Down
3 changes: 3 additions & 0 deletions src/main/java/net/coderbot/iris/mixin/MixinLevelRenderer.java
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,9 @@ public class MixinLevelRenderer {
private void iris$endLevelRender(PoseStack poseStack, float tickDelta, long limitTime, boolean renderBlockOutline, Camera camera, GameRenderer gameRenderer, LightTexture lightTexture, Matrix4f projectionMatrix, CallbackInfo callback) {
HandRenderer.INSTANCE.renderTranslucent(poseStack, tickDelta, camera, gameRenderer, pipeline);
Minecraft.getInstance().getProfiler().popPush("iris_final");
CapturedRenderingState.INSTANCE.velocityInfoEdit = new PoseStack();
CapturedRenderingState.INSTANCE.velocityInfoEdit.mulPose(Axis.XP.rotationDegrees(camera.getXRot()));
CapturedRenderingState.INSTANCE.velocityInfoEdit.mulPose(Axis.YP.rotationDegrees(camera.getYRot() + 180.0F));
pipeline.finalizeLevelRendering();
pipeline = null;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ public class MixinBlockEntityRenderDispatcher {
int intId = blockStateIds.getOrDefault(state, -1);

CapturedRenderingState.INSTANCE.setCurrentBlockEntity(intId);
CapturedRenderingState.INSTANCE.setUniqueEntityId(blockEntity.getBlockPos().getX() + blockEntity.getBlockPos().getY() + blockEntity.getType().hashCode());

return type ->
bufferSource.getBuffer(OuterWrappedRenderType.wrapExactlyOnce("iris:is_block_entity", type, BlockEntityRenderStateShard.INSTANCE));
Expand All @@ -67,5 +68,7 @@ public class MixinBlockEntityRenderDispatcher {
private void iris$afterRender(BlockEntity blockEntity, float tickDelta, PoseStack matrix,
MultiBufferSource bufferSource, CallbackInfo ci) {
CapturedRenderingState.INSTANCE.setCurrentBlockEntity(0);
CapturedRenderingState.INSTANCE.setUniqueEntityId(0);

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,15 @@
import net.minecraft.client.renderer.MultiBufferSource;
import net.minecraft.client.renderer.RenderStateShard;
import net.minecraft.client.renderer.entity.EntityRenderDispatcher;
import net.minecraft.client.renderer.entity.EntityRenderer;
import net.minecraft.core.Registry;
import net.minecraft.core.registries.BuiltInRegistries;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.world.entity.Entity;
import net.minecraft.world.entity.monster.ZombieVillager;
import net.minecraft.world.phys.Vec3;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.Shadow;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Inject;
import org.spongepowered.asm.mixin.injection.ModifyVariable;
Expand All @@ -31,18 +34,35 @@
* rendered.
*/
@Mixin(EntityRenderDispatcher.class)
public class MixinEntityRenderDispatcher {
public abstract class MixinEntityRenderDispatcher {
@Shadow
public abstract <T extends Entity> EntityRenderer<? super T> getRenderer(T pEntityRenderDispatcher0);

// Inject after MatrixStack#push since at this point we know that most cancellation checks have already passed.
@ModifyVariable(method = "render", at = @At(value = "INVOKE", target = "Lcom/mojang/blaze3d/vertex/PoseStack;pushPose()V", shift = At.Shift.AFTER),
allow = 1, require = 1)
private MultiBufferSource iris$beginEntityRender(MultiBufferSource bufferSource, Entity entity) {
private MultiBufferSource iris$beginEntityRender(MultiBufferSource bufferSource, Entity entity,
double pDouble1,
double pDouble2,
double pDouble3,
float pFloat4,
float pFloat5) {

Vec3 lvVec314 = this.getRenderer(entity).getRenderOffset(entity, pFloat5);
double lvDouble15 = pDouble1 + lvVec314.x();
double lvDouble17 = pDouble2 + lvVec314.y();
double lvDouble19 = pDouble3 + lvVec314.z();
CapturedRenderingState.INSTANCE.velocityInfoEdit.pushPose();
CapturedRenderingState.INSTANCE.velocityInfoEdit.translate(lvDouble15, lvDouble17, lvDouble19);

if (!(bufferSource instanceof Groupable)) {
// Fully batched entity rendering is not being used, do not use this wrapper!!!
return bufferSource;
}

Object2IntFunction<NamespacedId> entityIds = BlockRenderingSettings.INSTANCE.getEntityIds();


if (entityIds == null) {
return bufferSource;
}
Expand All @@ -57,6 +77,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,6 +90,8 @@ public class MixinEntityRenderDispatcher {
PoseStack poseStack, MultiBufferSource bufferSource, int light,
CallbackInfo ci) {
CapturedRenderingState.INSTANCE.setCurrentEntity(0);
CapturedRenderingState.INSTANCE.setUniqueEntityId(0);
CapturedRenderingState.INSTANCE.velocityInfoEdit.popPose();
CapturedRenderingState.INSTANCE.setCurrentRenderedItem(0);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package net.coderbot.iris.mixin.entity_render_context;

import com.mojang.blaze3d.vertex.PoseStack;
import net.coderbot.iris.uniforms.CapturedRenderingState;
import net.minecraft.client.renderer.MultiBufferSource;
import net.minecraft.client.renderer.entity.EntityRenderDispatcher;
import net.minecraft.client.renderer.entity.EntityRenderer;
import net.minecraft.network.chat.Component;
import net.minecraft.world.entity.Entity;
import org.spongepowered.asm.mixin.Final;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.Shadow;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Inject;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;

@Mixin(EntityRenderer.class)
public class MixinEntityRenderer {
@Shadow
@Final
protected EntityRenderDispatcher entityRenderDispatcher;

@Inject(method = "renderNameTag", at = @At(value = "INVOKE", target = "Lnet/minecraft/world/entity/Entity;getNameTagOffsetY()F"))
private void iris$editVelocityInfo(Entity pEntityRenderer0, Component pComponent1, PoseStack pPoseStack2, MultiBufferSource pMultiBufferSource3, int pInt4, CallbackInfo ci) {
CapturedRenderingState.INSTANCE.velocityInfoEdit.translate(0.0F, pEntityRenderer0.getNameTagOffsetY(), 0.0F);
CapturedRenderingState.INSTANCE.velocityInfoEdit.mulPose(this.entityRenderDispatcher.cameraOrientation());
CapturedRenderingState.INSTANCE.velocityInfoEdit.scale(-0.025F, -0.025F, 0.025F);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,9 @@ public abstract class MixinBufferBuilder extends DefaultedVertexConsumer impleme
@Unique
private boolean iris$isTerrain = false;

@Unique
private boolean iris$isParticle = false;

@Unique
private int vertexCount;

Expand Down Expand Up @@ -103,6 +106,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 All @@ -113,7 +118,7 @@ public abstract class MixinBufferBuilder extends DefaultedVertexConsumer impleme
@Inject(method = "begin", at = @At("HEAD"))
private void iris$onBegin(VertexFormat.Mode drawMode, VertexFormat format, CallbackInfo ci) {
boolean shouldExtend = (!iris$shouldNotExtend) && BlockRenderingSettings.INSTANCE.shouldUseExtendedVertexFormat();
extending = shouldExtend && (format == DefaultVertexFormat.BLOCK || format == DefaultVertexFormat.NEW_ENTITY
extending = shouldExtend && (format == DefaultVertexFormat.BLOCK || format == DefaultVertexFormat.NEW_ENTITY || format == DefaultVertexFormat.PARTICLE
|| format == DefaultVertexFormat.POSITION_COLOR_TEX_LIGHTMAP);
vertexCount = 0;

Expand All @@ -128,12 +133,20 @@ public abstract class MixinBufferBuilder extends DefaultedVertexConsumer impleme
if (format == DefaultVertexFormat.BLOCK) {
this.switchFormat(IrisVertexFormats.TERRAIN);
this.iris$isTerrain = true;
this.iris$isParticle = false;
} else if (format == DefaultVertexFormat.PARTICLE) {
this.switchFormat(IrisVertexFormats.PARTICLES);
this.iris$isTerrain = false;
this.iris$isParticle = true;
} else if (format == DefaultVertexFormat.POSITION_COLOR_TEX_LIGHTMAP) {
this.switchFormat(IrisVertexFormats.GLYPH);
this.iris$isTerrain = false;
injectNormalAndUV1 = true;
this.iris$isParticle = false;
} else {
this.switchFormat(IrisVertexFormats.ENTITY);
this.iris$isTerrain = false;
this.iris$isParticle = false;
}
this.currentElement = this.format.getElements().get(0);
}
Expand All @@ -151,12 +164,21 @@ public abstract class MixinBufferBuilder extends DefaultedVertexConsumer impleme
if (format == DefaultVertexFormat.BLOCK) {
this.switchFormat(IrisVertexFormats.TERRAIN);
this.iris$isTerrain = true;
this.iris$isParticle = false;
} else if (format == DefaultVertexFormat.PARTICLE) {
this.switchFormat(IrisVertexFormats.PARTICLES);
this.iris$isTerrain = false;
this.iris$isParticle = true;
} else if (format == DefaultVertexFormat.POSITION_COLOR_TEX_LIGHTMAP) {
this.switchFormat(IrisVertexFormats.GLYPH);
this.iris$isTerrain = false;
injectNormalAndUV1 = true;

this.iris$isParticle = false;
} else {
this.switchFormat(IrisVertexFormats.ENTITY);
this.iris$isTerrain = false;
this.iris$isParticle = false;
}
}
return arg;
Expand Down Expand Up @@ -187,6 +209,15 @@ public abstract class MixinBufferBuilder extends DefaultedVertexConsumer impleme
return;
}

if (iris$isParticle) {
this.putFloat(0, 0);
this.putFloat(4, 0);
this.putFloat(8, 0);
this.nextElement();
vertexCount++;
return;
}

if (injectNormalAndUV1 && currentElement == DefaultVertexFormat.ELEMENT_NORMAL) {
this.putInt(0, 0);
this.nextElement();
Expand All @@ -206,20 +237,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 @@ -229,6 +266,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 @@ -253,17 +295,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 @@ -272,8 +317,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 @@ -282,8 +327,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
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,10 @@ public class MixinVertexFormat {
if ((Object) this == DefaultVertexFormat.BLOCK) {
IrisVertexFormats.TERRAIN.setupBufferState();

ci.cancel();
} else if ((Object) this == DefaultVertexFormat.PARTICLE) {
IrisVertexFormats.PARTICLES.setupBufferState();

ci.cancel();
} else if ((Object) this == DefaultVertexFormat.POSITION_COLOR_TEX_LIGHTMAP) {
IrisVertexFormats.GLYPH.setupBufferState();
Expand All @@ -40,6 +44,10 @@ public class MixinVertexFormat {
if ((Object) this == DefaultVertexFormat.BLOCK) {
IrisVertexFormats.TERRAIN.clearBufferState();

ci.cancel();
} else if ((Object) this == DefaultVertexFormat.PARTICLE) {
IrisVertexFormats.PARTICLES.clearBufferState();

ci.cancel();
} else if ((Object) this == DefaultVertexFormat.POSITION_COLOR_TEX_LIGHTMAP) {
IrisVertexFormats.GLYPH.clearBufferState();
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
Loading
Loading