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