mirror of
https://gitlab.bixilon.de/bixilon/minosoft.git
synced 2025-09-17 03:15:35 -04:00
make particle shader uniform safe
This commit is contained in:
parent
93240f51fe
commit
0a14b8e769
@ -13,7 +13,6 @@
|
||||
|
||||
package de.bixilon.minosoft.gui.rendering.particle
|
||||
|
||||
import de.bixilon.kotlinglm.mat4x4.Mat4
|
||||
import de.bixilon.kotlinglm.vec3.Vec3
|
||||
import de.bixilon.kutil.concurrent.lock.simple.SimpleLock
|
||||
import de.bixilon.kutil.concurrent.time.TimeWorker
|
||||
@ -34,9 +33,6 @@ import de.bixilon.minosoft.gui.rendering.system.base.RenderSystem
|
||||
import de.bixilon.minosoft.gui.rendering.system.base.phases.SkipAll
|
||||
import de.bixilon.minosoft.gui.rendering.system.base.phases.TranslucentDrawable
|
||||
import de.bixilon.minosoft.gui.rendering.system.base.phases.TransparentDrawable
|
||||
import de.bixilon.minosoft.gui.rendering.system.base.shader.Shader
|
||||
import de.bixilon.minosoft.gui.rendering.system.base.shader.Shader.Companion.loadAnimated
|
||||
import de.bixilon.minosoft.gui.rendering.system.base.shader.ShaderUniforms
|
||||
import de.bixilon.minosoft.modding.event.listener.CallbackEventListener.Companion.listen
|
||||
import de.bixilon.minosoft.protocol.network.connection.play.PlayConnection
|
||||
import de.bixilon.minosoft.protocol.network.connection.play.PlayConnectionStates
|
||||
@ -53,8 +49,8 @@ class ParticleRenderer(
|
||||
) : AsyncRenderer, TransparentDrawable, TranslucentDrawable, SkipAll, AbstractParticleRenderer {
|
||||
override val renderSystem: RenderSystem = renderWindow.renderSystem
|
||||
private val profile = connection.profiles.particle
|
||||
private val transparentShader: Shader = renderSystem.createShader(minosoft("particle"))
|
||||
private val translucentShader: Shader = renderSystem.createShader(minosoft("particle"))
|
||||
private val transparentShader = renderSystem.createShader(minosoft("particle")) { ParticleShader(it, true) }
|
||||
private val translucentShader = renderSystem.createShader(minosoft("particle")) { ParticleShader(it, false) }
|
||||
|
||||
// There is no opaque mesh because it is simply not needed (every particle has transparency)
|
||||
private var transparentMesh = ParticleMesh(renderWindow, DirectArrayFloatList(RenderConstants.MAXIMUM_PARTICLE_AMOUNT * ParticleMesh.ParticleMeshStruct.FLOATS_PER_VERTEX))
|
||||
@ -64,6 +60,7 @@ class ParticleRenderer(
|
||||
private var particles: MutableList<Particle> = mutableListOf()
|
||||
private var particleQueueLock = SimpleLock()
|
||||
private var particleQueue: MutableList<Particle> = mutableListOf()
|
||||
private var matrixUpdate = true
|
||||
|
||||
|
||||
private lateinit var particleTask: TimeWorkerTask
|
||||
@ -110,19 +107,7 @@ class ParticleRenderer(
|
||||
profile::enabled.profileWatch(this, true, profile) { enabled = it }
|
||||
|
||||
connection.events.listen<CameraMatrixChangeEvent> {
|
||||
renderWindow.queue += {
|
||||
fun applyToShader(shader: Shader) {
|
||||
shader.apply {
|
||||
use()
|
||||
setMat4(ShaderUniforms.VIEW_PROJECTION_MATRIX, Mat4(it.viewProjectionMatrix))
|
||||
setVec3(ShaderUniforms.CAMERA_RIGHT, Vec3(it.viewMatrix[0][0], it.viewMatrix[1][0], it.viewMatrix[2][0]))
|
||||
setVec3(ShaderUniforms.CAMERA_UP, Vec3(it.viewMatrix[0][1], it.viewMatrix[1][1], it.viewMatrix[2][1]))
|
||||
}
|
||||
}
|
||||
|
||||
applyToShader(transparentShader)
|
||||
applyToShader(translucentShader)
|
||||
}
|
||||
matrixUpdate = true
|
||||
}
|
||||
|
||||
transparentMesh.load()
|
||||
@ -137,9 +122,8 @@ class ParticleRenderer(
|
||||
}
|
||||
|
||||
override fun postInit(latch: CountUpAndDownLatch) {
|
||||
transparentShader.defines[Shader.TRANSPARENT_DEFINE] = ""
|
||||
transparentShader.loadAnimated(light = true)
|
||||
translucentShader.loadAnimated(light = true)
|
||||
transparentShader.load()
|
||||
translucentShader.load()
|
||||
|
||||
connection.world.particleRenderer = this
|
||||
|
||||
@ -209,7 +193,23 @@ class ParticleRenderer(
|
||||
particleQueueLock.unlock()
|
||||
}
|
||||
|
||||
private fun updateShaders() {
|
||||
val matrix = renderWindow.camera.matrixHandler.viewProjectionMatrix
|
||||
val cameraRight = Vec3(matrix[0][0], matrix[1][0], matrix[2][0])
|
||||
val cameraUp = Vec3(matrix[0][1], matrix[1][1], matrix[2][1])
|
||||
|
||||
transparentShader.cameraRight = cameraRight
|
||||
transparentShader.cameraUp = cameraUp
|
||||
|
||||
translucentShader.cameraRight = cameraRight
|
||||
translucentShader.cameraUp = cameraUp
|
||||
}
|
||||
|
||||
override fun prePrepareDraw() {
|
||||
if (matrixUpdate) {
|
||||
updateShaders()
|
||||
matrixUpdate = false
|
||||
}
|
||||
transparentMesh.unload()
|
||||
translucentMesh.unload()
|
||||
}
|
||||
|
@ -0,0 +1,37 @@
|
||||
/*
|
||||
* Minosoft
|
||||
* Copyright (C) 2020-2022 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.particle
|
||||
|
||||
import de.bixilon.kotlinglm.mat4x4.Mat4
|
||||
import de.bixilon.kotlinglm.vec3.Vec3
|
||||
import de.bixilon.minosoft.gui.rendering.shader.MinosoftShader
|
||||
import de.bixilon.minosoft.gui.rendering.shader.types.LightShader
|
||||
import de.bixilon.minosoft.gui.rendering.shader.types.TextureShader
|
||||
import de.bixilon.minosoft.gui.rendering.shader.types.TransparentShader
|
||||
import de.bixilon.minosoft.gui.rendering.shader.types.ViewProjectionShader
|
||||
import de.bixilon.minosoft.gui.rendering.system.base.shader.Shader
|
||||
import de.bixilon.minosoft.gui.rendering.system.base.shader.ShaderUniforms
|
||||
import de.bixilon.minosoft.gui.rendering.system.base.texture.TextureManager
|
||||
import de.bixilon.minosoft.gui.rendering.world.light.LightmapBuffer
|
||||
|
||||
class ParticleShader(
|
||||
override val native: Shader,
|
||||
override val transparent: Boolean,
|
||||
) : MinosoftShader(), TextureShader, LightShader, TransparentShader, ViewProjectionShader {
|
||||
override var textures: TextureManager by textureManager()
|
||||
override val lightmap: LightmapBuffer by lightmap()
|
||||
override var viewProjectionMatrix: Mat4 by viewProjectionMatrix()
|
||||
var cameraRight by uniform(ShaderUniforms.CAMERA_RIGHT, Vec3(), Shader::setVec3)
|
||||
var cameraUp by uniform(ShaderUniforms.CAMERA_UP, Vec3(), Shader::setVec3)
|
||||
}
|
@ -13,22 +13,24 @@
|
||||
|
||||
package de.bixilon.minosoft.gui.rendering.shader
|
||||
|
||||
import de.bixilon.minosoft.gui.rendering.shader.types.TransparentShader
|
||||
import de.bixilon.minosoft.gui.rendering.shader.uniform.ShaderUniform
|
||||
import de.bixilon.minosoft.gui.rendering.system.base.shader.Shader
|
||||
|
||||
abstract class MinosoftShader : AbstractMinosoftShader {
|
||||
private val uniforms: MutableMap<String, ShaderUniform<*>> = mutableMapOf()
|
||||
|
||||
fun load() {
|
||||
native.load()
|
||||
native.renderWindow.renderSystem.minosoftShaders += this
|
||||
}
|
||||
|
||||
fun unload() {
|
||||
native.unload()
|
||||
native.renderWindow.renderSystem.minosoftShaders -= this
|
||||
}
|
||||
|
||||
fun postLoad() {
|
||||
fun load() {
|
||||
if (this is TransparentShader && this.transparent) {
|
||||
native.defines[Shader.TRANSPARENT_DEFINE] = " "
|
||||
}
|
||||
native.load()
|
||||
native.renderWindow.renderSystem.minosoftShaders += this
|
||||
for (uniform in uniforms.values) {
|
||||
uniform.upload()
|
||||
}
|
||||
|
@ -0,0 +1,17 @@
|
||||
/*
|
||||
* Minosoft
|
||||
* Copyright (C) 2020-2022 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.shader.types
|
||||
|
||||
interface AnimatedShader {
|
||||
}
|
@ -0,0 +1,26 @@
|
||||
/*
|
||||
* Minosoft
|
||||
* Copyright (C) 2020-2022 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.shader.types
|
||||
|
||||
import de.bixilon.minosoft.gui.rendering.shader.AbstractMinosoftShader
|
||||
import de.bixilon.minosoft.gui.rendering.shader.uniform.ShaderUniform
|
||||
import de.bixilon.minosoft.gui.rendering.world.light.LightmapBuffer
|
||||
|
||||
interface LightShader : AbstractMinosoftShader {
|
||||
val lightmap: LightmapBuffer
|
||||
|
||||
fun lightmap(name: String = "uLightMapBuffer", buffer: LightmapBuffer = native.renderWindow.light.map.buffer): ShaderUniform<LightmapBuffer> {
|
||||
return uniform(name, buffer) { native, name, value -> value.use(native, name) }
|
||||
}
|
||||
}
|
@ -11,16 +11,17 @@
|
||||
* This software is not affiliated with Mojang AB, the original developer of Minecraft.
|
||||
*/
|
||||
|
||||
package de.bixilon.minosoft.gui.rendering.shader
|
||||
package de.bixilon.minosoft.gui.rendering.shader.types
|
||||
|
||||
import de.bixilon.minosoft.gui.rendering.shader.AbstractMinosoftShader
|
||||
import de.bixilon.minosoft.gui.rendering.shader.uniform.ShaderUniform
|
||||
import de.bixilon.minosoft.gui.rendering.system.base.texture.TextureManager
|
||||
|
||||
interface TextureShader : AbstractMinosoftShader {
|
||||
val textures: TextureManager
|
||||
var textures: TextureManager
|
||||
|
||||
fun textureManager(textureManager: TextureManager, animated: Boolean): ShaderUniform<TextureManager> {
|
||||
return uniform("uTextures", textureManager) { native, name, value: TextureManager ->
|
||||
fun textureManager(name: String = "uTextures", textureManager: TextureManager = native.renderWindow.textureManager, animated: Boolean = this is AnimatedShader): ShaderUniform<TextureManager> {
|
||||
return uniform(name, textureManager) { native, name, value: TextureManager ->
|
||||
value.use(native, name)
|
||||
if (animated) {
|
||||
textureManager.staticTextures.animator.use(native)
|
@ -0,0 +1,18 @@
|
||||
/*
|
||||
* Minosoft
|
||||
* Copyright (C) 2020-2022 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.shader.types
|
||||
|
||||
interface TransparentShader {
|
||||
val transparent: Boolean
|
||||
}
|
@ -0,0 +1,29 @@
|
||||
/*
|
||||
* Minosoft
|
||||
* Copyright (C) 2020-2022 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.shader.types
|
||||
|
||||
import de.bixilon.kotlinglm.mat4x4.Mat4
|
||||
import de.bixilon.minosoft.gui.rendering.shader.AbstractMinosoftShader
|
||||
import de.bixilon.minosoft.gui.rendering.shader.uniform.ShaderUniform
|
||||
import de.bixilon.minosoft.gui.rendering.system.base.shader.Shader
|
||||
import de.bixilon.minosoft.gui.rendering.system.base.shader.ShaderUniforms
|
||||
|
||||
interface ViewProjectionShader : AbstractMinosoftShader {
|
||||
var viewProjectionMatrix: Mat4
|
||||
|
||||
|
||||
fun viewProjectionMatrix(): ShaderUniform<Mat4> {
|
||||
return uniform(ShaderUniforms.VIEW_PROJECTION_MATRIX, Mat4(), Shader::setMat4)
|
||||
}
|
||||
}
|
@ -43,10 +43,6 @@ abstract class PlanetRenderer(
|
||||
open var uvStart = Vec2(0.0f, 0.0f)
|
||||
open var uvEnd = Vec2(1.0f, 1.0f)
|
||||
|
||||
override fun init() {
|
||||
shader.load()
|
||||
}
|
||||
|
||||
private fun prepareMesh() {
|
||||
mesh.addYQuad(
|
||||
start = Vec2(-0.2f, -0.2f),
|
||||
@ -68,8 +64,8 @@ abstract class PlanetRenderer(
|
||||
}
|
||||
|
||||
override fun postInit() {
|
||||
shader.load()
|
||||
prepareMesh()
|
||||
shader.postLoad()
|
||||
sky::matrix.observe(this) { calculateMatrix(it) }
|
||||
}
|
||||
|
||||
|
@ -16,7 +16,7 @@ package de.bixilon.minosoft.gui.rendering.sky.planet
|
||||
import de.bixilon.kotlinglm.mat4x4.Mat4
|
||||
import de.bixilon.kotlinglm.vec4.Vec4
|
||||
import de.bixilon.minosoft.gui.rendering.shader.MinosoftShader
|
||||
import de.bixilon.minosoft.gui.rendering.shader.TextureShader
|
||||
import de.bixilon.minosoft.gui.rendering.shader.types.TextureShader
|
||||
import de.bixilon.minosoft.gui.rendering.system.base.shader.Shader
|
||||
import de.bixilon.minosoft.gui.rendering.system.base.texture.TextureManager
|
||||
|
||||
@ -25,6 +25,6 @@ class PlanetShader(
|
||||
) : MinosoftShader(), TextureShader {
|
||||
var matrix: Mat4 by uniform("uMatrix", Mat4())
|
||||
var tintColor: Vec4 by uniform("uTintColor", Vec4())
|
||||
override val textures: TextureManager by textureManager(native.renderWindow.textureManager, false)
|
||||
override var textures: TextureManager by textureManager(animated = false)
|
||||
|
||||
}
|
||||
|
@ -102,6 +102,7 @@ interface RenderSystem {
|
||||
fun readPixels(start: Vec2i, end: Vec2i, type: PixelTypes): ByteBuffer
|
||||
|
||||
|
||||
@Deprecated("Use MinosoftShader")
|
||||
fun createShader(resourceLocation: ResourceLocation): Shader {
|
||||
return createShader(
|
||||
vertex = "$resourceLocation.vsh".toResourceLocation(),
|
||||
|
@ -36,7 +36,6 @@ interface Shader {
|
||||
fun load()
|
||||
fun unload()
|
||||
|
||||
@Deprecated("Highly buggy, do not use in normal environment")
|
||||
fun reload()
|
||||
|
||||
fun use(): Shader {
|
||||
|
Loading…
x
Reference in New Issue
Block a user