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

    module Graphics.Rendering.OpenGL.GL.Texturing.Filter (
       TextureFilter(..),
       MinificationFilter, marshalMinificationFilter, unmarshalMinificationFilter,
       MagnificationFilter, marshalMagnificationFilter, unmarshalMagnificationFilter
    ) where

    import Graphics.GL

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

    data TextureFilter =
         Nearest
       | Linear'
       deriving ( TextureFilter -> TextureFilter -> Bool
    (TextureFilter -> TextureFilter -> Bool)
    -> (TextureFilter -> TextureFilter -> Bool) -> Eq TextureFilter
    forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
    $c== :: TextureFilter -> TextureFilter -> Bool
    == :: TextureFilter -> TextureFilter -> Bool
    $c/= :: TextureFilter -> TextureFilter -> Bool
    /= :: TextureFilter -> TextureFilter -> Bool
    Eq, Eq TextureFilter
    Eq TextureFilter =>
    (TextureFilter -> TextureFilter -> Ordering)
    -> (TextureFilter -> TextureFilter -> Bool)
    -> (TextureFilter -> TextureFilter -> Bool)
    -> (TextureFilter -> TextureFilter -> Bool)
    -> (TextureFilter -> TextureFilter -> Bool)
    -> (TextureFilter -> TextureFilter -> TextureFilter)
    -> (TextureFilter -> TextureFilter -> TextureFilter)
    -> Ord TextureFilter
    TextureFilter -> TextureFilter -> Bool
    TextureFilter -> TextureFilter -> Ordering
    TextureFilter -> TextureFilter -> TextureFilter
    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 :: TextureFilter -> TextureFilter -> Ordering
    compare :: TextureFilter -> TextureFilter -> Ordering
    $c< :: TextureFilter -> TextureFilter -> Bool
    < :: TextureFilter -> TextureFilter -> Bool
    $c<= :: TextureFilter -> TextureFilter -> Bool
    <= :: TextureFilter -> TextureFilter -> Bool
    $c> :: TextureFilter -> TextureFilter -> Bool
    > :: TextureFilter -> TextureFilter -> Bool
    $c>= :: TextureFilter -> TextureFilter -> Bool
    >= :: TextureFilter -> TextureFilter -> Bool
    $cmax :: TextureFilter -> TextureFilter -> TextureFilter
    max :: TextureFilter -> TextureFilter -> TextureFilter
    $cmin :: TextureFilter -> TextureFilter -> TextureFilter
    min :: TextureFilter -> TextureFilter -> TextureFilter
    Ord, Int -> TextureFilter -> ShowS
    [TextureFilter] -> ShowS
    TextureFilter -> String
    (Int -> TextureFilter -> ShowS)
    -> (TextureFilter -> String)
    -> ([TextureFilter] -> ShowS)
    -> Show TextureFilter
    forall a.
    (Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
    $cshowsPrec :: Int -> TextureFilter -> ShowS
    showsPrec :: Int -> TextureFilter -> ShowS
    $cshow :: TextureFilter -> String
    show :: TextureFilter -> String
    $cshowList :: [TextureFilter] -> ShowS
    showList :: [TextureFilter] -> ShowS
    Show )

    type MinificationFilter = (TextureFilter, Maybe TextureFilter)

    type MagnificationFilter = TextureFilter

    -- We treat MagnificationFilter as a degenerated case of MinificationFilter
    magToMin :: MagnificationFilter -> MinificationFilter
    magToMin :: TextureFilter -> MinificationFilter
    magToMin TextureFilter
    magFilter = (TextureFilter
    magFilter, Maybe TextureFilter
    forall a. Maybe a
    Nothing)

    minToMag :: MinificationFilter -> MagnificationFilter
    minToMag :: MinificationFilter -> TextureFilter
    minToMag (TextureFilter
    magFilter, Maybe TextureFilter
    Nothing) = TextureFilter
    magFilter
    minToMag MinificationFilter
    minFilter = String -> TextureFilter
    forall a. HasCallStack => String -> a
    error (String
    "minToMag: illegal value " String -> ShowS
    forall a. [a] -> [a] -> [a]
    ++ MinificationFilter -> String
    forall a. Show a => a -> String
    show MinificationFilter
    minFilter)

    marshalMinificationFilter :: MinificationFilter -> GLint
    marshalMinificationFilter :: MinificationFilter -> GLint
    marshalMinificationFilter MinificationFilter
    x = GLenum -> GLint
    forall a b. (Integral a, Num b) => a -> b
    fromIntegral (GLenum -> GLint) -> GLenum -> GLint
    forall a b. (a -> b) -> a -> b
    $ case MinificationFilter
    x of
       (TextureFilter
    Nearest, Maybe TextureFilter
    Nothing     ) -> GLenum
    GL_NEAREST
       (TextureFilter
    Linear', Maybe TextureFilter
    Nothing     ) -> GLenum
    GL_LINEAR
       (TextureFilter
    Nearest, Just TextureFilter
    Nearest) -> GLenum
    GL_NEAREST_MIPMAP_NEAREST
       (TextureFilter
    Linear', Just TextureFilter
    Nearest) -> GLenum
    GL_LINEAR_MIPMAP_NEAREST
       (TextureFilter
    Nearest, Just TextureFilter
    Linear') -> GLenum
    GL_NEAREST_MIPMAP_LINEAR
       (TextureFilter
    Linear', Just TextureFilter
    Linear') -> GLenum
    GL_LINEAR_MIPMAP_LINEAR

    marshalMagnificationFilter :: MagnificationFilter -> GLint
    marshalMagnificationFilter :: TextureFilter -> GLint
    marshalMagnificationFilter = MinificationFilter -> GLint
    marshalMinificationFilter (MinificationFilter -> GLint)
    -> (TextureFilter -> MinificationFilter) -> TextureFilter -> GLint
    forall b c a. (b -> c) -> (a -> b) -> a -> c
    . TextureFilter -> MinificationFilter
    magToMin

    unmarshalMinificationFilter :: GLint -> MinificationFilter
    unmarshalMinificationFilter :: GLint -> MinificationFilter
    unmarshalMinificationFilter GLint
    x
       | GLenum
    y GLenum -> GLenum -> Bool
    forall a. Eq a => a -> a -> Bool
    == GLenum
    GL_NEAREST = (TextureFilter
    Nearest, Maybe TextureFilter
    forall a. Maybe a
    Nothing)
       | GLenum
    y GLenum -> GLenum -> Bool
    forall a. Eq a => a -> a -> Bool
    == GLenum
    GL_LINEAR = (TextureFilter
    Linear', Maybe TextureFilter
    forall a. Maybe a
    Nothing)
       | GLenum
    y GLenum -> GLenum -> Bool
    forall a. Eq a => a -> a -> Bool
    == GLenum
    GL_NEAREST_MIPMAP_NEAREST = (TextureFilter
    Nearest, TextureFilter -> Maybe TextureFilter
    forall a. a -> Maybe a
    Just TextureFilter
    Nearest)
       | GLenum
    y GLenum -> GLenum -> Bool
    forall a. Eq a => a -> a -> Bool
    == GLenum
    GL_LINEAR_MIPMAP_NEAREST = (TextureFilter
    Linear', TextureFilter -> Maybe TextureFilter
    forall a. a -> Maybe a
    Just TextureFilter
    Nearest)
       | GLenum
    y GLenum -> GLenum -> Bool
    forall a. Eq a => a -> a -> Bool
    == GLenum
    GL_NEAREST_MIPMAP_LINEAR = (TextureFilter
    Nearest, TextureFilter -> Maybe TextureFilter
    forall a. a -> Maybe a
    Just TextureFilter
    Linear')
       | GLenum
    y GLenum -> GLenum -> Bool
    forall a. Eq a => a -> a -> Bool
    == GLenum
    GL_LINEAR_MIPMAP_LINEAR = (TextureFilter
    Linear', TextureFilter -> Maybe TextureFilter
    forall a. a -> Maybe a
    Just TextureFilter
    Linear')
       | Bool
    otherwise = String -> MinificationFilter
    forall a. HasCallStack => String -> a
    error (String
    "unmarshalMinificationFilter: illegal value " String -> ShowS
    forall a. [a] -> [a] -> [a]
    ++ GLint -> String
    forall a. Show a => a -> String
    show GLint
    x)
       where y :: GLenum
    y = GLint -> GLenum
    forall a b. (Integral a, Num b) => a -> b
    fromIntegral GLint
    x

    unmarshalMagnificationFilter :: GLint -> MagnificationFilter
    unmarshalMagnificationFilter :: GLint -> TextureFilter
    unmarshalMagnificationFilter = MinificationFilter -> TextureFilter
    minToMag (MinificationFilter -> TextureFilter)
    -> (GLint -> MinificationFilter) -> GLint -> TextureFilter
    forall b c a. (b -> c) -> (a -> b) -> a -> c
    . GLint -> MinificationFilter
    unmarshalMinificationFilter