summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorCsaba Hruska <csaba.hruska@gmail.com>2016-01-10 22:58:12 +0100
committerCsaba Hruska <csaba.hruska@gmail.com>2016-01-10 22:58:12 +0100
commit2344d2af7eb3e5515408d134a074b989e7b5efde (patch)
tree01b905a55d0b8e0560097241dd2cd277f6bfab46 /src
parente936d3c32c17c0d00939893fa85996c3807ed3e7 (diff)
update example
Diffstat (limited to 'src')
-rw-r--r--src/LambdaCube/GL.hs13
-rw-r--r--src/LambdaCube/GL/Backend.hs10
-rw-r--r--src/LambdaCube/GL/Input.hs33
3 files changed, 48 insertions, 8 deletions
diff --git a/src/LambdaCube/GL.hs b/src/LambdaCube/GL.hs
index 3e11be1..258d2ba 100644
--- a/src/LambdaCube/GL.hs
+++ b/src/LambdaCube/GL.hs
@@ -28,6 +28,7 @@ module LambdaCube.GL (
28 uploadTexture2DToGPU', 28 uploadTexture2DToGPU',
29 29
30 -- GL: Renderer, Storage, Object 30 -- GL: Renderer, Storage, Object
31 GLUniformName,
31 GLRenderer, 32 GLRenderer,
32 GLStorage, 33 GLStorage,
33 Object, 34 Object,
@@ -48,7 +49,6 @@ module LambdaCube.GL (
48 setObjectOrder, 49 setObjectOrder,
49 objectUniformSetter, 50 objectUniformSetter,
50 setScreenSize, 51 setScreenSize,
51 sortSlotObjects,
52 52
53 uniformBool, 53 uniformBool,
54 uniformV2B, 54 uniformV2B,
@@ -80,7 +80,16 @@ module LambdaCube.GL (
80 uniformM43F, 80 uniformM43F,
81 uniformM44F, 81 uniformM44F,
82 82
83 uniformFTexture2D 83 uniformFTexture2D,
84
85 -- schema builder utility functions
86 (@:),
87 defObjectArray,
88 defUniforms,
89 makeSchema,
90
91 (@=),
92 updateUniforms
84) where 93) where
85 94
86import LambdaCube.GL.Type 95import LambdaCube.GL.Type
diff --git a/src/LambdaCube/GL/Backend.hs b/src/LambdaCube/GL/Backend.hs
index 2753ac3..677e925 100644
--- a/src/LambdaCube/GL/Backend.hs
+++ b/src/LambdaCube/GL/Backend.hs
@@ -696,12 +696,12 @@ renderSlot cmds = forM_ cmds $ \cmd -> do
696 texUnit <- readIORef tuRef 696 texUnit <- readIORef tuRef
697 glActiveTexture $ GL_TEXTURE0 + fromIntegral texUnit 697 glActiveTexture $ GL_TEXTURE0 + fromIntegral texUnit
698 glBindTexture txTarget txObj 698 glBindTexture txTarget txObj
699 putStrLn $ "to texture unit " ++ show texUnit ++ " texture object " ++ show txObj 699 --putStrLn $ "to texture unit " ++ show texUnit ++ " texture object " ++ show txObj
700 GLSetVertexAttrib idx val -> do 700 GLSetVertexAttrib idx val -> do
701 glDisableVertexAttribArray idx 701 glDisableVertexAttribArray idx
702 setVertexAttrib idx val 702 setVertexAttrib idx val
703 isOk <- checkGL 703 --isOk <- checkGL
704 putStrLn $ isOk ++ " - " ++ show cmd 704 --putStrLn $ isOk ++ " - " ++ show cmd
705 705
706renderFrame :: GLRenderer -> IO () 706renderFrame :: GLRenderer -> IO ()
707renderFrame glp = do 707renderFrame glp = do
@@ -750,8 +750,8 @@ renderFrame glp = do
750 GLSaveImage 750 GLSaveImage
751 GLLoadImage 751 GLLoadImage
752 -} 752 -}
753 isOk <- checkGL 753 --isOk <- checkGL
754 putStrLn $ isOk ++ " - " ++ show cmd 754 --putStrLn $ isOk ++ " - " ++ show cmd
755 755
756data CGState 756data CGState
757 = CGState 757 = CGState
diff --git a/src/LambdaCube/GL/Input.hs b/src/LambdaCube/GL/Input.hs
index 742fc35..7e4ba74 100644
--- a/src/LambdaCube/GL/Input.hs
+++ b/src/LambdaCube/GL/Input.hs
@@ -1,8 +1,10 @@
1{-# LANGUAGE FlexibleContexts #-}
1module LambdaCube.GL.Input where 2module LambdaCube.GL.Input where
2 3
3import Control.Applicative 4import Control.Applicative
4import Control.Exception 5import Control.Exception
5import Control.Monad 6import Control.Monad
7import Control.Monad.Writer
6import Data.IORef 8import Data.IORef
7import Data.Map (Map) 9import Data.Map (Map)
8import Data.IntMap (IntMap) 10import Data.IntMap (IntMap)
@@ -68,7 +70,7 @@ allocStorage sch = do
68 } 70 }
69 71
70disposeStorage :: GLStorage -> IO () 72disposeStorage :: GLStorage -> IO ()
71disposeStorage = error "not implemented: disposeStorage" 73disposeStorage _ = putStrLn "not implemented: disposeStorage"
72 74
73-- object 75-- object
74addObject :: GLStorage -> String -> Primitive -> Maybe (IndexStream Buffer) -> Map String (Stream Buffer) -> [String] -> IO Object 76addObject :: GLStorage -> String -> Primitive -> Maybe (IndexStream Buffer) -> Map String (Stream Buffer) -> [String] -> IO Object
@@ -137,6 +139,7 @@ addObject input slotName prim indices attribs uniformNames = do
137 let emptyV = V.replicate (V.length $ glPrograms p) [] 139 let emptyV = V.replicate (V.length $ glPrograms p) []
138 return $ emptyV // [(prgIdx,createObjectCommands (glTexUnitMapping p) topUnis obj (glPrograms p ! prgIdx))| prgIdx <- glSlotPrograms p ! pSlotIdx] 140 return $ emptyV // [(prgIdx,createObjectCommands (glTexUnitMapping p) topUnis obj (glPrograms p ! prgIdx))| prgIdx <- glSlotPrograms p ! pSlotIdx]
139 writeIORef cmdsRef cmds 141 writeIORef cmdsRef cmds
142 sortSlotObjects input
140 return obj 143 return obj
141 144
142removeObject :: GLStorage -> Object -> IO () 145removeObject :: GLStorage -> Object -> IO ()
@@ -149,6 +152,7 @@ setObjectOrder :: GLStorage -> Object -> Int -> IO ()
149setObjectOrder p obj i = do 152setObjectOrder p obj i = do
150 writeIORef (objOrder obj) i 153 writeIORef (objOrder obj) i
151 modifyIORef (slotVector p ! objSlot obj) $ \(GLSlot objs sorted _) -> GLSlot objs sorted Reorder 154 modifyIORef (slotVector p ! objSlot obj) $ \(GLSlot objs sorted _) -> GLSlot objs sorted Reorder
155 sortSlotObjects p
152 156
153objectUniformSetter :: Object -> Map GLUniformName InputSetter 157objectUniformSetter :: Object -> Map GLUniformName InputSetter
154objectUniformSetter = objUniSetter 158objectUniformSetter = objUniSetter
@@ -387,3 +391,30 @@ uniformM44F n is = case Map.lookup n is of
387uniformFTexture2D n is = case Map.lookup n is of 391uniformFTexture2D n is = case Map.lookup n is of
388 Just (SFTexture2D fun) -> fun 392 Just (SFTexture2D fun) -> fun
389 _ -> nullSetter n "FTexture2D" 393 _ -> nullSetter n "FTexture2D"
394
395a @: b = tell [(a,b)]
396defObjectArray n p m = mapM_ tell [PipelineSchema (Map.singleton n $ ObjectArraySchema p $ Map.singleton a t) mempty | (a,t) <- execWriter m]
397defUniforms m = tell $ PipelineSchema mempty $ Map.fromList $ execWriter m
398makeSchema a = execWriter a :: PipelineSchema
399
400unionObjectArraySchema (ObjectArraySchema a1 b1) (ObjectArraySchema a2 b2) =
401 ObjectArraySchema (if a1 == a2 then a1 else error $ "object array schema primitive mismatch " ++ show (a1,a2))
402 (Map.unionWith (\a b -> if a == b then a else error $ "object array schema attribute type mismatch " ++ show (a,b)) b1 b2)
403
404instance Monoid PipelineSchema where
405 mempty = PipelineSchema mempty mempty
406 mappend (PipelineSchema a1 b1) (PipelineSchema a2 b2) =
407 PipelineSchema (Map.unionWith unionObjectArraySchema a1 a2) (Map.unionWith (\a b -> if a == b then a else error $ "schema type mismatch " ++ show (a,b)) b1 b2)
408
409type UniM = Writer [GLStorage -> IO ()]
410
411class UniformSetter a where
412 (@=) :: GLUniformName -> IO a -> UniM ()
413
414instance UniformSetter Float where
415 n @= act = tell [\s -> let f = uniformFloat n (uniformSetter s) in f =<< act]
416
417instance UniformSetter TextureData where
418 n @= act = tell [\s -> let f = uniformFTexture2D n (uniformSetter s) in f =<< act]
419
420updateUniforms storage m = sequence_ $ let l = map ($ storage) $ execWriter m in l