summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/LambdaCube/GL/Backend.hs58
1 files changed, 42 insertions, 16 deletions
diff --git a/src/LambdaCube/GL/Backend.hs b/src/LambdaCube/GL/Backend.hs
index 90cb014..0584a34 100644
--- a/src/LambdaCube/GL/Backend.hs
+++ b/src/LambdaCube/GL/Backend.hs
@@ -126,7 +126,7 @@ setupAccumulationContext (AccumulationContext n ops) = cvt ops
126 glDepthFunc $! comparisonFunctionToGLType df 126 glDepthFunc $! comparisonFunctionToGLType df
127 glDepthMask (cvtBool dm) 127 glDepthMask (cvtBool dm)
128 cvtC 0 xs 128 cvtC 0 xs
129 cvt xs = do 129 cvt xs = do
130 glDisable GL_DEPTH_TEST 130 glDisable GL_DEPTH_TEST
131 glDisable GL_STENCIL_TEST 131 glDisable GL_STENCIL_TEST
132 cvtC 0 xs 132 cvtC 0 xs
@@ -169,8 +169,8 @@ setupAccumulationContext (AccumulationContext n ops) = cvt ops
169 cvtBool True = 1 169 cvtBool True = 1
170 cvtBool False = 0 170 cvtBool False = 0
171 171
172clearRenderTarget :: [ClearImage] -> IO () 172clearRenderTarget :: GLRenderTarget -> [ClearImage] -> IO ()
173clearRenderTarget values = do 173clearRenderTarget GLRenderTarget{..} values = do
174 let setClearValue (m,i) value = case value of 174 let setClearValue (m,i) value = case value of
175 ClearImage Depth (VFloat v) -> do 175 ClearImage Depth (VFloat v) -> do
176 glDepthMask 1 176 glDepthMask 1
@@ -180,20 +180,46 @@ clearRenderTarget values = do
180 glClearStencil $ fromIntegral v 180 glClearStencil $ fromIntegral v
181 return (m .|. GL_STENCIL_BUFFER_BIT, i) 181 return (m .|. GL_STENCIL_BUFFER_BIT, i)
182 ClearImage Color c -> do 182 ClearImage Color c -> do
183 let (r,g,b,a) = case c of
184 VFloat r -> (realToFrac r, 0, 0, 1)
185 VV2F (V2 r g) -> (realToFrac r, realToFrac g, 0, 1)
186 VV3F (V3 r g b) -> (realToFrac r, realToFrac g, realToFrac b, 1)
187 VV4F (V4 r g b a) -> (realToFrac r, realToFrac g, realToFrac b, realToFrac a)
188 _ -> (0,0,0,1)
189 glColorMask 1 1 1 1 183 glColorMask 1 1 1 1
190 glClearColor r g b a 184 if framebufferObject == 0
191 return (m .|. GL_COLOR_BUFFER_BIT, i+1) 185 then
186 clearDefaultFB >>
187 pure (m .|. GL_COLOR_BUFFER_BIT, i+1)
188 else
189 clearFBColorAttachment >>
190 pure (m, i+1)
191 where
192 clearDefaultFB = do
193 let (r,g,b,a) = case c of
194 VFloat r -> (realToFrac r, 0, 0, 1)
195 VV2F (V2 r g) -> (realToFrac r, realToFrac g, 0, 1)
196 VV3F (V3 r g b) -> (realToFrac r, realToFrac g, realToFrac b, 1)
197 VV4F (V4 r g b a) -> (realToFrac r, realToFrac g, realToFrac b, realToFrac a)
198 _ -> (0,0,0,1)
199 glClearColor r g b a
200 clearFBColorAttachment = do
201 let buf = GL_COLOR
202 case c of -- there must be some clever way to extract the generality here, I'm sure..
203 VFloat r -> with (V4 r 0 0 1) $ glClearBufferfv buf i . castPtr
204 VV2F (V2 r g) -> with (V4 r g 0 1) $ glClearBufferfv buf i . castPtr
205 VV3F (V3 r g b) -> with (V4 r g b 1) $ glClearBufferfv buf i . castPtr
206 VV4F (V4 r g b a) -> with (V4 r g b a) $ glClearBufferfv buf i . castPtr
207
208 VInt r -> with (V4 r 0 0 1) $ glClearBufferiv buf i . castPtr
209 VV2I (V2 r g) -> with (V4 r g 0 1) $ glClearBufferiv buf i . castPtr
210 VV3I (V3 r g b) -> with (V4 r g b 1) $ glClearBufferiv buf i . castPtr
211 VV4I (V4 r g b a) -> with (V4 r g b a) $ glClearBufferiv buf i . castPtr
212
213 VWord r -> with (V4 r 0 0 1) $ glClearBufferiv buf i . castPtr
214 VV2U (V2 r g) -> with (V4 r g 0 1) $ glClearBufferiv buf i . castPtr
215 VV3U (V3 r g b) -> with (V4 r g b 1) $ glClearBufferiv buf i . castPtr
216 VV4U (V4 r g b a) -> with (V4 r g b a) $ glClearBufferiv buf i . castPtr
217 _ -> error $ "internal error: unsupported color attachment format: " <> show c
218
192 _ -> error "internal error (clearRenderTarget)" 219 _ -> error "internal error (clearRenderTarget)"
193 (mask,_) <- foldM setClearValue (0,0) values 220 (mask,_) <- foldM setClearValue (0,0) values
194 glClear $ fromIntegral mask 221 glClear $ fromIntegral mask
195 222
196
197printGLStatus = checkGL >>= print 223printGLStatus = checkGL >>= print
198printFBOStatus = checkFBO >>= print 224printFBOStatus = checkFBO >>= print
199 225
@@ -353,7 +379,7 @@ compileRenderTarget texs glTexs (RenderTarget targets) = do
353 | n > 1 -> attachArray 379 | n > 1 -> attachArray
354 | otherwise -> attach2D 380 | otherwise -> attach2D
355 TextureBuffer _ -> fail "internalError (compileRenderTarget/TextureBuffer)!" 381 TextureBuffer _ -> fail "internalError (compileRenderTarget/TextureBuffer)!"
356 382
357 go a (TargetItem Stencil (Just img)) = do 383 go a (TargetItem Stencil (Just img)) = do
358 fail "Stencil support is not implemented yet!" 384 fail "Stencil support is not implemented yet!"
359 return a 385 return a
@@ -462,7 +488,7 @@ createStreamCommands texUnitMap topUnis attrs primitive prg = streamUniCmds ++ s
462 488
463 -- object attribute stream commands 489 -- object attribute stream commands
464 streamCmds = [attrCmd i s | (i,name) <- Map.elems attrMap, let s = fromMaybe (error $ "missing attribute: " ++ name) $ Map.lookup name attrs] 490 streamCmds = [attrCmd i s | (i,name) <- Map.elems attrMap, let s = fromMaybe (error $ "missing attribute: " ++ name) $ Map.lookup name attrs]
465 where 491 where
466 attrMap = inputStreams prg 492 attrMap = inputStreams prg
467 attrCmd i s = case s of 493 attrCmd i s = case s of
468 Stream ty (Buffer arrs bo) arrIdx start len -> case ty of 494 Stream ty (Buffer arrs bo) arrIdx start len -> case ty of
@@ -592,7 +618,7 @@ isSubTrie eqFun universe subset = and [isMember a (Map.lookup n universe) | (n,a
592 when (sPrim /= (primitiveToFetchPrimitive prim)) $ throw $ userError $ 618 when (sPrim /= (primitiveToFetchPrimitive prim)) $ throw $ userError $
593 "Primitive mismatch for slot (" ++ show slotName ++ ") expected " ++ show sPrim ++ " but got " ++ show prim 619 "Primitive mismatch for slot (" ++ show slotName ++ ") expected " ++ show sPrim ++ " but got " ++ show prim
594 let sType = fmap streamToStreamType attribs 620 let sType = fmap streamToStreamType attribs
595 when (sType /= sAttrs) $ throw $ userError $ unlines $ 621 when (sType /= sAttrs) $ throw $ userError $ unlines $
596 [ "Attribute stream mismatch for slot (" ++ show slotName ++ ") expected " 622 [ "Attribute stream mismatch for slot (" ++ show slotName ++ ") expected "
597 , show sAttrs 623 , show sAttrs
598 , " but got " 624 , " but got "
@@ -809,7 +835,7 @@ renderFrame GLRenderer{..} = do
809 case cmd of 835 case cmd of
810 GLClearRenderTarget rt vals -> do 836 GLClearRenderTarget rt vals -> do
811 setupRenderTarget glInput rt 837 setupRenderTarget glInput rt
812 clearRenderTarget vals 838 clearRenderTarget rt vals
813 modifyIORef glDrawContextRef $ \ctx -> ctx {glRenderTarget = rt} 839 modifyIORef glDrawContextRef $ \ctx -> ctx {glRenderTarget = rt}
814 840
815 GLRenderStream ctx streamIdx progIdx -> do 841 GLRenderStream ctx streamIdx progIdx -> do