From adbedea49230eee4748f5531371c50d0d257dd89 Mon Sep 17 00:00:00 2001 From: Lyzev Date: Fri, 24 May 2024 23:17:22 +0200 Subject: [PATCH] added reinit particle shader when scaling window added reinit particle shader when releasing slider Signed-off-by: Lyzev --- .../client/MixinMinecraftClient.java | 5 +- .../kotlin/dev/lyzev/api/events/Events.kt | 2 +- .../ImGuiRenderableDeveloperTool.kt | 4 +- .../dev/lyzev/api/opengl/shader/Shader.kt | 52 ++++++++---- .../setting/settings/SettingClientSlider.kt | 79 +++++++++++-------- .../features/gui/guis/ImGuiScreenFeature.kt | 13 +-- .../render/ModuleToggleableWaterMark.kt | 2 +- 7 files changed, 98 insertions(+), 59 deletions(-) diff --git a/src/main/java/dev/lyzev/schizoid/injection/mixins/minecraft/client/MixinMinecraftClient.java b/src/main/java/dev/lyzev/schizoid/injection/mixins/minecraft/client/MixinMinecraftClient.java index c11cfb6275..7935a9e97b 100644 --- a/src/main/java/dev/lyzev/schizoid/injection/mixins/minecraft/client/MixinMinecraftClient.java +++ b/src/main/java/dev/lyzev/schizoid/injection/mixins/minecraft/client/MixinMinecraftClient.java @@ -6,12 +6,11 @@ package dev.lyzev.schizoid.injection.mixins.minecraft.client; import dev.lyzev.api.events.EventItemUse; -import dev.lyzev.api.events.EventReload; +import dev.lyzev.api.events.EventReloadShader; import dev.lyzev.api.events.EventStartup; import dev.lyzev.api.events.EventWindowResize; import dev.lyzev.schizoid.Schizoid; import net.minecraft.client.MinecraftClient; -import org.jetbrains.annotations.Nullable; import org.spongepowered.asm.mixin.Mixin; import org.spongepowered.asm.mixin.Shadow; import org.spongepowered.asm.mixin.injection.At; @@ -79,7 +78,7 @@ private void onItemUse(CallbackInfo ci) { @Inject(method = "reloadResources()Ljava/util/concurrent/CompletableFuture;", at = @At(value = "HEAD")) private void onReloadResources(CallbackInfoReturnable> cir) { - EventReload.INSTANCE.fire(); + EventReloadShader.INSTANCE.fire(); } } diff --git a/src/main/kotlin/dev/lyzev/api/events/Events.kt b/src/main/kotlin/dev/lyzev/api/events/Events.kt index 985d35e907..b8635bd6d0 100644 --- a/src/main/kotlin/dev/lyzev/api/events/Events.kt +++ b/src/main/kotlin/dev/lyzev/api/events/Events.kt @@ -163,7 +163,7 @@ class EventAttackEntity(val player: PlayerEntity, val entity: Entity) : Event class EventOSThemeUpdate(val theme: OSTheme.Theme) : Event -object EventReload : Event +object EventReloadShader : Event class EventGetFOV(var fov: Double) : Event diff --git a/src/main/kotlin/dev/lyzev/api/imgui/render/renderable/ImGuiRenderableDeveloperTool.kt b/src/main/kotlin/dev/lyzev/api/imgui/render/renderable/ImGuiRenderableDeveloperTool.kt index dd64729e67..37e7226f03 100644 --- a/src/main/kotlin/dev/lyzev/api/imgui/render/renderable/ImGuiRenderableDeveloperTool.kt +++ b/src/main/kotlin/dev/lyzev/api/imgui/render/renderable/ImGuiRenderableDeveloperTool.kt @@ -5,7 +5,7 @@ package dev.lyzev.api.imgui.render.renderable -import dev.lyzev.api.events.EventReload +import dev.lyzev.api.events.EventReloadShader import dev.lyzev.api.imgui.font.ImGuiFonts.OPEN_SANS_BOLD import dev.lyzev.api.imgui.font.ImGuiFonts.OPEN_SANS_REGULAR import dev.lyzev.api.imgui.render.ImGuiRenderable @@ -22,7 +22,7 @@ class ImGuiRenderableDeveloperTool : ImGuiRenderable { OPEN_SANS_BOLD.end() OPEN_SANS_REGULAR.begin() if (button("Reload Shaders", getColumnWidth(), OPEN_SANS_REGULAR.size + getStyle().framePaddingY * 2)) { - EventReload.fire() + EventReloadShader.fire() } OPEN_SANS_REGULAR.end() } diff --git a/src/main/kotlin/dev/lyzev/api/opengl/shader/Shader.kt b/src/main/kotlin/dev/lyzev/api/opengl/shader/Shader.kt index d0127099c2..4d1a3f8f5f 100644 --- a/src/main/kotlin/dev/lyzev/api/opengl/shader/Shader.kt +++ b/src/main/kotlin/dev/lyzev/api/opengl/shader/Shader.kt @@ -7,7 +7,7 @@ package dev.lyzev.api.opengl.shader import com.mojang.blaze3d.systems.RenderSystem import dev.lyzev.api.events.EventListener -import dev.lyzev.api.events.EventReload +import dev.lyzev.api.events.EventReloadShader import dev.lyzev.api.events.EventWindowResize import dev.lyzev.api.events.on import dev.lyzev.schizoid.Schizoid @@ -29,7 +29,7 @@ import java.io.FileNotFoundException */ abstract class Shader(val shader: String) : EventListener { - var program = glCreateProgram() + var program = 0 private val uniforms = HashMap() /** @@ -180,10 +180,19 @@ abstract class Shader(val shader: String) : EventListener { } open fun init() { - glLinkProgram(program) + if (program == 0) + program = glCreateProgram() + } - val isLinked = glGetProgrami(program, GL_LINK_STATUS) + open fun delete() { + if (program != 0) + glDeleteProgram(program) + program = 0 + } + fun link() { + glLinkProgram(program) + val isLinked = glGetProgrami(program, GL_LINK_STATUS) if (isLinked == 0) Schizoid.logger.error( glGetProgramInfoLog( @@ -194,15 +203,14 @@ abstract class Shader(val shader: String) : EventListener { } fun reload() { - glDeleteProgram(program) - program = glCreateProgram() + delete() init() } override val shouldHandleEvents = true init { - on { + on { if (!Schizoid.DEVELOPER_MODE) return@on reload() } @@ -235,6 +243,10 @@ abstract class ShaderVertexFragment( ) : Shader(shader) { final override fun init() { + val tmp = program + super.init() + if (tmp == program) + return val vSource = ClassLoader.getSystemResourceAsStream("$PATH/core/$shader/${shader}_VP.glsl")?.use { it.readAllBytes().decodeToString() } ?: "null" @@ -249,7 +261,8 @@ abstract class ShaderVertexFragment( glDeleteShader(vShader) glDeleteShader(fShader) - super.init() + + link() } init { @@ -309,6 +322,10 @@ abstract class ShaderCompute( } override fun init() { + val tmp = program + super.init() + if (tmp == program) + return ClassLoader.getSystemResourceAsStream("$PATH/core/$shader/${shader}_CP.glsl") ?.use { val computeShader = compile( @@ -319,19 +336,22 @@ abstract class ShaderCompute( glDeleteShader(computeShader) } - if (texture == 0) { + link() + + if (texture == 0) genTexture() - } - super.init() + } + + final override fun delete() { + super.delete() + if (texture != 0) + glDeleteTextures(texture) + texture = 0 } init { on { - // delete old texture and create a new one - if (texture != 0) { - glDeleteTextures(texture) - } - genTexture() + reload() } } diff --git a/src/main/kotlin/dev/lyzev/api/setting/settings/SettingClientSlider.kt b/src/main/kotlin/dev/lyzev/api/setting/settings/SettingClientSlider.kt index 9affe1939d..185402cc6b 100644 --- a/src/main/kotlin/dev/lyzev/api/setting/settings/SettingClientSlider.kt +++ b/src/main/kotlin/dev/lyzev/api/setting/settings/SettingClientSlider.kt @@ -26,7 +26,7 @@ import kotlin.reflect.KProperty * @param unit The unit of the slider setting. * @param allowOutOfBounds A flag to determine whether the slider setting allows out of bounds values. * @param hide A lambda function that determines whether this setting is hidden or not. - * @param change A lambda function that will be called when the value of the setting changes. + * @param onChange A lambda function that will be called when the value of the setting changes. */ @Suppress("UNCHECKED_CAST") class SettingClientSlider( @@ -39,29 +39,31 @@ class SettingClientSlider( private val decimalPlaces: Int, private val unit: String?, private val allowOutOfBounds: Boolean, + private val onlyUpdateOnRelease: Boolean, hide: () -> Boolean, - change: (T) -> Unit -) : SettingClient(container, name, desc, value, hide, change) { - - /** - * The class of the value type. - */ - private val valueType: KClass = value::class + onChange: (T) -> Unit +) : SettingClient(container, name, desc, value, hide, onChange) { override fun render() { text(name) if (desc != null && isItemHovered()) setTooltip(desc) if (value is Int) { - i[0] = value as Int setNextItemWidth(getColumnWidth()) if (sliderInt("", i, minValue as Int, maxValue as Int, "%d" + if (unit != null) " $unit" else "")) { + if (!onlyUpdateOnRelease) { + value = if (allowOutOfBounds) + i[0] as T + else + MathHelper.clamp(i[0], minValue.toInt(), maxValue.toInt()) as T + } + } + if (isItemDeactivatedAfterEdit() && onlyUpdateOnRelease) { value = if (allowOutOfBounds) i[0] as T else MathHelper.clamp(i[0], minValue.toInt(), maxValue.toInt()) as T } } else if (value is Float) { - f[0] = value as Float setNextItemWidth(getColumnWidth()) if (sliderFloat( "", @@ -71,6 +73,14 @@ class SettingClientSlider( "%.${decimalPlaces}f" + if (unit != null) " $unit" else "" ) ) { + if (!onlyUpdateOnRelease) { + value = if (allowOutOfBounds) + f[0] as T + else + MathHelper.clamp(f[0], minValue.toFloat(), maxValue.toFloat()) as T + } + } + if (isItemDeactivatedAfterEdit() && onlyUpdateOnRelease) { value = if (allowOutOfBounds) f[0] as T else @@ -82,33 +92,35 @@ class SettingClientSlider( override fun setValue(ref: Any, prop: KProperty<*>, value: T) { if (allowOutOfBounds) super.setValue(ref, prop, value) - else if (valueType == Int::class) + else if (this.value is Int) super.setValue(ref, prop, MathHelper.clamp(value.toInt(), minValue.toInt(), maxValue.toInt()) as T) - else if (valueType == Float::class) + else if (this.value is Float) super.setValue(ref, prop, MathHelper.clamp(value.toFloat(), minValue.toFloat(), maxValue.toFloat()) as T) + if (this.value is Int) + i[0] = this.value as Int + else if (this.value is Float) + f[0] = this.value as Float } override fun load(value: JsonElement) { - this.value = when (valueType) { - Int::class -> value.jsonPrimitive.int.let { if (allowOutOfBounds) it else it.coerceIn(minValue.toInt(), maxValue.toInt()) } as T - Float::class -> value.jsonPrimitive.float.let { if (allowOutOfBounds) it else it.coerceIn(minValue.toFloat(), maxValue.toFloat()) } as T + this.value = when (this.value) { + is Int -> (value.jsonPrimitive.int.let { if (allowOutOfBounds) it else it.coerceIn(minValue.toInt(), maxValue.toInt()) } as T).apply { i[0] = this as Int } + is Float -> (value.jsonPrimitive.float.let { if (allowOutOfBounds) it else it.coerceIn(minValue.toFloat(), maxValue.toFloat()) } as T).apply { f[0] = this as Float } else -> return } } override fun save(): JsonElement = JsonPrimitive(value) - companion object { - /** - * An array to store the integer value of the slider. - */ - private val i = intArrayOf(0) + /** + * An array to store the integer value of the slider. + */ + private val i = intArrayOf(0) - /** - * An array to store the float value of the slider. - */ - private val f = floatArrayOf(0f) - } + /** + * An array to store the float value of the slider. + */ + private val f = floatArrayOf(0f) } /** @@ -121,8 +133,9 @@ class SettingClientSlider( * @param maxValue The maximum value of the slider setting. * @param unit The unit of the slider setting. * @param allowOutOfBounds A flag to determine whether the slider setting allows out of bounds values. + * @param onlyUpdateOnRelease A flag to determine whether the slider setting only updates on release. * @param hide A lambda function that determines whether this setting is hidden or not. - * @param change A lambda function that will be called when the value of the setting changes. + * @param onChange A lambda function that will be called when the value of the setting changes. * * @return The created integer slider setting. */ @@ -134,9 +147,10 @@ fun IFeature.slider( maxValue: Int, unit: String? = null, allowOutOfBounds: Boolean = false, + onlyUpdateOnRelease: Boolean = false, hide: () -> Boolean = { false }, - change: (Int) -> Unit = {} -) = SettingClientSlider(this::class, name, desc, value, minValue, maxValue, 0, unit, allowOutOfBounds, hide, change) + onChange: (Int) -> Unit = {} +) = SettingClientSlider(this::class, name, desc, value, minValue, maxValue, 0, unit, allowOutOfBounds, onlyUpdateOnRelease, hide, onChange) /** * Creates a new float slider setting. @@ -149,8 +163,9 @@ fun IFeature.slider( * @param decimalPlaces The number of decimal places for the slider setting. * @param unit The unit of the slider setting. * @param allowOutOfBounds A flag to determine whether the slider setting allows out of bounds values. + * @param onlyUpdateOnRelease A flag to determine whether the slider setting only updates on release. * @param hide A lambda function that determines whether this setting is hidden or not. - * @param change A lambda function that will be called when the value of the setting changes. + * @param onChange A lambda function that will be called when the value of the setting changes. * * @return The created float slider setting. */ @@ -163,8 +178,9 @@ fun IFeature.slider( decimalPlaces: Int = 1, unit: String? = null, allowOutOfBounds: Boolean = false, + onlyUpdateOnRelease: Boolean = false, hide: () -> Boolean = { false }, - change: (Float) -> Unit = {} + onChange: (Float) -> Unit = {} ) = SettingClientSlider( this::class, name, @@ -175,6 +191,7 @@ fun IFeature.slider( decimalPlaces, unit, allowOutOfBounds, + onlyUpdateOnRelease, hide, - change + onChange ) diff --git a/src/main/kotlin/dev/lyzev/schizoid/feature/features/gui/guis/ImGuiScreenFeature.kt b/src/main/kotlin/dev/lyzev/schizoid/feature/features/gui/guis/ImGuiScreenFeature.kt index 206da88d98..aef5361f3f 100644 --- a/src/main/kotlin/dev/lyzev/schizoid/feature/features/gui/guis/ImGuiScreenFeature.kt +++ b/src/main/kotlin/dev/lyzev/schizoid/feature/features/gui/guis/ImGuiScreenFeature.kt @@ -44,19 +44,22 @@ object ImGuiScreenFeature : ImGuiScreen("Feature Screen"), EventListener { } val particles: Boolean by switch("Particles", "Enables the particle shader.\nToggle to reload.", false) { - if (!it) return@switch - ShaderParticle.amount = particleAmount * 1000 - ShaderParticle.reload() + if (it) ShaderParticle.init() + else ShaderParticle.delete() } val particleAmount by slider( "Particle Amount", "The amount of particles.", 100, 1, - 1000, + 999, "k", + onlyUpdateOnRelease = true, hide = ::particles neq true - ) + ) { + ShaderParticle.amount = it * 1_000 + ShaderParticle.reload() + } private val texturesMario = Array(3) { Identifier(Schizoid.MOD_ID, "textures/mario_$it.png") diff --git a/src/main/kotlin/dev/lyzev/schizoid/feature/features/module/modules/render/ModuleToggleableWaterMark.kt b/src/main/kotlin/dev/lyzev/schizoid/feature/features/module/modules/render/ModuleToggleableWaterMark.kt index 511388cb2d..88a0008701 100644 --- a/src/main/kotlin/dev/lyzev/schizoid/feature/features/module/modules/render/ModuleToggleableWaterMark.kt +++ b/src/main/kotlin/dev/lyzev/schizoid/feature/features/module/modules/render/ModuleToggleableWaterMark.kt @@ -36,7 +36,7 @@ object ModuleToggleableWaterMark : val waterMark by text("Water Mark", "The water mark to display.", "SCHIZOID", ::waterMarkMath eq true) val showFPS by switch("Show FPS", "Shows the current FPS.", true) val fpsBoost by switch("FPS Boost", "Boosts the FPS.", false) - val boost by slider("Boost", "The amount to boost the FPS by.", 1000, 0, 1000, "fps", true, ::fpsBoost neq true) + val boost by slider("Boost", "The amount to boost the FPS by.", 1000, 0, 1000, "fps", true, hide = ::fpsBoost neq true) private val functions = Math::class.members.map { it.name }.toMutableList() private var time = System.currentTimeMillis()