summaryrefslogtreecommitdiff
path: root/lc
diff options
context:
space:
mode:
Diffstat (limited to 'lc')
-rw-r--r--lc/Builtins.lc572
-rw-r--r--lc/Internals.lc62
-rw-r--r--lc/Prelude.lc362
3 files changed, 996 insertions, 0 deletions
diff --git a/lc/Builtins.lc b/lc/Builtins.lc
new file mode 100644
index 00000000..674335cb
--- /dev/null
+++ b/lc/Builtins.lc
@@ -0,0 +1,572 @@
1{-# LANGUAGE NoImplicitPrelude #-}
2module Builtins
3 ( module Internals
4 , module Builtins
5 ) where
6
7import Internals
8
9id x = x
10
11---------------------------------------
12
13data Nat = Zero | Succ Nat
14
15data List a = Nil | Cons a (List a)
16
17type family JoinTupleType t1 t2 where
18 -- TODO
19 JoinTupleType () a = a
20 JoinTupleType a () = a
21 JoinTupleType (a, b) c = (a, b, c)
22 JoinTupleType a (b, c) = (a, b, c)
23 JoinTupleType a (b, c, d) = (a, b, c, d)
24 JoinTupleType a (b, c, d, e) = (a, b, c, d, e)
25 JoinTupleType a b = (a, b)
26
27class AttributeTuple a
28instance AttributeTuple a -- TODO
29class ValidOutput a
30instance ValidOutput a -- TODO
31class ValidFrameBuffer a
32instance ValidFrameBuffer a -- TODO
33
34data VecS (a :: Type) :: Nat -> Type where
35 V2 :: a -> a -> VecS a 2
36 V3 :: a -> a -> a -> VecS a 3
37 V4 :: a -> a -> a -> a -> VecS a 4
38
39type family Vec (n :: Nat) t where Vec n t = VecS t n
40
41type family VecScalar (n :: Nat) a where
42 VecScalar 1 a = a
43 VecScalar ('Succ ('Succ n)) a = Vec ('Succ ('Succ n)) a
44
45-- may be a data family?
46type family TFVec (n :: Nat) a where
47 TFVec n a = Vec n a -- TODO: check range: n = 2,3,4; a is Float, Int, Word, Bool
48
49-- todo: use less constructors with more parameters
50data Mat :: Nat -> Nat -> Type -> Type where
51 M22F :: Vec 2 Float -> Vec 2 Float -> Mat 2 2 Float
52 M32F :: Vec 3 Float -> Vec 3 Float -> Mat 3 2 Float
53 M42F :: Vec 4 Float -> Vec 4 Float -> Mat 4 2 Float
54 M23F :: Vec 2 Float -> Vec 2 Float -> Vec 2 Float -> Mat 2 3 Float
55 M33F :: Vec 3 Float -> Vec 3 Float -> Vec 3 Float -> Mat 3 3 Float
56 M43F :: Vec 4 Float -> Vec 4 Float -> Vec 4 Float -> Mat 4 3 Float
57 M24F :: Vec 2 Float -> Vec 2 Float -> Vec 2 Float -> Vec 2 Float -> Mat 2 4 Float
58 M34F :: Vec 3 Float -> Vec 3 Float -> Vec 3 Float -> Vec 3 Float -> Mat 3 4 Float
59 M44F :: Vec 4 Float -> Vec 4 Float -> Vec 4 Float -> Vec 4 Float -> Mat 4 4 Float
60
61type family MatVecElem a where
62 MatVecElem (VecS a n) = a
63 MatVecElem (Mat i j a) = a
64
65type family MatVecScalarElem a where
66 MatVecScalarElem Float = Float
67 MatVecScalarElem Bool = Bool
68 MatVecScalarElem Int = Int
69 MatVecScalarElem (VecS a n) = a
70 MatVecScalarElem (Mat i j a) = a
71
72-- may be a data family?
73type family TFMat i j where
74 TFMat (VecS a i) (VecS a' j) = T2 (a ~ a') (Mat i j a)
75
76--------------------------------------- swizzling
77
78data Swizz = Sx | Sy | Sz | Sw
79
80-- todo: use pattern matching
81mapVec :: forall a b m . (a -> b) -> Vec m a -> Vec m b
82mapVec @a @b @m f v = 'VecSCase (\m _ -> 'Vec m b)
83 (\x y -> V2 (f x) (f y))
84 (\x y z -> V3 (f x) (f y) (f z))
85 (\x y z w -> V4 (f x) (f y) (f z) (f w))
86 @m
87 v
88
89-- todo: make it more type safe
90swizzscalar :: forall n . Vec n a -> Swizz -> a
91swizzscalar (V2 x y) Sx = x
92swizzscalar (V2 x y) Sy = y
93swizzscalar (V3 x y z) Sx = x
94swizzscalar (V3 x y z) Sy = y
95swizzscalar (V3 x y z) Sz = z
96swizzscalar (V4 x y z w) Sx = x
97swizzscalar (V4 x y z w) Sy = y
98swizzscalar (V4 x y z w) Sz = z
99swizzscalar (V4 x y z w) Sw = w
100
101-- used to prevent unfolding of swizzvector on variables (behind GPU lambda)
102definedVec :: forall a m . Vec m a -> Bool
103definedVec (V2 _ _) = True
104definedVec (V3 _ _ _) = True
105definedVec (V4 _ _ _ _) = True
106
107swizzvector :: forall n . forall m . Vec n a -> Vec m Swizz -> Vec m a
108swizzvector v w | definedVec v = mapVec (swizzscalar v) w
109
110
111--------------------------------------- type classes
112
113class CNum a
114
115instance CNum Int
116instance CNum Float
117
118class Signed a
119
120instance Signed Int
121instance Signed Float
122
123class Num a where
124 fromInt :: Int -> a
125 compare :: a -> a -> Ordering
126 negate :: a -> a
127
128instance Num Int where
129 fromInt = id
130 compare = primCompareInt
131 negate = primNegateInt
132instance Num Word where
133 fromInt = primIntToWord
134 compare = primCompareWord
135 negate = primNegateWord
136instance Num Float where
137 fromInt = primIntToFloat
138 compare = primCompareFloat
139 negate = primNegateFloat
140
141class Component a where
142 vec2 :: a -> a -> Vec 2 a
143 vec3 :: a -> a -> a -> Vec 3 a
144 vec4 :: a -> a -> a -> a -> Vec 4 a
145 zeroComp :: a
146 oneComp :: a
147-- PrimZero, PrimOne :: {- (Component a) => -- TODO -} a
148
149instance Component Bool where
150 vec2 = V2
151 vec3 = V3
152 vec4 = V4
153 zeroComp = False
154 oneComp = True
155instance Component Int where
156 vec2 = V2
157 vec3 = V3
158 vec4 = V4
159 zeroComp = 0 :: Int -- todo
160 oneComp = 1 :: Int -- todo
161
162instance Component Word where
163 vec2 = V2
164 vec3 = V3
165 vec4 = V4
166 zeroComp = 0 :: Word
167 oneComp = 1 :: Word
168
169instance Component Float where
170 vec2 = V2
171 vec3 = V3
172 vec4 = V4
173 zeroComp = 0.0 -- todo: 0
174 oneComp = 1.0 -- todo: 1
175instance Component (VecS Float 2) where
176 vec2 = V2
177 vec3 = V3
178 vec4 = V4
179 zeroComp = V2 0.0 0.0
180 oneComp = V2 1.0 1.0
181instance Component (VecS Float 3) where
182 vec2 = V2
183 vec3 = V3
184 vec4 = V4
185 zeroComp = V3 0.0 0.0 0.0
186 oneComp = V3 1.0 1.0 1.0
187instance Component (VecS Float 4) where
188 vec2 = V2
189 vec3 = V3
190 vec4 = V4
191 zeroComp = V4 0.0 0.0 0.0 0.0
192 oneComp = V4 1.0 1.0 1.0 1.0
193instance Component (VecS Bool 2) {-where-}
194instance Component (VecS Bool 3) {-where-}
195instance Component (VecS Bool 4) where
196 vec2 = V2
197 vec3 = V3
198 vec4 = V4
199 zeroComp = V4 False False False False
200 oneComp = V4 True True True True
201
202class Integral a
203
204instance Integral Int
205instance Integral Word
206
207class NumComponent a
208
209instance NumComponent Int
210instance NumComponent Word
211instance NumComponent Float
212instance NumComponent (VecS Float 2)
213instance NumComponent (VecS Float 3)
214instance NumComponent (VecS Float 4)
215
216class Floating a
217
218instance Floating Float
219instance Floating (VecS Float 2) -- todo: use Vec
220instance Floating (VecS Float 3)
221instance Floating (VecS Float 4)
222instance Floating (Mat 2 2 Float)
223instance Floating (Mat 2 3 Float)
224instance Floating (Mat 2 4 Float)
225instance Floating (Mat 3 2 Float)
226instance Floating (Mat 3 3 Float)
227instance Floating (Mat 3 4 Float)
228instance Floating (Mat 4 2 Float)
229instance Floating (Mat 4 3 Float)
230instance Floating (Mat 4 4 Float)
231
232data BlendingFactor
233 = Zero' --- FIXME: modified
234 | One
235 | SrcColor
236 | OneMinusSrcColor
237 | DstColor
238 | OneMinusDstColor
239 | SrcAlpha
240 | OneMinusSrcAlpha
241 | DstAlpha
242 | OneMinusDstAlpha
243 | ConstantColor
244 | OneMinusConstantColor
245 | ConstantAlpha
246 | OneMinusConstantAlpha
247 | SrcAlphaSaturate
248
249data BlendEquation
250 = FuncAdd
251 | FuncSubtract
252 | FuncReverseSubtract
253 | Min
254 | Max
255
256data LogicOperation
257 = Clear
258 | And
259 | AndReverse
260 | Copy
261 | AndInverted
262 | Noop
263 | Xor
264 | Or
265 | Nor
266 | Equiv
267 | Invert
268 | OrReverse
269 | CopyInverted
270 | OrInverted
271 | Nand
272 | Set
273
274data StencilOperation
275 = OpZero
276 | OpKeep
277 | OpReplace
278 | OpIncr
279 | OpIncrWrap
280 | OpDecr
281 | OpDecrWrap
282 | OpInvert
283
284data ComparisonFunction
285 = Never
286 | Less
287 | Equal
288 | Lequal
289 | Greater
290 | Notequal
291 | Gequal
292 | Always
293
294data ProvokingVertex
295 = LastVertex
296 | FirstVertex
297
298data FrontFace
299 = CW
300 | CCW
301
302data CullMode
303 = CullFront FrontFace
304 | CullBack FrontFace
305 | CullNone
306
307data PointSize
308 = PointSize Float
309 | ProgramPointSize
310
311data PolygonMode
312 = PolygonFill
313 | PolygonPoint PointSize
314 | PolygonLine Float
315
316data PolygonOffset
317 = NoOffset
318 | Offset Float Float
319
320data PointSpriteCoordOrigin
321 = LowerLeft
322 | UpperLeft
323
324
325data Depth a where
326data Stencil a where
327data Color a where
328
329type family ColorRepr a where
330 ColorRepr () = ()
331 ColorRepr (a, b) = (Color a, Color b)
332 ColorRepr (a, b, c) = (Color a, Color b, Color c)
333 ColorRepr (a, b, c, d) = (Color a, Color b, Color c, Color d)
334 ColorRepr (a, b, c, d, e) = (Color a, Color b, Color c, Color d, Color e)
335 ColorRepr a = Color a -- TODO
336
337data PrimitiveType
338 = Triangle
339 | Line
340 | Point
341 | TriangleAdjacency
342 | LineAdjacency
343
344-- builtin
345primTexture :: () -> Vec 2 Float -> Vec 4 Float
346
347-- builtins
348Uniform :: String -> t
349Attribute :: String -> t
350
351data FragmentShader :: Type -> Type where
352 FragmentShader :: (a ~ ColorRepr t) => (b -> t) -> FragmentShader (b -> a)
353 FragmentShaderDepth :: (x ~ ColorRepr t, a ~ JoinTupleType (Depth Float) x) => (b -> (Float, t))
354 -> FragmentShader (b -> a)
355 FragmentShaderRastDepth :: (x ~ ColorRepr t, a ~ JoinTupleType (Depth Float) x) => (b -> t)
356 -> FragmentShader (b -> a)
357
358data RasterContext :: PrimitiveType -> Type where
359 TriangleCtx :: CullMode -> PolygonMode -> PolygonOffset -> ProvokingVertex -> RasterContext Triangle
360 PointCtx :: PointSize -> Float -> PointSpriteCoordOrigin -> RasterContext Point
361 LineCtx :: Float -> ProvokingVertex -> RasterContext Line
362
363data Interpolated t where
364 Smooth, NoPerspective
365 :: (Floating t) => t -> Interpolated t
366 Flat :: t -> Interpolated t
367
368type family FTRepr' a where
369 -- TODO
370 FTRepr' [a] = a
371 FTRepr' ([a], [b]) = (a, b)
372 FTRepr' (Interpolated a) = a
373 FTRepr' (Interpolated a, Interpolated b) = (a, b)
374 FTRepr' (Interpolated a, Interpolated b, Interpolated c) = (a, b, c)
375
376data VertexOut a where
377 VertexOut :: (a ~ FTRepr' x) => Vec 4 Float -> Float -> (){-TODO-} -> x -> VertexOut a
378
379data Blending :: Type -> Type where
380 NoBlending :: Blending t
381 BlendLogicOp :: (Integral t) => LogicOperation -> Blending t
382 Blend :: (BlendEquation, BlendEquation)
383 -> ((BlendingFactor, BlendingFactor), (BlendingFactor, BlendingFactor))
384 -> Vec 4 Float -> Blending Float
385
386{- TODO: more precise kinds
387 FragmentOperation :: Semantic -> *
388 FragmentOut :: Semantic -> *
389 VertexOut :: ???
390-}
391
392data StencilTests
393data StencilOps
394data Int32
395
396data FragmentOperation :: Type -> Type where
397 ColorOp :: (mask ~ VecScalar d Bool, color ~ VecScalar d c, Num c) => Blending c -> mask
398 -> FragmentOperation (Color color)
399 DepthOp :: ComparisonFunction -> Bool -> FragmentOperation (Depth Float)
400 StencilOp :: StencilTests -> StencilOps -> StencilOps -> FragmentOperation (Stencil Int32)
401
402type family FragOps a where
403 FragOps (FragmentOperation t) = t
404 FragOps (FragmentOperation t1, FragmentOperation t2) = (t1, t2)
405 FragOps (FragmentOperation t1, FragmentOperation t2, FragmentOperation t3) = (t1, t2, t3)
406 FragOps (FragmentOperation t1, FragmentOperation t2, FragmentOperation t3, FragmentOperation t4) = (t1, t2, t3, t4)
407 FragOps (FragmentOperation t1, FragmentOperation t2, FragmentOperation t3, FragmentOperation t4, FragmentOperation t5) = (t1, t2, t3, t4, t5)
408
409data AccumulationContext a where
410 AccumulationContext :: (a ~ FragOps t) => t -> AccumulationContext a
411
412data FragmentFilter t where
413 PassAll :: FragmentFilter t
414 Filter :: (t -> Bool) -> FragmentFilter t
415
416data VertexStream (a :: PrimitiveType) t where
417 Fetch :: (AttributeTuple t) => String -> t -> VertexStream a t
418 FetchArrays :: (AttributeTuple t, t ~ FTRepr' t') => t' -> VertexStream a t
419
420fetch s a t = Fetch @a s t
421fetchArrays a t = FetchArrays @a t
422
423data PrimitiveStream (p :: PrimitiveType) :: Nat -> Type -> Type where
424 Transform :: (a -> VertexOut b) -> VertexStream p a -> PrimitiveStream p 1 b
425
426 -- Render Operations
427data FragmentStream (n :: Nat) a where
428 Rasterize :: RasterContext x -> PrimitiveStream x n a -> FragmentStream n a
429
430data FilteredFragmentStream (n :: Nat) a where
431 FilteredFragmentStream
432 :: FragmentFilter a -> FragmentStream n a -> FilteredFragmentStream n a
433
434data Fragments (n :: Nat) b where
435 Fragments :: ValidOutput b => FragmentShader (a -> b) -> FilteredFragmentStream n a -> Fragments n b
436
437-- todo: mutually defined with FrameBuffer and Image
438type family TFFrameBuffer a {-where
439 TFFrameBuffer (Image n t) = FrameBuffer n t
440 TFFrameBuffer (Image n1 t1, Image n2 t2) {- TODO | n1 == n2 -} = FrameBuffer n1 (t1, t2)
441 TFFrameBuffer (Image n1 t1, Image n2 t2, Image n2 t3) {- TODO | n1 == n2 && n2 == n3 -} = FrameBuffer n1 (t1, t2, t3)
442-}
443data FrameBuffer (n :: Nat) b where
444 Accumulate :: AccumulationContext b -> Fragments n b -> FrameBuffer n b -> FrameBuffer n b
445 FrameBuffer :: (ValidFrameBuffer b, FrameBuffer n b ~ TFFrameBuffer a) => a -> FrameBuffer n b
446
447accumulate ctx ffilt fshader fstr fb = Accumulate ctx (Fragments fshader (FilteredFragmentStream ffilt fstr)) fb
448
449data Image :: Nat -> Type -> Type where
450 ColorImage :: forall a d t color . (Num t, color ~ VecScalar d t)
451 => color -> Image a (Color color)
452 DepthImage :: forall a . Float -> Image a (Depth Float)
453 StencilImage :: forall a . Int -> Image a (Stencil Int)
454
455 -- texture support
456 PrjImage :: FrameBuffer 1 a -> Image 1 a
457 PrjImageColor :: FrameBuffer 1 (Depth Float, Color (Vec 4 Float)) -> Image 1 (Color (Vec 4 Float))
458
459data Output where
460 ScreenOut :: FrameBuffer a b -> Output
461
462-------------------------------------------------------------------
463-- * Builtin Primitive Functions *
464-- Arithmetic Functions (componentwise)
465
466PrimAdd, PrimSub, PrimMul :: Num (MatVecElem a) => a -> a -> a
467PrimAddS, PrimSubS, PrimMulS :: (t ~ MatVecScalarElem a, Num t) => a -> t -> a
468PrimDiv, PrimMod :: (Num t, a ~ VecScalar d t) => a -> a -> a
469PrimDivS, PrimModS :: (Num t, a ~ VecScalar d t) => a -> t -> a
470PrimNeg :: Signed (MatVecScalarElem a) => a -> a
471-- Bit-wise Functions
472PrimBAnd, PrimBOr, PrimBXor :: (Integral t, a ~ VecScalar d t) => a -> a -> a
473PrimBAndS, PrimBOrS, PrimBXorS:: (Integral t, a ~ VecScalar d t) => a -> t -> a
474PrimBNot :: (Integral t, a ~ VecScalar d t) => a -> a
475PrimBShiftL, PrimBShiftR :: (Integral t, a ~ VecScalar d t, b ~ VecScalar d Word) => a -> b -> a
476PrimBShiftLS, PrimBShiftRS :: (Integral t, a ~ VecScalar d t) => a -> Word -> a
477-- Logic Functions
478PrimAnd, PrimOr, PrimXor :: Bool -> Bool -> Bool
479PrimNot :: (a ~ VecScalar d Bool) => a -> a
480PrimAny, PrimAll :: VecScalar d Bool -> Bool
481
482-- Angle, Trigonometry and Exponential Functions
483PrimACos, PrimACosH, PrimASin, PrimASinH, PrimATan, PrimATanH, PrimCos, PrimCosH, PrimDegrees, PrimRadians, PrimSin, PrimSinH, PrimTan, PrimTanH, PrimExp, PrimLog, PrimExp2, PrimLog2, PrimSqrt, PrimInvSqrt
484 :: (a ~ VecScalar d Float) => a -> a
485PrimPow, PrimATan2 :: (a ~ VecScalar d Float) => a -> a -> a
486-- Common Functions
487PrimFloor, PrimTrunc, PrimRound, PrimRoundEven, PrimCeil, PrimFract
488 :: (a ~ VecScalar d Float) => a -> a
489PrimMin, PrimMax :: (Num t, a ~ VecScalar d t) => a -> a -> a
490PrimMinS, PrimMaxS :: (Num t, a ~ VecScalar d t) => a -> t -> a
491PrimIsNan, PrimIsInf :: (a ~ VecScalar d Float, b ~ VecScalar d Bool) => a -> b
492PrimAbs, PrimSign :: (Signed t, a ~ VecScalar d t) => a -> a
493PrimModF :: (a ~ VecScalar d Float) => a -> (a, a)
494PrimClamp :: (Num t, a ~ VecScalar d t) => a -> a -> a -> a
495PrimClampS :: (Num t, a ~ VecScalar d t) => a -> t -> t -> a
496PrimMix :: (a ~ VecScalar d Float) => a -> a -> a -> a
497PrimMixS :: (a ~ VecScalar d Float) => a -> a -> Float -> a
498PrimMixB :: (a ~ VecScalar d Float, b ~ VecScalar d Bool) => a -> a -> b -> a
499PrimStep :: (a ~ TFVec d Float) => a -> a -> a
500PrimStepS :: (a ~ VecScalar d Float) => Float -> a -> a
501PrimSmoothStep :: (a ~ TFVec d Float) => a -> a -> a -> a
502PrimSmoothStepS :: (a ~ VecScalar d Float) => Float -> Float -> a -> a
503
504-- Integer/Floatonversion Functions
505PrimFloatBitsToInt :: VecScalar d Float -> VecScalar d Int
506PrimFloatBitsToUInt :: VecScalar d Float -> VecScalar d Word
507PrimIntBitsToFloat :: VecScalar d Int -> VecScalar d Float
508PrimUIntBitsToFloat :: VecScalar d Word -> VecScalar d Float
509-- Geometric Functions
510PrimLength :: (a ~ VecScalar d Float) => a -> Float
511PrimDistance, PrimDot :: (a ~ VecScalar d Float) => a -> a -> Float
512PrimCross :: (a ~ VecScalar 3 Float) => a -> a -> a
513PrimNormalize :: (a ~ VecScalar d Float) => a -> a
514PrimFaceForward, PrimRefract :: (a ~ VecScalar d Float) => a -> a -> a -> a
515PrimReflect :: (a ~ VecScalar d Float) => a -> a -> a
516-- Matrix Functions
517PrimTranspose :: TFMat h w -> TFMat w h
518PrimDeterminant :: TFMat s s -> Float
519PrimInverse :: TFMat s s -> TFMat s s
520PrimOuterProduct :: w -> h -> TFMat h w
521PrimMulMatVec :: TFMat h w -> w -> h
522PrimMulVecMat :: h -> TFMat h w -> w
523PrimMulMatMat :: TFMat i j -> TFMat j k -> TFMat i k
524-- Vector and Scalar Relational Functions
525PrimLessThan, PrimLessThanEqual, PrimGreaterThan, PrimGreaterThanEqual, PrimEqualV, PrimNotEqualV
526 :: (Num t, a ~ VecScalar d t, b ~ VecScalar d Bool) => a -> a -> b
527PrimEqual, PrimNotEqual :: (t ~ MatVecScalarElem a) => a -> a -> Bool
528-- Fragment Processing Functions
529PrimDFdx, PrimDFdy, PrimFWidth
530 :: (a ~ VecScalar d Float) => a -> a
531-- Noise Functions
532PrimNoise1 :: VecScalar d Float -> Float
533PrimNoise2 :: VecScalar d Float -> Vec 2 Float
534PrimNoise3 :: VecScalar d Float -> Vec 3 Float
535PrimNoise4 :: VecScalar d Float -> Vec 4 Float
536
537{-
538-- Vec/Mat (de)construction
539PrimTupToV2 :: Component a => PrimFun stage ((a,a) -> V2 a)
540PrimTupToV3 :: Component a => PrimFun stage ((a,a,a) -> V3 a)
541PrimTupToV4 :: Component a => PrimFun stage ((a,a,a,a) -> V4 a)
542PrimV2ToTup :: Component a => PrimFun stage (V2 a -> (a,a))
543PrimV3ToTup :: Component a => PrimFun stage (V3 a -> (a,a,a))
544PrimV4ToTup :: Component a => PrimFun stage (V4 a -> (a,a,a,a))
545-}
546
547--------------------
548-- * Texture support
549-- FIXME: currently only Float RGBA 2D texture is supported
550
551data Texture where
552 Texture2DSlot :: String -- texture slot name
553 -> Texture
554
555 Texture2D :: Vec 2 Int -- FIXME: use Word here
556 -> Image 1 (Color (Vec 4 Float))
557 -> Texture
558
559data Filter
560 = PointFilter
561 | LinearFilter
562
563data EdgeMode
564 = Repeat
565 | MirroredRepeat
566 | ClampToEdge
567
568data Sampler = Sampler Filter EdgeMode Texture
569
570-- builtin
571texture2D :: Sampler -> Vec 2 Float -> Vec 4 Float
572
diff --git a/lc/Internals.lc b/lc/Internals.lc
new file mode 100644
index 00000000..b64d5a41
--- /dev/null
+++ b/lc/Internals.lc
@@ -0,0 +1,62 @@
1{-# LANGUAGE NoImplicitPrelude #-}
2-- declarations of builtin functions and data types used by the compiler
3module Internals where
4
5undefined :: forall (a :: Type) . a
6
7data Unit = TT
8data String
9data Empty (a :: String)
10
11-- TODO: generate?
12data Tuple0 = Tuple0
13data Tuple1 a = Tuple1 a
14data Tuple2 a b = Tuple2 a b
15data Tuple3 a b c = Tuple3 a b c
16data Tuple4 a b c d = Tuple4 a b c d
17data Tuple5 a b c d e = Tuple5 a b c d e
18
19
20-- ... TODO
21
22-- builtin used for overlapping instances
23parEval :: forall a -> a -> a -> a
24
25type family EqC a b -- equality constraints
26type family T2 a b -- conjuction of constraints
27
28-- builtin conjuction of constraint witnesses
29t2C :: Unit -> Unit -> Unit
30
31-- builtin type constructors
32data Int
33data Word
34data Float
35data Char
36
37data Bool = False | True
38
39data Ordering = LT | EQ | GT
40
41-- builtin primitives
42primIntToWord :: Int -> Word
43primIntToFloat :: Int -> Float
44primCompareInt :: Int -> Int -> Ordering
45primCompareWord :: Word -> Word -> Ordering
46primCompareFloat :: Float -> Float -> Ordering
47primCompareString :: String -> String -> Ordering
48primNegateInt :: Int -> Int
49primNegateWord :: Word -> Word
50primNegateFloat :: Float -> Float
51primAddInt :: Int -> Int -> Int
52primSubInt :: Int -> Int -> Int
53primModInt :: Int -> Int -> Int
54primSqrtFloat :: Float -> Float
55primRound :: Float -> Int
56
57
58primIfThenElse :: Bool -> a -> a -> a
59primIfThenElse True a b = a
60primIfThenElse False a b = b
61
62
diff --git a/lc/Prelude.lc b/lc/Prelude.lc
new file mode 100644
index 00000000..6ab787d1
--- /dev/null
+++ b/lc/Prelude.lc
@@ -0,0 +1,362 @@
1{-# LANGUAGE NoImplicitPrelude #-}
2module Prelude
3 ( module Prelude
4 , module Builtins
5 ) where
6
7import Builtins
8
9infixr 9 .
10infixl 7 `PrimMulMatVec`, `PrimDot`
11infixr 3 ***
12infixr 5 :
13infixr 0 $
14--infixl 0 &
15
16const x y = x
17
18otherwise = True
19
20x & f = f x
21
22($) = \f x -> f x
23(.) = \f g x -> f (g x)
24
25uncurry f (x, y) = f x y
26
27(***) f g (x, y) = (f x, g y)
28
29pi = 3.14
30
31zip :: [a] -> [b] -> [(a,b)]
32zip [] xs = []
33zip xs [] = []
34zip (a: as) (b: bs) = (a,b): zip as bs
35
36unzip :: [(a,b)] -> ([a],[b])
37unzip [] = ([],[])
38unzip ((a,b):xs) = (a:as,b:bs)
39 where (as,bs) = unzip xs
40
41filter pred [] = []
42filter pred (x:xs) = case pred x of
43 True -> (x : filter pred xs)
44 False -> (filter pred xs)
45
46head :: [a] -> a
47head (a: _) = a
48
49tail :: [a] -> [a]
50tail (_: xs) = xs
51
52pairs :: [a] -> [(a, a)]
53pairs v = zip v (tail v)
54
55foldl' f e [] = e
56foldl' f e (x: xs) = foldl' f (f e x) xs
57
58singleton a = [a]
59
60append [] ys = ys
61append (x:xs) ys = x : append xs ys
62
63concat = foldl' append []
64
65map _ [] = []
66map f (x:xs) = f x : map f xs
67
68concatMap :: (a -> [b]) -> [a] -> [b]
69concatMap f x = concat (map f x)
70
71split [] = ([], [])
72split (x: xs) = (x: bs, as) where (as, bs) = split xs
73
74mergeBy f (x:xs) (y:ys) = case f x y of
75 LT -> x: mergeBy f xs (y:ys)
76 _ -> y: mergeBy f (x:xs) ys
77mergeBy f [] xs = xs
78mergeBy f xs [] = xs
79
80sortBy f [] = []
81sortBy f [x] = [x]
82sortBy f xs = uncurry (mergeBy f) ((sortBy f *** sortBy f) (split xs))
83
84data Maybe a
85 = Nothing
86 | Just a
87-- deriving (Eq, Ord, Show)
88
89
90fst (a, b) = a
91snd (a, b) = b
92
93tuptype :: [Type] -> Type
94tuptype [] = '()
95tuptype (x:xs) = '(x, tuptype xs)
96
97data RecordC (xs :: [(String, Type)])
98 = RecordCons (tuptype (map snd xs))
99
100foldr1 f [x] = x
101foldr1 f (x: xs) = f x (foldr1 f xs)
102
103isEQ EQ = True
104isEQ _ = False
105
106False ||| x = x
107True ||| x = True
108
109infixr 2 |||
110
111True &&& x = x
112False &&& x = False
113
114infixr 3 &&&
115
116class Eq a where
117 (===) :: a -> a -> Bool -- todo: use (==) sign
118
119infix 4 ===
120
121instance Eq String where
122 a === b = isEQ (primCompareString a b)
123
124------------------------------------ Row polymorphism
125-- todo: sorted field names (more efficient & easier to use)
126
127isKey _ [] = False
128isKey s ((s', _): ss) = s === s' ||| isKey s ss
129
130isKeyC _ _ [] = 'Empty ""
131isKeyC s t ((s', t'): ss) = if s === s' then t ~ t' else isKeyC s t ss
132
133subList [] _ = []
134subList ((s, t): xs) ys = if isKey s ys then subList xs ys else (s, t): subList xs ys
135
136addList [] ys = ys
137addList ((s, t): xs) ys = if isKey s ys then addList xs ys else (s, t): addList xs ys
138
139findEq x [] = 'Unit
140findEq (s, t) ((s', t'):xs) = if s === s' then 'T2 (t ~ t') (findEq (s, t) xs) else findEq (s, t) xs
141
142sameEq [] _ = 'Unit
143sameEq (x: xs) ys = 'T2 (findEq x ys) (sameEq xs ys)
144
145defined [] = True
146defined (x: xs) = defined xs
147
148type family Split a b c
149type instance Split (RecordC xs) (RecordC ys) z | defined xs &&& defined ys = T2 (sameEq xs ys) (z ~ RecordC (subList xs ys))
150type instance Split (RecordC xs) z (RecordC ys) | defined xs &&& defined ys = T2 (sameEq xs ys) (z ~ RecordC (subList xs ys))
151type instance Split z (RecordC xs) (RecordC ys) | defined xs &&& defined ys = T2 (sameEq xs ys) (z ~ RecordC (addList xs ys))
152
153-- builtin
154-- TODO
155record :: [(String, Type)] -> Type
156--record xs = RecordCons ({- TODO: sortBy fst-} xs)
157
158-- builtin
159unsafeCoerce :: forall a b . a -> b
160
161-- todo: don't use unsafeCoerce
162project :: forall a (xs :: [(String, Type)]) . forall (s :: String) -> 'isKeyC s a xs => RecordC xs -> a
163project @a @((s', a'): xs) s @_ (RecordCons ts) | s === s' = fst (unsafeCoerce @_ @(a, tuptype (map snd xs)) ts)
164project @a @((s', a'): xs) s @_ (RecordCons ts) = project @a @xs s @undefined (RecordCons (snd (unsafeCoerce @_ @(a, tuptype (map snd xs)) ts)))
165
166--------------------------------------- HTML colors
167
168rgb r g b = V4 r g b 1.0
169
170black = rgb 0.0 0.0 0.0
171gray = rgb 0.5 0.5 0.5
172silver = rgb 0.75 0.75 0.75
173white = rgb 1.0 1.0 1.0
174maroon = rgb 0.5 0.0 0.0
175red = rgb 1.0 0.0 0.0
176olive = rgb 0.5 0.5 0.0
177yellow = rgb 1.0 1.0 0.0
178green = rgb 0.0 0.5 0.0
179lime = rgb 0.0 1.0 0.0
180teal = rgb 0.0 0.5 0.5
181aqua = rgb 0.0 1.0 1.0
182navy = rgb 0.0 0.0 0.5
183blue = rgb 0.0 0.0 1.0
184purple = rgb 0.5 0.0 0.5
185fuchsia = rgb 1.0 0.0 1.0
186
187colorImage1 = ColorImage @1
188colorImage2 = ColorImage @2
189
190depthImage1 = DepthImage @1
191
192v3FToV4F :: Vec 3 Float -> Vec 4 Float
193v3FToV4F v = V4 0.0 0.0 0.0 1.0 --- todo! -- V4 v%x v%y v%z 1
194
195------------
196-- * WebGL 1
197------------
198
199-- angle and trigonometric
200radians = PrimRadians
201degrees = PrimDegrees
202sin = PrimSin
203cos = PrimCos
204tan = PrimTan
205asin = PrimASin
206acos = PrimACos
207atan = PrimATan
208atan2 = PrimATan2
209
210-- exponential functions
211pow = PrimPow
212exp = PrimExp
213log = PrimLog
214exp2 = PrimExp2
215log2 = PrimLog2
216sqrt = PrimSqrt
217inversesqrt = PrimInvSqrt
218
219-- common functions
220abs = PrimAbs
221sign = PrimSign
222floor = PrimFloor
223ceil = PrimCeil
224fract = PrimFract
225mod = PrimMod
226min = PrimMin
227max = PrimMax
228clamp = PrimClamp
229clampS = PrimClampS
230mix = PrimMix
231step = PrimStep
232smoothstep = PrimSmoothStep
233
234-- geometric functions
235length = PrimLength
236distance = PrimDistance
237dot = PrimDot
238cross = PrimCross
239normalize = PrimNormalize
240faceforward = PrimFaceForward
241reflect = PrimReflect
242refract = PrimRefract
243
244-- operators
245infixl 7 *, /, %
246infixl 6 +, -
247infix 4 ==, /=, <, <=, >=, >
248
249infixr 3 &&
250infixr 2 ||
251
252infix 7 `dot` -- dot
253infix 7 `cross` -- cross
254
255infixr 7 *. -- mulmv
256infixl 7 .* -- mulvm
257infixl 7 .*. -- mulmm
258
259-- arithemtic
260a + b = PrimAdd a b
261a - b = PrimSub a b
262a * b = PrimMul a b
263a / b = PrimDiv a b
264a % b = PrimMod a b
265
266neg a = PrimNeg a
267
268-- comparison
269a == b = PrimEqual a b
270a /= b = PrimNotEqual a b
271a < b = PrimLessThan a b
272a <= b = PrimLessThanEqual a b
273a >= b = PrimGreaterThanEqual a b
274a > b = PrimGreaterThan a b
275
276-- logical
277a && b = PrimAnd a b
278a || b = PrimOr a b
279not a = PrimNot a
280any a = PrimAny a
281all a = PrimAll a
282
283-- matrix functions
284a .*. b = PrimMulMatMat a b
285a *. b = PrimMulMatVec a b
286a .* b = PrimMulVecMat a b
287
288dFdx = PrimDFdx
289dFdy = PrimDFdy
290
291-- extra
292round = PrimRound
293
294
295-- temp hack for vector <---> scalar operators
296infixl 7 *!, /!, %!
297infixl 6 +!, -!
298
299-- arithemtic
300a +! b = PrimAddS a b
301a -! b = PrimSubS a b
302a *! b = PrimMulS a b
303a /! b = PrimDivS a b
304a %! b = PrimModS a b
305
306------------------
307-- common matrices
308------------------
309{-
310-- | Perspective transformation matrix in row major order.
311perspective :: Float -- ^ Near plane clipping distance (always positive).
312 -> Float -- ^ Far plane clipping distance (always positive).
313 -> Float -- ^ Field of view of the y axis, in radians.
314 -> Float -- ^ Aspect ratio, i.e. screen's width\/height.
315 -> Mat 4 4 Float
316perspective n f fovy aspect = --transpose $
317 M44F (V4F (2*n/(r-l)) 0 (-(r+l)/(r-l)) 0)
318 (V4F 0 (2*n/(t-b)) ((t+b)/(t-b)) 0)
319 (V4F 0 0 (-(f+n)/(f-n)) (-2*f*n/(f-n)))
320 (V4F 0 0 (-1) 0)
321 where
322 t = n*tan(fovy/2)
323 b = -t
324 r = aspect*t
325 l = -r
326-}
327rotMatrixZ a = M44F (V4 c s 0 0) (V4 (-s) c 0 0) (V4 0 0 1 0) (V4 0 0 0 1)
328 where
329 c = cos a
330 s = sin a
331
332rotMatrixY a = M44F (V4 c 0 (-s) 0) (V4 0 1 0 0) (V4 s 0 c 0) (V4 0 0 0 1)
333 where
334 c = cos a
335 s = sin a
336
337rotMatrixX a = M44F (V4 1 0 0 0) (V4 0 c s 0) (V4 0 (-s) c 0) (V4 0 0 0 1)
338 where
339 c = cos a
340 s = sin a
341
342rotationEuler a b c = rotMatrixY a .*. rotMatrixX b .*. rotMatrixZ c
343
344{-
345-- | Camera transformation matrix.
346lookat :: Vec 3 Float -- ^ Camera position.
347 -> Vec 3 Float -- ^ Target position.
348 -> Vec 3 Float -- ^ Upward direction.
349 -> M44F
350lookat pos target up = translateBefore4 (neg pos) (orthogonal $ toOrthoUnsafe r)
351 where
352 w = normalize $ pos - target
353 u = normalize $ up `cross` w
354 v = w `cross` u
355 r = transpose $ Mat3 u v w
356-}
357
358scale t v = v * V4 t t t 1.0
359
360fromTo :: Float -> Float -> [Float]
361fromTo a b = if a > b then [] else a: fromTo (a +! 1.0) b
362