mirror of
https://gitlab.bixilon.de/bixilon/minosoft.git
synced 2025-09-17 19:35:00 -04:00
wip safe uniforms
This commit is contained in:
parent
4d48a4b263
commit
9171268e5f
@ -0,0 +1,49 @@
|
|||||||
|
/*
|
||||||
|
* 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
|
||||||
|
import kotlin.reflect.KFunction3
|
||||||
|
|
||||||
|
abstract class MinosoftShader {
|
||||||
|
private val uniforms: MutableMap<String, ShaderUniform<*>> = mutableMapOf()
|
||||||
|
abstract val native: Shader
|
||||||
|
|
||||||
|
fun load() {
|
||||||
|
native.load()
|
||||||
|
}
|
||||||
|
|
||||||
|
fun postLoad() {
|
||||||
|
for (uniform in uniforms.values) {
|
||||||
|
uniform.upload()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fun use() {
|
||||||
|
native.use()
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
fun <T> uniform(name: String, default: T, type: KFunction3<Shader, String, T, Unit> = Shader::set): ShaderUniform<T> {
|
||||||
|
val uniform = ShaderUniform(native, default, name, type)
|
||||||
|
val previous = uniforms.put(name, uniform)
|
||||||
|
|
||||||
|
if (previous != null) {
|
||||||
|
throw IllegalStateException("Duplicated uniform: $name")
|
||||||
|
}
|
||||||
|
|
||||||
|
return uniform
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,54 @@
|
|||||||
|
/*
|
||||||
|
* 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.uniform
|
||||||
|
|
||||||
|
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>,
|
||||||
|
) : ReadWriteProperty<Any, T> {
|
||||||
|
private var value = default
|
||||||
|
private var upload: Boolean = true
|
||||||
|
|
||||||
|
fun upload() {
|
||||||
|
if (!upload) {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
native.use()
|
||||||
|
setter(native, name, value)
|
||||||
|
upload = false
|
||||||
|
}
|
||||||
|
|
||||||
|
override fun getValue(thisRef: Any, property: KProperty<*>): T {
|
||||||
|
return value
|
||||||
|
}
|
||||||
|
|
||||||
|
override fun setValue(thisRef: Any, property: KProperty<*>, value: T) {
|
||||||
|
if (this.value == value) {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
this.value = value
|
||||||
|
upload = true
|
||||||
|
if (Thread.currentThread() == native.renderWindow.thread) {
|
||||||
|
upload()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -31,7 +31,7 @@ abstract class PlanetRenderer(
|
|||||||
protected val sky: SkyRenderer,
|
protected val sky: SkyRenderer,
|
||||||
) : SkyChildRenderer {
|
) : SkyChildRenderer {
|
||||||
protected abstract val texture: AbstractTexture
|
protected abstract val texture: AbstractTexture
|
||||||
protected val shader = sky.renderWindow.renderSystem.createShader(minosoft("sky/planet"))
|
protected val shader = sky.renderWindow.renderSystem.createShader(minosoft("sky/planet")) { PlanetShader(it) }
|
||||||
private var mesh = PlanetMesh(sky.renderWindow)
|
private var mesh = PlanetMesh(sky.renderWindow)
|
||||||
protected var day = -1L
|
protected var day = -1L
|
||||||
protected var matrix = Mat4()
|
protected var matrix = Mat4()
|
||||||
@ -69,6 +69,7 @@ abstract class PlanetRenderer(
|
|||||||
|
|
||||||
override fun postInit() {
|
override fun postInit() {
|
||||||
prepareMesh()
|
prepareMesh()
|
||||||
|
shader.postLoad()
|
||||||
sky.renderWindow.textureManager.staticTextures.use(shader)
|
sky.renderWindow.textureManager.staticTextures.use(shader)
|
||||||
sky::matrix.observe(this) { calculateMatrix(it) }
|
sky::matrix.observe(this) { calculateMatrix(it) }
|
||||||
}
|
}
|
||||||
@ -108,11 +109,11 @@ abstract class PlanetRenderer(
|
|||||||
}
|
}
|
||||||
shader.use()
|
shader.use()
|
||||||
if (matrixUpdate) {
|
if (matrixUpdate) {
|
||||||
shader.setMat4("uMatrix", matrix)
|
shader.matrix = matrix
|
||||||
|
|
||||||
val intensity = calculateIntensity()
|
val intensity = calculateIntensity()
|
||||||
if (this.intensity != intensity) {
|
if (this.intensity != intensity) {
|
||||||
shader.setVec4("uTintColor", Vec4(1.0f, 1.0f, 1.0f, intensity))
|
shader.tintColor = Vec4(1.0f, 1.0f, 1.0f, intensity)
|
||||||
this.intensity = intensity
|
this.intensity = intensity
|
||||||
}
|
}
|
||||||
this.matrixUpdate = false
|
this.matrixUpdate = false
|
||||||
|
@ -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.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.system.base.shader.Shader
|
||||||
|
|
||||||
|
class PlanetShader(
|
||||||
|
override val native: Shader,
|
||||||
|
) : MinosoftShader() {
|
||||||
|
var matrix: Mat4 by uniform("uMatrix", Mat4())
|
||||||
|
var tintColor: Vec4 by uniform("uTintColor", Vec4())
|
||||||
|
}
|
@ -18,6 +18,7 @@ import de.bixilon.minosoft.data.registries.ResourceLocation
|
|||||||
import de.bixilon.minosoft.data.text.formatting.color.Colors
|
import de.bixilon.minosoft.data.text.formatting.color.Colors
|
||||||
import de.bixilon.minosoft.data.text.formatting.color.RGBColor
|
import de.bixilon.minosoft.data.text.formatting.color.RGBColor
|
||||||
import de.bixilon.minosoft.gui.rendering.RenderWindow
|
import de.bixilon.minosoft.gui.rendering.RenderWindow
|
||||||
|
import de.bixilon.minosoft.gui.rendering.shader.MinosoftShader
|
||||||
import de.bixilon.minosoft.gui.rendering.system.base.buffer.frame.Framebuffer
|
import de.bixilon.minosoft.gui.rendering.system.base.buffer.frame.Framebuffer
|
||||||
import de.bixilon.minosoft.gui.rendering.system.base.buffer.uniform.FloatUniformBuffer
|
import de.bixilon.minosoft.gui.rendering.system.base.buffer.uniform.FloatUniformBuffer
|
||||||
import de.bixilon.minosoft.gui.rendering.system.base.buffer.uniform.IntUniformBuffer
|
import de.bixilon.minosoft.gui.rendering.system.base.buffer.uniform.IntUniformBuffer
|
||||||
@ -108,6 +109,10 @@ interface RenderSystem {
|
|||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fun <T : MinosoftShader> createShader(resourceLocation: ResourceLocation, creator: (native: Shader) -> T): T {
|
||||||
|
return creator(createShader(resourceLocation))
|
||||||
|
}
|
||||||
|
|
||||||
fun createShader(vertex: ResourceLocation, geometry: ResourceLocation? = null, fragment: ResourceLocation): Shader
|
fun createShader(vertex: ResourceLocation, geometry: ResourceLocation? = null, fragment: ResourceLocation): Shader
|
||||||
|
|
||||||
fun createVertexBuffer(structure: MeshStruct, data: FloatBuffer, primitiveType: PrimitiveTypes = preferredPrimitiveType): FloatVertexBuffer
|
fun createVertexBuffer(structure: MeshStruct, data: FloatBuffer, primitiveType: PrimitiveTypes = preferredPrimitiveType): FloatVertexBuffer
|
||||||
|
@ -14,6 +14,7 @@
|
|||||||
package de.bixilon.minosoft.gui.rendering.system.base.texture
|
package de.bixilon.minosoft.gui.rendering.system.base.texture
|
||||||
|
|
||||||
import de.bixilon.kutil.latch.CountUpAndDownLatch
|
import de.bixilon.kutil.latch.CountUpAndDownLatch
|
||||||
|
import de.bixilon.minosoft.gui.rendering.shader.MinosoftShader
|
||||||
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.shader.ShaderUniforms
|
import de.bixilon.minosoft.gui.rendering.system.base.shader.ShaderUniforms
|
||||||
|
|
||||||
@ -21,5 +22,8 @@ interface TextureArray {
|
|||||||
fun load(latch: CountUpAndDownLatch)
|
fun load(latch: CountUpAndDownLatch)
|
||||||
|
|
||||||
fun activate()
|
fun activate()
|
||||||
|
|
||||||
|
@Deprecated("safe uniforms")
|
||||||
fun use(shader: Shader, name: String = ShaderUniforms.TEXTURES)
|
fun use(shader: Shader, name: String = ShaderUniforms.TEXTURES)
|
||||||
|
fun use(shader: MinosoftShader) = use(shader.native)
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user