lightmap drawing won't use spritebatch

This commit is contained in:
minjaesong
2019-02-20 23:06:35 +09:00
parent 72ad6dc4e0
commit b55fe09d9f
6 changed files with 56 additions and 43 deletions

View File

@@ -252,7 +252,7 @@ public class AppLoader implements ApplicationListener {
private static ShaderProgram shaderBayerSkyboxFill; private static ShaderProgram shaderBayerSkyboxFill;
public static ShaderProgram shaderHicolour; public static ShaderProgram shaderHicolour;
public static ShaderProgram shaderPassthru; public static ShaderProgram shaderPassthruRGB;
public static ShaderProgram shaderColLUT; public static ShaderProgram shaderColLUT;
public static Mesh fullscreenQuad; public static Mesh fullscreenQuad;
@@ -308,8 +308,8 @@ public class AppLoader implements ApplicationListener {
// set GL graphics constants // set GL graphics constants
shaderBayerSkyboxFill = loadShader("assets/4096.vert", "assets/4096_bayer_skyboxfill.frag"); shaderBayerSkyboxFill = loadShader("assets/4096.vert", "assets/4096_bayer_skyboxfill.frag");
shaderHicolour = loadShader("assets/4096.vert", "assets/hicolour.frag"); shaderHicolour = loadShader("assets/4096.vert", "assets/hicolour.frag");
shaderPassthru = loadShader("assets/4096.vert", "assets/passthru.frag"); shaderPassthruRGB = loadShader("assets/4096.vert", "assets/passthrurgb.frag");
shaderColLUT = loadShader("assets/4096.vert", "assets/passthru.frag"); shaderColLUT = loadShader("assets/4096.vert", "assets/passthrurgb.frag");
fullscreenQuad = new Mesh( fullscreenQuad = new Mesh(
true, 4, 6, true, 4, 6,
@@ -513,7 +513,7 @@ public class AppLoader implements ApplicationListener {
shaderBayerSkyboxFill.dispose(); shaderBayerSkyboxFill.dispose();
shaderHicolour.dispose(); shaderHicolour.dispose();
shaderPassthru.dispose(); shaderPassthruRGB.dispose();
shaderColLUT.dispose(); shaderColLUT.dispose();
assetManager.dispose(); assetManager.dispose();

View File

@@ -93,7 +93,7 @@ object PostProcessor {
if (AppLoader.getConfigBoolean("fxdither")) if (AppLoader.getConfigBoolean("fxdither"))
AppLoader.shaderHicolour AppLoader.shaderHicolour
else else
AppLoader.shaderPassthru AppLoader.shaderPassthruRGB
fbo.colorBufferTexture.bind(0) fbo.colorBufferTexture.bind(0)

View File

@@ -295,7 +295,7 @@ object Terrarum : Screen {
shaderSkyboxFill.end() shaderSkyboxFill.end()
} }
else { 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") shaderSkyboxFill = AppLoader.loadShader("assets/4096.vert", "assets/skyboxfill.frag")
} }

View File

@@ -31,6 +31,8 @@ object IngameRenderer {
lateinit var batch: SpriteBatch lateinit var batch: SpriteBatch
private lateinit var camera: OrthographicCamera private lateinit var camera: OrthographicCamera
private lateinit var blurWriteQuad: Mesh
private lateinit var lightmapFboA: FrameBuffer private lateinit var lightmapFboA: FrameBuffer
private lateinit var lightmapFboB: FrameBuffer private lateinit var lightmapFboB: FrameBuffer
private lateinit var fboRGB: FrameBuffer private lateinit var fboRGB: FrameBuffer
@@ -45,6 +47,7 @@ object IngameRenderer {
private val shaderBlendGlow = Terrarum.shaderBlendGlow private val shaderBlendGlow = Terrarum.shaderBlendGlow
private val shaderRGBOnly = Terrarum.shaderRGBOnly private val shaderRGBOnly = Terrarum.shaderRGBOnly
private val shaderAtoGrey = Terrarum.shaderAtoGrey private val shaderAtoGrey = Terrarum.shaderAtoGrey
private val shaderPassthru = SpriteBatch.createDefaultShader()
private val width = Terrarum.WIDTH private val width = Terrarum.WIDTH
private val height = Terrarum.HEIGHT private val height = Terrarum.HEIGHT
@@ -490,44 +493,42 @@ object IngameRenderer {
// initialise readBuffer with untreated lightmap // initialise readBuffer with untreated lightmap
blurReadBuffer.inAction(camera, batch) { blurReadBuffer.inAction(camera, batch) {
batch.inUse { val texture = LightmapRenderer.draw()
blendDisable(batch) texture.bind(0)
batch.color = Color.WHITE
LightmapRenderer.draw(batch) 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) { for (i in 0 until blurIterations) {
blurWriteBuffer.inAction(camera, batch) { 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 // swap
batch.shader.setUniformf("iResolution", val t = blurWriteBuffer
blurWriteBuffer.width.toFloat(), blurWriteBuffer.height.toFloat()) blurWriteBuffer = blurReadBuffer
batch.shader.setUniformf("flip", 1f) blurReadBuffer = t
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
}
} }
} }
@@ -540,6 +541,13 @@ object IngameRenderer {
fun resize(width: Int, height: Int) { fun resize(width: Int, height: Int) {
if (!init) { if (!init) {
blurWriteQuad = Mesh(
true, 4, 6,
VertexAttribute.Position(),
VertexAttribute.ColorUnpacked(),
VertexAttribute.TexCoords(0)
)
init = true init = true
} }
else { else {
@@ -572,7 +580,13 @@ object IngameRenderer {
LightmapRenderer.resize(width, height) 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() private val TILE_SIZEF = FeaturesDrawer.TILE_SIZE.toFloat()

View File

@@ -1,11 +1,8 @@
package net.torvald.terrarum.worlddrawer package net.torvald.terrarum.worlddrawer
import com.badlogic.gdx.Gdx
import com.badlogic.gdx.graphics.Color import com.badlogic.gdx.graphics.Color
import com.badlogic.gdx.graphics.GL20
import com.badlogic.gdx.graphics.Pixmap import com.badlogic.gdx.graphics.Pixmap
import com.badlogic.gdx.graphics.Texture import com.badlogic.gdx.graphics.Texture
import com.badlogic.gdx.graphics.g2d.SpriteBatch
import com.badlogic.gdx.graphics.glutils.ShaderProgram import com.badlogic.gdx.graphics.glutils.ShaderProgram
import com.jme3.math.FastMath import com.jme3.math.FastMath
import net.torvald.terrarum.* import net.torvald.terrarum.*
@@ -559,7 +556,7 @@ object LightmapRenderer {
private var _lightBufferAsTex: Texture = Texture(1, 1, Pixmap.Format.RGBA8888) 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 // when shader is not used: 0.5 ms on 6700K
AppLoader.measureDebugTime("Renderer.LightToScreen") { AppLoader.measureDebugTime("Renderer.LightToScreen") {
@@ -600,12 +597,14 @@ object LightmapRenderer {
_lightBufferAsTex = Texture(lightBuffer) _lightBufferAsTex = Texture(lightBuffer)
_lightBufferAsTex.setFilter(Texture.TextureFilter.Nearest, Texture.TextureFilter.Nearest) _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... // we might not need shader here...
//batch.draw(lightBufferAsTex, 0f, 0f, lightBufferAsTex.width.toFloat(), lightBufferAsTex.height.toFloat()) //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) batch.draw(_lightBufferAsTex, 0f, 0f, _lightBufferAsTex.width * DRAW_TILE_SIZE, _lightBufferAsTex.height * DRAW_TILE_SIZE)
*/
} }
return _lightBufferAsTex
} }
fun dispose() { fun dispose() {