diff --git a/assets/passthru.frag b/assets/passthrurgb.frag similarity index 100% rename from assets/passthru.frag rename to assets/passthrurgb.frag diff --git a/src/net/torvald/terrarum/AppLoader.java b/src/net/torvald/terrarum/AppLoader.java index 7d902b0d1..bf11452a7 100644 --- a/src/net/torvald/terrarum/AppLoader.java +++ b/src/net/torvald/terrarum/AppLoader.java @@ -252,7 +252,7 @@ public class AppLoader implements ApplicationListener { private static ShaderProgram shaderBayerSkyboxFill; public static ShaderProgram shaderHicolour; - public static ShaderProgram shaderPassthru; + public static ShaderProgram shaderPassthruRGB; public static ShaderProgram shaderColLUT; public static Mesh fullscreenQuad; @@ -308,8 +308,8 @@ public class AppLoader implements ApplicationListener { // set GL graphics constants shaderBayerSkyboxFill = loadShader("assets/4096.vert", "assets/4096_bayer_skyboxfill.frag"); shaderHicolour = loadShader("assets/4096.vert", "assets/hicolour.frag"); - shaderPassthru = loadShader("assets/4096.vert", "assets/passthru.frag"); - shaderColLUT = loadShader("assets/4096.vert", "assets/passthru.frag"); + shaderPassthruRGB = loadShader("assets/4096.vert", "assets/passthrurgb.frag"); + shaderColLUT = loadShader("assets/4096.vert", "assets/passthrurgb.frag"); fullscreenQuad = new Mesh( true, 4, 6, @@ -513,7 +513,7 @@ public class AppLoader implements ApplicationListener { shaderBayerSkyboxFill.dispose(); shaderHicolour.dispose(); - shaderPassthru.dispose(); + shaderPassthruRGB.dispose(); shaderColLUT.dispose(); assetManager.dispose(); diff --git a/src/net/torvald/terrarum/PostProcessor.kt b/src/net/torvald/terrarum/PostProcessor.kt index 556a77da4..25bee1e60 100644 --- a/src/net/torvald/terrarum/PostProcessor.kt +++ b/src/net/torvald/terrarum/PostProcessor.kt @@ -93,7 +93,7 @@ object PostProcessor { if (AppLoader.getConfigBoolean("fxdither")) AppLoader.shaderHicolour else - AppLoader.shaderPassthru + AppLoader.shaderPassthruRGB fbo.colorBufferTexture.bind(0) diff --git a/src/net/torvald/terrarum/Terrarum.kt b/src/net/torvald/terrarum/Terrarum.kt index ed105b547..9c8bfa1a7 100644 --- a/src/net/torvald/terrarum/Terrarum.kt +++ b/src/net/torvald/terrarum/Terrarum.kt @@ -295,7 +295,7 @@ object Terrarum : Screen { shaderSkyboxFill.end() } else { - shaderBayer = AppLoader.loadShader("assets/4096.vert", "assets/passthru.frag") + shaderBayer = AppLoader.loadShader("assets/4096.vert", "assets/passthrurgb.frag") shaderSkyboxFill = AppLoader.loadShader("assets/4096.vert", "assets/skyboxfill.frag") } diff --git a/src/net/torvald/terrarum/modulebasegame/IngameRenderer.kt b/src/net/torvald/terrarum/modulebasegame/IngameRenderer.kt index d882cf66b..1d6622835 100644 --- a/src/net/torvald/terrarum/modulebasegame/IngameRenderer.kt +++ b/src/net/torvald/terrarum/modulebasegame/IngameRenderer.kt @@ -31,6 +31,8 @@ object IngameRenderer { lateinit var batch: SpriteBatch private lateinit var camera: OrthographicCamera + private lateinit var blurWriteQuad: Mesh + private lateinit var lightmapFboA: FrameBuffer private lateinit var lightmapFboB: FrameBuffer private lateinit var fboRGB: FrameBuffer @@ -45,6 +47,7 @@ object IngameRenderer { private val shaderBlendGlow = Terrarum.shaderBlendGlow private val shaderRGBOnly = Terrarum.shaderRGBOnly private val shaderAtoGrey = Terrarum.shaderAtoGrey + private val shaderPassthru = SpriteBatch.createDefaultShader() private val width = Terrarum.WIDTH private val height = Terrarum.HEIGHT @@ -490,44 +493,42 @@ object IngameRenderer { // initialise readBuffer with untreated lightmap blurReadBuffer.inAction(camera, batch) { - batch.inUse { - blendDisable(batch) - batch.color = Color.WHITE - LightmapRenderer.draw(batch) - } + val texture = LightmapRenderer.draw() + texture.bind(0) + + shaderPassthru.begin() + shaderPassthru.setUniformMatrix("u_projTrans", camera.combined) + shaderPassthru.setUniformi("u_texture", 0) + blurWriteQuad.render(shaderPassthru, GL20.GL_TRIANGLES) + shaderPassthru.end() } - - - + // do blurring for (i in 0 until blurIterations) { blurWriteBuffer.inAction(camera, batch) { - batch.inUse { - val texture = blurReadBuffer.colorBufferTexture + val texture = blurReadBuffer.colorBufferTexture + texture.setFilter(Texture.TextureFilter.Linear, Texture.TextureFilter.Linear) + texture.bind(0) - texture.setFilter(Texture.TextureFilter.Linear, Texture.TextureFilter.Linear) + shaderBlur.begin() + shaderBlur.setUniformMatrix("u_projTrans", camera.combined) + shaderBlur.setUniformi("u_texture", 0) + shaderBlur.setUniformf("iResolution", + blurWriteBuffer.width.toFloat(), blurWriteBuffer.height.toFloat()) + shaderBlur.setUniformf("flip", 1f) + if (i % 2 == 0) + shaderBlur.setUniformf("direction", blurRadius, 0f) + else + shaderBlur.setUniformf("direction", 0f, blurRadius) + blurWriteQuad.render(shaderBlur, GL20.GL_TRIANGLES) + shaderBlur.end() - batch.shader = shaderBlur - batch.shader.setUniformf("iResolution", - blurWriteBuffer.width.toFloat(), blurWriteBuffer.height.toFloat()) - batch.shader.setUniformf("flip", 1f) - if (i % 2 == 0) - batch.shader.setUniformf("direction", blurRadius, 0f) - else - batch.shader.setUniformf("direction", 0f, blurRadius) - - - batch.color = Color.WHITE - batch.draw(texture, 0f, 0f) - - - // swap - val t = blurWriteBuffer - blurWriteBuffer = blurReadBuffer - blurReadBuffer = t - } + // swap + val t = blurWriteBuffer + blurWriteBuffer = blurReadBuffer + blurReadBuffer = t } } @@ -540,6 +541,13 @@ object IngameRenderer { fun resize(width: Int, height: Int) { if (!init) { + blurWriteQuad = Mesh( + true, 4, 6, + VertexAttribute.Position(), + VertexAttribute.ColorUnpacked(), + VertexAttribute.TexCoords(0) + ) + init = true } else { @@ -572,7 +580,13 @@ object IngameRenderer { LightmapRenderer.resize(width, height) - //LightmapRenderer.fireRecalculateEvent() + blurWriteQuad.setVertices(floatArrayOf( + 0f,0f,0f, 1f,1f,1f,1f, 0f,1f, + lightmapFboA.width.toFloat(),0f,0f, 1f,1f,1f,1f, 1f,1f, + lightmapFboA.width.toFloat(),lightmapFboA.height.toFloat(),0f, 1f,1f,1f,1f, 1f,0f, + 0f,lightmapFboA.height.toFloat(),0f, 1f,1f,1f,1f, 0f,0f)) + blurWriteQuad.setIndices(shortArrayOf(0, 1, 2, 2, 3, 0)) + } private val TILE_SIZEF = FeaturesDrawer.TILE_SIZE.toFloat() diff --git a/src/net/torvald/terrarum/worlddrawer/LightmapRendererNew.kt b/src/net/torvald/terrarum/worlddrawer/LightmapRendererNew.kt index bfa520b35..13de3a0b3 100644 --- a/src/net/torvald/terrarum/worlddrawer/LightmapRendererNew.kt +++ b/src/net/torvald/terrarum/worlddrawer/LightmapRendererNew.kt @@ -1,11 +1,8 @@ package net.torvald.terrarum.worlddrawer -import com.badlogic.gdx.Gdx import com.badlogic.gdx.graphics.Color -import com.badlogic.gdx.graphics.GL20 import com.badlogic.gdx.graphics.Pixmap import com.badlogic.gdx.graphics.Texture -import com.badlogic.gdx.graphics.g2d.SpriteBatch import com.badlogic.gdx.graphics.glutils.ShaderProgram import com.jme3.math.FastMath import net.torvald.terrarum.* @@ -559,7 +556,7 @@ object LightmapRenderer { private var _lightBufferAsTex: Texture = Texture(1, 1, Pixmap.Format.RGBA8888) - internal fun draw(batch: SpriteBatch) { + internal fun draw(): Texture { // when shader is not used: 0.5 ms on 6700K AppLoader.measureDebugTime("Renderer.LightToScreen") { @@ -600,12 +597,14 @@ object LightmapRenderer { _lightBufferAsTex = Texture(lightBuffer) _lightBufferAsTex.setFilter(Texture.TextureFilter.Nearest, Texture.TextureFilter.Nearest) - - Gdx.gl.glActiveTexture(GL20.GL_TEXTURE0) // so that batch that comes next will bind any tex to it + /*Gdx.gl.glActiveTexture(GL20.GL_TEXTURE0) // so that batch that comes next will bind any tex to it // we might not need shader here... //batch.draw(lightBufferAsTex, 0f, 0f, lightBufferAsTex.width.toFloat(), lightBufferAsTex.height.toFloat()) batch.draw(_lightBufferAsTex, 0f, 0f, _lightBufferAsTex.width * DRAW_TILE_SIZE, _lightBufferAsTex.height * DRAW_TILE_SIZE) + */ } + + return _lightBufferAsTex } fun dispose() {