Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Optimized Render code for TextBuffers #2865

Open
wants to merge 2 commits into
base: master-MC1.7.10
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -16,64 +16,22 @@ import net.minecraft.tileentity.TileEntity
import org.lwjgl.opengl.GL11

object TextBufferRenderCache extends Callable[Int] with RemovalListener[TileEntity, Int] {
val renderer =
if (Settings.get.fontRenderer == "texture") new font.StaticFontRenderer()
else new font.DynamicFontRenderer()

final val renderer = new font.TextureFontRenderer()

private val cache = com.google.common.cache.CacheBuilder.newBuilder().
expireAfterAccess(2, TimeUnit.SECONDS).
removalListener(this).
asInstanceOf[CacheBuilder[TextBufferRenderData, Int]].
build[TextBufferRenderData, Int]()

// To allow access in cache entry init.
private var currentBuffer: TextBufferRenderData = _

// ----------------------------------------------------------------------- //
// Rendering
// ----------------------------------------------------------------------- //

def render(buffer: TextBufferRenderData) {
currentBuffer = buffer
compileOrDraw(cache.get(currentBuffer, this))
}

private def compileOrDraw(list: Int) = {
if (currentBuffer.dirty) {
RenderState.checkError(getClass.getName + ".compileOrDraw: entering (aka: wasntme)")

for (line <- currentBuffer.data.buffer) {
renderer.generateChars(line)
}

val doCompile = !RenderState.compilingDisplayList
if (doCompile) {
currentBuffer.dirty = false
GL11.glNewList(list, GL11.GL_COMPILE_AND_EXECUTE)

RenderState.checkError(getClass.getName + ".compileOrDraw: glNewList")
}

renderer.drawBuffer(currentBuffer.data, currentBuffer.viewport._1, currentBuffer.viewport._2)

RenderState.checkError(getClass.getName + ".compileOrDraw: drawString")

if (doCompile) {
GL11.glEndList()

RenderState.checkError(getClass.getName + ".compileOrDraw: glEndList")

}

RenderState.checkError(getClass.getName + ".compileOrDraw: leaving")

true
}
else {
GL11.glCallList(list)

RenderState.checkError(getClass.getName + ".compileOrDraw: glCallList")
}
var reRender = cache.getIfPresent(buffer) == null
renderer.drawBuffer(buffer.data, buffer.viewport._1, buffer.viewport._2, cache.get(buffer, this), reRender)
}

// ----------------------------------------------------------------------- //
Expand All @@ -82,19 +40,19 @@ object TextBufferRenderCache extends Callable[Int] with RemovalListener[TileEnti

def call = {
RenderState.checkError(getClass.getName + ".call: entering (aka: wasntme)")

val list = GLAllocation.generateDisplayLists(1)
currentBuffer.dirty = true // Force compilation.

var texID = font.TextureFontRenderer.createTexture

RenderState.checkError(getClass.getName + ".call: leaving")

list
texID
}


def onRemoval(e: RemovalNotification[TileEntity, Int]) {
RenderState.checkError(getClass.getName + ".onRemoval: entering (aka: wasntme)")

GLAllocation.deleteDisplayLists(e.getValue)
GL11.glDeleteTextures(e.getValue)

RenderState.checkError(getClass.getName + ".onRemoval: leaving")
}
Expand Down

This file was deleted.

33 changes: 33 additions & 0 deletions src/main/scala/li/cil/oc/client/renderer/font/FontParserHex.java
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ public ByteBuffer getGlyph(int charCode) {
if (charCode < 0 || charCode >= glyphs.length || glyphs[charCode] == null || glyphs[charCode].length == 0)
return null;
final byte[] glyph = glyphs[charCode];

final ByteBuffer buffer = BufferUtils.createByteBuffer(glyph.length * getGlyphWidth() * 4);
for (byte aGlyph : glyph) {
int c = ((int) aGlyph) & 0xFF;
Expand All @@ -84,6 +85,38 @@ public ByteBuffer getGlyph(int charCode) {
buffer.rewind();
return buffer;
}

@Override
public ByteBuffer getGlyph(int charCode, int color, int bg, ByteBuffer buffer, int stride) {
if (charCode < 0 || charCode >= glyphs.length || glyphs[charCode] == null || glyphs[charCode].length == 0)
return null;

final byte[] cdata = {(byte) (((color & 0xFF0000) >> 16)), (byte) (((color & 0x00FF00) >> 8)), (byte) (((color & 0x0000FF) >> 0)), (byte) 255};
final byte[] bdata = {(byte) (((bg & 0xFF0000) >> 16)), (byte) (((bg & 0x00FF00) >> 8)), (byte) (((bg & 0x0000FF) >> 0)), (byte) 255};
final byte[] glyph = glyphs[charCode];
boolean first = true;
int width = FontUtils.wcwidth(charCode);
for (int i = 0; i < glyph.length; i+=width) {
if (first) {
first = false;
} else {
buffer.position(buffer.position() + stride * 4);
}

for (int k = 0; k < width; k++) {
byte aGlyph = glyph[i + k];
int c = ((int) aGlyph) & 0xFF;
// Grab all bits by grabbing the leftmost one then shifting.
for (int j = 0; j < 8; j++) {
final boolean isBitSet = (c & 0x80) > 0;
if (isBitSet) buffer.put(cdata);
else buffer.put(bdata);
c <<= 1;
}
}
}
return null;
}

@Override
public int getGlyphWidth() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

import java.nio.ByteBuffer;

import li.cil.oc.util.PackedColor;

/**
* Common interface for classes providing glyph data in a format that can be
* rendered using the {@link li.cil.oc.client.renderer.font.DynamicFontRenderer}.
Expand Down Expand Up @@ -33,6 +35,8 @@ public interface IGlyphProvider {
* @see FontParserHex#getGlyph(int) See the hexfont parser for a reference implementation.
*/
ByteBuffer getGlyph(int charCode);

public ByteBuffer getGlyph(int charCode, int color, int bg, ByteBuffer buffer, int stride);

/**
* Get the single-width glyph width for this provider, in pixels.
Expand Down

This file was deleted.

Loading