diff options
author | Péter Diviánszky <divipp@gmail.com> | 2016-01-16 05:20:24 +0100 |
---|---|---|
committer | Péter Diviánszky <divipp@gmail.com> | 2016-01-16 05:20:24 +0100 |
commit | 16beb27727151212e43b6c350a270b0ea89b6be6 (patch) | |
tree | f6c7d5ce914ce07602478397929de465e179d1b2 /lc | |
parent | 5dea3fb50fa19395aaf7012a00983a59bbf8f105 (diff) |
Builtins.lc API refactoring
Diffstat (limited to 'lc')
-rw-r--r-- | lc/Builtins.lc | 140 |
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 | ||
17 | type family JoinTupleType t1 t2 where | 17 | type 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 | |||
308 | data Stencil a where | 306 | data Stencil a where |
309 | data Color a where | 307 | data Color a where |
310 | 308 | ||
311 | type 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 | |||
319 | data PrimitiveType | 309 | data PrimitiveType |
320 | = Triangle | 310 | = Triangle |
321 | | Line | 311 | | Line |
@@ -330,13 +320,6 @@ primTexture :: () -> Vec 2 Float -> Vec 4 Float | |||
330 | Uniform :: String -> t | 320 | Uniform :: String -> t |
331 | Attribute :: String -> t | 321 | Attribute :: String -> t |
332 | 322 | ||
333 | data 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 | |||
340 | data RasterContext :: PrimitiveType -> Type where | 323 | data 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 | ||
345 | data Interpolated t where | 328 | data 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 | ||
350 | type family FTRepr' a where | 333 | type 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 | ||
358 | data VertexOut a where | 338 | type 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 | ||
361 | data Blending :: Type -> Type where | 344 | data 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 | ||
374 | data StencilTests | 356 | data 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 | ||
391 | data FragmentFilter t where | 373 | type 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 | |||
380 | data Stream a | ||
381 | |||
382 | mapStream :: (a -> b) -> Stream a -> Stream b | ||
383 | filterStream :: (a -> Bool) -> Stream a -> Stream a | ||
384 | |||
385 | data Primitive (a :: PrimitiveType) t | ||
386 | |||
387 | mapPrimitive :: (a -> b) -> Primitive p a -> Primitive p b | ||
388 | |||
389 | fetch_ :: forall a t . (AttributeTuple t) => String -> t -> Stream (Primitive a t) | ||
390 | fetchArrays_ :: forall a t t' . (AttributeTuple t, t ~ FTRepr' t') => t' -> Stream (Primitive a t) | ||
391 | |||
392 | mapPrimitives :: (t' -> t) -> Stream (Primitive a t') -> Stream (Primitive a t) | ||
393 | mapPrimitives f = mapStream (mapPrimitive f) | ||
394 | |||
395 | fetch s a t = fetch_ @a s t | ||
396 | fetchArrays a t = fetchArrays_ @a t | ||
397 | |||
398 | data DepthHandler = NoDepth | DefinedDepth | ||
399 | {- todo: remove? | ||
400 | type 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 | -} | ||
414 | type 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 | ||
395 | data VertexStream (a :: PrimitiveType) t where | 428 | -- Render Operations |
396 | Fetch :: (AttributeTuple t) => String -> t -> VertexStream a t | 429 | data Fragment :: Nat -> DepthHandler -> Type -> Type |
397 | FetchArrays :: (AttributeTuple t, t ~ FTRepr' t') => t' -> VertexStream a t | ||
398 | 430 | ||
399 | fetch s a t = Fetch @a s t | 431 | customizeDepth :: (a -> Float) -> Fragment n _ a -> Fragment n DefinedDepth a |
400 | fetchArrays a t = FetchArrays @a t | ||
401 | 432 | ||
402 | data PrimitiveStream (p :: PrimitiveType) :: Nat -> Type -> Type where | 433 | customizeDepths f = mapStream (customizeDepth f) |
403 | Transform :: (a -> VertexOut b) -> VertexStream p a -> PrimitiveStream p 1 b | 434 | {- todo: remove? |
435 | noDepth :: Fragment n _ a -> Fragment n NoDepth a | ||
404 | 436 | ||
405 | -- Render Operations | 437 | noDepths = mapStream noDepth |
406 | data FragmentStream (n :: Nat) a where | 438 | -} |
407 | Rasterize :: RasterContext x -> PrimitiveStream x n a -> FragmentStream n a | 439 | |
440 | rasterize_ :: (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 | |||
446 | filterFragment :: (a -> Bool) -> Fragment n _ a -> Bool | ||
447 | |||
448 | filterFragments :: (a -> Bool) -> Stream (Fragment n d a) -> Stream (Fragment n d a) | ||
449 | filterFragments p = filterStream (filterFragment p) | ||
450 | |||
451 | mapFragment :: (a -> b) -> Fragment n d a -> Fragment n d b | ||
408 | 452 | ||
409 | data FilteredFragmentStream (n :: Nat) a where | 453 | mapFragments f = mapStream (mapFragment f) |
410 | FilteredFragmentStream | ||
411 | :: FragmentFilter a -> FragmentStream n a -> FilteredFragmentStream n a | ||
412 | 454 | ||
413 | data 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 |
417 | type family TFFrameBuffer a {-where | 458 | type family TFFrameBuffer a {-where |
@@ -422,10 +463,10 @@ type family TFFrameBuffer a {-where | |||
422 | type family SameLayerCounts a | 463 | type family SameLayerCounts a |
423 | 464 | ||
424 | data FrameBuffer (n :: Nat) b where | 465 | data 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 | ||
428 | accumulate ctx ffilt fshader fstr fb = Accumulate ctx (ShadedFragmentStream fshader (FilteredFragmentStream ffilt fstr)) fb | 469 | accumulate ctx fshader fstr fb = Accumulate ctx (mapFragments fshader fstr) fb |
429 | 470 | ||
430 | accumulationContext x = x | 471 | accumulationContext x = x |
431 | 472 | ||
@@ -554,15 +595,10 @@ data Sampler = Sampler Filter EdgeMode Texture | |||
554 | texture2D :: Sampler -> Vec 2 Float -> Vec 4 Float | 595 | texture2D :: Sampler -> Vec 2 Float -> Vec 4 Float |
555 | 596 | ||
556 | 597 | ||
557 | rasterize = Rasterize | 598 | rasterizePrimitives ctx is = mapStream (rasterize_ (\_ -> 1) is ctx) |
558 | filterFragmentStream = FilteredFragmentStream | 599 | rasterizePrimitivesWithPointSize ctx ps is = mapStream (rasterize_ ps is ctx) |
559 | transformFragmentsRastDepth f = ShadedFragmentStream (FragmentShaderRastDepth f) | ||
560 | accumulateWith ctx x = (ctx, x) | 600 | accumulateWith ctx x = (ctx, x) |
561 | overlay cl (ctx, str) = Accumulate ctx str cl | 601 | overlay cl (ctx, str) = Accumulate ctx str cl |
562 | transformVertices0 f () s = Transform (\v -> VertexOut (f v) 1 () ()) s | ||
563 | transformVertices1 f g1 s = Transform (\v -> VertexOut (f v) 1 () (Smooth (g1 v))) s | ||
564 | transformVertices2 f (g1, g2) s = Transform (\v -> VertexOut (f v) 1 () (Smooth (g1 v), Smooth (g2 v))) s | ||
565 | transformVertices3 f (g1, g2, g3) s = Transform (\v -> VertexOut (f v) 1 () (Smooth (g1 v), Smooth (g2 v), Smooth (g3 v))) s | ||
566 | renderFrame = ScreenOut | 602 | renderFrame = ScreenOut |
567 | imageFrame = FrameBuffer | 603 | imageFrame = FrameBuffer |
568 | emptyDepthImage = DepthImage @1 | 604 | emptyDepthImage = DepthImage @1 |