-----------------------------------------------------------------------------
-- |
-- 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 = GLenum -> FramebufferObject
FramebufferObject GLenum
0

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

bindFramebuffer :: FramebufferTarget -> StateVar FramebufferObject
bindFramebuffer :: FramebufferTarget -> StateVar FramebufferObject
bindFramebuffer 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 FramebufferTarget
x = case FramebufferTarget
x of
   FramebufferTarget
DrawFramebuffer -> PName1I
GetDrawFramebufferBinding
   FramebufferTarget
ReadFramebuffer -> PName1I
GetReadFramebufferBinding
   FramebufferTarget
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
forall a. (GLint -> a) -> PName1I -> IO a
getInteger1 (GLenum -> FramebufferObject
FramebufferObject (GLenum -> FramebufferObject)
-> (GLint -> GLenum) -> GLint -> FramebufferObject
forall b c a. (b -> c) -> (a -> b) -> a -> c
. GLint -> GLenum
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 FramebufferTarget
fbt =
   GLenum -> GLenum -> IO ()
forall (m :: * -> *). MonadIO m => GLenum -> GLenum -> m ()
glBindFramebuffer (FramebufferTarget -> GLenum
marshalFramebufferTarget FramebufferTarget
fbt) (GLenum -> IO ())
-> (FramebufferObject -> GLenum) -> FramebufferObject -> IO ()
forall b c a. (b -> c) -> (a -> b) -> a -> c
. FramebufferObject -> GLenum
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
$c== :: FramebufferStatus -> FramebufferStatus -> Bool
== :: FramebufferStatus -> FramebufferStatus -> Bool
$c/= :: FramebufferStatus -> FramebufferStatus -> Bool
/= :: 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
$ccompare :: FramebufferStatus -> FramebufferStatus -> Ordering
compare :: FramebufferStatus -> FramebufferStatus -> Ordering
$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
>= :: FramebufferStatus -> FramebufferStatus -> Bool
$cmax :: FramebufferStatus -> FramebufferStatus -> FramebufferStatus
max :: FramebufferStatus -> FramebufferStatus -> FramebufferStatus
$cmin :: FramebufferStatus -> FramebufferStatus -> FramebufferStatus
min :: FramebufferStatus -> FramebufferStatus -> 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
$cshowsPrec :: Int -> FramebufferStatus -> ShowS
showsPrec :: Int -> FramebufferStatus -> ShowS
$cshow :: FramebufferStatus -> String
show :: FramebufferStatus -> String
$cshowList :: [FramebufferStatus] -> ShowS
showList :: [FramebufferStatus] -> ShowS
Show )

unmarshalFramebufferStatus :: GLenum -> FramebufferStatus
unmarshalFramebufferStatus :: GLenum -> FramebufferStatus
unmarshalFramebufferStatus GLenum
x
   | GLenum
x GLenum -> GLenum -> Bool
forall a. Eq a => a -> a -> Bool
== GLenum
GL_FRAMEBUFFER_COMPLETE = FramebufferStatus
Complete
   | GLenum
x GLenum -> GLenum -> Bool
forall a. Eq a => a -> a -> Bool
== GLenum
GL_FRAMEBUFFER_UNDEFINED = FramebufferStatus
Undefined
   | GLenum
x GLenum -> GLenum -> Bool
forall a. Eq a => a -> a -> Bool
== GLenum
GL_FRAMEBUFFER_INCOMPLETE_MISSING_ATTACHMENT
      = FramebufferStatus
IncompleteMissingAttachment
   | GLenum
x GLenum -> GLenum -> Bool
forall a. Eq a => a -> a -> Bool
== GLenum
GL_FRAMEBUFFER_INCOMPLETE_DRAW_BUFFER = FramebufferStatus
IncompleteDrawBuffer
   | GLenum
x GLenum -> GLenum -> Bool
forall a. Eq a => a -> a -> Bool
== GLenum
GL_FRAMEBUFFER_INCOMPLETE_READ_BUFFER = FramebufferStatus
IncompleteReadBuffer
   | GLenum
x GLenum -> GLenum -> Bool
forall a. Eq a => a -> a -> Bool
== GLenum
GL_FRAMEBUFFER_INCOMPLETE_MULTISAMPLE = FramebufferStatus
IncompleteMultiSample
   | GLenum
x GLenum -> GLenum -> Bool
forall a. Eq a => a -> a -> Bool
== GLenum
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
$ String
"unmarshalFramebufferStatus: unknown value: "
      String -> ShowS
forall a. [a] -> [a] -> [a]
++ GLenum -> String
forall a. Show a => a -> String
show GLenum
x

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

framebufferStatus :: FramebufferTarget -> GettableStateVar FramebufferStatus
framebufferStatus :: FramebufferTarget -> GettableStateVar FramebufferStatus
framebufferStatus 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
$ (GLenum -> FramebufferStatus)
-> IO GLenum -> GettableStateVar FramebufferStatus
forall a b. (a -> b) -> IO a -> IO b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap GLenum -> FramebufferStatus
unmarshalFramebufferStatus
   (IO GLenum -> GettableStateVar FramebufferStatus)
-> (FramebufferTarget -> IO GLenum)
-> FramebufferTarget
-> GettableStateVar FramebufferStatus
forall b c a. (b -> c) -> (a -> b) -> a -> c
. GLenum -> IO GLenum
forall (m :: * -> *). MonadIO m => GLenum -> m GLenum
glCheckFramebufferStatus (GLenum -> IO GLenum)
-> (FramebufferTarget -> GLenum) -> FramebufferTarget -> IO GLenum
forall b c a. (b -> c) -> (a -> b) -> a -> c
. FramebufferTarget -> GLenum
marshalFramebufferTarget (FramebufferTarget -> GettableStateVar FramebufferStatus)
-> FramebufferTarget -> GettableStateVar FramebufferStatus
forall a b. (a -> b) -> a -> b
$ FramebufferTarget
t

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