skeletal: pack matrices async

This commit is contained in:
Moritz Zwerger 2023-11-26 20:26:31 +01:00
parent 6d46b8358b
commit a243693998
No known key found for this signature in database
GPG Key ID: 5CAD791931B09AC4
9 changed files with 35 additions and 11 deletions

View File

@ -65,6 +65,7 @@ class EntitiesRenderer(
visibility.update(it, millis)
if (!it.visible) return@iterate
it.update(millis)
it.prepare()
visibility.collect(it)
} catch (error: Throwable) {
error.printStackTrace()

View File

@ -36,6 +36,7 @@ abstract class EntityRenderFeature(val renderer: EntityRenderer<*>) : Comparable
}
open fun reset() = Unit
open fun prepare() = Unit
open fun update(millis: Long, delta: Float) = Unit
open fun unload() = Unit

View File

@ -37,6 +37,13 @@ class FeatureManager(val renderer: EntityRenderer<*>) : Iterable<EntityRenderFea
}
}
fun prepare() {
for (feature in features) {
if (feature.isInvisible()) continue
feature.prepare()
}
}
fun unload() {
for (feature in features) {
feature.unload()

View File

@ -67,6 +67,10 @@ open class SkeletalFeature(
instance.animation.draw(delta)
}
override fun prepare() {
instance.transform.pack(instance.matrix)
}
override fun draw() {
var tint = renderer.light.value
if (renderer is LivingEntityRenderer<*>) {

View File

@ -48,7 +48,7 @@ open class PlayerModel(
shader.skinParts = this.skinParts
manager.upload(instance, instance.matrix)
manager.upload(instance)
instance.model.mesh.draw()
}
}

View File

@ -79,6 +79,10 @@ abstract class EntityRenderer<E : Entity>(
features.update(millis, delta)
}
open fun prepare() {
features.prepare()
}
open fun updateRenderInfo(millis: Long) {
entity.draw(millis)
this.distance = (entity.renderInfo.eyePosition - renderer.connection.camera.entity.renderInfo.eyePosition).length2()

View File

@ -24,7 +24,6 @@ import org.lwjgl.system.MemoryUtil.memAllocFloat
class SkeletalManager(
val context: RenderContext,
) {
private val cache: Array<Mat4> = Array(MAX_TRANSFORMS) { Mat4() } // reusing matrices
val buffer = context.system.createFloatUniformBuffer(memAllocFloat(MAX_TRANSFORMS * Mat4.length))
val shader = context.system.createShader(minosoft("skeletal/normal")) { SkeletalShader(it, buffer) }
val lightmapShader = context.system.createShader(minosoft("skeletal/lightmap")) { LightmapSkeletalShader(it, buffer) }
@ -38,8 +37,8 @@ class SkeletalManager(
lightmapShader.load()
}
fun upload(instance: SkeletalInstance, matrix: Mat4) {
instance.transform.pack(buffer.buffer, matrix, cache)
fun upload(instance: SkeletalInstance) {
instance.transform.pack(buffer.buffer)
buffer.upload(0, instance.model.transformCount * Mat4.length)
}

View File

@ -53,13 +53,14 @@ class SkeletalInstance(
fun draw(shader: Shader) {
shader.use()
context.skeletal.upload(this, matrix)
context.skeletal.upload(this)
model.mesh.draw()
}
fun update(time: Long = millis()) {
transform.reset()
animation.draw(time)
transform.pack(matrix)
}
fun update(position: Vec3, rotation: Vec3, pivot: Vec3 = Vec3.EMPTY_INSTANCE, matrix: Mat4? = null) {

View File

@ -36,17 +36,24 @@ class TransformInstance(
}
}
fun pack(buffer: FloatBuffer, parent: Mat4, cache: Array<Mat4>) {
val temp = cache[this.id]
parent.times(value, temp) // TODO: don't multiply them on the rendering thread
fun pack(parent: Mat4) {
parent.times(value, value)
for (child in array) {
child.pack(this.value)
}
}
fun pack(buffer: FloatBuffer) {
val array = value.array
val offset = this.id * Mat4.length
for (index in 0 until Mat4.length) {
buffer.put(offset + index, temp.array[index])
buffer.put(offset + index, array[index])
}
for (child in array) {
child.pack(buffer, temp, cache)
for (child in this.array) {
child.pack(buffer)
}
}