summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJoe Crayne <joe@jerkface.net>2019-04-21 15:35:01 -0400
committerJoe Crayne <joe@jerkface.net>2019-05-11 19:24:49 -0400
commite4a0905679ebb6796e09a7c45cfddb4291781cd9 (patch)
tree3dba6db336fe539e5b7f76e0ec002c67f1c428a8
parentd5056330392550ba42b245cc25b81cc649088ef2 (diff)
Some type signatures (and build fix?).
-rw-r--r--src/LambdaCube/Compiler/CoreToIR.hs105
1 files changed, 104 insertions, 1 deletions
diff --git a/src/LambdaCube/Compiler/CoreToIR.hs b/src/LambdaCube/Compiler/CoreToIR.hs
index 357702f5..45bfa1ad 100644
--- a/src/LambdaCube/Compiler/CoreToIR.hs
+++ b/src/LambdaCube/Compiler/CoreToIR.hs
@@ -21,6 +21,7 @@ import Data.List
21import qualified Data.Map as Map 21import qualified Data.Map as Map
22import qualified Data.Set as Set 22import qualified Data.Set as Set
23import qualified Data.Vector as Vector 23import qualified Data.Vector as Vector
24import Data.String
24import GHC.Stack 25import GHC.Stack
25import GHC.Word 26import GHC.Word
26import Control.Arrow hiding ((<+>)) 27import Control.Arrow hiding ((<+>))
@@ -73,16 +74,39 @@ type CG = State (List IR.StreamData, Map IR.Program Int, List IR.RenderTarget, M
73 74
74type List a = (Int, [a]) 75type List a = (Int, [a])
75 76
77streamLens :: ((t1 -> (t1, t2, t3, t4, t5)) -> t6 -> t7)
78 -> (t6, t2, t3, t4, t5) -> t7
76streamLens f (a,b,c,d,e) = f (,b,c,d,e) a 79streamLens f (a,b,c,d,e) = f (,b,c,d,e) a
80programLens :: ((t1 -> (t2, t1, t3, t4, t5)) -> t6 -> t7)
81 -> (t2, t6, t3, t4, t5) -> t7
77programLens f (a,b,c,d,e) = f (a,,c,d,e) b 82programLens f (a,b,c,d,e) = f (a,,c,d,e) b
83targetLens :: ((t1 -> (t2, t3, t1, t4, t5)) -> t6 -> t7)
84 -> (t2, t3, t6, t4, t5) -> t7
78targetLens f (a,b,c,d,e) = f (a,b,,d,e) c 85targetLens f (a,b,c,d,e) = f (a,b,,d,e) c
86slotLens :: ((t1 -> (t2, t3, t4, t1, t5)) -> t6 -> t7)
87 -> (t2, t3, t4, t6, t5) -> t7
79slotLens f (a,b,c,d,e) = f (a,b,c,,e) d 88slotLens f (a,b,c,d,e) = f (a,b,c,,e) d
89textureLens :: ((t1 -> (t2, t3, t4, t5, t1)) -> t6 -> t7)
90 -> (t2, t3, t4, t5, t6) -> t7
80textureLens f (a,b,c,d,e) = f (a,b,c,d,) e 91textureLens f (a,b,c,d,e) = f (a,b,c,d,) e
81 92
93modL :: MonadState s m =>
94 (((b -> c) -> a1 -> (d, c)) -> s -> (a2, s))
95 -> (a1 -> (d, b)) -> m a2
82modL gs f = state $ gs $ \fx -> second fx . f 96modL gs f = state $ gs $ \fx -> second fx . f
83 97
98addL' :: (MonadState s m, Ord k) =>
99 (((Map k (Int, t) -> c) -> Map k (Int, t) -> (Int, c))
100 -> s -> (a, s))
101 -> k -> (t -> t -> t) -> t -> m a
84addL' l p f x = modL l $ \sv -> maybe (length sv, Map.insert p (length sv, x) sv) (\(i, x') -> (i, Map.insert p (i, f x x') sv)) $ Map.lookup p sv 102addL' l p f x = modL l $ \sv -> maybe (length sv, Map.insert p (length sv, x) sv) (\(i, x') -> (i, Map.insert p (i, f x x') sv)) $ Map.lookup p sv
103addL :: (MonadState s m, Num a1) =>
104 ((((a1, [a2]) -> c) -> (a1, [a2]) -> (a1, c)) -> s -> (a3, s))
105 -> a2 -> m a3
85addL l x = modL l $ \(i, sv) -> (i, (i+1, x: sv)) 106addL l x = modL l $ \(i, sv) -> (i, (i+1, x: sv))
107addLEq :: (MonadState s m, Ord k) =>
108 (((Map k Int -> c) -> Map k Int -> (Int, c)) -> s -> (a, s))
109 -> k -> m a
86addLEq l x = modL l $ \sv -> maybe (let i = length sv in i `seq` (i, Map.insert x i sv)) (\i -> (i, sv)) $ Map.lookup x sv 110addLEq l x = modL l $ \sv -> maybe (let i = length sv in i `seq` (i, Map.insert x i sv)) (\i -> (i, sv)) $ Map.lookup x sv
87 111
88--------------------------------------------------------- 112---------------------------------------------------------
@@ -251,6 +275,7 @@ type SamplerBinding = (IR.UniformName,IR.ImageRef)
251 275
252---------------------------------------------------------------- 276----------------------------------------------------------------
253 277
278frameBufferType :: ExpTV -> ExpTV
254frameBufferType (A2 "FrameBuffer" _ ty) = ty 279frameBufferType (A2 "FrameBuffer" _ ty) = ty
255frameBufferType x = error $ "illegal target type: " ++ ppShow x 280frameBufferType x = error $ "illegal target type: " ++ ppShow x
256 281
@@ -266,44 +291,55 @@ getImageTextureTypes = map (imageInputTypeTextureType . compImageInputType) . ge
266getImageInputTypes :: ExpTV -> [IR.InputType] 291getImageInputTypes :: ExpTV -> [IR.InputType]
267getImageInputTypes = map compImageInputType . getFramebufferType 292getImageInputTypes = map compImageInputType . getFramebufferType
268 293
294getFragFilter :: ExpTV -> (Maybe ExpTV, ExpTV)
269getFragFilter (A2 "map" (EtaPrim2 "filterFragment" p) x) = (Just p, x) 295getFragFilter (A2 "map" (EtaPrim2 "filterFragment" p) x) = (Just p, x)
270getFragFilter x = (Nothing, x) 296getFragFilter x = (Nothing, x)
271 297
298getVertexShader :: ExpTV -> ((Maybe ExpTV, Ty), ExpTV)
272getVertexShader (A2 "map" (EtaPrim2 "mapPrimitive" f@(etaReds -> Just (_, o))) x) = ((Just f, tyOf o), x) 299getVertexShader (A2 "map" (EtaPrim2 "mapPrimitive" f@(etaReds -> Just (_, o))) x) = ((Just f, tyOf o), x)
273--getVertexShader (A2 "map" (EtaPrim2 "mapPrimitive" f) x) = error $ "gff: " ++ show (case f of ExpTV x _ _ -> x) --ppShow (mapVal unFunc' f) 300--getVertexShader (A2 "map" (EtaPrim2 "mapPrimitive" f) x) = error $ "gff: " ++ show (case f of ExpTV x _ _ -> x) --ppShow (mapVal unFunc' f)
274--getVertexShader x = error $ "gf: " ++ ppShow x 301--getVertexShader x = error $ "gf: " ++ ppShow x
275getVertexShader x = ((Nothing, getPrim' $ tyOf x), x) 302getVertexShader x = ((Nothing, getPrim' $ tyOf x), x)
276 303
304getFragmentShader :: ExpTV -> ((Maybe ExpTV, Ty), ExpTV)
277getFragmentShader (A2 "map" (EtaPrim2 "mapFragment" f@(etaReds -> Just (_, o))) x) = ((Just f, tyOf o), x) 305getFragmentShader (A2 "map" (EtaPrim2 "mapFragment" f@(etaReds -> Just (_, o))) x) = ((Just f, tyOf o), x)
278--getFragmentShader (A2 "map" (EtaPrim2 "mapFragment" f) x) = error $ "gff: " ++ ppShow f 306--getFragmentShader (A2 "map" (EtaPrim2 "mapFragment" f) x) = error $ "gff: " ++ ppShow f
279--getFragmentShader x = error $ "gf: " ++ ppShow x 307--getFragmentShader x = error $ "gf: " ++ ppShow x
280getFragmentShader x = ((Nothing, getPrim'' $ tyOf x), x) 308getFragmentShader x = ((Nothing, getPrim'' $ tyOf x), x)
281 309
310getPrim :: ExpTV -> ExpTV
282getPrim (A1 "List" (A2 "Primitive" _ p)) = p 311getPrim (A1 "List" (A2 "Primitive" _ p)) = p
312getPrim' :: ExpTV -> ExpTV
283getPrim' (A1 "List" (A2 "Primitive" a _)) = a 313getPrim' (A1 "List" (A2 "Primitive" a _)) = a
314getPrim'' :: ExpTV -> ExpTV
284getPrim'' (A1 "List" (A2 "Vector" _ (A1 "Maybe" (A1 "SimpleFragment" (TTuple [a]))))) = a 315getPrim'' (A1 "List" (A2 "Vector" _ (A1 "Maybe" (A1 "SimpleFragment" (TTuple [a]))))) = a
285getPrim'' x = error $ "getPrim'':" ++ ppShow x 316getPrim'' x = error $ "getPrim'':" ++ ppShow x
286 317
318compFrameBuffer :: ExpTV -> IR.ClearImage
287compFrameBuffer = \case 319compFrameBuffer = \case
288 A1 "DepthImage" a -> IR.ClearImage IR.Depth $ compValue a 320 A1 "DepthImage" a -> IR.ClearImage IR.Depth $ compValue a
289 A1 "ColorImage" a -> IR.ClearImage IR.Color $ compValue a 321 A1 "ColorImage" a -> IR.ClearImage IR.Color $ compValue a
290 x -> error $ "compFrameBuffer " ++ ppShow x 322 x -> error $ "compFrameBuffer " ++ ppShow x
291 323
324compList :: ExpTV -> [ExpTV]
292compList (A2 ":" a x) = a : compList x 325compList (A2 ":" a x) = a : compList x
293compList (A0 "Nil") = [] 326compList (A0 "Nil") = []
294compList x = error $ "compList: " ++ ppShow x 327compList x = error $ "compList: " ++ ppShow x
295 328
329compFilter :: ExpTV -> IR.Filter
296compFilter = \case 330compFilter = \case
297 A0 "PointFilter" -> IR.Nearest 331 A0 "PointFilter" -> IR.Nearest
298 A0 "LinearFilter" -> IR.Linear 332 A0 "LinearFilter" -> IR.Linear
299 x -> error $ "compFilter: " ++ ppShow x 333 x -> error $ "compFilter: " ++ ppShow x
300 334
335compEdgeMode :: ExpTV -> IR.EdgeMode
301compEdgeMode = \case 336compEdgeMode = \case
302 A0 "Repeat" -> IR.Repeat 337 A0 "Repeat" -> IR.Repeat
303 A0 "MirroredRepeat" -> IR.MirroredRepeat 338 A0 "MirroredRepeat" -> IR.MirroredRepeat
304 A0 "ClampToEdge" -> IR.ClampToEdge 339 A0 "ClampToEdge" -> IR.ClampToEdge
305 x -> error $ "compEdgeMode: " ++ ppShow x 340 x -> error $ "compEdgeMode: " ++ ppShow x
306 341
342compSemantic :: ExpTV -> IR.ImageSemantic
307compSemantic = \case 343compSemantic = \case
308 A0 "Depth" -> IR.Depth 344 A0 "Depth" -> IR.Depth
309 A0 "Stencil" -> IR.Stencil 345 A0 "Stencil" -> IR.Stencil
@@ -338,14 +374,17 @@ compImageInputType = \case
338 _ -> error $ "Unexpected color image element type: " <> ppShow c 374 _ -> error $ "Unexpected color image element type: " <> ppShow c
339 x -> error $ "compImageType: " ++ ppShow x 375 x -> error $ "compImageType: " ++ ppShow x
340 376
377compAC :: ExpTV -> IR.AccumulationContext
341compAC (ETuple x) = IR.AccumulationContext Nothing $ map compFrag x 378compAC (ETuple x) = IR.AccumulationContext Nothing $ map compFrag x
342 379
380compBlending :: ExpTV -> IR.Blending
343compBlending x = case x of 381compBlending x = case x of
344 A0 "NoBlending" -> IR.NoBlending 382 A0 "NoBlending" -> IR.NoBlending
345 A1 "BlendLogicOp" a -> IR.BlendLogicOp (compLO a) 383 A1 "BlendLogicOp" a -> IR.BlendLogicOp (compLO a)
346 A3 "Blend" (ETuple [a,b]) (ETuple [ETuple [c,d],ETuple [e,f]]) (compValue -> IR.VV4F g) -> IR.Blend (compBE a) (compBE b) (compBF c) (compBF d) (compBF e) (compBF f) g 384 A3 "Blend" (ETuple [a,b]) (ETuple [ETuple [c,d],ETuple [e,f]]) (compValue -> IR.VV4F g) -> IR.Blend (compBE a) (compBE b) (compBF c) (compBF d) (compBF e) (compBF f) g
347 x -> error $ "compBlending " ++ ppShow x 385 x -> error $ "compBlending " ++ ppShow x
348 386
387compBF :: ExpTV -> IR.BlendingFactor
349compBF x = case x of 388compBF x = case x of
350 A0 "ZeroBF" -> IR.Zero 389 A0 "ZeroBF" -> IR.Zero
351 A0 "OneBF" -> IR.One 390 A0 "OneBF" -> IR.One
@@ -364,6 +403,7 @@ compBF x = case x of
364 A0 "SrcAlphaSaturate" -> IR.SrcAlphaSaturate 403 A0 "SrcAlphaSaturate" -> IR.SrcAlphaSaturate
365 x -> error $ "compBF " ++ ppShow x 404 x -> error $ "compBF " ++ ppShow x
366 405
406compBE :: ExpTV -> IR.BlendEquation
367compBE x = case x of 407compBE x = case x of
368 A0 "FuncAdd" -> IR.FuncAdd 408 A0 "FuncAdd" -> IR.FuncAdd
369 A0 "FuncSubtract" -> IR.FuncSubtract 409 A0 "FuncSubtract" -> IR.FuncSubtract
@@ -372,6 +412,7 @@ compBE x = case x of
372 A0 "Max" -> IR.Max 412 A0 "Max" -> IR.Max
373 x -> error $ "compBE " ++ ppShow x 413 x -> error $ "compBE " ++ ppShow x
374 414
415compLO :: ExpTV -> IR.LogicOperation
375compLO x = case x of 416compLO x = case x of
376 A0 "Clear" -> IR.Clear 417 A0 "Clear" -> IR.Clear
377 A0 "And" -> IR.And 418 A0 "And" -> IR.And
@@ -391,6 +432,7 @@ compLO x = case x of
391 A0 "Set" -> IR.Set 432 A0 "Set" -> IR.Set
392 x -> error $ "compLO " ++ ppShow x 433 x -> error $ "compLO " ++ ppShow x
393 434
435compComparisonFunction :: ExpTV -> IR.ComparisonFunction
394compComparisonFunction x = case x of 436compComparisonFunction x = case x of
395 A0 "Never" -> IR.Never 437 A0 "Never" -> IR.Never
396 A0 "Less" -> IR.Less 438 A0 "Less" -> IR.Less
@@ -404,19 +446,24 @@ compComparisonFunction x = case x of
404 446
405pattern EBool a <- (compBool -> Just a) 447pattern EBool a <- (compBool -> Just a)
406 448
449compBool :: ExpTV -> Maybe Bool
407compBool x = case x of 450compBool x = case x of
408 A0 "True" -> Just True 451 A0 "True" -> Just True
409 A0 "False" -> Just False 452 A0 "False" -> Just False
410 x -> Nothing 453 x -> Nothing
411 454
455compFrag :: ExpTV -> IR.FragmentOperation
412compFrag x = case x of 456compFrag x = case x of
413 A2 "DepthOp" (compComparisonFunction -> a) (EBool b) -> IR.DepthOp a b 457 A2 "DepthOp" (compComparisonFunction -> a) (EBool b) -> IR.DepthOp a b
414 A2 "ColorOp" (compBlending -> b) (compValue -> v) -> IR.ColorOp b v 458 A2 "ColorOp" (compBlending -> b) (compValue -> v) -> IR.ColorOp b v
415 x -> error $ "compFrag " ++ ppShow x 459 x -> error $ "compFrag " ++ ppShow x
416 460
461toGLSLType :: IsString p => [Char] -> ExpTV -> p
417toGLSLType msg x = showGLSLType msg $ compInputType msg x 462toGLSLType msg x = showGLSLType msg $ compInputType msg x
418 463
419-- move to lambdacube-ir? 464-- move to lambdacube-ir?
465showGLSLType :: IsString p =>
466 [Char] -> IR.InputType -> p
420showGLSLType msg = \case 467showGLSLType msg = \case
421 IR.Bool -> "bool" 468 IR.Bool -> "bool"
422 IR.Word -> "uint" 469 IR.Word -> "uint"
@@ -446,6 +493,7 @@ showGLSLType msg = \case
446 IR.FTexture2D -> "sampler2D" 493 IR.FTexture2D -> "sampler2D"
447 t -> error $ "toGLSLType: " ++ msg ++ " " ++ show t 494 t -> error $ "toGLSLType: " ++ msg ++ " " ++ show t
448 495
496supType :: ExpTV -> Bool
449supType = isJust . compInputType_ 497supType = isJust . compInputType_
450 498
451compInputType_ :: ExpTV -> Maybe IR.InputType 499compInputType_ :: ExpTV -> Maybe IR.InputType
@@ -477,12 +525,16 @@ compInputType_ x = case x of
477 TMat 4 4 TFloat -> Just IR.M44F 525 TMat 4 4 TFloat -> Just IR.M44F
478 _ -> Nothing 526 _ -> Nothing
479 527
528compInputType :: [Char] -> ExpTV -> IR.InputType
480compInputType msg x = fromMaybe (error $ "compInputType " ++ msg ++ " " ++ ppShow x) $ compInputType_ x 529compInputType msg x = fromMaybe (error $ "compInputType " ++ msg ++ " " ++ ppShow x) $ compInputType_ x
481 530
531is234 :: Integer -> Bool
482is234 = (`elem` [2,3,4]) 532is234 = (`elem` [2,3,4])
483 533
484compInputType'' (ETuple attrs) = map compAttribute attrs 534compInputType'' :: ExpTV -> [(String, IR.InputType)]
535compInputType'' (ETuple attrs) = map compAttribute attrs -- pattern fail.
485 536
537compAttribute :: ExpTV -> (String, IR.InputType)
486compAttribute = \case 538compAttribute = \case
487 x@(A1 "Attribute" (EString s)) -> (s, compInputType "compAttr" $ tyOf x) 539 x@(A1 "Attribute" (EString s)) -> (s, compInputType "compAttr" $ tyOf x)
488 x -> error $ "compAttribute " ++ ppShow x 540 x -> error $ "compAttribute " ++ ppShow x
@@ -510,6 +562,7 @@ compAttributeValue (ETuple x) = checkLength $ map go x
510 where (A1 "List" (compInputType "compAV" -> t)) = tyOf a 562 where (A1 "List" (compInputType "compAV" -> t)) = tyOf a
511 values = map compValue $ compList a 563 values = map compValue $ compList a
512 564
565compFetchPrimitive :: ExpTV -> IR.FetchPrimitive
513compFetchPrimitive x = case x of 566compFetchPrimitive x = case x of
514 A0 "Point" -> IR.Points 567 A0 "Point" -> IR.Points
515 A0 "Line" -> IR.Lines 568 A0 "Line" -> IR.Lines
@@ -533,52 +586,62 @@ compValue x = case x of
533 A4 "V4" (EBool a) (EBool b) (EBool c) (EBool d) -> IR.VV4B $ IR.V4 a b c d 586 A4 "V4" (EBool a) (EBool b) (EBool c) (EBool d) -> IR.VV4B $ IR.V4 a b c d
534 x -> error $ "compValue " ++ ppShow x 587 x -> error $ "compValue " ++ ppShow x
535 588
589compRC :: ExpTV -> IR.RasterContext
536compRC x = case x of 590compRC x = case x of
537 A3 "PointCtx" a (EFloat b) c -> IR.PointCtx (compPS a) (realToFrac b) (compPSCO c) 591 A3 "PointCtx" a (EFloat b) c -> IR.PointCtx (compPS a) (realToFrac b) (compPSCO c)
538 A2 "LineCtx" (EFloat a) b -> IR.LineCtx (realToFrac a) (compPV b) 592 A2 "LineCtx" (EFloat a) b -> IR.LineCtx (realToFrac a) (compPV b)
539 A4 "TriangleCtx" a b c d -> IR.TriangleCtx (compCM a) (compPM b) (compPO c) (compPV d) 593 A4 "TriangleCtx" a b c d -> IR.TriangleCtx (compCM a) (compPM b) (compPO c) (compPV d)
540 x -> error $ "compRC " ++ ppShow x 594 x -> error $ "compRC " ++ ppShow x
541 595
596compRC' :: ExpTV -> Maybe ExpTV
542compRC' x = case x of 597compRC' x = case x of
543 A3 "PointCtx" a _ _ -> compPS' a 598 A3 "PointCtx" a _ _ -> compPS' a
544 A4 "TriangleCtx" _ b _ _ -> compPM' b 599 A4 "TriangleCtx" _ b _ _ -> compPM' b
545 x -> Nothing 600 x -> Nothing
546 601
602compPSCO :: ExpTV -> IR.PointSpriteCoordOrigin
547compPSCO x = case x of 603compPSCO x = case x of
548 A0 "LowerLeft" -> IR.LowerLeft 604 A0 "LowerLeft" -> IR.LowerLeft
549 A0 "UpperLeft" -> IR.UpperLeft 605 A0 "UpperLeft" -> IR.UpperLeft
550 x -> error $ "compPSCO " ++ ppShow x 606 x -> error $ "compPSCO " ++ ppShow x
551 607
608compCM :: ExpTV -> IR.CullMode
552compCM x = case x of 609compCM x = case x of
553 A0 "CullNone" -> IR.CullNone 610 A0 "CullNone" -> IR.CullNone
554 A0 "CullFront" -> IR.CullFront IR.CCW 611 A0 "CullFront" -> IR.CullFront IR.CCW
555 A0 "CullBack" -> IR.CullBack IR.CCW 612 A0 "CullBack" -> IR.CullBack IR.CCW
556 x -> error $ "compCM " ++ ppShow x 613 x -> error $ "compCM " ++ ppShow x
557 614
615compPM :: ExpTV -> IR.PolygonMode
558compPM x = case x of 616compPM x = case x of
559 A0 "PolygonFill" -> IR.PolygonFill 617 A0 "PolygonFill" -> IR.PolygonFill
560 A1 "PolygonLine" (EFloat a) -> IR.PolygonLine $ realToFrac a 618 A1 "PolygonLine" (EFloat a) -> IR.PolygonLine $ realToFrac a
561 A1 "PolygonPoint" a -> IR.PolygonPoint $ compPS a 619 A1 "PolygonPoint" a -> IR.PolygonPoint $ compPS a
562 x -> error $ "compPM " ++ ppShow x 620 x -> error $ "compPM " ++ ppShow x
563 621
622compPM' :: ExpTV -> Maybe ExpTV
564compPM' x = case x of 623compPM' x = case x of
565 A1 "PolygonPoint" a -> compPS' a 624 A1 "PolygonPoint" a -> compPS' a
566 x -> Nothing 625 x -> Nothing
567 626
627compPS :: ExpTV -> IR.PointSize
568compPS x = case x of 628compPS x = case x of
569 A1 "PointSize" (EFloat a) -> IR.PointSize $ realToFrac a 629 A1 "PointSize" (EFloat a) -> IR.PointSize $ realToFrac a
570 A1 "ProgramPointSize" _ -> IR.ProgramPointSize 630 A1 "ProgramPointSize" _ -> IR.ProgramPointSize
571 x -> error $ "compPS " ++ ppShow x 631 x -> error $ "compPS " ++ ppShow x
572 632
633compPS' :: ExpTV -> Maybe ExpTV
573compPS' x = case x of 634compPS' x = case x of
574 A1 "ProgramPointSize" x -> Just x 635 A1 "ProgramPointSize" x -> Just x
575 x -> Nothing 636 x -> Nothing
576 637
638compPO :: ExpTV -> IR.PolygonOffset
577compPO x = case x of 639compPO x = case x of
578 A2 "Offset" (EFloat a) (EFloat b) -> IR.Offset (realToFrac a) (realToFrac b) 640 A2 "Offset" (EFloat a) (EFloat b) -> IR.Offset (realToFrac a) (realToFrac b)
579 A0 "NoOffset" -> IR.NoOffset 641 A0 "NoOffset" -> IR.NoOffset
580 x -> error $ "compPO " ++ ppShow x 642 x -> error $ "compPO " ++ ppShow x
581 643
644compPV :: ExpTV -> IR.ProvokingVertex
582compPV x = case x of 645compPV x = case x of
583 A0 "FirstVertex" -> IR.FirstVertex 646 A0 "FirstVertex" -> IR.FirstVertex
584 A0 "LastVertex" -> IR.LastVertex 647 A0 "LastVertex" -> IR.LastVertex
@@ -741,8 +804,10 @@ data Uniform
741 804
742type Uniforms = Map String (Uniform, IR.InputType) 805type Uniforms = Map String (Uniform, IR.InputType)
743 806
807tellUniform :: (MonadWriter (a, b) m, Monoid b) => a -> m ()
744tellUniform x = tell (x, mempty) 808tellUniform x = tell (x, mempty)
745 809
810simpleExpr :: ExpTV -> Bool
746simpleExpr = \case 811simpleExpr = \case
747 Con cn xs -> case cn of 812 Con cn xs -> case cn of
748 "Uniform" -> True 813 "Uniform" -> True
@@ -944,8 +1009,10 @@ type Ty = ExpTV
944tyOf :: ExpTV -> Ty 1009tyOf :: ExpTV -> Ty
945tyOf (ExpTV _ t vs) = t .@ vs 1010tyOf (ExpTV _ t vs) = t .@ vs
946 1011
1012expOf :: ExpTV -> I.Exp
947expOf (ExpTV x _ _) = x 1013expOf (ExpTV x _ _) = x
948 1014
1015mapVal :: (I.Exp -> I.Exp) -> ExpTV -> ExpTV
949mapVal f (ExpTV a b c) = ExpTV (f a) b c 1016mapVal f (ExpTV a b c) = ExpTV (f a) b c
950 1017
951toExp :: I.ExpType -> ExpTV 1018toExp :: I.ExpType -> ExpTV
@@ -964,18 +1031,23 @@ pattern EString s <- ELit (LString s)
964pattern EFloat s <- ELit (LFloat s) 1031pattern EFloat s <- ELit (LFloat s)
965pattern EInt s <- ELit (LInt s) 1032pattern EInt s <- ELit (LInt s)
966 1033
1034(.@) :: I.Exp -> [I.Exp] -> ExpTV
967t .@ vs = ExpTV t I.TType vs 1035t .@ vs = ExpTV t I.TType vs
968infix 1 .@ 1036infix 1 .@
969 1037
1038mkVar :: ExpTV -> Maybe (Int, ExpTV)
970mkVar (ExpTV (I.Var i) t vs) = Just (i, t .@ vs) 1039mkVar (ExpTV (I.Var i) t vs) = Just (i, t .@ vs)
971mkVar _ = Nothing 1040mkVar _ = Nothing
972 1041
1042mkPi :: ExpTV -> Maybe (Visibility, ExpTV, ExpTV)
973mkPi (ExpTV (I.Pi b x y) _ vs) = Just (b, x .@ vs, y .@ addToEnv x vs) 1043mkPi (ExpTV (I.Pi b x y) _ vs) = Just (b, x .@ vs, y .@ addToEnv x vs)
974mkPi _ = Nothing 1044mkPi _ = Nothing
975 1045
1046mkLam :: ExpTV -> Maybe (Visibility, ExpTV, ExpTV)
976mkLam (ExpTV (I.Lam y) (I.Pi b x yt) vs) = Just (b, x .@ vs, ExpTV y yt $ addToEnv x vs) 1047mkLam (ExpTV (I.Lam y) (I.Pi b x yt) vs) = Just (b, x .@ vs, ExpTV y yt $ addToEnv x vs)
977mkLam _ = Nothing 1048mkLam _ = Nothing
978 1049
1050mkCon :: ExpTV -> Maybe (SName, [ExpTV])
979mkCon (ExpTV (I.Con s n (reverse -> xs)) et vs) = Just (untick $ show s, chain vs (I.conType et s) $ I.mkConPars n et ++ xs) 1051mkCon (ExpTV (I.Con s n (reverse -> xs)) et vs) = Just (untick $ show s, chain vs (I.conType et s) $ I.mkConPars n et ++ xs)
980mkCon (ExpTV (I.TyCon s (reverse -> xs)) et vs) = Just (untick $ show s, chain vs (nType s) xs) 1052mkCon (ExpTV (I.TyCon s (reverse -> xs)) et vs) = Just (untick $ show s, chain vs (nType s) xs)
981mkCon (ExpTV (I.Neut (I.Fun s@(I.FunName _ loc _{-I.DeltaDef{}-} _) (reverse -> xs) def)) et vs) = Just (untick $ show s, drop loc $ chain vs (nType s) xs) 1053mkCon (ExpTV (I.Neut (I.Fun s@(I.FunName _ loc _{-I.DeltaDef{}-} _) (reverse -> xs) def)) et vs) = Just (untick $ show s, drop loc $ chain vs (nType s) xs)
@@ -983,14 +1055,17 @@ mkCon (ExpTV (I.CaseFun s xs n) et vs) = Just (untick $ show s, chain vs (nType
983mkCon (ExpTV (I.TyCaseFun s [m, t, f] n) et vs) = Just (untick $ show s, chain vs (nType s) [m, t, I.Neut n, f]) 1055mkCon (ExpTV (I.TyCaseFun s [m, t, f] n) et vs) = Just (untick $ show s, chain vs (nType s) [m, t, I.Neut n, f])
984mkCon _ = Nothing 1056mkCon _ = Nothing
985 1057
1058mkApp :: ExpTV -> Maybe (ExpTV, ExpTV)
986mkApp (ExpTV (I.Neut (I.App_ a b)) et vs) = Just (ExpTV (I.Neut a) t vs, head $ chain vs t [b]) 1059mkApp (ExpTV (I.Neut (I.App_ a b)) et vs) = Just (ExpTV (I.Neut a) t vs, head $ chain vs t [b])
987 where t = neutType' (mkEnv vs) a 1060 where t = neutType' (mkEnv vs) a
988mkApp _ = Nothing 1061mkApp _ = Nothing
989 1062
1063removeRHS :: (Num t, Ord t) => t -> I.Exp -> Maybe I.Exp
990removeRHS 0 (I.RHS x) = Just x 1064removeRHS 0 (I.RHS x) = Just x
991removeRHS n (I.Lam x) | n > 0 = I.Lam <$> removeRHS (n-1) x 1065removeRHS n (I.Lam x) | n > 0 = I.Lam <$> removeRHS (n-1) x
992removeRHS _ _ = Nothing 1066removeRHS _ _ = Nothing
993 1067
1068mkFunc :: ExpTV -> Maybe ([Char], ExpTV, Ty, [ExpTV])
994mkFunc r@(ExpTV (I.Neut (I.Fun (I.FunName (show -> n) loc (I.ExpDef def_) nt) xs I.RHS{})) ty vs) 1069mkFunc r@(ExpTV (I.Neut (I.Fun (I.FunName (show -> n) loc (I.ExpDef def_) nt) xs I.RHS{})) ty vs)
995 | Just def <- removeRHS (length xs) def_ 1070 | Just def <- removeRHS (length xs) def_
996 , all (supType . tyOf) (r: xs') && n `notElem` ["typeAnn"] && all validChar n 1071 , all (supType . tyOf) (r: xs') && n `notElem` ["typeAnn"] && all validChar n
@@ -1015,19 +1090,24 @@ mkFunc r@(ExpTV (I.Neut (I.Fun (I.FunName (show -> n) loc (I.ExpDef def_) nt) xs
1015-} 1090-}
1016mkFunc _ = Nothing 1091mkFunc _ = Nothing
1017 1092
1093chain :: [I.Exp] -> I.Exp -> [I.Exp] -> [ExpTV]
1018chain vs t@(I.Pi Hidden at y) (a: as) = chain vs (I.appTy t a) as 1094chain vs t@(I.Pi Hidden at y) (a: as) = chain vs (I.appTy t a) as
1019chain vs t xs = map snd $ chain' vs t xs 1095chain vs t xs = map snd $ chain' vs t xs
1020 1096
1097chain' :: [I.Exp] -> I.Exp -> [I.Exp] -> [(Visibility, ExpTV)]
1021chain' vs t [] = [] 1098chain' vs t [] = []
1022chain' vs t@(I.Pi b at y) (a: as) = (b, ExpTV a at vs): chain' vs (I.appTy t a) as 1099chain' vs t@(I.Pi b at y) (a: as) = (b, ExpTV a at vs): chain' vs (I.appTy t a) as
1023chain' vs t _ = error $ "chain: " ++ ppShow t 1100chain' vs t _ = error $ "chain: " ++ ppShow t
1024 1101
1102mkTVar :: Int -> ExpTV -> ExpTV
1025mkTVar i (ExpTV t _ vs) = ExpTV (I.Var i) t vs 1103mkTVar i (ExpTV t _ vs) = ExpTV (I.Var i) t vs
1026 1104
1105unLab' :: I.Exp -> I.Exp
1027unLab' (I.Reduced x) = unLab' x 1106unLab' (I.Reduced x) = unLab' x
1028unLab' (I.RHS x) = unLab' x -- TODO: remove 1107unLab' (I.RHS x) = unLab' x -- TODO: remove
1029unLab' x = x 1108unLab' x = x
1030 1109
1110unFunc' :: I.Exp -> I.Exp
1031unFunc' (I.Reduced x) = unFunc' x -- todo: remove? 1111unFunc' (I.Reduced x) = unFunc' x -- todo: remove?
1032unFunc' (I.Neut (I.Fun (I.FunName _ _ I.ExpDef{} _) _ y)) = unFunc' y 1112unFunc' (I.Neut (I.Fun (I.FunName _ _ I.ExpDef{} _) _ y)) = unFunc' y
1033unFunc' (I.RHS x) = unFunc' x -- TODO: remove 1113unFunc' (I.RHS x) = unFunc' x -- TODO: remove
@@ -1036,7 +1116,9 @@ unFunc' x = x
1036instance Subst I.Exp ExpTV where 1116instance Subst I.Exp ExpTV where
1037 subst_ i0 dx x (ExpTV a at vs) = ExpTV (subst_ i0 dx x a) (subst_ i0 dx x at) (zipWith (\i -> subst_ (i0+i) (I.shiftFreeVars i dx) $ up i x{-todo: review-}) [1..] vs) 1117 subst_ i0 dx x (ExpTV a at vs) = ExpTV (subst_ i0 dx x a) (subst_ i0 dx x at) (zipWith (\i -> subst_ (i0+i) (I.shiftFreeVars i dx) $ up i x{-todo: review-}) [1..] vs)
1038 1118
1119addToEnv :: a -> [a] -> [a]
1039addToEnv x xs = x: xs 1120addToEnv x xs = x: xs
1121mkEnv :: Rearrange c => [c] -> [c]
1040mkEnv xs = {-trace_ ("mk " ++ show (length xs)) $ -} zipWith up [1..] xs 1122mkEnv xs = {-trace_ ("mk " ++ show (length xs)) $ -} zipWith up [1..] xs
1041 1123
1042instance HasFreeVars ExpTV where 1124instance HasFreeVars ExpTV where
@@ -1045,15 +1127,18 @@ instance HasFreeVars ExpTV where
1045instance PShow ExpTV where 1127instance PShow ExpTV where
1046 pShow (ExpTV x t _) = pShow (x, t) 1128 pShow (ExpTV x t _) = pShow (x, t)
1047 1129
1130isSampler :: I.Exp -> Bool
1048isSampler (I.TyCon n _) = show n == "'Sampler" 1131isSampler (I.TyCon n _) = show n == "'Sampler"
1049isSampler _ = False 1132isSampler _ = False
1050 1133
1051-------------------------------------------------------------------------------- ExpTV conversion -- TODO: remove 1134-------------------------------------------------------------------------------- ExpTV conversion -- TODO: remove
1052 1135
1136removeLams :: (Eq t, Num t) => t -> ExpTV -> ExpTV
1053removeLams 0 x = x 1137removeLams 0 x = x
1054removeLams i (ELam _ x) = removeLams (i-1) x 1138removeLams i (ELam _ x) = removeLams (i-1) x
1055removeLams i (Lam Hidden _ x) = removeLams i x 1139removeLams i (Lam Hidden _ x) = removeLams i x
1056 1140
1141etaReds :: ExpTV -> Maybe ([ExpTV], ExpTV)
1057etaReds (ELam _ (App (down 0 -> Just f) (EVar 0))) = etaReds f 1142etaReds (ELam _ (App (down 0 -> Just f) (EVar 0))) = etaReds f
1058etaReds (ELam _ (hlistLam -> x@Just{})) = x 1143etaReds (ELam _ (hlistLam -> x@Just{})) = x
1059etaReds (ELam p i) = Just ([p], i) 1144etaReds (ELam p i) = Just ([p], i)
@@ -1064,6 +1149,8 @@ hlistLam (A3 "hlistNilCase" _ (down 0 -> Just x) (EVar 0)) = Just ([], x)
1064hlistLam (A3 "hlistConsCase" _ (down 0 -> Just (getPats 2 -> Just ([p, px], x))) (EVar 0)) = first (p:) <$> hlistLam x 1149hlistLam (A3 "hlistConsCase" _ (down 0 -> Just (getPats 2 -> Just ([p, px], x))) (EVar 0)) = first (p:) <$> hlistLam x
1065hlistLam _ = Nothing 1150hlistLam _ = Nothing
1066 1151
1152getPats :: (Eq a, Num a, Show a) =>
1153 a -> ExpTV -> Maybe ([ExpTV], ExpTV)
1067getPats 0 e = Just ([], e) 1154getPats 0 e = Just ([], e)
1068getPats i (ELam p e) = first (p:) <$> getPats (i-1) e 1155getPats i (ELam p e) = first (p:) <$> getPats (i-1) e
1069getPats i (Lam Hidden p (down 0 -> Just e)) = getPats i e 1156getPats i (Lam Hidden p (down 0 -> Just e)) = getPats i e
@@ -1076,12 +1163,15 @@ pattern EtaPrim4 s x1 x2 x3 <- (getEtaPrim -> Just (s, [x1, x2, x3]))
1076pattern EtaPrim5 s x1 x2 x3 x4 <- (getEtaPrim -> Just (s, [x1, x2, x3, x4])) 1163pattern EtaPrim5 s x1 x2 x3 x4 <- (getEtaPrim -> Just (s, [x1, x2, x3, x4]))
1077pattern EtaPrim2_2 s <- (getEtaPrim2 -> Just (s, [])) 1164pattern EtaPrim2_2 s <- (getEtaPrim2 -> Just (s, []))
1078 1165
1166getEtaPrim :: ExpTV -> Maybe (SName, [ExpTV])
1079getEtaPrim (ELam _ (Con s (initLast -> Just (traverse (down 0) -> Just xs, EVar 0)))) = Just (s, xs) 1167getEtaPrim (ELam _ (Con s (initLast -> Just (traverse (down 0) -> Just xs, EVar 0)))) = Just (s, xs)
1080getEtaPrim _ = Nothing 1168getEtaPrim _ = Nothing
1081 1169
1170getEtaPrim2 :: ExpTV -> Maybe (SName, [ExpTV])
1082getEtaPrim2 (ELam _ (ELam _ (Con s (initLast -> Just (initLast -> Just (traverse (down 0) -> Just (traverse (down 0) -> Just xs), EVar 0), EVar 0))))) = Just (s, xs) 1171getEtaPrim2 (ELam _ (ELam _ (Con s (initLast -> Just (initLast -> Just (traverse (down 0) -> Just (traverse (down 0) -> Just xs), EVar 0), EVar 0))))) = Just (s, xs)
1083getEtaPrim2 _ = Nothing 1172getEtaPrim2 _ = Nothing
1084 1173
1174initLast :: [b] -> Maybe ([b], b)
1085initLast [] = Nothing 1175initLast [] = Nothing
1086initLast xs = Just (init xs, last xs) 1176initLast xs = Just (init xs, last xs)
1087 1177
@@ -1117,9 +1207,11 @@ fromNat _ = Nothing
1117pattern TTuple xs <- (getTTuple -> Just xs) 1207pattern TTuple xs <- (getTTuple -> Just xs)
1118pattern ETuple xs <- (getTuple -> Just xs) 1208pattern ETuple xs <- (getTuple -> Just xs)
1119 1209
1210getTTuple :: ExpTV -> Maybe [ExpTV]
1120getTTuple (A1 "HList" l) = Just $ compList l 1211getTTuple (A1 "HList" l) = Just $ compList l
1121getTTuple _ = Nothing 1212getTTuple _ = Nothing
1122 1213
1214getTuple :: ExpTV -> Maybe [ExpTV]
1123getTuple (A0 "HNil") = Just [] 1215getTuple (A0 "HNil") = Just []
1124getTuple (A2 "HCons" x (getTuple -> Just xs)) = Just (x: xs) 1216getTuple (A2 "HCons" x (getTuple -> Just xs)) = Just (x: xs)
1125getTuple _ = Nothing 1217getTuple _ = Nothing
@@ -1308,6 +1400,14 @@ genHLSL dns e = case e of
1308 1400
1309 showSwizzProj x a = parens a <> "." <> text x 1401 showSwizzProj x a = parens a <> "." <> text x
1310 1402
1403genHLSLs :: Traversable t =>
1404 Backend
1405 -> t ExpTV
1406 -> ExpTV
1407 -> (Maybe ExpTV, ExpTV)
1408 -> (Maybe ExpTV, ExpTV)
1409 -> Maybe ExpTV
1410 -> ([[Char]], Uniforms, Doc, Doc)
1311genHLSLs backend 1411genHLSLs backend
1312 rp -- program point size 1412 rp -- program point size
1313 (ETuple ints) -- interpolations 1413 (ETuple ints) -- interpolations
@@ -1453,9 +1553,12 @@ genHLSLs backend
1453 shaderDecl a b c = shaderDecl' (a <+> b) c 1553 shaderDecl a b c = shaderDecl' (a <+> b) c
1454 shaderDecl' b c = shaderStmt $ b <+> c 1554 shaderDecl' b c = shaderStmt $ b <+> c
1455 1555
1556toHLSLType :: IsString p => [Char] -> ExpTV -> p
1456toHLSLType msg x = showHLSLType msg $ compInputType msg x 1557toHLSLType msg x = showHLSLType msg $ compInputType msg x
1457 1558
1458-- move to lambdacube-ir? 1559-- move to lambdacube-ir?
1560showHLSLType :: IsString p =>
1561 [Char] -> IR.InputType -> p
1459showHLSLType msg = \case 1562showHLSLType msg = \case
1460 IR.Bool -> "bool" 1563 IR.Bool -> "bool"
1461 IR.Word -> "uint" 1564 IR.Word -> "uint"