Skip to content

Commit

Permalink
Add API sourceset and frustum changes
Browse files Browse the repository at this point in the history
  • Loading branch information
IMS212 committed Oct 19, 2024
1 parent 9b36b1a commit 8bf40a6
Show file tree
Hide file tree
Showing 14 changed files with 129 additions and 60 deletions.
13 changes: 13 additions & 0 deletions common/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ dependencies {
sourceSets {
val main = getByName("main")
val headers = create("headers")
val api = create("api")
val vendored = create("vendored")
val desktop = getByName("desktop")

Expand All @@ -84,6 +85,12 @@ sourceSets {
}
}

api.apply {
java {
compileClasspath += main.compileClasspath
}
}

desktop.apply {
java {
srcDir("src/desktop/java")
Expand All @@ -93,7 +100,9 @@ sourceSets {
main.apply {
java {
compileClasspath += headers.output
compileClasspath += api.output
compileClasspath += vendored.output
runtimeClasspath += api.output
runtimeClasspath += vendored.output
}
}
Expand Down Expand Up @@ -128,6 +137,10 @@ tasks {
from(vendored.output.classesDirs)
from(vendored.output.resourcesDir)

val api = sourceSets.getByName("api")
from(api.output.classesDirs)
from(api.output.resourcesDir)

val desktop = sourceSets.getByName("desktop")
from(desktop.output.classesDirs)
from(desktop.output.resourcesDir)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
package net.irisshaders.iris.api.v0;

import net.irisshaders.iris.apiimpl.IrisApiV0Impl;

import java.nio.ByteBuffer;
import java.util.function.IntFunction;

Expand All @@ -16,7 +14,7 @@ public interface IrisApi {
* @since API v0.0
*/
static IrisApi getInstance() {
return IrisApiV0Impl.INSTANCE;
return IrisApiInternal.INSTANCE;
}

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package net.irisshaders.iris.api.v0;

import org.jetbrains.annotations.ApiStatus;

@ApiStatus.Internal
public class IrisApiInternal {
static final IrisApi INSTANCE;

static {
try {
INSTANCE = (IrisApi) Class.forName("net.irisshaders.iris.apiimpl.IrisApiV0Impl").getField("INSTANCE").get(null);
} catch (IllegalAccessException | NoSuchFieldException | ClassNotFoundException e) {
throw new RuntimeException(e);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,7 @@ private void injectMidBlock(float x, float y, float z, CallbackInfoReturnable<Ve
this.elementsToFill = this.elementsToFill & ~IrisVertexFormats.TANGENT_ELEMENT.mask();

if (injectNormalAndUV1 && this.elementsToFill != (this.elementsToFill & ~VertexFormatElement.NORMAL.mask())) {
this.setNormal(0, 0, 0);
this.setNormal(0, 1, 0);
}

if (skipEndVertexOnce) {
Expand Down Expand Up @@ -238,16 +238,23 @@ private void fillExtendedData(int vertexAmount) {
MemoryUtil.memPutInt(newPointer + tangentOffset, tangent);
}
} else {
// TODO: Temporary fix for EMI item batching
boolean recalculateNormal = ImmediateState.isRenderingLevel;
NormalHelper.computeFaceNormal(normal, polygon);
int packedNormal = NormI8.pack(normal.x, normal.y, normal.z, 0.0f);
int packedNormal = 0;
if (recalculateNormal) {
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++) {
long newPointer = ((MojangBufferAccessor) buffer).getPointer() + vertexOffsets[vertex];

MemoryUtil.memPutFloat(newPointer + midTexOffset, midU);
MemoryUtil.memPutFloat(newPointer + midTexOffset + 4, midV);
MemoryUtil.memPutInt(newPointer + normalOffset, packedNormal);
if (recalculateNormal) {
MemoryUtil.memPutInt(newPointer + normalOffset, packedNormal);
}
MemoryUtil.memPutInt(newPointer + tangentOffset, tangent);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -335,12 +335,13 @@ private FrustumHolder createShadowFrustum(float renderMultiplier, FrustumHolder

shadowLightVectorFromOrigin.normalize();

Matrix4f projView = ((shouldRenderDH && DHCompat.hasRenderingEnabled()) ? DHCompat.getProjection() : CapturedRenderingState.INSTANCE.getGbufferProjection())
.mul(CapturedRenderingState.INSTANCE.getGbufferModelView(), new Matrix4f());

if (isReversed) {
return holder.setInfo(new ReversedAdvancedShadowCullingFrustum(CapturedRenderingState.INSTANCE.getGbufferModelView(),
(shouldRenderDH && DHCompat.hasRenderingEnabled()) ? DHCompat.getProjection() : CapturedRenderingState.INSTANCE.getGbufferProjection(), shadowLightVectorFromOrigin, boxCuller, new BoxCuller(halfPlaneLength * renderMultiplier)), distanceInfo, cullingInfo);
return holder.setInfo(new ReversedAdvancedShadowCullingFrustum(projView, PROJECTION, shadowLightVectorFromOrigin, boxCuller, new BoxCuller(halfPlaneLength * renderMultiplier)), distanceInfo, cullingInfo);
} else {
return holder.setInfo(new AdvancedShadowCullingFrustum(CapturedRenderingState.INSTANCE.getGbufferModelView(),
(shouldRenderDH && DHCompat.hasRenderingEnabled()) ? DHCompat.getProjection() : CapturedRenderingState.INSTANCE.getGbufferProjection(), shadowLightVectorFromOrigin, boxCuller), distanceInfo, cullingInfo);
return holder.setInfo(new AdvancedShadowCullingFrustum(projView, PROJECTION, shadowLightVectorFromOrigin, boxCuller), distanceInfo, cullingInfo);
}
}

Expand Down Expand Up @@ -382,6 +383,19 @@ public void renderShadows(LevelRendererAccessor levelRenderer, Camera playerCame
PoseStack modelView = createShadowModelView(this.sunPathRotation, this.intervalSize);
MODELVIEW = new Matrix4f(modelView.last().pose());

// Set up our orthographic projection matrix and load it into RenderSystem
Matrix4f shadowProjection;
if (this.fov != null) {
// If FOV is not null, the pack wants a perspective based projection matrix. (This is to support legacy packs)
shadowProjection = ShadowMatrices.createPerspectiveMatrix(this.fov);
} else {
shadowProjection = ShadowMatrices.createOrthoMatrix(halfPlaneLength, nearPlane < 0 ? -DHCompat.getRenderDistance() : nearPlane, farPlane < 0 ? DHCompat.getRenderDistance() : farPlane);
}

IrisRenderSystem.setShadowProjection(shadowProjection);

PROJECTION = shadowProjection;

levelRenderer.getLevel().getProfiler().push("terrain_setup");

if (levelRenderer instanceof CullingDataCache) {
Expand Down Expand Up @@ -433,20 +447,6 @@ public void renderShadows(LevelRendererAccessor levelRenderer, Camera playerCame

levelRenderer.getLevel().getProfiler().popPush("terrain");


// Set up our orthographic projection matrix and load it into RenderSystem
Matrix4f shadowProjection;
if (this.fov != null) {
// If FOV is not null, the pack wants a perspective based projection matrix. (This is to support legacy packs)
shadowProjection = ShadowMatrices.createPerspectiveMatrix(this.fov);
} else {
shadowProjection = ShadowMatrices.createOrthoMatrix(halfPlaneLength, nearPlane < 0 ? -DHCompat.getRenderDistance() : nearPlane, farPlane < 0 ? DHCompat.getRenderDistance() : farPlane);
}

IrisRenderSystem.setShadowProjection(shadowProjection);

PROJECTION = shadowProjection;

// Disable backface culling
// This partially works around an issue where if the front face of a mountain isn't visible, it casts no
// shadow.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ public class AdvancedShadowCullingFrustum extends Frustum implements net.caffein
* </ul>
* </p>
*/
private final Vector4f[] planes = new Vector4f[MAX_CLIPPING_PLANES];
private final float[][] planes = new float[MAX_CLIPPING_PLANES][4];
private final Vector3f shadowLightVectorFromOrigin;
private final Vector3d position = new Vector3d();
// The center coordinates of this frustum.
Expand All @@ -74,21 +74,28 @@ public class AdvancedShadowCullingFrustum extends Frustum implements net.caffein
public double z;
private int planeCount = 0;

public AdvancedShadowCullingFrustum(Matrix4fc playerView, Matrix4fc playerProjection, Vector3f shadowLightVectorFromOrigin,
public AdvancedShadowCullingFrustum(Matrix4fc modelViewProjection, Matrix4fc shadowProjection, Vector3f shadowLightVectorFromOrigin,
BoxCuller boxCuller) {
// We're overriding all of the methods, don't pass any matrices down.
super(new org.joml.Matrix4f(), new org.joml.Matrix4f());


/*
testing code, please ignore
System.out.println(shadowProjection.toString(NumberFormat.getNumberInstance()));
System.out.println(modelViewProjection.toString(NumberFormat.getNumberInstance()));
*/

this.shadowLightVectorFromOrigin = shadowLightVectorFromOrigin;
BaseClippingPlanes baseClippingPlanes = new BaseClippingPlanes(playerView, playerProjection);
BaseClippingPlanes baseClippingPlanes = new BaseClippingPlanes(modelViewProjection);

boolean[] isBack = addBackPlanes(baseClippingPlanes);
addEdgePlanes(baseClippingPlanes, isBack);

this.boxCuller = boxCuller;
}

private void addPlane(Vector4f plane) {
private void addPlane(float[] plane) {
planes[planeCount] = plane;
planeCount += 1;
}
Expand Down Expand Up @@ -121,7 +128,7 @@ private boolean[] addBackPlanes(BaseClippingPlanes baseClippingPlanes) {
isBack[planeIndex] = back;

if (back || edge) {
addPlane(plane);
addPlane(new float[] { plane.x, plane.y, plane.z, plane.w });
}
}

Expand Down Expand Up @@ -267,7 +274,7 @@ private void addEdgePlane(Vector4f backPlane4, Vector4f frontPlane4) {
}
}*/

addPlane(plane);
addPlane(new float[] { plane.x, plane.y, plane.z, plane.w });
}

// Note: These functions are copied & modified from the vanilla Frustum class.
Expand Down Expand Up @@ -347,29 +354,39 @@ private static float safeFMA(float a, float b, float c) {
* @return 0 if nothing is visible, 1 if everything is visible, 2 if only some corners are visible.
*/
protected int checkCornerVisibility(float minX, float minY, float minZ, float maxX, float maxY, float maxZ) {
boolean inside = true;

for (int i = 0; i < planeCount; ++i) {
Vector4f plane = this.planes[i];
float[] plane = this.planes[i];

// Check if plane is inside or intersecting.
// This is ported from JOML's FrustumIntersection.

float outsideBoundX = (plane.x < 0) ? minX : maxX;
float outsideBoundY = (plane.y < 0) ? minY : maxY;
float outsideBoundZ = (plane.z < 0) ? minZ : maxZ;
float outsideBoundX = (plane[0] < 0) ? minX : maxX;
float outsideBoundY = (plane[1] < 0) ? minY : maxY;
float outsideBoundZ = (plane[2] < 0) ? minZ : maxZ;

// Use Math.fma for the dot product calculation to get vectorization (sorry old Intel users)
if (FMA_SUPPORT) {
if (Math.fma(plane.x, outsideBoundX, Math.fma(plane.y, outsideBoundY, plane.z * outsideBoundZ)) < -plane.w) {
if (Math.fma(plane[0], outsideBoundX, Math.fma(plane[1], outsideBoundY, plane[2] * outsideBoundZ)) >= -plane[3]) {
inside &= Math.fma(plane[0], (plane[0] < 0 ? maxX : minX),
Math.fma(plane[1], (plane[1] < 0 ? maxY : minY),
Math.fma(plane[2], (plane[2] < 0 ? maxZ : minZ), plane[3]))) >= 0;
} else {
return 0;
}
} else {
if (safeFMA(plane.x, outsideBoundX, safeFMA(plane.y, outsideBoundY, plane.z * outsideBoundZ)) < -plane.w) {
if (safeFMA(plane[0], outsideBoundX, safeFMA(plane[1], outsideBoundY, plane[2] * outsideBoundZ)) >= -plane[3]) {
inside &= safeFMA(plane[0], (plane[0] < 0 ? maxX : minX),
safeFMA(plane[1], (plane[1] < 0 ? maxY : minY),
safeFMA(plane[2], (plane[2] < 0 ? maxZ : minZ), plane[3]))) >= 0;
} else {
return 0;
}
}
}

return 2;
return inside ? 1 : 2;
}

/**
Expand All @@ -385,7 +402,13 @@ protected int checkCornerVisibility(float minX, float minY, float minZ, float ma
*/
public boolean checkCornerVisibilityBool(float minX, float minY, float minZ, float maxX, float maxY, float maxZ) {
for (int i = 0; i < planeCount; ++i) {
if (planes[i].x * (planes[i].x < 0 ? minX : maxX) + planes[i].y * (planes[i].y < 0 ? minY : maxY) + planes[i].z * (planes[i].z < 0 ? minZ : maxZ) < -planes[i].w) {
float[] plane = planes[i];

float outsideBoundX = (plane[0] < 0) ? minX : maxX;
float outsideBoundY = (plane[1] < 0) ? minY : maxY;
float outsideBoundZ = (plane[2] < 0) ? minZ : maxZ;

if (Math.fma(plane[0], outsideBoundX, Math.fma(plane[1], outsideBoundY, plane[2] * outsideBoundZ)) < -plane[3]) {
return false;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@
public class BaseClippingPlanes {
private final Vector4f[] planes = new Vector4f[6];

public BaseClippingPlanes(Matrix4fc playerView, Matrix4fc playerProjection) {
this.init(playerView, playerProjection);
public BaseClippingPlanes(Matrix4fc modelViewProjection) {
this.init(modelViewProjection);
}

private static Vector4f transform(Matrix4fc transform, float x, float y, float z) {
Expand All @@ -19,11 +19,10 @@ private static Vector4f transform(Matrix4fc transform, float x, float y, float z
return vector4f;
}

private void init(Matrix4fc view, Matrix4fc projection) {
private void init(Matrix4fc modelViewProjection) {
// Transform = Transpose(Projection x View)

Matrix4f transform = new Matrix4f(projection);
transform.mul(view);
Matrix4f transform = new Matrix4f(modelViewProjection);
transform.transpose();

planes[0] = transform(transform, -1, 0, 0);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@
public class ReversedAdvancedShadowCullingFrustum extends AdvancedShadowCullingFrustum implements Frustum {
private final BoxCuller distanceCuller;

public ReversedAdvancedShadowCullingFrustum(Matrix4fc playerView, Matrix4fc playerProjection, Vector3f shadowLightVectorFromOrigin, BoxCuller voxelCuller, BoxCuller distanceCuller) {
super(playerView, playerProjection, shadowLightVectorFromOrigin, voxelCuller);
public ReversedAdvancedShadowCullingFrustum(Matrix4fc modelViewProjection, Matrix4fc shadowProjection, Vector3f shadowLightVectorFromOrigin, BoxCuller voxelCuller, BoxCuller distanceCuller) {
super(modelViewProjection, shadowProjection, shadowLightVectorFromOrigin, voxelCuller);
this.distanceCuller = distanceCuller;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -387,6 +387,12 @@ public static int computeTangent(float normalX, float normalY, float normalZ, Tr
return NormI8.pack(tangentx, tangenty, tangentz, tangentW);
}

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) {
return computeTangent(null, normalX, normalY, normalZ, x0, y0, z0, u0, v0, x1, y1, z1, u1, v1, x2, y2, z2, u2, v2);
}

public static int computeTangent(Vector4f output, 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) {
Expand Down
1 change: 1 addition & 0 deletions fabric/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ dependencies {
implementAndInclude("org.anarres:jcpp:1.4.14")

implementation(project.project(":common").sourceSets.getByName("vendored").output)
implementation(project.project(":common").sourceSets.getByName("api").output)
compileOnly(project.project(":common").sourceSets.getByName("headers").output)
implementation(project.project(":common").sourceSets.getByName("main").output)

Expand Down
Loading

0 comments on commit 8bf40a6

Please sign in to comment.