caramia-0.2.0.1: Less painful OpenGL 3.3 rendering

Safe HaskellNone
LanguageHaskell2010

Graphics.Caramia.Context

Contents

Description

This module manages OpenGL contexts in Caramia.

Caramia does not actually have any functionality about creating OpenGL contexts. You need to tell it about them with this module.

giveContext is the most important function in this module. You also want to runPendingFinalizers regularly to make sure OpenGL resources are garbage collected.

Synopsis

Running with an OpenGL context

giveContext :: IO a -> IO a Source

Tell Caramia the current thread has an OpenGL context active.

When the given IO action returns, Caramia will think that the OpenGL context is now gone. It is probably best to actually close the context because this also means OpenGL finalizers will not be run (Caramia thinks they were all released when the OpenGL context went away).

The context in the IO action is referred to as 'Caramia context' in this documentation to distinguish it from OpenGL context.

If the environment variable 'CARAMIA_OPENGL_DEBUG' is set, then, if 'GL_KHR_debug' extension is supported, OpenGL debug output is written. Note that you might need a debug OpenGL context for there to be any messages.

Throws TooOldOpenGL if the code detects a context that does not provide OpenGL 3.3.

Context IDs

currentContextID :: IO (Maybe ContextID) Source

Returns the current Caramia context ID.

The context ID is unique between different calls to giveContext.

Returns Nothing if there is no context active.

type ContextID = Int Source

The type of a Caramia context ID.

Finalization

runPendingFinalizers :: IO () Source

Run any pending finalizers in the current Caramia context.

Does nothing if current thread does not have a Caramia context or there are no pending finalizers.

If any finalizer throws an exception (asynchronous or synchronous), the Caramia context dies and that exception is propagated upwards. runPendingFinalizers itself runs mask_ to run the finalizers with exceptions masked but you might still receive asynchronous exceptions with, for example, the MVar functions.

A good place to call this is right after or before swapping buffers.

scheduleFinalizer :: ContextID -> IO () -> IO () Source

Schedules a finalizer to be run in a Caramia context.

Does nothing if given context is not alive anymore.

This is typically called from Haskell garbage collector finalizers because they cannot do finalization there (Haskell finalizers are running in the wrong operating system thread).

Context local data

storeContextLocalData :: Typeable a => a -> IO () Source

Stores a context local value.

The type of the given value is used as a key. This means that if a value of the same type was stored before, that value is thrown away and replaced with this new value you just gave.

The value is evaluated to WHNF.

You don't need this function to work with context local data. retrieveContextLocalData is sufficient as it also lets you set a default value in case a value was not already set.

Context local data is wiped to oblivion once giveContext ends.

retrieveContextLocalData Source

Arguments

:: forall a . Typeable a 
=> IO a

Default value generating action; not evaluated if there was already a value stored.

-> IO a 

Retrieves a context local value.

See storeContextLocalData.

Exceptions

data TooOldOpenGL Source

An exception that is thrown when the OpenGL version is too old for this library.

Constructors

TooOldOpenGL 

Fields

wantedVersion :: (Int, Int)

The OpenGL version this library needs.

reportedVersion :: (Int, Int)

The OpenGL version reported by current OpenGL context.