rendering: reduce amount of floats per vertex, improve preparing performance

This commit is contained in:
Bixilon 2021-11-10 19:57:12 +01:00
parent 71ebcab943
commit 7d501bfc6b
No known key found for this signature in database
GPG Key ID: 5CAD791931B09AC4
13 changed files with 66 additions and 94 deletions

View File

@ -60,8 +60,6 @@ object RenderConstants {
val DEBUG_TEXTURE_RESOURCE_LOCATION = ResourceLocation("minosoft:textures/debug.png")
const val DEBUG_TEXTURE_ID = 0 // always add the debug texture to the texture array first to ensure the id is 0
const val FORCE_DEBUG_TEXTURE = false
val PIXEL_UV_PIXEL_ADD = Vec2(0, 0.1f)

View File

@ -18,8 +18,6 @@ import de.bixilon.minosoft.data.assets.Resources
import de.bixilon.minosoft.data.direction.Directions
import de.bixilon.minosoft.data.direction.FakeDirection
import de.bixilon.minosoft.data.registries.ResourceLocation
import de.bixilon.minosoft.data.registries.blocks.properties.BlockProperties
import de.bixilon.minosoft.data.registries.blocks.properties.MultipartDirectionParser
import de.bixilon.minosoft.data.world.ChunkSection
import de.bixilon.minosoft.data.world.World
import de.bixilon.minosoft.gui.rendering.RenderWindow
@ -74,10 +72,9 @@ class WorldRenderer(
val random = Random(0L)
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 blockState1 = connection.registries.blockRegistry["redstone_block"]?.defaultState
val blockState2 = connection.registries.blockRegistry["diamond_block"]!!.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 < 4096) return@Array blockState2 else return@Array null
when (random.nextInt(3)) {
1 -> blockState1
2 -> blockState2
@ -88,8 +85,8 @@ class WorldRenderer(
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..."}

View File

@ -23,38 +23,27 @@ import de.bixilon.minosoft.gui.rendering.util.mesh.MeshStruct
import glm_.vec2.Vec2
import glm_.vec3.Vec3
class ChunkSectionMesh(renderWindow: RenderWindow) : Mesh(renderWindow, SectionArrayMeshStruct, PrimitiveTypes.QUAD, initialCacheSize = 100000) {
class ChunkSectionMesh(renderWindow: RenderWindow) : Mesh(renderWindow, SectionArrayMeshStruct, PrimitiveTypes.QUAD, initialCacheSize = 200000) {
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
} else {
texture.renderData?.layer ?: RenderConstants.DEBUG_TEXTURE_ID
}
val transformedUV = texture.renderData?.transformUV(uv) ?: uv
data.addAll(
floatArrayOf(
position[0],
position[1],
position[2],
transformedUV.x,
transformedUV.y,
Float.fromBits(textureLayer),
Float.fromBits(texture.renderData?.animationData ?: -1),
Float.fromBits(tintColor?.rgb ?: 0xFFFFFF), // white
Float.fromBits(light),
))
data.addAll(floatArrayOf(
position[0],
position[1],
position[2],
transformedUV.x,
transformedUV.y,
Float.fromBits(texture.renderData?.shaderTextureId ?: RenderConstants.DEBUG_TEXTURE_ID),
Float.fromBits((tintColor?.rgb ?: 0xFFFFFF) or (light shl 24)), // white
))
}
data class SectionArrayMeshStruct(
val position: Vec3,
val uv: Vec2,
val textureLayer: Int,
val animationId: Int,
val tintColor: RGBColor,
val light: Int,
val indexLayerAnimation: Int,
val tintLight: Int,
) {
companion object : MeshStruct(SectionArrayMeshStruct::class)
}

View File

@ -42,7 +42,7 @@ class GUIMesh(
data class GUIMeshStruct(
val position: Vec3,
val uv: Vec2,
val textureLayer: Int,
val indexLayerAnimation: Int,
val tintColor: RGBColor,
) {
companion object : MeshStruct(GUIMeshStruct::class)
@ -69,7 +69,7 @@ class GUIMesh(
BASE_Z + Z_MULTIPLIER * z,
uv.x,
uv.y,
Float.fromBits(texture.renderData?.layer ?: RenderConstants.DEBUG_TEXTURE_ID),
Float.fromBits(texture.renderData?.shaderTextureId ?: RenderConstants.DEBUG_TEXTURE_ID),
Float.fromBits(color.rgba),
)
}

View File

@ -27,12 +27,6 @@ import glm_.vec3.Vec3d
class ParticleMesh(renderWindow: RenderWindow, initialCacheSize: Int) : Mesh(renderWindow, ParticleMeshStruct, PrimitiveTypes.POINT, initialCacheSize) {
fun addVertex(position: Vec3d, scale: Float, texture: AbstractTexture, tintColor: RGBColor, uvMin: Vec2 = Vec2(0.0f, 0.0f), uvMax: Vec2 = Vec2(1.0f, 1.0f)) {
val textureLayer = if (RenderConstants.FORCE_DEBUG_TEXTURE) {
RenderConstants.DEBUG_TEXTURE_ID
} else {
texture.renderData?.layer ?: RenderConstants.DEBUG_TEXTURE_ID
}
val minTransformedUV = texture.renderData?.transformUV(uvMin) ?: uvMin
val maxTransformedUV = texture.renderData?.transformUV(uvMax) ?: uvMax
data.addAll(
@ -44,8 +38,7 @@ class ParticleMesh(renderWindow: RenderWindow, initialCacheSize: Int) : Mesh(ren
minTransformedUV.y,
maxTransformedUV.x,
maxTransformedUV.y,
Float.fromBits(textureLayer),
Float.fromBits(texture.renderData?.animationData ?: -1),
Float.fromBits(texture.renderData?.shaderTextureId ?: RenderConstants.DEBUG_TEXTURE_ID),
scale,
Float.fromBits(tintColor.rgba),
))
@ -56,8 +49,7 @@ class ParticleMesh(renderWindow: RenderWindow, initialCacheSize: Int) : Mesh(ren
val position: Vec3,
val minUVCoordinates: Vec2,
val maxUVCoordinates: Vec2,
val textureLayer: Int,
val animationId: Int,
val indexLayerAnimation: Int,
val scale: Float,
val tintColor: RGBColor,
) {

View File

@ -16,7 +16,7 @@ package de.bixilon.minosoft.gui.rendering.system.base.texture.texture
import glm_.vec2.Vec2
interface TextureRenderData {
val layer: Int
val shaderTextureId: Int
val animationData: Int
fun transformUV(end: Vec2): Vec2

View File

@ -68,8 +68,8 @@ class OpenGLSpriteAnimator : SpriteAnimator {
val arrayOffset = textureAnimation.texture.renderData!!.animationData * INTS_PER_ANIMATED_TEXTURE
uniformBuffer.data[arrayOffset] = currentFrame.texture.renderData!!.layer
uniformBuffer.data[arrayOffset + 1] = nextFrame.texture.renderData!!.layer
uniformBuffer.data[arrayOffset] = currentFrame.texture.renderData!!.shaderTextureId
uniformBuffer.data[arrayOffset + 1] = nextFrame.texture.renderData!!.shaderTextureId
uniformBuffer.data[arrayOffset + 2] = interpolation.toInt()
}

View File

@ -22,7 +22,7 @@ class OpenGLTextureData(
val uvEnd: Vec2?,
override val animationData: Int = -1,
) : TextureRenderData {
override val layer: Int = (array shl 24) or (index)
override val shaderTextureId: Int = (array shl 28) or (index shl 12) or (animationData + 1)
override fun transformUV(end: Vec2): Vec2 {
if (uvEnd == null) {

View File

@ -23,12 +23,6 @@ import glm_.vec3.Vec3
open class SimpleTextureMesh(renderWindow: RenderWindow) : Mesh(renderWindow, SimpleTextureMeshStruct, initialCacheSize = 2 * 3 * SimpleTextureMeshStruct.FLOATS_PER_VERTEX) {
fun addVertex(position: Vec3, texture: AbstractTexture, uv: Vec2, tintColor: RGBColor) {
val textureLayer = if (RenderConstants.FORCE_DEBUG_TEXTURE) {
RenderConstants.DEBUG_TEXTURE_ID
} else {
texture.renderData?.layer ?: RenderConstants.DEBUG_TEXTURE_ID
}
data.addAll(
floatArrayOf(
position.x,
@ -36,8 +30,7 @@ open class SimpleTextureMesh(renderWindow: RenderWindow) : Mesh(renderWindow, Si
position.z,
uv.x,
uv.y,
Float.fromBits(textureLayer),
Float.fromBits(tintColor.rgba),
Float.fromBits(texture.renderData?.shaderTextureId ?: RenderConstants.DEBUG_TEXTURE_ID),
))
}
@ -45,8 +38,7 @@ open class SimpleTextureMesh(renderWindow: RenderWindow) : Mesh(renderWindow, Si
data class SimpleTextureMeshStruct(
val position: Vec3,
val uv: Vec2,
val textureLayer: Int,
val animationId: Int,
val indexLayerAnimation: Int,
) {
companion object : MeshStruct(SimpleTextureMeshStruct::class)
}

View File

@ -15,7 +15,7 @@
layout (location = 0) in vec3 vinPosition;
layout (location = 1) in vec2 vinUV;
layout (location = 2) in uint vinTextureLayer;
layout (location = 2) in uint vinIndexLayerAnimation;
layout (location = 3) in uint vinTintColor;
flat out uint finTextureIndex;
@ -26,7 +26,10 @@ out vec4 finTintColor;
void main() {
gl_Position = vec4(vinPosition.xyz, 1.0f);
finTextureCoordinates = vec3(vinUV, vinTextureLayer & 0xFFFFFFu);
finTextureIndex = vinTextureLayer >> 24u;
// ToDo: Texture animations
finTextureIndex = vinIndexLayerAnimation >> 28u;
finTextureCoordinates = vec3(vinUV, ((vinIndexLayerAnimation >> 12) & 0xFFFFu));
finTintColor = getRGBAColor(vinTintColor);
}

View File

@ -16,11 +16,10 @@
layout (location = 0) in vec3 vinPosition;
layout (location = 1) in vec2 vinMinUVCoordinates;
layout (location = 2) in vec2 vinMaxUVCoordinates;
layout (location = 3) in uint vinTextureLayer;
layout (location = 4) in int vinAnimationIndex;
layout (location = 3) in uint vinIndexLayerAnimation;
layout (location = 5) in float vinScale;
layout (location = 6) in uint vinTintColor;
layout (location = 4) in float vinScale;
layout (location = 5) in uint vinTintColor;
#include "minosoft:animation"
@ -50,24 +49,29 @@ void main() {
ginVertex.scale = vinScale;
ginVertex.tintColor = getRGBAColor(vinTintColor);
if (vinAnimationIndex == -1) {
ginVertex.textureIndex1 = vinTextureLayer >> 24u;
ginVertex.textureLayer1 = vinTextureLayer & 0xFFFFFFu;
uint animationIndex = vinIndexLayerAnimation & 0xFFFu;
if (animationIndex == 0u) {
ginVertex.textureIndex1 = vinIndexLayerAnimation >> 28u;
ginVertex.textureLayer1 = ((vinIndexLayerAnimation >> 12) & 0xFFFFu);
ginVertex.interpolation = 0.0f;
return;
}
uvec4 data = uAnimationData[vinAnimationIndex];
uvec4 data = uAnimationData[animationIndex -1u];
uint texture1 = data.x;
uint texture2 = data.y;
uint interpolation = data.z;
ginVertex.textureIndex1 = texture1 >> 24u;
ginVertex.textureLayer1 = texture1 & 0xFFFFFFu;
ginVertex.textureIndex2 = texture2 >> 24u;
ginVertex.textureLayer2 = texture2 & 0xFFFFFFu;
ginVertex.textureIndex1 = texture1 >> 28u;
ginVertex.textureLayer1 = (texture1 >> 12) & 0xFFFFu;
ginVertex.textureIndex2 = texture2 >> 28u;
ginVertex.textureLayer2 = (texture2 >> 12) & 0xFFFFu;
ginVertex.interpolation = interpolation / 100.0f;
}

View File

@ -15,7 +15,7 @@
layout (location = 0) in vec3 vinPosition;
layout (location = 1) in vec2 vinUV;
layout (location = 2) in uint vinTextureLayer;
layout (location = 2) in uint vinIndexLayerAnimation;
layout (location = 3) in uint vinTintColor;
out vec4 finTintColor;
@ -29,7 +29,8 @@ uniform mat4 uSkyViewProjectionMatrix;
void main() {
gl_Position = (uSkyViewProjectionMatrix * vec4(vinPosition, 1.0f)).xyww - vec4(0.0f, 0.0f, 0.000001f, 0.0f);// prevent face fighting
finTextureCoordinates = vec3(vinUV, vinTextureLayer & 0xFFFFFFu);
finTextureIndex = vinTextureLayer >> 24u;
finTextureIndex = vinIndexLayerAnimation >> 28u;
finTextureCoordinates = vec3(vinUV, ((vinIndexLayerAnimation >> 12) & 0xFFFFu));
finTintColor = getRGBAColor(vinTintColor);
}

View File

@ -15,11 +15,8 @@
layout (location = 0) in vec3 vinPosition;
layout (location = 1) in vec2 vinUV;
layout (location = 2) in uint vinTextureLayer;
layout (location = 3) in int vinAnimationIndex;
layout (location = 4) in uint vinTintColor;
layout (location = 5) in uint vinLight;
layout (location = 2) in uint vinIndexLayerAnimation;// texture index (0xF0000000), texture layer (0x0FFFF000), animation index (0x00000FFF)
layout (location = 3) in uint vinTintColorAndLight;// Light (0xFF000000); 3 bytes color (0x00FFFFFF)
flat out uint finTextureIndex1;
out vec3 finTextureCoordinates1;
@ -39,28 +36,27 @@ uniform mat4 uViewProjectionMatrix;
void work() {
gl_Position = uViewProjectionMatrix * vec4(vinPosition, 1.0f);
finTintColor = getRGBColor(vinTintColor) * getLight(vinLight);
finTintColor = getRGBColor(vinTintColorAndLight & 0xFFFFFFu) * getLight(vinTintColorAndLight >> 24u);
if (vinAnimationIndex == -1) {
finTextureIndex1 = vinTextureLayer >> 24u;
finTextureCoordinates1 = vec3(vinUV, (vinTextureLayer & 0xFFFFFFu));
uint animationIndex = vinIndexLayerAnimation & 0xFFFu;
if (animationIndex == 0u) {
finTextureIndex1 = vinIndexLayerAnimation >> 28u;
finTextureCoordinates1 = vec3(vinUV, ((vinIndexLayerAnimation >> 12) & 0xFFFFu));
finInterpolation = 0.0f;
return;
}
uvec4 data = uAnimationData[vinAnimationIndex];
uint firstTexture = data.x;
uint secondTexture = data.y;
uvec4 data = uAnimationData[animationIndex - 1u];
uint texture1 = data.x;
uint texture2 = data.y;
uint interpolation = data.z;
finTextureIndex1 = firstTexture >> 24u;
finTextureCoordinates1 = vec3(vinUV, firstTexture & 0xFFFFFFu);
finTextureIndex1 = texture1 >> 28u;
finTextureCoordinates1 = vec3(vinUV, ((texture1 >> 12) & 0xFFFFu));
finTextureIndex2 = secondTexture >> 24u;
finTextureCoordinates2 = vec3(vinUV, secondTexture & 0xFFFFFFu);
finTextureIndex2 = texture2 >> 28u;
finTextureCoordinates2 = vec3(vinUV, ((texture2 >> 12) & 0xFFFFu));
finInterpolation = interpolation / 100.0f;
}