Skip to content

Commit

Permalink
Merge branch '1.6.2-without-0.4.11' into 1.20-new-sodium
Browse files Browse the repository at this point in the history
# Conflicts:
#	src/main/java/net/coderbot/iris/vertices/NormalHelper.java
  • Loading branch information
IMS212 committed Aug 4, 2023
2 parents eab0389 + f94b6c6 commit 1557f95
Show file tree
Hide file tree
Showing 10 changed files with 192 additions and 57 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package net.coderbot.iris.mixin.entity_render_context;

import com.mojang.blaze3d.vertex.PoseStack;
import com.mojang.blaze3d.vertex.VertexConsumer;
import net.coderbot.iris.block_rendering.BlockRenderingSettings;
import net.coderbot.iris.shaderpack.materialmap.NamespacedId;
import net.coderbot.iris.uniforms.CapturedRenderingState;
Expand All @@ -9,13 +10,16 @@
import net.minecraft.client.renderer.entity.RenderLayerParent;
import net.minecraft.client.renderer.entity.layers.HumanoidArmorLayer;
import net.minecraft.client.renderer.entity.layers.RenderLayer;
import net.minecraft.client.renderer.texture.TextureAtlasSprite;
import net.minecraft.core.Registry;
import net.minecraft.core.registries.BuiltInRegistries;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.world.entity.EquipmentSlot;
import net.minecraft.world.entity.LivingEntity;
import net.minecraft.world.item.ArmorItem;
import net.minecraft.world.item.ArmorMaterial;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.item.armortrim.ArmorTrim;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Inject;
Expand All @@ -38,6 +42,23 @@ private void changeId(PoseStack pHumanoidArmorLayer0, MultiBufferSource pMultiBu
CapturedRenderingState.INSTANCE.setCurrentRenderedItem(BlockRenderingSettings.INSTANCE.getItemIds().applyAsInt(new NamespacedId(location.getNamespace(), location.getPath())));
}

private int backupValue = 0;

@Inject(method = "renderTrim", at = @At(value = "HEAD"), locals = LocalCapture.CAPTURE_FAILHARD)
private void changeTrimTemp(ArmorMaterial pHumanoidArmorLayer0, PoseStack pPoseStack1, MultiBufferSource pMultiBufferSource2, int pInt3, ArmorTrim pArmorTrim4, boolean pBoolean5, A pHumanoidModel6, boolean pBoolean7, float pFloat8, float pFloat9, float pFloat10, CallbackInfo ci) {
if (BlockRenderingSettings.INSTANCE.getItemIds() == null) return;

backupValue = CapturedRenderingState.INSTANCE.getCurrentRenderedItem();
CapturedRenderingState.INSTANCE.setCurrentRenderedItem(BlockRenderingSettings.INSTANCE.getItemIds().applyAsInt(new NamespacedId("minecraft", "trim_" + pArmorTrim4.material().value().assetName())));
}

@Inject(method = "renderTrim", at = @At(value = "TAIL"), locals = LocalCapture.CAPTURE_FAILHARD)
private void changeTrimTemp2(ArmorMaterial pHumanoidArmorLayer0, PoseStack pPoseStack1, MultiBufferSource pMultiBufferSource2, int pInt3, ArmorTrim pArmorTrim4, boolean pBoolean5, A pHumanoidModel6, boolean pBoolean7, float pFloat8, float pFloat9, float pFloat10, CallbackInfo ci, TextureAtlasSprite lvTextureAtlasSprite12, VertexConsumer lvVertexConsumer13) {
if (BlockRenderingSettings.INSTANCE.getItemIds() == null) return;
CapturedRenderingState.INSTANCE.setCurrentRenderedItem(backupValue);
backupValue = 0;
}

@Inject(method = "renderArmorPiece", at = @At(value = "TAIL"))
private void changeId2(CallbackInfo ci) {
CapturedRenderingState.INSTANCE.setCurrentRenderedItem(0);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import com.mojang.blaze3d.vertex.VertexFormatElement;
import net.coderbot.iris.block_rendering.BlockRenderingSettings;
import org.joml.Vector3f;
import net.coderbot.iris.vertices.NormI8;
import org.jetbrains.annotations.NotNull;
import net.coderbot.iris.uniforms.CapturedRenderingState;
import net.coderbot.iris.vertices.BlockSensitiveBufferBuilder;
Expand Down Expand Up @@ -245,16 +246,6 @@ private void fillExtendedData(int vertexAmount) {
midU /= vertexAmount;
midV /= vertexAmount;

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

int tangent = NormalHelper.computeTangent(normal.x, normal.y, normal.z, polygon);

int midUOffset;
int midVOffset;
int normalOffset;
Expand All @@ -271,11 +262,29 @@ private void fillExtendedData(int vertexAmount) {
tangentOffset = 6;
}

for (int vertex = 0; vertex < vertexAmount; vertex++) {
buffer.putFloat(nextElementByte - midUOffset - stride * vertex, midU);
buffer.putFloat(nextElementByte - midVOffset - stride * vertex, midV);
buffer.putInt(nextElementByte - normalOffset - stride * vertex, packedNormal);
buffer.putInt(nextElementByte - tangentOffset - stride * vertex, tangent);
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.

for (int vertex = 0; vertex < vertexAmount; vertex++) {
int packedNormal = buffer.getInt(nextElementByte - normalOffset - stride * vertex); // retrieve per-vertex normal

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.putInt(nextElementByte - tangentOffset - stride * vertex, tangent);
}
} else {
NormalHelper.computeFaceNormal(normal, polygon);
int packedNormal = NormI8.pack(normal.x, normal.y, normal.z, 0.0f);
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.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 @@ -82,7 +82,7 @@ private void vertex(float x, float y, float z, int color, float u, float v, int
float normalX = saveNormal.x;
float normalY = saveNormal.y;
float normalZ = saveNormal.z;
int normal = NormalHelper.packNormal(saveNormal, 0.0F);
int normal = NormI8.pack(normalX, normalY, normalZ, 0.0F);

int tangent = NormalHelper.computeTangent(normalX, normalY, normalZ, quad);

Expand Down
87 changes: 87 additions & 0 deletions src/main/java/net/coderbot/iris/vertices/NormI8.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
package net.coderbot.iris.vertices;

import net.minecraft.util.Mth;
import org.joml.Vector3f;

/**
* Provides some utilities for working with packed normal vectors. Each normal component provides 8 bits of
* precision in the range of [-1.0,1.0].
* Copied from Sodium, licensed under the LGPLv3. Modified to support a W component.
*
* | 32 | 24 | 16 | 8 |
* | 0000 0000 | 0110 1100 | 0110 1100 | 0110 1100 |
* | W | X | Y | Z |
*/
public class NormI8 {
private static final int X_COMPONENT_OFFSET = 0;
private static final int Y_COMPONENT_OFFSET = 8;
private static final int Z_COMPONENT_OFFSET = 16;
private static final int W_COMPONENT_OFFSET = 24;

/**
* The maximum value of a normal's vector component.
*/
private static final float COMPONENT_RANGE = 127.0f;

/**
* Constant value which can be multiplied with a floating-point vector component to get the normalized value. The
* multiplication is slightly faster than a floating point division, and this code is a hot path which justifies it.
*/
private static final float NORM = 1.0f / COMPONENT_RANGE;

public static int pack(Vector3f normal) {
return pack(normal.x(), normal.y(), normal.z(), 0);
}

/**
* Packs the specified vector components into a 32-bit integer in XYZ ordering with the 8 bits of padding at the
* end.
* @param x The x component of the normal's vector
* @param y The y component of the normal's vector
* @param z The z component of the normal's vector
*/
public static int pack(float x, float y, float z, float w) {
return ((int) (x * 127) & 0xFF) | (((int) (y * 127) & 0xFF) << 8) | (((int) (z * 127) & 0xFF) << 16) | (((int) (w * 127) & 0xFF) << 24);
}

/**
* Encodes a float in the range of -1.0..1.0 to a normalized unsigned integer in the range of 0..255 which can then
* be passed to graphics memory.
*/
private static int encode(float comp) {
// TODO: is the clamp necessary here? our inputs should always be normalized vector components
return ((int) (Mth.clamp(comp, -1.0F, 1.0F) * COMPONENT_RANGE) & 255);
}

/**
* Unpacks the x-component of the packed normal, denormalizing it to a float in the range of -1.0..1.0.
* @param norm The packed normal
*/
public static float unpackX(int norm) {
return ((byte) ((norm >> X_COMPONENT_OFFSET) & 0xFF)) * NORM;
}

/**
* Unpacks the y-component of the packed normal, denormalizing it to a float in the range of -1.0..1.0.
* @param norm The packed normal
*/
public static float unpackY(int norm) {
return ((byte) ((norm >> Y_COMPONENT_OFFSET) & 0xFF)) * NORM;
}

/**
* Unpacks the z-component of the packed normal, denormalizing it to a float in the range of -1.0..1.0.
* @param norm The packed normal
*/
public static float unpackZ(int norm) {
return ((byte) ((norm >> Z_COMPONENT_OFFSET) & 0xFF)) * NORM;
}

/**
* Unpacks the w-component of the packed normal, denormalizing it to a float in the range of -1.0..1.0.
* @param norm The packed normal
*/
public static float unpackW(int norm) {
return ((byte) ((norm >> W_COMPONENT_OFFSET) & 0xFF)) * NORM;
}
}
83 changes: 48 additions & 35 deletions src/main/java/net/coderbot/iris/vertices/NormalHelper.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,20 +23,6 @@
public abstract class NormalHelper {
private NormalHelper() { }

/**
* Stores a normal plus an extra value as a quartet of signed bytes.
* This is the same normal format that vanilla item rendering expects.
* The extra value is for use by shaders.
*/
public static int packNormal(float x, float y, float z, float w) {
x = Mth.clamp(x, -1, 1);
y = Mth.clamp(y, -1, 1);
z = Mth.clamp(z, -1, 1);
w = Mth.clamp(w, -1, 1);

return ((int) (x * 127) & 0xFF) | (((int) (y * 127) & 0xFF) << 8) | (((int) (z * 127) & 0xFF) << 16) | (((int) (w * 127) & 0xFF) << 24);
}

public static int invertPackedNormal(int packed) {
int ix = -(packed & 255);
int iy = -((packed >> 8) & 255);
Expand All @@ -49,22 +35,6 @@ public static int invertPackedNormal(int packed) {
return (packed & 0xFF000000) | (iz << 16) | (iy << 8) | ix;
}

/**
* Version of {@link #packNormal(float, float, float, float)} that accepts a vector type.
*/
public static int packNormal(Vector3f normal, float w) {
return packNormal(normal.x, normal.y, normal.z, w);
}

/**
* Retrieves values packed by {@link #packNormal(float, float, float, float)}.
*
* <p>Components are x, y, z, w - zero based.
*/
public static float getPackedNormalComponent(int packedNormal, int component) {
return ((byte) (packedNormal >> (8 * component))) / 127f;
}

/**
* Computes the face normal of the given quad and saves it in the provided non-null vector.
*
Expand Down Expand Up @@ -214,9 +184,43 @@ public static void computeFaceNormalTri(@NotNull Vector3f saveTo, TriView t) {
saveTo.set(normX, normY, normZ);
}

public static int computeTangent(float normalX, float normalY, float normalZ, float x0, float y0, float z0, float u0, float v0,
float x1, float y1, float z1, float u1, float v1,
float x2, float y2, float z2, float u2, float v2) {
public static int computeTangentSmooth(float normalX, float normalY, float normalZ, TriView t) {
// Capture all of the relevant vertex positions
float x0 = t.x(0);
float y0 = t.y(0);
float z0 = t.z(0);

float x1 = t.x(1);
float y1 = t.y(1);
float z1 = t.z(1);

float x2 = t.x(2);
float y2 = t.y(2);
float z2 = t.z(2);

// Project all vertices onto normal plane (for smooth normal support). Optionally skip this step for flat shading.
// Procedure:
// project v onto normal
// offset v by the projection to get the point on the plane
// project x0, y0, z0 onto normal
float d0 = x0 * normalX + y0 * normalY + z0 * normalZ;
float d1 = x1 * normalX + y1 * normalY + z1 * normalZ;
float d2 = x2 * normalX + y2 * normalY + z2 * normalZ;

// offset x, y, z by the projection to get the projected point on the normal plane
x0 -= d0 * normalX;
y0 -= d0 * normalY;
z0 -= d0 * normalZ;

x1 -= d1 * normalX;
y1 -= d1 * normalY;
z1 -= d1 * normalZ;

x2 -= d2 * normalX;
y2 -= d2 * normalY;
z2 -= d2 * normalZ;


float edge1x = x1 - x0;
float edge1y = y1 - y0;
float edge1z = z1 - z0;
Expand All @@ -225,6 +229,15 @@ public static int computeTangent(float normalX, float normalY, float normalZ, fl
float edge2y = y2 - y0;
float edge2z = z2 - z0;

float u0 = t.u(0);
float v0 = t.v(0);

float u1 = t.u(1);
float v1 = t.v(1);

float u2 = t.u(2);
float v2 = t.v(2);

float deltaU1 = u1 - u0;
float deltaV1 = v1 - v0;
float deltaU2 = u2 - u0;
Expand Down Expand Up @@ -278,7 +291,7 @@ public static int computeTangent(float normalX, float normalY, float normalZ, fl
tangentW = 1.0F;
}

return packNormal(tangentx, tangenty, tangentz, tangentW);
return NormI8.pack(tangentx, tangenty, tangentz, tangentW);
}

public static int computeTangent(float normalX, float normalY, float normalZ, TriView t) {
Expand Down Expand Up @@ -365,7 +378,7 @@ public static int computeTangent(float normalX, float normalY, float normalZ, Tr
tangentW = 1.0F;
}

return packNormal(tangentx, tangenty, tangentz, tangentW);
return NormI8.pack(tangentx, tangenty, tangentz, tangentW);
}

private static float rsqrt(float value) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import net.caffeinemc.mods.sodium.api.vertex.format.VertexFormatDescription;
import net.caffeinemc.mods.sodium.api.vertex.format.VertexFormatRegistry;
import net.coderbot.iris.vertices.IrisVertexFormats;
import net.coderbot.iris.vertices.NormI8;
import net.coderbot.iris.vertices.NormalHelper;
import org.joml.Matrix4f;
import org.joml.Vector3f;
Expand Down Expand Up @@ -35,7 +36,7 @@ public static void write(long ptr, Matrix4f matrix, float x, float y, float z, i
quad.setup(ptr, STRIDE);

NormalHelper.computeFaceNormal(saveNormal, quad);
int normal = NormalHelper.packNormal(saveNormal, 0.0F);
int normal = NormI8.pack(saveNormal);

for (long vertex = 0; vertex < 4; vertex++) {
MemoryUtil.memPutInt(ptr + 16L - STRIDE * vertex, normal);
Expand All @@ -56,7 +57,7 @@ public static void write(long ptr, float x, float y, float z, int color) {
quad.setup(ptr, STRIDE);

NormalHelper.computeFaceNormal(saveNormal, quad);
int normal = NormalHelper.packNormal(saveNormal, 0.0F);
int normal = NormI8.pack(saveNormal);

for (long vertex = 0; vertex < 4; vertex++) {
MemoryUtil.memPutInt(ptr + 16L - STRIDE * vertex, normal);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import net.caffeinemc.mods.sodium.api.vertex.format.VertexFormatRegistry;
import net.coderbot.iris.uniforms.CapturedRenderingState;
import net.coderbot.iris.vertices.IrisVertexFormats;
import net.coderbot.iris.vertices.NormI8;
import net.coderbot.iris.vertices.NormalHelper;
import net.minecraft.client.renderer.texture.OverlayTexture;
import org.joml.Matrix3f;
Expand Down Expand Up @@ -82,7 +83,7 @@ private static void endQuad(long ptr) {
normalX = saveNormal.x;
normalY = saveNormal.y;
normalZ = saveNormal.z;
int normal = NormalHelper.packNormal(saveNormal, 0.0F);
int normal = NormI8.pack(saveNormal);

int tangent = NormalHelper.computeTangent(normalX, normalY, normalZ, quad);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import me.jellysquid.mods.sodium.client.render.chunk.vertex.format.ChunkVertexEncoder;
import net.coderbot.iris.compat.sodium.impl.block_context.BlockContextHolder;
import net.coderbot.iris.compat.sodium.impl.block_context.ContextAwareVertexWriter;
import net.coderbot.iris.vertices.NormI8;
import org.joml.Vector3f;
import net.coderbot.iris.vertices.ExtendedDataHelper;
import net.coderbot.iris.vertices.NormalHelper;
Expand Down Expand Up @@ -142,7 +143,7 @@ public long write(long ptr,
} else {
NormalHelper.computeFaceNormal(normal, quad);
}
int packedNormal = NormalHelper.packNormal(normal, 0.0f);
int packedNormal = NormI8.pack(normal);



Expand Down
Loading

0 comments on commit 1557f95

Please sign in to comment.