diff options
author | Péter Diviánszky <divipp@gmail.com> | 2016-02-22 16:46:40 +0100 |
---|---|---|
committer | Péter Diviánszky <divipp@gmail.com> | 2016-02-22 16:47:27 +0100 |
commit | f183de529a6cab5539906e94c6d0104226dca398 (patch) | |
tree | 0fc2fecc59fb140dedf03500206fadd20e33ae98 /lc | |
parent | 55c705a119ba1eba24abffd6a3ba5ea432a038a6 (diff) |
refactoring
Diffstat (limited to 'lc')
-rw-r--r-- | lc/Builtins.lc | 490 |
1 files changed, 250 insertions, 240 deletions
diff --git a/lc/Builtins.lc b/lc/Builtins.lc index 9484a9d9..29533cbf 100644 --- a/lc/Builtins.lc +++ b/lc/Builtins.lc | |||
@@ -11,16 +11,16 @@ id x = x | |||
11 | 11 | ||
12 | --------------------------------------- | 12 | --------------------------------------- |
13 | 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 | 14 | data VecS (a :: Type) :: Nat -> Type where |
20 | V2 :: a -> a -> VecS a 2 | 15 | V2 :: a -> a -> VecS a 2 |
21 | V3 :: a -> a -> a -> VecS a 3 | 16 | V3 :: a -> a -> a -> VecS a 3 |
22 | V4 :: a -> a -> a -> a -> VecS a 4 | 17 | V4 :: a -> a -> a -> a -> VecS a 4 |
23 | 18 | ||
19 | mapVec :: (a -> b) -> VecS a n -> VecS b n | ||
20 | mapVec f (V2 x y) = V2 (f x) (f y) | ||
21 | mapVec f (V3 x y z) = V3 (f x) (f y) (f z) | ||
22 | mapVec f (V4 x y z w) = V4 (f x) (f y) (f z) (f w) | ||
23 | |||
24 | type family Vec (n :: Nat) t where Vec n t = VecS t n | 24 | type family Vec (n :: Nat) t where Vec n t = VecS t n |
25 | 25 | ||
26 | type family VecScalar (n :: Nat) a where | 26 | type family VecScalar (n :: Nat) a where |
@@ -46,53 +46,6 @@ type family MatVecScalarElem a where | |||
46 | MatVecScalarElem (VecS a n) = a | 46 | MatVecScalarElem (VecS a n) = a |
47 | MatVecScalarElem (Mat i j a) = a | 47 | MatVecScalarElem (Mat i j a) = a |
48 | 48 | ||
49 | --------------------------------------- swizzling | ||
50 | |||
51 | mapVec :: (a -> b) -> VecS a n -> VecS b n | ||
52 | mapVec f (V2 x y) = V2 (f x) (f y) | ||
53 | mapVec f (V3 x y z) = V3 (f x) (f y) (f z) | ||
54 | mapVec f (V4 x y z w) = V4 (f x) (f y) (f z) (f w) | ||
55 | |||
56 | data Swizz = Sx | Sy | Sz | Sw | ||
57 | |||
58 | data Swizz' :: Nat -> Type where | ||
59 | Sx' :: forall n . Swizz' (Succ n) | ||
60 | Sy' :: forall n . Swizz' (Succ (Succ n)) | ||
61 | Sz' :: forall n . Swizz' (Succ (Succ (Succ n))) | ||
62 | Sw' :: forall n . Swizz' (Succ (Succ (Succ (Succ n)))) | ||
63 | |||
64 | swizzscalar' :: forall n -> Vec n a -> Swizz' n -> a | ||
65 | {- | ||
66 | swizzscalar' 2 = \x -> case x of | ||
67 | V2 x y -> \s -> case s of | ||
68 | Sx' -> x | ||
69 | Sy' -> y | ||
70 | swizzscalar' 3 = \x -> case x of | ||
71 | V3 x y z -> \s -> case s of | ||
72 | Sx' -> x | ||
73 | -} | ||
74 | -- todo: make it more type safe | ||
75 | swizzscalar :: forall n . Vec n a -> Swizz -> a | ||
76 | swizzscalar (V2 x y) Sx = x | ||
77 | swizzscalar (V2 x y) Sy = y | ||
78 | swizzscalar (V3 x y z) Sx = x | ||
79 | swizzscalar (V3 x y z) Sy = y | ||
80 | swizzscalar (V3 x y z) Sz = z | ||
81 | swizzscalar (V4 x y z w) Sx = x | ||
82 | swizzscalar (V4 x y z w) Sy = y | ||
83 | swizzscalar (V4 x y z w) Sz = z | ||
84 | swizzscalar (V4 x y z w) Sw = w | ||
85 | |||
86 | -- used to prevent unfolding of swizzvector on variables (behind GPU lambda) | ||
87 | definedVec :: forall a m . Vec m a -> Bool | ||
88 | definedVec (V2 _ _) = True | ||
89 | definedVec (V3 _ _ _) = True | ||
90 | definedVec (V4 _ _ _ _) = True | ||
91 | |||
92 | swizzvector :: forall n . forall m . Vec n a -> Vec m Swizz -> Vec m a | ||
93 | swizzvector v w | definedVec v = mapVec (swizzscalar v) w | ||
94 | |||
95 | |||
96 | --------------------------------------- type classes | 49 | --------------------------------------- type classes |
97 | 50 | ||
98 | class Signed a | 51 | class Signed a |
@@ -156,6 +109,239 @@ instance Floating (Mat 4 2 Float) | |||
156 | instance Floating (Mat 4 3 Float) | 109 | instance Floating (Mat 4 3 Float) |
157 | instance Floating (Mat 4 4 Float) | 110 | instance Floating (Mat 4 4 Float) |
158 | 111 | ||
112 | |||
113 | ------------------------------------------------------------------- | ||
114 | -- * Builtin Primitive Functions * | ||
115 | -- Arithmetic Functions (componentwise) | ||
116 | |||
117 | PrimAdd, PrimSub, PrimMul :: Num (MatVecScalarElem a) => a -> a -> a | ||
118 | PrimAddS, PrimSubS, PrimMulS :: (t ~ MatVecScalarElem a, Num t) => a -> t -> a | ||
119 | PrimDiv, PrimMod :: (Num t, a ~ VecScalar d t) => a -> a -> a | ||
120 | PrimDivS, PrimModS :: (Num t, a ~ VecScalar d t) => a -> t -> a | ||
121 | PrimNeg :: Signed (MatVecScalarElem a) => a -> a | ||
122 | -- Bit-wise Functions | ||
123 | PrimBAnd, PrimBOr, PrimBXor :: (Integral t, a ~ VecScalar d t) => a -> a -> a | ||
124 | PrimBAndS, PrimBOrS, PrimBXorS:: (Integral t, a ~ VecScalar d t) => a -> t -> a | ||
125 | PrimBNot :: (Integral t, a ~ VecScalar d t) => a -> a | ||
126 | PrimBShiftL, PrimBShiftR :: (Integral t, a ~ VecScalar d t, b ~ VecScalar d Word) => a -> b -> a | ||
127 | PrimBShiftLS, PrimBShiftRS :: (Integral t, a ~ VecScalar d t) => a -> Word -> a | ||
128 | -- Logic Functions | ||
129 | PrimAnd, PrimOr, PrimXor :: Bool -> Bool -> Bool | ||
130 | PrimNot :: forall a d . (a ~ VecScalar d Bool) => a -> a | ||
131 | PrimAny, PrimAll :: VecScalar d Bool -> Bool | ||
132 | |||
133 | -- Angle, Trigonometry and Exponential Functions | ||
134 | PrimACos, PrimACosH, PrimASin, PrimASinH, PrimATan, PrimATanH, PrimCos, PrimCosH, PrimDegrees, PrimRadians, PrimSin, PrimSinH, PrimTan, PrimTanH, PrimExp, PrimLog, PrimExp2, PrimLog2, PrimSqrt, PrimInvSqrt | ||
135 | :: (a ~ VecScalar d Float) => a -> a | ||
136 | PrimPow, PrimATan2 :: (a ~ VecScalar d Float) => a -> a -> a | ||
137 | -- Common Functions | ||
138 | PrimFloor, PrimTrunc, PrimRound, PrimRoundEven, PrimCeil, PrimFract | ||
139 | :: (a ~ VecScalar d Float) => a -> a | ||
140 | PrimMin, PrimMax :: (Num t, a ~ VecScalar d t) => a -> a -> a | ||
141 | PrimMinS, PrimMaxS :: (Num t, a ~ VecScalar d t) => a -> t -> a | ||
142 | PrimIsNan, PrimIsInf :: (a ~ VecScalar d Float, b ~ VecScalar d Bool) => a -> b | ||
143 | PrimAbs, PrimSign :: (Signed t, a ~ VecScalar d t) => a -> a | ||
144 | PrimModF :: (a ~ VecScalar d Float) => a -> (a, a) | ||
145 | PrimClamp :: (Num t, a ~ VecScalar d t) => a -> a -> a -> a | ||
146 | PrimClampS :: (Num t, a ~ VecScalar d t) => a -> t -> t -> a | ||
147 | PrimMix :: (a ~ VecScalar d Float) => a -> a -> a -> a | ||
148 | PrimMixS :: (a ~ VecScalar d Float) => a -> a -> Float -> a | ||
149 | PrimMixB :: (a ~ VecScalar d Float, b ~ VecScalar d Bool) => a -> a -> b -> a | ||
150 | PrimStep :: (a ~ Vec d Float) => a -> a -> a | ||
151 | PrimStepS :: (a ~ VecScalar d Float) => Float -> a -> a | ||
152 | PrimSmoothStep :: (a ~ Vec d Float) => a -> a -> a -> a | ||
153 | PrimSmoothStepS :: (a ~ VecScalar d Float) => Float -> Float -> a -> a | ||
154 | |||
155 | -- Integer/Floatonversion Functions | ||
156 | PrimFloatBitsToInt :: VecScalar d Float -> VecScalar d Int | ||
157 | PrimFloatBitsToUInt :: VecScalar d Float -> VecScalar d Word | ||
158 | PrimIntBitsToFloat :: VecScalar d Int -> VecScalar d Float | ||
159 | PrimUIntBitsToFloat :: VecScalar d Word -> VecScalar d Float | ||
160 | -- Geometric Functions | ||
161 | PrimLength :: (a ~ VecScalar d Float) => a -> Float | ||
162 | PrimDistance, PrimDot :: (a ~ VecScalar d Float) => a -> a -> Float | ||
163 | PrimCross :: (a ~ VecScalar 3 Float) => a -> a -> a | ||
164 | PrimNormalize :: (a ~ VecScalar d Float) => a -> a | ||
165 | PrimFaceForward, PrimRefract :: (a ~ VecScalar d Float) => a -> a -> a -> a | ||
166 | PrimReflect :: (a ~ VecScalar d Float) => a -> a -> a | ||
167 | -- Matrix Functions | ||
168 | PrimTranspose :: Mat h w a -> Mat w h a | ||
169 | PrimDeterminant :: Mat s s a -> Float | ||
170 | PrimInverse :: Mat s s a -> Mat s s a | ||
171 | PrimOuterProduct :: Vec w a -> Vec h a -> Mat h w a | ||
172 | PrimMulMatVec :: Mat h w a -> Vec w a -> Vec h a | ||
173 | PrimMulVecMat :: Vec h a -> Mat h w a -> Vec w a | ||
174 | PrimMulMatMat :: Mat i j a -> Mat j k a -> Mat i k a | ||
175 | -- Vector and Scalar Relational Functions | ||
176 | PrimLessThan, PrimLessThanEqual, PrimGreaterThan, PrimGreaterThanEqual, PrimEqualV, PrimNotEqualV | ||
177 | :: forall a d t b . (Num t, a ~ VecScalar d t, b ~ VecScalar d Bool) => a -> a -> b | ||
178 | PrimEqual, PrimNotEqual :: forall a t . (t ~ MatVecScalarElem a) => a -> a -> Bool | ||
179 | -- Fragment Processing Functions | ||
180 | PrimDFdx, PrimDFdy, PrimFWidth | ||
181 | :: (a ~ VecScalar d Float) => a -> a | ||
182 | -- Noise Functions | ||
183 | PrimNoise1 :: VecScalar d Float -> Float | ||
184 | PrimNoise2 :: VecScalar d Float -> Vec 2 Float | ||
185 | PrimNoise3 :: VecScalar d Float -> Vec 3 Float | ||
186 | PrimNoise4 :: VecScalar d Float -> Vec 4 Float | ||
187 | |||
188 | {- | ||
189 | -- Vec/Mat (de)construction | ||
190 | PrimTupToV2 :: Component a => PrimFun stage ((a,a) -> V2 a) | ||
191 | PrimTupToV3 :: Component a => PrimFun stage ((a,a,a) -> V3 a) | ||
192 | PrimTupToV4 :: Component a => PrimFun stage ((a,a,a,a) -> V4 a) | ||
193 | PrimV2ToTup :: Component a => PrimFun stage (V2 a -> (a,a)) | ||
194 | PrimV3ToTup :: Component a => PrimFun stage (V3 a -> (a,a,a)) | ||
195 | PrimV4ToTup :: Component a => PrimFun stage (V4 a -> (a,a,a,a)) | ||
196 | -} | ||
197 | |||
198 | ------------------------------------------------------- | ||
199 | |||
200 | head (x: _) = x | ||
201 | |||
202 | [] ++ ys = ys | ||
203 | x:xs ++ ys = x : xs ++ ys | ||
204 | |||
205 | foldr f e [] = e | ||
206 | foldr f e (x: xs) = f x (foldr f e xs) | ||
207 | |||
208 | concat = foldr (++) [] | ||
209 | |||
210 | map _ [] = [] | ||
211 | map f (x:xs) = f x : map f xs | ||
212 | |||
213 | concatMap :: (a -> [b]) -> [a] -> [b] | ||
214 | concatMap f x = concat (map f x) | ||
215 | |||
216 | ------------------- | ||
217 | |||
218 | data Maybe a | ||
219 | = Nothing | ||
220 | | Just a | ||
221 | -- deriving (Eq, Ord, Show) | ||
222 | |||
223 | data Vector (n :: Nat) t | ||
224 | |||
225 | ------------------------------------------------------- | ||
226 | |||
227 | data PrimitiveType | ||
228 | = Triangle | ||
229 | | Line | ||
230 | | Point | ||
231 | | TriangleAdjacency | ||
232 | | LineAdjacency | ||
233 | |||
234 | data Primitive a :: PrimitiveType -> Type where | ||
235 | PrimPoint :: a -> Primitive a Point | ||
236 | PrimLine :: a -> a -> Primitive a Line | ||
237 | PrimTriangle :: a -> a -> a -> Primitive a Triangle | ||
238 | |||
239 | mapPrimitive :: (a -> b) -> Primitive a p -> Primitive b p | ||
240 | {- todo | ||
241 | mapPrimitive f (PrimPoint a) = PrimPoint (f a) | ||
242 | mapPrimitive f (PrimLine a b) = PrimLine (f a) (f b) | ||
243 | mapPrimitive f (PrimTriangle a b c) = PrimTriangle (f a) (f b) (f c) | ||
244 | -} | ||
245 | |||
246 | type PrimitiveStream a t = [Primitive t a] | ||
247 | |||
248 | mapPrimitives :: (a -> b) -> PrimitiveStream p a -> PrimitiveStream p b | ||
249 | mapPrimitives f = map (mapPrimitive f) | ||
250 | |||
251 | type family ListElem a where ListElem [a] = a | ||
252 | |||
253 | --class AttributeTuple a | ||
254 | --instance AttributeTuple a -- TODO | ||
255 | |||
256 | fetchArrays :: forall a t t' . ({-AttributeTuple t, -} t ~ map ListElem t') => HList t' -> PrimitiveStream a (HList t) | ||
257 | |||
258 | fetch :: forall a t . {-(AttributeTuple t) => -} String -> t -> PrimitiveStream a t | ||
259 | |||
260 | ------------------------------------------------------ | ||
261 | |||
262 | type Fragment n t = Vector n (Maybe (SimpleFragment t)) | ||
263 | |||
264 | data SimpleFragment t = SimpleFragment | ||
265 | { sFragmentCoords :: Vec 3 Float | ||
266 | , sFragmentValue :: t | ||
267 | } | ||
268 | |||
269 | type FragmentStream n t = [Fragment n t] | ||
270 | |||
271 | customizeDepth :: (a -> Float) -> Fragment n a -> Fragment n a | ||
272 | |||
273 | customizeDepths :: (a -> Float) -> FragmentStream n a -> FragmentStream n a | ||
274 | customizeDepths f = map (customizeDepth f) | ||
275 | |||
276 | filterFragment :: (a -> Bool) -> Fragment n a -> Fragment n a | ||
277 | |||
278 | filterFragments :: (a -> Bool) -> FragmentStream n a -> FragmentStream n a | ||
279 | filterFragments p = map (filterFragment p) | ||
280 | |||
281 | mapFragment :: (a -> b) -> Fragment n a -> Fragment n b | ||
282 | |||
283 | mapFragments :: (a -> b) -> FragmentStream n a -> FragmentStream n b | ||
284 | mapFragments f = map (mapFragment f) | ||
285 | |||
286 | ------------------------------------------------------------------------- | ||
287 | |||
288 | data ImageSemantics = Depth Type | Stencil Type | Color Type | ||
289 | |||
290 | data Image (n :: Nat) (t :: ImageSemantics) -- = Vector n [[t]] | ||
291 | |||
292 | ColorImage :: forall a d t color . (Num t, color ~ VecScalar d t) | ||
293 | => color -> Image a (Color color) | ||
294 | DepthImage :: forall a . Float -> Image a (Depth Float) | ||
295 | StencilImage :: forall a . Int -> Image a (Stencil Int) | ||
296 | |||
297 | emptyDepthImage = DepthImage @1 | ||
298 | emptyColorImage = ColorImage @1 | ||
299 | |||
300 | ------------------------------------------------------------------------- | ||
301 | |||
302 | |||
303 | --------------------------------------- swizzling | ||
304 | |||
305 | data Swizz = Sx | Sy | Sz | Sw | ||
306 | {- | ||
307 | data Swizz' :: Nat -> Type where | ||
308 | Sx' :: forall n . Swizz' (Succ n) | ||
309 | Sy' :: forall n . Swizz' (Succ (Succ n)) | ||
310 | Sz' :: forall n . Swizz' (Succ (Succ (Succ n))) | ||
311 | Sw' :: forall n . Swizz' (Succ (Succ (Succ (Succ n)))) | ||
312 | |||
313 | swizzscalar' :: forall n -> Vec n a -> Swizz' n -> a | ||
314 | swizzscalar' 2 = \x -> case x of | ||
315 | V2 x y -> \s -> case s of | ||
316 | Sx' -> x | ||
317 | Sy' -> y | ||
318 | swizzscalar' 3 = \x -> case x of | ||
319 | V3 x y z -> \s -> case s of | ||
320 | Sx' -> x | ||
321 | -} | ||
322 | -- todo: make it more type safe | ||
323 | swizzscalar :: forall n . Vec n a -> Swizz -> a | ||
324 | swizzscalar (V2 x y) Sx = x | ||
325 | swizzscalar (V2 x y) Sy = y | ||
326 | swizzscalar (V3 x y z) Sx = x | ||
327 | swizzscalar (V3 x y z) Sy = y | ||
328 | swizzscalar (V3 x y z) Sz = z | ||
329 | swizzscalar (V4 x y z w) Sx = x | ||
330 | swizzscalar (V4 x y z w) Sy = y | ||
331 | swizzscalar (V4 x y z w) Sz = z | ||
332 | swizzscalar (V4 x y z w) Sw = w | ||
333 | |||
334 | -- used to prevent unfolding of swizzvector on variables (behind GPU lambda) | ||
335 | definedVec :: forall a m . Vec m a -> Bool | ||
336 | definedVec (V2 _ _) = True | ||
337 | definedVec (V3 _ _ _) = True | ||
338 | definedVec (V4 _ _ _ _) = True | ||
339 | |||
340 | swizzvector :: forall n . forall m . Vec n a -> Vec m Swizz -> Vec m a | ||
341 | swizzvector v w | definedVec v = mapVec (swizzscalar v) w | ||
342 | |||
343 | ----------------------------------------------------------------------------- | ||
344 | |||
159 | data BlendingFactor | 345 | data BlendingFactor |
160 | = Zero' --- FIXME: modified | 346 | = Zero' --- FIXME: modified |
161 | | One | 347 | | One |
@@ -244,15 +430,6 @@ data PointSpriteCoordOrigin | |||
244 | = LowerLeft | 430 | = LowerLeft |
245 | | UpperLeft | 431 | | UpperLeft |
246 | 432 | ||
247 | data ImageSemantics = Depth Type | Stencil Type | Color Type | ||
248 | |||
249 | data PrimitiveType | ||
250 | = Triangle | ||
251 | | Line | ||
252 | | Point | ||
253 | | TriangleAdjacency | ||
254 | | LineAdjacency | ||
255 | |||
256 | -- builtin | 433 | -- builtin |
257 | primTexture :: () -> Vec 2 Float -> Vec 4 Float | 434 | primTexture :: () -> Vec 2 Float -> Vec 4 Float |
258 | 435 | ||
@@ -265,13 +442,6 @@ data RasterContext a :: PrimitiveType -> Type where | |||
265 | PointCtx :: PointSize a -> Float -> PointSpriteCoordOrigin -> RasterContext a Point | 442 | PointCtx :: PointSize a -> Float -> PointSpriteCoordOrigin -> RasterContext a Point |
266 | LineCtx :: Float -> ProvokingVertex -> RasterContext a Line | 443 | LineCtx :: Float -> ProvokingVertex -> RasterContext a Line |
267 | 444 | ||
268 | map _ [] = [] | ||
269 | map f (x:xs) = f x : map f xs | ||
270 | |||
271 | type family ListElem a where ListElem [a] = a | ||
272 | |||
273 | type family HListElem a :: [Type] where HListElem (HList l) = l | ||
274 | |||
275 | data Blending :: Type -> Type where | 445 | data Blending :: Type -> Type where |
276 | NoBlending :: Blending t | 446 | NoBlending :: Blending t |
277 | BlendLogicOp :: (Integral t) => LogicOperation -> Blending t | 447 | BlendLogicOp :: (Integral t) => LogicOperation -> Blending t |
@@ -288,71 +458,6 @@ data FragmentOperation :: ImageSemantics -> Type where | |||
288 | DepthOp :: ComparisonFunction -> Bool -> FragmentOperation (Depth Float) | 458 | DepthOp :: ComparisonFunction -> Bool -> FragmentOperation (Depth Float) |
289 | StencilOp :: StencilTests -> StencilOps -> StencilOps -> FragmentOperation (Stencil Int32) | 459 | StencilOp :: StencilTests -> StencilOps -> StencilOps -> FragmentOperation (Stencil Int32) |
290 | 460 | ||
291 | [] ++ ys = ys | ||
292 | x:xs ++ ys = x : xs ++ ys | ||
293 | |||
294 | foldr f e [] = e | ||
295 | foldr f e (x: xs) = f x (foldr f e xs) | ||
296 | |||
297 | concat = foldr (++) [] | ||
298 | |||
299 | concatMap :: (a -> [b]) -> [a] -> [b] | ||
300 | concatMap f x = concat (map f x) | ||
301 | |||
302 | data Primitive a :: PrimitiveType -> Type where | ||
303 | PrimPoint :: a -> Primitive a Point | ||
304 | PrimLine :: a -> a -> Primitive a Line | ||
305 | PrimTriangle :: a -> a -> a -> Primitive a Triangle | ||
306 | |||
307 | type PrimitiveStream a t = [Primitive t a] | ||
308 | |||
309 | mapPrimitive :: (a -> b) -> Primitive a p -> Primitive b p | ||
310 | {- todo | ||
311 | mapPrimitive f (PrimPoint a) = PrimPoint (f a) | ||
312 | mapPrimitive f (PrimLine a b) = PrimLine (f a) (f b) | ||
313 | mapPrimitive f (PrimTriangle a b c) = PrimTriangle (f a) (f b) (f c) | ||
314 | -} | ||
315 | |||
316 | fetch :: forall a t . (AttributeTuple t) => String -> t -> PrimitiveStream a t | ||
317 | fetchArrays :: forall a t t' . (AttributeTuple t, t ~ HList (map ListElem (HListElem t'))) => t' -> PrimitiveStream a t | ||
318 | |||
319 | mapPrimitives :: (a -> b) -> PrimitiveStream p a -> PrimitiveStream p b | ||
320 | mapPrimitives f = map (mapPrimitive f) | ||
321 | |||
322 | ------------------- | ||
323 | |||
324 | data Maybe a | ||
325 | = Nothing | ||
326 | | Just a | ||
327 | -- deriving (Eq, Ord, Show) | ||
328 | |||
329 | data Vector (n :: Nat) t | ||
330 | |||
331 | type Fragment n t = Vector n (Maybe (SimpleFragment t)) | ||
332 | |||
333 | data SimpleFragment t = SimpleFragment | ||
334 | { sFragmentCoords :: Vec 3 Float | ||
335 | , sFragmentValue :: t | ||
336 | } | ||
337 | |||
338 | type FragmentStream n t = [Fragment n t] | ||
339 | |||
340 | customizeDepth :: (a -> Float) -> Fragment n a -> Fragment n a | ||
341 | |||
342 | customizeDepths :: (a -> Float) -> FragmentStream n a -> FragmentStream n a | ||
343 | customizeDepths f = map (customizeDepth f) | ||
344 | |||
345 | filterFragment :: (a -> Bool) -> Fragment n a -> Fragment n a | ||
346 | |||
347 | filterFragments :: (a -> Bool) -> FragmentStream n a -> FragmentStream n a | ||
348 | filterFragments p = map (filterFragment p) | ||
349 | |||
350 | mapFragment :: (a -> b) -> Fragment n a -> Fragment n b | ||
351 | |||
352 | mapFragments :: (a -> b) -> FragmentStream n a -> FragmentStream n b | ||
353 | mapFragments f = map (mapFragment f) | ||
354 | |||
355 | |||
356 | data Interpolated t where | 461 | data Interpolated t where |
357 | Smooth, NoPerspective | 462 | Smooth, NoPerspective |
358 | :: (Floating t) => Interpolated t | 463 | :: (Floating t) => Interpolated t |
@@ -368,13 +473,6 @@ rasterizePrimitive | |||
368 | 473 | ||
369 | rasterizePrimitives ctx is s = concat (map (rasterizePrimitive is ctx) s) | 474 | rasterizePrimitives ctx is s = concat (map (rasterizePrimitive is ctx) s) |
370 | 475 | ||
371 | data Image (n :: Nat) (t :: ImageSemantics) -- = Vector n [[t]] | ||
372 | |||
373 | ColorImage :: forall a d t color . (Num t, color ~ VecScalar d t) | ||
374 | => color -> Image a (Color color) | ||
375 | DepthImage :: forall a . Float -> Image a (Depth Float) | ||
376 | StencilImage :: forall a . Int -> Image a (Stencil Int) | ||
377 | |||
378 | type family ImageLC a :: Nat where ImageLC (Image n t) = n | 476 | type family ImageLC a :: Nat where ImageLC (Image n t) = n |
379 | 477 | ||
380 | allSame :: [a] -> Type | 478 | allSame :: [a] -> Type |
@@ -409,21 +507,21 @@ type family FragmentOperationSem a :: ImageSemantics where FragmentOperationSem | |||
409 | 507 | ||
410 | Accumulate :: forall (n :: Nat) (c :: [Type]) . (b ~ map FragmentOperationSem c) => HList c -> FragmentStream n (HList (remSemantics' b)) -> FrameBuffer n b -> FrameBuffer n b | 508 | Accumulate :: forall (n :: Nat) (c :: [Type]) . (b ~ map FragmentOperationSem c) => HList c -> FragmentStream n (HList (remSemantics' b)) -> FrameBuffer n b -> FrameBuffer n b |
411 | 509 | ||
412 | type family ImageSem a :: ImageSemantics where ImageSem (Image n t) = t | 510 | accumulateWith ctx x = (ctx, x) |
511 | overlay cl (ctx, str) = Accumulate ctx str cl | ||
413 | 512 | ||
414 | tfFrameBuffer t = map 'ImageSem t | 513 | infixl 0 `overlay` |
415 | 514 | ||
416 | class ValidFrameBuffer (a :: [ImageSemantics]) | 515 | type family ImageSem a :: ImageSemantics where ImageSem (Image n t) = t |
417 | instance ValidFrameBuffer a -- TODO | ||
418 | 516 | ||
419 | head (x: _) = x | 517 | --class ValidFrameBuffer (a :: [ImageSemantics]) |
518 | --instance ValidFrameBuffer a -- TODO | ||
420 | 519 | ||
421 | FrameBuffer :: forall (a :: [Type]) . (sameLayerCounts a) => HList a -> FrameBuffer (ImageLC (head a)) (tfFrameBuffer a) | 520 | FrameBuffer :: forall (a :: [Type]) . (sameLayerCounts a) => HList a -> FrameBuffer (ImageLC (head a)) (map ImageSem a) |
422 | 521 | ||
423 | accumulate ctx fshader fstr fb = Accumulate ctx (mapFragments fshader fstr) fb | 522 | imageFrame = FrameBuffer |
424 | 523 | ||
425 | -- todo: remove | 524 | accumulate ctx fshader fstr fb = Accumulate ctx (mapFragments fshader fstr) fb |
426 | accumulationContext x = x | ||
427 | 525 | ||
428 | -- texture support | 526 | -- texture support |
429 | PrjImage :: FrameBuffer 1 '[a] -> Image 1 a | 527 | PrjImage :: FrameBuffer 1 '[a] -> Image 1 a |
@@ -432,90 +530,7 @@ PrjImageColor :: FrameBuffer 1 '[Depth 'Float, Color '(Vec 4 Float)] -> Im | |||
432 | data Output where | 530 | data Output where |
433 | ScreenOut :: FrameBuffer a b -> Output | 531 | ScreenOut :: FrameBuffer a b -> Output |
434 | 532 | ||
435 | ------------------------------------------------------------------- | 533 | renderFrame = ScreenOut |
436 | -- * Builtin Primitive Functions * | ||
437 | -- Arithmetic Functions (componentwise) | ||
438 | |||
439 | PrimAdd, PrimSub, PrimMul :: Num (MatVecScalarElem a) => a -> a -> a | ||
440 | PrimAddS, PrimSubS, PrimMulS :: (t ~ MatVecScalarElem a, Num t) => a -> t -> a | ||
441 | PrimDiv, PrimMod :: (Num t, a ~ VecScalar d t) => a -> a -> a | ||
442 | PrimDivS, PrimModS :: (Num t, a ~ VecScalar d t) => a -> t -> a | ||
443 | PrimNeg :: Signed (MatVecScalarElem a) => a -> a | ||
444 | -- Bit-wise Functions | ||
445 | PrimBAnd, PrimBOr, PrimBXor :: (Integral t, a ~ VecScalar d t) => a -> a -> a | ||
446 | PrimBAndS, PrimBOrS, PrimBXorS:: (Integral t, a ~ VecScalar d t) => a -> t -> a | ||
447 | PrimBNot :: (Integral t, a ~ VecScalar d t) => a -> a | ||
448 | PrimBShiftL, PrimBShiftR :: (Integral t, a ~ VecScalar d t, b ~ VecScalar d Word) => a -> b -> a | ||
449 | PrimBShiftLS, PrimBShiftRS :: (Integral t, a ~ VecScalar d t) => a -> Word -> a | ||
450 | -- Logic Functions | ||
451 | PrimAnd, PrimOr, PrimXor :: Bool -> Bool -> Bool | ||
452 | PrimNot :: forall a d . (a ~ VecScalar d Bool) => a -> a | ||
453 | PrimAny, PrimAll :: VecScalar d Bool -> Bool | ||
454 | |||
455 | -- Angle, Trigonometry and Exponential Functions | ||
456 | PrimACos, PrimACosH, PrimASin, PrimASinH, PrimATan, PrimATanH, PrimCos, PrimCosH, PrimDegrees, PrimRadians, PrimSin, PrimSinH, PrimTan, PrimTanH, PrimExp, PrimLog, PrimExp2, PrimLog2, PrimSqrt, PrimInvSqrt | ||
457 | :: (a ~ VecScalar d Float) => a -> a | ||
458 | PrimPow, PrimATan2 :: (a ~ VecScalar d Float) => a -> a -> a | ||
459 | -- Common Functions | ||
460 | PrimFloor, PrimTrunc, PrimRound, PrimRoundEven, PrimCeil, PrimFract | ||
461 | :: (a ~ VecScalar d Float) => a -> a | ||
462 | PrimMin, PrimMax :: (Num t, a ~ VecScalar d t) => a -> a -> a | ||
463 | PrimMinS, PrimMaxS :: (Num t, a ~ VecScalar d t) => a -> t -> a | ||
464 | PrimIsNan, PrimIsInf :: (a ~ VecScalar d Float, b ~ VecScalar d Bool) => a -> b | ||
465 | PrimAbs, PrimSign :: (Signed t, a ~ VecScalar d t) => a -> a | ||
466 | PrimModF :: (a ~ VecScalar d Float) => a -> (a, a) | ||
467 | PrimClamp :: (Num t, a ~ VecScalar d t) => a -> a -> a -> a | ||
468 | PrimClampS :: (Num t, a ~ VecScalar d t) => a -> t -> t -> a | ||
469 | PrimMix :: (a ~ VecScalar d Float) => a -> a -> a -> a | ||
470 | PrimMixS :: (a ~ VecScalar d Float) => a -> a -> Float -> a | ||
471 | PrimMixB :: (a ~ VecScalar d Float, b ~ VecScalar d Bool) => a -> a -> b -> a | ||
472 | PrimStep :: (a ~ Vec d Float) => a -> a -> a | ||
473 | PrimStepS :: (a ~ VecScalar d Float) => Float -> a -> a | ||
474 | PrimSmoothStep :: (a ~ Vec d Float) => a -> a -> a -> a | ||
475 | PrimSmoothStepS :: (a ~ VecScalar d Float) => Float -> Float -> a -> a | ||
476 | |||
477 | -- Integer/Floatonversion Functions | ||
478 | PrimFloatBitsToInt :: VecScalar d Float -> VecScalar d Int | ||
479 | PrimFloatBitsToUInt :: VecScalar d Float -> VecScalar d Word | ||
480 | PrimIntBitsToFloat :: VecScalar d Int -> VecScalar d Float | ||
481 | PrimUIntBitsToFloat :: VecScalar d Word -> VecScalar d Float | ||
482 | -- Geometric Functions | ||
483 | PrimLength :: (a ~ VecScalar d Float) => a -> Float | ||
484 | PrimDistance, PrimDot :: (a ~ VecScalar d Float) => a -> a -> Float | ||
485 | PrimCross :: (a ~ VecScalar 3 Float) => a -> a -> a | ||
486 | PrimNormalize :: (a ~ VecScalar d Float) => a -> a | ||
487 | PrimFaceForward, PrimRefract :: (a ~ VecScalar d Float) => a -> a -> a -> a | ||
488 | PrimReflect :: (a ~ VecScalar d Float) => a -> a -> a | ||
489 | -- Matrix Functions | ||
490 | PrimTranspose :: Mat h w a -> Mat w h a | ||
491 | PrimDeterminant :: Mat s s a -> Float | ||
492 | PrimInverse :: Mat s s a -> Mat s s a | ||
493 | PrimOuterProduct :: Vec w a -> Vec h a -> Mat h w a | ||
494 | PrimMulMatVec :: Mat h w a -> Vec w a -> Vec h a | ||
495 | PrimMulVecMat :: Vec h a -> Mat h w a -> Vec w a | ||
496 | PrimMulMatMat :: Mat i j a -> Mat j k a -> Mat i k a | ||
497 | -- Vector and Scalar Relational Functions | ||
498 | PrimLessThan, PrimLessThanEqual, PrimGreaterThan, PrimGreaterThanEqual, PrimEqualV, PrimNotEqualV | ||
499 | :: forall a d t b . (Num t, a ~ VecScalar d t, b ~ VecScalar d Bool) => a -> a -> b | ||
500 | PrimEqual, PrimNotEqual :: forall a t . (t ~ MatVecScalarElem a) => a -> a -> Bool | ||
501 | -- Fragment Processing Functions | ||
502 | PrimDFdx, PrimDFdy, PrimFWidth | ||
503 | :: (a ~ VecScalar d Float) => a -> a | ||
504 | -- Noise Functions | ||
505 | PrimNoise1 :: VecScalar d Float -> Float | ||
506 | PrimNoise2 :: VecScalar d Float -> Vec 2 Float | ||
507 | PrimNoise3 :: VecScalar d Float -> Vec 3 Float | ||
508 | PrimNoise4 :: VecScalar d Float -> Vec 4 Float | ||
509 | |||
510 | {- | ||
511 | -- Vec/Mat (de)construction | ||
512 | PrimTupToV2 :: Component a => PrimFun stage ((a,a) -> V2 a) | ||
513 | PrimTupToV3 :: Component a => PrimFun stage ((a,a,a) -> V3 a) | ||
514 | PrimTupToV4 :: Component a => PrimFun stage ((a,a,a,a) -> V4 a) | ||
515 | PrimV2ToTup :: Component a => PrimFun stage (V2 a -> (a,a)) | ||
516 | PrimV3ToTup :: Component a => PrimFun stage (V3 a -> (a,a,a)) | ||
517 | PrimV4ToTup :: Component a => PrimFun stage (V4 a -> (a,a,a,a)) | ||
518 | -} | ||
519 | 534 | ||
520 | -------------------- | 535 | -------------------- |
521 | -- * Texture support | 536 | -- * Texture support |
@@ -544,12 +559,7 @@ data Sampler = Sampler Filter EdgeMode Texture | |||
544 | texture2D :: Sampler -> Vec 2 Float -> Vec 4 Float | 559 | texture2D :: Sampler -> Vec 2 Float -> Vec 4 Float |
545 | 560 | ||
546 | 561 | ||
547 | accumulateWith ctx x = (ctx, x) | 562 | -- todo: remove |
548 | overlay cl (ctx, str) = Accumulate ctx str cl | 563 | accumulationContext x = x |
549 | renderFrame = ScreenOut | ||
550 | imageFrame = FrameBuffer | ||
551 | emptyDepthImage = DepthImage @1 | ||
552 | emptyColorImage = ColorImage @1 | ||
553 | 564 | ||
554 | infixl 0 `overlay` | ||
555 | 565 | ||