GPipe-2.1.8: Typesafe functional GPU graphics programming

Safe HaskellNone
LanguageHaskell98

Graphics.GPipe.Shader

Contents

Description

A Shader is a monad in which PrimitiveStreams and FragmentStreams live, together with samplers and uniform values. Any computations made on the streams and values in the Shader monad will be performed on the GPU. A Shader needs to be compiled before it can be used. In order to make it work over different environments after it has been compiled, it closes over an environment value just like a Reader monad, with the distinction that there is no ask action, since we cannot make the actual monad operation depend on the environment.

A Shader is an instance of Alternative and MonadPlus which makes it possible to express choice with functions like guard. The left most alternative will always be the resulting monad.

Synopsis

The Shader monad

data Shader os f s a Source

The monad in which all GPU computations are done. 'Shader os f s a' lives in an object space os and a context with format f, closing over an environent of type s.

Instances

compileShader :: (MonadIO m, MonadException m) => Shader os f x () -> ContextT w os f' m (CompiledShader os f x) Source

Compiles a shader into a CompiledShader. This action will usually take a second or more, so put it during a loading sequence or something.

May throw a GPipeException if the graphics driver doesn't support something in this shader (e.g. too many interpolated floats sent between a vertex and a fragment shader), or if shader evaluates to mzero.

withoutContext :: Render os () () -> Render os f () Source

Use this to run a shader that doesn't reference the context frame buffer, allowing the same shader to be run in another context with a different context format (but still with same object space).

type CompiledShader os f s = s -> Render os f () Source

A compiled shader is just a function that takes an environment and returns a Render action

The Render monad

data Render os f a Source

A monad in which shaders are run.

Instances

render :: (MonadIO m, MonadException m) => Render os f () -> ContextT w os f m () Source

Run a Render monad, that may have the effect of the context window or textures being drawn to.

May throw a GPipeException if a combination of draw images (FBO) used by this render call is unsupported by the graphics driver

Shader monad combinators

guard' :: (s -> Bool) -> Shader os f s () Source

Like guard, but dependent on the Shaders environment value. Since this will be evaluated at shader run time, as opposed to shader compile time for guard, using this to do recursion will make compileShader diverge. You can break that divergence by combining it with a normal guard and a maximum loop count.

mapShader :: (s -> s') -> Shader os f s' a -> Shader os f s a Source

Map the environment to a different environment and run a Shader in that sub environment, returning it's result.

maybeShader :: (s -> Maybe s') -> Shader os f s' () -> Shader os f s () Source

Conditionally run the effects of a shader when a Maybe value is Just something.

chooseShader :: (s -> Either s' s'') -> Shader os f s' a -> Shader os f s'' a -> Shader os f s a Source

Select one of two Shader actions based on whether an Either value is Left or Right.

silenceShader :: Shader os f' s a -> Shader os f s a Source

Discard all effects of a Shader action (i.e., dont draw anything) and just return the resulting value. This makes it possible to use a Shader written for a different context format.