forked from expipiplus1/vulkan
/
Reflect.hs
437 lines (383 loc) · 16.4 KB
/
Reflect.hs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
{-# LANGUAGE NamedFieldPuns #-}
{-# LANGUAGE TupleSections #-}
{-# LANGUAGE LambdaCase #-}
{-# LANGUAGE RecordWildCards #-}
{-# LANGUAGE TypeOperators #-}
{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE AllowAmbiguousTypes #-}
{-# LANGUAGE TypeApplications #-}
{-# LANGUAGE DataKinds #-}
{-# LANGUAGE DeriveGeneric #-}
{-# LANGUAGE DeriveAnyClass #-}
{-# LANGUAGE DuplicateRecordFields #-}
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE OverloadedLists #-}
module Vulkan.Utils.Reflect
( ShaderStage (..)
, ShaderInfo (..)
, reflection
, reflection'
, makeShaderInfo
, makeDescriptorInfo
, makeInputInfo
) where
import Vulkan.Zero (zero)
import Vulkan.CStruct.Extends (SomeStruct (..))
import Vulkan.NamedType ((:::))
import Vulkan.Core10.Shader (ShaderModuleCreateInfo (..), ShaderModule (..))
import Vulkan.Core10.Enums.Format (Format (..))
import Vulkan.Core10.Enums.DescriptorType (DescriptorType (..))
import Vulkan.Core10.Enums.VertexInputRate (VertexInputRate (..))
import Vulkan.Core10.DescriptorSet (DescriptorSetLayoutBinding (..), DescriptorSetLayoutCreateInfo (..))
import Vulkan.Core10.Pipeline (ShaderStageFlagBits (..), PipelineShaderStageCreateInfo (..), PipelineVertexInputStateCreateInfo (..), VertexInputBindingDescription (..), VertexInputAttributeDescription (..))
import Foreign.Storable (Storable (sizeOf))
import GHC.Generics (Generic)
import qualified Data.ByteString.Lazy.Char8 as BL
import qualified Data.ByteString.Char8 as B
import Data.Aeson ((.=), (.:), (.:?), FromJSON (..), ToJSON (..), Value (String), decode, object, pairs, withObject, withText)
import Data.Aeson.Types (prependFailure, unexpected)
import Data.Text (Text)
import qualified Data.Text as T
import qualified Data.Text.IO as T
import Data.Vector (Vector)
import qualified Data.Vector as V
import System.FilePath ((</>))
import System.Process.Typed (proc, readProcess)
import System.IO.Temp (withSystemTempDirectory)
import System.IO.Unsafe (unsafePerformIO)
import Control.Monad.IO.Class (MonadIO, liftIO)
import Data.List (sortOn, groupBy)
import Data.Map (Map)
import qualified Data.Map as M
import Data.Function (on)
import Data.Maybe (fromMaybe, catMaybes)
import Data.Word (Word32)
import Control.Applicative (liftA2)
import Control.Monad (join)
import Control.Exception (Exception (..), throw, handle, evaluate)
data EntryPoint = EntryPoint
{ name :: Text
, mode :: ShaderStage
} deriving (Show, Generic, FromJSON, ToJSON)
data Input = Input
{ type' :: Text
, name :: Text
, location :: Int
, array :: Maybe (Vector Int)
} deriving (Show, Generic)
instance FromJSON Input where
parseJSON = withObject "inputs" $ \v -> Input
<$> v .: "type"
<*> v .: "name"
<*> v .: "location"
<*> v .:? "array"
instance ToJSON Input where
toJSON (Input type' name location array) = object
[ "type" .= type'
, "name" .= name
, "location" .= location
, "array" .= array
]
toEncoding (Input type' name location array) = pairs
( "type" .= type'
<> "name" .= name
<> "location" .= location
<> "array" .= array
)
data Ubo = Ubo
{ name :: Text
, set :: Int
, binding :: Int
, array :: Maybe (Vector Int)
} deriving (Show, Generic, FromJSON, ToJSON)
data Texture = Texture
{ type' :: Text
, name :: Text
, set :: Int
, binding :: Int
, array :: Maybe (Vector Int)
} deriving (Show, Generic)
instance FromJSON Texture where
parseJSON = withObject "textures" $ \v -> Texture
<$> v .: "type"
<*> v .: "name"
<*> v .: "set"
<*> v .: "binding"
<*> v .:? "array"
instance ToJSON Texture where
toJSON (Texture type' name set binding array) = object
[ "type" .= type'
, "name" .= name
, "set" .= set
, "binding" .= binding
, "array" .= array
]
toEncoding (Texture type' name set binding array) = pairs
( "type" .= type'
<> "name" .= name
<> "set" .= set
<> "binding" .= binding
<> "array" .= array
)
data Reflection = Reflection
{ entryPoints :: Vector EntryPoint
, inputs :: Maybe (Vector Input)
, textures :: Maybe (Vector Texture)
, ubos :: Maybe (Vector Ubo)
} deriving (Show, Generic, FromJSON, ToJSON)
data ConvertException = ConvertException Text Text
deriving (Show)
instance Exception ConvertException
class Convert a where
from :: Text -> a
to :: a -> Text
checkConvert :: forall a . Convert a => Text -> Bool
checkConvert x = unsafePerformIO $ handle (\(ConvertException _raw _err) -> pure True) (evaluate . flip seq False . (from :: Text -> a) $ x)
convertErrorMessage :: forall a . Convert a => Text -> Text
convertErrorMessage x = unsafePerformIO $ handle (\(ConvertException _raw err) -> pure err) (evaluate . flip seq "" . (from :: Text -> a) $ x)
data ShaderStage
= Vert
| Frag
| Comp
| Tesc
| Tese
| Geom
| Rgen
| Rint
| Rahit
| Rchit
| Rmiss
| Rcall
| Task
| Mesh
deriving (Eq, Show)
instance Convert ShaderStage where
from = \case
"vert" -> Vert
"frag" -> Frag
"comp" -> Comp
"tesc" -> Tesc
"tese" -> Tese
"geom" -> Geom
"rgen" -> Rgen
"rint" -> Rint
"rahit" -> Rahit
"rchit" -> Rchit
"rmiss" -> Rmiss
"rcall" -> Rcall
"task" -> Task
"mesh" -> Mesh
unsupport -> throw $ ConvertException unsupport $ "ShaderStage not support '" <> unsupport <> "'"
to = \case
Vert -> "vert"
Frag -> "frag"
Comp -> "comp"
Tesc -> "tesc"
Tese -> "tese"
Geom -> "geom"
Rgen -> "rgen"
Rint -> "rint"
Rahit -> "rahit"
Rchit -> "rchit"
Rmiss -> "rmiss"
Rcall -> "rcall"
Task -> "task"
Mesh -> "mesh"
instance FromJSON ShaderStage where
parseJSON value@(String x) | checkConvert @ShaderStage x = prependFailure (T.unpack . convertErrorMessage @ShaderStage $ x) . unexpected $ value
| otherwise = withText "mode" (pure . from) value
parseJSON x = withText "mode" (pure . from) x
instance ToJSON ShaderStage where
toJSON = String . to
toEncoding = toEncoding . to
convertStage :: ShaderStage -> ShaderStageFlagBits
convertStage = \case
Vert -> SHADER_STAGE_VERTEX_BIT
Frag -> SHADER_STAGE_FRAGMENT_BIT
Comp -> SHADER_STAGE_COMPUTE_BIT
Tesc -> SHADER_STAGE_TESSELLATION_CONTROL_BIT
Tese -> SHADER_STAGE_TESSELLATION_EVALUATION_BIT
Geom -> SHADER_STAGE_GEOMETRY_BIT
Rgen -> SHADER_STAGE_RAYGEN_BIT_KHR
Rint -> SHADER_STAGE_INTERSECTION_BIT_KHR
Rahit -> SHADER_STAGE_ANY_HIT_BIT_KHR
Rchit -> SHADER_STAGE_CLOSEST_HIT_BIT_KHR
Rmiss -> SHADER_STAGE_MISS_BIT_KHR
Rcall -> SHADER_STAGE_CALLABLE_BIT_KHR
Task -> SHADER_STAGE_TASK_BIT_NV
Mesh -> SHADER_STAGE_MESH_BIT_NV
data Shader = Shader
{ stage :: ShaderStage
, code :: B.ByteString
} deriving (Show)
data ShaderInfo = ShaderInfo
{ shaderModuleCreateInfo:: ShaderModuleCreateInfo '[]
, pipelineShaderStageCreateInfos :: ShaderModule -> Vector (SomeStruct PipelineShaderStageCreateInfo)
}
makeShaderInfo :: (Shader, Reflection) -> ShaderInfo
makeShaderInfo (Shader {..}, Reflection {..}) = ShaderInfo {..}
where
shaderModuleCreateInfo = makeShaderModuleCreateInfo code
pipelineShaderStageCreateInfos = (SomeStruct <$>) . makePipelineShaderStageCreateInfos stage entryPoints
makeDescriptorInfo :: Vector (Shader, Reflection) -> Vector (DescriptorSetLayoutCreateInfo '[])
makeDescriptorInfo = makeDescriptorSetLayoutCreateInfos . join . V.map (makeDescriptorSetLayoutBindings . (stage :: Shader -> ShaderStage) . fst <*> fromMaybe [] . ubos . snd <*> fromMaybe [] . textures . snd)
makeInputInfo :: Vector (Shader, Reflection) -> Maybe (SomeStruct PipelineVertexInputStateCreateInfo)
makeInputInfo = (SomeStruct <$>) . makePipelineVertexInputStateCreateInfo . join . V.fromList . catMaybes . (inputs . snd <$>) . filter ((== Vert) . (stage :: Shader -> ShaderStage) . fst) . V.toList
makeShaderModuleCreateInfo :: "code" ::: B.ByteString -> ShaderModuleCreateInfo '[]
makeShaderModuleCreateInfo code = zero { code = code }
makePipelineShaderStageCreateInfo :: ShaderStage -> ShaderModule -> EntryPoint -> PipelineShaderStageCreateInfo '[]
makePipelineShaderStageCreateInfo stage shaderModule EntryPoint {..} = zero
{ stage = convertStage stage
, module' = shaderModule
, name = B.pack . T.unpack $ name
}
makePipelineShaderStageCreateInfos :: ShaderStage -> Vector EntryPoint -> ShaderModule -> Vector (PipelineShaderStageCreateInfo '[])
makePipelineShaderStageCreateInfos stage entryPoints shaderModule = V.map (makePipelineShaderStageCreateInfo stage shaderModule) entryPoints
-- https://github.com/KhronosGroup/GLSL/blob/master/extensions/khr/GL_KHR_vulkan_glsl.txt
-- https://vulkan.lunarg.com/doc/view/1.2.135.0/linux/tutorial/html/08-init_pipeline_layout.html
makeUboDescriptorSetLayoutBinding :: ShaderStage -> Ubo -> (Int, DescriptorSetLayoutBinding)
makeUboDescriptorSetLayoutBinding stage Ubo {..} = (set, zero
{ binding = fromIntegral binding
, descriptorType = DESCRIPTOR_TYPE_UNIFORM_BUFFER
, descriptorCount = maybe 1 (V.sum . (fromIntegral <$>)) array
, stageFlags = convertStage stage
})
data TextureDescriptorType
= Sampler2D
instance Convert TextureDescriptorType where
from = \case
"sampler2D" -> Sampler2D
unsupport -> throw $ ConvertException unsupport $ "TextureDescriptorType not support '" <> unsupport <> "'"
to = \case
Sampler2D -> "sampler2D"
convertTextureDescriptorType :: TextureDescriptorType -> DescriptorType
convertTextureDescriptorType = \case
Sampler2D -> DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER
makeTextureDescriptorSetLayoutBinding :: ShaderStage -> Texture -> (Int, DescriptorSetLayoutBinding)
makeTextureDescriptorSetLayoutBinding stage Texture {..} = (set, zero
{ binding = fromIntegral binding
, descriptorType = convertTextureDescriptorType . from $ type'
, descriptorCount = maybe 1 (V.sum . (fromIntegral <$>)) array
, stageFlags = convertStage stage
})
makeDescriptorSetLayoutBindings :: ShaderStage -> Vector Ubo -> Vector Texture -> Vector (Int, DescriptorSetLayoutBinding)
makeDescriptorSetLayoutBindings stage ubos textures = uboBindings <> textureBindings
where
uboBindings = V.map (makeUboDescriptorSetLayoutBinding stage) ubos
textureBindings = V.map (makeTextureDescriptorSetLayoutBinding stage) textures
makeDescriptorSetLayoutCreateInfos :: Vector (Int, DescriptorSetLayoutBinding) -> V.Vector (DescriptorSetLayoutCreateInfo '[])
makeDescriptorSetLayoutCreateInfos bindings = V.map makeDescriptorSetLayoutCreateInfo . V.fromList . M.elems . M.unionWith (V.++) sets $ setsMap
where
setSize = V.maximum . (fst <$>) $ bindings :: Int
sets :: Map Int (Vector DescriptorSetLayoutBinding)
sets = M.fromList . map (, []) $ [ 0 .. setSize ]
setsMap :: Map Int (Vector DescriptorSetLayoutBinding)
setsMap = M.fromList . map (liftA2 (,) (fst . head) (V.fromList . (snd <$>))) . groupBy ((==) `on` fst) . sortOn fst . V.toList $ bindings
makeDescriptorSetLayoutCreateInfo :: Vector DescriptorSetLayoutBinding -> DescriptorSetLayoutCreateInfo '[]
makeDescriptorSetLayoutCreateInfo bindings = zero { bindings = bindings }
data VertexAttributeType
= Vec2
| Vec3
| Vec4
instance Convert VertexAttributeType where
from = \case
"vec2" -> Vec2
"vec3" -> Vec3
"vec4" -> Vec4
unsupport -> throw $ ConvertException unsupport $ "VertexAttributeType not support '" <> unsupport <> "'"
to = \case
Vec2 -> "vec2"
Vec3 -> "vec3"
Vec4 -> "vec4"
convertVertexAttributeType :: VertexAttributeType -> (Word32, Format)
convertVertexAttributeType = \case
Vec2 -> (fromIntegral $ 2 * sizeOf (undefined :: Word32), FORMAT_R32G32_SFLOAT)
Vec3 -> (fromIntegral $ 3 * sizeOf (undefined :: Word32), FORMAT_R32G32B32_SFLOAT)
Vec4 -> (fromIntegral $ 4 * sizeOf (undefined :: Word32), FORMAT_R32G32B32A32_SFLOAT)
data VertexAttribute = VertexAttribute
{ binding :: Word32
, location :: Word32
, size :: Word32
, format :: Format
} deriving (Show)
-- only support single binding for input vertex
-- [SaschaWillems - Multiple Vulkan buffer binding points](https://gist.github.com/SaschaWillems/428d15ed4b5d71ead462bc63adffa93a)
-- maybe can provide a binding map parameter to specify individual binding by name, like `foo [(1, ["inPos", "inColor"]) (2, ["texCoord"])]` speficies that `(binding = 1) inPos, (binding = 1) inColor, (binding = 2) texCoord`
-- [island pipeline](https://github.com/tgfrerer/island/blob/76d0d38cba74181fa3774cef38aba4d96b6861dc/modules/le_backend_vk/le_pipeline.cpp#L21)
makePipelineVertexInputStateCreateInfo :: Vector Input -> Maybe (PipelineVertexInputStateCreateInfo '[])
makePipelineVertexInputStateCreateInfo [] = Nothing
makePipelineVertexInputStateCreateInfo inputs = Just zero
{ vertexBindingDescriptions
, vertexAttributeDescriptions
}
where
vertexAttributes = join . V.map makeVertexAttribute $ inputs :: Vector VertexAttribute
vertexAttributeDescriptions = makeVertexInputAttributeDescriptions vertexAttributes :: Vector VertexInputAttributeDescription
vertexBindingDescriptions = makeVertexInputBindingDescriptions vertexAttributes :: Vector VertexInputBindingDescription
makeVertexInputBindingDescriptions :: Vector VertexAttribute -> Vector VertexInputBindingDescription
makeVertexInputBindingDescriptions = V.fromList . map makeVertexInputBindingDescription . calculate . groupBy ((==) `on` fst) . sortOn fst . extract
where
extract :: Vector VertexAttribute -> [ ("binding" ::: Int, "size" ::: Int) ]
extract = map ((,) . fromIntegral . (binding :: VertexAttribute -> Word32) <*> fromIntegral . size) . V.toList
calculate :: [ [ ("binding" ::: Int, "size" ::: Int) ] ] -> [ ("binding" ::: Int, "stride" ::: Int) ]
calculate = map (liftA2 (,) (fst . head) (sum . (snd <$>)))
makeVertexInputBindingDescription :: ("binding" ::: Int, "stride" ::: Int) -> VertexInputBindingDescription
makeVertexInputBindingDescription (binding, stride) = zero
{ binding = fromIntegral binding
, stride = fromIntegral stride
, inputRate = VERTEX_INPUT_RATE_VERTEX
}
makeVertexAttribute :: Input -> Vector VertexAttribute
makeVertexAttribute Input {..} = V.map (\i -> VertexAttribute
{ binding = 0
, location = fromIntegral . (+ location) $ i
, size
, format
})
[ 0 .. count - 1
]
where
count = maybe 1 V.sum array :: Int
(size, format) = convertVertexAttributeType . from $ type'
makeVertexInputAttributeDescriptions :: Vector VertexAttribute -> Vector VertexInputAttributeDescription
makeVertexInputAttributeDescriptions = V.fromList . join . map process . groupBy ((==) `on` (binding :: VertexAttribute -> Word32)) . V.toList
where
process :: [ VertexAttribute ] -> [ VertexInputAttributeDescription ]
process = snd . foldl (\(nextOffset, acc) cur -> liftA2 (,) fst ((acc ++) . pure . snd) (makeVertexInputAttributeDescription nextOffset cur)) (0, [])
makeVertexInputAttributeDescription :: Word32 -> VertexAttribute -> (Word32, VertexInputAttributeDescription)
makeVertexInputAttributeDescription offset VertexAttribute {..} = (offset + size, zero
{ binding
, location
, offset
, format
})
reflect :: MonadIO m => ShaderStage -> "code" ::: Text -> m (B.ByteString, BL.ByteString)
reflect shaderStage code = liftIO . withSystemTempDirectory "th-spirv" $ \dir -> do
let stage = T.unpack . to $ shaderStage
let shader = dir </> "glsl." <> stage
let spv = dir </> stage <> ".spv"
T.writeFile shader code
(_exitCode, _spv, _err) <- readProcess . proc "glslangValidator" $ [ "-S", stage, "-V", shader, "-o", spv]
spirv <- B.readFile spv
(_exitCode, reflectionRaw, _err) <- readProcess . proc "spirv-cross" $ [spv, "--vulkan-semantics", "--reflect"]
pure (spirv, reflectionRaw)
reflect' :: MonadIO m => "spirv" ::: B.ByteString -> m BL.ByteString
reflect' spirv = liftIO . withSystemTempDirectory "th-spirv" $ \dir -> do
let spv = dir </> "glsl.spv"
B.writeFile spv spirv
(_exitCode, reflectionRaw, _err) <- readProcess . proc "spirv-cross" $ [spv, "--vulkan-semantics", "--reflect"]
pure reflectionRaw
reflection :: MonadIO m => ShaderStage -> "code" ::: Text -> m (Shader, Reflection)
reflection stage code = do
(spirv, reflectionRaw) <- reflect stage code
case decode reflectionRaw of
Just reflection -> pure (Shader {stage, code = spirv}, reflection)
Nothing -> error "fail to reflect"
reflection' :: MonadIO m => "spirv" ::: B.ByteString -> m (Shader, Reflection)
reflection' spirv = do
reflectionRaw <- reflect' spirv
case decode reflectionRaw of
Just reflection -> pure (Shader {stage = getShaderStage reflection, code = spirv}, reflection)
Nothing -> error "fail to reflect"
getShaderStage :: Reflection -> ShaderStage
getShaderStage Reflection {..} = mode . V.head $ entryPoints