Refactored the hologram renderer a bit (split phases into separate functions - init [create common buffer], validate [check if dirty, rebuild as needed] and publish [do the actual rendering]).

Also caching the buffer used for the color data and indexes to avoid spamming the GC with instances of those (can't hurt, right?)
This commit is contained in:
Florian Nücke 2014-06-10 16:54:47 +02:00
parent 234b33f474
commit 2b709eee3c
2 changed files with 203 additions and 192 deletions

View File

@ -13,16 +13,28 @@ import org.lwjgl.opengl.{GL15, GL11}
import scala.util.Random import scala.util.Random
import org.lwjgl.BufferUtils import org.lwjgl.BufferUtils
import li.cil.oc.Settings import li.cil.oc.Settings
import java.nio.IntBuffer
object HologramRenderer extends TileEntitySpecialRenderer with Callable[Int] with RemovalListener[TileEntity, Int] with ITickHandler { object HologramRenderer extends TileEntitySpecialRenderer with Callable[(Int, IntBuffer)] with RemovalListener[TileEntity, (Int, IntBuffer)] with ITickHandler {
val random = new Random() private val random = new Random()
/** We cache the VBOs for the projectors we render for performance. */ /** We cache the VBOs for the projectors we render for performance. */
val cache = com.google.common.cache.CacheBuilder.newBuilder(). private val cache = com.google.common.cache.CacheBuilder.newBuilder().
expireAfterAccess(10, TimeUnit.SECONDS). expireAfterAccess(10, TimeUnit.SECONDS).
removalListener(this). removalListener(this).
asInstanceOf[CacheBuilder[Hologram, Int]]. asInstanceOf[CacheBuilder[Hologram, (Int, IntBuffer)]].
build[Hologram, Int]() build[Hologram, (Int, IntBuffer)]()
/**
* Common for all holograms. Holds the vertex positions, texture
* coordinates and normals information. Layout is: u v nx ny nz x y z
*
* WARNING: this optimization only works if all the holograms have the
* same dimensions (in voxels). If we ever need holograms of different
* sizes we could probably just fake that by making the outer layers
* immutable (i.e. always empty).
*/
private var commonBuffer = 0
/** Used to pass the current screen along to call(). */ /** Used to pass the current screen along to call(). */
private var hologram: Hologram = null private var hologram: Hologram = null
@ -33,6 +45,7 @@ object HologramRenderer extends TileEntitySpecialRenderer with Callable[Int] wit
hologram = te.asInstanceOf[Hologram] hologram = te.asInstanceOf[Hologram]
if (!hologram.hasPower) return if (!hologram.hasPower) return
GL11.glPushClientAttrib(GL11.GL_ALL_CLIENT_ATTRIB_BITS)
GL11.glPushAttrib(GL11.GL_ALL_ATTRIB_BITS) GL11.glPushAttrib(GL11.GL_ALL_ATTRIB_BITS)
RenderState.makeItBlend() RenderState.makeItBlend()
GL11.glBlendFunc(GL11.GL_SRC_ALPHA, GL11.GL_ONE) GL11.glBlendFunc(GL11.GL_SRC_ALPHA, GL11.GL_ONE)
@ -58,44 +71,47 @@ object HologramRenderer extends TileEntitySpecialRenderer with Callable[Int] wit
// We do two passes here to avoid weird transparency effects: in the first // We do two passes here to avoid weird transparency effects: in the first
// pass we find the front-most fragment, in the second we actually draw it. // pass we find the front-most fragment, in the second we actually draw it.
// TODO proper transparency shader? depth peeling e.g. // When we don't do this the hologram will look different from different
// evg-zhabotinsky: I'd rather not do it. Anyway it won't work for multiple holograms. // angles (because some faces will shine through sometimes and sometimes
// Also I commented out the first pass to see what it will look like and I prefer it the way it is now. // they won't), so a more... consistent look is desirable.
val (glBuffer, dataBuffer) = cache.get(hologram, this)
GL11.glColorMask(false, false, false, false) GL11.glColorMask(false, false, false, false)
GL11.glDepthMask(true) GL11.glDepthMask(true)
val privateBuf = cache.get(hologram, this) draw(glBuffer, dataBuffer)
compileOrDraw(privateBuf)
GL11.glColorMask(true, true, true, true) GL11.glColorMask(true, true, true, true)
GL11.glDepthFunc(GL11.GL_EQUAL) GL11.glDepthFunc(GL11.GL_EQUAL)
compileOrDraw(privateBuf) draw(glBuffer, dataBuffer)
GL11.glPopMatrix() GL11.glPopMatrix()
GL11.glPopAttrib() GL11.glPopAttrib()
GL11.glPopClientAttrib()
RenderState.checkError(getClass.getName + ".renderTileEntityAt: leaving") RenderState.checkError(getClass.getName + ".renderTileEntityAt: leaving")
} }
val compileOrDraw = { def draw(glBuffer: Int, dataBuffer: IntBuffer) {
// WARNING works only if all the holograms have the same dimensions (in voxels) initialize()
var commonBuffer = 0 // Common for all holograms (a-la static variable) validate(glBuffer, dataBuffer)
(privateBuf: Int) => { publish(glBuffer)
// Save current state (don't forget to restore)
GL11.glPushClientAttrib(GL11.GL_ALL_CLIENT_ATTRIB_BITS)
GL11.glPushAttrib(GL11.GL_ALL_ATTRIB_BITS)
if (commonBuffer == 0) { // First run only
commonBuffer = GL15.glGenBuffers()
var tmpBuf = BufferUtils.createFloatBuffer(hologram.width * hologram.width * hologram.height * 24 * (2 + 3 + 3))
def newVert = (x: Int, y: Int, z: Int, u: Int, v: Int, nx: Int, ny: Int, nz: Int) => {
tmpBuf.put(u)
tmpBuf.put(v)
tmpBuf.put(nx)
tmpBuf.put(ny)
tmpBuf.put(nz)
tmpBuf.put(x)
tmpBuf.put(y)
tmpBuf.put(z)
} }
private def initialize() {
// First run only, create structure information.
if (commonBuffer == 0) {
commonBuffer = GL15.glGenBuffers()
val data = BufferUtils.createFloatBuffer(hologram.width * hologram.width * hologram.height * 24 * (2 + 3 + 3))
def addVertex(x: Int, y: Int, z: Int, u: Int, v: Int, nx: Int, ny: Int, nz: Int) {
data.put(u)
data.put(v)
data.put(nx)
data.put(ny)
data.put(nz)
data.put(x)
data.put(y)
data.put(z)
}
for (x <- 0 until hologram.width) { for (x <- 0 until hologram.width) {
for (z <- 0 until hologram.width) { for (z <- 0 until hologram.width) {
for (y <- 0 until hologram.height) { for (y <- 0 until hologram.height) {
@ -110,171 +126,159 @@ object HologramRenderer extends TileEntitySpecialRenderer with Callable[Int] wit
*/ */
// South // South
newVert(x + 1, y + 1, z + 1, 0, 0, 0, 0, 1) // 5 addVertex(x + 1, y + 1, z + 1, 0, 0, 0, 0, 1) // 5
newVert(x + 0, y + 1, z + 1, 1, 0, 0, 0, 1) // 4 addVertex(x + 0, y + 1, z + 1, 1, 0, 0, 0, 1) // 4
newVert(x + 0, y + 0, z + 1, 1, 1, 0, 0, 1) // 7 addVertex(x + 0, y + 0, z + 1, 1, 1, 0, 0, 1) // 7
newVert(x + 1, y + 0, z + 1, 0, 1, 0, 0, 1) // 6 addVertex(x + 1, y + 0, z + 1, 0, 1, 0, 0, 1) // 6
// North // North
newVert(x + 1, y + 0, z + 0, 0, 0, 0, 0, -1) // 3 addVertex(x + 1, y + 0, z + 0, 0, 0, 0, 0, -1) // 3
newVert(x + 0, y + 0, z + 0, 1, 0, 0, 0, -1) // 2 addVertex(x + 0, y + 0, z + 0, 1, 0, 0, 0, -1) // 2
newVert(x + 0, y + 1, z + 0, 1, 1, 0, 0, -1) // 1 addVertex(x + 0, y + 1, z + 0, 1, 1, 0, 0, -1) // 1
newVert(x + 1, y + 1, z + 0, 0, 1, 0, 0, -1) // 0 addVertex(x + 1, y + 1, z + 0, 0, 1, 0, 0, -1) // 0
// East // East
newVert(x + 1, y + 1, z + 1, 1, 0, 1, 0, 0) // 5 addVertex(x + 1, y + 1, z + 1, 1, 0, 1, 0, 0) // 5
newVert(x + 1, y + 0, z + 1, 1, 1, 1, 0, 0) // 6 addVertex(x + 1, y + 0, z + 1, 1, 1, 1, 0, 0) // 6
newVert(x + 1, y + 0, z + 0, 0, 1, 1, 0, 0) // 3 addVertex(x + 1, y + 0, z + 0, 0, 1, 1, 0, 0) // 3
newVert(x + 1, y + 1, z + 0, 0, 0, 1, 0, 0) // 0 addVertex(x + 1, y + 1, z + 0, 0, 0, 1, 0, 0) // 0
// West // West
newVert(x + 0, y + 0, z + 1, 1, 0, -1, 0, 0) // 7 addVertex(x + 0, y + 0, z + 1, 1, 0, -1, 0, 0) // 7
newVert(x + 0, y + 1, z + 1, 1, 1, -1, 0, 0) // 4 addVertex(x + 0, y + 1, z + 1, 1, 1, -1, 0, 0) // 4
newVert(x + 0, y + 1, z + 0, 0, 1, -1, 0, 0) // 1 addVertex(x + 0, y + 1, z + 0, 0, 1, -1, 0, 0) // 1
newVert(x + 0, y + 0, z + 0, 0, 0, -1, 0, 0) // 2 addVertex(x + 0, y + 0, z + 0, 0, 0, -1, 0, 0) // 2
// Up // Up
newVert(x + 1, y + 1, z + 0, 0, 0, 0, 1, 0) // 0 addVertex(x + 1, y + 1, z + 0, 0, 0, 0, 1, 0) // 0
newVert(x + 0, y + 1, z + 0, 1, 0, 0, 1, 0) // 1 addVertex(x + 0, y + 1, z + 0, 1, 0, 0, 1, 0) // 1
newVert(x + 0, y + 1, z + 1, 1, 1, 0, 1, 0) // 4 addVertex(x + 0, y + 1, z + 1, 1, 1, 0, 1, 0) // 4
newVert(x + 1, y + 1, z + 1, 0, 1, 0, 1, 0) // 5 addVertex(x + 1, y + 1, z + 1, 0, 1, 0, 1, 0) // 5
// Down // Down
newVert(x + 1, y + 0, z + 1, 0, 0, 0, -1, 0) // 6 addVertex(x + 1, y + 0, z + 1, 0, 0, 0, -1, 0) // 6
newVert(x + 0, y + 0, z + 1, 1, 0, 0, -1, 0) // 7 addVertex(x + 0, y + 0, z + 1, 1, 0, 0, -1, 0) // 7
newVert(x + 0, y + 0, z + 0, 1, 1, 0, -1, 0) // 2 addVertex(x + 0, y + 0, z + 0, 1, 1, 0, -1, 0) // 2
newVert(x + 1, y + 0, z + 0, 0, 1, 0, -1, 0) // 3 addVertex(x + 1, y + 0, z + 0, 0, 1, 0, -1, 0) // 3
} }
} }
} }
tmpBuf.rewind() // Important!
GL15.glBindBuffer(GL15.GL_ARRAY_BUFFER, commonBuffer)
GL15.glBufferData(GL15.GL_ARRAY_BUFFER, tmpBuf, GL15.GL_STATIC_DRAW)
}
if (hologram.dirty) { // Refresh hologram // Important! OpenGL will start reading from the current buffer position.
data.rewind()
// This buffer never ever changes, so static is the way to go.
GL15.glBindBuffer(GL15.GL_ARRAY_BUFFER, commonBuffer)
GL15.glBufferData(GL15.GL_ARRAY_BUFFER, data, GL15.GL_STATIC_DRAW)
}
}
private def validate(glBuffer: Int, dataBuffer: IntBuffer) {
// Refresh indexes when the hologram's data changed.
if (hologram.dirty) {
def value(hx: Int, hy: Int, hz: Int) = if (hx >= 0 && hy >= 0 && hz >= 0 && hx < hologram.width && hy < hologram.height && hz < hologram.width) hologram.getColor(hx, hy, hz) else 0 def value(hx: Int, hy: Int, hz: Int) = if (hx >= 0 && hy >= 0 && hz >= 0 && hx < hologram.width && hy < hologram.height && hz < hologram.width) hologram.getColor(hx, hy, hz) else 0
def isSolid(hx: Int, hy: Int, hz: Int) = value(hx, hy, hz) != 0 def isSolid(hx: Int, hy: Int, hz: Int) = value(hx, hy, hz) != 0
var tmpBuf = BufferUtils.createIntBuffer(hologram.width * hologram.width * hologram.height * 24 * 2) def addFace(index: Int, color: Int) {
dataBuffer.put(index)
dataBuffer.put(index + 1)
dataBuffer.put(index + 2)
dataBuffer.put(index + 3)
dataBuffer.put(index, color)
dataBuffer.put(index + 1, color)
dataBuffer.put(index + 2, color)
dataBuffer.put(index + 3, color)
hologram.visibleQuads += 1
}
// Copy color information, identify which quads to render and prepare data for glDrawElements // Copy color information, identify which quads to render and prepare data for glDrawElements
hologram.visibleQuads = 0 hologram.visibleQuads = 0
var c = 0 var index = 0
tmpBuf.position(hologram.width * hologram.width * hologram.height * 24) dataBuffer.position(hologram.width * hologram.width * hologram.height * 6 * 4)
for (hx <- 0 until hologram.width) { for (hx <- 0 until hologram.width) {
for (hz <- 0 until hologram.width) { for (hz <- 0 until hologram.width) {
for (hy <- 0 until hologram.height) { for (hy <- 0 until hologram.height) {
// Do we need to draw at least one face?
if (isSolid(hx, hy, hz)) { if (isSolid(hx, hy, hz)) {
val v: Int = hologram.colors(value(hx, hy, hz) - 1) // Yes, get the color of the voxel.
val color = hologram.colors(value(hx, hy, hz) - 1)
// South // South
if (!isSolid(hx, hy, hz + 1)) { if (!isSolid(hx, hy, hz + 1)) {
tmpBuf.put(c) addFace(index, color)
tmpBuf.put(c + 1)
tmpBuf.put(c + 2)
tmpBuf.put(c + 3)
tmpBuf.put(c, v)
tmpBuf.put(c + 1, v)
tmpBuf.put(c + 2, v)
tmpBuf.put(c + 3, v)
hologram.visibleQuads += 1
} }
c += 4 index += 4
// North // North
if (!isSolid(hx, hy, hz - 1)) { if (!isSolid(hx, hy, hz - 1)) {
tmpBuf.put(c) addFace(index, color)
tmpBuf.put(c + 1)
tmpBuf.put(c + 2)
tmpBuf.put(c + 3)
tmpBuf.put(c, v)
tmpBuf.put(c + 1, v)
tmpBuf.put(c + 2, v)
tmpBuf.put(c + 3, v)
hologram.visibleQuads += 1
} }
c += 4 index += 4
// East // East
if (!isSolid(hx + 1, hy, hz)) { if (!isSolid(hx + 1, hy, hz)) {
tmpBuf.put(c) addFace(index, color)
tmpBuf.put(c + 1)
tmpBuf.put(c + 2)
tmpBuf.put(c + 3)
tmpBuf.put(c, v)
tmpBuf.put(c + 1, v)
tmpBuf.put(c + 2, v)
tmpBuf.put(c + 3, v)
hologram.visibleQuads += 1
} }
c += 4 index += 4
// West // West
if (!isSolid(hx - 1, hy, hz)) { if (!isSolid(hx - 1, hy, hz)) {
tmpBuf.put(c) addFace(index, color)
tmpBuf.put(c + 1)
tmpBuf.put(c + 2)
tmpBuf.put(c + 3)
tmpBuf.put(c, v)
tmpBuf.put(c + 1, v)
tmpBuf.put(c + 2, v)
tmpBuf.put(c + 3, v)
hologram.visibleQuads += 1
} }
c += 4 index += 4
// Up // Up
if (!isSolid(hx, hy + 1, hz)) { if (!isSolid(hx, hy + 1, hz)) {
tmpBuf.put(c) addFace(index, color)
tmpBuf.put(c + 1)
tmpBuf.put(c + 2)
tmpBuf.put(c + 3)
tmpBuf.put(c, v)
tmpBuf.put(c + 1, v)
tmpBuf.put(c + 2, v)
tmpBuf.put(c + 3, v)
hologram.visibleQuads += 1
} }
c += 4 index += 4
// Down // Down
if (!isSolid(hx, hy - 1, hz)) { if (!isSolid(hx, hy - 1, hz)) {
tmpBuf.put(c) addFace(index, color)
tmpBuf.put(c + 1)
tmpBuf.put(c + 2)
tmpBuf.put(c + 3)
tmpBuf.put(c, v)
tmpBuf.put(c + 1, v)
tmpBuf.put(c + 2, v)
tmpBuf.put(c + 3, v)
hologram.visibleQuads += 1
} }
c += 4 index += 4
} else c += 24 }
else {
// No, skip all associated indices.
index += 6 * 4
} }
} }
} }
tmpBuf.rewind() // Important! }
GL15.glBindBuffer(GL15.GL_ARRAY_BUFFER, privateBuf)
GL15.glBufferData(GL15.GL_ARRAY_BUFFER, tmpBuf, GL15.GL_STATIC_DRAW) // Important! OpenGL will start reading from the current buffer position.
dataBuffer.rewind()
// This buffer can be updated quite frequently, so dynamic seems sensible.
GL15.glBindBuffer(GL15.GL_ARRAY_BUFFER, glBuffer)
GL15.glBufferData(GL15.GL_ARRAY_BUFFER, dataBuffer, GL15.GL_DYNAMIC_DRAW)
hologram.dirty = false hologram.dirty = false
} }
}
GL11.glEnable(GL11.GL_NORMALIZE) // Normalize normals!!! (Yes, glScale scales them too!) private def publish(glBuffer: Int) {
GL11.glEnable(GL11.GL_CULL_FACE)
GL11.glCullFace(GL11.GL_BACK) // Because fragment processing started to slow things down
bindTexture(Textures.blockHologram) bindTexture(Textures.blockHologram)
// Normalize normals (yes, glScale scales them too).
GL11.glEnable(GL11.GL_NORMALIZE)
// evg-zhabotinsky: Because fragment processing started to slow things down
// TODO but holograms look terrible from the inside otherwise,
// and I don't see a difference in FPS anyway? Maybe a
// check for the camera position - if inside don't cull?
// GL11.glEnable(GL11.GL_CULL_FACE)
// GL11.glCullFace(GL11.GL_BACK)
GL15.glBindBuffer(GL15.GL_ARRAY_BUFFER, commonBuffer) GL15.glBindBuffer(GL15.GL_ARRAY_BUFFER, commonBuffer)
GL11.glEnableClientState(GL11.GL_VERTEX_ARRAY) GL11.glEnableClientState(GL11.GL_VERTEX_ARRAY)
GL11.glEnableClientState(GL11.GL_TEXTURE_COORD_ARRAY) GL11.glEnableClientState(GL11.GL_TEXTURE_COORD_ARRAY)
GL11.glEnableClientState(GL11.GL_NORMAL_ARRAY) GL11.glEnableClientState(GL11.GL_NORMAL_ARRAY)
GL11.glInterleavedArrays(GL11.GL_T2F_N3F_V3F, 0, 0) GL11.glInterleavedArrays(GL11.GL_T2F_N3F_V3F, 0, 0)
GL15.glBindBuffer(GL15.GL_ARRAY_BUFFER, privateBuf)
GL15.glBindBuffer(GL15.GL_ARRAY_BUFFER, glBuffer)
GL11.glEnableClientState(GL11.GL_COLOR_ARRAY) GL11.glEnableClientState(GL11.GL_COLOR_ARRAY)
GL11.glColorPointer(3, GL11.GL_UNSIGNED_BYTE, 4, 0) GL11.glColorPointer(3, GL11.GL_UNSIGNED_BYTE, 4, 0)
GL15.glBindBuffer(GL15.GL_ELEMENT_ARRAY_BUFFER, privateBuf)
GL11.glDrawElements(GL11.GL_QUADS, hologram.visibleQuads * 4, GL11.GL_UNSIGNED_INT, hologram.width * hologram.width * hologram.height * 24 * 4) GL15.glBindBuffer(GL15.GL_ELEMENT_ARRAY_BUFFER, glBuffer)
GL11.glDrawElements(GL11.GL_QUADS, hologram.visibleQuads * 4, GL11.GL_UNSIGNED_INT, hologram.width * hologram.width * hologram.height * 6 * 4 * 4)
// Restore original state
GL11.glPopAttrib()
GL11.glPopClientAttrib()
}
} }
// ----------------------------------------------------------------------- // // ----------------------------------------------------------------------- //
@ -282,13 +286,19 @@ object HologramRenderer extends TileEntitySpecialRenderer with Callable[Int] wit
// ----------------------------------------------------------------------- // // ----------------------------------------------------------------------- //
def call = { def call = {
val privateBuf = GL15.glGenBuffers() val glBuffer = GL15.glGenBuffers()
hologram.dirty = true // Force compilation. val dataBuffer = BufferUtils.createIntBuffer(hologram.width * hologram.width * hologram.height * 6 * 4 * 2)
privateBuf
// Force re-indexing.
hologram.dirty = true
(glBuffer, dataBuffer)
} }
def onRemoval(e: RemovalNotification[TileEntity, Int]) { def onRemoval(e: RemovalNotification[TileEntity, (Int, IntBuffer)]) {
GL15.glDeleteBuffers(e.getValue) val (glBuffer, dataBuffer) = e.getValue
GL15.glDeleteBuffers(glBuffer)
dataBuffer.clear()
} }
// ----------------------------------------------------------------------- // // ----------------------------------------------------------------------- //

View File

@ -34,7 +34,8 @@ class Hologram(var tier: Int) extends traits.Environment with SidedEnvironment w
// Whether we need to send an update packet/recompile our display list. // Whether we need to send an update packet/recompile our display list.
var dirty = false var dirty = false
// Store it here for convenience // Store it here for convenience, this is the number of visible voxel faces
// as determined in the last VBO index update. See HologramRenderer.
var visibleQuads = 0 var visibleQuads = 0
// Interval of dirty columns. // Interval of dirty columns.