improve shader mesh struct

This commit is contained in:
Bixilon 2021-07-08 17:47:36 +02:00
parent f5e09b27bb
commit 831b77dc90
No known key found for this signature in database
GPG Key ID: 5CAD791931B09AC4
12 changed files with 53 additions and 33 deletions

View File

@ -23,7 +23,7 @@ import de.bixilon.minosoft.gui.rendering.util.mesh.MeshStruct
import glm_.vec2.Vec2
import glm_.vec3.Vec3
class ChunkSectionArrayMesh(renderWindow: RenderWindow) : Mesh(renderWindow, SectionArrayMeshStruct::class, initialCacheSize = 100000) {
class ChunkSectionArrayMesh(renderWindow: RenderWindow) : Mesh(renderWindow, SectionArrayMeshStruct, initialCacheSize = 100000) {
fun addVertex(position: Vec3, textureCoordinates: Vec2, texture: Texture, tintColor: RGBColor?, light: Int) {
val color = tintColor ?: ChatColors.WHITE

View File

@ -24,7 +24,7 @@ import glm_.vec2.Vec2
import glm_.vec3.Vec3
import glm_.vec3.Vec3d
class ParticleMesh(renderWindow: RenderWindow) : Mesh(renderWindow, ParticleMeshStruct::class, PrimitiveTypes.POINT) {
class ParticleMesh(renderWindow: RenderWindow) : Mesh(renderWindow, ParticleMeshStruct, PrimitiveTypes.POINT) {
fun addVertex(position: Vec3d, scale: Float, texture: Texture, tintColor: RGBColor, uvMin: Vec2 = Vec2(0, 0), uvMax: Vec2 = Vec2(1, 1)) {
val textureLayer = if (RenderConstants.FORCE_DEBUG_TEXTURE) {

View File

@ -17,7 +17,7 @@ import de.bixilon.minosoft.gui.rendering.RenderWindow
import de.bixilon.minosoft.gui.rendering.util.mesh.Mesh
import de.bixilon.minosoft.gui.rendering.util.mesh.PositionOnlyMeshStruct
class SkyboxMesh(renderWindow: RenderWindow) : Mesh(renderWindow, PositionOnlyMeshStruct::class, initialCacheSize = 6 * 2 * 3 * PositionOnlyMeshStruct.FLOATS_PER_VERTEX) {
class SkyboxMesh(renderWindow: RenderWindow) : Mesh(renderWindow, PositionOnlyMeshStruct, initialCacheSize = 6 * 2 * 3 * PositionOnlyMeshStruct.FLOATS_PER_VERTEX) {
init {
data.addAll(

View File

@ -19,9 +19,9 @@ import de.bixilon.minosoft.gui.rendering.system.base.buffer.uniform.IntUniformBu
import de.bixilon.minosoft.gui.rendering.system.base.buffer.vertex.FloatVertexBuffer
import de.bixilon.minosoft.gui.rendering.system.base.buffer.vertex.PrimitiveTypes
import de.bixilon.minosoft.gui.rendering.system.base.shader.Shader
import de.bixilon.minosoft.gui.rendering.util.mesh.MeshStruct
import glm_.vec2.Vec2i
import java.nio.ByteBuffer
import kotlin.reflect.KClass
interface RenderSystem {
val shaders: MutableSet<Shader>
@ -71,7 +71,7 @@ interface RenderSystem {
fun createShader(resourceLocation: ResourceLocation): Shader
fun createVertexBuffer(structure: KClass<*>, data: FloatArray, primitiveType: PrimitiveTypes = PrimitiveTypes.TRIANGLE): FloatVertexBuffer
fun createVertexBuffer(structure: MeshStruct, data: FloatArray, primitiveType: PrimitiveTypes = PrimitiveTypes.TRIANGLE): FloatVertexBuffer
fun createIntUniformBuffer(bindingIndex: Int = 0, data: IntArray = IntArray(0)): IntUniformBuffer
fun createFloatUniformBuffer(bindingIndex: Int = 0, data: FloatArray = FloatArray(0)): FloatUniformBuffer
}

View File

@ -1,11 +1,11 @@
package de.bixilon.minosoft.gui.rendering.system.base.buffer.vertex
import kotlin.reflect.KClass
import de.bixilon.minosoft.gui.rendering.util.mesh.MeshStruct
interface VertexBuffer {
val vertices: Int
val primitiveType: PrimitiveTypes
val structure: KClass<*>
val structure: MeshStruct
fun draw()
}

View File

@ -26,6 +26,7 @@ import de.bixilon.minosoft.gui.rendering.system.opengl.buffer.uniform.FloatOpenG
import de.bixilon.minosoft.gui.rendering.system.opengl.buffer.uniform.IntOpenGLUniformBuffer
import de.bixilon.minosoft.gui.rendering.system.opengl.buffer.vertex.FloatOpenGLVertexBuffer
import de.bixilon.minosoft.gui.rendering.system.opengl.vendor.*
import de.bixilon.minosoft.gui.rendering.util.mesh.MeshStruct
import de.bixilon.minosoft.modding.event.CallbackEventInvoker
import de.bixilon.minosoft.util.KUtil.synchronizedSetOf
import glm_.vec2.Vec2i
@ -33,7 +34,6 @@ import org.lwjgl.BufferUtils
import org.lwjgl.opengl.GL
import org.lwjgl.opengl.GL20.*
import java.nio.ByteBuffer
import kotlin.reflect.KClass
class OpenGLRenderSystem(
private val renderWindow: RenderWindow,
@ -173,7 +173,7 @@ class OpenGLRenderSystem(
return OpenGLShader(renderWindow, resourceLocation)
}
override fun createVertexBuffer(structure: KClass<*>, data: FloatArray, primitiveType: PrimitiveTypes): FloatVertexBuffer {
override fun createVertexBuffer(structure: MeshStruct, data: FloatArray, primitiveType: PrimitiveTypes): FloatVertexBuffer {
return FloatOpenGLVertexBuffer(structure, data, primitiveType)
}

View File

@ -5,30 +5,20 @@ import de.bixilon.minosoft.gui.rendering.system.base.buffer.vertex.FloatVertexBu
import de.bixilon.minosoft.gui.rendering.system.base.buffer.vertex.PrimitiveTypes
import de.bixilon.minosoft.gui.rendering.system.opengl.buffer.FloatOpenGLBuffer
import de.bixilon.minosoft.gui.rendering.util.mesh.MeshStruct
import de.bixilon.minosoft.gui.rendering.util.mesh.MeshStruct.Companion.BYTES
import de.bixilon.minosoft.util.KUtil.unsafeCast
import de.bixilon.minosoft.util.Util
import org.lwjgl.opengl.ARBVertexArrayObject.glBindVertexArray
import org.lwjgl.opengl.ARBVertexArrayObject.glGenVertexArrays
import org.lwjgl.opengl.GL11.*
import org.lwjgl.opengl.GL15.glBufferData
import org.lwjgl.opengl.GL20.glEnableVertexAttribArray
import org.lwjgl.opengl.GL20.glVertexAttribPointer
import kotlin.reflect.KClass
import kotlin.reflect.full.companionObjectInstance
import kotlin.reflect.full.primaryConstructor
class FloatOpenGLVertexBuffer(override val structure: KClass<*>, data: FloatArray, override val primitiveType: PrimitiveTypes = PrimitiveTypes.TRIANGLE) : FloatOpenGLBuffer(data), FloatVertexBuffer {
class FloatOpenGLVertexBuffer(override val structure: MeshStruct, data: FloatArray, override val primitiveType: PrimitiveTypes = PrimitiveTypes.TRIANGLE) : FloatOpenGLBuffer(data), FloatVertexBuffer {
override var vertices = -1
private set
private var vao = -1
override fun init() {
Util.forceClassInit(structure.java)
val bytesPerVertex = structure.companionObjectInstance!!.unsafeCast<MeshStruct>().BYTES_PER_VERTEX
val floatsPerVertex = bytesPerVertex / Float.SIZE_BYTES
val floatsPerVertex = structure.BYTES_PER_VERTEX / Float.SIZE_BYTES
vertices = data.size / floatsPerVertex
vao = glGenVertexArrays()
@ -42,14 +32,11 @@ class FloatOpenGLVertexBuffer(override val structure: KClass<*>, data: FloatArra
_data = null
var stride = 0L
for ((index, parameter) in structure.primaryConstructor!!.parameters.withIndex()) {
val bytes = parameter.BYTES
glVertexAttribPointer(index, bytes / Float.SIZE_BYTES, GL_FLOAT, false, bytesPerVertex, stride)
glEnableVertexAttribArray(index)
stride += bytes
for (attribute in structure.attributes) {
glVertexAttribPointer(attribute.index, attribute.size, GL_FLOAT, false, structure.BYTES_PER_VERTEX, attribute.stride)
glEnableVertexAttribArray(attribute.index)
}
unbind()
}

View File

@ -18,7 +18,7 @@ import de.bixilon.minosoft.data.text.RGBColor
import de.bixilon.minosoft.gui.rendering.RenderWindow
import glm_.vec3.Vec3
open class GenericColorMesh(renderWindow: RenderWindow) : Mesh(renderWindow, GenericColorMeshStruct::class) {
open class GenericColorMesh(renderWindow: RenderWindow) : Mesh(renderWindow, GenericColorMeshStruct) {
fun addVertex(position: Vec3, color: RGBColor?) {
data.addAll(

View File

@ -19,11 +19,10 @@ import de.bixilon.minosoft.gui.rendering.system.base.buffer.vertex.PrimitiveType
import de.bixilon.minosoft.util.collections.ArrayFloatList
import glm_.vec2.Vec2
import glm_.vec3.Vec3
import kotlin.reflect.KClass
abstract class Mesh(
val renderWindow: RenderWindow,
private val struct: KClass<*>,
private val struct: MeshStruct,
private val primitiveType: PrimitiveTypes = PrimitiveTypes.TRIANGLE,
initialCacheSize: Int = 10000,
) {

View File

@ -0,0 +1,20 @@
/*
* Minosoft
* Copyright (C) 2021 Moritz Zwerger
*
* This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along with this program. If not, see <https://www.gnu.org/licenses/>.
*
* This software is not affiliated with Mojang AB, the original developer of Minecraft.
*/
package de.bixilon.minosoft.gui.rendering.util.mesh
data class MeshAttribute(
val index: Int,
val size: Int,
val stride: Long,
)

View File

@ -28,6 +28,20 @@ import kotlin.reflect.full.primaryConstructor
abstract class MeshStruct(val struct: KClass<*>) {
val BYTES_PER_VERTEX: Int = calculateBytesPerVertex(struct)
val FLOATS_PER_VERTEX: Int = BYTES_PER_VERTEX / Float.SIZE_BYTES
val attributes: List<MeshAttribute>
init {
val attributes: MutableList<MeshAttribute> = mutableListOf()
var stride = 0L
for ((index, parameter) in struct.primaryConstructor!!.parameters.withIndex()) {
val bytes = parameter.BYTES
attributes += MeshAttribute(index, bytes / Float.SIZE_BYTES, stride)
stride += bytes
}
this.attributes = attributes.toList()
}
companion object {

View File

@ -20,7 +20,7 @@ import de.bixilon.minosoft.gui.rendering.textures.Texture
import glm_.vec2.Vec2
import glm_.vec3.Vec3
open class SimpleTextureMesh(renderWindow: RenderWindow) : Mesh(renderWindow, SimpleTextureMeshStruct::class, initialCacheSize = 2 * 3 * SimpleTextureMeshStruct.FLOATS_PER_VERTEX) {
open class SimpleTextureMesh(renderWindow: RenderWindow) : Mesh(renderWindow, SimpleTextureMeshStruct, initialCacheSize = 2 * 3 * SimpleTextureMeshStruct.FLOATS_PER_VERTEX) {
fun addVertex(position: Vec3, texture: Texture, textureCoordinates: Vec2, tintColor: RGBColor) {
val textureLayer = if (RenderConstants.FORCE_DEBUG_TEXTURE) {