diff options
author | Csaba Hruska <csaba.hruska@gmail.com> | 2016-01-08 14:53:25 +0100 |
---|---|---|
committer | Csaba Hruska <csaba.hruska@gmail.com> | 2016-01-08 14:53:25 +0100 |
commit | 2cc82723838be8f9fc084c7582bbc87f26e1a794 (patch) | |
tree | 154ecec9f0a82f0d6aeb3c5a9f2cd7b489cfa8cb /src/LambdaCube/GL/Backend.hs | |
parent | 64e13239772dae2a73e30bd0aa8ca2c70154987c (diff) |
use Map instead of Trie
Diffstat (limited to 'src/LambdaCube/GL/Backend.hs')
-rw-r--r-- | src/LambdaCube/GL/Backend.hs | 82 |
1 files changed, 40 insertions, 42 deletions
diff --git a/src/LambdaCube/GL/Backend.hs b/src/LambdaCube/GL/Backend.hs index 7251a78..196280d 100644 --- a/src/LambdaCube/GL/Backend.hs +++ b/src/LambdaCube/GL/Backend.hs | |||
@@ -11,8 +11,6 @@ import Data.IntMap (IntMap) | |||
11 | import Data.Maybe (isNothing,fromJust) | 11 | import Data.Maybe (isNothing,fromJust) |
12 | import Data.Map (Map) | 12 | import Data.Map (Map) |
13 | import Data.Set (Set) | 13 | import Data.Set (Set) |
14 | import Data.Trie as T | ||
15 | import Data.Trie.Convenience as T | ||
16 | import Data.Vector (Vector,(!),(//)) | 14 | import Data.Vector (Vector,(!),(//)) |
17 | import qualified Data.ByteString.Char8 as SB | 15 | import qualified Data.ByteString.Char8 as SB |
18 | import qualified Data.Foldable as F | 16 | import qualified Data.Foldable as F |
@@ -198,7 +196,7 @@ clearRenderTarget values = do | |||
198 | printGLStatus = checkGL >>= print | 196 | printGLStatus = checkGL >>= print |
199 | printFBOStatus = checkFBO >>= print | 197 | printFBOStatus = checkFBO >>= print |
200 | 198 | ||
201 | compileProgram :: Trie InputType -> Program -> IO GLProgram | 199 | compileProgram :: Map ByteString InputType -> Program -> IO GLProgram |
202 | compileProgram uniTrie p = do | 200 | compileProgram uniTrie p = do |
203 | po <- glCreateProgram | 201 | po <- glCreateProgram |
204 | putStrLn $ "compile program: " ++ show po | 202 | putStrLn $ "compile program: " ++ show po |
@@ -230,10 +228,10 @@ compileProgram uniTrie p = do | |||
230 | (attributes,attributesType) <- queryStreams po | 228 | (attributes,attributesType) <- queryStreams po |
231 | print uniforms | 229 | print uniforms |
232 | print attributes | 230 | print attributes |
233 | let lcUniforms = (toTrie $ programUniforms p) `unionL` (toTrie $ programInTextures p) | 231 | let lcUniforms = (toTrie $ programUniforms p) `Map.union` (toTrie $ programInTextures p) |
234 | lcStreams = fmap ty (toTrie $ programStreams p) | 232 | lcStreams = fmap ty (toTrie $ programStreams p) |
235 | check a m = and $ map go $ T.toList m | 233 | check a m = and $ map go $ Map.toList m |
236 | where go (k,b) = case T.lookup k a of | 234 | where go (k,b) = case Map.lookup k a of |
237 | Nothing -> False | 235 | Nothing -> False |
238 | Just x -> x == b | 236 | Just x -> x == b |
239 | unless (check lcUniforms uniformsType) $ do | 237 | unless (check lcUniforms uniformsType) $ do |
@@ -241,31 +239,31 @@ compileProgram uniTrie p = do | |||
241 | putStrLn $ "actual: " ++ show uniformsType | 239 | putStrLn $ "actual: " ++ show uniformsType |
242 | fail "shader program uniform input mismatch!" | 240 | fail "shader program uniform input mismatch!" |
243 | unless (check lcStreams attributesType) $ fail $ "shader program stream input mismatch! " ++ show (attributesType,lcStreams) | 241 | unless (check lcStreams attributesType) $ fail $ "shader program stream input mismatch! " ++ show (attributesType,lcStreams) |
244 | -- the public (user) pipeline and program input is encoded by the slots, therefore the programs does not distinct the render and slot textures input | 242 | -- the public (user) pipeline and program input is encoded by the objectArrays, therefore the programs does not distinct the render and slot textures input |
245 | let inUniNames = toTrie $ programUniforms p | 243 | let inUniNames = toTrie $ programUniforms p |
246 | inUniforms = L.filter (\(n,v) -> T.member n inUniNames) $ T.toList $ uniforms | 244 | inUniforms = L.filter (\(n,v) -> Map.member n inUniNames) $ Map.toList $ uniforms |
247 | inTextureNames = toTrie $ programInTextures p | 245 | inTextureNames = toTrie $ programInTextures p |
248 | inTextures = L.filter (\(n,v) -> T.member n inTextureNames) $ T.toList $ uniforms | 246 | inTextures = L.filter (\(n,v) -> Map.member n inTextureNames) $ Map.toList $ uniforms |
249 | texUnis = [n | (n,_) <- inTextures, T.member n uniTrie] | 247 | texUnis = [n | (n,_) <- inTextures, Map.member n uniTrie] |
250 | putStrLn $ "uniTrie: " ++ show (T.keys uniTrie) | 248 | putStrLn $ "uniTrie: " ++ show (Map.keys uniTrie) |
251 | putStrLn $ "inUniNames: " ++ show inUniNames | 249 | putStrLn $ "inUniNames: " ++ show inUniNames |
252 | putStrLn $ "inUniforms: " ++ show inUniforms | 250 | putStrLn $ "inUniforms: " ++ show inUniforms |
253 | putStrLn $ "inTextureNames: " ++ show inTextureNames | 251 | putStrLn $ "inTextureNames: " ++ show inTextureNames |
254 | putStrLn $ "inTextures: " ++ show inTextures | 252 | putStrLn $ "inTextures: " ++ show inTextures |
255 | putStrLn $ "texUnis: " ++ show texUnis | 253 | putStrLn $ "texUnis: " ++ show texUnis |
256 | let valA = T.toList $ attributes | 254 | let valA = Map.toList $ attributes |
257 | valB = T.toList $ toTrie $ programStreams p | 255 | valB = Map.toList $ toTrie $ programStreams p |
258 | putStrLn "------------" | 256 | putStrLn "------------" |
259 | print $ T.toList $ attributes | 257 | print $ Map.toList $ attributes |
260 | print $ T.toList $ toTrie $ programStreams p | 258 | print $ Map.toList $ toTrie $ programStreams p |
261 | let lcStreamName = fmap name (toTrie $ programStreams p) | 259 | let lcStreamName = fmap name (toTrie $ programStreams p) |
262 | return $ GLProgram | 260 | return $ GLProgram |
263 | { shaderObjects = objs | 261 | { shaderObjects = objs |
264 | , programObject = po | 262 | , programObject = po |
265 | , inputUniforms = T.fromList inUniforms | 263 | , inputUniforms = Map.fromList inUniforms |
266 | , inputTextures = T.fromList inTextures | 264 | , inputTextures = Map.fromList inTextures |
267 | , inputTextureUniforms = S.fromList $ texUnis | 265 | , inputTextureUniforms = S.fromList $ texUnis |
268 | , inputStreams = T.fromList [(n,(idx, pack attrName)) | (n,idx) <- T.toList $ attributes, let Just attrName = T.lookup n lcStreamName] | 266 | , inputStreams = Map.fromList [(n,(idx, pack attrName)) | (n,idx) <- Map.toList $ attributes, let Just attrName = Map.lookup n lcStreamName] |
269 | } | 267 | } |
270 | 268 | ||
271 | compileSampler :: SamplerDescriptor -> IO GLSampler | 269 | compileSampler :: SamplerDescriptor -> IO GLSampler |
@@ -415,32 +413,32 @@ compileStreamData s = do | |||
415 | , glStreamProgram = V.head $ streamPrograms s | 413 | , glStreamProgram = V.head $ streamPrograms s |
416 | } | 414 | } |
417 | 415 | ||
418 | createStreamCommands :: Trie (IORef GLint) -> Trie GLUniform -> Trie (Stream Buffer) -> Primitive -> GLProgram -> [GLObjectCommand] | 416 | createStreamCommands :: Map ByteString (IORef GLint) -> Map ByteString GLUniform -> Map ByteString (Stream Buffer) -> Primitive -> GLProgram -> [GLObjectCommand] |
419 | createStreamCommands texUnitMap topUnis attrs primitive prg = streamUniCmds ++ streamCmds ++ [drawCmd] | 417 | createStreamCommands texUnitMap topUnis attrs primitive prg = streamUniCmds ++ streamCmds ++ [drawCmd] |
420 | where | 418 | where |
421 | -- object draw command | 419 | -- object draw command |
422 | drawCmd = GLDrawArrays prim 0 (fromIntegral count) | 420 | drawCmd = GLDrawArrays prim 0 (fromIntegral count) |
423 | where | 421 | where |
424 | prim = primitiveToGLType primitive | 422 | prim = primitiveToGLType primitive |
425 | count = head [c | Stream _ _ _ _ c <- T.elems attrs] | 423 | count = head [c | Stream _ _ _ _ c <- Map.elems attrs] |
426 | 424 | ||
427 | -- object uniform commands | 425 | -- object uniform commands |
428 | -- texture slot setup commands | 426 | -- texture slot setup commands |
429 | streamUniCmds = uniCmds ++ texCmds | 427 | streamUniCmds = uniCmds ++ texCmds |
430 | where | 428 | where |
431 | uniCmds = [GLSetUniform i u | (n,i) <- uniMap, let u = topUni n] | 429 | uniCmds = [GLSetUniform i u | (n,i) <- uniMap, let u = topUni n] |
432 | uniMap = T.toList $ inputUniforms prg | 430 | uniMap = Map.toList $ inputUniforms prg |
433 | topUni n = T.lookupWithDefault (error "internal error (createStreamCommands)!") n topUnis | 431 | topUni n = Map.findWithDefault (error "internal error (createStreamCommands)!") n topUnis |
434 | texUnis = S.toList $ inputTextureUniforms prg | 432 | texUnis = S.toList $ inputTextureUniforms prg |
435 | texCmds = [ GLBindTexture (inputTypeToTextureTarget $ uniInputType u) texUnit u | 433 | texCmds = [ GLBindTexture (inputTypeToTextureTarget $ uniInputType u) texUnit u |
436 | | n <- texUnis | 434 | | n <- texUnis |
437 | , let u = topUni n | 435 | , let u = topUni n |
438 | , let texUnit = T.lookupWithDefault (error "internal error (createStreamCommands - Texture Unit)") n texUnitMap | 436 | , let texUnit = Map.findWithDefault (error "internal error (createStreamCommands - Texture Unit)") n texUnitMap |
439 | ] | 437 | ] |
440 | uniInputType (GLUniform ty _) = ty | 438 | uniInputType (GLUniform ty _) = ty |
441 | 439 | ||
442 | -- object attribute stream commands | 440 | -- object attribute stream commands |
443 | streamCmds = [attrCmd i s | (i,name) <- T.elems attrMap, let Just s = T.lookup name attrs] | 441 | streamCmds = [attrCmd i s | (i,name) <- Map.elems attrMap, let Just s = Map.lookup name attrs] |
444 | where | 442 | where |
445 | attrMap = inputStreams prg | 443 | attrMap = inputStreams prg |
446 | attrCmd i s = case s of | 444 | attrCmd i s = case s of |
@@ -485,7 +483,7 @@ allocRenderer p = do | |||
485 | prgs <- V.mapM (compileProgram uniTrie) $ programs p | 483 | prgs <- V.mapM (compileProgram uniTrie) $ programs p |
486 | -- texture unit mapping ioref trie | 484 | -- texture unit mapping ioref trie |
487 | -- texUnitMapRefs :: Map UniformName (IORef TextureUnit) | 485 | -- texUnitMapRefs :: Map UniformName (IORef TextureUnit) |
488 | texUnitMapRefs <- T.fromList <$> mapM (\k -> (k,) <$> newIORef 0) (S.toList $ S.fromList $ concat $ V.toList $ V.map (T.keys . toTrie . programInTextures) $ programs p) | 486 | texUnitMapRefs <- Map.fromList <$> mapM (\k -> (k,) <$> newIORef 0) (S.toList $ S.fromList $ concat $ V.toList $ V.map (Map.keys . toTrie . programInTextures) $ programs p) |
489 | let (cmds,st) = runState (mapM (compileCommand texUnitMapRefs smps texs trgs prgs) $ V.toList $ commands p) initCGState | 487 | let (cmds,st) = runState (mapM (compileCommand texUnitMapRefs smps texs trgs prgs) $ V.toList $ commands p) initCGState |
490 | input <- newIORef Nothing | 488 | input <- newIORef Nothing |
491 | -- default Vertex Array Object | 489 | -- default Vertex Array Object |
@@ -518,8 +516,8 @@ disposeRenderer p = do | |||
518 | with (glVAO p) $ (glDeleteVertexArrays 1) | 516 | with (glVAO p) $ (glDeleteVertexArrays 1) |
519 | 517 | ||
520 | {- | 518 | {- |
521 | data SlotSchema | 519 | data ObjectArraySchema |
522 | = SlotSchema | 520 | = ObjectArraySchema |
523 | { primitive :: FetchPrimitive | 521 | { primitive :: FetchPrimitive |
524 | , attributes :: Trie StreamType | 522 | , attributes :: Trie StreamType |
525 | } | 523 | } |
@@ -527,13 +525,13 @@ data SlotSchema | |||
527 | 525 | ||
528 | data PipelineSchema | 526 | data PipelineSchema |
529 | = PipelineSchema | 527 | = PipelineSchema |
530 | { slots :: Trie SlotSchema | 528 | { objectArrays :: Trie ObjectArraySchema |
531 | , uniforms :: Trie InputType | 529 | , uniforms :: Trie InputType |
532 | } | 530 | } |
533 | deriving Show | 531 | deriving Show |
534 | -} | 532 | -} |
535 | isSubTrie :: (a -> a -> Bool) -> Trie a -> Trie a -> Bool | 533 | isSubTrie :: (a -> a -> Bool) -> Map ByteString a -> Map ByteString a -> Bool |
536 | isSubTrie eqFun universe subset = and [isMember a (T.lookup n universe) | (n,a) <- T.toList subset] | 534 | isSubTrie eqFun universe subset = and [isMember a (Map.lookup n universe) | (n,a) <- Map.toList subset] |
537 | where | 535 | where |
538 | isMember a Nothing = False | 536 | isMember a Nothing = False |
539 | isMember a (Just b) = eqFun a b | 537 | isMember a (Just b) = eqFun a b |
@@ -541,12 +539,12 @@ isSubTrie eqFun universe subset = and [isMember a (T.lookup n universe) | (n,a) | |||
541 | -- TODO: if there is a mismatch thow detailed error message in the excoeption, containing the missing attributes and uniforms | 539 | -- TODO: if there is a mismatch thow detailed error message in the excoeption, containing the missing attributes and uniforms |
542 | {- | 540 | {- |
543 | let sch = schema input | 541 | let sch = schema input |
544 | forM_ uniformNames $ \n -> case T.lookup n (uniforms sch) of | 542 | forM_ uniformNames $ \n -> case Map.lookup n (uniforms sch) of |
545 | Nothing -> throw $ userError $ "Unknown uniform: " ++ show n | 543 | Nothing -> throw $ userError $ "Unknown uniform: " ++ show n |
546 | _ -> return () | 544 | _ -> return () |
547 | case T.lookup slotName (slots sch) of | 545 | case Map.lookup slotName (objectArrays sch) of |
548 | Nothing -> throw $ userError $ "Unknown slot: " ++ show slotName | 546 | Nothing -> throw $ userError $ "Unknown slot: " ++ show slotName |
549 | Just (SlotSchema sPrim sAttrs) -> do | 547 | Just (ObjectArraySchema sPrim sAttrs) -> do |
550 | when (sPrim /= (primitiveToFetchPrimitive prim)) $ throw $ userError $ | 548 | when (sPrim /= (primitiveToFetchPrimitive prim)) $ throw $ userError $ |
551 | "Primitive mismatch for slot (" ++ show slotName ++ ") expected " ++ show sPrim ++ " but got " ++ show prim | 549 | "Primitive mismatch for slot (" ++ show slotName ++ ") expected " ++ show sPrim ++ " but got " ++ show prim |
552 | let sType = fmap streamToStreamType attribs | 550 | let sType = fmap streamToStreamType attribs |
@@ -571,7 +569,7 @@ setStorage' p input' = do | |||
571 | -} | 569 | -} |
572 | {- | 570 | {- |
573 | deletion: | 571 | deletion: |
574 | - remove pipeline's object commands from used slots | 572 | - remove pipeline's object commands from used objectArrays |
575 | - remove pipeline from attached pipelines vector | 573 | - remove pipeline from attached pipelines vector |
576 | -} | 574 | -} |
577 | ic' <- readIORef $ glInput p | 575 | ic' <- readIORef $ glInput p |
@@ -592,7 +590,7 @@ setStorage' p input' = do | |||
592 | - get an id from pipeline input | 590 | - get an id from pipeline input |
593 | - add to attached pipelines | 591 | - add to attached pipelines |
594 | - generate slot mappings | 592 | - generate slot mappings |
595 | - update used slots, and generate object commands for objects in the related slots | 593 | - update used objectArrays, and generate object commands for objects in the related objectArrays |
596 | -} | 594 | -} |
597 | case input' of | 595 | case input' of |
598 | Nothing -> writeIORef (glInput p) Nothing >> return Nothing | 596 | Nothing -> writeIORef (glInput p) Nothing >> return Nothing |
@@ -610,11 +608,11 @@ setStorage' p input' = do | |||
610 | return (i,Nothing) | 608 | return (i,Nothing) |
611 | -- create input connection | 609 | -- create input connection |
612 | let sm = slotMap input | 610 | let sm = slotMap input |
613 | pToI = [i | n <- glSlotNames p, let Just i = T.lookup n sm] | 611 | pToI = [i | n <- glSlotNames p, let Just i = Map.lookup n sm] |
614 | iToP = V.update (V.replicate (T.size sm) Nothing) (V.imap (\i v -> (v, Just i)) pToI) | 612 | iToP = V.update (V.replicate (Map.size sm) Nothing) (V.imap (\i v -> (v, Just i)) pToI) |
615 | writeIORef (glInput p) $ Just $ InputConnection idx input pToI iToP | 613 | writeIORef (glInput p) $ Just $ InputConnection idx input pToI iToP |
616 | 614 | ||
617 | -- generate object commands for related slots | 615 | -- generate object commands for related objectArrays |
618 | {- | 616 | {- |
619 | for each slot in pipeline: | 617 | for each slot in pipeline: |
620 | map slot name to input slot name | 618 | map slot name to input slot name |
@@ -771,7 +769,7 @@ initCGState = CGState | |||
771 | 769 | ||
772 | type CG a = State CGState a | 770 | type CG a = State CGState a |
773 | 771 | ||
774 | compileCommand :: Trie (IORef GLint) -> Vector GLSampler -> Vector GLTexture -> Vector GLRenderTarget -> Vector GLProgram -> Command -> CG GLCommand | 772 | compileCommand :: Map ByteString (IORef GLint) -> Vector GLSampler -> Vector GLTexture -> Vector GLRenderTarget -> Vector GLProgram -> Command -> CG GLCommand |
775 | compileCommand texUnitMap samplers textures targets programs cmd = case cmd of | 773 | compileCommand texUnitMap samplers textures targets programs cmd = case cmd of |
776 | SetRasterContext rCtx -> return $ GLSetRasterContext rCtx | 774 | SetRasterContext rCtx -> return $ GLSetRasterContext rCtx |
777 | SetAccumulationContext aCtx -> return $ GLSetAccumulationContext aCtx | 775 | SetAccumulationContext aCtx -> return $ GLSetAccumulationContext aCtx |
@@ -782,9 +780,9 @@ compileCommand texUnitMap samplers textures targets programs cmd = case cmd of | |||
782 | SetSamplerUniform n tu -> do | 780 | SetSamplerUniform n tu -> do |
783 | modify (\s@CGState{..} -> s {samplerUniforms = Map.insert n tu samplerUniforms}) | 781 | modify (\s@CGState{..} -> s {samplerUniforms = Map.insert n tu samplerUniforms}) |
784 | p <- currentProgram <$> get | 782 | p <- currentProgram <$> get |
785 | case T.lookup (pack n) (inputTextures $ programs ! p) of | 783 | case Map.lookup (pack n) (inputTextures $ programs ! p) of |
786 | Nothing -> fail $ "internal error (SetSamplerUniform)! - " ++ show cmd | 784 | Nothing -> fail $ "internal error (SetSamplerUniform)! - " ++ show cmd |
787 | Just i -> case T.lookup (pack n) texUnitMap of | 785 | Just i -> case Map.lookup (pack n) texUnitMap of |
788 | Nothing -> fail $ "internal error (SetSamplerUniform - IORef)! - " ++ show cmd | 786 | Nothing -> fail $ "internal error (SetSamplerUniform - IORef)! - " ++ show cmd |
789 | Just r -> return $ GLSetSamplerUniform i (fromIntegral tu) r | 787 | Just r -> return $ GLSetSamplerUniform i (fromIntegral tu) r |
790 | SetTexture tu t -> do | 788 | SetTexture tu t -> do |