From 3707716569d5f181583ce5abb5a354e510e3ebb1 Mon Sep 17 00:00:00 2001 From: jgk Date: Fri, 20 Aug 2021 04:14:59 +0200 Subject: [PATCH] Downsample when blurring bloom --- shader/texture/bloomBlur.frag | 6 ++++-- src/Dodge/Render.hs | 12 +++++++----- src/Framebuffer/Update.hs | 21 +++++++++++++-------- src/Preload/Update.hs | 2 +- 4 files changed, 25 insertions(+), 16 deletions(-) diff --git a/shader/texture/bloomBlur.frag b/shader/texture/bloomBlur.frag index 7a85a239e..bcea5626c 100644 --- a/shader/texture/bloomBlur.frag +++ b/shader/texture/bloomBlur.frag @@ -3,10 +3,12 @@ in vec2 vTexPos; out vec4 fColor; const vec2 winSize = vec2 (300,300); // note that the above opening bracket should be the first in this file +// this is so that the window size can be extracted and the shader recompiled on +// the fly uniform sampler2D screenTexture; -const float hOff = 4.0 / winSize.x; -const float vOff = 4.0 / winSize.y; +const float hOff = 3.0 / winSize.x; +const float vOff = 3.0 / winSize.y; const float frac = 1.0 / 9; const vec2 off[9] = vec2[] ( vec2( vOff,hOff ) diff --git a/src/Dodge/Render.hs b/src/Dodge/Render.hs index 9f186964f..2be343333 100644 --- a/src/Dodge/Render.hs +++ b/src/Dodge/Render.hs @@ -104,21 +104,21 @@ doDrawing pdata w = do renderLayer 5 shadV layerCounts depthMask $= Enabled + -- this is not ideal if the original is not divisible by 2 + viewport $= (Position 0 0 + , divideSize (2 * (w ^. config . shadow_resolution)) $ Size (round $ fstV2 wins) (round $ sndV2 wins)) bindFramebuffer Framebuffer $= fst (_fboFourth1 pdata) - --viewport $= (Position 0 0, Size 300 300) depthFunc $= Just Always textureBinding Texture2D $= Just (snd $ _fboBloom pdata) blend $= Disabled drawShader (_bloomBlurShader pdata) 4 replicateM_ 3 $ pingPongBetween (_fboFourth1 pdata) (_fboFourth2 pdata) (_bloomBlurShader pdata) --- viewport $= (Position 0 0, Size (round $ fstV2 wins) (round $ sndV2 wins)) blend $= Enabled + viewport $= (Position 0 0 + , divideSize (w ^. config . shadow_resolution) $ Size (round $ fstV2 wins) (round $ sndV2 wins)) bindFramebuffer Framebuffer $= fst (_fboLighting pdata) - --viewport $= (Position 0 0 - -- , divideSize (w ^. config . shadow_resolution) $ Size (round $ fstV2 wins) (round $ sndV2 wins)) createLightMap pdata lightPoints nWalls nSils nsurfVs - --viewport $= (Position 0 0, Size (round $ fstV2 wins) (round $ sndV2 wins)) colorMask $= Color4 Enabled Enabled Enabled Enabled clearColor $= Color4 0 0 0 0 @@ -138,8 +138,10 @@ doDrawing pdata w = do textureBinding Texture2D $= Just (snd $ _fboColor pdata) drawShader (_colorBlurShader pdata) 4 --textureBinding Texture2D $= Just (snd3 $ _fbo2 pdata) + --blendFunc $= (SrcAlpha, One) textureBinding Texture2D $= Just (snd $ _fboFourth1 pdata) drawShader (_fullscreenShader pdata) 4 + blendFunc $= (SrcAlpha, OneMinusSrcAlpha) textureBinding Texture2D $= Just (snd $ _fboBloom pdata) drawShader (_fullscreenShader pdata) 4 diff --git a/src/Framebuffer/Update.hs b/src/Framebuffer/Update.hs index a3a647c33..ccbbf07b2 100644 --- a/src/Framebuffer/Update.hs +++ b/src/Framebuffer/Update.hs @@ -23,10 +23,11 @@ sizeFBOs -> IO (RenderData) sizeFBOs xsize ysize xfull yfull rdata = do resizeRBO (_rboBaseBloom rdata) xsize ysize - resizeRBO (_rboLighting rdata) xsize ysize - rdata' <- foldM (updateFBOTO xsize ysize) rdata [fboBase, fboBloom, fboColor,fboFourth1,fboFourth2] - rdata'' <- foldM (updateFBOTO xfull yfull) rdata' [fbo2, fbo3] - updateFBOTO xsize ysize rdata'' fboLighting + resizeRBO (_rboLighting rdata) xsize ysize + rdata' <- foldM (updateFBOTO xsize ysize minMagFilter) rdata [fboBase, fboColor,fboLighting] + rdata'' <- updateFBOTO xsize ysize minMagFilter rdata' fboBloom + rdata''' <- foldM (updateFBOTO xfull yfull minMagFilter) rdata'' [fbo2, fbo3] + foldM (updateFBOTO (xsize `div` 2) (ysize `div` 2) linMinMagFilter) rdata''' [fboFourth1,fboFourth2] resizeRBO :: RenderbufferObject @@ -42,19 +43,21 @@ resizeRBO rboName xsize ysize = do updateFBOTO :: Int -> Int + -> ((TextureFilter, Maybe TextureFilter),TextureFilter) -> RenderData -> ALens' RenderData (FramebufferObject, TextureObject) -> IO (RenderData) -updateFBOTO xsize ysize pdata target = do - newfbo2 <- resizeFBOTO (pdata ^# target) xsize ysize +updateFBOTO xsize ysize mmfilt pdata target = do + newfbo2 <- resizeFBOTO (pdata ^# target) xsize ysize mmfilt return $ storing target newfbo2 pdata resizeFBOTO :: (FramebufferObject , TextureObject) -> Int -> Int + -> ((TextureFilter, Maybe TextureFilter),TextureFilter) -> IO (FramebufferObject, TextureObject) -resizeFBOTO (fboName,toOld) xsize ysize = do +resizeFBOTO (fboName,toOld) xsize ysize mmfilt = do bindFramebuffer Framebuffer $= fboName let xsize' = fromIntegral xsize ysize' = fromIntegral ysize @@ -62,7 +65,7 @@ resizeFBOTO (fboName,toOld) xsize ysize = do toName <- genObjectName textureBinding Texture2D $= Just toName glTexStorage2D GL_TEXTURE_2D 1 GL_RGBA8 xsize' ysize' - textureFilter Texture2D $= minMagFilter + textureFilter Texture2D $= mmfilt framebufferTexture2D Framebuffer (ColorAttachment 0) Texture2D toName 0 fboStatus <- framebufferStatus Framebuffer putStrLn $ "after resize, framebuffer status:" ++ show fboStatus @@ -71,3 +74,5 @@ resizeFBOTO (fboName,toOld) xsize ysize = do minMagFilter :: ((TextureFilter, Maybe TextureFilter),TextureFilter) minMagFilter = ((Nearest,Nothing),Nearest) +linMinMagFilter :: ((TextureFilter, Maybe TextureFilter),TextureFilter) +linMinMagFilter = ((Linear',Nothing),Linear') diff --git a/src/Preload/Update.hs b/src/Preload/Update.hs index 01c44aff2..e095c1028 100644 --- a/src/Preload/Update.hs +++ b/src/Preload/Update.hs @@ -34,7 +34,7 @@ renderDataResizeUpdate xsize ysize xfull yfull rdata = do bbFrag <- BS.readFile "shader/texture/bloomBlur.frag" let (bh,bmid) = BS.breakSubstring "(" bbFrag (_,btt) = BS.breakSubstring ")" bmid - bbFrag' = BS.append bh $ BS.append (BSC.pack $ '(' : show xfull ++ "," ++ show yfull) btt + bbFrag' = BS.append bh $ BS.append (BSC.pack $ '(' : show xsize ++ "," ++ show ysize) btt --BSC.putStrLn bbFrag' bbShad <- makeByteStringShaderUsingVAO "bloomBlur" [(vert,bbVert),(frag,bbFrag')] ETriangleStrip (_fullscreenShader rdata)