From afa9376b2644300580ab4ec074a8ef391c398b26 Mon Sep 17 00:00:00 2001 From: "rem..om" Date: Mon, 26 Aug 2013 16:17:48 +0000 Subject: [PATCH] - FilterPostProcessor no longer use a different camera to render the fulscreen quad. This greatly simplify the multiviewport filtering handling and global uniform matrix will now be usable in fliter's shaders, because it uses the same cam as the back buffer scene. - Changed the post.vert and post15.vert to do a very basic projection to screen, saves a 16 float uniform and replace 4 matrices multiplication with 4 MAD operations per filter. - Used ResolutionInverse instead of Resolution in FXAA to multiply instead of dividing once per pixel - Same in CartoonEdge - LightScattering now uses the standard post.vert and post15.vert - Various cleanups in SSAO git-svn-id: https://jmonkeyengine.googlecode.com/svn/trunk@10750 75d07b2b-3a1a-0410-a2c5-0572b91ccdca --- .../Common/MatDefs/Blur/HGaussianBlur.j3md | 1 - .../Common/MatDefs/Blur/RadialBlur.j3md | 2 - .../Common/MatDefs/Blur/VGaussianBlur.j3md | 1 - .../Common/MatDefs/Post/BloomExtract.j3md | 2 - .../Common/MatDefs/Post/BloomFinal.j3md | 2 - .../Common/MatDefs/Post/CartoonEdge.frag | 4 +- .../Common/MatDefs/Post/CartoonEdge.j3md | 6 +- .../Common/MatDefs/Post/CartoonEdge15.frag | 4 +- .../Common/MatDefs/Post/Compose.j3md | 2 - .../Common/MatDefs/Post/CrossHatch.j3md | 1 - .../Common/MatDefs/Post/DepthOfField.j3md | 1 - .../Common/MatDefs/Post/FXAA.frag | 6 +- .../Common/MatDefs/Post/FXAA.j3md | 3 +- .../Common/MatDefs/Post/FXAA.vert | 7 +-- .../Common/MatDefs/Post/Fade.j3md | 2 - .../core-effects/Common/MatDefs/Post/Fog.j3md | 2 - .../Common/MatDefs/Post/GammaCorrection.j3md | 2 - .../Common/MatDefs/Post/LightScattering.frag | 6 +- .../Common/MatDefs/Post/LightScattering.j3md | 10 ++- .../Common/MatDefs/Post/LightScattering.vert | 14 ----- .../MatDefs/Post/LightScattering15.frag | 11 ++-- .../MatDefs/Post/LightScattering15.vert | 6 +- .../Common/MatDefs/Post/Overlay.j3md | 4 +- .../Common/MatDefs/Post/Post.vert | 7 +-- .../Common/MatDefs/Post/Post15.vert | 4 +- .../Common/MatDefs/Post/Posterization.j3md | 4 +- .../Common/MatDefs/SSAO/ssao.frag | 7 ++- .../Common/MatDefs/SSAO/ssao.j3md | 5 +- .../Common/MatDefs/SSAO/ssao15.frag | 8 ++- .../Common/MatDefs/SSAO/ssaoBlur.j3md | 7 +-- .../Common/MatDefs/SSAO/ssaoBlur15.frag | 28 --------- .../Common/MatDefs/Water/Water.j3md | 2 - .../com/jme3/post/FilterPostProcessor.java | 61 ++++++++----------- 33 files changed, 74 insertions(+), 158 deletions(-) delete mode 100644 engine/src/core-effects/Common/MatDefs/Post/LightScattering.vert diff --git a/engine/src/core-data/Common/MatDefs/Blur/HGaussianBlur.j3md b/engine/src/core-data/Common/MatDefs/Blur/HGaussianBlur.j3md index e46ef5af1..fb5d3808a 100644 --- a/engine/src/core-data/Common/MatDefs/Blur/HGaussianBlur.j3md +++ b/engine/src/core-data/Common/MatDefs/Blur/HGaussianBlur.j3md @@ -12,7 +12,6 @@ MaterialDef Bloom { FragmentShader GLSL100: Common/MatDefs/Blur/HGaussianBlur.frag WorldParameters { - WorldViewProjectionMatrix } } } \ No newline at end of file diff --git a/engine/src/core-data/Common/MatDefs/Blur/RadialBlur.j3md b/engine/src/core-data/Common/MatDefs/Blur/RadialBlur.j3md index eb3a80d2a..048cd9c7f 100644 --- a/engine/src/core-data/Common/MatDefs/Blur/RadialBlur.j3md +++ b/engine/src/core-data/Common/MatDefs/Blur/RadialBlur.j3md @@ -14,7 +14,6 @@ MaterialDef Radial Blur { FragmentShader GLSL150: Common/MatDefs/Blur/RadialBlur15.frag WorldParameters { - WorldViewProjectionMatrix } Defines { @@ -27,7 +26,6 @@ MaterialDef Radial Blur { FragmentShader GLSL120: Common/MatDefs/Blur/RadialBlur.frag WorldParameters { - WorldViewProjectionMatrix } } } \ No newline at end of file diff --git a/engine/src/core-data/Common/MatDefs/Blur/VGaussianBlur.j3md b/engine/src/core-data/Common/MatDefs/Blur/VGaussianBlur.j3md index a4c127280..78f0c7fa8 100644 --- a/engine/src/core-data/Common/MatDefs/Blur/VGaussianBlur.j3md +++ b/engine/src/core-data/Common/MatDefs/Blur/VGaussianBlur.j3md @@ -12,7 +12,6 @@ MaterialDef Bloom { FragmentShader GLSL100: Common/MatDefs/Blur/VGaussianBlur.frag WorldParameters { - WorldViewProjectionMatrix } } } \ No newline at end of file diff --git a/engine/src/core-effects/Common/MatDefs/Post/BloomExtract.j3md b/engine/src/core-effects/Common/MatDefs/Post/BloomExtract.j3md index 46817698c..8158b6c8e 100644 --- a/engine/src/core-effects/Common/MatDefs/Post/BloomExtract.j3md +++ b/engine/src/core-effects/Common/MatDefs/Post/BloomExtract.j3md @@ -14,7 +14,6 @@ MaterialDef Bloom { FragmentShader GLSL150: Common/MatDefs/Post/bloomExtract15.frag WorldParameters { - WorldViewProjectionMatrix } Defines { @@ -29,7 +28,6 @@ MaterialDef Bloom { FragmentShader GLSL100: Common/MatDefs/Post/bloomExtract.frag WorldParameters { - WorldViewProjectionMatrix } Defines { diff --git a/engine/src/core-effects/Common/MatDefs/Post/BloomFinal.j3md b/engine/src/core-effects/Common/MatDefs/Post/BloomFinal.j3md index 036c665ee..2c614fdc7 100644 --- a/engine/src/core-effects/Common/MatDefs/Post/BloomFinal.j3md +++ b/engine/src/core-effects/Common/MatDefs/Post/BloomFinal.j3md @@ -12,7 +12,6 @@ MaterialDef Bloom Final { FragmentShader GLSL150: Common/MatDefs/Post/bloomFinal15.frag WorldParameters { - WorldViewProjectionMatrix } Defines { @@ -25,7 +24,6 @@ MaterialDef Bloom Final { FragmentShader GLSL100: Common/MatDefs/Post/bloomFinal.frag WorldParameters { - WorldViewProjectionMatrix } } } \ No newline at end of file diff --git a/engine/src/core-effects/Common/MatDefs/Post/CartoonEdge.frag b/engine/src/core-effects/Common/MatDefs/Post/CartoonEdge.frag index 7ee7f789f..e7520601e 100644 --- a/engine/src/core-effects/Common/MatDefs/Post/CartoonEdge.frag +++ b/engine/src/core-effects/Common/MatDefs/Post/CartoonEdge.frag @@ -15,7 +15,7 @@ uniform sampler2D m_Texture; uniform sampler2D m_NormalsTexture; uniform sampler2D m_DepthTexture; -uniform vec2 g_Resolution; +uniform vec2 g_ResolutionInverse; vec4 fetchNormalDepth(vec2 tc){ vec4 nd; @@ -27,7 +27,7 @@ vec4 fetchNormalDepth(vec2 tc){ void main(){ vec3 color = texture2D(m_Texture, texCoord).rgb; - vec2 edgeOffset = vec2(m_EdgeWidth) / g_Resolution; + vec2 edgeOffset = vec2(m_EdgeWidth) * g_ResolutionInverse; vec4 n1 = fetchNormalDepth(texCoord + vec2(-1.0, -1.0) * edgeOffset); vec4 n2 = fetchNormalDepth(texCoord + vec2( 1.0, 1.0) * edgeOffset); diff --git a/engine/src/core-effects/Common/MatDefs/Post/CartoonEdge.j3md b/engine/src/core-effects/Common/MatDefs/Post/CartoonEdge.j3md index 0d190291f..91ad18de4 100644 --- a/engine/src/core-effects/Common/MatDefs/Post/CartoonEdge.j3md +++ b/engine/src/core-effects/Common/MatDefs/Post/CartoonEdge.j3md @@ -20,9 +20,8 @@ MaterialDef Cartoon Edge { FragmentShader GLSL150: Common/MatDefs/Post/CartoonEdge15.frag WorldParameters { - WorldViewProjectionMatrix WorldViewMatrix - Resolution + ResolutionInverse } Defines { @@ -37,9 +36,8 @@ MaterialDef Cartoon Edge { FragmentShader GLSL100: Common/MatDefs/Post/CartoonEdge.frag WorldParameters { - WorldViewProjectionMatrix WorldViewMatrix - Resolution + ResolutionInverse } } } \ No newline at end of file diff --git a/engine/src/core-effects/Common/MatDefs/Post/CartoonEdge15.frag b/engine/src/core-effects/Common/MatDefs/Post/CartoonEdge15.frag index 3c3921a98..7481954a9 100644 --- a/engine/src/core-effects/Common/MatDefs/Post/CartoonEdge15.frag +++ b/engine/src/core-effects/Common/MatDefs/Post/CartoonEdge15.frag @@ -4,7 +4,7 @@ uniform COLORTEXTURE m_Texture; uniform DEPTHTEXTURE m_DepthTexture; uniform sampler2D m_NormalsTexture; -uniform vec2 g_Resolution; +uniform vec2 g_ResolutionInverse; uniform vec4 m_EdgeColor; @@ -30,7 +30,7 @@ vec4 fetchNormalDepth(vec2 tc){ void main(){ vec3 color = getColor(m_Texture, texCoord).rgb; - vec2 edgeOffset = vec2(m_EdgeWidth) / textureSize(m_NormalsTexture, 0); + vec2 edgeOffset = vec2(m_EdgeWidth) * g_ResolutionInverse; vec4 n1 = fetchNormalDepth(texCoord + vec2(-1.0, -1.0) * edgeOffset); vec4 n2 = fetchNormalDepth(texCoord + vec2( 1.0, 1.0) * edgeOffset); vec4 n3 = fetchNormalDepth(texCoord + vec2(-1.0, 1.0) * edgeOffset); diff --git a/engine/src/core-effects/Common/MatDefs/Post/Compose.j3md b/engine/src/core-effects/Common/MatDefs/Post/Compose.j3md index 67cbc70b6..bcd796aa8 100644 --- a/engine/src/core-effects/Common/MatDefs/Post/Compose.j3md +++ b/engine/src/core-effects/Common/MatDefs/Post/Compose.j3md @@ -12,7 +12,6 @@ MaterialDef Default GUI { FragmentShader GLSL150: Common/MatDefs/Post/Compose15.frag WorldParameters { - WorldViewProjectionMatrix } Defines { @@ -26,7 +25,6 @@ MaterialDef Default GUI { FragmentShader GLSL100: Common/MatDefs/Post/Compose.frag WorldParameters { - WorldViewProjectionMatrix } } diff --git a/engine/src/core-effects/Common/MatDefs/Post/CrossHatch.j3md b/engine/src/core-effects/Common/MatDefs/Post/CrossHatch.j3md index 460c96a1e..dadcd77e1 100644 --- a/engine/src/core-effects/Common/MatDefs/Post/CrossHatch.j3md +++ b/engine/src/core-effects/Common/MatDefs/Post/CrossHatch.j3md @@ -22,7 +22,6 @@ MaterialDef CrossHatch { FragmentShader GLSL150: Common/MatDefs/Post/CrossHatch15.frag WorldParameters { - WorldViewProjectionMatrix } } diff --git a/engine/src/core-effects/Common/MatDefs/Post/DepthOfField.j3md b/engine/src/core-effects/Common/MatDefs/Post/DepthOfField.j3md index edd7ac5d4..644bed7d9 100644 --- a/engine/src/core-effects/Common/MatDefs/Post/DepthOfField.j3md +++ b/engine/src/core-effects/Common/MatDefs/Post/DepthOfField.j3md @@ -16,7 +16,6 @@ MaterialDef Depth Of Field { FragmentShader GLSL100: Common/MatDefs/Post/DepthOfField.frag WorldParameters { - WorldViewProjectionMatrix } } } \ No newline at end of file diff --git a/engine/src/core-effects/Common/MatDefs/Post/FXAA.frag b/engine/src/core-effects/Common/MatDefs/Post/FXAA.frag index d630b3595..2fea421ab 100644 --- a/engine/src/core-effects/Common/MatDefs/Post/FXAA.frag +++ b/engine/src/core-effects/Common/MatDefs/Post/FXAA.frag @@ -1,7 +1,7 @@ #extension GL_EXT_gpu_shader4 : enable uniform sampler2D m_Texture; -uniform vec2 g_Resolution; +uniform vec2 g_ResolutionInverse; uniform float m_VxOffset; uniform float m_SpanMax; @@ -83,6 +83,6 @@ vec3 FxaaPixelShader( void main() { - vec2 rcpFrame = vec2(1.0) / g_Resolution; - gl_FragColor = vec4(FxaaPixelShader(posPos, m_Texture, rcpFrame), 1.0); + + gl_FragColor = vec4(FxaaPixelShader(posPos, m_Texture, g_ResolutionInverse), 1.0); } \ No newline at end of file diff --git a/engine/src/core-effects/Common/MatDefs/Post/FXAA.j3md b/engine/src/core-effects/Common/MatDefs/Post/FXAA.j3md index 5ce8b46e5..61e2b79ef 100644 --- a/engine/src/core-effects/Common/MatDefs/Post/FXAA.j3md +++ b/engine/src/core-effects/Common/MatDefs/Post/FXAA.j3md @@ -11,8 +11,7 @@ MaterialDef FXAA { VertexShader GLSL100: Common/MatDefs/Post/FXAA.vert FragmentShader GLSL100: Common/MatDefs/Post/FXAA.frag WorldParameters { - WorldViewProjectionMatrix - Resolution + ResolutionInverse } } } \ No newline at end of file diff --git a/engine/src/core-effects/Common/MatDefs/Post/FXAA.vert b/engine/src/core-effects/Common/MatDefs/Post/FXAA.vert index 21a2ae45f..696a9bb9a 100644 --- a/engine/src/core-effects/Common/MatDefs/Post/FXAA.vert +++ b/engine/src/core-effects/Common/MatDefs/Post/FXAA.vert @@ -1,5 +1,5 @@ uniform mat4 g_WorldViewProjectionMatrix; -uniform vec2 g_Resolution; +uniform vec2 g_ResolutionInverse; uniform float m_SubPixelShift; @@ -12,8 +12,7 @@ varying vec4 posPos; void main() { vec2 pos = (g_WorldViewProjectionMatrix * inPosition).xy; gl_Position = vec4(pos, 0.0, 1.0); - texCoord = inTexCoord; - vec2 rcpFrame = vec2(1.0) / g_Resolution; + texCoord = inTexCoord; posPos.xy = inTexCoord.xy; - posPos.zw = inTexCoord.xy - (rcpFrame * vec2(0.5 + m_SubPixelShift)); + posPos.zw = inTexCoord.xy - (g_ResolutionInverse * vec2(0.5 + m_SubPixelShift)); } \ No newline at end of file diff --git a/engine/src/core-effects/Common/MatDefs/Post/Fade.j3md b/engine/src/core-effects/Common/MatDefs/Post/Fade.j3md index eeddbfce4..46d36e49b 100644 --- a/engine/src/core-effects/Common/MatDefs/Post/Fade.j3md +++ b/engine/src/core-effects/Common/MatDefs/Post/Fade.j3md @@ -11,7 +11,6 @@ MaterialDef Fade { FragmentShader GLSL150: Common/MatDefs/Post/Fade15.frag WorldParameters { - WorldViewProjectionMatrix } Defines { @@ -24,7 +23,6 @@ MaterialDef Fade { FragmentShader GLSL100: Common/MatDefs/Post/Fade.frag WorldParameters { - WorldViewProjectionMatrix } } } \ No newline at end of file diff --git a/engine/src/core-effects/Common/MatDefs/Post/Fog.j3md b/engine/src/core-effects/Common/MatDefs/Post/Fog.j3md index f802c8a24..fc966af11 100644 --- a/engine/src/core-effects/Common/MatDefs/Post/Fog.j3md +++ b/engine/src/core-effects/Common/MatDefs/Post/Fog.j3md @@ -15,7 +15,6 @@ MaterialDef Fade { FragmentShader GLSL150: Common/MatDefs/Post/Fog15.frag WorldParameters { - WorldViewProjectionMatrix } Defines { @@ -29,7 +28,6 @@ MaterialDef Fade { FragmentShader GLSL100: Common/MatDefs/Post/Fog.frag WorldParameters { - WorldViewProjectionMatrix } } } \ No newline at end of file diff --git a/engine/src/core-effects/Common/MatDefs/Post/GammaCorrection.j3md b/engine/src/core-effects/Common/MatDefs/Post/GammaCorrection.j3md index d0ca74307..973679efd 100644 --- a/engine/src/core-effects/Common/MatDefs/Post/GammaCorrection.j3md +++ b/engine/src/core-effects/Common/MatDefs/Post/GammaCorrection.j3md @@ -12,7 +12,6 @@ MaterialDef GammaCorrection { FragmentShader GLSL150: Common/MatDefs/Post/GammaCorrection15.frag WorldParameters { - WorldViewProjectionMatrix } Defines { @@ -25,7 +24,6 @@ MaterialDef GammaCorrection { FragmentShader GLSL100: Common/MatDefs/Post/GammaCorrection.frag WorldParameters { - WorldViewProjectionMatrix } Defines { diff --git a/engine/src/core-effects/Common/MatDefs/Post/LightScattering.frag b/engine/src/core-effects/Common/MatDefs/Post/LightScattering.frag index 683bbeaa7..7e1c542f0 100644 --- a/engine/src/core-effects/Common/MatDefs/Post/LightScattering.frag +++ b/engine/src/core-effects/Common/MatDefs/Post/LightScattering.frag @@ -5,8 +5,8 @@ uniform float m_BlurStart; uniform float m_BlurWidth; uniform float m_LightDensity; uniform bool m_Display; +uniform vec3 m_LightPosition; -varying vec2 lightPos; varying vec2 texCoord; void main(void) @@ -16,12 +16,12 @@ void main(void) vec4 colorRes= texture2D(m_Texture,texCoord); float factor=(m_BlurWidth/float(m_NbSamples-1.0)); float scale; - vec2 texCoo=texCoord-lightPos; + vec2 texCoo=texCoord - m_LightPosition.xy; vec2 scaledCoord; vec4 res = vec4(0.0); for(int i=0; i filters = new ArrayList(); - private AssetManager assetManager; - private Camera filterCam = new Camera(1, 1); + private AssetManager assetManager; private Picture fsQuad; private boolean computeDepth = false; private FrameBuffer outputBuffer; @@ -78,7 +77,7 @@ public class FilterPostProcessor implements SceneProcessor, Savable { private int originalHeight; private int lastFilterIndex = -1; private boolean cameraInit = false; - private boolean clearColor= true; + private boolean multiView = false; /** * Create a FilterProcessor @@ -135,7 +134,9 @@ public class FilterPostProcessor implements SceneProcessor, Savable { renderer = rm.getRenderer(); viewPort = vp; fsQuad = new Picture("filter full screen quad"); - + fsQuad.setWidth(1); + fsQuad.setHeight(1); + Camera cam = vp.getCamera(); //save view port diensions @@ -176,29 +177,23 @@ public class FilterPostProcessor implements SceneProcessor, Savable { * @param mat */ private void renderProcessing(Renderer r, FrameBuffer buff, Material mat) { - if (buff == outputBuffer) { - fsQuad.setWidth(width); - fsQuad.setHeight(height); - filterCam.resize(originalWidth, originalHeight, true); - fsQuad.setPosition(left * originalWidth, bottom * originalHeight); - } else { - fsQuad.setWidth(buff.getWidth()); - fsQuad.setHeight(buff.getHeight()); - filterCam.resize(buff.getWidth(), buff.getHeight(), true); - fsQuad.setPosition(0, 0); + if (buff == outputBuffer && multiView) { + viewPort.getCamera().resize(originalWidth, originalHeight, false); + viewPort.getCamera().setViewPort(left, right, bottom, top); + viewPort.getCamera().update(); + renderManager.setCamera( viewPort.getCamera(), false); } if (mat.getAdditionalRenderState().isDepthWrite()) { mat.getAdditionalRenderState().setDepthTest(false); mat.getAdditionalRenderState().setDepthWrite(false); } - + fsQuad.setMaterial(mat); fsQuad.updateGeometricState(); - - renderManager.setCamera(filterCam, true); + r.setFrameBuffer(buff); - r.clearBuffers(clearColor, true, true); + r.clearBuffers(true, true, true); renderManager.renderGeometry(fsQuad); } @@ -216,8 +211,7 @@ public class FilterPostProcessor implements SceneProcessor, Savable { } } - } - Picture pic = new Picture("debug"); + } /** * iterate through the filter list and renders filters @@ -317,11 +311,14 @@ public class FilterPostProcessor implements SceneProcessor, Savable { } else { viewPort.setOutputFrameBuffer(renderFrameBuffer); } - //init of the camera if it wasn't already - if (!cameraInit) { - viewPort.getCamera().resize(width, height, true); + //if we are ina multiview situation we need to resize the camera + //to the viewportsize so that the backbuffer is rendered correctly + if (multiView) { + viewPort.getCamera().resize(width, height, false); viewPort.getCamera().setViewPort(0, 1, 0, 1); - } + viewPort.getCamera().update(); + renderManager.setCamera(viewPort.getCamera(), false); + } } for (Iterator it = filters.iterator(); it.hasNext();) { @@ -376,8 +373,8 @@ public class FilterPostProcessor implements SceneProcessor, Savable { } public void reshape(ViewPort vp, int w, int h) { - //this has no effect at first init but is useful when resizing the canvas with multi views Camera cam = vp.getCamera(); + //this has no effect at first init but is useful when resizing the canvas with multi views cam.setViewPort(left, right, bottom, top); //resizing the camera to fit the new viewport and saving original dimensions cam.resize(w, h, false); @@ -387,24 +384,20 @@ public class FilterPostProcessor implements SceneProcessor, Savable { bottom = cam.getViewPortBottom(); originalWidth = w; originalHeight = h; - cam.setViewPort(0, 1, 0, 1); - //computing real dimension of the viewport and resizing he camera + //computing real dimension of the viewport and resizing the camera width = (int) (w * (Math.abs(right - left))); height = (int) (h * (Math.abs(bottom - top))); width = Math.max(1, width); height = Math.max(1, height); //Testing original versus actual viewport dimension. - //If they are different we are in a multiview situation and color from other view port must not be cleared. - //However, not clearing the color can cause issues when AlphaToCoverage is active on the renderer. + //If they are different we are in a multiview situation and + //camera must be handled differently if(originalWidth!=width || originalHeight!=height){ - clearColor = false; - }else{ - clearColor = true; + multiView = true; } - - cam.resize(width, height, false); + cameraInit = true; computeDepth = false;