gui: replace meshes Vec2t<*> with Vec2 or Vec2i

This reduces memory allocation rate
This commit is contained in:
Bixilon 2023-05-28 19:54:19 +02:00
parent f526d99441
commit 5fd52fb192
No known key found for this signature in database
GPG Key ID: 5CAD791931B09AC4
6 changed files with 56 additions and 28 deletions

View File

@ -15,7 +15,6 @@ package de.bixilon.minosoft.gui.rendering.font
import de.bixilon.kotlinglm.vec2.Vec2 import de.bixilon.kotlinglm.vec2.Vec2
import de.bixilon.kotlinglm.vec2.Vec2i import de.bixilon.kotlinglm.vec2.Vec2i
import de.bixilon.kotlinglm.vec2.Vec2t
import de.bixilon.kutil.math.simple.FloatMath.ceil import de.bixilon.kutil.math.simple.FloatMath.ceil
import de.bixilon.minosoft.data.text.formatting.color.RGBColor import de.bixilon.minosoft.data.text.formatting.color.RGBColor
import de.bixilon.minosoft.gui.rendering.RenderContext import de.bixilon.minosoft.gui.rendering.RenderContext
@ -47,12 +46,12 @@ class CharData(
_render(position, color, false, italic, bold, strikethrough, underlined, consumer, options, scale) _render(position, color, false, italic, bold, strikethrough, underlined, consumer, options, scale)
} }
private fun GUIVertexConsumer.addQuad(start: Vec2t<*>, end: Vec2t<*>, texture: AbstractTexture, uvStart: Vec2, uvEnd: Vec2, italic: Boolean, tint: RGBColor, options: GUIVertexOptions?) { private fun GUIVertexConsumer.addQuad(start: Vec2, end: Vec2, texture: AbstractTexture, uvStart: Vec2, uvEnd: Vec2, italic: Boolean, tint: RGBColor, options: GUIVertexOptions?) {
val italicOffset = if (italic) (end.y.toFloat() - start.y.toFloat()) / Font.CHAR_HEIGHT.toFloat() * ITALIC_OFFSET else 0.0f val italicOffset = if (italic) (end.y - start.y) / Font.CHAR_HEIGHT.toFloat() * ITALIC_OFFSET else 0.0f
val positions = arrayOf( val positions = arrayOf(
Vec2(start.x.toFloat() + italicOffset, start.y), Vec2(start.x + italicOffset, start.y),
Vec2(end.x.toFloat() + italicOffset, start.y), Vec2(end.x + italicOffset, start.y),
end, end,
Vec2(start.x, end.y), Vec2(start.x, end.y),
) )
@ -107,7 +106,7 @@ class CharData(
} }
if (underlined) { if (underlined) {
vertexConsumer.addQuad(startPosition + Vec2i(-horizontalSpacing, charHeight), Vec2i(endPosition.x + boldOffset + horizontalSpacing, startPosition.y + charHeight + verticalSpacing / 2.0f), whiteTexture.texture, whiteTexture.uvStart, whiteTexture.uvEnd, italic, color, options) vertexConsumer.addQuad(startPosition + Vec2(-horizontalSpacing, charHeight), Vec2(endPosition.x + boldOffset + horizontalSpacing, startPosition.y + charHeight + verticalSpacing / 2.0f), whiteTexture.texture, whiteTexture.uvStart, whiteTexture.uvEnd, italic, color, options)
} }
// ToDo: Obfuscated // ToDo: Obfuscated

View File

@ -15,7 +15,6 @@ package de.bixilon.minosoft.gui.rendering.font
import de.bixilon.kotlinglm.mat4x4.Mat4 import de.bixilon.kotlinglm.mat4x4.Mat4
import de.bixilon.kotlinglm.vec2.Vec2 import de.bixilon.kotlinglm.vec2.Vec2
import de.bixilon.kotlinglm.vec2.Vec2t
import de.bixilon.minosoft.data.text.formatting.color.RGBColor import de.bixilon.minosoft.data.text.formatting.color.RGBColor
import de.bixilon.minosoft.gui.rendering.font.renderer.ChatComponentRenderer import de.bixilon.minosoft.gui.rendering.font.renderer.ChatComponentRenderer
import de.bixilon.minosoft.gui.rendering.gui.mesh.GUIMeshCache import de.bixilon.minosoft.gui.rendering.gui.mesh.GUIMeshCache
@ -29,8 +28,8 @@ import de.bixilon.minosoft.gui.rendering.world.mesh.SingleWorldMesh
class WorldGUIConsumer(val mesh: SingleWorldMesh, val transform: Mat4, val light: Int) : GUIVertexConsumer { class WorldGUIConsumer(val mesh: SingleWorldMesh, val transform: Mat4, val light: Int) : GUIVertexConsumer {
override val order: Array<Pair<Int, Int>> get() = mesh.order override val order: Array<Pair<Int, Int>> get() = mesh.order
override fun addVertex(position: Vec2t<*>, texture: ShaderIdentifiable, uv: Vec2, tint: RGBColor, options: GUIVertexOptions?) { override fun addVertex(position: Vec2, texture: ShaderIdentifiable, uv: Vec2, tint: RGBColor, options: GUIVertexOptions?) {
val transformed = transform.fastTimes(position.x.toFloat() / ChatComponentRenderer.TEXT_BLOCK_RESOLUTION, -position.y.toFloat() / ChatComponentRenderer.TEXT_BLOCK_RESOLUTION) val transformed = transform.fastTimes(position.x / ChatComponentRenderer.TEXT_BLOCK_RESOLUTION, -position.y / ChatComponentRenderer.TEXT_BLOCK_RESOLUTION)
mesh.addVertex(transformed, uv, texture as AbstractTexture, tint.rgb, light) mesh.addVertex(transformed, uv, texture as AbstractTexture, tint.rgb, light)
} }

View File

@ -14,7 +14,6 @@
package de.bixilon.minosoft.gui.rendering.gui.mesh package de.bixilon.minosoft.gui.rendering.gui.mesh
import de.bixilon.kotlinglm.vec2.Vec2 import de.bixilon.kotlinglm.vec2.Vec2
import de.bixilon.kotlinglm.vec2.Vec2t
import de.bixilon.kutil.collections.primitive.floats.AbstractFloatList import de.bixilon.kutil.collections.primitive.floats.AbstractFloatList
import de.bixilon.minosoft.data.text.formatting.color.RGBColor import de.bixilon.minosoft.data.text.formatting.color.RGBColor
import de.bixilon.minosoft.gui.rendering.RenderContext import de.bixilon.minosoft.gui.rendering.RenderContext
@ -29,7 +28,7 @@ class GUIMesh(
data: AbstractFloatList, data: AbstractFloatList,
) : Mesh(context, GUIMeshStruct, initialCacheSize = 40000, clearOnLoad = false, data = data), GUIVertexConsumer { ) : Mesh(context, GUIMeshStruct, initialCacheSize = 40000, clearOnLoad = false, data = data), GUIVertexConsumer {
override fun addVertex(position: Vec2t<*>, texture: ShaderIdentifiable, uv: Vec2, tint: RGBColor, options: GUIVertexOptions?) { override fun addVertex(position: Vec2, texture: ShaderIdentifiable, uv: Vec2, tint: RGBColor, options: GUIVertexOptions?) {
addVertex(data, halfSize, position, texture, uv, tint, options) addVertex(data, halfSize, position, texture, uv, tint, options)
} }
@ -49,12 +48,15 @@ class GUIMesh(
companion object { companion object {
fun transformPosition(position: Vec2, halfSize: Vec2): Vec2 { fun transformPosition(position: Vec2, halfSize: Vec2): Vec2 {
val pixel = position / halfSize val res = Vec2(position)
return Vec2(pixel.x - 1.0f, 1.0f - pixel.y) res /= halfSize
res.x -= 1.0f
res.y = 1.0f - res.y
return res
} }
fun addVertex(data: AbstractFloatList, halfSize: Vec2, position: Vec2t<*>, texture: ShaderIdentifiable, uv: Vec2, tint: RGBColor, options: GUIVertexOptions?) { fun addVertex(data: AbstractFloatList, halfSize: Vec2, position: Vec2, texture: ShaderIdentifiable, uv: Vec2, tint: RGBColor, options: GUIVertexOptions?) {
val outPosition = transformPosition(Vec2(position), halfSize) val outPosition = transformPosition(position, halfSize)
var color = tint.rgba var color = tint.rgba
if (options != null) { if (options != null) {

View File

@ -15,7 +15,6 @@ package de.bixilon.minosoft.gui.rendering.gui.mesh
import de.bixilon.kotlinglm.vec2.Vec2 import de.bixilon.kotlinglm.vec2.Vec2
import de.bixilon.kotlinglm.vec2.Vec2i import de.bixilon.kotlinglm.vec2.Vec2i
import de.bixilon.kotlinglm.vec2.Vec2t
import de.bixilon.kutil.collections.primitive.floats.AbstractFloatList import de.bixilon.kutil.collections.primitive.floats.AbstractFloatList
import de.bixilon.kutil.collections.primitive.floats.HeapArrayFloatList import de.bixilon.kutil.collections.primitive.floats.HeapArrayFloatList
import de.bixilon.minosoft.data.text.formatting.color.RGBColor import de.bixilon.minosoft.data.text.formatting.color.RGBColor
@ -40,7 +39,7 @@ class GUIMeshCache(
} }
} }
override fun addVertex(position: Vec2t<*>, texture: ShaderIdentifiable, uv: Vec2, tint: RGBColor, options: GUIVertexOptions?) { override fun addVertex(position: Vec2, texture: ShaderIdentifiable, uv: Vec2, tint: RGBColor, options: GUIVertexOptions?) {
GUIMesh.addVertex(data, halfSize, position, texture, uv, tint, options) GUIMesh.addVertex(data, halfSize, position, texture, uv, tint, options)
revision++ revision++
} }

View File

@ -14,7 +14,7 @@
package de.bixilon.minosoft.gui.rendering.gui.mesh package de.bixilon.minosoft.gui.rendering.gui.mesh
import de.bixilon.kotlinglm.vec2.Vec2 import de.bixilon.kotlinglm.vec2.Vec2
import de.bixilon.kotlinglm.vec2.Vec2t import de.bixilon.kotlinglm.vec2.Vec2i
import de.bixilon.minosoft.data.text.formatting.color.RGBColor import de.bixilon.minosoft.data.text.formatting.color.RGBColor
import de.bixilon.minosoft.gui.rendering.gui.atlas.TextureLike import de.bixilon.minosoft.gui.rendering.gui.atlas.TextureLike
import de.bixilon.minosoft.gui.rendering.system.base.texture.ShaderIdentifiable import de.bixilon.minosoft.gui.rendering.system.base.texture.ShaderIdentifiable
@ -22,9 +22,12 @@ import de.bixilon.minosoft.gui.rendering.system.base.texture.ShaderIdentifiable
interface GUIVertexConsumer { interface GUIVertexConsumer {
val order: Array<Pair<Int, Int>> val order: Array<Pair<Int, Int>>
fun addVertex(position: Vec2t<*>, texture: ShaderIdentifiable, uv: Vec2, tint: RGBColor, options: GUIVertexOptions?) fun addVertex(position: Vec2, texture: ShaderIdentifiable, uv: Vec2, tint: RGBColor, options: GUIVertexOptions?)
fun addVertex(position: Vec2i, texture: ShaderIdentifiable, uv: Vec2, tint: RGBColor, options: GUIVertexOptions?) {
addVertex(Vec2(position), texture, uv, tint, options)
}
fun addQuad(start: Vec2t<*>, end: Vec2t<*>, texture: ShaderIdentifiable, uvStart: Vec2 = Vec2(0.0f, 0.0f), uvEnd: Vec2 = Vec2(1.0f, 1.0f), tint: RGBColor, options: GUIVertexOptions?) { fun addQuad(start: Vec2, end: Vec2, texture: ShaderIdentifiable, uvStart: Vec2 = UV_START, uvEnd: Vec2 = UV_END, tint: RGBColor, options: GUIVertexOptions?) {
val positions = arrayOf( val positions = arrayOf(
start, start,
Vec2(end.x, start.y), Vec2(end.x, start.y),
@ -43,11 +46,39 @@ interface GUIVertexConsumer {
} }
} }
fun addQuad(start: Vec2t<*>, end: Vec2t<*>, texture: TextureLike, tint: RGBColor, options: GUIVertexOptions?) { fun addQuad(start: Vec2i, end: Vec2i, texture: ShaderIdentifiable, uvStart: Vec2 = UV_START, uvEnd: Vec2 = UV_END, tint: RGBColor, options: GUIVertexOptions?) {
val positions = arrayOf(
Vec2(start),
Vec2(end.x, start.y),
Vec2(end),
Vec2(start.x, end.y),
)
val texturePositions = arrayOf(
Vec2(uvEnd.x, uvStart.y),
uvStart,
Vec2(uvStart.x, uvEnd.y),
uvEnd,
)
for ((vertexIndex, textureIndex) in order) {
addVertex(positions[vertexIndex], texture, texturePositions[textureIndex], tint, options)
}
}
fun addQuad(start: Vec2, end: Vec2, texture: TextureLike, tint: RGBColor, options: GUIVertexOptions?) {
addQuad(start, end, texture.texture, texture.uvStart, texture.uvEnd, tint, options)
}
fun addQuad(start: Vec2i, end: Vec2i, texture: TextureLike, tint: RGBColor, options: GUIVertexOptions?) {
addQuad(start, end, texture.texture, texture.uvStart, texture.uvEnd, tint, options) addQuad(start, end, texture.texture, texture.uvStart, texture.uvEnd, tint, options)
} }
fun addCache(cache: GUIMeshCache) fun addCache(cache: GUIMeshCache)
fun ensureSize(size: Int) fun ensureSize(size: Int)
companion object {
val UV_START = Vec2(0.0f, 0.0f)
val UV_END = Vec2(1.0f, 1.0f)
}
} }

View File

@ -27,9 +27,7 @@ class SingleWorldMesh(context: RenderContext, initialCacheSize: Int, onDemand: B
fun addVertex(position: FloatArray, uv: Vec2, texture: AbstractTexture, tintColor: Int, light: Int) { fun addVertex(position: FloatArray, uv: Vec2, texture: AbstractTexture, tintColor: Int, light: Int) {
data.ensureSize(WorldMeshStruct.FLOATS_PER_VERTEX) data.ensureSize(WorldMeshStruct.FLOATS_PER_VERTEX)
val transformedUV = texture.renderData.transformUV(uv).array val transformedUV = texture.renderData.transformUV(uv).array
data.add(position[0]) data.add(position)
data.add(position[1])
data.add(position[2])
data.add(transformedUV) data.add(transformedUV)
data.add(texture.renderData.shaderTextureId.buffer()) data.add(texture.renderData.shaderTextureId.buffer())
data.add((tintColor or (light shl 24)).buffer()) data.add((tintColor or (light shl 24)).buffer())