-----------------------------------------------------------------------------
-- |
-- 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 =
    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 =
   forall p a. GetPName1I p => (GLint -> a) -> p -> IO a
getInteger1 (GLenum -> FramebufferObject
FramebufferObject forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a b. (Integral a, Num b) => a -> b
fromIntegral) forall b c a. (b -> c) -> (a -> b) -> a -> c
. FramebufferTarget -> PName1I
marshalFramebufferTargetBinding

setFramebuffer :: FramebufferTarget -> FramebufferObject -> IO ()
setFramebuffer :: FramebufferTarget -> FramebufferObject -> IO ()
setFramebuffer FramebufferTarget
fbt =
   forall (m :: * -> *). MonadIO m => GLenum -> GLenum -> m ()
glBindFramebuffer (FramebufferTarget -> GLenum
marshalFramebufferTarget FramebufferTarget
fbt) 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
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
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
Ord, Int -> FramebufferStatus -> ShowS
[FramebufferStatus] -> ShowS
FramebufferStatus -> String
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 :: GLenum -> FramebufferStatus
unmarshalFramebufferStatus GLenum
x
   | GLenum
x forall a. Eq a => a -> a -> Bool
== GLenum
GL_FRAMEBUFFER_COMPLETE = FramebufferStatus
Complete
   | GLenum
x forall a. Eq a => a -> a -> Bool
== GLenum
GL_FRAMEBUFFER_UNDEFINED = FramebufferStatus
Undefined
   | GLenum
x forall a. Eq a => a -> a -> Bool
== GLenum
GL_FRAMEBUFFER_INCOMPLETE_MISSING_ATTACHMENT
      = FramebufferStatus
IncompleteMissingAttachment
   | GLenum
x forall a. Eq a => a -> a -> Bool
== GLenum
GL_FRAMEBUFFER_INCOMPLETE_DRAW_BUFFER = FramebufferStatus
IncompleteDrawBuffer
   | GLenum
x forall a. Eq a => a -> a -> Bool
== GLenum
GL_FRAMEBUFFER_INCOMPLETE_READ_BUFFER = FramebufferStatus
IncompleteReadBuffer
   | GLenum
x forall a. Eq a => a -> a -> Bool
== GLenum
GL_FRAMEBUFFER_INCOMPLETE_MULTISAMPLE = FramebufferStatus
IncompleteMultiSample
   | GLenum
x forall a. Eq a => a -> a -> Bool
== GLenum
GL_FRAMEBUFFER_UNSUPPORTED = FramebufferStatus
Unsupported
   | Bool
otherwise = forall a. HasCallStack => String -> a
error forall a b. (a -> b) -> a -> b
$ String
"unmarshalFramebufferStatus: unknown value: "
      forall a. [a] -> [a] -> [a]
++ forall a. Show a => a -> String
show GLenum
x

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

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

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