shader: outsource some uniform constants

This commit is contained in:
Bixilon 2022-08-16 22:28:52 +02:00
parent c0e6ff06ba
commit b3d36351ca
No known key found for this signature in database
GPG Key ID: 5CAD791931B09AC4
9 changed files with 59 additions and 20 deletions

View File

@ -98,7 +98,7 @@ class FogManager(
val end = fogEnd
val color = fogColor
for (shader in renderWindow.renderSystem.shaders) {
if (!shader.uniforms.contains("uFogColor")) {
if (FOG_COLOR !in shader.uniforms) {
continue
}
@ -107,10 +107,10 @@ class FogManager(
shader["uFogStart"] = start
shader["uFogEnd"] = end
if (color == null) {
shader["uUseFogColor"] = false
shader[USE_FOG_COLOR] = false
} else {
shader["uFogColor"] = color
shader["uUseFogColor"] = true
shader[FOG_COLOR] = color
shader[USE_FOG_COLOR] = true
}
}
updateShaders = false
@ -118,5 +118,8 @@ class FogManager(
companion object {
private val LAVA_FOG_COLOR = RGBColor(0.6f, 0.1f, 0.0f)
private const val FOG_COLOR = "uFogColor"
private const val USE_FOG_COLOR = "uUseFogColor"
}
}

View File

@ -25,6 +25,7 @@ import de.bixilon.minosoft.gui.rendering.camera.frustum.Frustum
import de.bixilon.minosoft.gui.rendering.modding.events.CameraMatrixChangeEvent
import de.bixilon.minosoft.gui.rendering.modding.events.CameraPositionChangeEvent
import de.bixilon.minosoft.gui.rendering.modding.events.ResizeWindowEvent
import de.bixilon.minosoft.gui.rendering.system.base.shader.ShaderUniforms
import de.bixilon.minosoft.gui.rendering.util.vec.vec3.Vec3Util.EMPTY
import de.bixilon.minosoft.gui.rendering.util.vec.vec3.Vec3Util.blockPosition
import de.bixilon.minosoft.gui.rendering.util.vec.vec3.Vec3iUtil.chunkPosition
@ -172,11 +173,11 @@ class MatrixHandler(
private fun updateShaders(cameraPosition: Vec3) {
for (shader in renderWindow.renderSystem.shaders) {
if ("uViewProjectionMatrix" in shader.uniforms) {
shader.use().setMat4("uViewProjectionMatrix", viewProjectionMatrix)
if (ShaderUniforms.VIEW_PROJECTION_MATRIX in shader.uniforms) {
shader.use().setMat4(ShaderUniforms.VIEW_PROJECTION_MATRIX, viewProjectionMatrix)
}
if ("uCameraPosition" in shader.uniforms) {
shader.use().setVec3("uCameraPosition", cameraPosition)
if (ShaderUniforms.CAMERA_POSITION in shader.uniforms) {
shader.use().setVec3(ShaderUniforms.CAMERA_POSITION, cameraPosition)
}
}
}

View File

@ -35,6 +35,7 @@ import de.bixilon.minosoft.gui.rendering.system.base.phases.SkipAll
import de.bixilon.minosoft.gui.rendering.system.base.phases.TranslucentDrawable
import de.bixilon.minosoft.gui.rendering.system.base.phases.TransparentDrawable
import de.bixilon.minosoft.gui.rendering.system.base.shader.Shader
import de.bixilon.minosoft.gui.rendering.system.base.shader.ShaderUniforms
import de.bixilon.minosoft.modding.event.invoker.CallbackEventInvoker
import de.bixilon.minosoft.protocol.network.connection.play.PlayConnection
import de.bixilon.minosoft.protocol.network.connection.play.PlayConnectionStates
@ -111,9 +112,9 @@ class ParticleRenderer(
fun applyToShader(shader: Shader) {
shader.apply {
use()
setMat4("uViewProjectionMatrix", Mat4(it.viewProjectionMatrix))
setVec3("uCameraRight", Vec3(it.viewMatrix[0][0], it.viewMatrix[1][0], it.viewMatrix[2][0]))
setVec3("uCameraUp", Vec3(it.viewMatrix[0][1], it.viewMatrix[1][1], it.viewMatrix[2][1]))
setMat4(ShaderUniforms.VIEW_PROJECTION_MATRIX, Mat4(it.viewProjectionMatrix))
setVec3(ShaderUniforms.CAMERA_RIGHT, Vec3(it.viewMatrix[0][0], it.viewMatrix[1][0], it.viewMatrix[2][0]))
setVec3(ShaderUniforms.CAMERA_UP, Vec3(it.viewMatrix[0][1], it.viewMatrix[1][1], it.viewMatrix[2][1]))
}
}

View File

@ -35,7 +35,7 @@ class SkeletalManager(
shader.loadAnimated()
renderWindow.textureManager.dynamicTextures.use(shader)
shader["uSkeletalBuffer"] = uniformBuffer
shader.setUInt("uLight", 0xFF)
shader.setUInt(LIGHT, 0xFF)
renderWindow.lightMap.use(shader)
}
@ -50,7 +50,7 @@ class SkeletalManager(
fun draw(instance: SkeletalInstance, light: Int) {
prepareDraw()
shader.setUInt("uLight", light)
shader.setUInt(LIGHT, light)
val transforms = instance.calculateTransforms()
var stride = 0
for (transform in transforms) {
@ -66,5 +66,7 @@ class SkeletalManager(
private companion object {
private const val TRANSFORMS = 32
private const val MAT4_SIZE = 4 * 4
private const val LIGHT = "uLight"
}
}

View File

@ -63,7 +63,7 @@ class SkyRenderer(
connection.registerEvent(CallbackEventInvoker.of<CameraMatrixChangeEvent> {
val viewProjectionMatrix = it.projectionMatrix * it.viewMatrix.toMat3().toMat4()
renderWindow.queue += {
skyboxShader.use().setMat4("uSkyViewProjectionMatrix", Mat4(viewProjectionMatrix))
skyboxShader.use().setMat4(SKY_MATRIX, Mat4(viewProjectionMatrix))
setSunMatrix(viewProjectionMatrix)
}
})
@ -82,7 +82,7 @@ class SkyRenderer(
} else {
projectionViewMatrix.rotate(timeAngle, Vec3(0.0f, 0.0f, 1.0f))
}
skySunShader.use().setMat4("uSkyViewProjectionMatrix", rotatedMatrix)
skySunShader.use().setMat4(SKY_MATRIX, rotatedMatrix)
}
override fun postInit(latch: CountUpAndDownLatch) {
@ -134,7 +134,7 @@ class SkyRenderer(
fogManager.fogColor?.let { skyColor = it }
skyboxShader.use().setRGBColor("uSkyColor", skyColor)
skyboxShader.use().setRGBColor(SKY_COLOR, skyColor)
}
private fun drawSkybox() {
@ -151,6 +151,8 @@ class SkyRenderer(
companion object : RendererBuilder<SkyRenderer> {
override val RESOURCE_LOCATION = ResourceLocation("minosoft:sky")
private const val SKY_MATRIX = "uSkyViewProjectionMatrix"
private const val SKY_COLOR = "uSkyColor"
private val SUN_TEXTURE_RESOURCE_LOCATION = ResourceLocation("minecraft:textures/environment/sun.png")

View File

@ -0,0 +1,27 @@
/*
* 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.system.base.shader
object ShaderUniforms {
const val VIEW_PROJECTION_MATRIX = "uViewProjectionMatrix"
const val CAMERA_RIGHT = "uCameraRight"
const val CAMERA_UP = "uCameraUp"
const val CAMERA_POSITION = "uCameraPosition"
const val CAMERA_HEIGHT = "uCameraHeight"
const val TEXTURE_OFFSET = "uTextureOffset"
const val TEXTURES = "uTextures"
}

View File

@ -15,10 +15,11 @@ package de.bixilon.minosoft.gui.rendering.system.base.texture
import de.bixilon.kutil.latch.CountUpAndDownLatch
import de.bixilon.minosoft.gui.rendering.system.base.shader.Shader
import de.bixilon.minosoft.gui.rendering.system.base.shader.ShaderUniforms
interface TextureArray {
fun load(latch: CountUpAndDownLatch)
fun activate()
fun use(shader: Shader, name: String = "uTextures")
fun use(shader: Shader, name: String = ShaderUniforms.TEXTURES)
}

View File

@ -19,6 +19,7 @@ import de.bixilon.kutil.concurrent.pool.DefaultThreadPool
import de.bixilon.kutil.latch.CountUpAndDownLatch
import de.bixilon.minosoft.gui.rendering.RenderWindow
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.dynamic.DynamicTextureState
@ -151,7 +152,7 @@ class OpenGLDynamicTextureArray(
_use(shader, name)
}
private fun _use(shader: Shader, name: String = "uTextures") {
private fun _use(shader: Shader, name: String = ShaderUniforms.TEXTURES) {
shader.use()
activate()

View File

@ -26,6 +26,7 @@ import de.bixilon.minosoft.gui.rendering.renderer.RendererBuilder
import de.bixilon.minosoft.gui.rendering.system.base.BlendingFunctions
import de.bixilon.minosoft.gui.rendering.system.base.RenderSystem
import de.bixilon.minosoft.gui.rendering.system.base.phases.TranslucentDrawable
import de.bixilon.minosoft.gui.rendering.system.base.shader.ShaderUniforms
import de.bixilon.minosoft.gui.rendering.system.base.texture.texture.AbstractTexture
import de.bixilon.minosoft.gui.rendering.textures.TextureUtil.texture
import de.bixilon.minosoft.protocol.network.connection.play.PlayConnection
@ -72,8 +73,8 @@ class WorldBorderRenderer(
offsetReset = time
}
val textureOffset = (offsetReset - time) / ANIMATION_SPEED.toFloat()
shader.setFloat("uTextureOffset", 1.0f - textureOffset)
shader.setFloat("uCameraHeight", renderWindow.camera.matrixHandler.eyePosition.y)
shader.setFloat(ShaderUniforms.TEXTURE_OFFSET, 1.0f - textureOffset)
shader.setFloat(ShaderUniforms.CAMERA_HEIGHT, renderWindow.camera.matrixHandler.eyePosition.y)
val distance = border.getDistanceTo(renderWindow.connection.player.position)
val strength = 1.0f - (distance.toFloat().clamp(0.0f, 100.0f) / 100.0f)