-----------------------------------------------------------------------------
-- |
-- Module      :  Graphics.Rendering.OpenGL.GL.FramebufferObjects.FramebufferObjects
-- Copyright   :  (c) Sven Panne 2011-2019, Lars Corbijn 2011-2016
-- License     :  BSD3
--
-- Maintainer  :  Sven Panne <svenpanne@gmail.com>
-- Stability   :  stable
-- Portability :  portable
--
-----------------------------------------------------------------------------

module Graphics.Rendering.OpenGL.GL.FramebufferObjects.FramebufferObjects (
   FramebufferObject, defaultFramebufferObject,
   FramebufferTarget(..), bindFramebuffer,
   FramebufferStatus(..), framebufferStatus,
) where

import Data.StateVar
import Graphics.Rendering.OpenGL.GL.FramebufferObjects.FramebufferObject
import Graphics.Rendering.OpenGL.GL.FramebufferObjects.FramebufferTarget
import Graphics.Rendering.OpenGL.GL.QueryUtils
import Graphics.GL

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

defaultFramebufferObject :: FramebufferObject
defaultFramebufferObject :: FramebufferObject
defaultFramebufferObject = GLuint -> FramebufferObject
FramebufferObject 0

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

bindFramebuffer :: FramebufferTarget -> StateVar FramebufferObject
bindFramebuffer :: FramebufferTarget -> StateVar FramebufferObject
bindFramebuffer fbt :: FramebufferTarget
fbt =
    IO FramebufferObject
-> (FramebufferObject -> IO ()) -> StateVar FramebufferObject
forall a. IO a -> (a -> IO ()) -> StateVar a
makeStateVar (FramebufferTarget -> IO FramebufferObject
getBoundFramebuffer FramebufferTarget
fbt) (FramebufferTarget -> FramebufferObject -> IO ()
setFramebuffer FramebufferTarget
fbt)

marshalFramebufferTargetBinding :: FramebufferTarget -> PName1I
marshalFramebufferTargetBinding :: FramebufferTarget -> PName1I
marshalFramebufferTargetBinding x :: FramebufferTarget
x = case FramebufferTarget
x of
   DrawFramebuffer -> PName1I
GetDrawFramebufferBinding
   ReadFramebuffer -> PName1I
GetReadFramebufferBinding
   Framebuffer -> PName1I
GetFramebufferBinding

getBoundFramebuffer :: FramebufferTarget -> IO FramebufferObject
getBoundFramebuffer :: FramebufferTarget -> IO FramebufferObject
getBoundFramebuffer =
   (GLint -> FramebufferObject) -> PName1I -> IO FramebufferObject
forall p a. GetPName1I p => (GLint -> a) -> p -> IO a
getInteger1 (GLuint -> FramebufferObject
FramebufferObject (GLuint -> FramebufferObject)
-> (GLint -> GLuint) -> GLint -> FramebufferObject
forall b c a. (b -> c) -> (a -> b) -> a -> c
. GLint -> GLuint
forall a b. (Integral a, Num b) => a -> b
fromIntegral) (PName1I -> IO FramebufferObject)
-> (FramebufferTarget -> PName1I)
-> FramebufferTarget
-> IO FramebufferObject
forall b c a. (b -> c) -> (a -> b) -> a -> c
. FramebufferTarget -> PName1I
marshalFramebufferTargetBinding

setFramebuffer :: FramebufferTarget -> FramebufferObject -> IO ()
setFramebuffer :: FramebufferTarget -> FramebufferObject -> IO ()
setFramebuffer fbt :: FramebufferTarget
fbt =
   GLuint -> GLuint -> IO ()
forall (m :: * -> *). MonadIO m => GLuint -> GLuint -> m ()
glBindFramebuffer (FramebufferTarget -> GLuint
marshalFramebufferTarget FramebufferTarget
fbt) (GLuint -> IO ())
-> (FramebufferObject -> GLuint) -> FramebufferObject -> IO ()
forall b c a. (b -> c) -> (a -> b) -> a -> c
. FramebufferObject -> GLuint
framebufferID

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

data FramebufferStatus =
     Complete
   | Undefined
   | IncompleteMissingAttachment
   | IncompleteDrawBuffer
   | IncompleteReadBuffer
   | IncompleteMultiSample
   | Unsupported
   deriving ( FramebufferStatus -> FramebufferStatus -> Bool
(FramebufferStatus -> FramebufferStatus -> Bool)
-> (FramebufferStatus -> FramebufferStatus -> Bool)
-> Eq FramebufferStatus
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: FramebufferStatus -> FramebufferStatus -> Bool
$c/= :: FramebufferStatus -> FramebufferStatus -> Bool
== :: FramebufferStatus -> FramebufferStatus -> Bool
$c== :: FramebufferStatus -> FramebufferStatus -> Bool
Eq, Eq FramebufferStatus
Eq FramebufferStatus =>
(FramebufferStatus -> FramebufferStatus -> Ordering)
-> (FramebufferStatus -> FramebufferStatus -> Bool)
-> (FramebufferStatus -> FramebufferStatus -> Bool)
-> (FramebufferStatus -> FramebufferStatus -> Bool)
-> (FramebufferStatus -> FramebufferStatus -> Bool)
-> (FramebufferStatus -> FramebufferStatus -> FramebufferStatus)
-> (FramebufferStatus -> FramebufferStatus -> FramebufferStatus)
-> Ord FramebufferStatus
FramebufferStatus -> FramebufferStatus -> Bool
FramebufferStatus -> FramebufferStatus -> Ordering
FramebufferStatus -> FramebufferStatus -> FramebufferStatus
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 :: FramebufferStatus -> FramebufferStatus -> FramebufferStatus
$cmin :: FramebufferStatus -> FramebufferStatus -> FramebufferStatus
max :: FramebufferStatus -> FramebufferStatus -> FramebufferStatus
$cmax :: FramebufferStatus -> FramebufferStatus -> FramebufferStatus
>= :: FramebufferStatus -> FramebufferStatus -> Bool
$c>= :: FramebufferStatus -> FramebufferStatus -> Bool
> :: FramebufferStatus -> FramebufferStatus -> Bool
$c> :: FramebufferStatus -> FramebufferStatus -> Bool
<= :: FramebufferStatus -> FramebufferStatus -> Bool
$c<= :: FramebufferStatus -> FramebufferStatus -> Bool
< :: FramebufferStatus -> FramebufferStatus -> Bool
$c< :: FramebufferStatus -> FramebufferStatus -> Bool
compare :: FramebufferStatus -> FramebufferStatus -> Ordering
$ccompare :: FramebufferStatus -> FramebufferStatus -> Ordering
$cp1Ord :: Eq FramebufferStatus
Ord, Int -> FramebufferStatus -> ShowS
[FramebufferStatus] -> ShowS
FramebufferStatus -> String
(Int -> FramebufferStatus -> ShowS)
-> (FramebufferStatus -> String)
-> ([FramebufferStatus] -> ShowS)
-> Show FramebufferStatus
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [FramebufferStatus] -> ShowS
$cshowList :: [FramebufferStatus] -> ShowS
show :: FramebufferStatus -> String
$cshow :: FramebufferStatus -> String
showsPrec :: Int -> FramebufferStatus -> ShowS
$cshowsPrec :: Int -> FramebufferStatus -> ShowS
Show )

unmarshalFramebufferStatus :: GLenum -> FramebufferStatus
unmarshalFramebufferStatus :: GLuint -> FramebufferStatus
unmarshalFramebufferStatus x :: GLuint
x
   | GLuint
x GLuint -> GLuint -> Bool
forall a. Eq a => a -> a -> Bool
== GLuint
GL_FRAMEBUFFER_COMPLETE = FramebufferStatus
Complete
   | GLuint
x GLuint -> GLuint -> Bool
forall a. Eq a => a -> a -> Bool
== GLuint
GL_FRAMEBUFFER_UNDEFINED = FramebufferStatus
Undefined
   | GLuint
x GLuint -> GLuint -> Bool
forall a. Eq a => a -> a -> Bool
== GLuint
GL_FRAMEBUFFER_INCOMPLETE_MISSING_ATTACHMENT
      = FramebufferStatus
IncompleteMissingAttachment
   | GLuint
x GLuint -> GLuint -> Bool
forall a. Eq a => a -> a -> Bool
== GLuint
GL_FRAMEBUFFER_INCOMPLETE_DRAW_BUFFER = FramebufferStatus
IncompleteDrawBuffer
   | GLuint
x GLuint -> GLuint -> Bool
forall a. Eq a => a -> a -> Bool
== GLuint
GL_FRAMEBUFFER_INCOMPLETE_READ_BUFFER = FramebufferStatus
IncompleteReadBuffer
   | GLuint
x GLuint -> GLuint -> Bool
forall a. Eq a => a -> a -> Bool
== GLuint
GL_FRAMEBUFFER_INCOMPLETE_MULTISAMPLE = FramebufferStatus
IncompleteMultiSample
   | GLuint
x GLuint -> GLuint -> Bool
forall a. Eq a => a -> a -> Bool
== GLuint
GL_FRAMEBUFFER_UNSUPPORTED = FramebufferStatus
Unsupported
   | Bool
otherwise = String -> FramebufferStatus
forall a. HasCallStack => String -> a
error (String -> FramebufferStatus) -> String -> FramebufferStatus
forall a b. (a -> b) -> a -> b
$ "unmarshalFramebufferStatus: unknown value: "
      String -> ShowS
forall a. [a] -> [a] -> [a]
++ GLuint -> String
forall a. Show a => a -> String
show GLuint
x

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

framebufferStatus :: FramebufferTarget -> GettableStateVar FramebufferStatus
framebufferStatus :: FramebufferTarget -> GettableStateVar FramebufferStatus
framebufferStatus t :: FramebufferTarget
t = GettableStateVar FramebufferStatus
-> GettableStateVar FramebufferStatus
forall a. IO a -> IO a
makeGettableStateVar (GettableStateVar FramebufferStatus
 -> GettableStateVar FramebufferStatus)
-> GettableStateVar FramebufferStatus
-> GettableStateVar FramebufferStatus
forall a b. (a -> b) -> a -> b
$ (GLuint -> FramebufferStatus)
-> IO GLuint -> GettableStateVar FramebufferStatus
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap GLuint -> FramebufferStatus
unmarshalFramebufferStatus
   (IO GLuint -> GettableStateVar FramebufferStatus)
-> (FramebufferTarget -> IO GLuint)
-> FramebufferTarget
-> GettableStateVar FramebufferStatus
forall b c a. (b -> c) -> (a -> b) -> a -> c
. GLuint -> IO GLuint
forall (m :: * -> *). MonadIO m => GLuint -> m GLuint
glCheckFramebufferStatus (GLuint -> IO GLuint)
-> (FramebufferTarget -> GLuint) -> FramebufferTarget -> IO GLuint
forall b c a. (b -> c) -> (a -> b) -> a -> c
. FramebufferTarget -> GLuint
marshalFramebufferTarget (FramebufferTarget -> GettableStateVar FramebufferStatus)
-> FramebufferTarget -> GettableStateVar FramebufferStatus
forall a b. (a -> b) -> a -> b
$ FramebufferTarget
t

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