Remove polymorphism of shader type

This commit is contained in:
2021-06-11 19:41:17 +02:00
parent 0e0d8f4e99
commit 7b6521587d
6 changed files with 61 additions and 52 deletions
+15 -14
View File
@@ -23,6 +23,7 @@ module Shader
import Geometry
import Shader.Data
import MatrixHelper
import Picture.Data
import Foreign
import Codec.Picture
@@ -36,20 +37,20 @@ import Graphics.Rendering.OpenGL hiding (Point,translate,scale,imageHeight)
import Linear.Matrix
import Linear.V4
extractProgAndUnis :: FullShader a -> (Program,UniformLocation)
extractProgAndUnis :: FullShader -> (Program,UniformLocation)
extractProgAndUnis s = (_shaderProgram s, _shaderMatrixUniform s)
pokeShaders :: [FullShader a] -> F.FoldM IO a [Int]
pokeShaders :: [FullShader] -> F.FoldM IO RenderType [Int]
pokeShaders = traverse pokeShader
pokeShader :: FullShader a -> F.FoldM IO a Int
pokeShader :: FullShader -> F.FoldM IO RenderType Int
pokeShader fs = F.FoldM (pokeRender fls (zip ptrs nAtss)) (return 0) return
where
(_,ptrs,nAtss) = unzip3 $ _vaoBufferTargets $ _shaderVAO fs
fls = _shaderPokeStrategy fs
pokeRender :: (a -> [[[Float]]])
-> [(Ptr Float,Int)] -> Int -> a -> IO Int
pokeRender :: (RenderType -> [[[Float]]])
-> [(Ptr Float,Int)] -> Int -> RenderType -> IO Int
pokeRender toFs ptrs n rt = pokeList ptrs n (toFs rt)
pokeList :: [(Ptr Float,Int)] -> Int -> [[[Float]]] -> IO Int
@@ -72,15 +73,15 @@ bindArrayBuffers numVs ps = do
bindBuffer ArrayBuffer $= Just bo
bufferData ArrayBuffer $= (fromIntegral $ floatSize * numVs * i, ptr, DynamicDraw)
bindShaderBuffers :: [FullShader a] -> [Int] -> IO ()
bindShaderBuffers :: [FullShader] -> [Int] -> IO ()
bindShaderBuffers = zipWithM_ f
where
f fs i = bindArrayBuffers i $ _vaoBufferTargets $ _shaderVAO fs
drawShaders :: [FullShader a] -> [Int] -> IO ()
drawShaders :: [FullShader] -> [Int] -> IO ()
drawShaders = zipWithM_ drawShader
drawShader :: FullShader a -> Int -> IO ()
drawShader :: FullShader -> Int -> IO ()
{-# INLINE drawShader #-}
drawShader fs i = do
currentProgram $= Just (_shaderProgram fs)
@@ -93,7 +94,7 @@ drawShader fs i = do
-- I am not sure if this assumes that the shader is constructed directly before
-- the texture is added...
addTexture :: String -> FullShader a -> IO (FullShader a)
addTexture :: String -> FullShader -> IO (FullShader)
addTexture texturePath shad = do
Right cmap <- readImage texturePath
let tex = convertRGBA8 cmap
@@ -118,8 +119,8 @@ makeShader
-> [ShaderType] -- ^ Filetype extensions
-> [(GLuint,Int)] -- ^ The shaders input vertices (location, size)
-> PrimitiveMode
-> (a -> [[[Float]]]) -- ^ Poke strategy: method for creating a list of vertex data to be bound
-> IO (FullShader a)
-> (RenderType -> [[[Float]]]) -- ^ Poke strategy: method for creating a list of vertex data to be bound
-> IO (FullShader)
makeShader s shaderlist alocs pm renStrat = do
(prog,unis) <- makeSourcedShader s shaderlist
vaob <- setupVAO alocs
@@ -215,7 +216,7 @@ makeSourcedShader s sts = do
uniformLocations <- uniformLocation prog "worldMat"
return (prog,uniformLocations)
addUniforms :: [String] -> FullShader a -> IO (FullShader a)
addUniforms :: [String] -> FullShader -> IO (FullShader)
addUniforms uniStrings shad = do
uniLocs <- mapM (uniformLocation $ _shaderProgram shad) uniStrings
return $ shad & shaderCustomUnis %~ (++ uniLocs)
@@ -286,7 +287,7 @@ setPerpMatUniform
-> Point2 -- ^ translation
-> Point2 -- ^ window size
-> Point2 -- ^ viewfrom point
-> FullShader a
-> FullShader
-> IO ()
setPerpMatUniform rot czoom trans wins vFrom shad = do
pmat <- (newMatrix RowMajor $ perspectiveMatrix rot czoom trans wins vFrom) :: IO (GLmatrix GLfloat)
@@ -294,7 +295,7 @@ setPerpMatUniform rot czoom trans wins vFrom shad = do
uniform (_shaderMatrixUniform shad) $= pmat
return ()
freeShaderPointers :: FullShader a -> IO ()
freeShaderPointers :: FullShader -> IO ()
freeShaderPointers fs = do
forM_ (_vaoBufferTargets $ _shaderVAO fs) $ \(_,ptr,_) ->
free ptr