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

rewrite TextBuffer rendering logic, optimize, adopt VBOs #3138

Open
wants to merge 5 commits into
base: master-MC1.12
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
new renderer fixes
  • Loading branch information
asiekierka committed Aug 9, 2019
commit ea3656133c73931a90696b0aadb1d39113184a50
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,10 @@ interface Receiver {
int getCharWidth();
int getCharHeight();

boolean isDynamic();
int getTextureCount();
void begin(int texture);
void loadCodePoint(int codePoint);
void drawCodePoint(int codePoint, float tx, float ty, Receiver receiver);
void end(int texture);
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
package li.cil.oc.client.renderer.font

import java.nio.ByteBuffer

import it.unimi.dsi.fastutil.ints.Int2ObjectOpenHashMap
import li.cil.oc.Settings
import li.cil.oc.client.renderer.font.MultiDynamicFontTextureProvider.{CharIcon, CharTexture}
import li.cil.oc.client.renderer.font.MultiDynamicGlyphFontTextureProvider.{CharIcon, CharTexture}
import li.cil.oc.client.renderer.font.FontTextureProvider.Receiver
import li.cil.oc.util.FontUtils
import li.cil.oc.util.RenderState
Expand All @@ -16,14 +18,10 @@ import org.lwjgl.opengl._

import scala.collection.mutable

/**
* Font renderer that dynamically generates lookup textures by rendering a font
* to it. It's pretty broken right now, and font rendering looks crappy as hell.
*/
class MultiDynamicFontTextureProvider(private val glyphProvider: IGlyphProvider) extends FontTextureProvider with IResourceManagerReloadListener {
class MultiDynamicGlyphFontTextureProvider(private val glyphProvider: IGlyphProvider) extends FontTextureProvider with IResourceManagerReloadListener {
private val textures = mutable.ArrayBuffer.empty[CharTexture]

private val charMap = new Int2ObjectOpenHashMap[MultiDynamicFontTextureProvider.CharIcon]
private val charMap = new Int2ObjectOpenHashMap[MultiDynamicGlyphFontTextureProvider.CharIcon]

private var activeTexture: CharTexture = _

Expand All @@ -41,10 +39,10 @@ class MultiDynamicFontTextureProvider(private val glyphProvider: IGlyphProvider)

textures.clear()
charMap.clear()
textures += new MultiDynamicFontTextureProvider.CharTexture(this)
textures += new MultiDynamicGlyphFontTextureProvider.CharTexture(this)
activeTexture = textures.head

StaticFontTextureProvider.basicChars.foreach(c => getCodePoint(c))
StaticTextureFontTextureProvider.basicChars.foreach(c => getCodePoint(c))
}

def onResourceManagerReload(manager: IResourceManager) {
Expand Down Expand Up @@ -92,25 +90,29 @@ class MultiDynamicFontTextureProvider(private val glyphProvider: IGlyphProvider)
}
}

private def createCharIcon(char: Int): MultiDynamicFontTextureProvider.CharIcon = {
private def createCharIcon(char: Int): MultiDynamicGlyphFontTextureProvider.CharIcon = {
if (FontUtils.wcwidth(char) < 1 || glyphProvider.getGlyph(char) == null) {
if (char == '?') null
else getCodePoint('?')
}
else {
if (textures.last.isFull(char)) {
textures += new MultiDynamicFontTextureProvider.CharTexture(this)
textures += new MultiDynamicGlyphFontTextureProvider.CharTexture(this)
textures.last.bind()
}
textures.last.add(char)
}
}

override def isDynamic: Boolean = true

override def loadCodePoint(codePoint: Int): Unit = getCodePoint(codePoint)
}

object MultiDynamicFontTextureProvider {
object MultiDynamicGlyphFontTextureProvider {
private val size = 512

class CharTexture(val owner: MultiDynamicFontTextureProvider) {
class CharTexture(val owner: MultiDynamicGlyphFontTextureProvider) {
private val id = GlStateManager.generateTexture()
RenderState.bindTexture(id)
if (Settings.get.textLinearFiltering) {
Expand All @@ -120,7 +122,7 @@ object MultiDynamicFontTextureProvider {
}
GL11.glTexParameteri(GL11.GL_TEXTURE_2D, GL11.GL_TEXTURE_MAG_FILTER, GL11.GL_NEAREST)

GL11.glTexImage2D(GL11.GL_TEXTURE_2D, 0, GL11.GL_ALPHA8, size, size, 0, GL11.GL_ALPHA, GL11.GL_UNSIGNED_BYTE, BufferUtils.createByteBuffer(size * size * 4))
GL11.glTexImage2D(GL11.GL_TEXTURE_2D, 0, GL11.GL_INTENSITY8, size, size, 0, GL11.GL_RED, GL11.GL_UNSIGNED_BYTE, null.asInstanceOf[ByteBuffer])
RenderState.bindTexture(0)

RenderState.checkError(getClass.getName + ".<init>: create texture")
Expand Down Expand Up @@ -159,7 +161,7 @@ object MultiDynamicFontTextureProvider {
val y = chars / cols

RenderState.bindTexture(id)
GL11.glTexSubImage2D(GL11.GL_TEXTURE_2D, 0, 1 + x * cellWidth, 1 + y * cellHeight, w, h, GL11.GL_ALPHA, GL11.GL_UNSIGNED_BYTE, owner.glyphProvider.getGlyph(char))
GL11.glTexSubImage2D(GL11.GL_TEXTURE_2D, 0, 1 + x * cellWidth, 1 + y * cellHeight, w, h, GL11.GL_RED, GL11.GL_UNSIGNED_BYTE, owner.glyphProvider.getGlyph(char))

chars += glyphWidth

Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
package li.cil.oc.client.renderer.font

import java.nio.ByteBuffer

import it.unimi.dsi.fastutil.ints.Int2ObjectOpenHashMap
import li.cil.oc.Settings
import li.cil.oc.client.renderer.font.FontTextureProvider.Receiver
import li.cil.oc.client.renderer.font.SingleDynamicFontTextureProvider.{CharIcon, CharTexture}
import li.cil.oc.client.renderer.font.SingleGlyphFontTextureProvider.{CharIcon, CharTexture}
import li.cil.oc.util.{FontUtils, RenderState}
import net.minecraft.client.Minecraft
import net.minecraft.client.renderer.GlStateManager
Expand All @@ -12,7 +14,7 @@ import net.minecraft.util.math.MathHelper
import org.lwjgl.BufferUtils
import org.lwjgl.opengl.GL11

class SingleDynamicFontTextureProvider(private val glyphProvider: IGlyphProvider) extends FontTextureProvider with IResourceManagerReloadListener {
class SingleGlyphFontTextureProvider(private val glyphProvider: IGlyphProvider, private val dynamic: Boolean) extends FontTextureProvider with IResourceManagerReloadListener {
private var texture: CharTexture = _

private val charMap = new Int2ObjectOpenHashMap[CharIcon]
Expand All @@ -33,9 +35,8 @@ class SingleDynamicFontTextureProvider(private val glyphProvider: IGlyphProvider

// estimate texture size required
// be lazy about it, because most GPUs are way overkill for this... but it is 16MB
// TODO: better implementation
val maxTextureSize = Minecraft.getGLMaximumTextureSize
val worstCaseSideSize = MathHelper.smallestEncompassingPowerOfTwo(256 * math.max(glyphProvider.getGlyphWidth, glyphProvider.getGlyphHeight))
val worstCaseSideSize = 4096 // hardcoded for now... TODO

if (maxTextureSize < worstCaseSideSize) {
throw new CouldNotFitTextureException()
Expand All @@ -44,7 +45,14 @@ class SingleDynamicFontTextureProvider(private val glyphProvider: IGlyphProvider
charMap.clear()
texture = new CharTexture(worstCaseSideSize, worstCaseSideSize, this)

StaticFontTextureProvider.basicChars.foreach(c => getCodePoint(c))
if (dynamic) {
// preload only basic chars
StaticTextureFontTextureProvider.basicChars.foreach(c => getCodePoint(c))
} else {
for (i <- 0 until FontUtils.codepoint_limit) {
getCodePoint(i)
}
}
}

def onResourceManagerReload(manager: IResourceManager) {
Expand Down Expand Up @@ -101,10 +109,14 @@ class SingleDynamicFontTextureProvider(private val glyphProvider: IGlyphProvider
texture.add(char)
}
}

override def isDynamic: Boolean = dynamic

override def loadCodePoint(codePoint: Int): Unit = if (dynamic) getCodePoint(codePoint)
}

object SingleDynamicFontTextureProvider {
class CharTexture(val xSize: Int, val ySize: Int, val owner: SingleDynamicFontTextureProvider) {
object SingleGlyphFontTextureProvider {
class CharTexture(val xSize: Int, val ySize: Int, val owner: SingleGlyphFontTextureProvider) {
private val id = GlStateManager.generateTexture()
RenderState.bindTexture(id)
if (Settings.get.textLinearFiltering) {
Expand All @@ -113,7 +125,7 @@ object SingleDynamicFontTextureProvider {
GL11.glTexParameteri(GL11.GL_TEXTURE_2D, GL11.GL_TEXTURE_MIN_FILTER, GL11.GL_NEAREST)
}
GL11.glTexParameteri(GL11.GL_TEXTURE_2D, GL11.GL_TEXTURE_MAG_FILTER, GL11.GL_NEAREST)
GL11.glTexImage2D(GL11.GL_TEXTURE_2D, 0, GL11.GL_ALPHA8, xSize, ySize, 0, GL11.GL_ALPHA, GL11.GL_UNSIGNED_BYTE, BufferUtils.createByteBuffer(xSize * ySize * 4))
GL11.glTexImage2D(GL11.GL_TEXTURE_2D, 0, GL11.GL_INTENSITY8, xSize, ySize, 0, GL11.GL_RED, GL11.GL_UNSIGNED_BYTE, null.asInstanceOf[ByteBuffer])
RenderState.bindTexture(0)

RenderState.checkError(getClass.getName + ".<init>: create texture")
Expand Down Expand Up @@ -153,7 +165,7 @@ object SingleDynamicFontTextureProvider {
val y = chars / cols

RenderState.bindTexture(id)
GL11.glTexSubImage2D(GL11.GL_TEXTURE_2D, 0, 1 + x * cellWidth, 1 + y * cellHeight, w, h, GL11.GL_ALPHA, GL11.GL_UNSIGNED_BYTE, owner.glyphProvider.getGlyph(char))
GL11.glTexSubImage2D(GL11.GL_TEXTURE_2D, 0, 1 + x * cellWidth, 1 + y * cellHeight, w, h, GL11.GL_RED, GL11.GL_UNSIGNED_BYTE, owner.glyphProvider.getGlyph(char))

chars += glyphWidth

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import scala.io.Source
* Font renderer using a user specified texture file, meaning the list of
* supported characters is fixed. But at least this one works.
*/
class StaticFontTextureProvider extends FontTextureProvider {
class StaticTextureFontTextureProvider extends FontTextureProvider {
protected val (chars, charWidth, charHeight) = try {
val lines = Source.fromInputStream(Minecraft.getMinecraft.getResourceManager.getResource(new ResourceLocation(Settings.resourceDomain, "textures/font/chars.txt")).getInputStream)(Charsets.UTF_8).getLines()
val chars = lines.next()
Expand All @@ -27,7 +27,7 @@ class StaticFontTextureProvider extends FontTextureProvider {
catch {
case t: Throwable =>
OpenComputers.log.warn("Failed reading font metadata, using defaults.", t)
(StaticFontTextureProvider.basicChars, 10, 18)
(StaticTextureFontTextureProvider.basicChars, 10, 18)
}

private val cols = 256.0f / charWidth
Expand Down Expand Up @@ -75,8 +75,14 @@ class StaticFontTextureProvider extends FontTextureProvider {
receiver.draw(tx - dw, tx + charWidth * s, ty - dh, ty + charHeight * s, u, u + uSize, v, v + vSize)
}
}

override def isDynamic: Boolean = false

override def loadCodePoint(codePoint: Int): Unit = {

}
}

object StaticFontTextureProvider {
object StaticTextureFontTextureProvider {
final val basicChars = """☺☻♥♦♣♠•◘○◙♂♀♪♫☼►◄↕‼¶§▬↨↑↓→←∟↔▲▼ !"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\]^_`abcdefghijklmnopqrstuvwxyz{|}~⌂ÇüéâäàåçêëèïîìÄÅÉæÆôöòûùÿÖÜ¢£¥₧ƒáíóúñѪº¿⌐¬½¼¡«»░▒▓│┤╡╢╖╕╣║╗╝╜╛┐└┴┬├─┼╞╟╚╔╩╦╠═╬╧╨╤╥╙╘╒╓╫╪┘┌█▄▌▐▀αßΓπΣσµτΦΘΩδ∞φε∩≡±≥≤⌠⌡÷≈°∙·√ⁿ²■"""
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import java.util.concurrent.{Callable, TimeUnit}

import com.google.common.cache.{CacheBuilder, RemovalListener, RemovalNotification}
import li.cil.oc.{OpenComputers, Settings}
import li.cil.oc.client.renderer.font.{CouldNotFitTextureException, FontParserHex, FontTextureProvider, MultiDynamicFontTextureProvider, SingleDynamicFontTextureProvider, StaticFontTextureProvider, TextBufferRenderData}
import li.cil.oc.client.renderer.font.{CouldNotFitTextureException, FontParserHex, FontTextureProvider, MultiDynamicGlyphFontTextureProvider, SingleGlyphFontTextureProvider, StaticTextureFontTextureProvider, TextBufferRenderData}
import li.cil.oc.util.RenderState
import net.minecraft.client.Minecraft
import net.minecraftforge.fml.common.eventhandler.SubscribeEvent
Expand All @@ -19,16 +19,16 @@ object TextBufferRenderCache extends Callable[TextBufferRenderer] with RemovalLi

val fontTextureProvider: FontTextureProvider = {
if (Settings.get.fontRenderer == "texture") {
new StaticFontTextureProvider()
new StaticTextureFontTextureProvider()
}
else {
val glyphProvider = new FontParserHex()
try {
new SingleDynamicFontTextureProvider(glyphProvider)
new SingleGlyphFontTextureProvider(glyphProvider, false)
} catch {
case _: CouldNotFitTextureException => {
OpenComputers.log.warn("Could not fit font into maximum texture; using slow fallback renderer.")
new MultiDynamicFontTextureProvider(glyphProvider)
new MultiDynamicGlyphFontTextureProvider(glyphProvider)
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,15 @@ class TextBufferRendererDisplayList extends TextBufferRenderer {

val doCompile = !RenderState.compilingDisplayList
if (doCompile) {
if (fontTextureProvider.isDynamic) {
for (y <- 0 until (currentBuffer.viewport._2 min currentBuffer.data.height)) {
val line = currentBuffer.data.buffer(y)
for (n <- 0 until currentBuffer.viewport._1) {
fontTextureProvider.loadCodePoint(line(n))
}
}
}

currentBuffer.dirty = false
GL11.glNewList(list, GL11.GL_COMPILE_AND_EXECUTE)

Expand All @@ -37,31 +46,29 @@ class TextBufferRendererDisplayList extends TextBufferRenderer {
RenderState.checkError(getClass.getName + ".render: leaving")

profiler.endSection()

true
}
else {
profiler.startSection("execute_list")

GL11.glCallList(list)
GlStateManager.enableTexture2D()
GlStateManager.depthMask(true)
GlStateManager.color(1, 1, 1, 1)

// Because display lists and the GlStateManager don't like each other, apparently.
GL11.glEnable(GL11.GL_TEXTURE_2D)
RenderState.bindTexture(0)
GL11.glDepthMask(true)
GL11.glColor4f(1, 1, 1, 1)

RenderState.disableBlend()

RenderState.checkError(getClass.getName + ".render: glCallList")

profiler.endSection()

true
}

RenderState.disableBlend()
GlStateManager.enableTexture2D()
GlStateManager.depthMask(true)
GlStateManager.color(1, 1, 1, 1)

// Because display lists and the GlStateManager don't like each other, apparently.
GL11.glEnable(GL11.GL_TEXTURE_2D)
RenderState.bindTexture(0)
GL11.glDepthMask(true)
GL11.glColor4f(1, 1, 1, 1)

true
}

override def destroy(): Boolean = {
Expand All @@ -82,10 +89,8 @@ class TextBufferRendererDisplayList extends TextBufferRenderer {

GL11.glDepthMask(false)
GL11.glDisable(GL11.GL_BLEND)
GL11.glEnable(GL11.GL_ALPHA_TEST)
GL11.glDisable(GL11.GL_ALPHA_TEST)
GL11.glDisable(GL11.GL_TEXTURE_2D)
GL11.glAlphaFunc(GL11.GL_GEQUAL, 0.5f)
GlStateManager.alphaFunc(GL11.GL_GEQUAL, 0.5f)

RenderState.checkError(getClass.getName + ".drawBuffer: configure state")

Expand Down Expand Up @@ -113,6 +118,9 @@ class TextBufferRendererDisplayList extends TextBufferRenderer {
RenderState.checkError(getClass.getName + ".drawBuffer: background")

GL11.glEnable(GL11.GL_TEXTURE_2D)
GL11.glEnable(GL11.GL_ALPHA_TEST)
GL11.glAlphaFunc(GL11.GL_GEQUAL, 0.5f)
GlStateManager.alphaFunc(GL11.GL_GEQUAL, 0.5f)

if (Settings.get.textLinearFiltering) {
GL11.glTexParameteri(GL11.GL_TEXTURE_2D, GL11.GL_TEXTURE_MIN_FILTER, GL11.GL_LINEAR)
Expand All @@ -137,10 +145,11 @@ class TextBufferRendererDisplayList extends TextBufferRenderer {
// Check if color changed.
if (col != cfg) {
cfg = col
GL11.glColor3f(
GL11.glColor4f(
((cfg & 0xFF0000) >> 16) / 255.0f,
((cfg & 0x00FF00) >> 8) / 255.0f,
((cfg & 0x0000FF) >> 0) / 255.0f)
((cfg & 0x0000FF) >> 0) / 255.0f,
1.0f)
}
fontTextureProvider.drawCodePoint(ch, tx, ty, TextBufferRendererDisplayList.receiver)
}
Expand All @@ -155,7 +164,7 @@ class TextBufferRendererDisplayList extends TextBufferRenderer {

GlStateManager.bindTexture(0)
GL11.glDepthMask(true)
GL11.glColor3f(1, 1, 1)
GL11.glColor4f(1, 1, 1, 1)
GL11.glDisable(GL11.GL_ALPHA_TEST)
GlStateManager.disableAlpha()
GlStateManager.disableBlend()
Expand All @@ -170,10 +179,11 @@ class TextBufferRendererDisplayList extends TextBufferRenderer {
val x1 = (x + width) * charWidth
val y0 = y * charHeight
val y1 = (y + 1) * charHeight
GlStateManager.color(
GL11.glColor4f(
((color >> 16) & 0xFF) / 255f,
((color >> 8) & 0xFF) / 255f,
(color & 0xFF) / 255f)
(color & 0xFF) / 255f,
1.0f)
GL11.glVertex3f(x0, y1, 0)
GL11.glVertex3f(x1, y1, 0)
GL11.glVertex3f(x1, y0, 0)
Expand Down Expand Up @@ -222,6 +232,8 @@ object TextBufferRendererDisplayList {

RenderState.popAttrib()
GlStateManager.popMatrix()

GL11.glColor4f(1f, 1f, 1f, 1f)
GlStateManager.color(1, 1, 1)
}
}