From db2e9c5c8634ac776b88a685f2e818fd9b5a9241 Mon Sep 17 00:00:00 2001 From: Bixilon Date: Fri, 4 Nov 2022 12:43:43 +0100 Subject: [PATCH] hot shader reloading --- .../gui/rendering/system/base/RenderSystem.kt | 8 +++++++ .../rendering/system/base/shader/Shader.kt | 4 ++++ .../rendering/system/opengl/OpenGLShader.kt | 13 ++++++++++ .../minosoft/terminal/commands/Commands.kt | 2 ++ .../commands/rendering/ReloadCommand.kt | 24 +++++++++++++++++++ .../commands/rendering/RenderingCommand.kt | 18 ++++++++++++++ 6 files changed, 69 insertions(+) create mode 100644 src/main/java/de/bixilon/minosoft/terminal/commands/rendering/ReloadCommand.kt create mode 100644 src/main/java/de/bixilon/minosoft/terminal/commands/rendering/RenderingCommand.kt diff --git a/src/main/java/de/bixilon/minosoft/gui/rendering/system/base/RenderSystem.kt b/src/main/java/de/bixilon/minosoft/gui/rendering/system/base/RenderSystem.kt index bc4b8460d..715ccad07 100644 --- a/src/main/java/de/bixilon/minosoft/gui/rendering/system/base/RenderSystem.kt +++ b/src/main/java/de/bixilon/minosoft/gui/rendering/system/base/RenderSystem.kt @@ -130,6 +130,14 @@ interface RenderSystem { setBlendFunction(BlendingFunctions.ONE, BlendingFunctions.ONE_MINUS_SOURCE_ALPHA, BlendingFunctions.ONE, BlendingFunctions.ZERO) } + @Deprecated("Highly unstable") + fun reloadShaders() { + val copy = shaders.toMutableSet() + for (shader in copy) { + shader.reload() + } + } + companion object { diff --git a/src/main/java/de/bixilon/minosoft/gui/rendering/system/base/shader/Shader.kt b/src/main/java/de/bixilon/minosoft/gui/rendering/system/base/shader/Shader.kt index fd91812aa..d04007b0f 100644 --- a/src/main/java/de/bixilon/minosoft/gui/rendering/system/base/shader/Shader.kt +++ b/src/main/java/de/bixilon/minosoft/gui/rendering/system/base/shader/Shader.kt @@ -34,6 +34,10 @@ interface Shader { val log: String fun load() + fun unload() + + @Deprecated("Highly buggy, do not use in normal environment") + fun reload() fun use(): Shader { renderWindow.renderSystem.shader = this diff --git a/src/main/java/de/bixilon/minosoft/gui/rendering/system/opengl/OpenGLShader.kt b/src/main/java/de/bixilon/minosoft/gui/rendering/system/opengl/OpenGLShader.kt index 6f3409288..99a5e83a5 100644 --- a/src/main/java/de/bixilon/minosoft/gui/rendering/system/opengl/OpenGLShader.kt +++ b/src/main/java/de/bixilon/minosoft/gui/rendering/system/opengl/OpenGLShader.kt @@ -105,6 +105,19 @@ class OpenGLShader( renderWindow.renderSystem.shaders += this } + override fun unload() { + check(loaded) { "Not loaded!" } + glDeleteProgram(this.shader) + loaded = false + this.shader = -1 + renderWindow.renderSystem.shaders -= this + } + + override fun reload() { + unload() + load() + } + private fun getUniformLocation(uniformName: String): Int { val location = uniformLocations.getOrPut(uniformName) { diff --git a/src/main/java/de/bixilon/minosoft/terminal/commands/Commands.kt b/src/main/java/de/bixilon/minosoft/terminal/commands/Commands.kt index 3b0e749cb..e9bebb3fe 100644 --- a/src/main/java/de/bixilon/minosoft/terminal/commands/Commands.kt +++ b/src/main/java/de/bixilon/minosoft/terminal/commands/Commands.kt @@ -14,11 +14,13 @@ package de.bixilon.minosoft.terminal.commands import de.bixilon.minosoft.terminal.commands.connection.SayCommand +import de.bixilon.minosoft.terminal.commands.rendering.ReloadCommand object Commands { val COMMANDS: List = listOf( HelpCommand, SayCommand, ConnectionManageCommand, + ReloadCommand, ) } diff --git a/src/main/java/de/bixilon/minosoft/terminal/commands/rendering/ReloadCommand.kt b/src/main/java/de/bixilon/minosoft/terminal/commands/rendering/ReloadCommand.kt new file mode 100644 index 000000000..c14f51dfb --- /dev/null +++ b/src/main/java/de/bixilon/minosoft/terminal/commands/rendering/ReloadCommand.kt @@ -0,0 +1,24 @@ +/* + * 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 . + * + * This software is not affiliated with Mojang AB, the original developer of Minecraft. + */ + +package de.bixilon.minosoft.terminal.commands.rendering + +import de.bixilon.minosoft.commands.nodes.LiteralNode + +object ReloadCommand : RenderingCommand { + override var node = LiteralNode("reload", setOf("rl")) + .addChild(LiteralNode("shaders", executor = { + it.connection.rendering!!.renderWindow.renderSystem.reloadShaders() + it.connection.util.sendDebugMessage("Shaders reloaded!") + })) +} diff --git a/src/main/java/de/bixilon/minosoft/terminal/commands/rendering/RenderingCommand.kt b/src/main/java/de/bixilon/minosoft/terminal/commands/rendering/RenderingCommand.kt new file mode 100644 index 000000000..268c5aa22 --- /dev/null +++ b/src/main/java/de/bixilon/minosoft/terminal/commands/rendering/RenderingCommand.kt @@ -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 . + * + * This software is not affiliated with Mojang AB, the original developer of Minecraft. + */ + +package de.bixilon.minosoft.terminal.commands.rendering + +import de.bixilon.minosoft.terminal.commands.Command + +interface RenderingCommand : Command