diff options
author | Joe Crayne <joe@jerkface.net> | 2019-05-19 17:44:28 -0400 |
---|---|---|
committer | Joe Crayne <joe@jerkface.net> | 2019-05-19 17:44:28 -0400 |
commit | 268aaced12086ed8376affdf62711abcb651277e (patch) | |
tree | 5dbba3fd4192be3579bdf359e880d6ed6c0773bc | |
parent | ccf714a691a95be84ba168a725b6f0187bb903ea (diff) |
Experiment: use Data.Data to generate AttributeKeys.
-rw-r--r-- | AttributeData.hs | 91 | ||||
-rw-r--r-- | MaskableStream.hs | 16 |
2 files changed, 106 insertions, 1 deletions
diff --git a/AttributeData.hs b/AttributeData.hs new file mode 100644 index 0000000..2b2bc96 --- /dev/null +++ b/AttributeData.hs | |||
@@ -0,0 +1,91 @@ | |||
1 | {-# LANGUAGE DataKinds #-} | ||
2 | {-# LANGUAGE DeriveDataTypeable #-} | ||
3 | {-# LANGUAGE GADTs #-} | ||
4 | {-# LANGUAGE LambdaCase #-} | ||
5 | {-# LANGUAGE PartialTypeSignatures #-} | ||
6 | {-# LANGUAGE RecordWildCards #-} | ||
7 | {-# LANGUAGE ScopedTypeVariables #-} | ||
8 | {-# LANGUAGE TypeOperators #-} | ||
9 | {-# LANGUAGE ConstraintKinds #-} | ||
10 | |||
11 | module AttributeData where | ||
12 | |||
13 | import Control.Monad | ||
14 | import Data.Data | ||
15 | import Data.Foldable | ||
16 | import Data.Function | ||
17 | import Data.Int | ||
18 | import Data.IORef | ||
19 | import Data.Maybe | ||
20 | import Data.Some | ||
21 | import Data.Word | ||
22 | import qualified Data.Map.Strict as Map | ||
23 | import qualified Data.Vector as V | ||
24 | ;import Data.Vector as V ((!),(//)) | ||
25 | import Foreign.C.Types (CPtrdiff) | ||
26 | import Foreign.Marshal | ||
27 | import Foreign.Ptr | ||
28 | import Foreign.Storable | ||
29 | |||
30 | import LambdaCube.GL as LC | ||
31 | import LambdaCube.GL.Data -- (uploadCubeMapToGPU,uploadTextureBufferToGPU,updateTextureBuffer) | ||
32 | import LambdaCube.GL.Mesh as LC | ||
33 | import LambdaCube.GL.Type | ||
34 | import LambdaCube.IR as LC | ||
35 | import LambdaCube.GL.Util | ||
36 | import LambdaCube.GL.Input.Type | ||
37 | import LambdaCube.GL.Input hiding (createObjectCommands) | ||
38 | |||
39 | |||
40 | import MaskableStream | ||
41 | |||
42 | data Witness c = c => Witness | ||
43 | |||
44 | tagTypable :: TypeTag c -> Witness (Typeable c) | ||
45 | tagTypable TypeBool = Witness | ||
46 | tagTypable TypeV2B = Witness | ||
47 | tagTypable TypeV3B = Witness | ||
48 | tagTypable TypeV4B = Witness | ||
49 | tagTypable TypeWord = Witness | ||
50 | tagTypable TypeV2U = Witness | ||
51 | tagTypable TypeV3U = Witness | ||
52 | tagTypable TypeV4U = Witness | ||
53 | tagTypable TypeInt = Witness | ||
54 | tagTypable TypeV2I = Witness | ||
55 | tagTypable TypeV3I = Witness | ||
56 | tagTypable TypeV4I = Witness | ||
57 | tagTypable TypeFloat = Witness | ||
58 | tagTypable TypeV2F = Witness | ||
59 | tagTypable TypeV3F = Witness | ||
60 | tagTypable TypeV4F = Witness | ||
61 | tagTypable TypeM22F = Witness | ||
62 | tagTypable TypeM23F = Witness | ||
63 | tagTypable TypeM24F = Witness | ||
64 | tagTypable TypeM32F = Witness | ||
65 | tagTypable TypeM33F = Witness | ||
66 | tagTypable TypeM34F = Witness | ||
67 | tagTypable TypeM42F = Witness | ||
68 | tagTypable TypeM43F = Witness | ||
69 | tagTypable TypeM44F = Witness | ||
70 | |||
71 | |||
72 | foo :: forall attrkeys. Data attrkeys => (String -> Maybe (Some AttributeKey)) -> Maybe attrkeys | ||
73 | foo lookupA = do | ||
74 | let dt = dataTypeOf (error "dataTypeOf attrkeys" :: attrkeys) | ||
75 | case dataTypeRep dt of | ||
76 | AlgRep (c:_) -> do | ||
77 | let fields = constrFields c | ||
78 | mkb :: Data k => String -> k | ||
79 | mkb n = fix $ \v -> | ||
80 | fromMaybe (error $ "mkb " ++ n) $ case lookupA n of | ||
81 | Just (This k@(AttributeKey tt _ _)) -> case tagTypable tt of | ||
82 | Witness -> cast k | ||
83 | _ -> Nothing | ||
84 | z :: x -> (,) [String] x | ||
85 | z r = (fields, r) | ||
86 | k :: Data b => (,) [String] (b -> r) -> (,) [String] r | ||
87 | k (n:ns,btor) = (ns,btor (mkb n)) | ||
88 | (_,ks) = gunfold k z c :: (,) [String] attrkeys | ||
89 | Just ks | ||
90 | _ -> Nothing | ||
91 | |||
diff --git a/MaskableStream.hs b/MaskableStream.hs index 5274d28..10f4bff 100644 --- a/MaskableStream.hs +++ b/MaskableStream.hs | |||
@@ -1,4 +1,4 @@ | |||
1 | {-# LANGUAGE LambdaCase, RecordWildCards, KindSignatures, GADTs #-} | 1 | {-# LANGUAGE LambdaCase, RecordWildCards, KindSignatures, GADTs, DeriveDataTypeable, StandaloneDeriving #-} |
2 | 2 | ||
3 | -- TODO: Formulate this module as a patch against lambdacube-gl. | 3 | -- TODO: Formulate this module as a patch against lambdacube-gl. |
4 | 4 | ||
@@ -7,6 +7,7 @@ module MaskableStream where | |||
7 | import Control.Monad | 7 | import Control.Monad |
8 | import Control.Monad.IO.Class | 8 | import Control.Monad.IO.Class |
9 | import Control.Monad.Writer | 9 | import Control.Monad.Writer |
10 | import Data.Data | ||
10 | import Data.Foldable | 11 | import Data.Foldable |
11 | import Data.Function | 12 | import Data.Function |
12 | import Data.Int | 13 | import Data.Int |
@@ -232,6 +233,8 @@ incrementalUpdateBuffer b byteoffset bytecount ptr = do | |||
232 | glBufferSubData GL_ARRAY_BUFFER byteoffset bytecount ptr | 233 | glBufferSubData GL_ARRAY_BUFFER byteoffset bytecount ptr |
233 | glBindBuffer GL_ARRAY_BUFFER 0 | 234 | glBindBuffer GL_ARRAY_BUFFER 0 |
234 | 235 | ||
236 | instance Typeable c => Data (AttributeKey c) where | ||
237 | |||
235 | data AttributeKey c = AttributeKey (TypeTag c) Buffer CPtrdiff | 238 | data AttributeKey c = AttributeKey (TypeTag c) Buffer CPtrdiff |
236 | 239 | ||
237 | attributeKey :: TypeTagable c => GPUData -> String -> Maybe (AttributeKey c) | 240 | attributeKey :: TypeTagable c => GPUData -> String -> Maybe (AttributeKey c) |
@@ -249,6 +252,17 @@ attributeKey dta name = do | |||
249 | Just $ AttributeKey typ b (fromIntegral off) | 252 | Just $ AttributeKey typ b (fromIntegral off) |
250 | _ -> Nothing | 253 | _ -> Nothing |
251 | 254 | ||
255 | lookupAttributeKey :: GPUData -> String -> Maybe (Some AttributeKey) | ||
256 | lookupAttributeKey dta name = do | ||
257 | stream <- Map.lookup name (dStreams dta) | ||
258 | case stream of | ||
259 | Stream t b i _ _ -> do | ||
260 | let a = bufArrays b ! i | ||
261 | off = arrOffset a | ||
262 | This tt <- witnessType (fromStreamType t) | ||
263 | Just $ This (AttributeKey tt b (fromIntegral off)) | ||
264 | _ -> Nothing | ||
265 | |||
252 | (@<-) :: GLData a c => AttributeKey c -> a -> Writer [DSum AttributeKey GLUniformValue] () | 266 | (@<-) :: GLData a c => AttributeKey c -> a -> Writer [DSum AttributeKey GLUniformValue] () |
253 | k @<- v = tell [k :=> GLUniformValue v] | 267 | k @<- v = tell [k :=> GLUniformValue v] |
254 | 268 | ||