summaryrefslogtreecommitdiff
path: root/lc
diff options
context:
space:
mode:
authorPéter Diviánszky <divipp@gmail.com>2016-01-16 05:20:24 +0100
committerPéter Diviánszky <divipp@gmail.com>2016-01-16 05:20:24 +0100
commit16beb27727151212e43b6c350a270b0ea89b6be6 (patch)
treef6c7d5ce914ce07602478397929de465e179d1b2 /lc
parent5dea3fb50fa19395aaf7012a00983a59bbf8f105 (diff)
Builtins.lc API refactoring
Diffstat (limited to 'lc')
-rw-r--r--lc/Builtins.lc140
1 files changed, 88 insertions, 52 deletions
diff --git a/lc/Builtins.lc b/lc/Builtins.lc
index d8ba4944..9886b177 100644
--- a/lc/Builtins.lc
+++ b/lc/Builtins.lc
@@ -16,9 +16,7 @@ data List a = Nil | Cons a (List a)
16 16
17type family JoinTupleType t1 t2 where 17type family JoinTupleType t1 t2 where
18 -- TODO 18 -- TODO
19 JoinTupleType () a = a
20 JoinTupleType a () = a 19 JoinTupleType a () = a
21 JoinTupleType (a, b) c = (a, b, c)
22 JoinTupleType a (b, c) = (a, b, c) 20 JoinTupleType a (b, c) = (a, b, c)
23 JoinTupleType a (b, c, d) = (a, b, c, d) 21 JoinTupleType a (b, c, d) = (a, b, c, d)
24 JoinTupleType a (b, c, d, e) = (a, b, c, d, e) 22 JoinTupleType a (b, c, d, e) = (a, b, c, d, e)
@@ -308,14 +306,6 @@ data Depth a where
308data Stencil a where 306data Stencil a where
309data Color a where 307data Color a where
310 308
311type family ColorRepr a where
312 ColorRepr () = ()
313 ColorRepr (a, b) = (Color a, Color b)
314 ColorRepr (a, b, c) = (Color a, Color b, Color c)
315 ColorRepr (a, b, c, d) = (Color a, Color b, Color c, Color d)
316 ColorRepr (a, b, c, d, e) = (Color a, Color b, Color c, Color d, Color e)
317 ColorRepr a = Color a -- TODO
318
319data PrimitiveType 309data PrimitiveType
320 = Triangle 310 = Triangle
321 | Line 311 | Line
@@ -330,13 +320,6 @@ primTexture :: () -> Vec 2 Float -> Vec 4 Float
330Uniform :: String -> t 320Uniform :: String -> t
331Attribute :: String -> t 321Attribute :: String -> t
332 322
333data FragmentShader :: Type -> Type where
334 FragmentShader :: (a ~ ColorRepr t) => (b -> t) -> FragmentShader (b -> a)
335 FragmentShaderDepth :: (x ~ ColorRepr t, a ~ JoinTupleType (Depth Float) x) => (b -> (Float, t))
336 -> FragmentShader (b -> a)
337 FragmentShaderRastDepth :: (x ~ ColorRepr t, a ~ JoinTupleType (Depth Float) x) => (b -> t)
338 -> FragmentShader (b -> a)
339
340data RasterContext :: PrimitiveType -> Type where 323data RasterContext :: PrimitiveType -> Type where
341 TriangleCtx :: CullMode -> PolygonMode -> PolygonOffset -> ProvokingVertex -> RasterContext Triangle 324 TriangleCtx :: CullMode -> PolygonMode -> PolygonOffset -> ProvokingVertex -> RasterContext Triangle
342 PointCtx :: PointSize -> Float -> PointSpriteCoordOrigin -> RasterContext Point 325 PointCtx :: PointSize -> Float -> PointSpriteCoordOrigin -> RasterContext Point
@@ -344,19 +327,19 @@ data RasterContext :: PrimitiveType -> Type where
344 327
345data Interpolated t where 328data Interpolated t where
346 Smooth, NoPerspective 329 Smooth, NoPerspective
347 :: (Floating t) => t -> Interpolated t 330 :: (Floating t) => Interpolated t
348 Flat :: t -> Interpolated t 331 Flat :: Interpolated t
349 332
350type family FTRepr' a where 333type family FTRepr' a where
351 -- TODO 334 -- TODO
352 FTRepr' [a] = a 335 FTRepr' [a] = a
353 FTRepr' ([a], [b]) = (a, b) 336 FTRepr' ([a], [b]) = (a, b)
354 FTRepr' (Interpolated a) = a
355 FTRepr' (Interpolated a, Interpolated b) = (a, b)
356 FTRepr' (Interpolated a, Interpolated b, Interpolated c) = (a, b, c)
357 337
358data VertexOut a where 338type family InterpolatedType a where
359 VertexOut :: (a ~ FTRepr' x) => Vec 4 Float -> Float -> (){-TODO-} -> x -> VertexOut a 339 InterpolatedType () = ()
340 InterpolatedType (Interpolated a) = a
341 InterpolatedType (Interpolated a, Interpolated b) = (a, b)
342 InterpolatedType (Interpolated a, Interpolated b, Interpolated c) = (a, b, c)
360 343
361data Blending :: Type -> Type where 344data Blending :: Type -> Type where
362 NoBlending :: Blending t 345 NoBlending :: Blending t
@@ -368,7 +351,6 @@ data Blending :: Type -> Type where
368{- TODO: more precise kinds 351{- TODO: more precise kinds
369 FragmentOperation :: Semantic -> * 352 FragmentOperation :: Semantic -> *
370 FragmentOut :: Semantic -> * 353 FragmentOut :: Semantic -> *
371 VertexOut :: ???
372-} 354-}
373 355
374data StencilTests 356data StencilTests
@@ -388,30 +370,89 @@ type family FragOps a where
388 FragOps (FragmentOperation t1, FragmentOperation t2, FragmentOperation t3, FragmentOperation t4) = (t1, t2, t3, t4) 370 FragOps (FragmentOperation t1, FragmentOperation t2, FragmentOperation t3, FragmentOperation t4) = (t1, t2, t3, t4)
389 FragOps (FragmentOperation t1, FragmentOperation t2, FragmentOperation t3, FragmentOperation t4, FragmentOperation t5) = (t1, t2, t3, t4, t5) 371 FragOps (FragmentOperation t1, FragmentOperation t2, FragmentOperation t3, FragmentOperation t4, FragmentOperation t5) = (t1, t2, t3, t4, t5)
390 372
391data FragmentFilter t where 373type family FragOps' a where
392 PassAll :: FragmentFilter t 374 FragOps' (t1, t2) = (FragmentOperation t1, FragmentOperation t2)
393 Filter :: (t -> Bool) -> FragmentFilter t 375 FragOps' (t1, t2, t3) = (FragmentOperation t1, FragmentOperation t2, FragmentOperation t3)
376 FragOps' (t1, t2, t3, t4) = (FragmentOperation t1, FragmentOperation t2, FragmentOperation t3, FragmentOperation t4)
377 FragOps' (t1, t2, t3, t4, t5) = (FragmentOperation t1, FragmentOperation t2, FragmentOperation t3, FragmentOperation t4, FragmentOperation t5)
378 FragOps' t = (FragmentOperation t)
379
380data Stream a
381
382mapStream :: (a -> b) -> Stream a -> Stream b
383filterStream :: (a -> Bool) -> Stream a -> Stream a
384
385data Primitive (a :: PrimitiveType) t
386
387mapPrimitive :: (a -> b) -> Primitive p a -> Primitive p b
388
389fetch_ :: forall a t . (AttributeTuple t) => String -> t -> Stream (Primitive a t)
390fetchArrays_ :: forall a t t' . (AttributeTuple t, t ~ FTRepr' t') => t' -> Stream (Primitive a t)
391
392mapPrimitives :: (t' -> t) -> Stream (Primitive a t') -> Stream (Primitive a t)
393mapPrimitives f = mapStream (mapPrimitive f)
394
395fetch s a t = fetch_ @a s t
396fetchArrays a t = fetchArrays_ @a t
397
398data DepthHandler = NoDepth | DefinedDepth
399{- todo: remove?
400type family AddSemantics (d :: DepthHandler) a where
401 AddSemantics 'NoDepth () = ()
402 AddSemantics 'NoDepth (a, b) = (Color a, Color b)
403 AddSemantics 'NoDepth (a, b, c) = (Color a, Color b, Color c)
404 AddSemantics 'NoDepth (a, b, c, d) = (Color a, Color b, Color c, Color d)
405 AddSemantics 'NoDepth (a, b, c, d, e) = (Color a, Color b, Color c, Color d, Color e)
406 AddSemantics 'NoDepth a = Color a -- TODO
407 AddSemantics 'DefinedDepth () = ()
408 AddSemantics 'DefinedDepth (a, b) = (Depth Float, Color a, Color b)
409 AddSemantics 'DefinedDepth (a, b, c) = (Depth Float, Color a, Color b, Color c)
410 AddSemantics 'DefinedDepth (a, b, c, d) = (Depth Float, Color a, Color b, Color c, Color d)
411-- AddSemantics 'DefinedDepth (a, b, c, d, e) = (Depth Float, Color a, Color b, Color c, Color d, Color e)
412 AddSemantics 'DefinedDepth a = (Depth Float, Color a) -- TODO
413-}
414type family RemSemantics a where
415 RemSemantics () = ()
416 RemSemantics (Color a) = a
417 RemSemantics (Color a, Color b) = (a, b)
418 RemSemantics (Color a, Color b, Color c) = (a, b, c)
419 RemSemantics (Color a, Color b, Color c, Color d) = (a, b, c, d)
420 RemSemantics (Color a, Color b, Color c, Color d, Color e) = (a, b, c, d, e)
421 RemSemantics (Depth Float) = ()
422 RemSemantics (Depth Float, Color a) = a
423 RemSemantics (Depth Float, Color a, Color b) = (a, b)
424 RemSemantics (Depth Float, Color a, Color b, Color c) = (a, b, c)
425 RemSemantics (Depth Float, Color a, Color b, Color c, Color d) = (a, b, c, d)
426-- RemSemantics 'DefinedDepth (a, b, c, d, e) = (Depth Float, Color a, Color b, Color c, Color d, Color e)
394 427
395data VertexStream (a :: PrimitiveType) t where 428 -- Render Operations
396 Fetch :: (AttributeTuple t) => String -> t -> VertexStream a t 429data Fragment :: Nat -> DepthHandler -> Type -> Type
397 FetchArrays :: (AttributeTuple t, t ~ FTRepr' t') => t' -> VertexStream a t
398 430
399fetch s a t = Fetch @a s t 431customizeDepth :: (a -> Float) -> Fragment n _ a -> Fragment n DefinedDepth a
400fetchArrays a t = FetchArrays @a t
401 432
402data PrimitiveStream (p :: PrimitiveType) :: Nat -> Type -> Type where 433customizeDepths f = mapStream (customizeDepth f)
403 Transform :: (a -> VertexOut b) -> VertexStream p a -> PrimitiveStream p 1 b 434{- todo: remove?
435noDepth :: Fragment n _ a -> Fragment n NoDepth a
404 436
405 -- Render Operations 437noDepths = mapStream noDepth
406data FragmentStream (n :: Nat) a where 438-}
407 Rasterize :: RasterContext x -> PrimitiveStream x n a -> FragmentStream n a 439
440rasterize_ :: (b ~ InterpolatedType y, a ~ JoinTupleType (Vec 4 Float) b)
441 => (a -> Float) -- point size
442 -> y -- tuple of Smooth & Flat
443 -> RasterContext x
444 -> Primitive x a -> Fragment 1 DefinedDepth b
445
446filterFragment :: (a -> Bool) -> Fragment n _ a -> Bool
447
448filterFragments :: (a -> Bool) -> Stream (Fragment n d a) -> Stream (Fragment n d a)
449filterFragments p = filterStream (filterFragment p)
450
451mapFragment :: (a -> b) -> Fragment n d a -> Fragment n d b
408 452
409data FilteredFragmentStream (n :: Nat) a where 453mapFragments f = mapStream (mapFragment f)
410 FilteredFragmentStream
411 :: FragmentFilter a -> FragmentStream n a -> FilteredFragmentStream n a
412 454
413data ShadedFragmentStream (n :: Nat) b where 455--data ShadedFragment :: Nat -> Type -> Type
414 ShadedFragmentStream :: ValidOutput b => FragmentShader (a -> b) -> FilteredFragmentStream n a -> ShadedFragmentStream n b
415 456
416-- todo: mutually defined with FrameBuffer and Image 457-- todo: mutually defined with FrameBuffer and Image
417type family TFFrameBuffer a {-where 458type family TFFrameBuffer a {-where
@@ -422,10 +463,10 @@ type family TFFrameBuffer a {-where
422type family SameLayerCounts a 463type family SameLayerCounts a
423 464
424data FrameBuffer (n :: Nat) b where 465data FrameBuffer (n :: Nat) b where
425 Accumulate :: (b ~ FragOps t) => t -> ShadedFragmentStream n b -> FrameBuffer n b -> FrameBuffer n b 466 Accumulate :: ({-x ~ FragOps' b, -}b ~ FragOps x) => x -> Stream (Fragment n d (RemSemantics b)) -> FrameBuffer n b -> FrameBuffer n b
426 FrameBuffer :: (ValidFrameBuffer b, SameLayerCounts a, FrameBuffer n b ~ TFFrameBuffer a) => a -> FrameBuffer n b 467 FrameBuffer :: (ValidFrameBuffer b, SameLayerCounts a, FrameBuffer n b ~ TFFrameBuffer a) => a -> FrameBuffer n b
427 468
428accumulate ctx ffilt fshader fstr fb = Accumulate ctx (ShadedFragmentStream fshader (FilteredFragmentStream ffilt fstr)) fb 469accumulate ctx fshader fstr fb = Accumulate ctx (mapFragments fshader fstr) fb
429 470
430accumulationContext x = x 471accumulationContext x = x
431 472
@@ -554,15 +595,10 @@ data Sampler = Sampler Filter EdgeMode Texture
554texture2D :: Sampler -> Vec 2 Float -> Vec 4 Float 595texture2D :: Sampler -> Vec 2 Float -> Vec 4 Float
555 596
556 597
557rasterize = Rasterize 598rasterizePrimitives ctx is = mapStream (rasterize_ (\_ -> 1) is ctx)
558filterFragmentStream = FilteredFragmentStream 599rasterizePrimitivesWithPointSize ctx ps is = mapStream (rasterize_ ps is ctx)
559transformFragmentsRastDepth f = ShadedFragmentStream (FragmentShaderRastDepth f)
560accumulateWith ctx x = (ctx, x) 600accumulateWith ctx x = (ctx, x)
561overlay cl (ctx, str) = Accumulate ctx str cl 601overlay cl (ctx, str) = Accumulate ctx str cl
562transformVertices0 f () s = Transform (\v -> VertexOut (f v) 1 () ()) s
563transformVertices1 f g1 s = Transform (\v -> VertexOut (f v) 1 () (Smooth (g1 v))) s
564transformVertices2 f (g1, g2) s = Transform (\v -> VertexOut (f v) 1 () (Smooth (g1 v), Smooth (g2 v))) s
565transformVertices3 f (g1, g2, g3) s = Transform (\v -> VertexOut (f v) 1 () (Smooth (g1 v), Smooth (g2 v), Smooth (g3 v))) s
566renderFrame = ScreenOut 602renderFrame = ScreenOut
567imageFrame = FrameBuffer 603imageFrame = FrameBuffer
568emptyDepthImage = DepthImage @1 604emptyDepthImage = DepthImage @1