Skip to content

Commit eae8a29

Browse files
committed
Hodgepodge compat
#191
1 parent 44fd8e9 commit eae8a29

5 files changed

Lines changed: 158 additions & 15 deletions

File tree

LICENSE

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,4 +82,7 @@ with this program; if not, write to the Free Software Foundation, Inc.,
8282
--------------------------------
8383
Some parts of the threading codebase use logic adapted from Angelica (GTNH Team), which is also
8484
licensed under the GNU Lesser General Public License, Version 3.
85+
--------------------------------
86+
The mipmapfix memoized exponent logic has been ported from HodgePodge (GTNH Team), which is also
87+
licensed under the GNU Lesser General Public License, Version 3.
8588
--------------------------------

src/main/java/com/falsepattern/falsetweaks/mixin/mixins/client/bsp/TessellatorBSPSortingMixin.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,8 @@
3333
import net.minecraft.client.renderer.Tessellator;
3434
import net.minecraft.client.shader.TesselatorVertexState;
3535

36-
@Mixin(Tessellator.class)
36+
@Mixin(value = Tessellator.class,
37+
priority = 900) // Hodgepodge conflict
3738
public abstract class TessellatorBSPSortingMixin implements IBSPTessellator {
3839
/**
3940
* @author FalsePattern

src/main/java/com/falsepattern/falsetweaks/mixin/mixins/client/mipmapfix/TextureUtilMixin.java

Lines changed: 125 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -23,32 +23,143 @@
2323

2424
package com.falsepattern.falsetweaks.mixin.mixins.client.mipmapfix;
2525

26+
import com.falsepattern.falsetweaks.modules.mipmapfix.Mipmaps;
2627
import org.objectweb.asm.Opcodes;
2728
import org.spongepowered.asm.mixin.Mixin;
29+
import org.spongepowered.asm.mixin.Overwrite;
2830
import org.spongepowered.asm.mixin.injection.At;
2931
import org.spongepowered.asm.mixin.injection.Inject;
3032
import org.spongepowered.asm.mixin.injection.Redirect;
3133
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;
3234

3335
import net.minecraft.client.renderer.texture.TextureUtil;
3436

35-
@Mixin(TextureUtil.class)
37+
// Ported from Hodgepodge
38+
@Mixin(value = TextureUtil.class,
39+
priority = 1100) // Hodgepodge conflict
3640
public abstract class TextureUtilMixin {
37-
private static ThreadLocal<int[]> threadLocalBuffer;
41+
/**
42+
* @author UltraHex
43+
* @reason Check entire texture for transparency.
44+
* @reason Don't divide by zero for small textures.
45+
*/
46+
@Overwrite
47+
public static int[][] generateMipmapData(int levels, int size, int[][] texture) {
48+
int[][] mipmaps = new int[levels + 1][];
49+
mipmaps[0] = texture[0];
3850

39-
@Inject(method = "<clinit>",
40-
at = @At("RETURN"),
41-
require = 1)
42-
private static void setupThreadLocal(CallbackInfo ci) {
43-
threadLocalBuffer = ThreadLocal.withInitial(() -> new int[4]);
51+
if (levels > 0) {
52+
boolean transparent = false;
53+
54+
for (int l = 0; l < texture[0].length; ++l) {
55+
if (texture[0][l] >> 24 == 0) {
56+
transparent = true;
57+
break;
58+
}
59+
}
60+
61+
for (int level = 1; level <= levels; ++level) {
62+
if (texture[level] != null) {
63+
mipmaps[level] = texture[level];
64+
} else {
65+
int[] prevLevel = mipmaps[level - 1];
66+
67+
int width = size >> level;
68+
if (width <= 0) {
69+
mipmaps[level] = prevLevel;
70+
continue;
71+
}
72+
73+
int[] mipmap = new int[prevLevel.length >> 2];
74+
75+
int height = mipmap.length / width;
76+
int prevWidth = width << 1;
77+
78+
for (int x = 0; x < width; ++x) {
79+
for (int y = 0; y < height; ++y) {
80+
int prevPos = 2 * (x + y * prevWidth);
81+
mipmap[x + y * width] = func_147943_a(
82+
prevLevel[prevPos],
83+
prevLevel[prevPos + 1],
84+
prevLevel[prevPos + prevWidth],
85+
prevLevel[prevPos + 1 + prevWidth],
86+
transparent);
87+
}
88+
}
89+
90+
mipmaps[level] = mipmap;
91+
}
92+
}
93+
}
94+
95+
return mipmaps;
4496
}
4597

46-
@Redirect(method = "func_147943_a",
47-
at = @At(value = "FIELD",
48-
target = "Lnet/minecraft/client/renderer/texture/TextureUtil;field_147957_g:[I",
49-
opcode = Opcodes.GETSTATIC),
50-
require = 9)
51-
private static int[] swapReferencesWithThreadLocal() {
52-
return threadLocalBuffer.get();
98+
/**
99+
* @author SuperCoder79
100+
* @reason Rewrite mipmap color math to use memoized value array instead of using Math.pow directly
101+
*/
102+
@Overwrite
103+
private static int func_147943_a(int one, int two, int three, int four, boolean alpha) {
104+
if (!alpha) {
105+
int a = Mipmaps.getColorComponent(one, two, three, four, 24);
106+
int r = Mipmaps.getColorComponent(one, two, three, four, 16);
107+
int g = Mipmaps.getColorComponent(one, two, three, four, 8);
108+
int b = Mipmaps.getColorComponent(one, two, three, four, 0);
109+
return a << 24 | r << 16 | g << 8 | b;
110+
} else {
111+
int n = 0;
112+
113+
float a = 0.0F;
114+
float r = 0.0F;
115+
float g = 0.0F;
116+
float b = 0.0F;
117+
if (one >> 24 != 0) {
118+
a += Mipmaps.get(one >> 24);
119+
r += Mipmaps.get(one >> 16);
120+
g += Mipmaps.get(one >> 8);
121+
b += Mipmaps.get(one);
122+
n++;
123+
}
124+
125+
if (two >> 24 != 0) {
126+
a += Mipmaps.get(two >> 24);
127+
r += Mipmaps.get(two >> 16);
128+
g += Mipmaps.get(two >> 8);
129+
b += Mipmaps.get(two);
130+
n++;
131+
}
132+
133+
if (three >> 24 != 0) {
134+
a += Mipmaps.get(three >> 24);
135+
r += Mipmaps.get(three >> 16);
136+
g += Mipmaps.get(three >> 8);
137+
b += Mipmaps.get(three);
138+
n++;
139+
}
140+
141+
if (four >> 24 != 0) {
142+
a += Mipmaps.get(four >> 24);
143+
r += Mipmaps.get(four >> 16);
144+
g += Mipmaps.get(four >> 8);
145+
b += Mipmaps.get(four);
146+
n++;
147+
}
148+
149+
a /= 4.0F;
150+
151+
if (n != 0) {
152+
r /= n;
153+
g /= n;
154+
b /= n;
155+
}
156+
157+
int ia = (int) (Math.pow(a, 0.45454545454545453) * 255.0);
158+
int ir = (int) (Math.pow(r, 0.45454545454545453) * 255.0);
159+
int ig = (int) (Math.pow(g, 0.45454545454545453) * 255.0);
160+
int ib = (int) (Math.pow(b, 0.45454545454545453) * 255.0);
161+
162+
return ia << 24 | ir << 16 | ig << 8 | ib;
163+
}
53164
}
54165
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package com.falsepattern.falsetweaks.modules.mipmapfix;
2+
3+
public class Mipmaps {
4+
5+
private static final float[] VALS = new float[256];
6+
7+
public static float get(int i) {
8+
return VALS[i & 0xFF];
9+
}
10+
11+
public static int getColorComponent(int one, int two, int three, int four, int bits) {
12+
float f = Mipmaps.get(one >> bits);
13+
float g = Mipmaps.get(two >> bits);
14+
float h = Mipmaps.get(three >> bits);
15+
float i = Mipmaps.get(four >> bits);
16+
float j = (float) Math.pow((f + g + h + i) * 0.25, 0.45454545454545453);
17+
return (int) (j * 255.0);
18+
}
19+
20+
static {
21+
for (int i = 0; i < VALS.length; ++i) {
22+
VALS[i] = (float) Math.pow((float) i / 255.0F, 2.2);
23+
}
24+
}
25+
}

src/main/resources/LICENSE

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,4 +82,7 @@ with this program; if not, write to the Free Software Foundation, Inc.,
8282
--------------------------------
8383
Some parts of the threading codebase use logic adapted from Angelica (GTNH Team), which is also
8484
licensed under the GNU Lesser General Public License, Version 3.
85+
--------------------------------
86+
The mipmapfix memoized exponent logic has been ported from HodgePodge (GTNH Team), which is also
87+
licensed under the GNU Lesser General Public License, Version 3.
8588
--------------------------------

0 commit comments

Comments
 (0)