-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: experimental post processing pipeline with FXAA
- Loading branch information
Showing
13 changed files
with
1,543 additions
and
6 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
19 changes: 19 additions & 0 deletions
19
core/src/me/vinceh121/wanderer/glx/post/FxaaPostProcessEffect.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
package me.vinceh121.wanderer.glx.post; | ||
|
||
import com.badlogic.gdx.graphics.Texture; | ||
|
||
import me.vinceh121.wanderer.GraphicsManager; | ||
import me.vinceh121.wanderer.platform.glx.IFXAAShader; | ||
|
||
public class FxaaPostProcessEffect implements IPostProcessEffect { | ||
private final IFXAAShader shader; | ||
|
||
public FxaaPostProcessEffect(IFXAAShader shader) { | ||
this.shader = shader; | ||
} | ||
|
||
@Override | ||
public Texture process(GraphicsManager glx, Texture tex) { | ||
return this.shader.run(glx.getModelBatch().getRenderContext(), tex); | ||
} | ||
} |
13 changes: 13 additions & 0 deletions
13
core/src/me/vinceh121/wanderer/glx/post/IPostProcessEffect.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
package me.vinceh121.wanderer.glx.post; | ||
|
||
import com.badlogic.gdx.graphics.Texture; | ||
|
||
import me.vinceh121.wanderer.GraphicsManager; | ||
|
||
public interface IPostProcessEffect { | ||
Texture process(GraphicsManager glx, Texture tex); | ||
|
||
default boolean isInPlace() { | ||
return true; | ||
} | ||
} |
77 changes: 77 additions & 0 deletions
77
core/src/me/vinceh121/wanderer/glx/post/PostProcessManager.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
package me.vinceh121.wanderer.glx.post; | ||
|
||
import java.util.LinkedList; | ||
|
||
import com.badlogic.gdx.Gdx; | ||
import com.badlogic.gdx.graphics.GL30; | ||
import com.badlogic.gdx.graphics.Pixmap.Format; | ||
import com.badlogic.gdx.graphics.Texture; | ||
import com.badlogic.gdx.graphics.glutils.FrameBuffer; | ||
|
||
import me.vinceh121.wanderer.GraphicsManager; | ||
import me.vinceh121.wanderer.platform.glx.IComputeShaderProvider; | ||
|
||
public class PostProcessManager { | ||
private final GraphicsManager glx; | ||
private final LinkedList<IPostProcessEffect> effects = new LinkedList<>(); | ||
private FrameBuffer fbo; | ||
|
||
public PostProcessManager(GraphicsManager glx) { | ||
this.glx = glx; | ||
|
||
this.effects.add(new FxaaPostProcessEffect( | ||
IComputeShaderProvider.get().buildFxaa(Gdx.files.internal("shaders/fxaa.glsl")))); | ||
} | ||
|
||
public void addEffectFirst(IPostProcessEffect effect) { | ||
this.effects.addFirst(effect); | ||
} | ||
|
||
public void addEffectLast(IPostProcessEffect effect) { | ||
this.effects.addLast(effect); | ||
} | ||
|
||
public void begin() { | ||
assert this.fbo == null; | ||
|
||
this.fbo = new FrameBuffer(Format.RGB888, Gdx.graphics.getWidth(), Gdx.graphics.getHeight(), true); | ||
this.fbo.begin(); | ||
} | ||
|
||
public Texture end() { | ||
this.fbo.end(); | ||
Texture tex = this.fbo.getColorBufferTexture(); | ||
|
||
for (final IPostProcessEffect fx : this.effects) { | ||
final Texture newTex = fx.process(this.glx, tex); | ||
|
||
if (!fx.isInPlace()) { | ||
tex.dispose(); | ||
} | ||
|
||
tex = newTex; | ||
} | ||
|
||
Gdx.gl30.glBindFramebuffer(GL30.GL_READ_FRAMEBUFFER, this.fbo.getFramebufferHandle()); | ||
Gdx.gl30.glBindFramebuffer(GL30.GL_DRAW_FRAMEBUFFER, 0); | ||
Gdx.gl30.glBlitFramebuffer(0, | ||
0, | ||
tex.getWidth(), | ||
tex.getHeight(), | ||
0, | ||
0, | ||
tex.getWidth(), | ||
tex.getHeight(), | ||
GL30.GL_COLOR_BUFFER_BIT, | ||
GL30.GL_NEAREST); | ||
|
||
this.fbo.dispose(); | ||
this.fbo = null; | ||
|
||
return null; | ||
} | ||
|
||
public LinkedList<IPostProcessEffect> getEffects() { | ||
return effects; | ||
} | ||
} |
20 changes: 20 additions & 0 deletions
20
core/src/me/vinceh121/wanderer/platform/glx/IComputeShader.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
package me.vinceh121.wanderer.platform.glx; | ||
|
||
import com.badlogic.gdx.utils.Disposable; | ||
|
||
public interface IComputeShader extends Disposable { | ||
void bind(); | ||
|
||
void dispatch(int x, int y, int z); | ||
|
||
int getUniformLocation(String name); | ||
|
||
void setUniform1i(int location, int value); | ||
|
||
void setUniform2f(int location, float v0, float v1); | ||
|
||
int getProgramHandle(); | ||
|
||
int getShaderHandle(); | ||
|
||
} |
30 changes: 30 additions & 0 deletions
30
core/src/me/vinceh121/wanderer/platform/glx/IComputeShaderProvider.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
package me.vinceh121.wanderer.platform.glx; | ||
|
||
import com.badlogic.gdx.Application.ApplicationType; | ||
import com.badlogic.gdx.Gdx; | ||
import com.badlogic.gdx.files.FileHandle; | ||
|
||
public interface IComputeShaderProvider { | ||
public static IComputeShaderProvider get() { | ||
try { | ||
final Class<?> cls; | ||
|
||
if (Gdx.app.getType() == ApplicationType.Desktop) { | ||
cls = Class.forName("me.vinceh121.wanderer.desktop.glx.LWJGLComputeShaderProvider"); | ||
} else { | ||
throw new UnsupportedOperationException( | ||
"Platform not supported for compute shader provider: " + Gdx.app.getType()); | ||
} | ||
|
||
return (IComputeShaderProvider) cls.getConstructor().newInstance(); | ||
} catch (Exception e) { | ||
throw new RuntimeException(e); | ||
} | ||
} | ||
|
||
IComputeShader build(FileHandle source); | ||
|
||
IComputeShader build(String source); | ||
|
||
IFXAAShader buildFxaa(FileHandle source); | ||
} |
16 changes: 16 additions & 0 deletions
16
core/src/me/vinceh121/wanderer/platform/glx/IFXAAShader.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
package me.vinceh121.wanderer.platform.glx; | ||
|
||
import com.badlogic.gdx.graphics.Texture; | ||
import com.badlogic.gdx.graphics.g3d.utils.RenderContext; | ||
|
||
public interface IFXAAShader extends IComputeShader { | ||
|
||
void setInputImage(RenderContext ctx, int texUnit); | ||
|
||
void setOutputImage(RenderContext ctx, int texUnit); | ||
|
||
void setInvResolution(float width, float height); | ||
|
||
Texture run(RenderContext ctx, Texture tex); | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
55 changes: 55 additions & 0 deletions
55
desktop/src/me/vinceh121/wanderer/desktop/glx/LWJGLComputeShader.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
package me.vinceh121.wanderer.desktop.glx; | ||
|
||
import org.lwjgl.opengl.GL43; | ||
|
||
import me.vinceh121.wanderer.platform.glx.IComputeShader; | ||
|
||
public class LWJGLComputeShader implements IComputeShader { | ||
private final int shaderHandle, programHandle; | ||
|
||
public LWJGLComputeShader(int shaderHandle, int programHandle) { | ||
this.shaderHandle = shaderHandle; | ||
this.programHandle = programHandle; | ||
} | ||
|
||
@Override | ||
public void bind() { | ||
GL43.glUseProgram(this.programHandle); | ||
} | ||
|
||
@Override | ||
public void dispatch(int x, int y, int z) { | ||
GL43.glDispatchCompute(x, y, z); | ||
} | ||
|
||
@Override | ||
public int getUniformLocation(String name) { | ||
return GL43.glGetUniformLocation(this.programHandle, name); | ||
} | ||
|
||
@Override | ||
public void setUniform1i(int location, int value) { | ||
GL43.glUniform1i(location, value); | ||
} | ||
|
||
@Override | ||
public void setUniform2f(int location, float v0, float v1) { | ||
GL43.glUniform2f(location, v0, v1); | ||
} | ||
|
||
@Override | ||
public int getShaderHandle() { | ||
return shaderHandle; | ||
} | ||
|
||
@Override | ||
public int getProgramHandle() { | ||
return programHandle; | ||
} | ||
|
||
@Override | ||
public void dispose() { | ||
GL43.glDeleteProgram(this.programHandle); | ||
GL43.glDeleteShader(this.shaderHandle); | ||
} | ||
} |
Oops, something went wrong.