diff options
author | Joe Crayne <joe@jerkface.net> | 2019-05-19 21:28:40 -0400 |
---|---|---|
committer | Joe Crayne <joe@jerkface.net> | 2019-05-19 21:28:40 -0400 |
commit | eb02d7ac3e47cba80a1701fc4d755073941e02dd (patch) | |
tree | 2e7fd326e42c89b292f534c1899cee70c780e2be | |
parent | e2478be6145d564008bbb948b7b9fc9a9d8b1716 (diff) |
Vertex attributes: Use relfection to obtain Parameter list.
-rw-r--r-- | AttributeData.hs | 99 |
1 files changed, 88 insertions, 11 deletions
diff --git a/AttributeData.hs b/AttributeData.hs index 86cb1e4..8e149af 100644 --- a/AttributeData.hs +++ b/AttributeData.hs | |||
@@ -26,6 +26,7 @@ import Foreign.C.Types (CPtrdiff) | |||
26 | import Foreign.Marshal | 26 | import Foreign.Marshal |
27 | import Foreign.Ptr | 27 | import Foreign.Ptr |
28 | import Foreign.Storable | 28 | import Foreign.Storable |
29 | import qualified Type.Reflection as R | ||
29 | 30 | ||
30 | import LambdaCube.GL as LC | 31 | import LambdaCube.GL as LC |
31 | import LambdaCube.GL.Data -- (uploadCubeMapToGPU,uploadTextureBufferToGPU,updateTextureBuffer) | 32 | import LambdaCube.GL.Data -- (uploadCubeMapToGPU,uploadTextureBufferToGPU,updateTextureBuffer) |
@@ -70,8 +71,8 @@ tagTypable TypeM43F = Witness | |||
70 | tagTypable TypeM44F = Witness | 71 | tagTypable TypeM44F = Witness |
71 | 72 | ||
72 | 73 | ||
73 | foo :: forall attrkeys. Data attrkeys => (String -> Maybe (Some AttributeKey)) -> Maybe attrkeys | 74 | lookupAttrKeys :: forall attrkeys. Data attrkeys => (String -> Maybe (Some AttributeKey)) -> Maybe attrkeys |
74 | foo lookupA = do | 75 | lookupAttrKeys lookupA = do |
75 | let dt = dataTypeOf (error "dataTypeOf attrkeys" :: attrkeys) | 76 | let dt = dataTypeOf (error "dataTypeOf attrkeys" :: attrkeys) |
76 | case dataTypeRep dt of | 77 | case dataTypeRep dt of |
77 | AlgRep (c:_) -> do | 78 | AlgRep (c:_) -> do |
@@ -81,14 +82,90 @@ foo lookupA = do | |||
81 | Just (This k@(AttributeKey tt _ _)) -> case tagTypable tt of | 82 | Just (This k@(AttributeKey tt _ _)) -> case tagTypable tt of |
82 | Witness -> maybe mzero return $ cast k | 83 | Witness -> maybe mzero return $ cast k |
83 | _ -> mzero | 84 | _ -> mzero |
84 | z :: x -> StateT [String] Maybe x | 85 | evalStateT (do { n:ns <- get ; put ns ; mkb n } `fromConstrM` c) |
85 | z r = put fields >> return r | 86 | fields |
86 | k :: Data b => StateT [String] Maybe (b -> r) -> StateT [String] Maybe r | ||
87 | k c = do btor <- c | ||
88 | n:ns <- get | ||
89 | put ns | ||
90 | a <- mkb n | ||
91 | return (btor a) | ||
92 | evalStateT (gunfold k z c) [] -- :: (,) [String] attrkeys | ||
93 | _ -> Nothing | 87 | _ -> Nothing |
94 | 88 | ||
89 | |||
90 | reflectDim :: Num a => R.SomeTypeRep -> Maybe a | ||
91 | reflectDim r = case () of | ||
92 | () | r==R.someTypeRep (Proxy :: Proxy 1) -> Just 1 | ||
93 | | r==R.someTypeRep (Proxy :: Proxy 2) -> Just 2 | ||
94 | | r==R.someTypeRep (Proxy :: Proxy 3) -> Just 3 | ||
95 | | r==R.someTypeRep (Proxy :: Proxy 4) -> Just 4 | ||
96 | | otherwise -> Nothing | ||
97 | |||
98 | reflectPrim :: R.SomeTypeRep -> Maybe (Some GLPointerType) | ||
99 | reflectPrim r = case () of | ||
100 | () | r==R.someTypeRep (Proxy :: Proxy Float) -> Just (This GLPrimFloat) | ||
101 | | r==R.someTypeRep (Proxy :: Proxy Int32) -> Just (This GLPrimInt) | ||
102 | | r==R.someTypeRep (Proxy :: Proxy Word32) -> Just (This GLPrimUInt) | ||
103 | | otherwise -> Nothing | ||
104 | |||
105 | |||
106 | reflectVectorType :: Typeable a => proxy a -> Maybe (Some TypeTag) | ||
107 | reflectVectorType proxy = case R.someTypeRep proxy of | ||
108 | R.SomeTypeRep r -> case R.splitApps r of | ||
109 | (v,[c,a]) -> do | ||
110 | cols <- reflectDim c | ||
111 | This p <- reflectPrim a | ||
112 | Just $ case p of | ||
113 | GLPrimUInt -> case cols of | ||
114 | 1 -> This TypeWord | ||
115 | 2 -> This TypeV2U | ||
116 | 3 -> This TypeV3U | ||
117 | 4 -> This TypeV4U | ||
118 | GLPrimInt -> case cols of | ||
119 | 1 -> This TypeInt | ||
120 | 2 -> This TypeV2I | ||
121 | 3 -> This TypeV3I | ||
122 | 4 -> This TypeV4I | ||
123 | GLPrimFloat -> case cols of | ||
124 | 1 -> This TypeFloat | ||
125 | 2 -> This TypeV2F | ||
126 | 3 -> This TypeV3F | ||
127 | 4 -> This TypeV4F | ||
128 | (m,[r,c,a]) -> do | ||
129 | rows <- reflectDim r | ||
130 | cols <- reflectDim c | ||
131 | This p <- reflectPrim a | ||
132 | case p of | ||
133 | GLPrimFloat -> case cols of | ||
134 | 2 -> case rows of | ||
135 | 2 -> Just $ This TypeM22F | ||
136 | 3 -> Just $ This TypeM23F | ||
137 | 4 -> Just $ This TypeM24F | ||
138 | _ -> Nothing | ||
139 | 3 -> case rows of | ||
140 | 2 -> Just $ This TypeM32F | ||
141 | 3 -> Just $ This TypeM33F | ||
142 | 4 -> Just $ This TypeM34F | ||
143 | _ -> Nothing | ||
144 | 4 -> case rows of | ||
145 | 2 -> Just $ This TypeM42F | ||
146 | 3 -> Just $ This TypeM43F | ||
147 | 4 -> Just $ This TypeM44F | ||
148 | _ -> Nothing | ||
149 | _ -> Nothing | ||
150 | _ -> Nothing | ||
151 | |||
152 | fieldParameters :: forall attrkeys proxy. Data attrkeys => proxy attrkeys -> (String -> String) -> [Parameter] | ||
153 | fieldParameters proxy toAttrName = do | ||
154 | let dt = dataTypeOf (error "dataTypeOf attrkeys" :: attrkeys) | ||
155 | case dataTypeRep dt of | ||
156 | AlgRep (c:_) -> do | ||
157 | let fields = constrFields c | ||
158 | mkb :: (MonadPlus m, Data k) => p k -> String -> m InputType | ||
159 | mkb pxy n = case reflectVectorType proxy of | ||
160 | Just (This tt) -> return $ unwitnessType tt | ||
161 | _ -> mzero | ||
162 | go :: Data c => StateT ([String],[Parameter]) Maybe c | ||
163 | go = do (n:ns,ps) <- get | ||
164 | fix $ \pxy -> do | ||
165 | t <- mkb pxy n | ||
166 | put (ns,Parameter (toAttrName n) t:ps) | ||
167 | return undefined | ||
168 | case runStateT (go `fromConstrM` c) (fields,[]) of | ||
169 | Just (x,(_,ps)) -> ps where _ = x :: attrkeys | ||
170 | Nothing -> [] | ||
171 | _ -> [] | ||