Use GL_QUADS, improve world render performance

- Downgrade to OpenGL 3.0 to use GL_QUADS
 - Use GL_QUADS in HUDRenderer and WorldRenderer (improves fps, reduces gpu load, reduces memory usage)
 - Deprecate PrimitiveTypes.QUADS (it is deprecated in OpenGL too)
 - Improve performance in BakedFace
This commit is contained in:
Bixilon 2021-11-10 17:35:38 +01:00
parent b811a95059
commit 71ebcab943
No known key found for this signature in database
GPG Key ID: 5CAD791931B09AC4
12 changed files with 43 additions and 28 deletions

View File

@ -77,7 +77,7 @@ class WorldRenderer(
val blockState1 = connection.registries.blockRegistry["grass_block"]?.defaultState
val blockState2 = connection.registries.blockRegistry["oak_fence"]!!.defaultState.withProperties(BlockProperties.MULTIPART_SOUTH to MultipartDirectionParser.SIDE, BlockProperties.MULTIPART_NORTH to MultipartDirectionParser.SIDE, BlockProperties.MULTIPART_EAST to MultipartDirectionParser.SIDE, BlockProperties.MULTIPART_WEST to MultipartDirectionParser.SIDE)
val section = ChunkSection(Array(4096) {
if (it < 256) return@Array blockState2 else return@Array null
if (it < 4096) return@Array blockState2 else return@Array null
when (random.nextInt(3)) {
1 -> blockState1
2 -> blockState2
@ -87,10 +87,11 @@ class WorldRenderer(
//val section = ChunkSection(Array(4096) { if (it < 1) blockState else null })
mesh = sectionPreparer.prepare(section)
/*
for (i in 0 until 1000)
mesh = sectionPreparer.prepare(section)
// for (i in 0 until 1000)
mesh = sectionPreparer.prepare(section)
/*
Log.log(LogMessageType.OTHER, LogLevels.WARN){"Culling now..."}
val culledMesh = culledPreparer.prepare(section)

View File

@ -16,15 +16,16 @@ package de.bixilon.minosoft.gui.rendering.block.mesh
import de.bixilon.minosoft.data.text.RGBColor
import de.bixilon.minosoft.gui.rendering.RenderConstants
import de.bixilon.minosoft.gui.rendering.RenderWindow
import de.bixilon.minosoft.gui.rendering.system.base.buffer.vertex.PrimitiveTypes
import de.bixilon.minosoft.gui.rendering.system.base.texture.texture.AbstractTexture
import de.bixilon.minosoft.gui.rendering.util.mesh.Mesh
import de.bixilon.minosoft.gui.rendering.util.mesh.MeshStruct
import glm_.vec2.Vec2
import glm_.vec3.Vec3
class ChunkSectionMesh(renderWindow: RenderWindow) : Mesh(renderWindow, SectionArrayMeshStruct, initialCacheSize = 100000) {
class ChunkSectionMesh(renderWindow: RenderWindow) : Mesh(renderWindow, SectionArrayMeshStruct, PrimitiveTypes.QUAD, initialCacheSize = 100000) {
fun addVertex(position: Vec3, uv: Vec2, texture: AbstractTexture, tintColor: RGBColor?, light: Int) {
fun addVertex(position: FloatArray, uv: Vec2, texture: AbstractTexture, tintColor: RGBColor?, light: Int) {
//val texture = renderWindow.WHITE_TEXTURE.texture
val textureLayer = if (RenderConstants.FORCE_DEBUG_TEXTURE) {
RenderConstants.DEBUG_TEXTURE_ID
@ -34,9 +35,9 @@ class ChunkSectionMesh(renderWindow: RenderWindow) : Mesh(renderWindow, SectionA
val transformedUV = texture.renderData?.transformUV(uv) ?: uv
data.addAll(
floatArrayOf(
position.x,
position.y,
position.z,
position[0],
position[1],
position[2],
transformedUV.x,
transformedUV.y,
Float.fromBits(textureLayer),

View File

@ -65,7 +65,7 @@ class CharData(
uvEnd,
)
for ((vertexIndex, textureIndex) in Mesh.QUAD_DRAW_ODER) {
for ((vertexIndex, textureIndex) in Mesh.QUAD_TO_QUAD_ORDER) {
addVertex(positions[vertexIndex], z, texture, texturePositions[textureIndex], tint, options)
}
}

View File

@ -16,6 +16,7 @@ package de.bixilon.minosoft.gui.rendering.gui.mesh
import de.bixilon.minosoft.data.text.RGBColor
import de.bixilon.minosoft.gui.rendering.RenderConstants
import de.bixilon.minosoft.gui.rendering.RenderWindow
import de.bixilon.minosoft.gui.rendering.system.base.buffer.vertex.PrimitiveTypes
import de.bixilon.minosoft.gui.rendering.system.base.texture.texture.AbstractTexture
import de.bixilon.minosoft.gui.rendering.util.mesh.Mesh
import de.bixilon.minosoft.gui.rendering.util.mesh.MeshStruct
@ -28,7 +29,7 @@ import glm_.vec4.Vec4
class GUIMesh(
renderWindow: RenderWindow,
val matrix: Mat4,
) : Mesh(renderWindow, GUIMeshStruct, initialCacheSize = 40000), GUIVertexConsumer {
) : Mesh(renderWindow, GUIMeshStruct, PrimitiveTypes.QUAD, initialCacheSize = 40000), GUIVertexConsumer {
override fun addVertex(position: Vec2t<*>, z: Int, texture: AbstractTexture, uv: Vec2, tint: RGBColor, options: GUIVertexOptions?) {
data.addAll(createVertex(matrix, position, z, texture, uv, tint, options))

View File

@ -26,8 +26,8 @@ interface GUIVertexConsumer {
fun addQuad(start: Vec2t<*>, end: Vec2t<*>, z: Int, texture: AbstractTexture, uvStart: Vec2, uvEnd: Vec2, tint: RGBColor, options: GUIVertexOptions?) {
val positions = arrayOf(
Vec2(start.x.toFloat(), start.y),
Vec2(end.x.toFloat(), start.y),
start,
Vec2(end.x, start.y),
end,
Vec2(start.x, end.y),
)
@ -38,7 +38,7 @@ interface GUIVertexConsumer {
uvEnd,
)
for ((vertexIndex, textureIndex) in Mesh.QUAD_DRAW_ODER) {
for ((vertexIndex, textureIndex) in Mesh.QUAD_TO_QUAD_ORDER) {
addVertex(positions[vertexIndex], z, texture, texturePositions[textureIndex], tint, options)
}
}

View File

@ -35,7 +35,7 @@ class BakedBlockStateModel(
}
override fun singleRender(position: Vec3i, mesh: ChunkSectionMesh, random: Random, neighbours: Array<BlockState?>, light: Int, ambientLight: FloatArray) {
val floatPosition = position.toVec3()
val floatPosition = position.toVec3().array
for ((index, faces) in faces.withIndex()) {
val direction = Directions.VALUES[index]
val neighbour = neighbours[index]?.model

View File

@ -33,10 +33,11 @@ class BakedFace(
val texture: AbstractTexture,
val touching: Boolean,
) {
fun singleRender(position: Vec3, mesh: ChunkSectionMesh, light: Int, ambientLight: FloatArray) {
fun singleRender(position: FloatArray, mesh: ChunkSectionMesh, light: Int, ambientLight: FloatArray) {
// ToDo: Ambient light
for ((index, textureIndex) in Mesh.QUAD_DRAW_ODER) {
mesh.addVertex(positions[index] + position, uv[textureIndex], texture, null, light)
for ((index, textureIndex) in Mesh.QUAD_TO_QUAD_ORDER) {
val indexPosition = positions[index].array
mesh.addVertex(floatArrayOf(indexPosition[0] + position[0], indexPosition[1] + position[1], indexPosition[2] + position[2]), uv[textureIndex], texture, null, light)
}
}
@ -64,8 +65,9 @@ class BakedFace(
uv[2] * uvMultiplier,
uv[3] * uvMultiplier,
)
for ((index, textureIndex) in Mesh.QUAD_DRAW_ODER) {
mesh.addVertex(positions[index], uv[textureIndex], texture, null, light)
for ((index, textureIndex) in Mesh.QUAD_TO_QUAD_ORDER) {
// ToDo
mesh.addVertex(positions[index].array, uv[textureIndex], texture, null, light)
}
}
}

View File

@ -17,6 +17,8 @@ enum class PrimitiveTypes(val vertices: Int) {
POINT(1),
LINE(2),
TRIANGLE(4),
@Deprecated("Removed in OpenGL 3.1+")
QUAD(4),
;
}

View File

@ -129,8 +129,8 @@ class GLFWWindow(
glfwDefaultWindowHints()
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3)
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3)
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE)
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 0)
// glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE)
glfwWindowHint(GLFW_VISIBLE, false.glfw)

View File

@ -16,9 +16,10 @@ package de.bixilon.minosoft.gui.rendering.util.mesh
import de.bixilon.minosoft.data.text.ChatColors
import de.bixilon.minosoft.data.text.RGBColor
import de.bixilon.minosoft.gui.rendering.RenderWindow
import de.bixilon.minosoft.gui.rendering.system.base.buffer.vertex.PrimitiveTypes
import glm_.vec3.Vec3
open class GenericColorMesh(renderWindow: RenderWindow) : Mesh(renderWindow, GenericColorMeshStruct) {
open class GenericColorMesh(renderWindow: RenderWindow, primitiveType: PrimitiveTypes = PrimitiveTypes.TRIANGLE) : Mesh(renderWindow, GenericColorMeshStruct, primitiveType) {
fun addVertex(position: Vec3, color: RGBColor?) {
data.addAll(

View File

@ -17,13 +17,14 @@ import de.bixilon.minosoft.data.registries.AABB
import de.bixilon.minosoft.data.registries.VoxelShape
import de.bixilon.minosoft.data.text.RGBColor
import de.bixilon.minosoft.gui.rendering.RenderWindow
import de.bixilon.minosoft.gui.rendering.system.base.buffer.vertex.PrimitiveTypes
import de.bixilon.minosoft.gui.rendering.util.vec.vec3.Vec3Util.EMPTY
import de.bixilon.minosoft.util.BitByte.isBit
import de.bixilon.minosoft.util.MMath.positiveNegative
import glm_.vec3.Vec3
import glm_.vec3.Vec3d
open class LineMesh(renderWindow: RenderWindow) : GenericColorMesh(renderWindow) {
open class LineMesh(renderWindow: RenderWindow) : GenericColorMesh(renderWindow, PrimitiveTypes.QUAD) {
fun drawLine(start: Vec3, end: Vec3, lineWidth: Float, color: RGBColor) {
val direction = (end - start).normalize()
@ -43,13 +44,13 @@ open class LineMesh(renderWindow: RenderWindow) : GenericColorMesh(renderWindow)
val halfLineWidth = lineWidth / 2
val normal1Multiplier = invertNormal1.positiveNegative
val normal2Multiplier = invertNormal2.positiveNegative
val positions = listOf(
val positions = arrayOf(
start + normal2 * normal2Multiplier * halfLineWidth - direction * halfLineWidth,
start + normal1 * normal1Multiplier * halfLineWidth - direction * halfLineWidth,
end + normal1 * normal1Multiplier * halfLineWidth + direction * halfLineWidth,
end + normal2 * normal2Multiplier * halfLineWidth + direction * halfLineWidth,
)
for ((_, positionIndex) in Mesh.QUAD_DRAW_ODER) {
for ((_, positionIndex) in QUAD_TO_QUAD_ORDER) {
addVertex(positions[positionIndex], color)
}
}

View File

@ -69,7 +69,7 @@ abstract class Mesh(
}
companion object {
val QUAD_DRAW_ODER = arrayOf(
val TRIANGLE_TO_QUAD_ORDER = arrayOf(
0 to 1,
3 to 2,
2 to 3,
@ -77,6 +77,12 @@ abstract class Mesh(
1 to 0,
0 to 1,
)
val QUAD_TO_QUAD_ORDER = arrayOf(
0 to 1,
3 to 2,
2 to 3,
1 to 0,
)
fun addQuad(start: Vec3, end: Vec3, uvStart: Vec2 = Vec2(0.0f, 0.0f), uvEnd: Vec2 = Vec2(1.0f, 1.0f), vertexConsumer: (position: Vec3, uv: Vec2) -> Unit) {
@ -93,7 +99,7 @@ abstract class Mesh(
uvEnd,
)
for ((vertexIndex, textureIndex) in QUAD_DRAW_ODER) {
for ((vertexIndex, textureIndex) in TRIANGLE_TO_QUAD_ORDER) {
vertexConsumer.invoke(positions[vertexIndex], texturePositions[textureIndex])
}
}