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

module Graphics.Rendering.OpenGL.GL.FramebufferObjects.FramebufferObjectAttachment (
   FramebufferObjectAttachment(..),
   marshalFramebufferObjectAttachment,
   unmarshalFramebufferObjectAttachment,
   unmarshalFramebufferObjectAttachmentSafe,
   fboaToBufferMode, fboaFromBufferMode,

   FramebufferAttachment(..), getFBAParameteriv
) where

import Data.Maybe
import Foreign.Marshal
import Graphics.Rendering.OpenGL.GL.BufferMode
import Graphics.Rendering.OpenGL.GL.FramebufferObjects.FramebufferTarget
import Graphics.Rendering.OpenGL.GL.PeekPoke
import Graphics.GL

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

data FramebufferObjectAttachment =
     ColorAttachment !GLuint
   | DepthAttachment
   | StencilAttachment
   | DepthStencilAttachment
   deriving ( FramebufferObjectAttachment -> FramebufferObjectAttachment -> Bool
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: FramebufferObjectAttachment -> FramebufferObjectAttachment -> Bool
$c/= :: FramebufferObjectAttachment -> FramebufferObjectAttachment -> Bool
== :: FramebufferObjectAttachment -> FramebufferObjectAttachment -> Bool
$c== :: FramebufferObjectAttachment -> FramebufferObjectAttachment -> Bool
Eq, Eq FramebufferObjectAttachment
FramebufferObjectAttachment -> FramebufferObjectAttachment -> Bool
FramebufferObjectAttachment
-> FramebufferObjectAttachment -> Ordering
FramebufferObjectAttachment
-> FramebufferObjectAttachment -> FramebufferObjectAttachment
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
min :: FramebufferObjectAttachment
-> FramebufferObjectAttachment -> FramebufferObjectAttachment
$cmin :: FramebufferObjectAttachment
-> FramebufferObjectAttachment -> FramebufferObjectAttachment
max :: FramebufferObjectAttachment
-> FramebufferObjectAttachment -> FramebufferObjectAttachment
$cmax :: FramebufferObjectAttachment
-> FramebufferObjectAttachment -> FramebufferObjectAttachment
>= :: FramebufferObjectAttachment -> FramebufferObjectAttachment -> Bool
$c>= :: FramebufferObjectAttachment -> FramebufferObjectAttachment -> Bool
> :: FramebufferObjectAttachment -> FramebufferObjectAttachment -> Bool
$c> :: FramebufferObjectAttachment -> FramebufferObjectAttachment -> Bool
<= :: FramebufferObjectAttachment -> FramebufferObjectAttachment -> Bool
$c<= :: FramebufferObjectAttachment -> FramebufferObjectAttachment -> Bool
< :: FramebufferObjectAttachment -> FramebufferObjectAttachment -> Bool
$c< :: FramebufferObjectAttachment -> FramebufferObjectAttachment -> Bool
compare :: FramebufferObjectAttachment
-> FramebufferObjectAttachment -> Ordering
$ccompare :: FramebufferObjectAttachment
-> FramebufferObjectAttachment -> Ordering
Ord, Int -> FramebufferObjectAttachment -> ShowS
[FramebufferObjectAttachment] -> ShowS
FramebufferObjectAttachment -> String
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [FramebufferObjectAttachment] -> ShowS
$cshowList :: [FramebufferObjectAttachment] -> ShowS
show :: FramebufferObjectAttachment -> String
$cshow :: FramebufferObjectAttachment -> String
showsPrec :: Int -> FramebufferObjectAttachment -> ShowS
$cshowsPrec :: Int -> FramebufferObjectAttachment -> ShowS
Show )

marshalFramebufferObjectAttachment :: FramebufferObjectAttachment -> Maybe GLenum
marshalFramebufferObjectAttachment :: FramebufferObjectAttachment -> Maybe GLuint
marshalFramebufferObjectAttachment FramebufferObjectAttachment
x = case FramebufferObjectAttachment
x of
   ColorAttachment GLuint
c -> let ec :: GLuint
ec = forall a b. (Integral a, Num b) => a -> b
fromIntegral GLuint
c in if GLuint
ec forall a. Ord a => a -> a -> Bool
>= GLuint
maxColorAttachments
      then forall a. Maybe a
Nothing
      else forall a. a -> Maybe a
Just forall a b. (a -> b) -> a -> b
$ GLuint
GL_COLOR_ATTACHMENT0 forall a. Num a => a -> a -> a
+ GLuint
ec
   FramebufferObjectAttachment
DepthAttachment -> forall a. a -> Maybe a
Just GLuint
GL_DEPTH_ATTACHMENT
   FramebufferObjectAttachment
StencilAttachment -> forall a. a -> Maybe a
Just GLuint
GL_STENCIL_ATTACHMENT
   FramebufferObjectAttachment
DepthStencilAttachment -> forall a. a -> Maybe a
Just GLuint
GL_DEPTH_STENCIL_ATTACHMENT

unmarshalFramebufferObjectAttachment :: GLenum -> FramebufferObjectAttachment
unmarshalFramebufferObjectAttachment :: GLuint -> FramebufferObjectAttachment
unmarshalFramebufferObjectAttachment GLuint
x = forall b a. b -> (a -> b) -> Maybe a -> b
maybe
   (forall a. HasCallStack => String -> a
error forall a b. (a -> b) -> a -> b
$ String
"unmarshalFramebufferObjectAttachment: unknown enum value " forall a. [a] -> [a] -> [a]
++ forall a. Show a => a -> String
show GLuint
x) forall a. a -> a
id forall a b. (a -> b) -> a -> b
$
      GLuint -> Maybe FramebufferObjectAttachment
unmarshalFramebufferObjectAttachmentSafe GLuint
x
--unmarshalFramebufferObjectAttachment x
--   | x == GL_DEPTH_ATTACHMENT = DepthAttachment
--   | x == GL_STENCIL_ATTACHMENT = StencilAttachment
--   | x == GL_DEPTH_STENCIL_ATTACHMENT = DepthStencilAttachment
--   | x >= gl_COLOR_ATTACHMENT0 && x <= gl_COLOR_ATTACHMENT15
--      = ColorAttachment . fromIntegral $ x - gl_COLOR_ATTACHMENT0
--   | otherwise = error $ "unmarshalFramebufferObjectAttachment: unknown enum value " ++ show x

unmarshalFramebufferObjectAttachmentSafe :: GLenum -> Maybe FramebufferObjectAttachment
unmarshalFramebufferObjectAttachmentSafe :: GLuint -> Maybe FramebufferObjectAttachment
unmarshalFramebufferObjectAttachmentSafe GLuint
x
   | GLuint
x forall a. Eq a => a -> a -> Bool
== GLuint
GL_DEPTH_ATTACHMENT = forall a. a -> Maybe a
Just FramebufferObjectAttachment
DepthAttachment
   | GLuint
x forall a. Eq a => a -> a -> Bool
== GLuint
GL_STENCIL_ATTACHMENT = forall a. a -> Maybe a
Just FramebufferObjectAttachment
StencilAttachment
   | GLuint
x forall a. Eq a => a -> a -> Bool
== GLuint
GL_DEPTH_STENCIL_ATTACHMENT = forall a. a -> Maybe a
Just FramebufferObjectAttachment
DepthStencilAttachment
   | GLuint
x forall a. Ord a => a -> a -> Bool
>= GLuint
GL_COLOR_ATTACHMENT0 Bool -> Bool -> Bool
&& GLuint
x forall a. Ord a => a -> a -> Bool
<= GLuint
GL_COLOR_ATTACHMENT0 forall a. Num a => a -> a -> a
+ GLuint
maxColorAttachments
      = forall a. a -> Maybe a
Just forall b c a. (b -> c) -> (a -> b) -> a -> c
. GLuint -> FramebufferObjectAttachment
ColorAttachment forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a b. (Integral a, Num b) => a -> b
fromIntegral forall a b. (a -> b) -> a -> b
$ GLuint
x forall a. Num a => a -> a -> a
- GLuint
GL_COLOR_ATTACHMENT0
   | Bool
otherwise = forall a. Maybe a
Nothing

fboaToBufferMode :: FramebufferObjectAttachment -> Maybe BufferMode
fboaToBufferMode :: FramebufferObjectAttachment -> Maybe BufferMode
fboaToBufferMode (ColorAttachment GLuint
i) = forall a. a -> Maybe a
Just forall b c a. (b -> c) -> (a -> b) -> a -> c
. GLsizei -> BufferMode
FBOColorAttachment forall a b. (a -> b) -> a -> b
$ forall a b. (Integral a, Num b) => a -> b
fromIntegral GLuint
i
fboaToBufferMode FramebufferObjectAttachment
_                   = forall a. Maybe a
Nothing

fboaFromBufferMode :: BufferMode -> Maybe FramebufferObjectAttachment
fboaFromBufferMode :: BufferMode -> Maybe FramebufferObjectAttachment
fboaFromBufferMode (FBOColorAttachment GLsizei
i) = forall a. a -> Maybe a
Just forall b c a. (b -> c) -> (a -> b) -> a -> c
. GLuint -> FramebufferObjectAttachment
ColorAttachment forall a b. (a -> b) -> a -> b
$ forall a b. (Integral a, Num b) => a -> b
fromIntegral GLsizei
i
fboaFromBufferMode BufferMode
_                      = forall a. Maybe a
Nothing

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

class Show a => FramebufferAttachment a where
   marshalAttachment :: a -> Maybe GLenum
   unmarshalAttachment :: GLenum -> a
   unmarshalAttachmentSafe :: GLenum -> Maybe a

instance FramebufferAttachment FramebufferObjectAttachment where
   marshalAttachment :: FramebufferObjectAttachment -> Maybe GLuint
marshalAttachment = FramebufferObjectAttachment -> Maybe GLuint
marshalFramebufferObjectAttachment
   unmarshalAttachment :: GLuint -> FramebufferObjectAttachment
unmarshalAttachment = GLuint -> FramebufferObjectAttachment
unmarshalFramebufferObjectAttachment
   unmarshalAttachmentSafe :: GLuint -> Maybe FramebufferObjectAttachment
unmarshalAttachmentSafe = GLuint -> Maybe FramebufferObjectAttachment
unmarshalFramebufferObjectAttachmentSafe

instance FramebufferAttachment BufferMode where
   marshalAttachment :: BufferMode -> Maybe GLuint
marshalAttachment = BufferMode -> Maybe GLuint
marshalBufferMode
   unmarshalAttachment :: GLuint -> BufferMode
unmarshalAttachment = GLuint -> BufferMode
unmarshalBufferMode
   unmarshalAttachmentSafe :: GLuint -> Maybe BufferMode
unmarshalAttachmentSafe = GLuint -> Maybe BufferMode
unmarshalBufferModeSafe

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

getFBAParameteriv :: FramebufferAttachment fba => FramebufferTarget -> fba
    -> (GLint -> a) -> GLenum -> IO a
getFBAParameteriv :: forall fba a.
FramebufferAttachment fba =>
FramebufferTarget -> fba -> (GLsizei -> a) -> GLuint -> IO a
getFBAParameteriv FramebufferTarget
fbt fba
fba GLsizei -> a
f GLuint
p = forall a b. Storable a => a -> (Ptr a -> IO b) -> IO b
with GLsizei
0 forall a b. (a -> b) -> a -> b
$ \Ptr GLsizei
buf -> do
   forall (m :: * -> *).
MonadIO m =>
GLuint -> GLuint -> GLuint -> Ptr GLsizei -> m ()
glGetFramebufferAttachmentParameteriv (FramebufferTarget -> GLuint
marshalFramebufferTarget FramebufferTarget
fbt)
      GLuint
mfba GLuint
p Ptr GLsizei
buf
   forall a b. Storable a => (a -> b) -> Ptr a -> IO b
peek1 GLsizei -> a
f Ptr GLsizei
buf
      where mfba :: GLuint
mfba = forall a. a -> Maybe a -> a
fromMaybe (forall a. HasCallStack => String -> a
error forall a b. (a -> b) -> a -> b
$ String
"invalid value" forall a. [a] -> [a] -> [a]
++ forall a. Show a => a -> String
show fba
fba) (forall a. FramebufferAttachment a => a -> Maybe GLuint
marshalAttachment fba
fba)