Skip to content

Commit e6fcc21

Browse files
committed
Added the missing parts of section 17.4 (Whole Framebuffer Operations).
1 parent b4db083 commit e6fcc21

File tree

1 file changed

+85
-46
lines changed

1 file changed

+85
-46
lines changed

src/Graphics/Rendering/OpenGL/GL/Framebuffer.hs

Lines changed: 85 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -29,21 +29,11 @@ module Graphics.Rendering.OpenGL.GL.Framebuffer (
2929
ClearBuffer(..), clear,
3030
clearColor, clearIndex, clearDepth, clearDepthf, clearStencil, clearAccum,
3131

32-
-- clearBufferiv,
33-
-- clearBufferfv,
34-
-- clearBufferuiv,
35-
-- clearNamedFramebufferiv,
36-
-- clearNamedFramebufferfv,
37-
-- clearNamedFramebufferuiv,
38-
39-
-- clearBufferfi,
40-
-- clearNamedFramebufferfi,
32+
ClearBufferCommand(..), clearBuffer, clearNamedFramebuffer,
4133

4234
-- * Invalidating Framebuffer Contents
43-
-- invalidateSubFramebuffer,
44-
-- invalidateNamedFramebufferSubData,
45-
-- invalidateFramebuffer,
46-
-- invalidateNamedFramebufferData,
35+
invalidateSubFramebuffer, invalidateNamedFramebufferSubData,
36+
invalidateFramebuffer, invalidateNamedFramebufferData,
4737

4838
-- * The Accumulation Buffer
4939
AccumOp(..), accum,
@@ -57,11 +47,15 @@ import Control.Monad
5747
import Data.Maybe
5848
import Data.StateVar
5949
import Foreign.Marshal.Array
60-
import Foreign.Ptr -- REMOVE ME ----------------------------------------------------------------------------------------------------
50+
import Foreign.Marshal.Utils
51+
import Foreign.Ptr
6152
import Graphics.Rendering.OpenGL.GL.BufferMode
6253
import Graphics.Rendering.OpenGL.GL.Capability
54+
import Graphics.Rendering.OpenGL.GL.CoordTrans
6355
import Graphics.Rendering.OpenGL.GL.Face
6456
import Graphics.Rendering.OpenGL.GL.FramebufferObjects.FramebufferObject
57+
import Graphics.Rendering.OpenGL.GL.FramebufferObjects.FramebufferObjectAttachment
58+
import Graphics.Rendering.OpenGL.GL.FramebufferObjects.FramebufferTarget
6559
import Graphics.Rendering.OpenGL.GL.GLboolean
6660
import Graphics.Rendering.OpenGL.GL.QueryUtils
6761
import Graphics.Rendering.OpenGL.GL.VertexSpec
@@ -345,49 +339,94 @@ clearAccum =
345339
makeStateVar (getFloat4 Color4 GetAccumClearValue)
346340
(\(Color4 r g b a) -> glClearAccum r g b a)
347341

348-
-- buffer = COLOR => drawbuffer i (= DRAW_BUFFERi, DrawBufferIndex), pointer to 4 elems
349-
-- buffer = DEPTH => drawbuffer must be 0, only "f" version allowed, 1 elem
350-
-- buffer = STENCIL => drawbuffer must be 0, only "i" version allowed, 1 elem
351-
352-
clearBufferiv :: GLenum -> GLint -> Ptr GLint -> IO ()
353-
clearBufferiv = glClearBufferiv
354-
355-
clearBufferfv :: GLenum -> GLint -> Ptr GLfloat -> IO ()
356-
clearBufferfv = glClearBufferfv
357-
358-
clearBufferuiv :: GLenum -> GLint -> Ptr GLuint -> IO ()
359-
clearBufferuiv = glClearBufferuiv
342+
-- | Describes which buffer(s) to clear and the value to use.
343+
344+
data ClearBufferCommand
345+
= ClearColorBufferInt DrawBufferIndex (Color4 GLint)
346+
-- ^ Clear the signed integer color buffer(s) at the given index.
347+
| ClearColorBufferFloat DrawBufferIndex (Color4 GLfloat)
348+
-- ^ Clear the fixed- or floating-point color buffer(s) at the given index.
349+
| ClearColorBufferUint DrawBufferIndex (Color4 GLuint)
350+
-- ^ Clear the unsigned color buffer(s) at the given index.
351+
| ClearDepthBuffer GLfloat
352+
-- ^ Clear the depth buffer.
353+
| ClearStencilBuffer GLint
354+
-- ^ Clear the stencil buffer.
355+
| ClearDepthAndStencilBuffers GLfloat GLint
356+
-- ^ Clear the depth buffer and the stencil buffer.
357+
deriving ( Eq, Ord, Show )
360358

361-
clearNamedFramebufferiv :: GLuint -> GLenum -> GLint -> Ptr GLint -> IO ()
362-
clearNamedFramebufferiv = glClearNamedFramebufferiv
359+
-- | Clear the given buffer(s).
360+
361+
clearBuffer :: ClearBufferCommand -> IO ()
362+
clearBuffer cmd = case cmd of
363+
ClearColorBufferInt i c ->
364+
with c $ glClearBufferiv gl_COLOR (fromIntegral i) . castPtr
365+
ClearColorBufferFloat i c ->
366+
with c $ glClearBufferfv gl_COLOR (fromIntegral i) . castPtr
367+
ClearColorBufferUint i c ->
368+
with c $ glClearBufferuiv gl_COLOR (fromIntegral i) . castPtr
369+
ClearDepthBuffer d ->
370+
with d $ glClearBufferfv gl_DEPTH 0
371+
ClearStencilBuffer s ->
372+
with s $ glClearBufferiv gl_STENCIL 0
373+
ClearDepthAndStencilBuffers d s ->
374+
glClearBufferfi gl_DEPTH_STENCIL 0 d s
375+
376+
-- | The direct-state-access version of 'clearBuffer'.
377+
378+
clearNamedFramebuffer :: FramebufferObject -> ClearBufferCommand -> IO ()
379+
clearNamedFramebuffer fbo cmd = case cmd of
380+
ClearColorBufferInt i c ->
381+
with c $ glClearNamedFramebufferiv f gl_COLOR (fromIntegral i) . castPtr
382+
ClearColorBufferFloat i c ->
383+
with c $ glClearNamedFramebufferfv f gl_COLOR (fromIntegral i) . castPtr
384+
ClearColorBufferUint i c ->
385+
with c $ glClearNamedFramebufferuiv f gl_COLOR (fromIntegral i) . castPtr
386+
ClearDepthBuffer d ->
387+
with d $ glClearNamedFramebufferfv f gl_DEPTH 0
388+
ClearStencilBuffer s ->
389+
with s $ glClearNamedFramebufferiv f gl_STENCIL 0
390+
ClearDepthAndStencilBuffers d s ->
391+
glClearNamedFramebufferfi f gl_DEPTH_STENCIL d s
392+
where f = framebufferID fbo
363393

364-
clearNamedFramebufferfv :: GLuint -> GLenum -> GLint -> Ptr GLfloat -> IO ()
365-
clearNamedFramebufferfv = glClearNamedFramebufferfv
394+
--------------------------------------------------------------------------------
366395

367-
clearNamedFramebufferuiv :: GLuint -> GLenum -> GLint -> Ptr GLuint -> IO ()
368-
clearNamedFramebufferuiv = glClearNamedFramebufferuiv
396+
-- | Invalidate a region of the attachments bound to the given target.
369397

370-
-- buffer must be DEPTH_STENCIL, drawbuffer must be 0, depth/stencil args
398+
invalidateSubFramebuffer :: FramebufferTarget -> [FramebufferObjectAttachment] -> (Position, Size) -> IO ()
399+
invalidateSubFramebuffer target attachments (Position x y, Size w h) =
400+
withAttachments attachments $ \numAttachments atts ->
401+
glInvalidateSubFramebuffer (marshalFramebufferTarget target) numAttachments atts x y w h
371402

372-
clearBufferfi :: GLenum -> GLint -> GLfloat -> GLint -> IO ()
373-
clearBufferfi = glClearBufferfi
403+
-- | The direct-state-access version of 'invalidateSubFramebuffer'.
374404

375-
clearNamedFramebufferfi :: GLuint -> GLenum -> GLfloat -> GLint -> IO ()
376-
clearNamedFramebufferfi = glClearNamedFramebufferfi
405+
invalidateNamedFramebufferSubData :: FramebufferObject -> [FramebufferObjectAttachment] -> (Position, Size) -> IO ()
406+
invalidateNamedFramebufferSubData fbo attachments (Position x y, Size w h) =
407+
withAttachments attachments $ \numAttachments atts ->
408+
glInvalidateNamedFramebufferSubData (framebufferID fbo) numAttachments atts x y w h
377409

378-
--------------------------------------------------------------------------------
410+
-- | A version of 'invalidateSubFramebuffer' affecting the whole viewport.
379411

380-
invalidateSubFramebuffer :: GLenum -> GLsizei -> Ptr GLenum -> GLint -> GLint -> GLsizei -> GLsizei -> IO ()
381-
invalidateSubFramebuffer = glInvalidateSubFramebuffer
412+
invalidateFramebuffer :: FramebufferTarget -> [FramebufferObjectAttachment] -> IO ()
413+
invalidateFramebuffer target attachments =
414+
withAttachments attachments $
415+
glInvalidateFramebuffer (marshalFramebufferTarget target)
382416

383-
invalidateNamedFramebufferSubData :: GLuint -> GLsizei -> Ptr GLenum -> GLint -> GLint -> GLsizei -> GLsizei -> IO ()
384-
invalidateNamedFramebufferSubData = glInvalidateNamedFramebufferSubData
417+
-- | The direct-state-access version of 'invalidateFramebuffer'.
385418

386-
invalidateFramebuffer :: GLenum -> GLsizei -> Ptr GLenum -> IO ()
387-
invalidateFramebuffer = glInvalidateFramebuffer
419+
invalidateNamedFramebufferData :: FramebufferObject -> [FramebufferObjectAttachment] -> IO ()
420+
invalidateNamedFramebufferData fbo attachments =
421+
withAttachments attachments $
422+
glInvalidateNamedFramebufferData (framebufferID fbo)
388423

389-
invalidateNamedFramebufferData :: GLuint -> GLsizei -> Ptr GLenum -> IO ()
390-
invalidateNamedFramebufferData = glInvalidateNamedFramebufferData
424+
withAttachments :: [FramebufferObjectAttachment] -> (GLsizei -> Ptr GLenum -> IO ()) -> IO ()
425+
withAttachments attachments success
426+
| all isJust atts = withArrayLen (catMaybes atts) $ \len buf ->
427+
success (fromIntegral len) buf
428+
| otherwise = recordInvalidEnum
429+
where atts = map marshalFramebufferObjectAttachment attachments
391430

392431
--------------------------------------------------------------------------------
393432

0 commit comments

Comments
 (0)