safe uniforms: textures

This commit is contained in:
Bixilon 2022-11-11 23:30:39 +01:00
parent 9171268e5f
commit 2c78e3c80c
No known key found for this signature in database
GPG Key ID: 5CAD791931B09AC4
8 changed files with 98 additions and 9 deletions

View File

@ -0,0 +1,23 @@
/*
* 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
import de.bixilon.minosoft.gui.rendering.shader.uniform.ShaderUniform
import de.bixilon.minosoft.gui.rendering.system.base.shader.Shader
interface AbstractMinosoftShader {
val native: Shader
fun <T> uniform(name: String, default: T, type: ShaderSetter<T> = Shader::set): ShaderUniform<T>
}

View File

@ -14,12 +14,9 @@
package de.bixilon.minosoft.gui.rendering.shader
import de.bixilon.minosoft.gui.rendering.shader.uniform.ShaderUniform
import de.bixilon.minosoft.gui.rendering.system.base.shader.Shader
import kotlin.reflect.KFunction3
abstract class MinosoftShader {
abstract class MinosoftShader : AbstractMinosoftShader {
private val uniforms: MutableMap<String, ShaderUniform<*>> = mutableMapOf()
abstract val native: Shader
fun load() {
native.load()
@ -35,8 +32,15 @@ abstract class MinosoftShader {
native.use()
}
fun reload() {
native.reload()
for (uniform in uniforms.values) {
uniform.forceUpload()
}
}
fun <T> uniform(name: String, default: T, type: KFunction3<Shader, String, T, Unit> = Shader::set): ShaderUniform<T> {
override fun <T> uniform(name: String, default: T, type: ShaderSetter<T>): ShaderUniform<T> {
val uniform = ShaderUniform(native, default, name, type)
val previous = uniforms.put(name, uniform)

View File

@ -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
import de.bixilon.minosoft.gui.rendering.system.base.shader.Shader
typealias ShaderSetter<T> = (shader: Shader, name: String, value: T) -> Unit

View File

@ -0,0 +1,30 @@
/*
* 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
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
fun textureManager(textureManager: TextureManager, animated: Boolean): ShaderUniform<TextureManager> {
return uniform("uTextures", textureManager) { native, name, value: TextureManager ->
value.use(native, name)
if (animated) {
textureManager.staticTextures.animator.use(native)
}
}
}
}

View File

@ -13,16 +13,16 @@
package de.bixilon.minosoft.gui.rendering.shader.uniform
import de.bixilon.minosoft.gui.rendering.shader.ShaderSetter
import de.bixilon.minosoft.gui.rendering.system.base.shader.Shader
import kotlin.properties.ReadWriteProperty
import kotlin.reflect.KFunction3
import kotlin.reflect.KProperty
class ShaderUniform<T>(
private val native: Shader,
default: T,
private val name: String,
private val setter: KFunction3<Shader, String, T, Unit>,
private val setter: ShaderSetter<T>,
) : ReadWriteProperty<Any, T> {
private var value = default
private var upload: Boolean = true
@ -31,6 +31,10 @@ class ShaderUniform<T>(
if (!upload) {
return
}
forceUpload()
}
fun forceUpload() {
native.use()
setter(native, name, value)
upload = false

View File

@ -70,7 +70,6 @@ abstract class PlanetRenderer(
override fun postInit() {
prepareMesh()
shader.postLoad()
sky.renderWindow.textureManager.staticTextures.use(shader)
sky::matrix.observe(this) { calculateMatrix(it) }
}

View File

@ -16,11 +16,15 @@ 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.system.base.shader.Shader
import de.bixilon.minosoft.gui.rendering.system.base.texture.TextureManager
class PlanetShader(
override val native: Shader,
) : MinosoftShader() {
) : 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)
}

View File

@ -23,6 +23,8 @@ import de.bixilon.minosoft.data.entities.entities.player.properties.textures.Pla
import de.bixilon.minosoft.data.registries.ResourceLocation
import de.bixilon.minosoft.gui.rendering.RenderConstants
import de.bixilon.minosoft.gui.rendering.gui.atlas.TextureLikeTexture
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.dynamic.DynamicTexture
import de.bixilon.minosoft.gui.rendering.system.base.texture.dynamic.DynamicTextureArray
import de.bixilon.minosoft.gui.rendering.system.base.texture.texture.AbstractTexture
@ -102,4 +104,9 @@ abstract class TextureManager {
}
return alexTexture
}
fun use(shader: Shader, name: String = ShaderUniforms.TEXTURES) {
staticTextures.use(shader, name)
dynamicTextures.use(shader, name)
}
}