mirror of
https://gitlab.bixilon.de/bixilon/minosoft.git
synced 2025-09-18 20:05:02 -04:00
safe uniforms: textures
This commit is contained in:
parent
9171268e5f
commit
2c78e3c80c
@ -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>
|
||||||
|
}
|
@ -14,12 +14,9 @@
|
|||||||
package de.bixilon.minosoft.gui.rendering.shader
|
package de.bixilon.minosoft.gui.rendering.shader
|
||||||
|
|
||||||
import de.bixilon.minosoft.gui.rendering.shader.uniform.ShaderUniform
|
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()
|
private val uniforms: MutableMap<String, ShaderUniform<*>> = mutableMapOf()
|
||||||
abstract val native: Shader
|
|
||||||
|
|
||||||
fun load() {
|
fun load() {
|
||||||
native.load()
|
native.load()
|
||||||
@ -35,8 +32,15 @@ abstract class MinosoftShader {
|
|||||||
native.use()
|
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 uniform = ShaderUniform(native, default, name, type)
|
||||||
val previous = uniforms.put(name, uniform)
|
val previous = uniforms.put(name, uniform)
|
||||||
|
|
||||||
|
@ -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
|
@ -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)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -13,16 +13,16 @@
|
|||||||
|
|
||||||
package de.bixilon.minosoft.gui.rendering.shader.uniform
|
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 de.bixilon.minosoft.gui.rendering.system.base.shader.Shader
|
||||||
import kotlin.properties.ReadWriteProperty
|
import kotlin.properties.ReadWriteProperty
|
||||||
import kotlin.reflect.KFunction3
|
|
||||||
import kotlin.reflect.KProperty
|
import kotlin.reflect.KProperty
|
||||||
|
|
||||||
class ShaderUniform<T>(
|
class ShaderUniform<T>(
|
||||||
private val native: Shader,
|
private val native: Shader,
|
||||||
default: T,
|
default: T,
|
||||||
private val name: String,
|
private val name: String,
|
||||||
private val setter: KFunction3<Shader, String, T, Unit>,
|
private val setter: ShaderSetter<T>,
|
||||||
) : ReadWriteProperty<Any, T> {
|
) : ReadWriteProperty<Any, T> {
|
||||||
private var value = default
|
private var value = default
|
||||||
private var upload: Boolean = true
|
private var upload: Boolean = true
|
||||||
@ -31,6 +31,10 @@ class ShaderUniform<T>(
|
|||||||
if (!upload) {
|
if (!upload) {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
forceUpload()
|
||||||
|
}
|
||||||
|
|
||||||
|
fun forceUpload() {
|
||||||
native.use()
|
native.use()
|
||||||
setter(native, name, value)
|
setter(native, name, value)
|
||||||
upload = false
|
upload = false
|
||||||
|
@ -70,7 +70,6 @@ abstract class PlanetRenderer(
|
|||||||
override fun postInit() {
|
override fun postInit() {
|
||||||
prepareMesh()
|
prepareMesh()
|
||||||
shader.postLoad()
|
shader.postLoad()
|
||||||
sky.renderWindow.textureManager.staticTextures.use(shader)
|
|
||||||
sky::matrix.observe(this) { calculateMatrix(it) }
|
sky::matrix.observe(this) { calculateMatrix(it) }
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -16,11 +16,15 @@ package de.bixilon.minosoft.gui.rendering.sky.planet
|
|||||||
import de.bixilon.kotlinglm.mat4x4.Mat4
|
import de.bixilon.kotlinglm.mat4x4.Mat4
|
||||||
import de.bixilon.kotlinglm.vec4.Vec4
|
import de.bixilon.kotlinglm.vec4.Vec4
|
||||||
import de.bixilon.minosoft.gui.rendering.shader.MinosoftShader
|
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.shader.Shader
|
||||||
|
import de.bixilon.minosoft.gui.rendering.system.base.texture.TextureManager
|
||||||
|
|
||||||
class PlanetShader(
|
class PlanetShader(
|
||||||
override val native: Shader,
|
override val native: Shader,
|
||||||
) : MinosoftShader() {
|
) : MinosoftShader(), TextureShader {
|
||||||
var matrix: Mat4 by uniform("uMatrix", Mat4())
|
var matrix: Mat4 by uniform("uMatrix", Mat4())
|
||||||
var tintColor: Vec4 by uniform("uTintColor", Vec4())
|
var tintColor: Vec4 by uniform("uTintColor", Vec4())
|
||||||
|
override val textures: TextureManager by textureManager(native.renderWindow.textureManager, false)
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -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.data.registries.ResourceLocation
|
||||||
import de.bixilon.minosoft.gui.rendering.RenderConstants
|
import de.bixilon.minosoft.gui.rendering.RenderConstants
|
||||||
import de.bixilon.minosoft.gui.rendering.gui.atlas.TextureLikeTexture
|
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.DynamicTexture
|
||||||
import de.bixilon.minosoft.gui.rendering.system.base.texture.dynamic.DynamicTextureArray
|
import de.bixilon.minosoft.gui.rendering.system.base.texture.dynamic.DynamicTextureArray
|
||||||
import de.bixilon.minosoft.gui.rendering.system.base.texture.texture.AbstractTexture
|
import de.bixilon.minosoft.gui.rendering.system.base.texture.texture.AbstractTexture
|
||||||
@ -102,4 +104,9 @@ abstract class TextureManager {
|
|||||||
}
|
}
|
||||||
return alexTexture
|
return alexTexture
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fun use(shader: Shader, name: String = ShaderUniforms.TEXTURES) {
|
||||||
|
staticTextures.use(shader, name)
|
||||||
|
dynamicTextures.use(shader, name)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user