optimize textures, optimize section preparing

This commit is contained in:
Bixilon 2021-11-06 00:22:21 +01:00
parent 0712db8bfb
commit ec35307a22
No known key found for this signature in database
GPG Key ID: 5CAD791931B09AC4
7 changed files with 32 additions and 12 deletions

View File

@ -19,6 +19,9 @@ import de.bixilon.minosoft.data.world.ChunkSection
import de.bixilon.minosoft.gui.rendering.RenderWindow import de.bixilon.minosoft.gui.rendering.RenderWindow
import de.bixilon.minosoft.gui.rendering.block.mesh.ChunkSectionMesh import de.bixilon.minosoft.gui.rendering.block.mesh.ChunkSectionMesh
import de.bixilon.minosoft.protocol.protocol.ProtocolDefinition import de.bixilon.minosoft.protocol.protocol.ProtocolDefinition
import de.bixilon.minosoft.util.logging.Log
import de.bixilon.minosoft.util.logging.LogLevels
import de.bixilon.minosoft.util.logging.LogMessageType
import glm_.vec3.Vec3i import glm_.vec3.Vec3i
import java.util.* import java.util.*
@ -28,12 +31,14 @@ class SectionPreparer(
fun prepare(section: ChunkSection): ChunkSectionMesh { fun prepare(section: ChunkSection): ChunkSectionMesh {
val startTime = System.nanoTime()
val mesh = ChunkSectionMesh(renderWindow) val mesh = ChunkSectionMesh(renderWindow)
val random = Random(0L)
for (x in 0 until ProtocolDefinition.SECTION_WIDTH_X) { for (x in 0 until ProtocolDefinition.SECTION_WIDTH_X) {
for (y in 0 until ProtocolDefinition.SECTION_HEIGHT_Y) { for (y in 0 until ProtocolDefinition.SECTION_HEIGHT_Y) {
for (z in 0 until ProtocolDefinition.SECTION_WIDTH_Z) { for (z in 0 until ProtocolDefinition.SECTION_WIDTH_Z) {
val block = section.blocks[ChunkSection.getIndex(x, y, z)] val block = section.blocks[ChunkSection.getIndex(x, y, z)] ?: continue
val neighbours: Array<BlockState?> = arrayOfNulls(Directions.VALUES.size) val neighbours: Array<BlockState?> = arrayOfNulls(Directions.VALUES.size)
@ -68,13 +73,18 @@ class SectionPreparer(
} else { } else {
section.blocks[ChunkSection.getIndex(x + 1, y, z)] section.blocks[ChunkSection.getIndex(x + 1, y, z)]
} }
val model = block.model
block?.model?.singleRender(Vec3i(x, y, z), mesh, Random(0L), neighbours, 0xFF, intArrayOf(0xF, 0xF, 0xF, 0xF)) random.setSeed(0L)
model?.singleRender(Vec3i(x, y, z), mesh, random, neighbours, 0xFF, intArrayOf(0xF, 0xF, 0xF, 0xF))
} }
} }
} }
val time = System.nanoTime()
val delta = time - startTime
Log.log(LogMessageType.OTHER, LogLevels.VERBOSE) { "Preparing took ${delta}ns, ${delta / 1000}µs, ${delta / 1000000}ms" }
return mesh return mesh
} }

View File

@ -65,6 +65,7 @@ class WorldRenderer(
val blockState = connection.registries.blockRegistry["diamond_block"]?.defaultState val blockState = connection.registries.blockRegistry["diamond_block"]?.defaultState
val chunk = ChunkSection(Array(4096) { if (it < 4096) blockState else null }) val chunk = ChunkSection(Array(4096) { if (it < 4096) blockState else null })
// for(i in 0 until 100000)
mesh = sectionPreparer.prepare(chunk) mesh = sectionPreparer.prepare(chunk)
mesh.load() mesh.load()
} }

View File

@ -13,7 +13,6 @@
package de.bixilon.minosoft.gui.rendering.block.mesh package de.bixilon.minosoft.gui.rendering.block.mesh
import de.bixilon.minosoft.data.text.ChatColors
import de.bixilon.minosoft.data.text.RGBColor import de.bixilon.minosoft.data.text.RGBColor
import de.bixilon.minosoft.gui.rendering.RenderConstants import de.bixilon.minosoft.gui.rendering.RenderConstants
import de.bixilon.minosoft.gui.rendering.RenderWindow import de.bixilon.minosoft.gui.rendering.RenderWindow
@ -26,8 +25,6 @@ import glm_.vec3.Vec3
class ChunkSectionMesh(renderWindow: RenderWindow) : Mesh(renderWindow, SectionArrayMeshStruct, initialCacheSize = 100000) { class ChunkSectionMesh(renderWindow: RenderWindow) : Mesh(renderWindow, SectionArrayMeshStruct, initialCacheSize = 100000) {
fun addVertex(position: Vec3, uv: Vec2, texture: AbstractTexture, tintColor: RGBColor?, light: Int) { fun addVertex(position: Vec3, uv: Vec2, texture: AbstractTexture, tintColor: RGBColor?, light: Int) {
val color = tintColor ?: ChatColors.WHITE
val textureLayer = if (RenderConstants.FORCE_DEBUG_TEXTURE) { val textureLayer = if (RenderConstants.FORCE_DEBUG_TEXTURE) {
RenderConstants.DEBUG_TEXTURE_ID RenderConstants.DEBUG_TEXTURE_ID
} else { } else {
@ -43,7 +40,7 @@ class ChunkSectionMesh(renderWindow: RenderWindow) : Mesh(renderWindow, SectionA
transformedUV.y, transformedUV.y,
Float.fromBits(textureLayer), Float.fromBits(textureLayer),
Float.fromBits(texture.renderData?.animationData ?: -1), Float.fromBits(texture.renderData?.animationData ?: -1),
Float.fromBits(color.rgb), Float.fromBits(tintColor?.rgb ?: 0xFFFFFF), // white
Float.fromBits(light), Float.fromBits(light),
)) ))
} }

View File

@ -18,7 +18,7 @@ import de.bixilon.minosoft.data.registries.blocks.BlockState
import de.bixilon.minosoft.data.world.light.LightAccessor import de.bixilon.minosoft.data.world.light.LightAccessor
import de.bixilon.minosoft.gui.rendering.block.mesh.ChunkSectionMesh import de.bixilon.minosoft.gui.rendering.block.mesh.ChunkSectionMesh
import de.bixilon.minosoft.gui.rendering.models.FaceSize import de.bixilon.minosoft.gui.rendering.models.FaceSize
import glm_.vec3.Vec3 import de.bixilon.minosoft.gui.rendering.util.vec.vec3.Vec3iUtil.toVec3
import glm_.vec3.Vec3i import glm_.vec3.Vec3i
import java.util.* import java.util.*
@ -31,7 +31,7 @@ class BakedBlockStateModel(
} }
override fun singleRender(position: Vec3i, mesh: ChunkSectionMesh, random: Random, neighbours: Array<BlockState?>, light: Int, ambientLight: IntArray) { override fun singleRender(position: Vec3i, mesh: ChunkSectionMesh, random: Random, neighbours: Array<BlockState?>, light: Int, ambientLight: IntArray) {
val floatPosition = Vec3(position) val floatPosition = position.toVec3()
for ((index, direction) in faces.withIndex()) { for ((index, direction) in faces.withIndex()) {
val neighbour = neighbours[index] val neighbour = neighbours[index]
if (neighbour != null) { if (neighbour != null) {

View File

@ -101,9 +101,12 @@ class OpenGLTextureArray(
} }
val uvEnd = Vec2(texture.size) / arrayResolution val uvEnd: Vec2? = if (texture.size.x == arrayResolution && texture.size.y == arrayResolution) {
null
} else {
Vec2(texture.size) / arrayResolution
}
val singlePixelSize = Vec2(1.0f) / arrayResolution val singlePixelSize = Vec2(1.0f) / arrayResolution
val textureArrayUV = Vec2(texture.size) / arrayResolution
if (texture is SpriteTexture) { if (texture is SpriteTexture) {
val animationIndex = lastAnimationIndex++ val animationIndex = lastAnimationIndex++
@ -122,7 +125,7 @@ class OpenGLTextureArray(
texturesByResolution[arrayId] += texture texturesByResolution[arrayId] += texture
texture.renderData = OpenGLTextureData(arrayId, lastTextureId[arrayId]++, uvEnd, -1) texture.renderData = OpenGLTextureData(arrayId, lastTextureId[arrayId]++, uvEnd, -1)
texture.singlePixelSize = singlePixelSize texture.singlePixelSize = singlePixelSize
texture.textureArrayUV = textureArrayUV texture.textureArrayUV = uvEnd ?: Vec2(1.0f, 1.0f)
} }
} }

View File

@ -19,12 +19,15 @@ import glm_.vec2.Vec2
class OpenGLTextureData( class OpenGLTextureData(
val array: Int, val array: Int,
val index: Int, val index: Int,
val uvEnd: Vec2, val uvEnd: Vec2?,
override val animationData: Int = -1, override val animationData: Int = -1,
) : TextureRenderData { ) : TextureRenderData {
override val layer: Int = (array shl 24) or (index) override val layer: Int = (array shl 24) or (index)
override fun transformUV(end: Vec2): Vec2 { override fun transformUV(end: Vec2): Vec2 {
if (uvEnd == null) {
return end
}
return end * uvEnd return end * uvEnd
} }
} }

View File

@ -14,6 +14,7 @@
package de.bixilon.minosoft.gui.rendering.util.vec.vec3 package de.bixilon.minosoft.gui.rendering.util.vec.vec3
import de.bixilon.minosoft.util.KUtil.toInt import de.bixilon.minosoft.util.KUtil.toInt
import glm_.vec3.Vec3
import glm_.vec3.Vec3i import glm_.vec3.Vec3i
object Vec3iUtil { object Vec3iUtil {
@ -28,6 +29,11 @@ object Vec3iUtil {
get() = Vec3i(Int.MAX_VALUE, Int.MAX_VALUE, Int.MAX_VALUE) get() = Vec3i(Int.MAX_VALUE, Int.MAX_VALUE, Int.MAX_VALUE)
fun Vec3i.toVec3(): Vec3 {
val array = array
return Vec3(floatArrayOf(array[0].toFloat(), array[1].toFloat(), array[2].toFloat()))
}
fun Any?.toVec3i(default: Vec3i? = null): Vec3i { fun Any?.toVec3i(default: Vec3i? = null): Vec3i {
return toVec3iN() ?: default ?: throw IllegalArgumentException("Not a Vec3i: $this") return toVec3iN() ?: default ?: throw IllegalArgumentException("Not a Vec3i: $this")
} }