    {-# OPTIONS_HADDOCK hide #-}
    --------------------------------------------------------------------------------
    -- |
    -- Module      :  Graphics.Rendering.OpenGL.GL.BufferMode
    -- Copyright   :  (c) Sven Panne 2002-2019
    -- License     :  BSD3
    --
    -- Maintainer  :  Sven Panne <svenpanne@gmail.com>
    -- Stability   :  stable
    -- Portability :  portable
    --
    -- This is a purely internal module for (un-)marshaling BufferMode.
    --
    --------------------------------------------------------------------------------

    module Graphics.Rendering.OpenGL.GL.BufferMode (
       BufferMode(..), marshalBufferMode, unmarshalBufferMode,
       unmarshalBufferModeSafe,
       maxColorAttachments,
    ) where

    import Graphics.GL

    --------------------------------------------------------------------------------

    -- | The set of color buffers which are selected for reading and writing. Note
    -- that 'FBOColorAttachment' can only be used with framebuffer objects, while
    -- the rest can only be used with the default framebuffer. Furthermore, OpenGL
    -- 3.0 deprecated auxiliary buffers, so avoid 'AuxBuffer' in modern code.

    data BufferMode =
         NoBuffers
         -- ^ No color buffers are selected.
       | FrontLeftBuffer
         -- ^ Only the front left color buffer is selected.
       | FrontRightBuffer
         -- ^ Only the front right color buffer is selected.
       | BackLeftBuffer
         -- ^ Only the  back left color buffer is selected.
       | BackRightBuffer
         -- ^ Only the back right color buffer is selected.
       | FrontBuffers
         -- ^ Only the front left and front right color buffers are selected. If
         -- there is no front right color buffer, only the front left color buffer
         -- is selected.
       | BackBuffers
         -- ^ Only the back left and back right color buffers are selected. If there
         -- is no back right color buffer, only the back left color buffer is
         -- selected.
       | LeftBuffers
         -- ^ Only the front left and back left color buffers are selected. If there
         -- is no back left color buffer, only the front left color buffer is
         -- selected.
       | RightBuffers
         -- ^ Only the front right and back right color buffers are selected. If
         -- there is no back right color buffer, only the front right color buffer
         -- is selected.
       | FrontAndBackBuffers
         -- ^ All the front and back color buffers (front left, front right, back
         -- left, back right) are selected. If there are no back color buffers, only
         -- the front left and front right color buffers are selected. If there are
         -- no right color buffers, only the front left and back left color buffers
         -- are selected. If there are no right or back color buffers, only the
         -- front left color buffer is selected.
       | AuxBuffer GLsizei
         -- ^ Only the given auxiliary color buffer no. /i/ is selected.
       | FBOColorAttachment GLsizei
         -- ^ Only the given color attachment of the bound framebufferobject is selected for reading
         -- or writing.
       deriving ( BufferMode -> BufferMode -> Bool
    (BufferMode -> BufferMode -> Bool)
    -> (BufferMode -> BufferMode -> Bool) -> Eq BufferMode
    forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
    $c== :: BufferMode -> BufferMode -> Bool
    == :: BufferMode -> BufferMode -> Bool
    $c/= :: BufferMode -> BufferMode -> Bool
    /= :: BufferMode -> BufferMode -> Bool
    Eq, Eq BufferMode
    Eq BufferMode =>
    (BufferMode -> BufferMode -> Ordering)
    -> (BufferMode -> BufferMode -> Bool)
    -> (BufferMode -> BufferMode -> Bool)
    -> (BufferMode -> BufferMode -> Bool)
    -> (BufferMode -> BufferMode -> Bool)
    -> (BufferMode -> BufferMode -> BufferMode)
    -> (BufferMode -> BufferMode -> BufferMode)
    -> Ord BufferMode
    BufferMode -> BufferMode -> Bool
    BufferMode -> BufferMode -> Ordering
    BufferMode -> BufferMode -> BufferMode
    forall a.
    Eq a =>
    (a -> a -> Ordering)
    -> (a -> a -> Bool)
    -> (a -> a -> Bool)
    -> (a -> a -> Bool)
    -> (a -> a -> Bool)
    -> (a -> a -> a)
    -> (a -> a -> a)
    -> Ord a
    $ccompare :: BufferMode -> BufferMode -> Ordering
    compare :: BufferMode -> BufferMode -> Ordering
    $c< :: BufferMode -> BufferMode -> Bool
    < :: BufferMode -> BufferMode -> Bool
    $c<= :: BufferMode -> BufferMode -> Bool
    <= :: BufferMode -> BufferMode -> Bool
    $c> :: BufferMode -> BufferMode -> Bool
    > :: BufferMode -> BufferMode -> Bool
    $c>= :: BufferMode -> BufferMode -> Bool
    >= :: BufferMode -> BufferMode -> Bool
    $cmax :: BufferMode -> BufferMode -> BufferMode
    max :: BufferMode -> BufferMode -> BufferMode
    $cmin :: BufferMode -> BufferMode -> BufferMode
    min :: BufferMode -> BufferMode -> BufferMode
    Ord, Int -> BufferMode -> ShowS
    [BufferMode] -> ShowS
    BufferMode -> String
    (Int -> BufferMode -> ShowS)
    -> (BufferMode -> String)
    -> ([BufferMode] -> ShowS)
    -> Show BufferMode
    forall a.
    (Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
    $cshowsPrec :: Int -> BufferMode -> ShowS
    showsPrec :: Int -> BufferMode -> ShowS
    $cshow :: BufferMode -> String
    show :: BufferMode -> String
    $cshowList :: [BufferMode] -> ShowS
    showList :: [BufferMode] -> ShowS
    Show )

    marshalBufferMode :: BufferMode -> Maybe GLenum
    marshalBufferMode :: BufferMode -> Maybe GLenum
    marshalBufferMode BufferMode
    x = case BufferMode
    x of
       BufferMode
    NoBuffers -> GLenum -> Maybe GLenum
    forall a. a -> Maybe a
    Just GLenum
    GL_NONE
       BufferMode
    FrontLeftBuffer -> GLenum -> Maybe GLenum
    forall a. a -> Maybe a
    Just GLenum
    GL_FRONT_LEFT
       BufferMode
    FrontRightBuffer -> GLenum -> Maybe GLenum
    forall a. a -> Maybe a
    Just GLenum
    GL_FRONT_RIGHT
       BufferMode
    BackLeftBuffer -> GLenum -> Maybe GLenum
    forall a. a -> Maybe a
    Just GLenum
    GL_BACK_LEFT
       BufferMode
    BackRightBuffer -> GLenum -> Maybe GLenum
    forall a. a -> Maybe a
    Just GLenum
    GL_BACK_RIGHT
       BufferMode
    FrontBuffers -> GLenum -> Maybe GLenum
    forall a. a -> Maybe a
    Just GLenum
    GL_FRONT
       BufferMode
    BackBuffers -> GLenum -> Maybe GLenum
    forall a. a -> Maybe a
    Just GLenum
    GL_BACK
       BufferMode
    LeftBuffers -> GLenum -> Maybe GLenum
    forall a. a -> Maybe a
    Just GLenum
    GL_LEFT
       BufferMode
    RightBuffers -> GLenum -> Maybe GLenum
    forall a. a -> Maybe a
    Just GLenum
    GL_RIGHT
       BufferMode
    FrontAndBackBuffers -> GLenum -> Maybe GLenum
    forall a. a -> Maybe a
    Just GLenum
    GL_FRONT_AND_BACK
       AuxBuffer GLsizei
    i
          | GLsizei -> GLenum
    forall a b. (Integral a, Num b) => a -> b
    fromIntegral GLsizei
    i GLenum -> GLenum -> Bool
    forall a. Ord a => a -> a -> Bool
    <= GLenum
    maxAuxBuffer -> GLenum -> Maybe GLenum
    forall a. a -> Maybe a
    Just (GLenum
    GL_AUX0 GLenum -> GLenum -> GLenum
    forall a. Num a => a -> a -> a
    + GLsizei -> GLenum
    forall a b. (Integral a, Num b) => a -> b
    fromIntegral GLsizei
    i)
          | Bool
    otherwise -> Maybe GLenum
    forall a. Maybe a
    Nothing
       FBOColorAttachment GLsizei
    i
          | GLsizei -> GLenum
    forall a b. (Integral a, Num b) => a -> b
    fromIntegral GLsizei
    i GLenum -> GLenum -> Bool
    forall a. Ord a => a -> a -> Bool
    <= GLenum
    maxColorAttachments -> GLenum -> Maybe GLenum
    forall a. a -> Maybe a
    Just (GLenum
    GL_COLOR_ATTACHMENT0 GLenum -> GLenum -> GLenum
    forall a. Num a => a -> a -> a
    + GLsizei -> GLenum
    forall a b. (Integral a, Num b) => a -> b
    fromIntegral GLsizei
    i)
          | Bool
    otherwise -> Maybe GLenum
    forall a. Maybe a
    Nothing

    unmarshalBufferMode :: GLenum -> BufferMode
    unmarshalBufferMode :: GLenum -> BufferMode
    unmarshalBufferMode GLenum
    x = BufferMode
    -> (BufferMode -> BufferMode) -> Maybe BufferMode -> BufferMode
    forall b a. b -> (a -> b) -> Maybe a -> b
    maybe
       (String -> BufferMode
    forall a. HasCallStack => String -> a
    error (String
    "unmarshalBufferMode: illegal value " String -> ShowS
    forall a. [a] -> [a] -> [a]
    ++ GLenum -> String
    forall a. Show a => a -> String
    show GLenum
    x)) BufferMode -> BufferMode
    forall a. a -> a
    id (Maybe BufferMode -> BufferMode) -> Maybe BufferMode -> BufferMode
    forall a b. (a -> b) -> a -> b
    $ GLenum -> Maybe BufferMode
    unmarshalBufferModeSafe GLenum
    x

    unmarshalBufferModeSafe :: GLenum -> Maybe BufferMode
    unmarshalBufferModeSafe :: GLenum -> Maybe BufferMode
    unmarshalBufferModeSafe GLenum
    x
       | GLenum
    x GLenum -> GLenum -> Bool
    forall a. Eq a => a -> a -> Bool
    == GLenum
    GL_NONE = BufferMode -> Maybe BufferMode
    forall a. a -> Maybe a
    Just BufferMode
    NoBuffers
       | GLenum
    x GLenum -> GLenum -> Bool
    forall a. Eq a => a -> a -> Bool
    == GLenum
    GL_FRONT_LEFT = BufferMode -> Maybe BufferMode
    forall a. a -> Maybe a
    Just BufferMode
    FrontLeftBuffer
       | GLenum
    x GLenum -> GLenum -> Bool
    forall a. Eq a => a -> a -> Bool
    == GLenum
    GL_FRONT_RIGHT = BufferMode -> Maybe BufferMode
    forall a. a -> Maybe a
    Just BufferMode
    FrontRightBuffer
       | GLenum
    x GLenum -> GLenum -> Bool
    forall a. Eq a => a -> a -> Bool
    == GLenum
    GL_BACK_LEFT = BufferMode -> Maybe BufferMode
    forall a. a -> Maybe a
    Just BufferMode
    BackLeftBuffer
       | GLenum
    x GLenum -> GLenum -> Bool
    forall a. Eq a => a -> a -> Bool
    == GLenum
    GL_BACK_RIGHT = BufferMode -> Maybe BufferMode
    forall a. a -> Maybe a
    Just BufferMode
    BackRightBuffer
       | GLenum
    x GLenum -> GLenum -> Bool
    forall a. Eq a => a -> a -> Bool
    == GLenum
    GL_FRONT = BufferMode -> Maybe BufferMode
    forall a. a -> Maybe a
    Just BufferMode
    FrontBuffers
       | GLenum
    x GLenum -> GLenum -> Bool
    forall a. Eq a => a -> a -> Bool
    == GLenum
    GL_BACK = BufferMode -> Maybe BufferMode
    forall a. a -> Maybe a
    Just BufferMode
    BackBuffers
       | GLenum
    x GLenum -> GLenum -> Bool
    forall a. Eq a => a -> a -> Bool
    == GLenum
    GL_LEFT = BufferMode -> Maybe BufferMode
    forall a. a -> Maybe a
    Just BufferMode
    LeftBuffers
       | GLenum
    x GLenum -> GLenum -> Bool
    forall a. Eq a => a -> a -> Bool
    == GLenum
    GL_RIGHT = BufferMode -> Maybe BufferMode
    forall a. a -> Maybe a
    Just BufferMode
    RightBuffers
       | GLenum
    x GLenum -> GLenum -> Bool
    forall a. Eq a => a -> a -> Bool
    == GLenum
    GL_FRONT_AND_BACK = BufferMode -> Maybe BufferMode
    forall a. a -> Maybe a
    Just BufferMode
    FrontAndBackBuffers
       | GLenum
    GL_AUX0 GLenum -> GLenum -> Bool
    forall a. Ord a => a -> a -> Bool
    <= GLenum
    x Bool -> Bool -> Bool
    && GLenum
    x GLenum -> GLenum -> Bool
    forall a. Ord a => a -> a -> Bool
    <= GLenum
    GL_AUX0 GLenum -> GLenum -> GLenum
    forall a. Num a => a -> a -> a
    + GLenum
    maxAuxBuffer = BufferMode -> Maybe BufferMode
    forall a. a -> Maybe a
    Just (BufferMode -> Maybe BufferMode)
    -> (GLenum -> BufferMode) -> GLenum -> Maybe BufferMode
    forall b c a. (b -> c) -> (a -> b) -> a -> c
    . GLsizei -> BufferMode
    AuxBuffer (GLsizei -> BufferMode)
    -> (GLenum -> GLsizei) -> GLenum -> BufferMode
    forall b c a. (b -> c) -> (a -> b) -> a -> c
    . GLenum -> GLsizei
    forall a b. (Integral a, Num b) => a -> b
    fromIntegral (GLenum -> Maybe BufferMode) -> GLenum -> Maybe BufferMode
    forall a b. (a -> b) -> a -> b
    $ GLenum
    x GLenum -> GLenum -> GLenum
    forall a. Num a => a -> a -> a
    - GLenum
    GL_AUX0
       | GLenum
    GL_COLOR_ATTACHMENT0 GLenum -> GLenum -> Bool
    forall a. Ord a => a -> a -> Bool
    <= GLenum
    x Bool -> Bool -> Bool
    && GLenum
    x GLenum -> GLenum -> Bool
    forall a. Ord a => a -> a -> Bool
    <= GLenum
    GL_COLOR_ATTACHMENT0 GLenum -> GLenum -> GLenum
    forall a. Num a => a -> a -> a
    + GLenum
    maxColorAttachments
          = BufferMode -> Maybe BufferMode
    forall a. a -> Maybe a
    Just (BufferMode -> Maybe BufferMode)
    -> (GLenum -> BufferMode) -> GLenum -> Maybe BufferMode
    forall b c a. (b -> c) -> (a -> b) -> a -> c
    . GLsizei -> BufferMode
    FBOColorAttachment (GLsizei -> BufferMode)
    -> (GLenum -> GLsizei) -> GLenum -> BufferMode
    forall b c a. (b -> c) -> (a -> b) -> a -> c
    . GLenum -> GLsizei
    forall a b. (Integral a, Num b) => a -> b
    fromIntegral (GLenum -> Maybe BufferMode) -> GLenum -> Maybe BufferMode
    forall a b. (a -> b) -> a -> b
    $ GLenum
    x GLenum -> GLenum -> GLenum
    forall a. Num a => a -> a -> a
    - GLenum
    GL_COLOR_ATTACHMENT0
       | Bool
    otherwise = Maybe BufferMode
    forall a. Maybe a
    Nothing

    maxAuxBuffer :: GLenum
    maxAuxBuffer :: GLenum
    maxAuxBuffer = GLenum
    246

    maxColorAttachments :: GLenum
    maxColorAttachments :: GLenum
    maxColorAttachments = GLenum
    16