packages feed

lambdacube-engine-0.1.1: Graphics/LambdaCube/HardwareBuffer.hs

module Graphics.LambdaCube.HardwareBuffer where

import Data.Word
import Foreign.Ptr

-- | Enums describing buffer usage; not mutually exclusive
data Usage 
    {-| Static buffer which the application rarely modifies once created. Modifying 
    the contents of this buffer will involve a performance hit.
    -}
    = HBU_STATIC
    {-| Indicates the application would like to modify this buffer with the CPU
    fairly often. 
    Buffers created with this flag will typically end up in AGP memory rather 
    than video memory.
    -}
    | HBU_DYNAMIC
    {-| Indicates the application will never read the contents of the buffer back, 
    it will only ever write data. Locking a buffer with this flag will ALWAYS 
    return a pointer to new, blank memory rather than the memory associated 
    with the contents of the buffer; this avoids DMA stalls because you can 
    write to a new memory area while the previous one is being used. 
    -}
    | HBU_WRITE_ONLY
    {-| Indicates that the application will be refilling the contents
    of the buffer regularly (not just updating, but generating the
    contents from scratch), and therefore does not mind if the contents 
    of the buffer are lost somehow and need to be recreated. This
    allows and additional level of optimisation on the buffer.
    This option only really makes sense when combined with 
    HBU_DYNAMIC_WRITE_ONLY.
    -}
    | HBU_DISCARDABLE
    -- | Combination of HBU_STATIC and HBU_WRITE_ONLY
    | HBU_STATIC_WRITE_ONLY
    {-| Combination of HBU_DYNAMIC and HBU_WRITE_ONLY. If you use 
    this, strongly consider using HBU_DYNAMIC_WRITE_ONLY_DISCARDABLE
    instead if you update the entire contents of the buffer very 
    regularly. 
    -}
    | HBU_DYNAMIC_WRITE_ONLY
    -- | Combination of HBU_DYNAMIC, HBU_WRITE_ONLY and HBU_DISCARDABLE
    | HBU_DYNAMIC_WRITE_ONLY_DISCARDABLE
    deriving Eq

-- | Locking options
data LockOptions
    {-| Normal mode, ie allows read/write and contents are preserved. -}
    = HBL_NORMAL
    {-| Discards the <em>entire</em> buffer while locking; this allows optimisation to be 
    performed because synchronisation issues are relaxed. Only allowed on buffers 
    created with the HBU_DYNAMIC flag. 
    -}
    | HBL_DISCARD
    {-| Lock the buffer for reading only. Not allowed in buffers which are created with HBU_WRITE_ONLY. 
    Mandatory on static buffers, i.e. those created without the HBU_DYNAMIC flag. 
    -} 
    | HBL_READ_ONLY
    {-| As HBL_NORMAL, except the application guarantees not to overwrite any 
    region of the buffer which has already been used in this frame, can allow
    some optimisation on some APIs. -}
    | HBL_NO_OVERWRITE
    deriving Eq


class Eq a => HardwareBuffer a where
    {-| Lock the buffer for (potentially) reading / writing.
    @param offset The byte offset from the start of the buffer to lock
    @param length The size of the area to lock, in bytes
    @param options Locking options
    @returns Pointer to the locked memory
    -}
    lock                :: a -> Int -> Int -> LockOptions -> IO (Ptr Word8) -- virtual void* lock(size_t offset, size_t length, LockOptions options)

    {-| Releases the lock on this buffer. 
    @remarks 
        Locking and unlocking a buffer can, in some rare circumstances such as 
        switching video modes whilst the buffer is locked, corrupt the 
        contents of a buffer. This is pretty rare, but if it occurs, 
        this method will throw an exception, meaning you
        must re-upload the data.
    @par
        Note that using the 'read' and 'write' forms of updating the buffer does not
        suffer from this problem, so if you want to be 100% sure your
        data will not be lost, use the 'read' and 'write' forms instead.
    -}
    unlock              :: a -> IO () -- virtual void unlock(void)

    getSizeInBytes      :: a -> Int             -- ^ Returns the size of this buffer in bytes
    getUsage            :: a -> Usage           -- ^ Returns the Usage flags with which this buffer was created
    isSystemMemory      :: a -> Bool            -- ^ Returns whether this buffer is held in system memory
    hasShadowBuffer     :: a -> Bool            -- ^ Returns whether this buffer has a system memory shadow for quicker reading
    isLocked            :: a -> IO Bool         -- ^ Returns whether or not this buffer is currently locked.