mischief-render 0.1.1.0 → 0.1.2.0
raw patch · 5 files changed
+59/−19 lines, 5 filesdep ~basePVP: major bump suggested
API removals or changes: PVP suggests a major version bump
Dependency ranges changed: base
API changes (from Hackage documentation)
+ Mischief.Render.Material.Pipeline: buildPipeline :: BindLayout -> VertexShader -> FragmentShader -> RenderDevice -> WGPUTextureFormat -> IO RenderPipeline
+ Mischief.Render.Material.Pipeline: type FragmentShader = Ptr WGPUShaderModule
+ Mischief.Render.Material.Pipeline: type VertexShader = Ptr WGPUShaderModule
+ Mischief.Render.Material.Pipeline: updateFragment :: Material mat => (VO mat GPU -> Shader mat (Vec4 Float)) -> System ()
+ Mischief.Shader: -- or not.
+ Mischief.Shader: -- | Set whether the relationships this component is used in are exclusive
+ Mischief.Shader: TBool :: PrimitiveTypes
+ Mischief.Shader: TFloat :: PrimitiveTypes
+ Mischief.Shader: TInt :: PrimitiveTypes
+ Mischief.Shader: TUInt :: PrimitiveTypes
+ Mischief.Shader: [ArrayType] :: Types -> Types
+ Mischief.Shader: [MType] :: Types -> Types
+ Mischief.Shader: [MatrixType] :: Nat -> Nat -> Types
+ Mischief.Shader: [Primitive] :: PrimitiveTypes -> Types
+ Mischief.Shader: [TSampler] :: Types
+ Mischief.Shader: [TTexture] :: Types
+ Mischief.Shader: [TVoid] :: Types
+ Mischief.Shader: [VectorType] :: Nat -> PrimitiveTypes -> Types
+ Mischief.Shader: data Expr (a :: Types)
+ Mischief.Shader: data PrimitiveTypes
+ Mischief.Shader: data Types
- Mischief.Shader: Shader :: State ShaderState a -> Shader (m :: k) a
+ Mischief.Shader: Shader :: State ShaderState a -> Shader m a
- Mischief.Shader: comment :: forall {k} (m :: k). Text -> Shader m ()
+ Mischief.Shader: comment :: Text -> Shader m ()
- Mischief.Shader: newtype Shader (m :: k) a
+ Mischief.Shader: newtype Shader m a
- Mischief.Shader: select :: forall {k} (a :: Types) (m :: k). SingI a => Expr ('Primitive 'TBool) -> Shader m (Expr a) -> Shader m (Expr a) -> Shader m (Expr a)
+ Mischief.Shader: select :: forall (a :: Types) m. SingI a => Expr ('Primitive 'TBool) -> Shader m (Expr a) -> Shader m (Expr a) -> Shader m (Expr a)
- Mischief.Shader: var :: forall {k} (a :: Types) (m :: k). SingI a => Expr a -> Shader m (Expr a)
+ Mischief.Shader: var :: forall (a :: Types) m. SingI a => Expr a -> Shader m (Expr a)
- Mischief.Shader.Functions: pick :: forall {k} (a :: Types) (m :: k). SingI a => [(Boolean, Shader m (Expr a))] -> Shader m (Expr a) -> Shader m (Expr a)
+ Mischief.Shader.Functions: pick :: forall (a :: Types) m. SingI a => [(Boolean, Shader m (Expr a))] -> Shader m (Expr a) -> Shader m (Expr a)
- Mischief.Shader.Functions: pickM :: forall {k} (a :: Types) (m :: k). SingI a => [(Shader m Boolean, Shader m (Expr a))] -> Shader m (Expr a) -> Shader m (Expr a)
+ Mischief.Shader.Functions: pickM :: forall (a :: Types) m. SingI a => [(Shader m Boolean, Shader m (Expr a))] -> Shader m (Expr a) -> Shader m (Expr a)
- Mischief.Shader.Generation: genFunction :: forall {k} a (m :: k). ShaderParam a => Text -> Text -> Text -> Map TypeRep ErasedValue -> Shader m (a GPU) -> Text
+ Mischief.Shader.Generation: genFunction :: ShaderParam a => Text -> Text -> Text -> Map TypeRep ErasedValue -> Shader m (a GPU) -> Text
- Mischief.Shader.Generation: genShader :: forall {k} (m :: k) p a. (ShaderParam p, ShaderParam a) => Text -> [ErasedBinding] -> (p GPU -> Shader m (a GPU)) -> Text
+ Mischief.Shader.Generation: genShader :: forall m p a. (ShaderParam p, ShaderParam a) => Text -> [ErasedBinding] -> (p GPU -> Shader m (a GPU)) -> Text
- Mischief.Shader.Generation: genShaderT :: forall {k} (m :: k) (b :: Type -> Type) a p. (Bindable b, ShaderParam p, ShaderParam a) => [ErasedBinding] -> (p GPU -> Shader m (a GPU)) -> String
+ Mischief.Shader.Generation: genShaderT :: forall m (b :: Type -> Type) a p. (Bindable b, ShaderParam p, ShaderParam a) => [ErasedBinding] -> (p GPU -> Shader m (a GPU)) -> String
- Mischief.Shader.Generation: shaderToWGSL :: forall {k} a (m :: k). ShaderParam a => Map TypeRep ErasedValue -> Shader m (a GPU) -> Text
+ Mischief.Shader.Generation: shaderToWGSL :: ShaderParam a => Map TypeRep ErasedValue -> Shader m (a GPU) -> Text
Files
- CHANGELOG.md +5/−0
- mischief-render.cabal +7/−7
- src/Mischief/Render/Material/Pipeline.hs +41/−11
- src/Mischief/Shader.hs +4/−0
- src/Mischief/Shader/Internal/State.hs +2/−1
CHANGELOG.md view
@@ -1,5 +1,10 @@ # Revision history for mischief-render +# 0.1.2.0 ++* Added `updateFragment` function for updating a material's fragment shader at runtime. +* Fixed a memory leak in relation to pipeline caching. + ## 0.1.1.0 * Added `windowToWorldPos` function.
mischief-render.cabal view
@@ -18,7 +18,7 @@ -- PVP summary: +-+------- breaking API changes -- | | +----- non-breaking API additions -- | | | +--- code changes with no API change-version: 0.1.1.0+version: 0.1.2.0 -- A short (one-line) description of the package. -- synopsis: -- A longer description of the package.@@ -137,7 +137,7 @@ -- Other library packages from which modules are imported. build-depends: async >=2.2.6 && <2.3,- base >=4.21.0.0 && <4.22,+ base ^>=4.21, binary >=0.8.9 && <0.9, bytestring >=0.12.2 && <0.13, containers >=0.7 && <0.8,@@ -221,7 +221,7 @@ -- Test dependencies. build-depends: async,- base,+ base ^>=4.21, bytestring, containers, data-default,@@ -246,7 +246,7 @@ hs-source-dirs: examples build-depends: async,- base,+ base ^>=4.21, bytestring, containers, data-default,@@ -321,7 +321,7 @@ hs-source-dirs: examples/ build-depends: async,- base,+ base ^>=4.21, bytestring, containers, data-default,@@ -360,7 +360,7 @@ build-depends: async,- base,+ base ^>=4.21, bytestring, containers, data-default,@@ -396,7 +396,7 @@ hs-source-dirs: examples/ build-depends: async,- base,+ base ^>=4.21, bytestring, containers, data-default,
src/Mischief/Render/Material/Pipeline.hs view
@@ -14,12 +14,14 @@ import Data.Set qualified as Set import Data.Text qualified as T import Data.Text.Encoding qualified as T+import Data.Traversable import Foreign (Ptr, nullPtr, with) import Foreign.C.ConstPtr import GHC.TypeLits import Language.Haskell.TH (Extension (AllowAmbiguousTypes)) import Mischief.Assets (Asset (..)) import Mischief.ECS+import Mischief.ECS.Resources (modifyRes, modifyRes') import Mischief.Math qualified as Math import Mischief.Render.Core import Mischief.Render.Material.Definition@@ -45,6 +47,27 @@ newtype PipelineCache = PipelineCache (Map PipelineKey RenderPipeline) deriving anyclass (Component) +-- TODO: this is just a workaround for the sake of live updating fragment shaders without needing a major version bump.++updateFragment :: forall mat. (Material mat) => (VO mat GPU -> Shader mat (Vec4 Mischief.Shader.Float)) -> System ()+updateFragment v = do+ Just (RenderDevice device) <- res+ bindLayout <- liftIO $ createBindLayout (Set.toList $ collectBindings @mat) (RenderDevice device)++ vertShader <- liftIO $ loadShaderFromBytes device (T.encodeUtf8 $ genShader @mat @(VI mat) @(VO mat) "vertex" (Set.toList $ collectBindings @mat) (vertex @mat))+ fragShader <- liftIO $ loadShaderFromBytes device (T.encodeUtf8 $ genShader @mat @(VO mat) @FragmentOutput "fragment" (Set.toList $ collectBindings @mat) (convertFrag v))++ cache <- res @PipelineCache++ for_ cache $ \(PipelineCache cache) -> do+ let e = filter (\(c, _) -> c.materialType Prelude.== typeRep (Proxy @mat)) . Map.toList $ cache+ -- let e = Map.toList cache+ e <- for e $ \(c, RenderPipeline p) -> do+ liftIO $ wgpuRenderPipelineRelease p+ (c,) <$> liftIO (buildPipeline bindLayout vertShader fragShader (RenderDevice device) c.format)++ insertRes $ PipelineCache $ foldr (\(k, a) c -> Map.insert k a c) cache e+ qgetPipeline :: forall mat. (Material mat) => TextureFormat -> Query System RenderPipeline qgetPipeline format' = do let format = toWGPUFormat format'@@ -60,12 +83,17 @@ case Map.lookup key cache of Just pipeline -> pure pipeline- Nothing -> liftIO $ createPipeline @mat device format+ Nothing -> do+ p <- liftIO $ createPipeline @mat device format+ insertRes $ PipelineCache $ Map.insert key p cache+ pure p -createPipeline :: forall mat. (Material mat) => RenderDevice -> WGPUTextureFormat -> IO RenderPipeline-createPipeline (RenderDevice device) format = do- BindLayout bindLayout <- createBindLayout (Set.toList $ collectBindings @mat) (RenderDevice device)+type VertexShader = Ptr WGPUShaderModule +type FragmentShader = Ptr WGPUShaderModule++buildPipeline :: BindLayout -> VertexShader -> FragmentShader -> RenderDevice -> WGPUTextureFormat -> IO RenderPipeline+buildPipeline (BindLayout bindLayout) vertShader fragShader (RenderDevice device) format = do pipelineLayoutDesc <- with bindLayout $ \bindGroupLayout -> do pure $ WGPUPipelineLayoutDescriptor@@ -79,13 +107,6 @@ pipelineLayout <- with pipelineLayoutDesc $ \pipelineLayoutDesc -> do wgpuDeviceCreatePipelineLayout device (ConstPtr {unConstPtr = pipelineLayoutDesc}) - -- putStrLn $ "------------------------------------------\n" ++ T.unpack (genShader @mat @(VO mat) @FragmentOutput "fragment" (collectBindings @mat) (convertFrag $ fragment @mat))-- -- putStrLn $ "------------------------------------------\n" ++ T.unpack (genShader @mat @(VI mat) @(VO mat) "vertex" (Set.toList $ collectBindings @mat) (vertex @mat))-- vertShader <- loadShaderFromBytes device (T.encodeUtf8 $ genShader @mat @(VI mat) @(VO mat) "vertex" (Set.toList $ collectBindings @mat) (vertex @mat))- fragShader <- loadShaderFromBytes device (T.encodeUtf8 $ genShader @mat @(VO mat) @FragmentOutput "fragment" (Set.toList $ collectBindings @mat) (convertFrag $ fragment @mat))- pipeline <- withWGPUString "main" $ \vertexEntry -> do withWGPUString "main" $ \fragmentEntry -> do withWGPUString "render pipeline" $ \label -> do@@ -143,3 +164,12 @@ with pipelineDesc $ \desc -> wgpuDeviceCreateRenderPipeline device desc pure $ RenderPipeline pipeline++createPipeline :: forall mat. (Material mat) => RenderDevice -> WGPUTextureFormat -> IO RenderPipeline+createPipeline (RenderDevice device) format = do+ bindLayout <- createBindLayout (Set.toList $ collectBindings @mat) (RenderDevice device)++ vertShader <- loadShaderFromBytes device (T.encodeUtf8 $ genShader @mat @(VI mat) @(VO mat) "vertex" (Set.toList $ collectBindings @mat) (vertex @mat))+ fragShader <- loadShaderFromBytes device (T.encodeUtf8 $ genShader @mat @(VO mat) @FragmentOutput "fragment" (Set.toList $ collectBindings @mat) (convertFrag $ fragment @mat))++ buildPipeline bindLayout vertShader fragShader (RenderDevice device) format
src/Mischief/Shader.hs view
@@ -18,6 +18,9 @@ module Mischief.Shader.Types, module Prelude, module Mischief.ECS,+ Expr,+ Types (..),+ PrimitiveTypes (..), ) where @@ -26,6 +29,7 @@ import Mischief.Shader.Internal.Bindings import Mischief.Shader.Internal.Buffers import Mischief.Shader.Internal.Params+import Mischief.Shader.Internal.Singletons import Mischief.Shader.Internal.State import Mischief.Shader.Types import Prelude (const, pure, show, undefined, ($), (.), (<$>), (<*>), (=<<), (>>=), type (~))
src/Mischief/Shader/Internal/State.hs view
@@ -7,6 +7,7 @@ import Control.Monad.State import Data.Data hiding (cast)+import Data.Kind (Type) import Data.Map (Map) import Data.Singletons (SingI, demote) import Data.Text (Text)@@ -291,7 +292,7 @@ -- stmtToWGSL (VLet a b) = " var " <> nameIndex a <> " = " <> exprToWGSL b <> ";" -newtype Shader m a = Shader (State ShaderState a) deriving newtype (Functor, Applicative, Monad, MonadState ShaderState)+newtype Shader (m :: Type) a = Shader (State ShaderState a) deriving newtype (Functor, Applicative, Monad, MonadState ShaderState) typeToWGSL :: forall (a :: Types). (SingI a) => Expr a -> Text typeToWGSL _ = typeToWGSL' (demote @a)