ref: 4161ae98a0bd95edcd9a8f48c5daf019dd7164bb misc/reflex-gl-demo/src/Shader.hs -rw-r--r-- 5.1 KiB View raw
                                                                                
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
{-# LANGUAGE FlexibleContexts  #-}
{-# LANGUAGE LambdaCase        #-}
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE PatternSynonyms   #-}

module Shader (compile, link, watchShaderProgram) where

import           Prelude hiding (filter)

import           Control.Applicative (liftA2)
import           Control.Lens (pattern Strict, _1, view)
import           Control.Monad.IO.Class (MonadIO(..))
import           Data.ByteString (ByteString)
import qualified Data.ByteString.Char8 as B
import           Data.Foldable (traverse_)
import           Data.Functor (($>), (<&>))
import           Data.StateVar (($=))
import           Data.Witherable (catMaybes, filter)
import           Graphics.Glow (Program, Shader, ShaderType)
import qualified Graphics.Glow as Glow
import           Path
import           Reflex
import           Reflex.FSNotify
import qualified System.FSNotify as FS

-- | Create and compile a shader each time the event fires. When a
-- shader is successfully compiled, the previous shader is marked for
-- deletion and a new one is created.
compile
  :: (MonadHold t m, MonadIO m, PerformEvent t m, MonadIO (Performable m))
  => ShaderType
  -> Event t ByteString
  -> m (Event t (Either ByteString Shader))
compile sType eCode = do
  eeShader <- performEvent $ eCode <&> \c -> do
    shader <- Glow.createShader sType
    Glow.shaderSource shader $= Strict c
    Glow.compileShader shader
    Glow.compileStatus shader >>= \case
      True -> pure $ Right shader
      False -> do
        err <- Glow.shaderInfoLog shader
        Glow.delete shader
        pure $ Left err

  dShader <- holdDyn Nothing . fmap Just . filterRight $ eeShader

  -- When we get a new shader, delete the old one
  performEvent_ $ traverse_ Glow.delete <$> current dShader <@ updated dShader

  pure eeShader

-- | Link a shader program after each event has fired at least
-- once. When a program is successfully linked, the old one is
-- deleted.
link
  :: ( MonadHold t m
     , MonadIO (Performable m)
     , PerformEvent t m
     )
  => Event t Shader -- ^ Vertex Shader
  -> Event t Shader -- ^ Fragment Shader
  -> m (Event t (Either ByteString Program))
link eVs eFs = do
  dVs <- holdDyn Nothing $ Just <$> eVs
  dFs <- holdDyn Nothing $ Just <$> eFs

  let
    dmShaders = zipDynWith (liftA2 (,)) dVs dFs
    eShaders = catMaybes $ updated dmShaders

  eeProgram <- performEvent $ eShaders <&> \(vs, fs) -> do
    program <- Glow.gen
    Glow.attachShader program vs
    Glow.attachShader program fs
    Glow.linkProgram program
    Glow.linkStatus program >>= \case
      True -> pure $ Right program
      False -> do
        err <- Glow.programInfoLog program
        Glow.delete program
        pure $ Left err

  let eProgram = filterRight eeProgram
  dProgram <- holdDyn Nothing $ Just <$> eProgram

  -- When we get a new program, delete the old one
  performEvent_ $ traverse_ Glow.delete <$> current dProgram <@ updated dProgram

  pure eeProgram

watchShaderProgram
  :: ( MonadHold t m
     , MonadIO m
     , MonadIO (Performable m)
     , PerformEvent t m
     , PostBuild t m
     , TriggerEvent t m
     )
  => FS.WatchManager
  -> Path Abs Dir -- ^ Directory name
  -> Path Rel File -- ^ Vertex shader name (relative to dir)
  -> Path Rel File -- ^ Fragment shader name (relative to dir)
  -> m (Event t Program)
watchShaderProgram manager dir vsFileRel fsFileRel = do
  ePostBuild <- getPostBuild

  let
    vsFileAbs = toFilePath $ dir </> vsFileRel
    fsFileAbs = toFilePath $ dir </> fsFileRel

  -- Read the initial sources
  vsInitial <- liftIO $ B.readFile vsFileAbs
  fsInitial <- liftIO $ B.readFile fsFileAbs

  -- Watch the dir for changes to the files we're interested in. In
  -- this demo program, we only care about one of the constructors of
  -- a 'FS.Event'. This means we can get away with
  -- 'mapMaybe'.
  --
  -- Exercise: Learn how to use 'fan' to efficiently split an 'Event t
  -- FS.Event' into four different 'Event's.
  let
    onlyModifications (FS.Modified path mtime isDir) = Just (path, mtime, isDir)
    onlyModifications _ = Nothing
  eDirChanges <- fmap (view _1) . mapMaybe onlyModifications
    <$> watchDir manager (toFilePath dir)

  let
    eVsChanges = filter (vsFileAbs ==) eDirChanges
    eFsChanges = filter (fsFileAbs ==) eDirChanges
  eVsUpdates <- performEvent $ liftIO . B.readFile <$> eVsChanges
  eFsUpdates <- performEvent $ liftIO . B.readFile <$> eFsChanges

  -- Tie the shader sources together and compile
  eeVs <- compile Glow.VertexShader $ leftmost
    [ eVsUpdates
    , ePostBuild $> vsInitial
    ]
  eeFs <- compile Glow.FragmentShader $ leftmost
    [ eFsUpdates
    , ePostBuild $> fsInitial
    ]

  -- Report compile errors
  let
    (eVsError, eVs) = fanEither eeVs
    (eFsError, eFs) = fanEither eeFs
  performEvent_ $ eVsError <&> \err -> liftIO . B.putStrLn $
    "Vertex Shader Compile Error: " <> err
  performEvent_ $ eFsError <&> \err -> liftIO . B.putStrLn $
    "Fragment Shader Compile Error: " <> err

  -- Link and report link errors
  eeProg <- link eVs eFs
  let (eLinkError, eProg) = fanEither eeProg
  performEvent_ $ eLinkError <&> \err -> liftIO . B.putStrLn $
    "Shader Link Error: " <> err

  pure eProg