Convert ResourceLocation -> int texture

This commit is contained in:
sisyphussy committed 2026-05-24 07:33:17 +02:00
1 parent 9a5ba2a17c
commit a32e034aa5
9 files changed
+106 -88

No files matched your search

@@ -26,7 +26,6 @@ import org.lwjgl.opengl.GL15;
import org.lwjgl.opengl.GL20;
import java.nio.ByteBuffer;
import java.util.Comparator;
import java.util.Objects;
import static com.gtnewhorizon.gtnhlib.bytebuf.MemoryUtilities.memAddress0;
@@ -61,13 +60,15 @@ public final class BatchingFontRenderer {
/** Location of the primary font atlas to bind. */
protected final ResourceLocation locationFontTexture;
private final int AAStrength;
private final int mvpMatrixLocation;
private static int AAStrength;
private static int mvpMatrixLocation;
private static int fontShaderId;
final boolean isSGA;
final boolean isSplash;
private final int fontTexture;
/** For use with modded books. Affects calculations and forces some defaults. */
@Setter
boolean bookMode = false;
@@ -86,13 +87,15 @@ public final class BatchingFontRenderer {
this.isSplash = FontStrategist.isSplashFontRendererActive(underlying);
FontProviderMC.get(this.isSGA).charWidth = this.charWidth;
FontProviderMC.get(this.isSGA).locationFontTexture = this.locationFontTexture;
((FontRendererAccessor) underlying).angelica$bindTexture(this.locationFontTexture);
this.fontTexture = GLStateManager.getBoundTextureForServerState();
final String vsh = ShaderProgram.loadShaderSource(
BatchingFontRenderer.class.getResourceAsStream("/assets/angelica/shaders/font/fontFilter.vsh")
BatchingFontRenderer.class.getResourceAsStream("/assets/angelica/shaders/font/font.vsh")
);
final String fsh = ShaderProgram.loadShaderSource(
BatchingFontRenderer.class.getResourceAsStream("/assets/angelica/shaders/font/fontFilter.fsh"),
BatchingFontRenderer.class.getResourceAsStream("/assets/angelica/shaders/font/font.fsh"),
new SimpleShaderDefine("AA_MODE", FontConfig.fontAAMode)
);
@@ -109,8 +112,6 @@ public final class BatchingFontRenderer {
// === Batched rendering
private static final int INITIAL_BATCH_SIZE = 2048;
private static final ResourceLocation DUMMY_RESOURCE_LOCATION = new ResourceLocation("angelica$dummy",
"this is invalid!");
// Layout in data:
// [v, v, t, t, c, c, c, c, tb, tb, tb, tb]
@@ -232,7 +233,7 @@ public final class BatchingFontRenderer {
private void pushShaderCmd(
int startIdx,
int idxCount,
ResourceLocation texture,
int texture,
float x, float y,
float width, float height
) {
@@ -258,7 +259,7 @@ public final class BatchingFontRenderer {
batchCommands.add(cmd);
}
private void pushDrawCmd(int startIdx, int idxCount, ResourceLocation texture, boolean isUnicode) {
private void pushDrawCmd(int startIdx, int idxCount, int texture, boolean isUnicode) {
if (!batchCommands.isEmpty()) {
final FontDrawCmd lastCmd = batchCommands.get(batchCommands.size() - 1);
final int prevEndVtx = lastCmd.startVtx + lastCmd.idxCount;
@@ -282,9 +283,9 @@ public final class BatchingFontRenderer {
public int startVtx;
public int idxCount;
public ResourceLocation texture;
public int texture;
public void reset(int startVtx, int vtxCount, ResourceLocation texture, boolean isShader) {
public void reset(int startVtx, int vtxCount, int texture, boolean isShader) {
this.startVtx = startVtx;
this.idxCount = vtxCount;
this.texture = texture;
@@ -453,9 +454,9 @@ public final class BatchingFontRenderer {
private int fontAAStrengthLast = -1;
private static final Comparator<FontDrawCmd> DRAW_ORDER_COMPARATOR = Comparator.comparing((FontDrawCmd fdc) -> fdc.texture,
Comparator.nullsLast(Comparator.comparing(ResourceLocation::getResourceDomain)
.thenComparing(ResourceLocation::getResourcePath))).thenComparing(fdc -> fdc.startVtx);
// private static final Comparator<FontDrawCmd> DRAW_ORDER_COMPARATOR = Comparator.comparing((FontDrawCmd fdc) -> fdc.texture,
// Comparator.nullsLast(Comparator.comparing(ResourceLocation::getResourceDomain)
// .thenComparing(ResourceLocation::getResourcePath))).thenComparing(fdc -> fdc.startVtx);
private void flushBatch() {
if (vertexDataPos == 0) {
@@ -471,14 +472,12 @@ public final class BatchingFontRenderer {
final int prevProgram = GLStateManager.glGetInteger(GL20.GL_CURRENT_PROGRAM);
// Sort&Draw
batchCommands.sort(DRAW_ORDER_COMPARATOR);
//batchCommands.sort(DRAW_ORDER_COMPARATOR);
final boolean isTextureEnabledBefore = GLStateManager.glIsEnabled(GL11.GL_TEXTURE_2D);
final boolean isBlendEnabledBefore = GLStateManager.glIsEnabled(GL11.GL_BLEND);
final int boundTextureBefore = GLStateManager.glGetInteger(GL11.GL_TEXTURE_BINDING_2D);
boolean textureChanged = false;
ResourceLocation lastTexture = DUMMY_RESOURCE_LOCATION;
GLStateManager.enableTexture();
GLStateManager.enableAlphaTest();
GLStateManager.enableBlend();
@@ -519,20 +518,22 @@ public final class BatchingFontRenderer {
GLStateManager.glBindVertexArray(fontVAO);
int lastTexture = -1;
// Use plain for loop to avoid allocations
final FontDrawCmd[] cmdsData = batchCommands.elements();
final int cmdsSize = batchCommands.size();
for (int i = 0; i < cmdsSize; i++) {
final FontDrawCmd cmd = cmdsData[i];
if (!Objects.equals(lastTexture, cmd.texture)) {
if (lastTexture == null) {
if (lastTexture <= 0) {
GLStateManager.glEnable(GL11.GL_TEXTURE_2D);
} else if (cmd.texture == null) {
} else if (cmd.texture > 0) {
GLStateManager.glDisable(GL11.GL_TEXTURE_2D);
}
if (cmd.texture != null) {
((FontRendererAccessor) underlying).angelica$bindTexture(cmd.texture);
if (cmd.texture > 0) {
GLStateManager.glBindTexture(GL11.GL_TEXTURE_2D, cmd.texture);
textureChanged = true;
}
lastTexture = cmd.texture;
@@ -658,7 +659,6 @@ public final class BatchingFontRenderer {
final int shadowColor = (color & 0xfcfcfc) >> 2 | color & 0xff000000;
FontProviderMC.get(this.isSGA).charWidth = this.charWidth;
FontProviderMC.get(this.isSGA).locationFontTexture = this.locationFontTexture;
this.beginBatch();
float curX = anchorX;
@@ -711,7 +711,7 @@ public final class BatchingFontRenderer {
if (curUnderline && underlineStartX != underlineEndX) {
final int ulIdx = idxWriterIndex;
pushUntexRect(underlineStartX, underlineY, underlineEndX - underlineStartX, glyphScaleY, curColor);
pushDrawCmd(ulIdx, 6, null, false);
pushDrawCmd(ulIdx, 6, 0, false);
underlineStartX = underlineEndX;
}
if (curStrikethrough && strikethroughStartX != strikethroughEndX) {
@@ -722,7 +722,7 @@ public final class BatchingFontRenderer {
strikethroughEndX - strikethroughStartX,
glyphScaleY,
curColor);
pushDrawCmd(ulIdx, 6, null, false);
pushDrawCmd(ulIdx, 6, 0, false);
strikethroughStartX = strikethroughEndX;
}
@@ -866,7 +866,13 @@ public final class BatchingFontRenderer {
final float shadowOffset = fontProvider.getShadowOffset();
final int shadowCopies = FontConfig.shadowCopies;
final int boldCopies = FontConfig.boldCopies;
final ResourceLocation texture = fontProvider.getTexture(chr);
final int texture;
if (fontProvider instanceof FontProviderMC) {
texture = this.fontTexture;
} else {
texture = fontProvider.getTexture(chr);
}
final int idxId = idxWriterIndex;
// Wave: Y offset via sine wave
@@ -947,13 +953,13 @@ public final class BatchingFontRenderer {
if ((curRainbow || curGradient) && curUnderline && underlineStartX != underlineEndX) {
final int ulIdx = idxWriterIndex;
pushUntexRect(underlineStartX, underlineY, underlineEndX - underlineStartX, glyphScaleY, curColor);
pushDrawCmd(ulIdx, 6, null, false);
pushDrawCmd(ulIdx, 6, 0, false);
underlineStartX = underlineEndX;
}
if ((curRainbow || curGradient) && curStrikethrough && strikethroughStartX != strikethroughEndX) {
final int stIdx = idxWriterIndex;
pushUntexRect(strikethroughStartX, strikethroughY, strikethroughEndX - strikethroughStartX, glyphScaleY, curColor);
pushDrawCmd(stIdx, 6, null, false);
pushDrawCmd(stIdx, 6, 0, false);
strikethroughStartX = strikethroughEndX;
}
}
@@ -961,7 +967,7 @@ public final class BatchingFontRenderer {
if (curUnderline && underlineStartX != underlineEndX) {
final int ulIdx = idxWriterIndex;
pushUntexRect(underlineStartX, underlineY, underlineEndX - underlineStartX, glyphScaleY, curColor);
pushDrawCmd(ulIdx, 6, null, false);
pushDrawCmd(ulIdx, 6, 0, false);
}
if (curStrikethrough && strikethroughStartX != strikethroughEndX) {
final int ulIdx = idxWriterIndex;
@@ -971,7 +977,7 @@ public final class BatchingFontRenderer {
strikethroughEndX - strikethroughStartX,
glyphScaleY,
curColor);
pushDrawCmd(ulIdx, 6, null, false);
pushDrawCmd(ulIdx, 6, 0, false);
}
} finally {
@@ -36,11 +36,11 @@ public final class FontOverlayShader extends ShaderProgram {
new IShaderReloadRunnable() {
@Override
public void run(ShaderProgram shader) {
shader.bindTextureSlots("textFBO", "sceneFBO");
mvpMatrixLocation = shader.getUniformLocation("u_MVPMatrix");
uTexelSize = shader.getUniformLocation("uTexelSize");
uTime = shader.getUniformLocation("uTime");
uTexBounds = shader.getUniformLocation("uTexBounds");
shader.bindTextureSlots("textFBO", "sceneFBO");
}
@@ -1,5 +1,9 @@
package com.gtnewhorizons.angelica.client.font;
import net.minecraft.client.Minecraft;
import net.minecraft.client.renderer.texture.ITextureObject;
import net.minecraft.client.renderer.texture.SimpleTexture;
import net.minecraft.client.renderer.texture.TextureManager;
import net.minecraft.util.ResourceLocation;
public interface FontProvider {
@@ -16,6 +20,17 @@ public interface FontProvider {
float getUSize(char chr);
float getVSize(char chr);
float getShadowOffset();
ResourceLocation getTexture(char chr);
int getTexture(char chr);
float getYScaleMultiplier();
default int getTextureFromLocation(ResourceLocation resource) {
final TextureManager textureManager = Minecraft.getMinecraft().getTextureManager();
if (textureManager == null) return 0;
ITextureObject texture = textureManager.getTexture(resource);
if (texture == null) {
texture = new SimpleTexture(resource);
textureManager.loadTexture(resource, texture);
}
return texture.getGlTextureId();
}
}
@@ -1,6 +1,7 @@
package com.gtnewhorizons.angelica.client.font;
import com.gtnewhorizons.angelica.config.FontConfig;
import com.gtnewhorizons.angelica.glsm.GLStateManager;
import lombok.Setter;
import lombok.Value;
import net.minecraft.client.Minecraft;
@@ -11,16 +12,10 @@ import net.minecraft.util.ResourceLocation;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import javax.imageio.ImageIO;
import java.awt.Font;
import java.awt.FontMetrics;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.Map;
import java.util.Objects;
@@ -125,7 +120,7 @@ public final class FontProviderCustom implements FontProvider {
private class FontAtlas {
GlyphData[] glyphData = new GlyphData[ATLAS_SIZE];
private ResourceLocation texture;
private int texture;
private final int id;
FontAtlas(int id) {
@@ -212,13 +207,14 @@ public final class FontProviderCustom implements FontProvider {
tileX++;
}
g2d.dispose();
try {
Files.createDirectories(Paths.get(getFontDir()));
ImageIO.write(image, "png", new File(getAtlasFullPath(this.id)));
} catch (IOException e) {
e.printStackTrace();
}
this.texture = new ResourceLocation(getAtlasResourceName(this.id));
// try {
// Files.createDirectories(Paths.get(getFontDir()));
// ImageIO.write(image, "png", new File(getAtlasFullPath(this.id)));
// } catch (IOException e) {
// e.printStackTrace();
// }
this.texture = TextureUtil.uploadTextureImageAllocate(GLStateManager.glGenTextures(), image, false, false);
//this.texture = new ResourceLocation(getAtlasResourceName(this.id));
}
}
@@ -280,7 +276,7 @@ public final class FontProviderCustom implements FontProvider {
}
@Override
public ResourceLocation getTexture(char chr) {
public int getTexture(char chr) {
return getAtlas(chr).texture;
}
@@ -3,7 +3,6 @@ package com.gtnewhorizons.angelica.client.font;
import com.gtnewhorizons.angelica.config.FontConfig;
import it.unimi.dsi.fastutil.chars.Char2ShortOpenHashMap;
import jss.util.RandomXoshiro256StarStar;
import net.minecraft.util.ResourceLocation;
public final class FontProviderMC implements FontProvider {
@@ -14,7 +13,7 @@ public final class FontProviderMC implements FontProvider {
private static final String MCFONT_CHARS = "\u00c0\u00c1\u00c2\u00c8\u00ca\u00cb\u00cd\u00d3\u00d4\u00d5\u00da\u00df\u00e3\u00f5\u011f\u0130\u0131\u0152\u0153\u015e\u015f\u0174\u0175\u017e\u0207\u0000\u0000\u0000\u0000\u0000\u0000\u0000 !\"#$%&\'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_`abcdefghijklmnopqrstuvwxyz{|}~\u2302\u00c7\u00fc\u00e9\u00e2\u00e4\u00e0\u00e5\u00e7\u00ea\u00eb\u00e8\u00ef\u00ee\u00ec\u00c4\u00c5\u00c9\u00e6\u00c6\u00f4\u00f6\u00f2\u00fb\u00f9\u00ff\u00d6\u00dc\u00f8\u00a3\u00d8\u00d7\u0192\u00e1\u00ed\u00f3\u00fa\u00f1\u00d1\u00aa\u00ba\u00bf\u00ae\u00ac\u00bd\u00bc\u00a1\u00ab\u00bb\u2591\u2592\u2593\u2502\u2524\u2561\u2562\u2556\u2555\u2563\u2551\u2557\u255d\u255c\u255b\u2510\u2514\u2534\u252c\u251c\u2500\u253c\u255e\u255f\u255a\u2554\u2569\u2566\u2560\u2550\u256c\u2567\u2568\u2564\u2565\u2559\u2558\u2552\u2553\u256b\u256a\u2518\u250c\u2588\u2584\u258c\u2590\u2580\u03b1\u03b2\u0393\u03c0\u03a3\u03c3\u03bc\u03c4\u03a6\u0398\u03a9\u03b4\u221e\u2205\u2208\u2229\u2261\u00b1\u2265\u2264\u2320\u2321\u00f7\u2248\u00b0\u2219\u00b7\u221a\u207f\u00b2\u25a0\u0000";
private FontProviderMC() {}
private static class InstLoader {
private static final class InstLoader {
static final FontProviderMC instance = new FontProviderMC();
static final FontProviderMC instanceSGA = new FontProviderMC();
}
@@ -23,12 +22,11 @@ public final class FontProviderMC implements FontProvider {
}
private static final Char2ShortOpenHashMap MCFONT_ASCII_MAP = new Char2ShortOpenHashMap();
ResourceLocation locationFontTexture = null;
private final RandomXoshiro256StarStar fontRandom = new RandomXoshiro256StarStar();
private static final RandomXoshiro256StarStar fontRandom = new RandomXoshiro256StarStar();
public int[] charWidth;
static {
MCFONT_ASCII_MAP.defaultReturnValue((short) -1);
MCFONT_ASCII_MAP.defaultReturnValue((short) 0);
for (short i = 0; i < MCFONT_CHARS.length(); i++) {
MCFONT_ASCII_MAP.put(MCFONT_CHARS.charAt(i), i);
}
@@ -43,13 +41,13 @@ public final class FontProviderMC implements FontProvider {
return MCFONT_ASCII_MAP.containsKey(chr);
}
public char getRandomReplacement(char chr){
public char getRandomReplacement(char chr) {
int lutIndex = lookupMcFontPosition(chr);
if (lutIndex != -1) {
int randomReplacementIndex;
do {
randomReplacementIndex = fontRandom.nextInt(this.charWidth.length);
} while (this.charWidth[lutIndex] != this.charWidth[randomReplacementIndex]);
randomReplacementIndex = fontRandom.nextInt(charWidth.length);
} while (charWidth[lutIndex] != charWidth[randomReplacementIndex]);
lutIndex = randomReplacementIndex;
return MCFONT_CHARS.charAt(lutIndex);
@@ -98,8 +96,10 @@ public final class FontProviderMC implements FontProvider {
}
@Override
public ResourceLocation getTexture(char chr) {
return locationFontTexture;
public int getTexture(char chr) {
throw new UnsupportedOperationException(
"Cannot call getTexture() in FontProviderMC - Implementation is dependant on the FontRenderer!"
);
}
@Override
@@ -24,17 +24,18 @@ public final class FontProviderUnicode implements FontProvider {
private static class InstLoader { static final FontProviderUnicode instance = new FontProviderUnicode(); }
public static FontProviderUnicode get() { return FontProviderUnicode.InstLoader.instance; }
private static final ResourceLocation[] unicodePageLocations = new ResourceLocation[256];
private static final int[] unicodePageLocations = new int[256];
private final byte[] glyphWidth = new byte[65536];
private ResourceLocation getUnicodePageLocation(int page) {
final ResourceLocation lookup = unicodePageLocations[page];
if (lookup == null) {
private int getUnicodePageTexture(int page) {
final int lookup = unicodePageLocations[page];
if (lookup == 0) {
final ResourceLocation rl = new ResourceLocation(String.format(
"textures/font/unicode_page_%02x.png",
page));
unicodePageLocations[page] = rl;
return rl;
final int texture = getTextureFromLocation(rl);
unicodePageLocations[page] = texture;
return texture;
} else {
return lookup;
}
@@ -101,9 +102,9 @@ public final class FontProviderUnicode implements FontProvider {
}
@Override
public ResourceLocation getTexture(char chr) {
public int getTexture(char chr) {
final int uniPage = chr / 256;
return getUnicodePageLocation(uniPage);
return getUnicodePageTexture(uniPage);
}
@Override
@@ -2,12 +2,12 @@
// ----- GLOW -----
//#define USE_GLOW
#define USE_GLOW
//#define RGB_GLOW
#define GLOW_COLOR vec3(0.992, 0.651, 0.227)
#define GLOW_COLOR vec3(0.882, 0.686, 1.0)
// ----- SPARKLES -----
//#define USE_SPARKLES
#define USE_SPARKLES
#define SPARKLE_COLOR vec3(1)
// ----- WAVE -----
@@ -20,6 +20,7 @@
// ----- SPOTLIGHT -----
#define USE_SPOTLIGHT
#define SPOTLIGHT_COLOR GLOW_COLOR
#define SPOTLIGHT_STRENGTH 0.333
@@ -99,12 +100,12 @@ float glow(vec2 uv) {
//dist += jitter;
//TODO
if (dist <= 0) return 1;
//if (dist <= 0) return 1;
//else return 0;
float timeGlow = (sin(uTime * 2.0)) * 0.2;
float d = dist;
d = d / (2 + timeGlow);
float glow = exp(-d);
//d = d / (2 + timeGlow);
float glow = exp(-d * 0.7);
//float d = (dist / (1 + (sin(uTime * 2.0)) * 0.1));
//return min(1.0 / pow(d, 1.2), 1);
return min(glow, 1);
@@ -117,7 +118,7 @@ float starfield(vec2 uv) {
float widthHeightRatio = resolution.x / resolution.y;
float t = uTime * 0.01;
float t = uTime * 0.015;
float dist = 0.0;
@@ -245,8 +246,7 @@ float spotlight(
1.0 / 4.0
);
// inverted radial falloff
return (1.0 - smoothstep(0.0, 1.0, dist)) * 0.4;
return (1.0 - smoothstep(0.0, 1.0, dist)) * SPOTLIGHT_STRENGTH;
}
//0xfda63a orang
@@ -256,33 +256,33 @@ float spotlight(
//0xe1afff
void main() {
float minX = uTexBounds.x - uTexelSize.x * 3;
float maxX = uTexBounds.y + uTexelSize.x * 3;
float minY = uTexBounds.z - uTexelSize.y * 3;
float maxY = uTexBounds.w + uTexelSize.y * 3;
float minX = uTexBounds.x;
float maxX = uTexBounds.y;
float minY = uTexBounds.z;
float maxY = uTexBounds.w;
vec2 relativeUV = vec2(texCoord.x - minX, texCoord.y - minY);
vec3 overlay = vec3(0);
#ifdef USE_SPOTLIGHT
overlay = SPOTLIGHT_COLOR * spotlight(texCoord, minX, maxX, minY, maxY);
#endif
#ifdef USE_SPARKLES
if (
texCoord.x >= minX &&
texCoord.x <= maxX &&
texCoord.y >= minY &&
texCoord.y <= maxY
texCoord.x >= minX - uTexelSize.x * 2 &&
texCoord.x <= maxX + uTexelSize.x * 2 &&
texCoord.y >= minY - uTexelSize.y * 4 &&
texCoord.y <= maxY + uTexelSize.y * 4
) {
float sparkle = starfield(vec2(texCoord.x - minX, texCoord.y - minY));
if (sparkle > 0.1) {
if (sparkle > 0.05) {
// glow sits in front of sparkle
overlay = mix(overlay, SPARKLE_COLOR, sparkle);
}
}
#endif
#ifdef USE_SPOTLIGHT
overlay = mix(overlay, SPOTLIGHT_COLOR, spotlight(texCoord, minX, maxX, minY, maxY));
#endif
#ifdef USE_GLOW
float glowVal = glow(texCoord);
if (glowVal > 0.1) {