diff options
author | Joe Crayne <joe@jerkface.net> | 2019-04-21 15:35:01 -0400 |
---|---|---|
committer | Joe Crayne <joe@jerkface.net> | 2019-05-11 19:24:49 -0400 |
commit | e4a0905679ebb6796e09a7c45cfddb4291781cd9 (patch) | |
tree | 3dba6db336fe539e5b7f76e0ec002c67f1c428a8 | |
parent | d5056330392550ba42b245cc25b81cc649088ef2 (diff) |
Some type signatures (and build fix?).
-rw-r--r-- | src/LambdaCube/Compiler/CoreToIR.hs | 105 |
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 | |||
21 | import qualified Data.Map as Map | 21 | import qualified Data.Map as Map |
22 | import qualified Data.Set as Set | 22 | import qualified Data.Set as Set |
23 | import qualified Data.Vector as Vector | 23 | import qualified Data.Vector as Vector |
24 | import Data.String | ||
24 | import GHC.Stack | 25 | import GHC.Stack |
25 | import GHC.Word | 26 | import GHC.Word |
26 | import Control.Arrow hiding ((<+>)) | 27 | import Control.Arrow hiding ((<+>)) |
@@ -73,16 +74,39 @@ type CG = State (List IR.StreamData, Map IR.Program Int, List IR.RenderTarget, M | |||
73 | 74 | ||
74 | type List a = (Int, [a]) | 75 | type List a = (Int, [a]) |
75 | 76 | ||
77 | streamLens :: ((t1 -> (t1, t2, t3, t4, t5)) -> t6 -> t7) | ||
78 | -> (t6, t2, t3, t4, t5) -> t7 | ||
76 | streamLens f (a,b,c,d,e) = f (,b,c,d,e) a | 79 | streamLens f (a,b,c,d,e) = f (,b,c,d,e) a |
80 | programLens :: ((t1 -> (t2, t1, t3, t4, t5)) -> t6 -> t7) | ||
81 | -> (t2, t6, t3, t4, t5) -> t7 | ||
77 | programLens f (a,b,c,d,e) = f (a,,c,d,e) b | 82 | programLens f (a,b,c,d,e) = f (a,,c,d,e) b |
83 | targetLens :: ((t1 -> (t2, t3, t1, t4, t5)) -> t6 -> t7) | ||
84 | -> (t2, t3, t6, t4, t5) -> t7 | ||
78 | targetLens f (a,b,c,d,e) = f (a,b,,d,e) c | 85 | targetLens f (a,b,c,d,e) = f (a,b,,d,e) c |
86 | slotLens :: ((t1 -> (t2, t3, t4, t1, t5)) -> t6 -> t7) | ||
87 | -> (t2, t3, t4, t6, t5) -> t7 | ||
79 | slotLens f (a,b,c,d,e) = f (a,b,c,,e) d | 88 | slotLens f (a,b,c,d,e) = f (a,b,c,,e) d |
89 | textureLens :: ((t1 -> (t2, t3, t4, t5, t1)) -> t6 -> t7) | ||
90 | -> (t2, t3, t4, t5, t6) -> t7 | ||
80 | textureLens f (a,b,c,d,e) = f (a,b,c,d,) e | 91 | textureLens f (a,b,c,d,e) = f (a,b,c,d,) e |
81 | 92 | ||
93 | modL :: MonadState s m => | ||
94 | (((b -> c) -> a1 -> (d, c)) -> s -> (a2, s)) | ||
95 | -> (a1 -> (d, b)) -> m a2 | ||
82 | modL gs f = state $ gs $ \fx -> second fx . f | 96 | modL gs f = state $ gs $ \fx -> second fx . f |
83 | 97 | ||
98 | addL' :: (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 | ||
84 | addL' 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 | 102 | addL' 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 |
103 | addL :: (MonadState s m, Num a1) => | ||
104 | ((((a1, [a2]) -> c) -> (a1, [a2]) -> (a1, c)) -> s -> (a3, s)) | ||
105 | -> a2 -> m a3 | ||
85 | addL l x = modL l $ \(i, sv) -> (i, (i+1, x: sv)) | 106 | addL l x = modL l $ \(i, sv) -> (i, (i+1, x: sv)) |
107 | addLEq :: (MonadState s m, Ord k) => | ||
108 | (((Map k Int -> c) -> Map k Int -> (Int, c)) -> s -> (a, s)) | ||
109 | -> k -> m a | ||
86 | addLEq 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 | 110 | addLEq 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 | ||
278 | frameBufferType :: ExpTV -> ExpTV | ||
254 | frameBufferType (A2 "FrameBuffer" _ ty) = ty | 279 | frameBufferType (A2 "FrameBuffer" _ ty) = ty |
255 | frameBufferType x = error $ "illegal target type: " ++ ppShow x | 280 | frameBufferType x = error $ "illegal target type: " ++ ppShow x |
256 | 281 | ||
@@ -266,44 +291,55 @@ getImageTextureTypes = map (imageInputTypeTextureType . compImageInputType) . ge | |||
266 | getImageInputTypes :: ExpTV -> [IR.InputType] | 291 | getImageInputTypes :: ExpTV -> [IR.InputType] |
267 | getImageInputTypes = map compImageInputType . getFramebufferType | 292 | getImageInputTypes = map compImageInputType . getFramebufferType |
268 | 293 | ||
294 | getFragFilter :: ExpTV -> (Maybe ExpTV, ExpTV) | ||
269 | getFragFilter (A2 "map" (EtaPrim2 "filterFragment" p) x) = (Just p, x) | 295 | getFragFilter (A2 "map" (EtaPrim2 "filterFragment" p) x) = (Just p, x) |
270 | getFragFilter x = (Nothing, x) | 296 | getFragFilter x = (Nothing, x) |
271 | 297 | ||
298 | getVertexShader :: ExpTV -> ((Maybe ExpTV, Ty), ExpTV) | ||
272 | getVertexShader (A2 "map" (EtaPrim2 "mapPrimitive" f@(etaReds -> Just (_, o))) x) = ((Just f, tyOf o), x) | 299 | getVertexShader (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 |
275 | getVertexShader x = ((Nothing, getPrim' $ tyOf x), x) | 302 | getVertexShader x = ((Nothing, getPrim' $ tyOf x), x) |
276 | 303 | ||
304 | getFragmentShader :: ExpTV -> ((Maybe ExpTV, Ty), ExpTV) | ||
277 | getFragmentShader (A2 "map" (EtaPrim2 "mapFragment" f@(etaReds -> Just (_, o))) x) = ((Just f, tyOf o), x) | 305 | getFragmentShader (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 |
280 | getFragmentShader x = ((Nothing, getPrim'' $ tyOf x), x) | 308 | getFragmentShader x = ((Nothing, getPrim'' $ tyOf x), x) |
281 | 309 | ||
310 | getPrim :: ExpTV -> ExpTV | ||
282 | getPrim (A1 "List" (A2 "Primitive" _ p)) = p | 311 | getPrim (A1 "List" (A2 "Primitive" _ p)) = p |
312 | getPrim' :: ExpTV -> ExpTV | ||
283 | getPrim' (A1 "List" (A2 "Primitive" a _)) = a | 313 | getPrim' (A1 "List" (A2 "Primitive" a _)) = a |
314 | getPrim'' :: ExpTV -> ExpTV | ||
284 | getPrim'' (A1 "List" (A2 "Vector" _ (A1 "Maybe" (A1 "SimpleFragment" (TTuple [a]))))) = a | 315 | getPrim'' (A1 "List" (A2 "Vector" _ (A1 "Maybe" (A1 "SimpleFragment" (TTuple [a]))))) = a |
285 | getPrim'' x = error $ "getPrim'':" ++ ppShow x | 316 | getPrim'' x = error $ "getPrim'':" ++ ppShow x |
286 | 317 | ||
318 | compFrameBuffer :: ExpTV -> IR.ClearImage | ||
287 | compFrameBuffer = \case | 319 | compFrameBuffer = \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 | ||
324 | compList :: ExpTV -> [ExpTV] | ||
292 | compList (A2 ":" a x) = a : compList x | 325 | compList (A2 ":" a x) = a : compList x |
293 | compList (A0 "Nil") = [] | 326 | compList (A0 "Nil") = [] |
294 | compList x = error $ "compList: " ++ ppShow x | 327 | compList x = error $ "compList: " ++ ppShow x |
295 | 328 | ||
329 | compFilter :: ExpTV -> IR.Filter | ||
296 | compFilter = \case | 330 | compFilter = \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 | ||
335 | compEdgeMode :: ExpTV -> IR.EdgeMode | ||
301 | compEdgeMode = \case | 336 | compEdgeMode = \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 | ||
342 | compSemantic :: ExpTV -> IR.ImageSemantic | ||
307 | compSemantic = \case | 343 | compSemantic = \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 | ||
377 | compAC :: ExpTV -> IR.AccumulationContext | ||
341 | compAC (ETuple x) = IR.AccumulationContext Nothing $ map compFrag x | 378 | compAC (ETuple x) = IR.AccumulationContext Nothing $ map compFrag x |
342 | 379 | ||
380 | compBlending :: ExpTV -> IR.Blending | ||
343 | compBlending x = case x of | 381 | compBlending 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 | ||
387 | compBF :: ExpTV -> IR.BlendingFactor | ||
349 | compBF x = case x of | 388 | compBF 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 | ||
406 | compBE :: ExpTV -> IR.BlendEquation | ||
367 | compBE x = case x of | 407 | compBE 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 | ||
415 | compLO :: ExpTV -> IR.LogicOperation | ||
375 | compLO x = case x of | 416 | compLO 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 | ||
435 | compComparisonFunction :: ExpTV -> IR.ComparisonFunction | ||
394 | compComparisonFunction x = case x of | 436 | compComparisonFunction 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 | ||
405 | pattern EBool a <- (compBool -> Just a) | 447 | pattern EBool a <- (compBool -> Just a) |
406 | 448 | ||
449 | compBool :: ExpTV -> Maybe Bool | ||
407 | compBool x = case x of | 450 | compBool 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 | ||
455 | compFrag :: ExpTV -> IR.FragmentOperation | ||
412 | compFrag x = case x of | 456 | compFrag 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 | ||
461 | toGLSLType :: IsString p => [Char] -> ExpTV -> p | ||
417 | toGLSLType msg x = showGLSLType msg $ compInputType msg x | 462 | toGLSLType msg x = showGLSLType msg $ compInputType msg x |
418 | 463 | ||
419 | -- move to lambdacube-ir? | 464 | -- move to lambdacube-ir? |
465 | showGLSLType :: IsString p => | ||
466 | [Char] -> IR.InputType -> p | ||
420 | showGLSLType msg = \case | 467 | showGLSLType 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 | ||
496 | supType :: ExpTV -> Bool | ||
449 | supType = isJust . compInputType_ | 497 | supType = isJust . compInputType_ |
450 | 498 | ||
451 | compInputType_ :: ExpTV -> Maybe IR.InputType | 499 | compInputType_ :: 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 | ||
528 | compInputType :: [Char] -> ExpTV -> IR.InputType | ||
480 | compInputType msg x = fromMaybe (error $ "compInputType " ++ msg ++ " " ++ ppShow x) $ compInputType_ x | 529 | compInputType msg x = fromMaybe (error $ "compInputType " ++ msg ++ " " ++ ppShow x) $ compInputType_ x |
481 | 530 | ||
531 | is234 :: Integer -> Bool | ||
482 | is234 = (`elem` [2,3,4]) | 532 | is234 = (`elem` [2,3,4]) |
483 | 533 | ||
484 | compInputType'' (ETuple attrs) = map compAttribute attrs | 534 | compInputType'' :: ExpTV -> [(String, IR.InputType)] |
535 | compInputType'' (ETuple attrs) = map compAttribute attrs -- pattern fail. | ||
485 | 536 | ||
537 | compAttribute :: ExpTV -> (String, IR.InputType) | ||
486 | compAttribute = \case | 538 | compAttribute = \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 | ||
565 | compFetchPrimitive :: ExpTV -> IR.FetchPrimitive | ||
513 | compFetchPrimitive x = case x of | 566 | compFetchPrimitive 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 | ||
589 | compRC :: ExpTV -> IR.RasterContext | ||
536 | compRC x = case x of | 590 | compRC 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 | ||
596 | compRC' :: ExpTV -> Maybe ExpTV | ||
542 | compRC' x = case x of | 597 | compRC' 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 | ||
602 | compPSCO :: ExpTV -> IR.PointSpriteCoordOrigin | ||
547 | compPSCO x = case x of | 603 | compPSCO 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 | ||
608 | compCM :: ExpTV -> IR.CullMode | ||
552 | compCM x = case x of | 609 | compCM 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 | ||
615 | compPM :: ExpTV -> IR.PolygonMode | ||
558 | compPM x = case x of | 616 | compPM 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 | ||
622 | compPM' :: ExpTV -> Maybe ExpTV | ||
564 | compPM' x = case x of | 623 | compPM' x = case x of |
565 | A1 "PolygonPoint" a -> compPS' a | 624 | A1 "PolygonPoint" a -> compPS' a |
566 | x -> Nothing | 625 | x -> Nothing |
567 | 626 | ||
627 | compPS :: ExpTV -> IR.PointSize | ||
568 | compPS x = case x of | 628 | compPS 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 | ||
633 | compPS' :: ExpTV -> Maybe ExpTV | ||
573 | compPS' x = case x of | 634 | compPS' x = case x of |
574 | A1 "ProgramPointSize" x -> Just x | 635 | A1 "ProgramPointSize" x -> Just x |
575 | x -> Nothing | 636 | x -> Nothing |
576 | 637 | ||
638 | compPO :: ExpTV -> IR.PolygonOffset | ||
577 | compPO x = case x of | 639 | compPO 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 | ||
644 | compPV :: ExpTV -> IR.ProvokingVertex | ||
582 | compPV x = case x of | 645 | compPV 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 | ||
742 | type Uniforms = Map String (Uniform, IR.InputType) | 805 | type Uniforms = Map String (Uniform, IR.InputType) |
743 | 806 | ||
807 | tellUniform :: (MonadWriter (a, b) m, Monoid b) => a -> m () | ||
744 | tellUniform x = tell (x, mempty) | 808 | tellUniform x = tell (x, mempty) |
745 | 809 | ||
810 | simpleExpr :: ExpTV -> Bool | ||
746 | simpleExpr = \case | 811 | simpleExpr = \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 | |||
944 | tyOf :: ExpTV -> Ty | 1009 | tyOf :: ExpTV -> Ty |
945 | tyOf (ExpTV _ t vs) = t .@ vs | 1010 | tyOf (ExpTV _ t vs) = t .@ vs |
946 | 1011 | ||
1012 | expOf :: ExpTV -> I.Exp | ||
947 | expOf (ExpTV x _ _) = x | 1013 | expOf (ExpTV x _ _) = x |
948 | 1014 | ||
1015 | mapVal :: (I.Exp -> I.Exp) -> ExpTV -> ExpTV | ||
949 | mapVal f (ExpTV a b c) = ExpTV (f a) b c | 1016 | mapVal f (ExpTV a b c) = ExpTV (f a) b c |
950 | 1017 | ||
951 | toExp :: I.ExpType -> ExpTV | 1018 | toExp :: I.ExpType -> ExpTV |
@@ -964,18 +1031,23 @@ pattern EString s <- ELit (LString s) | |||
964 | pattern EFloat s <- ELit (LFloat s) | 1031 | pattern EFloat s <- ELit (LFloat s) |
965 | pattern EInt s <- ELit (LInt s) | 1032 | pattern EInt s <- ELit (LInt s) |
966 | 1033 | ||
1034 | (.@) :: I.Exp -> [I.Exp] -> ExpTV | ||
967 | t .@ vs = ExpTV t I.TType vs | 1035 | t .@ vs = ExpTV t I.TType vs |
968 | infix 1 .@ | 1036 | infix 1 .@ |
969 | 1037 | ||
1038 | mkVar :: ExpTV -> Maybe (Int, ExpTV) | ||
970 | mkVar (ExpTV (I.Var i) t vs) = Just (i, t .@ vs) | 1039 | mkVar (ExpTV (I.Var i) t vs) = Just (i, t .@ vs) |
971 | mkVar _ = Nothing | 1040 | mkVar _ = Nothing |
972 | 1041 | ||
1042 | mkPi :: ExpTV -> Maybe (Visibility, ExpTV, ExpTV) | ||
973 | mkPi (ExpTV (I.Pi b x y) _ vs) = Just (b, x .@ vs, y .@ addToEnv x vs) | 1043 | mkPi (ExpTV (I.Pi b x y) _ vs) = Just (b, x .@ vs, y .@ addToEnv x vs) |
974 | mkPi _ = Nothing | 1044 | mkPi _ = Nothing |
975 | 1045 | ||
1046 | mkLam :: ExpTV -> Maybe (Visibility, ExpTV, ExpTV) | ||
976 | mkLam (ExpTV (I.Lam y) (I.Pi b x yt) vs) = Just (b, x .@ vs, ExpTV y yt $ addToEnv x vs) | 1047 | mkLam (ExpTV (I.Lam y) (I.Pi b x yt) vs) = Just (b, x .@ vs, ExpTV y yt $ addToEnv x vs) |
977 | mkLam _ = Nothing | 1048 | mkLam _ = Nothing |
978 | 1049 | ||
1050 | mkCon :: ExpTV -> Maybe (SName, [ExpTV]) | ||
979 | mkCon (ExpTV (I.Con s n (reverse -> xs)) et vs) = Just (untick $ show s, chain vs (I.conType et s) $ I.mkConPars n et ++ xs) | 1051 | mkCon (ExpTV (I.Con s n (reverse -> xs)) et vs) = Just (untick $ show s, chain vs (I.conType et s) $ I.mkConPars n et ++ xs) |
980 | mkCon (ExpTV (I.TyCon s (reverse -> xs)) et vs) = Just (untick $ show s, chain vs (nType s) xs) | 1052 | mkCon (ExpTV (I.TyCon s (reverse -> xs)) et vs) = Just (untick $ show s, chain vs (nType s) xs) |
981 | mkCon (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) | 1053 | mkCon (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 | |||
983 | mkCon (ExpTV (I.TyCaseFun s [m, t, f] n) et vs) = Just (untick $ show s, chain vs (nType s) [m, t, I.Neut n, f]) | 1055 | mkCon (ExpTV (I.TyCaseFun s [m, t, f] n) et vs) = Just (untick $ show s, chain vs (nType s) [m, t, I.Neut n, f]) |
984 | mkCon _ = Nothing | 1056 | mkCon _ = Nothing |
985 | 1057 | ||
1058 | mkApp :: ExpTV -> Maybe (ExpTV, ExpTV) | ||
986 | mkApp (ExpTV (I.Neut (I.App_ a b)) et vs) = Just (ExpTV (I.Neut a) t vs, head $ chain vs t [b]) | 1059 | mkApp (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 |
988 | mkApp _ = Nothing | 1061 | mkApp _ = Nothing |
989 | 1062 | ||
1063 | removeRHS :: (Num t, Ord t) => t -> I.Exp -> Maybe I.Exp | ||
990 | removeRHS 0 (I.RHS x) = Just x | 1064 | removeRHS 0 (I.RHS x) = Just x |
991 | removeRHS n (I.Lam x) | n > 0 = I.Lam <$> removeRHS (n-1) x | 1065 | removeRHS n (I.Lam x) | n > 0 = I.Lam <$> removeRHS (n-1) x |
992 | removeRHS _ _ = Nothing | 1066 | removeRHS _ _ = Nothing |
993 | 1067 | ||
1068 | mkFunc :: ExpTV -> Maybe ([Char], ExpTV, Ty, [ExpTV]) | ||
994 | mkFunc r@(ExpTV (I.Neut (I.Fun (I.FunName (show -> n) loc (I.ExpDef def_) nt) xs I.RHS{})) ty vs) | 1069 | mkFunc 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 | -} |
1016 | mkFunc _ = Nothing | 1091 | mkFunc _ = Nothing |
1017 | 1092 | ||
1093 | chain :: [I.Exp] -> I.Exp -> [I.Exp] -> [ExpTV] | ||
1018 | chain vs t@(I.Pi Hidden at y) (a: as) = chain vs (I.appTy t a) as | 1094 | chain vs t@(I.Pi Hidden at y) (a: as) = chain vs (I.appTy t a) as |
1019 | chain vs t xs = map snd $ chain' vs t xs | 1095 | chain vs t xs = map snd $ chain' vs t xs |
1020 | 1096 | ||
1097 | chain' :: [I.Exp] -> I.Exp -> [I.Exp] -> [(Visibility, ExpTV)] | ||
1021 | chain' vs t [] = [] | 1098 | chain' vs t [] = [] |
1022 | chain' vs t@(I.Pi b at y) (a: as) = (b, ExpTV a at vs): chain' vs (I.appTy t a) as | 1099 | chain' vs t@(I.Pi b at y) (a: as) = (b, ExpTV a at vs): chain' vs (I.appTy t a) as |
1023 | chain' vs t _ = error $ "chain: " ++ ppShow t | 1100 | chain' vs t _ = error $ "chain: " ++ ppShow t |
1024 | 1101 | ||
1102 | mkTVar :: Int -> ExpTV -> ExpTV | ||
1025 | mkTVar i (ExpTV t _ vs) = ExpTV (I.Var i) t vs | 1103 | mkTVar i (ExpTV t _ vs) = ExpTV (I.Var i) t vs |
1026 | 1104 | ||
1105 | unLab' :: I.Exp -> I.Exp | ||
1027 | unLab' (I.Reduced x) = unLab' x | 1106 | unLab' (I.Reduced x) = unLab' x |
1028 | unLab' (I.RHS x) = unLab' x -- TODO: remove | 1107 | unLab' (I.RHS x) = unLab' x -- TODO: remove |
1029 | unLab' x = x | 1108 | unLab' x = x |
1030 | 1109 | ||
1110 | unFunc' :: I.Exp -> I.Exp | ||
1031 | unFunc' (I.Reduced x) = unFunc' x -- todo: remove? | 1111 | unFunc' (I.Reduced x) = unFunc' x -- todo: remove? |
1032 | unFunc' (I.Neut (I.Fun (I.FunName _ _ I.ExpDef{} _) _ y)) = unFunc' y | 1112 | unFunc' (I.Neut (I.Fun (I.FunName _ _ I.ExpDef{} _) _ y)) = unFunc' y |
1033 | unFunc' (I.RHS x) = unFunc' x -- TODO: remove | 1113 | unFunc' (I.RHS x) = unFunc' x -- TODO: remove |
@@ -1036,7 +1116,9 @@ unFunc' x = x | |||
1036 | instance Subst I.Exp ExpTV where | 1116 | instance 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 | ||
1119 | addToEnv :: a -> [a] -> [a] | ||
1039 | addToEnv x xs = x: xs | 1120 | addToEnv x xs = x: xs |
1121 | mkEnv :: Rearrange c => [c] -> [c] | ||
1040 | mkEnv xs = {-trace_ ("mk " ++ show (length xs)) $ -} zipWith up [1..] xs | 1122 | mkEnv xs = {-trace_ ("mk " ++ show (length xs)) $ -} zipWith up [1..] xs |
1041 | 1123 | ||
1042 | instance HasFreeVars ExpTV where | 1124 | instance HasFreeVars ExpTV where |
@@ -1045,15 +1127,18 @@ instance HasFreeVars ExpTV where | |||
1045 | instance PShow ExpTV where | 1127 | instance PShow ExpTV where |
1046 | pShow (ExpTV x t _) = pShow (x, t) | 1128 | pShow (ExpTV x t _) = pShow (x, t) |
1047 | 1129 | ||
1130 | isSampler :: I.Exp -> Bool | ||
1048 | isSampler (I.TyCon n _) = show n == "'Sampler" | 1131 | isSampler (I.TyCon n _) = show n == "'Sampler" |
1049 | isSampler _ = False | 1132 | isSampler _ = False |
1050 | 1133 | ||
1051 | -------------------------------------------------------------------------------- ExpTV conversion -- TODO: remove | 1134 | -------------------------------------------------------------------------------- ExpTV conversion -- TODO: remove |
1052 | 1135 | ||
1136 | removeLams :: (Eq t, Num t) => t -> ExpTV -> ExpTV | ||
1053 | removeLams 0 x = x | 1137 | removeLams 0 x = x |
1054 | removeLams i (ELam _ x) = removeLams (i-1) x | 1138 | removeLams i (ELam _ x) = removeLams (i-1) x |
1055 | removeLams i (Lam Hidden _ x) = removeLams i x | 1139 | removeLams i (Lam Hidden _ x) = removeLams i x |
1056 | 1140 | ||
1141 | etaReds :: ExpTV -> Maybe ([ExpTV], ExpTV) | ||
1057 | etaReds (ELam _ (App (down 0 -> Just f) (EVar 0))) = etaReds f | 1142 | etaReds (ELam _ (App (down 0 -> Just f) (EVar 0))) = etaReds f |
1058 | etaReds (ELam _ (hlistLam -> x@Just{})) = x | 1143 | etaReds (ELam _ (hlistLam -> x@Just{})) = x |
1059 | etaReds (ELam p i) = Just ([p], i) | 1144 | etaReds (ELam p i) = Just ([p], i) |
@@ -1064,6 +1149,8 @@ hlistLam (A3 "hlistNilCase" _ (down 0 -> Just x) (EVar 0)) = Just ([], x) | |||
1064 | hlistLam (A3 "hlistConsCase" _ (down 0 -> Just (getPats 2 -> Just ([p, px], x))) (EVar 0)) = first (p:) <$> hlistLam x | 1149 | hlistLam (A3 "hlistConsCase" _ (down 0 -> Just (getPats 2 -> Just ([p, px], x))) (EVar 0)) = first (p:) <$> hlistLam x |
1065 | hlistLam _ = Nothing | 1150 | hlistLam _ = Nothing |
1066 | 1151 | ||
1152 | getPats :: (Eq a, Num a, Show a) => | ||
1153 | a -> ExpTV -> Maybe ([ExpTV], ExpTV) | ||
1067 | getPats 0 e = Just ([], e) | 1154 | getPats 0 e = Just ([], e) |
1068 | getPats i (ELam p e) = first (p:) <$> getPats (i-1) e | 1155 | getPats i (ELam p e) = first (p:) <$> getPats (i-1) e |
1069 | getPats i (Lam Hidden p (down 0 -> Just e)) = getPats i e | 1156 | getPats 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])) | |||
1076 | pattern EtaPrim5 s x1 x2 x3 x4 <- (getEtaPrim -> Just (s, [x1, x2, x3, x4])) | 1163 | pattern EtaPrim5 s x1 x2 x3 x4 <- (getEtaPrim -> Just (s, [x1, x2, x3, x4])) |
1077 | pattern EtaPrim2_2 s <- (getEtaPrim2 -> Just (s, [])) | 1164 | pattern EtaPrim2_2 s <- (getEtaPrim2 -> Just (s, [])) |
1078 | 1165 | ||
1166 | getEtaPrim :: ExpTV -> Maybe (SName, [ExpTV]) | ||
1079 | getEtaPrim (ELam _ (Con s (initLast -> Just (traverse (down 0) -> Just xs, EVar 0)))) = Just (s, xs) | 1167 | getEtaPrim (ELam _ (Con s (initLast -> Just (traverse (down 0) -> Just xs, EVar 0)))) = Just (s, xs) |
1080 | getEtaPrim _ = Nothing | 1168 | getEtaPrim _ = Nothing |
1081 | 1169 | ||
1170 | getEtaPrim2 :: ExpTV -> Maybe (SName, [ExpTV]) | ||
1082 | getEtaPrim2 (ELam _ (ELam _ (Con s (initLast -> Just (initLast -> Just (traverse (down 0) -> Just (traverse (down 0) -> Just xs), EVar 0), EVar 0))))) = Just (s, xs) | 1171 | getEtaPrim2 (ELam _ (ELam _ (Con s (initLast -> Just (initLast -> Just (traverse (down 0) -> Just (traverse (down 0) -> Just xs), EVar 0), EVar 0))))) = Just (s, xs) |
1083 | getEtaPrim2 _ = Nothing | 1172 | getEtaPrim2 _ = Nothing |
1084 | 1173 | ||
1174 | initLast :: [b] -> Maybe ([b], b) | ||
1085 | initLast [] = Nothing | 1175 | initLast [] = Nothing |
1086 | initLast xs = Just (init xs, last xs) | 1176 | initLast xs = Just (init xs, last xs) |
1087 | 1177 | ||
@@ -1117,9 +1207,11 @@ fromNat _ = Nothing | |||
1117 | pattern TTuple xs <- (getTTuple -> Just xs) | 1207 | pattern TTuple xs <- (getTTuple -> Just xs) |
1118 | pattern ETuple xs <- (getTuple -> Just xs) | 1208 | pattern ETuple xs <- (getTuple -> Just xs) |
1119 | 1209 | ||
1210 | getTTuple :: ExpTV -> Maybe [ExpTV] | ||
1120 | getTTuple (A1 "HList" l) = Just $ compList l | 1211 | getTTuple (A1 "HList" l) = Just $ compList l |
1121 | getTTuple _ = Nothing | 1212 | getTTuple _ = Nothing |
1122 | 1213 | ||
1214 | getTuple :: ExpTV -> Maybe [ExpTV] | ||
1123 | getTuple (A0 "HNil") = Just [] | 1215 | getTuple (A0 "HNil") = Just [] |
1124 | getTuple (A2 "HCons" x (getTuple -> Just xs)) = Just (x: xs) | 1216 | getTuple (A2 "HCons" x (getTuple -> Just xs)) = Just (x: xs) |
1125 | getTuple _ = Nothing | 1217 | getTuple _ = 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 | ||
1403 | genHLSLs :: 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) | ||
1311 | genHLSLs backend | 1411 | genHLSLs 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 | ||
1556 | toHLSLType :: IsString p => [Char] -> ExpTV -> p | ||
1456 | toHLSLType msg x = showHLSLType msg $ compInputType msg x | 1557 | toHLSLType msg x = showHLSLType msg $ compInputType msg x |
1457 | 1558 | ||
1458 | -- move to lambdacube-ir? | 1559 | -- move to lambdacube-ir? |
1560 | showHLSLType :: IsString p => | ||
1561 | [Char] -> IR.InputType -> p | ||
1459 | showHLSLType msg = \case | 1562 | showHLSLType msg = \case |
1460 | IR.Bool -> "bool" | 1563 | IR.Bool -> "bool" |
1461 | IR.Word -> "uint" | 1564 | IR.Word -> "uint" |