Render cleanup

This commit is contained in:
2023-03-24 00:14:25 +00:00
parent 9a66a09f96
commit 2bba3f7268
3 changed files with 106 additions and 91 deletions
Binary file not shown.

Before

Width:  |  Height:  |  Size: 9.4 KiB

After

Width:  |  Height:  |  Size: 9.4 KiB

+93 -88
View File
@@ -5,21 +5,20 @@ module Dodge.Render (
--import qualified Data.Vector as V --import qualified Data.Vector as V
import Dodge.DownscaleSize
import Data.List (sortOn)
import Shader.Poke.Cloud
import GLHelp
import Control.Lens import Control.Lens
import Control.Monad import Control.Monad
import qualified Control.Monad.Parallel as MP import qualified Control.Monad.Parallel as MP
import Data.List (sortOn)
import qualified Data.Map.Strict as M import qualified Data.Map.Strict as M
import Data.Preload.Render import Data.Preload.Render
import qualified Data.Vector.Unboxed.Mutable as UMV import qualified Data.Vector.Unboxed.Mutable as UMV
import Dodge.Data.Universe import Dodge.Data.Universe
import Dodge.DownscaleSize
import Dodge.Render.Lights import Dodge.Render.Lights
import Dodge.Render.ShapePicture import Dodge.Render.ShapePicture
import Dodge.Render.Walls import Dodge.Render.Walls
import Foreign import Foreign
import GLHelp
import Geometry import Geometry
import Graphics.GL.Core45 import Graphics.GL.Core45
import MatrixHelper import MatrixHelper
@@ -28,8 +27,8 @@ import qualified SDL
import Shader import Shader
import Shader.Bind import Shader.Bind
import Shader.Data import Shader.Data
import Shader.Parameters
import Shader.Poke import Shader.Poke
import Shader.Poke.Cloud
doDrawing :: SDL.Window -> RenderData -> Universe -> IO () doDrawing :: SDL.Window -> RenderData -> Universe -> IO ()
doDrawing window rdata u doDrawing window rdata u
@@ -51,7 +50,7 @@ doDrawing' win pdata u = do
trans = w ^. cWorld . camPos . camCenter trans = w ^. cWorld . camPos . camCenter
wins@(V2 winx winy) = V2 (_windowX cfig) (_windowY cfig) wins@(V2 winx winy) = V2 (_windowX cfig) (_windowY cfig)
resFact = resFactorNum $ cfig ^. graphics_resolution_factor resFact = resFactorNum $ cfig ^. graphics_resolution_factor
( windowPoints, wallSPics, wallsToPoke) = wallsToDraw w (windowPoints, wallSPics, wallsToPoke) = wallsToDraw w
lightPoints = lightsToRender cfig (w ^. cWorld . camPos) (w ^. cWorld . lWorld) lightPoints = lightsToRender cfig (w ^. cWorld . camPos) (w ^. cWorld . lWorld)
viewFroms@(V2 vfx vfy) = w ^. cWorld . camPos . camViewFrom viewFroms@(V2 vfx vfy) = w ^. cWorld . camPos . camViewFrom
shadV = _pictureShaders pdata shadV = _pictureShaders pdata
@@ -61,7 +60,7 @@ doDrawing' win pdata u = do
layerCounts <- UMV.replicate (numLayers * 6) 0 layerCounts <- UMV.replicate (numLayers * 6) 0
-- attempt to poke in parallel -- attempt to poke in parallel
let (ws, wp) = wallSPics <> worldSPic cfig u let (ws, wp) = wallSPics <> worldSPic cfig u
(( nWins,trueNWalls), (nShapeVs, nIndices, nSilIndices)) <- ((nWins, trueNWalls), (nShapeVs, nIndices, nSilIndices)) <-
MP.bindM3 MP.bindM3
(\_ a b -> return (a, b)) (\_ a b -> return (a, b))
( pokeLayVerxs ( pokeLayVerxs
@@ -70,8 +69,8 @@ doDrawing' win pdata u = do
wp wp
) )
( pokeWallsWindows ( pokeWallsWindows
( pdata ^. vboWindows . vboPtr) (pdata ^. vboWindows . vboPtr)
( pdata ^. wallVBO . vboPtr) (pdata ^. wallVBO . vboPtr)
windowPoints windowPoints
wallsToPoke wallsToPoke
) )
@@ -82,57 +81,59 @@ doDrawing' win pdata u = do
(0, 0, 0) (0, 0, 0)
ws ws
) )
(nCloudVs, nCloudIs) <-
foldM
(pokeCloud (pdata ^. cloudVBO . vboPtr) (pdata ^. cloudEBO . eboPtr))
(0, 0)
(sortOn (^. clPos . _3) $ w ^. cWorld . lWorld . clouds)
-- bind wall points, silhouette data, surface geometry -- bind wall points, silhouette data, surface geometry
bufferShaderLayers shadV layerCounts bufferShaderLayers shadV layerCounts
uncurry (zipWithM_ bufferPokedVBO) $ mapM_
unzip (uncurry bufferPokedVBO)
[ (pdata ^. vboWindows, nWins) [ (pdata ^. vboWindows, nWins)
, (pdata ^. wallVBO, trueNWalls) , (pdata ^. wallVBO, trueNWalls)
] , (_vboShapes pdata, nShapeVs)
bufferPokedVBO (_vboShapes pdata) nShapeVs , (pdata ^. cloudVBO, nCloudVs)
(nCloudVs,nCloudIs) <- foldM (pokeCloud (pdata ^. cloudVBO . vboPtr) (pdata ^. cloudEBO . eboPtr)) ]
(0,0) (sortOn (^. clPos . _3) $ w ^. cWorld . lWorld . clouds) mapM_
bufferPokedVBO (pdata ^. cloudVBO) nCloudVs (uncurry bufferEBO)
glNamedBufferSubData [ (_cloudEBO pdata, nCloudIs)
(_eboName $ _cloudEBO pdata) , (_shapeEBO pdata, nIndices)
0 , (_silhouetteEBO pdata, nSilIndices)
(fromIntegral $ glushortSize * nCloudIs) ]
(_eboPtr $ _cloudEBO pdata)
glNamedBufferSubData
(_eboName $ _shapeEBO pdata)
0
(fromIntegral $ glushortSize * nIndices)
(_eboPtr $ _shapeEBO pdata)
glNamedBufferSubData
(_eboName $ _silhouetteEBO pdata)
0
(fromIntegral $ glushortSize * nSilIndices)
(_eboPtr $ _silhouetteEBO pdata)
-- set the coordinate uniform ready for drawing elements using world coordinates -- set the coordinate uniform ready for drawing elements using world coordinates
glBindBufferBase GL_UNIFORM_BUFFER 0 (pdata ^. matUBO) glBindBufferBase GL_UNIFORM_BUFFER 0 (pdata ^. matUBO)
withArray (perspectiveMatrixb rot camzoom trans wins viewFroms) $ \ptr -> withArray (perspectiveMatrixb rot camzoom trans wins viewFroms) $ \ptr ->
glNamedBufferSubData (pdata ^. matUBO) 0 64 ptr glNamedBufferSubData (pdata ^. matUBO) 0 64 ptr
setViewportSize (round winx `div` resFact) (round winy `div` resFact) setViewportSize (round winx `div` resFact) (round winy `div` resFact)
glBindFramebuffer GL_FRAMEBUFFER (_unFBO (fst (_fboBase pdata))) glBindFramebuffer GL_FRAMEBUFFER (pdata ^. fboBase ._1 . unFBO)
glDepthMask GL_TRUE glDepthMask GL_TRUE
-- the following clear may not be necessary -- the following clear may not be necessary
withArray [0,0,0,1] $ \ptr -> glClearNamedFramebufferfv withArray [0, 0, 0, 1] $ \ptr ->
(pdata ^. fboBase . _1 . unFBO) glClearNamedFramebufferfv
GL_COLOR (pdata ^. fboBase . _1 . unFBO)
0 GL_COLOR
ptr 0
with 1 $ \ptr -> glClearNamedFramebufferfv ptr
(pdata ^. fboBase . _1 . unFBO) with 1 $ \ptr ->
GL_DEPTH glClearNamedFramebufferfv
0 (pdata ^. fboBase . _1 . unFBO)
ptr GL_DEPTH
0
ptr
glDepthFunc GL_LESS glDepthFunc GL_LESS
-- draw wall occlusions from the camera's point of view -- draw wall occlusions from the camera's point of view
glUseProgram (pdata ^. lightingWallShadShader . shaderUINT) glUseProgram (pdata ^. lightingWallShadShader . shaderUINT)
glProgramUniform3f (pdata ^. lightingWallShadShader . shaderUINT) glProgramUniform3f
0 vfx vfy 20 (pdata ^. lightingWallShadShader . shaderUINT)
glProgramUniform1f (pdata ^. lightingWallShadShader . shaderUINT) 0
1 1000 vfx
vfy
20
glProgramUniform1f
(pdata ^. lightingWallShadShader . shaderUINT)
1
1000
glBindVertexArray $ pdata ^. lightingWallShadShader . shaderVAO . vaoName glBindVertexArray $ pdata ^. lightingWallShadShader . shaderVAO . vaoName
unless (debugOn Remove_LOS cfig) $ unless (debugOn Remove_LOS cfig) $
glDrawArrays glDrawArrays
@@ -140,11 +141,12 @@ doDrawing' win pdata u = do
0 0
(fromIntegral trueNWalls) (fromIntegral trueNWalls)
-- clear normals -- clear normals
withArray [0,0,0,0] $ \ptr -> glClearNamedFramebufferfv withArray [0, 0, 0, 0] $ \ptr ->
(pdata ^. fboBase . _1 . unFBO) glClearNamedFramebufferfv
GL_COLOR (pdata ^. fboBase . _1 . unFBO)
2 GL_COLOR
ptr 2
ptr
--draw walls onto base buffer --draw walls onto base buffer
glDisable GL_BLEND glDisable GL_BLEND
-- maybe cull faces? maybe do this before? -- maybe cull faces? maybe do this before?
@@ -157,17 +159,17 @@ doDrawing' win pdata u = do
0 0
(fromIntegral trueNWalls) (fromIntegral trueNWalls)
--glDisable GL_CULL_FACE --glDisable GL_CULL_FACE
-- if cfig ^. graphics_wall_textured -- if cfig ^. graphics_wall_textured
-- then renderTextureWalls pdata nWalls -- then renderTextureWalls pdata nWalls
-- else renderBlankWalls pdata nWalls -- else renderBlankWalls pdata nWalls
--draw object pictures onto base buffer --draw object pictures onto base buffer
renderLayer BottomLayer shadV layerCounts renderLayer BottomLayer shadV layerCounts
--draw object shapes onto base buffer --draw object shapes onto base buffer
let fs = _shapeShader pdata let fs = _shapeShader pdata
glUseProgram (_shaderUINT fs) glUseProgram (_shaderUINT fs)
glBindVertexArray $ fs ^. shaderVAO . vaoName glBindVertexArray $ fs ^. shaderVAO . vaoName
-- glEnable GL_CULL_FACE -- glEnable GL_CULL_FACE
-- glCullFace GL_BACK -- glCullFace GL_BACK
glDrawElements glDrawElements
(_unPrimitiveMode $ _shaderPrimitive fs) (_unPrimitiveMode $ _shaderPrimitive fs)
(fromIntegral nIndices) (fromIntegral nIndices)
@@ -178,7 +180,7 @@ doDrawing' win pdata u = do
glUseProgram (pdata ^. floorShader . shaderUINT) glUseProgram (pdata ^. floorShader . shaderUINT)
glBindVertexArray $ pdata ^. floorShader . shaderVAO . vaoName glBindVertexArray $ pdata ^. floorShader . shaderVAO . vaoName
glDrawArrays glDrawArrays
(_unPrimitiveMode $ pdata ^. floorShader . shaderPrimitive ) (_unPrimitiveMode $ pdata ^. floorShader . shaderPrimitive)
0 0
(fromIntegral nFls) (fromIntegral nFls)
glEnable GL_BLEND glEnable GL_BLEND
@@ -207,11 +209,12 @@ doDrawing' win pdata u = do
ptr ptr
--draw bloom onto bloom buffer --draw bloom onto bloom buffer
glBindFramebuffer GL_FRAMEBUFFER (pdata ^. fboBloom . _1 . unFBO) glBindFramebuffer GL_FRAMEBUFFER (pdata ^. fboBloom . _1 . unFBO)
withArray [0,0,0,0] $ \ptr -> glClearNamedFramebufferfv withArray [0, 0, 0, 0] $ \ptr ->
(pdata ^. fboBloom . _1 . unFBO) glClearNamedFramebufferfv
GL_COLOR (pdata ^. fboBloom . _1 . unFBO)
0 GL_COLOR
ptr 0
ptr
glDepthFunc GL_LESS glDepthFunc GL_LESS
glBlendFunc GL_SRC_ALPHA GL_ONE_MINUS_SRC_ALPHA glBlendFunc GL_SRC_ALPHA GL_ONE_MINUS_SRC_ALPHA
glDepthMask GL_FALSE glDepthMask GL_FALSE
@@ -242,12 +245,13 @@ doDrawing' win pdata u = do
--glBlendFuncSeparate GL_SRC_ALPHA_SATURATE GL_ONE_MINUS_SRC_ALPHA GL_ONE GL_ONE_MINUS_SRC_ALPHA --glBlendFuncSeparate GL_SRC_ALPHA_SATURATE GL_ONE_MINUS_SRC_ALPHA GL_ONE GL_ONE_MINUS_SRC_ALPHA
--glBlendFunc GL_SRC_ALPHA GL_ONE_MINUS_SRC_ALPHA --glBlendFunc GL_SRC_ALPHA GL_ONE_MINUS_SRC_ALPHA
withArray [GL_COLOR_ATTACHMENT0, GL_NONE] $ \ptr -> glDrawBuffers 2 ptr withArray [GL_COLOR_ATTACHMENT0, GL_NONE] $ \ptr -> glDrawBuffers 2 ptr
withArray [0.5,0.5,0.5,0] $ \ptr -> glClearNamedFramebufferfv withArray [0.5, 0.5, 0.5, 0] $ \ptr ->
(pdata ^. fboCloud . _1 . unFBO) glClearNamedFramebufferfv
GL_COLOR (pdata ^. fboCloud . _1 . unFBO)
0 GL_COLOR
ptr 0
-- renderLayer MidLayer shadV layerCounts ptr
-- renderLayer MidLayer shadV layerCounts
glUseProgram (pdata ^. cloudShader . shaderUINT) glUseProgram (pdata ^. cloudShader . shaderUINT)
glBindVertexArray $ pdata ^. cloudShader . shaderVAO . vaoName glBindVertexArray $ pdata ^. cloudShader . shaderVAO . vaoName
glDrawElements glDrawElements
@@ -262,16 +266,18 @@ doDrawing' win pdata u = do
glDepthFunc GL_ALWAYS glDepthFunc GL_ALWAYS
glDisable GL_BLEND glDisable GL_BLEND
withArray [GL_NONE, GL_COLOR_ATTACHMENT1, GL_COLOR_ATTACHMENT2] $ \ptr -> glDrawBuffers 3 ptr withArray [GL_NONE, GL_COLOR_ATTACHMENT1, GL_COLOR_ATTACHMENT2] $ \ptr -> glDrawBuffers 3 ptr
withArray [0,0,0,0] $ \ptr -> glClearNamedFramebufferfv withArray [0, 0, 0, 0] $ \ptr ->
(pdata ^. fboCloud . _1 . unFBO) glClearNamedFramebufferfv
GL_COLOR (pdata ^. fboCloud . _1 . unFBO)
1 GL_COLOR
ptr 1
withArray [0,0,0,0] $ \ptr -> glClearNamedFramebufferfv ptr
(pdata ^. fboCloud . _1 . unFBO) withArray [0, 0, 0, 0] $ \ptr ->
GL_COLOR glClearNamedFramebufferfv
2 (pdata ^. fboCloud . _1 . unFBO)
ptr GL_COLOR
2
ptr
glEnable GL_BLEND glEnable GL_BLEND
-- we sum the positions weighted by alpha, and sum the alpha -- we sum the positions weighted by alpha, and sum the alpha
glBlendFuncSeparatei 1 GL_SRC_ALPHA GL_ONE GL_ONE GL_ONE glBlendFuncSeparatei 1 GL_SRC_ALPHA GL_ONE GL_ONE GL_ONE
@@ -287,11 +293,12 @@ doDrawing' win pdata u = do
nullPtr nullPtr
drawShader (_windowShader pdata) nWins drawShader (_windowShader pdata) nWins
glBindFramebuffer GL_FRAMEBUFFER (pdata ^. fboPos . _1 . unFBO) glBindFramebuffer GL_FRAMEBUFFER (pdata ^. fboPos . _1 . unFBO)
withArray [0,0,0,0] $ \ptr -> glClearNamedFramebufferfv withArray [0, 0, 0, 0] $ \ptr ->
(pdata ^. fboPos . _1 . unFBO) glClearNamedFramebufferfv
GL_COLOR (pdata ^. fboPos . _1 . unFBO)
0 GL_COLOR
ptr 0
ptr
glBindTextureUnit 0 (pdata ^. fboCloud . _2 . _2 . unTO) glBindTextureUnit 0 (pdata ^. fboCloud . _2 . _2 . unTO)
glDepthMask GL_FALSE glDepthMask GL_FALSE
drawShader (pdata ^. alphaDivideShader) 4 drawShader (pdata ^. alphaDivideShader) 4
@@ -378,6 +385,4 @@ setViewportSize :: Int -> Int -> IO ()
setViewportSize x y = setViewportSize x y =
glViewport 0 0 (fromIntegral x) (fromIntegral y) glViewport 0 0 (fromIntegral x) (fromIntegral y)
--cullFace $= Nothing --cullFace $= Nothing
+10
View File
@@ -1,8 +1,10 @@
module Shader.Bind ( module Shader.Bind (
bufferShaderLayers, bufferShaderLayers,
bufferPokedVBO, bufferPokedVBO,
bufferEBO,
) where ) where
import Control.Lens
import Control.Monad.Primitive import Control.Monad.Primitive
import qualified Data.Vector.Fusion.Stream.Monadic as VFSM import qualified Data.Vector.Fusion.Stream.Monadic as VFSM
import qualified Data.Vector.Mutable as MV import qualified Data.Vector.Mutable as MV
@@ -22,6 +24,14 @@ bufferPokedVBO theVBO numVs =
(fromIntegral $ floatSize * numVs * _vboVertexSize theVBO) (fromIntegral $ floatSize * numVs * _vboVertexSize theVBO)
(_vboPtr theVBO) (_vboPtr theVBO)
bufferEBO :: EBO -> Int -> IO ()
bufferEBO ebo numis =
glNamedBufferSubData
(ebo ^. eboName)
0
(fromIntegral $ glushortSize * numis)
(ebo ^. eboPtr)
bufferShaderLayers :: MV.MVector (PrimState IO) (Shader, VBO) -> UMV.MVector (PrimState IO) Int -> IO () bufferShaderLayers :: MV.MVector (PrimState IO) (Shader, VBO) -> UMV.MVector (PrimState IO) Int -> IO ()
bufferShaderLayers shads counts = MV.imapM_ f shads bufferShaderLayers shads counts = MV.imapM_ f shads
where where