Skip to content

Commit ae182d8

Browse files
authored
Merge pull request #16 from walseb/shaderdata-fir
Added ShaderData instance of FIR V & M
2 parents ad1c583 + 35976be commit ae182d8

File tree

1 file changed

+73
-1
lines changed
  • ghengin-core/ghengin-core-indep/Ghengin/Core

1 file changed

+73
-1
lines changed

ghengin-core/ghengin-core-indep/Ghengin/Core/Shader.hs

Lines changed: 73 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,12 @@ import Geomancy.Vec2
1818
import Geomancy.Vec3
1919
import Geomancy.Vec4
2020
import Geomancy.Mat4
21+
import Foreign.Ptr.Diff (pokeDiffOff, peekDiffOff)
22+
import Foreign.Storable as Store
2123

22-
import Ghengin.Core.Prelude (Float, Generic)
24+
import Ghengin.Core.Prelude (Float, Generic, ($), undefined)
25+
26+
import Control.Monad.IO.Class (liftIO)
2327

2428
import Math.Linear
2529
import qualified FIR
@@ -111,3 +115,71 @@ instance KnownSymbol name => ShaderData (StructMat4 name) where
111115

112116
instance KnownSymbol name => ShaderData (StructFloat name) where
113117
type FirType (StructFloat name) = FIR.Struct '[ name 'FIR.:-> Float ]
118+
119+
-- ** FIR Vector
120+
instance (KnownNat n, Storable x, Block x) => Block (V n x) where
121+
type PackedSize (V n x) = n * (PackedSize x)
122+
123+
alignment140 _ = Store.alignment (undefined :: V n x)
124+
alignment430 = alignment140
125+
126+
sizeOf140 _ = Store.sizeOf (undefined :: V n x)
127+
sizeOf430 = sizeOf140
128+
sizeOfPacked = sizeOf140
129+
130+
read140 p o = liftIO $ peekDiffOff p o
131+
read430 = read140
132+
readPacked = read140
133+
134+
write140 p o a = liftIO $ pokeDiffOff p o a
135+
write430 = write140
136+
writePacked = write140
137+
138+
instance (KnownNat n, Block x, Storable x) => ShaderData (V n x) where
139+
type FirType (V n x) = V n x
140+
141+
type StructV :: Nat -> Type -> Symbol -> Type
142+
newtype StructV n x name = StructV (V n x)
143+
deriving Generic
144+
145+
instance (KnownNat n, Block x, Storable x, KnownSymbol name) => Block (StructV n x name) where
146+
-- TODO: These functions can't be derived currently. Perhaps those default functions should be improved upstream.
147+
type PackedSize (StructV n x name) = PackedSize (V n x)
148+
sizeOfPacked = sizeOf140
149+
150+
instance (KnownNat n, Block x, Storable x, KnownSymbol name) => ShaderData (StructV n x name) where
151+
type FirType (StructV n x name) = FIR.Struct '[ name 'FIR.:-> V n x ]
152+
153+
-- ** FIR Matrix
154+
instance (KnownNat m, KnownNat n, Storable x, Block x) => Block (M m n x) where
155+
type PackedSize (M m n x) = m * n * (PackedSize x)
156+
157+
alignment140 _ = Store.alignment (undefined :: M m n x)
158+
alignment430 = alignment140
159+
160+
sizeOf140 _ = Store.sizeOf (undefined :: M m n x)
161+
sizeOf430 = sizeOf140
162+
sizeOfPacked = sizeOf140
163+
164+
read140 p o = liftIO $ peekDiffOff p o
165+
read430 = read140
166+
readPacked = read140
167+
168+
write140 p o a = liftIO $ pokeDiffOff p o a
169+
write430 = write140
170+
writePacked = write140
171+
172+
instance (KnownNat m, KnownNat n, Block x, Storable x) => ShaderData (M m n x) where
173+
type FirType (M m n x) = M m n x
174+
175+
type StructM :: Nat -> Nat -> Type -> Symbol -> Type
176+
newtype StructM m n x name = StructM (M m n x)
177+
deriving Generic
178+
179+
instance (KnownNat m, KnownNat n, Block x, Storable x, KnownSymbol name) => Block (StructM m n x name) where
180+
-- TODO: These functions can't be derived currently. Perhaps those default functions should be improved upstream.
181+
type PackedSize (StructM m n x name) = PackedSize (M m n x)
182+
sizeOfPacked = sizeOf140
183+
184+
instance (KnownNat m, KnownNat n, Block x, Storable x, KnownSymbol name) => ShaderData (StructM m n x name) where
185+
type FirType (StructM m n x name) = FIR.Struct '[ name 'FIR.:-> M m n x ]

0 commit comments

Comments
 (0)