Skip to content

Commit e1d8330

Browse files
authored
Remove unnecessary multi-texturing to keep state management simple. (#258)
1 parent bae0c28 commit e1d8330

File tree

4 files changed

+24
-20
lines changed

4 files changed

+24
-20
lines changed

earlydisplay/src/main/java/net/neoforged/fml/earlydisplay/DisplayWindow.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -224,7 +224,7 @@ private void initRender(final @Nullable String mcVersion, final String forgeVers
224224
this.context = new RenderElement.DisplayContext(854, 480, fbScale, elementShader, colourScheme, performanceInfo);
225225
framebuffer = new EarlyFramebuffer(this.context);
226226
try {
227-
this.font = new SimpleFont("Monocraft.ttf", fbScale, 200000, 1 + RenderElement.INDEX_TEXTURE_OFFSET);
227+
this.font = new SimpleFont("Monocraft.ttf", fbScale, 200000);
228228
} catch (Throwable t) {
229229
LOGGER.error("Crash during font initialization", t);
230230
crashElegantly("An error occurred initializing a font for rendering. " + t.getMessage());

earlydisplay/src/main/java/net/neoforged/fml/earlydisplay/RenderElement.java

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515
import net.neoforged.fml.loading.progress.StartupNotificationManager;
1616

1717
public class RenderElement {
18-
static final int INDEX_TEXTURE_OFFSET = 5;
1918
private final SimpleBufferBuilder bb;
2019
private final Renderer renderer;
2120
static int globalAlpha = 255;
@@ -247,7 +246,8 @@ private static Initializer initializeText(SimpleFont font, TextGenerator textGen
247246
}
248247

249248
private static void renderText(final SimpleFont font, final TextGenerator textGenerator, final SimpleBufferBuilder bb, final DisplayContext context) {
250-
context.elementShader().updateTextureUniform(font.textureNumber());
249+
GlState.activeTexture(GL_TEXTURE0);
250+
GlState.bindTexture2D(font.textureId());
251251
context.elementShader().updateRenderTypeUniform(ElementShader.RenderType.FONT);
252252
bb.begin(SimpleBufferBuilder.Format.POS_TEX_COLOR, SimpleBufferBuilder.Mode.QUADS);
253253
textGenerator.accept(bb, font, context);
@@ -260,11 +260,12 @@ private static TextGenerator text(int x, int y, String text, int colour) {
260260

261261
private static Initializer initializeTexture(final String textureFileName, int size, int textureNumber, TextureRenderer positionAndColour) {
262262
return () -> {
263-
int[] imgSize = STBHelper.loadTextureFromClasspath(textureFileName, size, GL_TEXTURE0 + textureNumber + INDEX_TEXTURE_OFFSET);
263+
var texture = STBHelper.loadTextureFromClasspath(textureFileName, size);
264264
return (bb, ctx, frame) -> {
265-
ctx.elementShader().updateTextureUniform(textureNumber + INDEX_TEXTURE_OFFSET);
265+
GlState.activeTexture(GL_TEXTURE0);
266+
GlState.bindTexture2D(texture.textureId());
266267
ctx.elementShader().updateRenderTypeUniform(ElementShader.RenderType.TEXTURE);
267-
renderTexture(bb, ctx, frame, imgSize, positionAndColour);
268+
renderTexture(bb, ctx, frame, texture.size(), positionAndColour);
268269
};
269270
};
270271
}
@@ -279,15 +280,15 @@ public static float clamp(float num, float min, float max) {
279280
if (num < min) {
280281
return min;
281282
} else {
282-
return num > max ? max : num;
283+
return Math.min(num, max);
283284
}
284285
}
285286

286287
public static int clamp(int num, int min, int max) {
287288
if (num < min) {
288289
return min;
289290
} else {
290-
return num > max ? max : num;
291+
return Math.min(num, max);
291292
}
292293
}
293294

earlydisplay/src/main/java/net/neoforged/fml/earlydisplay/STBHelper.java

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,13 +39,13 @@ public static ByteBuffer readFromClasspath(final String name, int initialCapacit
3939
return MemoryUtil.memSlice(buf); // we trim the final buffer to the size of the content
4040
}
4141

42-
public static int[] loadTextureFromClasspath(String file, int size, int textureNumber) {
42+
public static ImageTexture loadTextureFromClasspath(String file, int size) {
4343
int[] lw = new int[1];
4444
int[] lh = new int[1];
4545
int[] lc = new int[1];
4646
var img = loadImageFromClasspath(file, size, lw, lh, lc);
4747
var texid = glGenTextures();
48-
GlState.activeTexture(textureNumber);
48+
GlState.activeTexture(GL_TEXTURE0);
4949
GlState.bindTexture2D(texid);
5050
GlDebug.labelTexture(texid, "EarlyDisplay " + file);
5151
// glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
@@ -55,9 +55,13 @@ public static int[] loadTextureFromClasspath(String file, int size, int textureN
5555
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, lw[0], lh[0], 0, GL_RGBA, GL_UNSIGNED_BYTE, img);
5656
GlState.activeTexture(GL_TEXTURE0);
5757
MemoryUtil.memFree(img);
58-
return new int[] { lw[0], lh[0] };
58+
return new ImageTexture(
59+
texid,
60+
new int[] { lw[0], lh[0] });
5961
}
6062

63+
public record ImageTexture(int textureId, int[] size) {}
64+
6165
public static ByteBuffer loadImageFromClasspath(String file, int size, int[] width, int[] height, int[] channels) {
6266
ByteBuffer buf = STBHelper.readFromClasspath(file, size);
6367
return STBImage.stbi_load_from_memory(buf, width, height, channels, 4);

earlydisplay/src/main/java/net/neoforged/fml/earlydisplay/SimpleFont.java

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
import org.lwjgl.stb.STBTTPackedchar;
2020

2121
public class SimpleFont {
22-
private final int textureNumber;
22+
private final int textureId;
2323
private final int lineSpacing;
2424
private final int descent;
2525
private final int GLYPH_COUNT = 127 - 32;
@@ -42,7 +42,7 @@ Pos loadQuad(Pos pos, int colour, SimpleBufferBuilder bb) {
4242
/**
4343
* Build the font and store it in the textureNumber location
4444
*/
45-
public SimpleFont(String fontName, int scale, int bufferSize, int textureNumber) {
45+
public SimpleFont(String fontName, int scale, int bufferSize) {
4646
ByteBuffer buf = STBHelper.readFromClasspath(fontName, bufferSize);
4747
var info = STBTTFontinfo.create();
4848
if (!stbtt_InitFont(info, buf)) {
@@ -56,10 +56,10 @@ public SimpleFont(String fontName, int scale, int bufferSize, int textureNumber)
5656
stbtt_GetScaledFontVMetrics(buf, 0, fontSize, ascent, descent, lineGap);
5757
this.lineSpacing = (int) (ascent[0] - descent[0] + lineGap[0]);
5858
this.descent = (int) Math.floor(descent[0]);
59-
int fontTextureId = glGenTextures();
60-
GlState.activeTexture(GL_TEXTURE0 + textureNumber);
61-
this.textureNumber = textureNumber;
62-
GlState.bindTexture2D(fontTextureId);
59+
this.textureId = glGenTextures();
60+
GlState.activeTexture(GL_TEXTURE0);
61+
GlState.bindTexture2D(this.textureId);
62+
GlDebug.labelTexture(this.textureId, "font texture " + fontName);
6363
try (var packedchars = STBTTPackedchar.malloc(GLYPH_COUNT)) {
6464
int texwidth = 256;
6565
int texheight = 128;
@@ -83,7 +83,6 @@ public SimpleFont(String fontName, int scale, int bufferSize, int textureNumber)
8383
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
8484
}
8585
}
86-
GlState.activeTexture(GL_TEXTURE0);
8786
try (var q = STBTTAlignedQuad.malloc()) {
8887
float[] x = new float[1];
8988
float[] y = new float[1];
@@ -103,8 +102,8 @@ int lineSpacing() {
103102
return lineSpacing;
104103
}
105104

106-
int textureNumber() {
107-
return textureNumber;
105+
int textureId() {
106+
return textureId;
108107
}
109108

110109
int descent() {

0 commit comments

Comments
 (0)