From 20c05be23f333a12ed39f92d250599fe42ce5e4e Mon Sep 17 00:00:00 2001 From: justin Date: Fri, 24 Mar 2023 14:21:12 +0000 Subject: [PATCH] Fix resize crash --- shader/texture/simple.frag | 9 +--- shader/texture/simple.vert | 9 ++-- src/Dodge/Base/WinScale.hs | 4 +- src/Dodge/Event.hs | 6 +-- src/Dodge/Render.hs | 28 +++++++---- src/Dodge/TestString.hs | 2 + src/Framebuffer/Update.hs | 97 +++++++++++++++++++++++++------------- src/Preload/Render.hs | 4 +- 8 files changed, 99 insertions(+), 60 deletions(-) diff --git a/shader/texture/simple.frag b/shader/texture/simple.frag index 342560976..3f608e817 100644 --- a/shader/texture/simple.frag +++ b/shader/texture/simple.frag @@ -1,10 +1,5 @@ #version 450 core in vec2 vTexPos; out vec4 fColor; - -layout (binding = 0) uniform sampler2D screenTexture; - -void main() -{ - fColor = texture(screenTexture, vTexPos); -} +layout(binding = 0) uniform sampler2D screenTexture; +void main() { fColor = texture(screenTexture, vTexPos); } diff --git a/shader/texture/simple.vert b/shader/texture/simple.vert index 51ba3304d..fa5cbb7f5 100644 --- a/shader/texture/simple.vert +++ b/shader/texture/simple.vert @@ -1,9 +1,8 @@ #version 450 core -layout (location = 0) in vec2 pos; -layout (location = 1) in vec2 texPos; +layout(location = 0) in vec2 pos; +layout(location = 1) in vec2 texPos; out vec2 vTexPos; -void main() -{ - gl_Position = vec4(pos,0,1); +void main() { + gl_Position = vec4(pos, 0.0, 1.0); vTexPos = texPos; } diff --git a/src/Dodge/Base/WinScale.hs b/src/Dodge/Base/WinScale.hs index 886021d80..f893cffd0 100644 --- a/src/Dodge/Base/WinScale.hs +++ b/src/Dodge/Base/WinScale.hs @@ -6,7 +6,7 @@ import Picture winScale :: Configuration -> Picture -> Picture {-# INLINE winScale #-} -winScale cfig = scale (2 / _windowX cfig) (2 / _windowY cfig) +winScale cfig = scale (1 / _windowX cfig) (1 / _windowY cfig) doWindowScale :: Configuration -> Point2 -> Point2 -doWindowScale cfig (V2 x y) = V2 (x * 2 / _windowX cfig) (y * 2 / _windowY cfig) +doWindowScale cfig (V2 x y) = V2 (x / _windowX cfig) (y / _windowY cfig) diff --git a/src/Dodge/Event.hs b/src/Dodge/Event.hs index f38da1df0..551dd4606 100644 --- a/src/Dodge/Event.hs +++ b/src/Dodge/Event.hs @@ -55,9 +55,7 @@ handleWindowMoveEvent mev = handleResizeEvent :: WindowSizeChangedEventData -> Universe -> Universe handleResizeEvent sev u = u - & uvConfig . windowX .~ fromIntegral x - & uvConfig . windowY .~ fromIntegral y - -- & uvIOEffects %~ sideEffectUpdatePreload - & uvIOEffects %~ (updatePreload >=>) + & uvIOEffects %~ ( (updatePreload . updateconfig) >=>) where + updateconfig = (uvConfig . windowX .~ fromIntegral x) . (uvConfig . windowY .~ fromIntegral y) V2 x y = windowSizeChangedEventSize sev diff --git a/src/Dodge/Render.hs b/src/Dodge/Render.hs index d79da3598..c917f5e74 100644 --- a/src/Dodge/Render.hs +++ b/src/Dodge/Render.hs @@ -103,10 +103,26 @@ doDrawing' win pdata u = do , (_shapeEBO pdata, nIndices) , (_silhouetteEBO pdata, nSilIndices) ] + -- draw the overlay + glBindFramebuffer GL_FRAMEBUFFER (pdata ^. fboOverlay ._1 . unFBO) + withArray [0, 0, 0, 0] $ \ptr -> + glClearNamedFramebufferfv + (pdata ^. fboOverlay . _1 . unFBO) + GL_COLOR + 0 + ptr + glDepthFunc GL_ALWAYS + glDepthMask GL_FALSE + glEnable GL_BLEND + glBlendFunc GL_SRC_ALPHA GL_ONE_MINUS_SRC_ALPHA + setViewport _graphics_overlay_resolution cfig + glBindBufferBase GL_UNIFORM_BUFFER 0 (pdata ^. orthonormalMatUBO) + renderLayer FixedCoordLayer shadV layerCounts -- set the coordinate uniform ready for drawing elements using world coordinates glBindBufferBase GL_UNIFORM_BUFFER 0 (pdata ^. matUBO) withArray (perspectiveMatrixb rot camzoom trans wins viewFroms) $ \ptr -> glNamedBufferSubData (pdata ^. matUBO) 0 64 ptr + renderLayer DebugLayer shadV layerCounts setViewport _graphics_world_resolution cfig glBindFramebuffer GL_FRAMEBUFFER (pdata ^. fboBase ._1 . unFBO) glDepthMask GL_TRUE @@ -335,7 +351,7 @@ doDrawing' win pdata u = do drawShader (_fullscreenShader pdata) 4 --set viewport for radial distortion --setViewportSize (round winx) (round winy) - setViewport _graphics_overlay_resolution cfig + setViewport (const FullRes) cfig glDepthFunc GL_ALWAYS glBlendFunc GL_ONE GL_ZERO -- perform any radial distortion @@ -357,14 +373,10 @@ doDrawing' win pdata u = do toList = (pdata ^. fboBase . _2 . _1) : concat (repeat [snd $ _fbo2 pdata, snd $ _fbo3 pdata]) bindings = zipWith (>>) (map bindFBO fboList) (map (glBindTextureUnit 1 . _unTO) toList) zipWithM_ (>>) bindings $ map bindDrawDist rds - glDepthFunc GL_ALWAYS - glDepthMask GL_FALSE - glEnable GL_BLEND glBlendFunc GL_SRC_ALPHA GL_ONE_MINUS_SRC_ALPHA - renderLayer DebugLayer shadV layerCounts - -- draw overlay - glBindBufferBase GL_UNIFORM_BUFFER 0 (pdata ^. orthonormalMatUBO) - renderLayer FixedCoordLayer shadV layerCounts + --draw overlay onto base buffer + glBindTextureUnit 0 (pdata ^. fboOverlay . _2 . unTO) + drawShader (_fullscreenShader pdata) 4 SDL.glSwapWindow win getDistortions :: Configuration -> World -> [Distortion] diff --git a/src/Dodge/TestString.hs b/src/Dodge/TestString.hs index 0089d5515..b1d8c99d2 100644 --- a/src/Dodge/TestString.hs +++ b/src/Dodge/TestString.hs @@ -13,6 +13,8 @@ import Dodge.Data.Universe testStringInit :: Universe -> [String] testStringInit u = [show $ length $ lightsToRender (u ^. uvConfig) (u ^. uvWorld . cWorld . camPos) (u ^. uvWorld . cWorld . lWorld) + , show $ u ^. uvConfig . windowX + , show $ u ^. uvConfig . windowY , show $ u ^. uvWorld . input . scrollTestInt ] --[show $ u ^? uvWorld . hud . hudElement . diSections . sssExtra . sssSelPos . _Just] diff --git a/src/Framebuffer/Update.hs b/src/Framebuffer/Update.hs index 4d24c4f2a..25c22d28e 100644 --- a/src/Framebuffer/Update.hs +++ b/src/Framebuffer/Update.hs @@ -7,15 +7,15 @@ module Framebuffer.Update ( sizeFBOs, ) where -import Dodge.Data.Config -import Shader.Data -import Framebuffer.Check import Control.Lens import Control.Monad import Data.Preload.Render +import Dodge.Data.Config import Foreign.Marshal.Array +import Framebuffer.Check import GLHelp import Graphics.GL.Core45 +import Shader.Data import Unsafe.Coerce sizeFBOs :: @@ -24,28 +24,50 @@ sizeFBOs :: IO RenderData sizeFBOs cfig rdata = uncurry (resizeRBO (_rboBaseBloom rdata)) (getWindowSize _graphics_world_resolution cfig) - >> uncurry updateFBOTO3 (getWindowSize _graphics_world_resolution cfig) - GL_NEAREST GL_NEAREST GL_RGBA8 GL_RGBA16F GL_RGBA16F rdata fboBase - >>= flip ( uncurry updateFBOTO3 (getWindowSize _graphics_world_resolution cfig) - GL_NEAREST GL_NEAREST GL_RGBA8 GL_RGBA16F GL_RGBA16F) fboCloud - >>= flip ( uncurry updateFBOTO (getWindowSize _graphics_world_resolution cfig) - GL_NEAREST GL_NEAREST GL_RGBA8) fboLighting - >>= - ffoldM - (uncurry updateFBOTO (getWindowSize _graphics_world_resolution cfig) - GL_LINEAR_MIPMAP_LINEAR GL_LINEAR GL_RGBA16F) - [fboBloom, fboPos] - >>= - ffoldM - (uncurry updateFBOTO (getWindowSize _graphics_overlay_resolution cfig) GL_NEAREST GL_NEAREST GL_RGBA8) - [fbo2, fbo3] - >>= ffoldM - (uncurry updateFBOTO (getWindowSize _graphics_downsize_resolution cfig) GL_LINEAR_MIPMAP_LINEAR GL_LINEAR GL_RGBA16F) - [fboHalf1, fboHalf2, fboHalf3] - >>= resizeShadowFBO' cfig + >> foldM + ( uncurry + updateFBOTO3 + (getWindowSize _graphics_world_resolution cfig) + GL_NEAREST + GL_NEAREST + GL_RGBA8 + GL_RGBA16F + GL_RGBA16F + ) + rdata + [fboBase, fboCloud] + >>= flip + ( uncurry + updateFBOTO + (getWindowSize _graphics_world_resolution cfig) + GL_NEAREST + GL_NEAREST + GL_RGBA8 + ) + fboLighting + >>= ffoldM + ( uncurry + updateFBOTO + (getWindowSize _graphics_world_resolution cfig) + GL_LINEAR_MIPMAP_LINEAR + GL_LINEAR + GL_RGBA16F + ) + [fboBloom, fboPos] + >>= ffoldM + (uncurry updateFBOTO (getWindowSize _graphics_overlay_resolution cfig) GL_LINEAR GL_NEAREST GL_RGBA8) + [fbo2, fbo3, fboOverlay] + >>= ffoldM + (uncurry updateFBOTO (getWindowSize _graphics_downsize_resolution cfig) GL_LINEAR_MIPMAP_LINEAR GL_LINEAR GL_RGBA16F) + [fboHalf1, fboHalf2, fboHalf3] + >>= resizeShadowFBO' cfig -ffoldM :: (Foldable t, Monad m) => - (a1 -> a2 -> m a1) -> t a2 -> a1 -> m a1 +ffoldM :: + (Foldable t, Monad m) => + (a1 -> a2 -> m a1) -> + t a2 -> + a1 -> + m a1 ffoldM f = flip $ foldM f resizeRBO :: @@ -53,7 +75,8 @@ resizeRBO :: GLsizei -> GLsizei -> IO () -resizeRBO rboName = glNamedRenderbufferStorage +resizeRBO rboName = + glNamedRenderbufferStorage rboName GL_DEPTH24_STENCIL8 @@ -125,8 +148,16 @@ resizeShadowFBO x y (fbo, (oldto1, oldto2)) = do mglDelete glDeleteTextures $ _unTO oldto1 mglDelete glDeleteTextures $ _unTO oldto2 to1 <- initializeTexture2DArray fbo GL_COLOR_ATTACHMENT0 x y 20 GL_NEAREST GL_NEAREST GL_RGBA8 - to2 <- initializeTexture2DArray - fbo GL_DEPTH_STENCIL_ATTACHMENT x y 20 GL_NEAREST GL_NEAREST GL_DEPTH24_STENCIL8 + to2 <- + initializeTexture2DArray + fbo + GL_DEPTH_STENCIL_ATTACHMENT + x + y + 20 + GL_NEAREST + GL_NEAREST + GL_DEPTH24_STENCIL8 checkFBO fbo return (fbo, (TO to1, TO to2)) @@ -155,7 +186,7 @@ resizeShadowFBO x y (fbo, (oldto1, oldto2)) = do -- return (fbo, (TO to1, TO to2)) resizeFBOTO3 :: - (FBO, (TO, TO,TO)) -> + (FBO, (TO, TO, TO)) -> Int -> Int -> -- | minification filter @@ -168,8 +199,8 @@ resizeFBOTO3 :: GLenum -> -- | internal color format3 GLenum -> - IO (FBO, (TO, TO,TO)) -resizeFBOTO3 (fbo, (toOld1, toOld2,told3)) xsize ysize minfilt magfilt inFormat1 inFormat2 inform3 = do + IO (FBO, (TO, TO, TO)) +resizeFBOTO3 (fbo, (toOld1, toOld2, told3)) xsize ysize minfilt magfilt inFormat1 inFormat2 inform3 = do glBindFramebuffer GL_FRAMEBUFFER (_unFBO fbo) mglDelete glDeleteTextures $ _unTO toOld1 mglDelete glDeleteTextures $ _unTO toOld2 @@ -180,7 +211,7 @@ resizeFBOTO3 (fbo, (toOld1, toOld2,told3)) xsize ysize minfilt magfilt inFormat1 withArray [GL_COLOR_ATTACHMENT0, GL_COLOR_ATTACHMENT1, GL_COLOR_ATTACHMENT2] $ \ptr -> glNamedFramebufferDrawBuffers (_unFBO fbo) 3 ptr checkFBO fbo - return (fbo, (TO to1, TO to2,TO to3)) + return (fbo, (TO to1, TO to2, TO to3)) resizeFBOTO :: (FBO, TO) -> @@ -223,7 +254,7 @@ initializeTexture2DArray fbo attachpoint x y z minfilt magfilt informat = do glNamedFramebufferTexture (_unFBO fbo) attachpoint to1 0 return to1 -getWindowSize :: Integral a => (Configuration -> ResFactor) -> Configuration -> (a,a) -getWindowSize f cfig = (g _windowX,g _windowY) +getWindowSize :: Integral a => (Configuration -> ResFactor) -> Configuration -> (a, a) +getWindowSize f cfig = (g _windowX, g _windowY) where g h = fromIntegral $ round (h cfig) `div` resFactorNum (f cfig) diff --git a/src/Preload/Render.hs b/src/Preload/Render.hs index 9f7397d34..ef0c53e42 100644 --- a/src/Preload/Render.hs +++ b/src/Preload/Render.hs @@ -37,7 +37,7 @@ preloadRender = do glBindBufferBase GL_UNIFORM_BUFFER 0 theUBO orthonormalUBO <- mglCreate glCreateBuffers - withArray (isoMatrix 0 1 (V2 0 0) (V2 2 2)) $ \ptr -> + withArray (isoMatrix 0 1 (V2 0 0) (V2 1 1)) $ \ptr -> glNamedBufferStorage orthonormalUBO 64 ptr 0 lightsubo <- mglCreate glCreateBuffers @@ -84,6 +84,8 @@ preloadRender = do cslist <- makeShaderVBO "picture/charArray" [vert, frag] [3, 4, 4] pmTriangles initTexture2DArray 50 "data/texture/charMapVertBig.png" 2 32 64 95 GL_NEAREST_MIPMAP_LINEAR GL_LINEAR + --initTexture2DArray 50 "data/texture/charMapVertBig.png" 2 32 64 95 GL_LINEAR_MIPMAP_LINEAR GL_LINEAR + --initTexture2DArray 50 "data/texture/charMapVertBig.png" 2 32 64 95 GL_LINEAR_MIPMAP_NEAREST GL_LINEAR screentexturevbo <- mglCreate glCreateBuffers withArray (concat cornerList) $ \ptr ->