From 52938a4b604141c539b119ba23d14af94df5c20e Mon Sep 17 00:00:00 2001 From: minjaesong Date: Tue, 22 Aug 2023 17:43:53 +0900 Subject: [PATCH] improved cloud draw perf --- .../torvald/terrarum/weather/WeatherMixer.kt | 23 ++++++------------- .../terrarum/weather/WeatherObjectCloud.kt | 13 ----------- src/shaders/blendGlow.frag | 5 ++-- src/shaders/blendSkyboxStars.frag | 7 ++++-- src/shaders/clouds.frag | 13 +++++++---- src/shaders/float_to_disp_dither_static.frag | 5 ++-- src/shaders/ghastlywhite.frag | 5 ++-- src/shaders/postproc_dither.frag | 4 ++-- src/shaders/postproc_nodither.frag | 4 ++-- src/shaders/tiling.frag | 4 ++-- src/shaders/tiling_dither.frag | 4 ++-- 11 files changed, 38 insertions(+), 49 deletions(-) diff --git a/src/net/torvald/terrarum/weather/WeatherMixer.kt b/src/net/torvald/terrarum/weather/WeatherMixer.kt index f9db63143..1295951e2 100644 --- a/src/net/torvald/terrarum/weather/WeatherMixer.kt +++ b/src/net/torvald/terrarum/weather/WeatherMixer.kt @@ -310,15 +310,6 @@ internal object WeatherMixer : RNGConsumer { WeatherObjectCloud(cloud.spriteSheet.get(sheetX, sheetY), flip).also { it.scale = cloudScale - it.darkness.set(currentWeather.cloudGamma) - it.darkness.x *= it.scale - - val varX = 1f + r1.absoluteValue * currentWeather.cloudGammaVariance.x - val varY = 1f + r2.absoluteValue * currentWeather.cloudGammaVariance.y - - it.darkness.x *= if (r1 < 0) 1f / varX else varX - it.darkness.y *= if (r2 < 0) 1f / varY else varY - it.posX = posX it.posY = posY it.posZ = rZ @@ -369,13 +360,13 @@ internal object WeatherMixer : RNGConsumer { } private fun drawClouds(batch: SpriteBatch) { - batch.shader = shaderClouds - clouds.forEach { - batch.inUse { _ -> - batch.color = globalLightNow.toGdxColor().also { col -> - col.a = it.alpha - } // TODO add cloud-only colour strip on the CLUT - batch.shader.setUniformf("gamma", it.darkness) + batch.inUse { _ -> + batch.shader = shaderClouds + batch.shader.setUniformf("gamma", currentWeather.cloudGamma) + batch.shader.setUniformf("shadeCol", 0.06f, 0.07f, 0.08f, 1f) // TODO temporary value + + clouds.forEach { + batch.color = Color(globalLightNow.r, globalLightNow.g, globalLightNow.b, it.alpha) it.render(batch, 0f, 0f) } } diff --git a/src/net/torvald/terrarum/weather/WeatherObjectCloud.kt b/src/net/torvald/terrarum/weather/WeatherObjectCloud.kt index a401ebec2..0b3b97deb 100644 --- a/src/net/torvald/terrarum/weather/WeatherObjectCloud.kt +++ b/src/net/torvald/terrarum/weather/WeatherObjectCloud.kt @@ -13,19 +13,6 @@ import kotlin.math.sign */ class WeatherObjectCloud(private val texture: TextureRegion, private val flipW: Boolean) : WeatherObject(), Comparable { - /** - * To actually utilise this value, your render code must begin the spritebatch per-object, like so: - * ``` - * batch.shader = cloudShader - * for (it in clouds) { - * batch.begin() - * batch.shader.setUniformf("gamma", it.darkness) - * batch.draw(it, ...) - * batch.end() - * } - */ - var darkness: Vector2 = Vector2(0.5f, 2.0f) // the "gamma" value fed into the clouds shader - override fun update() { throw UnsupportedOperationException() } diff --git a/src/shaders/blendGlow.frag b/src/shaders/blendGlow.frag index 1dc605568..a84351850 100644 --- a/src/shaders/blendGlow.frag +++ b/src/shaders/blendGlow.frag @@ -1,4 +1,4 @@ -#version 150 +#version 400 #ifdef GL_ES precision mediump float; #endif @@ -16,5 +16,6 @@ const vec2 boolean = vec2(0.0, 1.0); void main(void) { vec4 colorTex0 = texture(u_texture, v_texCoords); // lightmap (RGB) pre-mixed vec4 colorTex1 = texture(tex1, v_texCoords); // lightmap (A) pre-mixed - fragColor = (max(colorTex0, colorTex1) * boolean.yyyx) + (colorTex0 * boolean.xxxy); +// fragColor = (max(colorTex0, colorTex1) * boolean.yyyx) + (colorTex0 * boolean.xxxy); + fragColor = fma(max(colorTex0, colorTex1), boolean.yyyx, colorTex0 * boolean.xxxy); } \ No newline at end of file diff --git a/src/shaders/blendSkyboxStars.frag b/src/shaders/blendSkyboxStars.frag index 8bf696b84..5f63b83f2 100644 --- a/src/shaders/blendSkyboxStars.frag +++ b/src/shaders/blendSkyboxStars.frag @@ -1,4 +1,4 @@ -#version 150 +#version 400 #ifdef GL_ES precision mediump float; #endif @@ -150,7 +150,10 @@ void main(void) { ); // c = c0..c1 - fragColor = (max(colorTex0, colorTex1) * boolean.yyyx) + boolean.xxxy; +// fragColor = (max(colorTex0, colorTex1) * boolean.yyyx) + boolean.xxxy; + fragColor = fma(max(colorTex0, colorTex1), boolean.yyyx, boolean.xxxy); + + // fragColor = colorTex1; // fragColor = randomness * boolean.yyyx + boolean.xxxy; // fragColor = (randomness.rrrr + (colorTex1 * vec4(2.0, -2.0, 2.0, 1.0))) * boolean.yyyx + boolean.xxxy; diff --git a/src/shaders/clouds.frag b/src/shaders/clouds.frag index 5b083d082..9f1d16637 100644 --- a/src/shaders/clouds.frag +++ b/src/shaders/clouds.frag @@ -1,4 +1,4 @@ -#version 150 +#version 400 #ifdef GL_ES #define LOWP lowp @@ -7,7 +7,7 @@ precision mediump float; #define LOWP #endif -in LOWP vec4 v_color; +in LOWP vec4 v_color; // lightCol.rgb + cloud's alpha in vec2 v_texCoords; uniform sampler2D u_texture; out vec4 fragColor; @@ -16,10 +16,15 @@ const vec2 boolean = vec2(0.0, 1.0); uniform vec2 gamma = vec2(10, 2.0); // vec2(gamma for RGB, gamma for A) +uniform LOWP vec4 shadeCol; + void main() { + // r: bw diffuse map, g: normal, b: normal, a: bw diffuse alpha vec4 inCol = texture(u_texture, v_texCoords); + vec4 rawCol = pow(inCol, gamma.xxxy); - vec4 outCol = pow(inCol, gamma.xxxy); + // do gradient mapping here + vec4 outCol = fma(mix(shadeCol, v_color, rawCol.r), boolean.yyyx, rawCol * boolean.xxxy); - fragColor = outCol * v_color; + fragColor = outCol * fma(v_color, boolean.xxxy, boolean.yyyx); } \ No newline at end of file diff --git a/src/shaders/float_to_disp_dither_static.frag b/src/shaders/float_to_disp_dither_static.frag index 431adb9ab..aceb8aca2 100644 --- a/src/shaders/float_to_disp_dither_static.frag +++ b/src/shaders/float_to_disp_dither_static.frag @@ -1,4 +1,4 @@ -#version 150 +#version 400 #ifdef GL_ES precision mediump float; #endif @@ -41,7 +41,8 @@ vec4 getDitherredDot(vec4 inColor) { void main(void) { - float scale = v_texCoords.y * (1.0 - parallax_size) + (parallax_size / 2.0) + (parallax * parallax_size / 2.0); + float parallaxAdder = 0.5 * (parallax + 1.0) * parallax_size; + float scale = fma(v_texCoords.y, 1.0 - parallax_size, parallaxAdder); float zoomSamplePoint = (1.0 - zoomInv) / 2.0;// will never quite exceed 0.5 diff --git a/src/shaders/ghastlywhite.frag b/src/shaders/ghastlywhite.frag index 3221ed288..2e4ee91dd 100644 --- a/src/shaders/ghastlywhite.frag +++ b/src/shaders/ghastlywhite.frag @@ -1,4 +1,4 @@ -#version 150 +#version 400 in vec4 v_color; in vec2 v_texCoords; uniform sampler2D u_texture; @@ -11,5 +11,6 @@ void main(void) { vec4 incolour = texture(u_texture, v_texCoords); float lum = dot(incolour * desaturate, boolean.yyyx) * 0.5 + 0.5; - fragColor = v_color * (vec4(lum) * boolean.yyyx + incolour * boolean.xxxy); +// fragColor = v_color * (vec4(lum) * boolean.yyyx + incolour * boolean.xxxy); + fragColor = v_color * fma(vec4(lum), boolean.yyyx, incolour * boolean.xxxy); } \ No newline at end of file diff --git a/src/shaders/postproc_dither.frag b/src/shaders/postproc_dither.frag index 804ae6477..436b123d1 100644 --- a/src/shaders/postproc_dither.frag +++ b/src/shaders/postproc_dither.frag @@ -3,7 +3,7 @@ * http://momentsingraphics.de/BlueNoise.html */ -#version 150 +#version 400 #ifdef GL_ES precision mediump float; #endif @@ -79,7 +79,7 @@ void main(void) { // Dither the output vec4 graded = ycocg_to_rgb * newColour; vec4 selvec = getDitherredDot(graded); - vec4 outcol = selvec * boolean.yyyx + boolean.xxxy; // use quantised RGB but not the A + vec4 outcol = fma(selvec, boolean.yyyx, boolean.xxxy); // use quantised RGB but not the A fragColor = outcol; // ivec4 bytes = ivec4(255.0 * outcol); diff --git a/src/shaders/postproc_nodither.frag b/src/shaders/postproc_nodither.frag index 8810e48e9..be4152967 100644 --- a/src/shaders/postproc_nodither.frag +++ b/src/shaders/postproc_nodither.frag @@ -3,7 +3,7 @@ * http://momentsingraphics.de/BlueNoise.html */ -#version 150 +#version 400 #ifdef GL_ES precision mediump float; #endif @@ -60,5 +60,5 @@ void main(void) { // Dither the output vec4 graded = ycocg_to_rgb * newColour; - fragColor = graded * boolean.yyyx + boolean.xxxy; // use quantised RGB but not the A + fragColor = fma(graded, boolean.yyyx, boolean.xxxy); // use quantised RGB but not the A } \ No newline at end of file diff --git a/src/shaders/tiling.frag b/src/shaders/tiling.frag index 70dcf0ad0..4470f7398 100644 --- a/src/shaders/tiling.frag +++ b/src/shaders/tiling.frag @@ -2,7 +2,7 @@ */ -#version 150 +#version 400 #ifdef GL_ES precision mediump float; #endif @@ -107,7 +107,7 @@ void main() { vec4 finalBreakage = drawBreakage * texture(tilesAtlas, finalUVCoordForBreakage); // drawBreakeage = 0 to not draw, = 1 to draw - vec4 finalColor =mix(finalTile, finalBreakage, finalBreakage.a) * bc.xxxy + (finalTile * bc.yyyx); + vec4 finalColor = fma(mix(finalTile, finalBreakage, finalBreakage.a), bc.xxxy, finalTile * bc.yyyx); fragColor = mix(colourFilter, colourFilter * finalColor, mulBlendIntensity); diff --git a/src/shaders/tiling_dither.frag b/src/shaders/tiling_dither.frag index 167387434..6f45c4458 100644 --- a/src/shaders/tiling_dither.frag +++ b/src/shaders/tiling_dither.frag @@ -2,7 +2,7 @@ */ -#version 150 +#version 400 #ifdef GL_ES precision mediump float; #endif @@ -121,5 +121,5 @@ void main() { vec2 entry = mod(gl_FragCoord.xy, vec2(bayerSize, bayerSize)); float bayerThreshold = float(bayer[int(entry.y) * int(bayerSize) + int(entry.x)]) / bayerDivider; - fragColor = undithered * bc.xxxy + vec4((undithered.a > bayerThreshold) ? 1.0 : 0.0) * bc.yyyx; + fragColor = fma(undithered, bc.xxxy, vec4((undithered.a > bayerThreshold) ? 1.0 : 0.0) * bc.yyyx); }