summaryrefslogtreecommitdiff
path: root/src/Game/Internal/LoadShaders.hs
diff options
context:
space:
mode:
authormtgmonkey <mtgmonkey@nixos>2025-12-07 23:52:20 +0100
committermtgmonkey <mtgmonkey@nixos>2025-12-07 23:52:20 +0100
commit852244a49159097a83fc99edcdad26f84f999098 (patch)
treeb5dca9302d0352c513f1ad23a022a7a1a6b243e2 /src/Game/Internal/LoadShaders.hs
parentea56936a1529e776602186c50f7e7f587ecbf2de (diff)
add changelog; see changelog <3
Diffstat (limited to 'src/Game/Internal/LoadShaders.hs')
-rw-r--r--src/Game/Internal/LoadShaders.hs89
1 files changed, 89 insertions, 0 deletions
diff --git a/src/Game/Internal/LoadShaders.hs b/src/Game/Internal/LoadShaders.hs
new file mode 100644
index 0000000..ded2da9
--- /dev/null
+++ b/src/Game/Internal/LoadShaders.hs
@@ -0,0 +1,89 @@
+--------------------------------------------------------------------------------
+-- |
+-- Module : LoadShaders
+-- Copyright : (c) Sven Panne 2013
+-- License : BSD3
+--
+-- Maintainer : Sven Panne <svenpanne@gmail.com>
+-- Stability : stable
+-- Portability : portable
+--
+-- Utilities for shader handling, adapted from LoadShaders.cpp which is (c) The
+-- Red Book Authors.
+--
+--------------------------------------------------------------------------------
+
+module Game.Internal.LoadShaders (
+ ShaderSource(..), ShaderInfo(..), loadShaders
+) where
+
+import Control.Exception
+import Control.Monad
+import qualified Data.ByteString as B
+import Graphics.Rendering.OpenGL
+
+--------------------------------------------------------------------------------
+
+-- | The source of the shader source code.
+
+data ShaderSource =
+ ByteStringSource B.ByteString
+ -- ^ The shader source code is directly given as a 'B.ByteString'.
+ | StringSource String
+ -- ^ The shader source code is directly given as a 'String'.
+ | FileSource FilePath
+ -- ^ The shader source code is located in the file at the given 'FilePath'.
+ deriving ( Eq, Ord, Show )
+
+getSource :: ShaderSource -> IO B.ByteString
+getSource (ByteStringSource bs) = return bs
+getSource (StringSource str) = return $ packUtf8 str
+getSource (FileSource path) = B.readFile path
+
+--------------------------------------------------------------------------------
+
+-- | A description of a shader: The type of the shader plus its source code.
+
+data ShaderInfo = ShaderInfo ShaderType ShaderSource
+ deriving ( Eq, Ord, Show )
+
+--------------------------------------------------------------------------------
+
+-- | Create a new program object from the given shaders, throwing an
+-- 'IOException' if something goes wrong.
+
+loadShaders :: [ShaderInfo] -> IO Program
+loadShaders infos =
+ createProgram `bracketOnError` deleteObjectName $ \program -> do
+ loadCompileAttach program infos
+ linkAndCheck program
+ return program
+
+linkAndCheck :: Program -> IO ()
+linkAndCheck = checked linkProgram linkStatus programInfoLog "link"
+
+loadCompileAttach :: Program -> [ShaderInfo] -> IO ()
+loadCompileAttach _ [] = return ()
+loadCompileAttach program (ShaderInfo shType source : infos) =
+ createShader shType `bracketOnError` deleteObjectName $ \shader -> do
+ src <- getSource source
+ shaderSourceBS shader $= src
+ compileAndCheck shader
+ attachShader program shader
+ loadCompileAttach program infos
+
+compileAndCheck :: Shader -> IO ()
+compileAndCheck = checked compileShader compileStatus shaderInfoLog "compile"
+
+checked :: (t -> IO ())
+ -> (t -> GettableStateVar Bool)
+ -> (t -> GettableStateVar String)
+ -> String
+ -> t
+ -> IO ()
+checked action getStatus getInfoLog message object = do
+ action object
+ ok <- get (getStatus object)
+ unless ok $ do
+ infoLog <- get (getInfoLog object)
+ fail (message ++ " log: " ++ infoLog)