Skip to content

Commit

Permalink
OpenGLRenderer: Cut down non-null asserted calls
Browse files Browse the repository at this point in the history
  • Loading branch information
skalarproduktraum committed Dec 13, 2018
1 parent 2eda6d1 commit 037a01d
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 21 deletions.
Expand Up @@ -2,6 +2,7 @@ package graphics.scenery.backends.opengl

import cleargl.GLTexture
import graphics.scenery.NodeMetadata
import graphics.scenery.utils.LazyLogger
import java.nio.ByteBuffer
import java.util.*
import java.util.concurrent.ConcurrentHashMap
Expand All @@ -14,6 +15,7 @@ import java.util.concurrent.ConcurrentHashMap
* default consumers.
*/
class OpenGLObjectState : NodeMetadata {
private val logger by LazyLogger()
/** List of consumers of this metadata, e.g. [OpenGLRenderer] */
override val consumers: MutableList<String> = ArrayList()

Expand Down Expand Up @@ -52,4 +54,18 @@ class OpenGLObjectState : NodeMetadata {
init {
consumers.add("OpenGLRenderer")
}

fun getUBO(name: String): OpenGLUBO? {
return UBOs[name]
}

fun getBackedUBO(name: String): Pair<OpenGLUBO, OpenGLRenderer.OpenGLBuffer>? {
val ubo = UBOs[name]
return if(ubo?.backingBuffer != null) {
ubo to ubo.backingBuffer
} else {
logger.warn("UBO for $name has no backing buffer")
null
}
}
}
50 changes: 29 additions & 21 deletions src/main/kotlin/graphics/scenery/backends/opengl/OpenGLRenderer.kt
Expand Up @@ -1069,17 +1069,21 @@ open class OpenGLRenderer(hub: Hub,

val s = node.metadata[this.javaClass.simpleName] as OpenGLObjectState

val ubo = s.UBOs["Matrices"]!!
val ubo = s.UBOs["Matrices"]
if(ubo?.backingBuffer == null) {
logger.warn("Matrices UBO for ${node.name} does not exist or does not have a backing buffer")
return@forEach
}

node.updateWorld(true, false)

var bufferOffset = ubo.backingBuffer!!.advance()
var bufferOffset = ubo.advanceBackingBuffer()
ubo.offset = bufferOffset
node.view.copyFrom(cam.view)
nodeUpdated = ubo.populate(offset = bufferOffset.toLong())

val materialUbo = (node.metadata["OpenGLRenderer"]!! as OpenGLObjectState).UBOs["MaterialProperties"]!!
bufferOffset = ubo.backingBuffer.advance()
bufferOffset = ubo.advanceBackingBuffer()
materialUbo.offset = bufferOffset

nodeUpdated = materialUbo.populate(offset = bufferOffset.toLong())
Expand Down Expand Up @@ -1320,8 +1324,9 @@ open class OpenGLRenderer(hub: Hub,

val instanceBufferSize = ubo.getSize() * parentNode.instances.size

val stagingBuffer = if(state.vertexBuffers.containsKey("instanceStaging") && state.vertexBuffers["instanceStaging"]!!.capacity() >= instanceBufferSize) {
state.vertexBuffers["instanceStaging"]!!
val existingStagingBuffer = state.vertexBuffers["instanceStaging"]
val stagingBuffer = if(existingStagingBuffer != null && existingStagingBuffer.capacity() >= instanceBufferSize) {
existingStagingBuffer
} else {
logger.debug("${parentNode.name}: Creating new staging buffer with capacity=$instanceBufferSize (${ubo.getSize()} x ${parentNode.instances.size})")
val buffer = BufferUtils.allocateByte(instanceBufferSize)
Expand Down Expand Up @@ -1350,9 +1355,7 @@ open class OpenGLRenderer(hub: Hub,
stagingBuffer.position(parentNode.instances.size * ubo.getSize())
stagingBuffer.flip()

val instanceBuffer = if (state.additionalBufferIds.containsKey("instance")) {
state.additionalBufferIds["instance"]!!
} else {
val instanceBuffer = state.additionalBufferIds.getOrPut("instance") {
logger.debug("Instance buffer for ${parentNode.name} needs to be reallocated due to insufficient size ($instanceBufferSize vs ${state.vertexBuffers["instance"]?.capacity() ?: "<not allocated yet>"})")

val bufferArray = intArrayOf(0)
Expand Down Expand Up @@ -1808,17 +1811,23 @@ open class OpenGLRenderer(hub: Hub,
}
}

arrayOf("VRParameters", "LightParameters").forEach { name ->
arrayOf("VRParameters", "LightParameters").forEach uboBinding@ { name ->
if (shader.uboSpecs.containsKey(name) && shader.isValid()) {
val buffer = buffers[name]
if(buffer == null) {
logger.warn("Buffer for $name not found")
return@uboBinding
}

val index = shader.getUniformBlockIndex(name)

if (index == -1) {
logger.error("Failed to bind shader parameter UBO $name for ${pass.passName} to $binding, though it is required by the shader")
} else {
gl.glUniformBlockBinding(shader.id, index, binding)
gl.glBindBufferRange(GL4.GL_UNIFORM_BUFFER, binding,
buffers[name]!!.id[0],
0L, buffers[name]!!.buffer.remaining().toLong())
buffer.id[0],
0L, buffer.buffer.remaining().toLong())


binding++
Expand Down Expand Up @@ -2046,15 +2055,13 @@ open class OpenGLRenderer(hub: Hub,
val quad: Node
val quadName = "fullscreenQuad-${program.id}"

if (!nodeStore.containsKey(quadName)) {
quad = Plane(GLVector(1.0f, 1.0f, 0.0f))
quad = nodeStore.getOrPut(quadName) {
val q = Plane(GLVector(1.0f, 1.0f, 0.0f))

quad.metadata["OpenGLRenderer"] = OpenGLObjectState()
initializeNode(quad)
q.metadata["OpenGLRenderer"] = OpenGLObjectState()
initializeNode(q)

nodeStore[quadName] = quad
} else {
quad = nodeStore[quadName]!!
q
}

drawNode(quad, count = 3)
Expand Down Expand Up @@ -2084,15 +2091,16 @@ open class OpenGLRenderer(hub: Hub,

val s: OpenGLObjectState

if (node.instanceOf == null) {
val instanceOf = node.instanceOf
if (instanceOf == null) {
s = node.metadata["OpenGLRenderer"] as OpenGLObjectState
} else {
s = node.instanceOf!!.metadata["OpenGLRenderer"] as OpenGLObjectState
s = instanceOf.metadata["OpenGLRenderer"] as OpenGLObjectState
node.metadata["OpenGLRenderer"] = s

if (!s.initialized) {
logger.trace("Instance not yet initialized, doing now...")
initializeNode(node.instanceOf!!)
initializeNode(instanceOf)
}

// if (!s.additionalBufferIds.containsKey("Model") || !s.additionalBufferIds.containsKey("ModelView") || !s.additionalBufferIds.containsKey("MVP")) {
Expand Down
7 changes: 7 additions & 0 deletions src/main/kotlin/graphics/scenery/backends/opengl/OpenGLUBO.kt
Expand Up @@ -71,4 +71,11 @@ class OpenGLUBO(val backingBuffer: OpenGLRenderer.OpenGLBuffer? = null) : UBO()
offset = it.advance()
}
}

fun advanceBackingBuffer(): Int {
if(backingBuffer == null) {
throw IllegalStateException("Tried to advance buffer that has no backing buffer")
}
return backingBuffer.advance()
}
}

0 comments on commit 037a01d

Please sign in to comment.